From c15a9c80602255561686f6e3bca20f93e59bb803 Mon Sep 17 00:00:00 2001 From: Christophe Gueret Date: Sun, 13 Jan 2013 13:59:36 +0000 Subject: First commit --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..555b0eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +dist +*.pyc +.project +.pydevproject diff --git a/Activity.py b/Activity.py new file mode 100644 index 0000000..f0ba268 --- /dev/null +++ b/Activity.py @@ -0,0 +1,83 @@ +''' +Created on 5 Nov 2012 + +@author: cgueret +''' +from gi.repository import Gtk + +from sugar3.activity import activity +from sugar3.graphics.toolbarbox import ToolbarBox +from sugar3.activity.widgets import ActivityButton +from sugar3.activity.widgets import TitleEntry +from sugar3.activity.widgets import StopButton +from sugar3.activity.widgets import DescriptionItem +from Application import Main +import simplejson + +class QuizActivity(activity.Activity): + def __init__(self, handle): + ''' + Initialise the activity + ''' + activity.Activity.__init__(self, handle) + + # Create the main part of the application + self._application = Main() + + # toolbar with the new toolbar redesign + toolbar_box = ToolbarBox() + + activity_button = ActivityButton(self) + toolbar_box.toolbar.insert(activity_button, 0) + activity_button.show() + + title_entry = TitleEntry(self) + toolbar_box.toolbar.insert(title_entry, -1) + title_entry.show() + + description_item = DescriptionItem(self) + toolbar_box.toolbar.insert(description_item, -1) + description_item.show() + + separator = Gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + toolbar_box.toolbar.insert(separator, -1) + separator.show() + + stop_button = StopButton(self) + toolbar_box.toolbar.insert(stop_button, -1) + stop_button.show() + + self.set_toolbar_box(toolbar_box) + toolbar_box.show() + + # Set the canvas + self.set_canvas(self._application.get_widget()) + self.canvas.show() + + def write_file(self, file_path): + if not self.metadata['mime_type']: + self.metadata['mime_type'] = 'text/plain' + + data = {} + data['last_score'] = self._application.get_score() + + fd = open(file_path, 'w') + text = simplejson.dumps(data) + fd.write(text) + fd.close() + + def read_file(self, file_path): + if self.metadata['mime_type'] != 'text/plain': + return + + fd = open(file_path, 'r') + text = fd.read() + data = simplejson.loads(text) + fd.close() + + if 'last_score' in data: + self._application.set_score(int(data['last_score'])) + else: + self._application.set_score(0) diff --git a/Application.py b/Application.py new file mode 100644 index 0000000..3172741 --- /dev/null +++ b/Application.py @@ -0,0 +1,93 @@ +''' +Created on 11 Jan 2013 + +@author: cgueret +''' +from gi.repository import Gtk +import csv +import random + +class Data(object): + ''' + Class used to interface with the data + ''' + def __init__(self): + ''' + Constructor + ''' + self._ranks = {} + with open('ranks.csv', 'rb') as csvfile: + reader = csv.reader(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) + for row in reader: + self._ranks[row[0]] = int(row[1]) + + def get_random_subset(self, size): + ''' + Generate a random subset of pairs (country, rank) + ''' + subset = {} + while len(subset.keys()) != size: + key = self._ranks.keys()[random.randint(0, len(self._ranks.keys())-1)] + if key not in subset: + subset[key] = self._ranks[key] + return subset + + +class Main(object): + def __init__(self): + ''' + Constructor + ''' + # Variable to count the number of points + self._points = 0 + self._good_answer = -1 + self._data = Data() + + # Load the graphical user interface + self._gui = Gtk.Builder() + self._gui.add_from_file("GUI.glade") + + # Connect the buttons + for number in range(1, 5): + button = self._gui.get_object("button%d" % number) + button.connect("clicked", self.on_button_clicked, number) + + # Create a new question + self.make_new_question() + + def get_widget(self): + ''' + Return the main widget of the application + ''' + return self._gui.get_object("main_box") + + def get_score(self): + return self._points + + def set_score(self, value): + self._points = value + self._gui.get_object("score_zone").set_label(str(self._points)) + + def make_new_question(self): + ''' + Prepare a new question: pick a set of cities and update the buttons + ''' + subset = self._data.get_random_subset(4).items() + self._good_answer = 1 + for i in range(1, 5): + (country, rank) = subset[i - 1] + if rank > subset[self._good_answer-1][1]: + self._good_answer = i + self._gui.get_object("button%d" % i).set_label(country) + + def on_button_clicked(self, button, number): + ''' + Function called when one of the buttons is clicked + ''' + # Check the answer picked + if number == self._good_answer: + self.set_score(self.get_score() + 1) + + # Create a new question + self.make_new_question() + diff --git a/GUI.glade b/GUI.glade new file mode 100644 index 0000000..1202abf --- /dev/null +++ b/GUI.glade @@ -0,0 +1,157 @@ + + + + + True + False + vertical + + + True + False + + + + + + True + True + 0 + + + + + True + False + + + True + False + 0 + out + + + True + False + vertical + 5 + True + start + + + True + True + True + + + True + True + 0 + + + + + True + True + True + + + True + True + 1 + + + + + True + True + True + + + True + True + 2 + + + + + True + True + True + + + True + True + 3 + + + + + + + True + False + <b>Who is the biggest ?</b> + True + + + + + True + True + 5 + 1 + + + + + True + False + 0 + out + + + True + False + 0 + + + + + True + False + <b>Score</b> + True + + + + + False + True + 5 + 3 + + + + + False + True + 1 + + + + + True + False + + + + + + True + True + 2 + + + + diff --git a/activity/activity-icon.svg b/activity/activity-icon.svg new file mode 100644 index 0000000..21751ef --- /dev/null +++ b/activity/activity-icon.svg @@ -0,0 +1,13 @@ + + +]> + + + + + + + + + \ No newline at end of file diff --git a/activity/activity.info b/activity/activity.info new file mode 100644 index 0000000..8f6cf78 --- /dev/null +++ b/activity/activity.info @@ -0,0 +1,9 @@ +[Activity] +name = Quiz +activity_version = 1 +license = GPLv3 +bundle_id = nl.vu.QuizActivity +exec = sugar-activity Activity.QuizActivity +icon = activity-icon +show_launcher = yes +summary = a simple quiz game \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..2903809 --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +''' +Created on 11 Jan 2013 + +@author: cgueret +''' +from gi.repository import Gtk, Gio +from Application import Main + +class QuizApplication(Gtk.Application): + def __init__(self): + Gtk.Application.__init__(self, application_id="nl.vu.quiz", flags=Gio.ApplicationFlags.FLAGS_NONE) + self.connect("activate", self.on_activate) + + def on_activate(self, data=None): + # Create the main part of the application + main = Main() + main_box = main.get_widget() + + # Create the window + window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL) + window.set_default_size(600,450) + window.set_title("Quiz activity") + window.set_border_width(2) + window.set_position(Gtk.WindowPosition.CENTER) + window.add(main_box) + + # Show the window and add it to the application + window.show_all() + self.add_window(window) + +if __name__ == "__main__": + app = QuizApplication() + app.run(None) diff --git a/ranks.csv b/ranks.csv new file mode 100644 index 0000000..5214268 --- /dev/null +++ b/ranks.csv @@ -0,0 +1,210 @@ +"Algeria",10 +"American Samoa",212 +"Anguilla",220 +"Antigua and Barbuda",195 +"Arab League",2 +"Argentina",8 +"Armenian Soviet Socialist Republic",15 +"Ascension Island",219 +"Australia",6 +"Austria",115 +"Avatele",8 +"Azerbaijan",114 +"Azerbaijan Soviet Socialist Republic",9 +"Bahrain",190 +"Bangladesh",94 +"Barbados",200 +"Belarus",85 +"Belgium",139 +"Belize",150 +"Bhutan",135 +"Bolivia",28 +"Bosnia and Herzegovina",127 +"Botswana",47 +"Brazil",5 +"British Virgin Islands",216 +"Brunei",172 +"Bulgaria",105 +"Burkina Faso",74 +"Burma",40 +"Burundi",145 +"Byelorussian Soviet Socialist Republic",6 +"Cambodia",88 +"Cameroon",54 +"Canada",2 +"Canton of Neuchâtel",15 +"Cape Verde",172 +"Cayman Islands",206 +"Chile",38 +"China",3 +"Colombia",26 +"Cook Islands",210 +"Costa Rica",128 +"Crimea",148 +"Croatia",126 +"Cuba",105 +"Cyprus",167 +"Czech Republic",116 +"Côte d'Ivoire",69 +"Democratic Republic of the Congo",11 +"Denmark",12 +"Djibouti",150 +"Dominica",184 +"Dominican Republic",130 +"E-Government in the United Arab Emirates",116 +"East Timor",159 +"Ecuador",75 +"Egypt",30 +"Equatorial Guinea",144 +"Eritrea",100 +"Estonia",132 +"Estonian Soviet Socialist Republic",13 +"Ethiopia",27 +"Falkland Islands",162 +"Faroe Islands",180 +"Federated States of Micronesia",188 +"Fiji",155 +"Finland",64 +"Gabon",76 +"Georgia (country)",120 +"Georgian Soviet Socialist Republic",10 +"Gibraltar",229 +"Greece",96 +"Greenland",12 +"Guam",190 +"Guatemala",107 +"Guinea",78 +"Guinea-Bissau",136 +"Guyana",84 +"Haiti",140 +"Hong Kong",179 +"Hungary",109 +"Iceland",108 +"India",7 +"Indonesia",15 +"Iran",18 +"Iraq",59 +"Israel",154 +"Jamaica",166 +"Japan",62 +"Jersey",227 +"Jordan",112 +"Karelo-Finnish Soviet Socialist Republic",7 +"Kazakh Soviet Socialist Republic",2 +"Kazakhstan",9 +"Kenya",47 +"Kingdom of the Netherlands",136 +"Kirghiz Soviet Socialist Republic",7 +"Kiribati",186 +"Korea",84 +"Kuwait",157 +"Kyrgyzstan",86 +"Laos",84 +"Latvia",124 +"Latvian Soviet Socialist Republic",12 +"Lebanon",166 +"Lesotho",140 +"Libya",17 +"Liechtenstein",215 +"Lithuanian Soviet Socialist Republic",11 +"Luxembourg",179 +"Macau",229 +"Madagascar",47 +"Malawi",99 +"Malaysia",67 +"Maldives",206 +"Mali",24 +"Malta",200 +"Marshall Islands",213 +"Mauritania",29 +"Mauritius",179 +"Mercosur",2 +"Mexico",14 +"Moldavian Soviet Socialist Republic",14 +"Moldova",138 +"Monaco",235 +"Mongolia",19 +"Montserrat",219 +"Morocco",58 +"Mozambique",35 +"Namibia",34 +"Nauru",239 +"Nepal",95 +"Netherlands",135 +"Nevis",207 +"New Caledonia",154 +"New Zealand",75 +"Nicaragua",97 +"Niger",22 +"Nigeria",32 +"Norfolk Island",227 +"North Korea",98 +"Northern Cyprus",174 +"Northern Mariana Islands",195 +"Oman",70 +"Pakistan",36 +"Palau",196 +"Panama",118 +"Papua New Guinea",54 +"Paraguay",60 +"Peru",20 +"Poland",69 +"Portugal",111 +"Puerto Rico",169 +"Qatar",164 +"Republic of Ireland",120 +"Republic of Macedonia",148 +"Republic of the Congo",64 +"Rwanda",149 +"Saint Kitts and Nevis",207 +"Saint Pierre and Miquelon",208 +"Saint Vincent and the Grenadines",198 +"Samoa",174 +"San Marino",219 +"Saudi Arabia",12 +"Senegal",87 +"Serbia",113 +"Seychelles",197 +"Sierra Leone",119 +"Singapore",189 +"Slovakia",129 +"Socialist Soviet Republic of Abkhazia",10 +"Solomon Islands",142 +"Somalia",44 +"South Africa",25 +"South Asian Association for Regional Cooperation",7 +"South Korea",109 +"South Sudan",42 +"Sri Lanka",122 +"Sudan",16 +"Swaziland",157 +"Sweden",57 +"Syria",89 +"Taiwan",136 +"Tajik Soviet Socialist Republic",8 +"Tajikistan",102 +"The Bahamas",160 +"The Gambia",164 +"Togo",125 +"Tonga",186 +"Tunisia",92 +"Turkey",37 +"Turkmen Soviet Socialist Republic",4 +"Turkmenistan",52 +"Turks and Caicos Islands",199 +"Tuvalu",226 +"Ukraine",46 +"United Arab Emirates",116 +"United Kingdom",80 +"United States",3 +"United States Minor Outlying Islands",190 +"United States Virgin Islands",202 +"Uzbek Soviet Socialist Republic",5 +"Uzbekistan",56 +"Vatican City",250 +"Vietnam",65 +"Wallis (island)",211 +"Western Sahara",76 +"Yemen",50 +"Zambia",39 +"Zimbabwe",60 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f169461 --- /dev/null +++ b/setup.py @@ -0,0 +1,3 @@ +from sugar3.activity import bundlebuilder +if __name__ == "__main__": + bundlebuilder.start() -- cgit v0.9.1