GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 30, 2014 3:52 pm

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Sorting of Gtk treeView and Gtk button in the header
PostPosted: Sat May 03, 2014 10:33 pm 

Joined: Sat May 03, 2014 4:05 pm
Posts: 1

I am trying to learn Gtk-programming. Inspired by the Gnome Gtk tutorial I implemented a small File-browser using the Gtk treeView using python. I bumped now into two issues I still was not able to solve.

1. I implemented sorting of the items (files and directories) in the tree view. The sorting should be: directories first alphabetical sorted, then files alphabetical sorted. It works, but first after I click on column header. I would like to have the sorted order by the time the aplication starts. I would also like to prohibit, that the sorting order can be changed by clicking on the header. The sorting order should stay at 'ascending' , The 'descending' sorting order destroys my algorithm for succesive filling the filesystem structure.

2. I do not want to have any title in the header. Preferably I would like to dismiss the header completly. I discovered the possibility to place an widget inside the toolbar, so to use the space I placed a Gtk button inside. The button semmes not be responding on clicking.

I would be very pleased if someone could help me with those issues. I will also welcome if you could point me to any other Gtk related mistakes in my code.

Here is the code:

import os, stat
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf

class MainWindow(Gtk.Window):

   def __init__(self):
      Gtk.Window.__init__(self, title="Simple Photo Importer", default_width = 400,default_height = 600)
        # initialize the filesystem treestore
      self.fileSystemTreeStore = Gtk.TreeStore(str, Pixbuf, str, bool)
      # populate the tree store
      self.populateFileSystemTreeStore(self.fileSystemTreeStore, '/home')
      # initialize the TreeView
      self.fileSystemTreeView = Gtk.TreeView(self.fileSystemTreeStore)
      # Create a TreeViewColumn
      treeViewCol = Gtk.TreeViewColumn(title=None)
      self.openDirButton = Gtk.Button(label="Open directory")
      self.openDirButton.connect("clicked", self.readNewDirectory)
      treeViewCol.set_clickable('false')# ???
      #setting coumn sort id
      treeViewCol.set_sort_column_id(0)# necessary
      #treeViewCol.set_sort_column_id(0, Gtk.SortType.ASCENDING)
      self.fileSystemTreeStore.set_sort_func(0, self.compareFileListEntries, None)

      # Create a column cell to display text
      colCellText = Gtk.CellRendererText()
      # Create a column cell to display an image
      colCellImg = Gtk.CellRendererPixbuf()
      # Add the cells to the column
      treeViewCol.pack_start(colCellImg, False)
      treeViewCol.pack_start(colCellText, True)
      # Bind the text cell to column 0 of the tree's model
      treeViewCol.add_attribute(colCellText, "text", 0)
      # Bind the image cell to column 1 of the tree's model
      treeViewCol.add_attribute(colCellImg, "pixbuf", 1)
      # Append the columns to the TreeView
      # add "on expand" callback
      self.fileSystemTreeView.connect("row-expanded", self.onRowExpanded)
      # add "on collapse" callback
      self.fileSystemTreeView.connect("row-collapsed", self.onRowCollapsed)
      self.scrollView = Gtk.ScrolledWindow()
      # append the scrollView to the window (this)

   def populateFileSystemTreeStore(self, treeStore, path, parent=None):
      itemCounter = 0
      # iterate over the items in the path
      for item in os.listdir(path):
         # Get the absolute path of the item
         itemFullname = os.path.join(path, item)
         # Extract metadata from the item
         itemMetaData = os.stat(itemFullname)
         # Determine if the item is a folder
         itemIsFolder = stat.S_ISDIR(itemMetaData.st_mode)
         # Generate an icon from the default icon theme
         itemIcon = Gtk.IconTheme.get_default().load_icon("folder" if itemIsFolder else "empty", 22, 0)
         # Append the item to the TreeStore
         currentIter = treeStore.append(parent, [item, itemIcon, itemFullname, itemIsFolder])
         # add dummy if current item was a folder
         if itemIsFolder: treeStore.append(currentIter, [None, None, None, 'true'])
         #increment the item counter
         itemCounter += 1
      # add the dummy node back if nothing was inserted before
      if itemCounter < 1: treeStore.append(parent, [None, None, None, 'true'])

   def onRowExpanded(self, treeView, treeIter, treePath):
      # get the associated model
      treeStore = treeView.get_model()
      # get the full path of the position
      newPath = treeStore.get_value(treeIter, 2)
      # populate the subtree on curent position
      self.populateFileSystemTreeStore(treeStore, newPath, treeIter)
      # remove the first child (dummy node)

   def onRowCollapsed(self, treeView, treeIter, treePath):
      # get the associated model
      treeStore = treeView.get_model()
      # get the iterator of the first child
      currentChildIter = treeStore.iter_children(treeIter)
      # loop as long as some childern exist
      while currentChildIter:
         # remove the first child
         # refresh the iterator of the next child
         currentChildIter = treeStore.iter_children(treeIter)
      # append dummy node
      treeStore.append(treeIter, [None, None, None, 'true'])

   def readNewDirectory(self, widget):
      print 'new dir'

   def compareFileListEntries(self, fileListModel, row1, row2, user_data):
      # name are stored in first column
      name1 = fileListModel.get_value(row1, 0)
      if name1: name1 = name1.lower()
      name2 = fileListModel.get_value(row2, 0)
      if name2: name2 = name2.lower()
      # is folder values are stored in fourth column
      isDir1 = fileListModel.get_value(row1, 3)
      isDir2 = fileListModel.get_value(row2, 3)
      # the comparator
      if isDir1 > isDir2:
         return -1
      elif isDir1 < isDir2:
         return 1
         if name1 < name2:
            return -1
         elif name1 > name2:
            return 1
            return 0

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)

Thank you !

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group