Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pyclic.py
blob: df42477ff4b5da739e2bb9fdd3e10a8fdf250c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#!/usr/bin/python

# PyqClicAdmin application, Administration interface. User loads an image, then places lables on them with circled
# numbers in colour of their choice (application has colour changing ability), then user writes meanings of labels on
# right hand side, and stores all this in an XML file. XML file contains tags as listed in pylic-common.py file,
# along with base 64 encoded image. This is for transportability reasons, specifically, transporting across xmpp
# tubes for collaboration. Application uses cairo and pango libraries for drawing and labeling image. It uses the
# minidom library for xml importing/exporting.

import pygtk
pygtk.require('2.0')
import gtk
import cairo
import math
from xml.dom import minidom
import os
import sys
from pyclic_common import *

class PyClicAdmin:
 
    #function to open xml file, though parsing of file happens in xml_load_from_file() function. 
    def on_file_open(self, widget):
	# use file_browse function to open file
	xml_file = self.pcc.file_browse(gtk.FILE_CHOOSER_ACTION_OPEN)
	# Check to see if we have a xml_file
	if (xml_file):
		if (self.xml_load_from_file(xml_file)):
			"""Allright it all worked! save the current file and
			set the title."""
			self.xml_file = xml_file
			# set window title to the name of loaded quiz
			self.pcc.set_window_title_from_file(self.xml_file,self.window)

    #Another in between file in the process of opening xml, basically this checks to see its really an xml document
    #and can be parsed by minidom, if not it will fail and tell user why. If successful sends it through to
    #xml_load which does the heavy parsing.
    def xml_load_from_file(self, xml_file):
	"""Load a quiz from an xml file
	@param xml_file - string - path to file that
	we will load.
	@returns boolean - True success. False failure
	"""
	#Init return value
	success = False

	#Load the xml_file to a document, show error if it fails
	try:
		xml_document = minidom.parse(xml_file)
		if (xml_document):
			success = ((self.xml_load(xml_document)))
	except IOError, (errno, strerror):
		#show error if not xml file or doesnt load.
		self.show_error_dlg(
			"Error loading quiz file(%s): %s" % (errno, strerror))
	return success

    # Heavy duty xml file parser. Stores false or true for all loaded objects, though currently unused in program
    # for anything. Title, Category, Author, Colours, Coordinates, Questions and Image are pulled in, parsed and placed
    # in their respective positions
    def xml_load(self, xml_document):
        """Load the current quiz from an xml document.
        @param xml_document - xml.dom.minidom.Document object -
        The xml document that we will load the quiz from.
        @returns boolean True - success.  False - Failure."""

        title_loaded = False
        category_loaded = False
        question_loaded = False
        author_loaded = False
	image_loaded = False
	coordinates_loaded = False
	entries_loaded = False
	questions_loaded = False
	colors_loaded = False
	# reset all values before loading document, make sure data is clean, and drawing area is clean
	self.on_file_new(self)
        #Loop through all child nodes of the root.
        for node in xml_document.documentElement.childNodes:
                #We are looking for the quiz Node, which will contain all the subelements title, author, category
		#buttons color, and image. Basically this is seperated as such because it needs to do just one
		#iteration per quiz document.
                if (node.nodeName == PyClicCommon._xml_tags[QUIZ_TAG]):
                        # Now loop through the quiz nodes children
                        for item_node in node.childNodes:
                                if (item_node.nodeName == PyClicCommon._xml_tags[TITLE_TAG]):
                                        """Set the title, the firstChild in this case is
                                        the actual title text that we saved."""

                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
                                                self.title_entry.set_text(item_node.firstChild.nodeValue)
                                        else:
                                                print "Tag not loaded, probably empty"
                                        title_loaded = True
				# Title Tag loaded at this point and placed by self.title_entry.set_text
                                elif (item_node.nodeName == PyClicCommon._xml_tags[CATEGORY_TAG]):
                                        """Set the category, the firstChild in this case is
                                        the actual category that we saved."""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
						#we make quite a hacky loop through the listbox, since gtk
						#doesnt have very nice manipulation of listboxes, and uses
						#just number indexing.
						if item_node.firstChild.nodeValue=="Geography":
							catindex=0
						elif item_node.firstChild.nodeValue=="Science":
							catindex=1
						elif item_node.firstChild.nodeValue=="ICT":
							catindex=2
						elif item_node.firstChild.nodeValue=="Languages":
							catindex=3
						elif item_node.firstChild.nodeValue=="Maths":
							catindex=4
						elif item_node.firstChild.nodeValue=="Music":
							catindex=5
						elif item_node.firstChild.nodeValue=="History":
							catindex=6
						elif item_node.firstChild.nodeValue=="Business":
							catindex=7
						elif item_node.firstChild.nodeValue=="Art":
							catindex=8
						#we set the active listbox element to category it found
						self.cb_category.set_active(catindex)
                                        else:
                                                print "Tag not loaded, probably empty"
                                        category_loaded = True
				#At this point we have found category and title and have placed them in their
				#their corresponding positions
                                elif (item_node.nodeName == PyClicCommon._xml_tags[AUTHOR_TAG]):
                                        """Set the author, the firstChild in this case is
                                        the actual author that we saved. This tag is not really that important
					and in fact was put in as an after though just in case its wanted"""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
                                                self.author_entry.set_text(item_node.firstChild.nodeValue)
                                        else:
                                                print "Tag not loaded, probably empty"
                                        author_loaded = True
                       	        #At this point category, title and author have been loaded and placed in the document
                                elif (item_node.nodeName == PyClicCommon._xml_tags[IMAGE_TAG]):
                                        """Set the image, the firstChild in this case is
                                        the image that we saved. Then decode from base64"""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
						#we set self.image equal to the decoding of the bin64 image, then
						#issue a queue draw to let X redraw the drawing area.
                                                self.image = self.pcc.decode64(item_node.firstChild.nodeValue)
						self.window.queue_draw()
                                        else:
                                                print "Tag not loaded, probably empty"
                                        image_loaded = True
                       	        #Weve now loaded the image, the category, the title and the author and placed
				#them in their corresponding positions.
                                elif (item_node.nodeName == PyClicCommon._xml_tags[COLOR_TAG]):
                                        """Set the image, the firstChild in this case is
                                        the image that we saved. Then decode from base64"""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
						#since weve saved the colors as a string, as its xml, and
						#all xml elements must be strings, we need to pull out those
						#values and make them proper floats to get exact colour values
						#aswell as split into the 3 corresponsding R, G and B. First we
						#split the values at the ,
						self.colourred,self.colourgreen,self.colourblue = item_node.firstChild.nodeValue.split(",")
						#now we replace the [ with nothing as that got pulled in on save on
						#conversion of list to string
						self.colorred=self.colourred.replace('[', '')
						self.colorred=float(self.colorred)
						self.colorgreen=float(self.colourgreen)
						#we do the same replacement for the outer bracket, and now have a
						# proper list again, split into its R, G, B components, as floats
						self.colorblue=self.colourblue.replace(']', '')
						self.colorblue=float(self.colorblue)
						#we send the color values to X to be redrawn.
                                                self.window.queue_draw()
                                        else:
                                                print "Tag not loaded, probably empty"
                                        colors_loaded = True
                        #We've now loaded all the QUIZ tag elements, the ones that require just one iteration.
		#We are looking for the questions Node, which contains all the iterrable values, that is to say
		#values for each question found.
                elif (node.nodeName == PyClicCommon._xml_tags[QUESTIONS_TAG]):
                        # Now loop through the question nodes children
                        for item_node in node.childNodes:
				#We create an empty questions list to iterate through and place items in
				self.questions_list=[]
				#we look for the question tag
				if (item_node.nodeName == PyClicCommon._xml_tags[QUESTION_TAG]):
                                        """Grab all the questions."""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
						#for each question, append it to the questions_list
                                                self.questions_list.append(item_node.firstChild.nodeValue)
						#now generate the questions labels show it
						for question in self.questions_list:
							self.label=gtk.Label(question)
							self.label_list.append(self.label)
							self.vbox2.pack_start(self.label,False,False,0)
							self.label.show()
                                        else:
                                                print "Tag not loaded, probably empty"
                                        question_loaded = True
				#create empty self.coordinate list to place coordinates into
				self.coordinate=[]
				# Look for coordinates tag
				if (item_node.nodeName == PyClicCommon._xml_tags[COORDINATES_TAG]):
                                        """Grab all the coordinates."""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
						#add all coordinates to list
                                                self.coordinate.append(item_node.firstChild.nodeValue)
						# but split x and y and turn into proper floats and point to proper
						# coordinates list, coordinate, is just used for iteration here.
						for coordinate in self.coordinate:
							x,y = coordinate.split(",")
							self.coordinates.append((float(x),float(y)))
							# Get X to place or coordinates in the drawin area
							self.window.queue_draw()	
                                        else:
                                                print "Tag not loaded, probably empty"
                                        coordinates_loaded = True
				# At this point all our question labels and coordinates
				# have been loaded and placed. We create a entries
				#iterator list called entradas.					
				self.entradas=[]
				#we look for entry tag
				if (item_node.nodeName == PyClicCommon._xml_tags[ENTRY_TAG]):
                                        """Grab all the entries."""
                                        #Make sure it's not a blank string
                                        if (item_node.firstChild):
						#iterate through entries
                                                self.entradas.append(item_node.firstChild.nodeValue)
						# now pull out values and place them in entry boxes, generating
						# an entry box for each value
						for entry in self.entradas:
							self.entry=gtk.Entry(max=30)
							self.entry_list.append(self.entry)
							# the below adds the text to the entry boxes
							self.entry.set_text(item_node.firstChild.nodeValue)
        						self.vbox2.pack_start(self.entry,False,False,0)
							self.entry.show()
                                        else:
                                                print "Tag not loaded, probably empty"
                                        entries_loaded = True
			break
			# break out of topmost loop since we are finished loading items from XML document.
			# also return all loaded values as true, which is curerntly unused, but might serve some
			# purpose at some point.
        return (title_loaded and category_loaded and author_loaded and image_loaded and entries_loaded and question_loaded and colors_loaded and coordinates_loaded)


    #function to save quiz to xml document, way simpler than loading the xml file. In this case we simply
    #generate all the tags, and place the respectve elements inside.
    def xml_save(self, xml_document):
	"""Save the current quiz to an xml document.
	@param xml_document - xml.dom.minidom.Document object -
	The xml document that we will save the post to."""

	#create <quiz></quiz>
	quiz_element = xml_document.createElement(PyClicCommon._xml_tags[QUIZ_TAG])
	#create <category></category>
	category_element = xml_document.createElement(PyClicCommon._xml_tags[CATEGORY_TAG])
	#Create <category>category</category>
	category_element.appendChild(xml_document.createTextNode(self.Category))
	#Creates <image></image>
	image_element = xml_document.createElement(PyClicCommon._xml_tags[IMAGE_TAG])
	#Create <IMAGE>image<IMAGE>
	image_element.appendChild(xml_document.createTextNode(self.Image))	
	#Creates <title></title>
	title_element = xml_document.createElement(PyClicCommon._xml_tags[TITLE_TAG])
	#Creates <title>title</title>
	title_element.appendChild(xml_document.createTextNode(self.Title))
	#Creates <author></author>
	author_element = xml_document.createElement(PyClicCommon._xml_tags[AUTHOR_TAG])
	#Creates <author>author</author>
	author_element.appendChild(xml_document.createTextNode(self.Author))
	#Creates <color></color>
	color_element = xml_document.createElement(PyClicCommon._xml_tags[COLOR_TAG])
	#Create colorvalues list, and add R,G,B values to it (we should really strip the brackets on string 
	#conversion here taking out the []), but we do that on load instead. Reason = this function was created 
	#before the load function
	colourvalues = []
	colourvalues.append(self.colorred)
	colourvalues.append(self.colorgreen)
	colourvalues.append(self.colorblue)
	#Creates <color>colorValues</color> and turns into string
	color_element.appendChild(xml_document.createTextNode(str(colourvalues)))
	#appends all elements to quiz tagged XML object
	quiz_element.appendChild(category_element)
	quiz_element.appendChild(title_element)
	quiz_element.appendChild(image_element)
	quiz_element.appendChild(author_element)
	quiz_element.appendChild(color_element)
	#Now add all quiz subelements to xml document, these are the non recursive elements of the quiz document,
	# there is only one of each.
	xml_document.documentElement.appendChild(quiz_element)
	#create <questions></questions>
	questions_element = xml_document.createElement(PyClicCommon._xml_tags[QUESTIONS_TAG])
	#Setup iteration
	Iterate = 0
	#Set questions here, no point in saving values before hand, in fact, come to think of it, we dont really even
	#need to save this, as it is quite clear we have questions going up in number starting at 1. But for
	#clarity of XML document we do it that way.
	question = 1
	# while there are coordinates in the self.coordinates list, iterate through and save stuff
	while Iterate < len(self.coordinates):		
		#Create <question></question>
		question_element = xml_document.createElement(PyClicCommon._xml_tags[QUESTION_TAG])
		#create <question>question<question> and turn to string (XML requires it)
		question_element.appendChild(xml_document.createTextNode(str(question)))
		c = self.coordinates[Iterate]
		#Create <coordinates></coordinates>
		coordinates_element = xml_document.createElement(PyClicCommon._xml_tags[COORDINATES_TAG])
		#create <coordinates>x,y<coordinates>, and turn into strings. This method is better than doing a str
		# as no bracets are saved, just value,value for each set of x,y coordinates.
		coordinates_element.appendChild(xml_document.createTextNode("%d,%d" % (c[0],c[1])))
		#Create <entry></entry>
		entry_element = xml_document.createElement(PyClicCommon._xml_tags[ENTRY_TAG])
		#create <entry>entry<entry> and append each entry via th iterator
		entry_element.appendChild(xml_document.createTextNode(self.Entries[Iterate]))
		
		#Add above to <questions>
		questions_element.appendChild(question_element)
		questions_element.appendChild(coordinates_element)
		questions_element.appendChild(entry_element)
		Iterate=Iterate+1
		question=question+1
	#Now add to the xml document
	xml_document.documentElement.appendChild(questions_element)

    #This is the main xml saver... basicallt it leads to xml_save, which does the heavy parsing work. Comparatitvaly
    #its far easier to open a  document than save one.
    def xml_save_to_file(self, widget):
	"""Save the current post to xml_file
	@param xml_file - string - path to file that
	we will save the xml to.
	@returns boolean - True success. False failure
	"""
	#Init return value
	success = False

	#Get the available DOM Implementation, in this case, the XML parser
	impl = minidom.getDOMImplementation()
	#Create the document, with PyClicAdmin as base node
	xml_document = impl.createDocument(None, PyClicCommon._xml_tags[MAIN_TAG], None)
	#Save the questions into the xml
	self.xml_save(xml_document)
	#Now actually try to save the file
	try:
		save_file = open(self.xml_file, 'w')
		#write the xml document to disc
		xml_document.documentElement.writexml(save_file)
		#Close the file after writing to it
		save_file.close()
	#print exception errors
	except IOError, (errno, strerror):
		self.show_error_dlg(
			"Error saving quiz(%s): %s" % (errno, strerror))
	else:
		#Allright it all worked! Set the return value
		success = True

	return success


    # open image file self.chooser with filter for just png. We use just png because thats what cairo likes
    # though it would be possible to create a pixbuf from any image and feed that to cairo instead. Would
    # require quite some restructuring of code.
    def open_image(self,widget):
	#create a  chooser dialog and give it the base file open dialog with cancel and ok buttons
	self.chooser = gtk.FileChooserDialog(title="Open Image",action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
	#add a file filter
	filter = gtk.FileFilter()
	#set the file filter name to Images
	filter.set_name("Images")
	#set the filters mime type. You can add more underneath for more filetypes
	filter.add_mime_type("image/png")
	#set the actual pattern were searching for, in this case png.
	filter.add_pattern("*.png")
	#add the filter to the chooser dialog
	self.chooser.add_filter(filter)

	#set response to whatever is returned from self.chooser
	response = self.chooser.run()
	#If user clicks on ok, then get the image and create a cairo surface object. It is here that cairo will only
	#accept pngs. We also put in a scale image function, in case the image is not the right size. We should probably
	#tell the user what size to use approximately so the image looks at its best.
	if response == gtk.RESPONSE_OK:
		self.image=self.image.create_from_png(self.chooser.get_filename())
		self.image_scale(self.image)
		self.image_filename=self.chooser.get_filename()
		self.image=self.image.create_from_png(self.chooser.get_filename())
		#expose the changes to X and let it redraw the screen
		self.darea.queue_draw()
	#user has cancelled if it comes to here
   	elif response == gtk.RESPONSE_CANCEL:
      		print 'Closed, no files selected'
	#kill the chooser dialog object
   	self.chooser.destroy()

    #A relatively simple function to scale an image by getting its height and width, and doing some funky stuff
    #with a cairo matrix.
    def image_scale(self,image):
	self.width=self.image.get_width()
	self.height=self.image.get_height()
	imgpat = cairo.SurfacePattern(self.image)

	scaler = cairo.Matrix()
	#image width/640,image height/480
	scaler.scale(self.width/640.0,self.height/480.0)
	imgpat.set_matrix(scaler)

	#set resampling filter
	imgpat.set_filter(cairo.FILTER_BEST)	
	canvas = cairo.ImageSurface(cairo.FORMAT_ARGB32,640,480)
	ctx = cairo.Context(canvas)
	ctx.set_source(imgpat)
	ctx.paint()
	canvas.write_to_png(self.chooser.get_filename())

    #this function grabs the mouse coordinates and appends them to the coordinates list.
    def grab_click(self,widget,event):
	x, y = event.get_coords()
	self.coordinates.append((x,y))
	gc = widget.window.new_gc()
	widget.queue_draw()
	#we send the x,y values to expose for drawing the filled circle

	#We add a label to the right hand vertical box, with the contents of the mouse click number.
	#could have just start an iterator here, but we got the amount of clicks instead... kinda cooler.
	self.label=gtk.Label(self.coordinates.index((x,y))+1)
	self.label_list.append(self.label)
	#pack the label in the 2nd vbox
	self.vbox2.pack_start(self.label,False,False,0)
	self.label.show()
	#Also add an entry box for every click
	self.entry=gtk.Entry(max=30)
	self.entry_list.append(self.entry)

        self.vbox2.pack_start(self.entry,False,False,0)

	self.entry.show()

    #This function does the hardcore Cairo drawing part with the circle and pango and stuff...
    def expose(self, widget, event):
	
	self.cr = widget.window.cairo_create()
        self.cr.set_source_surface(self.image)
        self.cr.paint()
	#set pango details for drawing in circle
	self.cr.select_font_face("Arial",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)
	self.cr.set_font_size(16)

	#for each entry in self.coordinates, in other words, for every mouse click position stored
	for x,y in self.coordinates: 	
	
		#set the colour split into its R, G, B components			
		self.cr.set_source_rgb(self.colorred,self.colorgreen,self.colorblue)
		#draw the respective circle
		self.cr.arc(x,y,14,0,2 * math.pi)
		#give the circle an edge
		self.cr.stroke
		#make it a filled circle
		self.cr.fill() 

		#define whats going in the circle, basically the mouseclick number
		self.string="%s" %(self.coordinates.index((x,y))+1)
		#where do we put it
		self.cr.move_to(x-4,y+5)
		#what colour do we make the pango object
		self.cr.set_source_rgb(1, 1, 1)
		#show the pango object
		self.cr.show_text(self.string)

    #needed function to get the active text from the combo box, as gtk doesnt have that built in. found on the web
    #some place    
    def get_active_text(combobox):
        model = combobox.get_model()
        active = combobox.get_active()
        if active < 0:
            return None
        return model[active][0]

    #function to encode in base64 the image so we can transport the XML document easily, especially across xmpp tubes.
    def encode64(self,filew):
 	   try:
 	      response = open(filew)
  	      data = response.read()
	   #give us the exception error if it doesnt work
 	   except IOError, (errno, strerror):
  	      self.show_error_dlg("Error saving quiz(%s): %s" % (errno, strerror))
	   #return the base64 encoded object
 	   return data.encode('base64')

    #File save function that gets called when all elements have been collected and xml file needs to be openned.
    def on_file_save(self, widget):
	"""This function saves the current Quiz as an XML file using sub functions""" 
	# Let the user browse for the save location and name of XML file
	if (self.xml_file == None):
		self.xml_file = self.pcc.file_browse(gtk.FILE_CHOOSER_ACTION_SAVE)
	#If we have a xml_file
	if (self.xml_file):
		#If we can actually send to the xml_save_to_file function
		if (self.xml_save_to_file(self.xml_file)):
			#Allright it all worked! Set the Title of the window
			self.pcc.set_window_title_from_file(self.xml_file,self.window)
			#Give a success notice of saving the file
			self.on_save_notice(self.window)


    #success notice for file save
    def on_save_notice(self,window):
        dialog = gtk.MessageDialog(self.window,
	gtk.DIALOG_DESTROY_WITH_PARENT, 
	gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, "File saved successfully")
	dialog.set_title("Save Successful")
	dialog.run()
	dialog.destroy()

    #warning notice that category is missing
    def on_items_missing(self,window):
        dialog = gtk.MessageDialog(self.window,
	gtk.DIALOG_DESTROY_WITH_PARENT, 
	gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, "You must have a category selected to save")
	dialog.set_title("Too few details")
	dialog.run()
	dialog.destroy()
			

    def on_file_save_as(self, widget):
	"""Let the user save the current file to a new location."""

	xml_file = "Untitled"
	if (self.xml_file != None):
		xml_file = os.path.basename(self.xml_file)

	xml_file = self.file_browse(gtk.FILE_CHOOSER_ACTION_SAVE, xml_file)
	#If we have a xml_file
	if (xml_file):
		if (self.xml_save_to_file(xml_file)):
			"""Allright it all worked! save the current file and
			set the title."""
			self.xml_file = xml_file
			PyClicCommon.set_window_title_from_file(self.xml_file)

    #simple little function to grab combobox changed value for category
    def on_changed(self,widget):
        self.Category = widget.get_active_text()

    #function to collect data and make sure category is selected
    def save_details(self,widget):
	self.Cat=self.cb_category.get_active_text()
	self.Title = self.title_entry.get_text()
	self.Author = self.author_entry.get_text()
	# save image to base64
	self.Image = self.encode64(self.image_filename)
	# function to grab entries from entry list
	self.Entries=[]
	for entry in self.entry_list:
		self.Entries.append(entry.get_text())
	if self.Cat==None:
		print "Category missing"
	 	self.on_items_missing(self.window)		
	else:
		self.on_file_save(file)

    #function to clear all data and all elements
    def on_file_new(self,widget):
	for entry in self.entry_list:
		self.vbox2.remove(entry)
	for label in self.label_list:
		self.vbox2.remove(label)
	self.entry_list=[]
	self.Entries=[]
	self.coordinates=[]
	self.author_entry.set_text("")
	self.title_entry.set_text("")
	self.question_list=[]
	self.xml_file = None
	self.entry=""
	self.window.queue_draw()

    #function to call color selector and set the individual colours to their respective numbers
    def on_clicked(self, widget):
        cdia = gtk.ColorSelectionDialog("Select color")
        response = cdia.run()
              
        if response == gtk.RESPONSE_OK:
            colorsel = cdia.colorsel
            self.colour = colorsel.get_current_color()
            self.colorred = self.colour.red/65535.0
	    self.colorgreen = self.colour.green/65535.0
	    self.colorblue = self.colour.blue/65535.0
  
        cdia.destroy()
	self.window.queue_draw()

    #Start the main constructor of our class, where the magic happens, kinda... not really...
    def __init__(self):
        # create the main window, and attach delete_event signal to terminating
        # the application
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	
	#set icon for app
	try:
		self.window.set_icon_from_file("pyclicicon.png")
	except Exception, e:
		print e.message
		sys.exit(1)

	#instantiate the pycliccomon class which we imported... yay... now I know what instantiate means :-)
	self.pcc=PyClicCommon()

        self.window.connect("delete_event", self.pcc.close_application)
	self.window.set_size_request(775,544)
        self.window.set_border_width(0)

	self.vbox2 = gtk.VBox(False, 2)

	#set an empty xml file on program start	
	self.xml_file=None
	

	#Insert menubar containing 2 columns
	mb = gtk.MenuBar()
	filemenu = gtk.Menu()

	filem = gtk.MenuItem("File")
	filem.set_submenu(filemenu)

	aboutm = gtk.MenuItem("About PyClicAdmin")
	aboutm.connect("activate",self.pcc.about_program)
	filemenu.append(aboutm)

	newquiz = gtk.MenuItem("New File / Clear")
	newquiz.connect("activate",self.on_file_new)
	filemenu.append(newquiz)


	openquiz = gtk.MenuItem("Open Quiz")
	openquiz.connect("activate",self.on_file_open)
	filemenu.append(openquiz)

	save = gtk.MenuItem("Save Quiz")
	save.connect("activate",self.save_details)
	filemenu.append(save)

	exit = gtk.MenuItem("Exit")
	exit.connect("activate",gtk.main_quit)
	filemenu.append(exit)

	viewmenu = gtk.Menu()
        view = gtk.MenuItem("Options")
        view.set_submenu(viewmenu)

	open = gtk.MenuItem("Open Image")
	open.connect("activate",self.open_image)
	viewmenu.append(open)

        stat = gtk.MenuItem("Change Color")
        stat.connect("activate", self.on_clicked)
        viewmenu.append(stat)

	mb.append(filem)
	mb.append(view)
	self.hbox = gtk.HBox(False, 2)


	#define and start vertical area
	vbox = gtk.VBox(False, 2)
	vbox.pack_start(mb,False,False,0)
	
	#Insert Toolbar with several options, and icons taken from stock library, toolbar only contains icons.
        toolbar = gtk.Toolbar()
        toolbar.set_style(gtk.TOOLBAR_ICONS)

	newtb = gtk.ToolButton(gtk.STOCK_CLEAR)
	newtb.set_tooltip_text("Clear the screan / start new quiz")
        opentb = gtk.ToolButton(gtk.STOCK_OPEN)
	opentb.set_tooltip_text("Open a PyQClic quiz")
        savetb = gtk.ToolButton(gtk.STOCK_SAVE)
	savetb.set_tooltip_text("Save a PyQClic Quiz with .gpx extension")
	colortb = gtk.ToolButton(gtk.STOCK_SELECT_COLOR)
	colortb.set_tooltip_text("Change round label color")
	importtb = gtk.ToolButton(gtk.STOCK_ORIENTATION_PORTRAIT)
	importtb.set_tooltip_text("Import/Open image for labeling")
        sep = gtk.SeparatorToolItem()
        quittb = gtk.ToolButton(gtk.STOCK_QUIT)
	quittb.set_tooltip_text("Quit the Program, C u l8r")

        toolbar.insert(newtb, 0)
        toolbar.insert(opentb, 1)
        toolbar.insert(savetb, 2)
	toolbar.insert(colortb,3)
	toolbar.insert(importtb,4)
        toolbar.insert(sep, 5)
        toolbar.insert(quittb, 6)
        
        

        vbox.pack_start(toolbar, False, False, 0)


        # A drawing area inside a fixed()

	fix = gtk.Fixed()


	self.darea=gtk.DrawingArea()

	self.image = cairo.ImageSurface.create_from_png("pyclic.png")
	self.image_filename="pyclic.png"
	self.darea.connect("expose_event", self.expose)
	self.darea.set_events(gtk.gdk.BUTTON_PRESS_MASK)
	self.darea.connect("button_press_event",self.grab_click)

	self.darea.set_size_request(606,540)
	fix.put(self.darea,0,0)

	#define coordinates list
	self.coordinates=[]

	#define text entries list
	self.entry_list=[]	

	self.label_list=[]

	self.question_list=[]

	#define color values for question circle
	self.colorred=0
	self.colorblue=0
	self.colorgreen=0

	# Layout
	self.window.add(vbox)
	vbox.add(self.hbox)
	self.hbox.add(fix)

	# Title and category entry boxes
	title_label=gtk.Label("Title")
	self.vbox2.pack_start(title_label,False,False,0)

	self.title_entry=gtk.Entry(max=50)
	self.vbox2.pack_start(self.title_entry,False,False,0)

	author_label=gtk.Label("Author")
	self.vbox2.pack_start(author_label,False,False,0)

	self.author_entry=gtk.Entry(max=50)
	self.vbox2.pack_start(self.author_entry,False,False,0)

	
	title_category=gtk.Label("Category")
	self.vbox2.pack_start(title_category,False,False,0)

	self.cb_category=gtk.combo_box_new_text()
	self.cb_category.connect("changed", self.on_changed)
	self.cb_category.append_text("Geography")
	self.cb_category.append_text("Science")
	self.cb_category.append_text("ICT")
	self.cb_category.append_text("Languages")
	self.cb_category.append_text("Maths")
	self.cb_category.append_text("Music")
	self.cb_category.append_text("History")
	self.cb_category.append_text("Business")
	self.cb_category.append_text("Art")
	self.vbox2.pack_start(self.cb_category,False,False,0)


	#submit button for entries
	submit_button=gtk.Button("Submit")
	self.vbox2.pack_start(submit_button,False,False,0)
	submit_button.set_events(gtk.gdk.BUTTON_PRESS_MASK)
	submit_button.connect("clicked",self.save_details)

	clear_button=gtk.Button("Clear")
	self.vbox2.pack_start(clear_button,False,False,0)
	clear_button.set_events(gtk.gdk.BUTTON_PRESS_MASK)
	clear_button.connect("clicked",self.on_file_new)

	self.scrollw = gtk.ScrolledWindow()
	self.scrollw.set_policy(gtk.POLICY_NEVER,gtk.POLICY_AUTOMATIC)
        self.hbox.pack_start(self.scrollw)
        self.scrollw.add_with_viewport(self.vbox2)

#	self.hbox.add(self.vbox2)

	self.window.show_all()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    PyClicAdmin()
    main()