Implemented drawing the grid in player application
authorHarishankar <v.harishankar@gmail.com>
Mon, 6 Dec 2010 16:30:51 +0000 (22:00 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 6 Dec 2010 16:30:51 +0000 (22:00 +0530)
Implemented the drawing functionality in gtk player application

player_mainwindow.py
playerwindow.glade

index 58de825..00be083 100644 (file)
@@ -4,14 +4,76 @@
 
 # Main window class for GetAClue player
 
+import cPickle
 import pygtk
 pygtk.require20 ()
 import gtk
+import pango
+import crosswordpuzzle
 
 class MainWindow:
        def gtk_main_quit (self, *args):
                gtk.main_quit ()
 
+       # drawing for puzzle grid
+       def on_puzzlegrid_expose_event (self, drawarea, event):
+               # if puzzle is loaded
+               if self.puzzle:
+                       # size the area
+                       drawarea.set_size_request (self.puzzle.cols*30+2, self.puzzle.rows*30+2)
+
+                       # set background color to white
+                       col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("white"))
+                       drawarea.window.set_background (col)
+
+                       #numlayout = gtk.PrintContext().create_pango_layout ()
+                       #numlayout.set_font_description (pango.FontDescription ("Sans 8"))
+                       ctx = drawarea.window.cairo_create ()
+                       ctx.set_line_width (1.5)
+
+                       for row in range (self.puzzle.rows):
+                               for col in range (self.puzzle.cols):
+                                       # if the area is not occupied
+                                       if (self.puzzle.data[row][col].occupied_across is False and
+                                               self.puzzle.data[row][col].occupied_down is False):
+                                               ctx.rectangle (col*30, row*30, 30, 30)
+                                               ctx.fill ()
+                                       else:
+                                               ctx.rectangle (col*30, row*30, 30, 30)
+                                               ctx.stroke ()
+                                               # if numbered
+                                               if self.puzzle.data[row][col].numbered <> 0:
+                                                       ctx.select_font_face ("Sans 7")
+                                                       ctx.move_to (col*30+2, row*30+10)
+                                                       ctx.show_text (str(self.puzzle.data[row][col].numbered))
+
+                       return False
+
+       def load_clues (self):
+               # get the clues list store objects
+               across = self.ui.get_object ("clues_across")
+               down = self.ui.get_object ("clues_down")
+               across.clear ()
+               down.clear ()
+
+               # if puzzle is loaded
+               if self.puzzle:
+                       clues_across = self.puzzle.get_clues_across ()
+                       clues_down = self.puzzle.get_clues_down ()
+                       # insert the numbers and the clues for across
+                       for word, clue in clues_across:
+                               across.append ([str(self.puzzle.data[word[1]][word[2]].numbered),
+                                                       clue])
+                       # insert the numbers and the clues for down
+                       for word, clue in clues_down:
+                               down.append ([ str(self.puzzle.data[word[1]][word[2]].numbered),
+                                                       clue])
+
+       def open_file (self, file):
+               self.puzzle = cPickle.load (open (file, "rb"))
+               self.window.set_title ("GetAClue player - " + file)
+               self.load_clues ()
+
        def __init__ (self, file_to_play = None):
                # load the user interface
                self.ui = gtk.Builder ()
@@ -41,9 +103,12 @@ class MainWindow:
                # connect the signals
                self.ui.connect_signals (self)
 
-               # set the window title
+               # set the puzzle to None
+               self.puzzle = None
+
+               # open the file if it is set
                if file_to_play:
-                       self.window.set_title ("GetAClue player - " + file_to_play)
+                       self.open_file (file_to_play)
 
                gtk.main ()
 
index a52e5ef..9441946 100644 (file)
@@ -23,6 +23,8 @@
     <property name="height_request">360</property>
     <property name="visible">True</property>
     <property name="title" translatable="yes">GetAClue player</property>
+    <property name="default_width">640</property>
+    <property name="default_height">480</property>
     <signal name="destroy" handler="gtk_main_quit"/>
     <child>
       <object class="GtkVBox" id="vbox1">
           <object class="GtkHBox" id="hbox1">
             <property name="visible">True</property>
             <child>
-              <object class="GtkDrawingArea" id="puzzlegrid">
-                <property name="width_request">120</property>
-                <property name="height_request">120</property>
+              <object class="GtkScrolledWindow" id="scrolledwindow3">
                 <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="hscrollbar_policy">automatic</property>
+                <property name="vscrollbar_policy">automatic</property>
+                <child>
+                  <object class="GtkViewport" id="viewport1">
+                    <property name="visible">True</property>
+                    <property name="resize_mode">queue</property>
+                    <child>
+                      <object class="GtkDrawingArea" id="puzzlegrid">
+                        <property name="width_request">120</property>
+                        <property name="height_request">120</property>
+                        <property name="visible">True</property>
+                        <signal name="expose_event" handler="on_puzzlegrid_expose_event"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
               </object>
               <packing>
                 <property name="position">0</property>
               <object class="GtkVPaned" id="vpaned1">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
+                <property name="position">180</property>
                 <child>
-                  <object class="GtkTreeView" id="tree_clues_across">
-                    <property name="width_request">80</property>
-                    <property name="height_request">150</property>
+                  <object class="GtkScrolledWindow" id="scrolledwindow1">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
-                    <property name="model">clues_across</property>
+                    <property name="hscrollbar_policy">automatic</property>
+                    <property name="vscrollbar_policy">automatic</property>
                     <child>
-                      <object class="GtkTreeViewColumn" id="number">
-                        <property name="title">Number</property>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkTreeViewColumn" id="clueacross">
-                        <property name="title">Clue Across</property>
+                      <object class="GtkTreeView" id="tree_clues_across">
+                        <property name="width_request">80</property>
+                        <property name="height_request">150</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="model">clues_across</property>
+                        <property name="headers_clickable">False</property>
+                        <property name="search_column">0</property>
+                        <child>
+                          <object class="GtkTreeViewColumn" id="number">
+                            <property name="title">Number</property>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkTreeViewColumn" id="clueacross">
+                            <property name="title">Clue Across</property>
+                          </object>
+                        </child>
                       </object>
                     </child>
                   </object>
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkTreeView" id="tree_clues_down">
-                    <property name="width_request">80</property>
-                    <property name="height_request">150</property>
+                  <object class="GtkScrolledWindow" id="scrolledwindow2">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
-                    <property name="model">clues_down</property>
+                    <property name="hscrollbar_policy">automatic</property>
+                    <property name="vscrollbar_policy">automatic</property>
                     <child>
-                      <object class="GtkTreeViewColumn" id="downnum">
-                        <property name="title">Number</property>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkTreeViewColumn" id="cluedown">
-                        <property name="title">Clue Down</property>
+                      <object class="GtkTreeView" id="tree_clues_down">
+                        <property name="width_request">80</property>
+                        <property name="height_request">150</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="model">clues_down</property>
+                        <property name="headers_clickable">False</property>
+                        <property name="search_column">0</property>
+                        <child>
+                          <object class="GtkTreeViewColumn" id="downnum">
+                            <property name="title">Number</property>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkTreeViewColumn" id="cluedown">
+                            <property name="title">Clue Down</property>
+                          </object>
+                        </child>
                       </object>
                     </child>
                   </object>