More work with context menus. Added image registration option to the
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 11 Jul 2004 19:16:04 +0000 (19:16 +0000)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 11 Jul 2004 19:16:04 +0000 (19:16 +0000)
album and object collection context menus. Added album properties
option to the object collection context menu. Made image operation
menu options properly insensitive. And other minor changes.

src/gkofoto/albums.py
src/gkofoto/gkofoto
src/gkofoto/glade/gkofoto.glade
src/gkofoto/mainwindow.py
src/gkofoto/menuhandler.py
src/gkofoto/objectcollection.py
src/gkofoto/objectcollectionview.py
src/gkofoto/registerimagesdialog.py

index 5e7876e..0e136e6 100644 (file)
@@ -4,15 +4,17 @@ import gtk
 from environment import env
 from albumdialog import AlbumDialog
 from menuhandler import *
+from registerimagesdialog import RegisterImagesDialog
 
 class Albums:
 
 ###############################################################################
 ### Public
 
-    __createAlbumLabel = "Create child album"
-    __destroyAlbumLabel = "Destroy album"
-    __editAlbumLabel = "Album properties"
+    __createAlbumLabel = "Create child album..."
+    __registerImagesLabel = "Register and add images..."
+    __destroyAlbumLabel = "Destroy album..."
+    __editAlbumLabel = "Album properties..."
 
     # TODO This class should probably be splited in a model and a view when/if
     #      a multiple windows feature is introduced.
@@ -56,6 +58,7 @@ class Albums:
             selection = self.__albumView.get_selection()
         albumModel, iterator =  self.__albumView.get_selection().get_selected()
         createMenuItem = self.__menuGroup[self.__createAlbumLabel]
+        registerMenuItem = self.__menuGroup[self.__registerImagesLabel]
         destroyMenuItem = self.__menuGroup[self.__destroyAlbumLabel]
         editMenuItem = self.__menuGroup[self.__editAlbumLabel]
         if iterator:
@@ -64,10 +67,12 @@ class Albums:
             album = env.shelf.getAlbum(
                 albumModel.get_value(iterator, self.__COLUMN_ALBUM_ID))
             createMenuItem.set_sensitive(album.isMutable())
+            registerMenuItem.set_sensitive(album.isMutable())
             destroyMenuItem.set_sensitive(album != env.shelf.getRootAlbum())
             editMenuItem.set_sensitive(True)
         else:
             createMenuItem.set_sensitive(False)
+            registerMenuItem.set_sensitive(False)
             destroyMenuItem.set_sensitive(False)
             editMenuItem.set_sensitive(False)
 
@@ -75,6 +80,14 @@ class Albums:
         dialog = AlbumDialog("Create album")
         dialog.run(self._createAlbumHelper)
 
+    def _registerImages(self, *dummies):
+        albumModel, iterator =  self.__albumView.get_selection().get_selected()
+        selectedAlbumId = albumModel.get_value(iterator, self.__COLUMN_ALBUM_ID)
+        selectedAlbum = env.shelf.getAlbum(selectedAlbumId)
+        dialog = RegisterImagesDialog(selectedAlbum)
+        dialog.run()
+        # TODO: update objectCollection?
+
     def _createAlbumHelper(self, tag, desc):
         newAlbum = env.shelf.createAlbum(tag)
         if len(desc) > 0:
@@ -83,8 +96,8 @@ class Albums:
         if iterator is None:
             selectedAlbum = env.shelf.getRootAlbum()
         else:
-            selectedAlbumTag = albumModel.get_value(iterator, self.__COLUMN_TAG)
-            selectedAlbum = env.shelf.getAlbum(selectedAlbumTag.decode("utf-8"))
+            selectedAlbumId = albumModel.get_value(iterator, self.__COLUMN_ALBUM_ID)
+            selectedAlbum = env.shelf.getAlbum(selectedAlbumId)
         children = list(selectedAlbum.getChildren())
         children.append(newAlbum)
         selectedAlbum.setChildren(children)
@@ -99,8 +112,8 @@ class Albums:
         result = dialog.run()
         if result == gtk.RESPONSE_OK:
             albumModel, iterator =  self.__albumView.get_selection().get_selected()
-            selectedAlbumTag = albumModel.get_value(iterator, self.__COLUMN_TAG)
-            env.shelf.deleteAlbum(selectedAlbumTag.decode("utf-8"))
+            selectedAlbumId = albumModel.get_value(iterator, self.__COLUMN_ALBUM_ID)
+            env.shelf.deleteAlbum(selectedAlbumId)
             # TODO The whole tree should not be reloaded
             self.loadAlbumTree()
             # TODO update objectCollection?
@@ -114,8 +127,8 @@ class Albums:
 
     def _editAlbumHelper(self, tag, desc):
         albumModel, iterator =  self.__albumView.get_selection().get_selected()
-        selectedAlbumTag = albumModel.get_value(iterator, self.__COLUMN_TAG)
-        selectedAlbum = env.shelf.getAlbum(selectedAlbumTag)
+        selectedAlbumId = albumModel.get_value(iterator, self.__COLUMN_ALBUM_ID)
+        selectedAlbum = env.shelf.getAlbum(selectedAlbumId)
         selectedAlbum.setTag(tag)
         if len(desc) > 0:
             selectedAlbum.setAttribute(u"title", desc)
@@ -167,6 +180,8 @@ class Albums:
         self.__menuGroup = MenuGroup()
         self.__menuGroup.addMenuItem(self.__createAlbumLabel,
                                      self._createChildAlbum)
+        self.__menuGroup.addMenuItem(self.__registerImagesLabel,
+                                     self._registerImages)
         self.__menuGroup.addMenuItem(self.__destroyAlbumLabel,
                                      self._destroyAlbum)
         self.__menuGroup.addMenuItem(self.__editAlbumLabel,
index c1fa8b0..167b808 100755 (executable)
@@ -117,10 +117,6 @@ class Controller:
             self.__mainWindow.reload()
         dialog.destroy()
 
-    def registerImages(self, app):
-        dialog = RegisterImagesDialog()
-        dialog.run()
-
 ######################################################################
 ### Start
 
index 8cf1d7a..5d8e957 100644 (file)
                    </widget>
                  </child>
 
-                 <child>
-                   <widget class="GtkImageMenuItem" id="registerImages">
-                     <property name="visible">True</property>
-                     <property name="label" translatable="yes">Register images</property>
-                     <property name="use_underline">True</property>
-                     <signal name="activate" handler="on_register_images_activate" last_modification_time="Tue, 06 Jul 2004 11:01:02 GMT"/>
-
-                     <child internal-child="image">
-                       <widget class="GtkImage" id="image16">
-                         <property name="visible">True</property>
-                         <property name="stock">gtk-add</property>
-                         <property name="icon_size">1</property>
-                         <property name="xalign">0.5</property>
-                         <property name="yalign">0.5</property>
-                         <property name="xpad">0</property>
-                         <property name="ypad">0</property>
-                       </widget>
-                     </child>
-                   </widget>
-                 </child>
-
-                 <child>
-                   <widget class="GtkSeparatorMenuItem" id="separator2">
-                     <property name="visible">True</property>
-                   </widget>
-                 </child>
-
                  <child>
                    <widget class="GtkImageMenuItem" id="quit">
                      <property name="visible">True</property>
index 80ea245..5e43519 100644 (file)
@@ -13,6 +13,7 @@ from gkofoto.objectcollection import *
 
 class MainWindow(gtk.Window):
     def __init__(self):
+        env.mainwindow = self
         self._toggleLock = False
         self.__currentObjectCollection = None
         self._currentView = None
@@ -31,7 +32,6 @@ class MainWindow(gtk.Window):
         env.widgets["quit"].connect("activate", env.controller.quit)
         env.widgets["save"].set_sensitive(False)
         env.widgets["revert"].set_sensitive(False)
-        env.widgets["registerImages"].connect("activate", env.controller.registerImages)
         env.widgets["previousButton"].set_sensitive(False)
         env.widgets["nextButton"].set_sensitive(False)
         env.widgets["zoom100"].set_sensitive(False)
@@ -69,6 +69,9 @@ class MainWindow(gtk.Window):
         self.__categories.loadCategoryTree()
         self.loadQuery(self.__query)
 
+    def reloadAlbumTree(self):
+        self.__albums.loadAlbumTree()
+
     def getIconImage(self, name):
         pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(env.iconDir, name))
         image = gtk.Image()
index 1b80f93..a2768e3 100644 (file)
@@ -55,7 +55,7 @@ class MenuGroup:
 
     def enable(self):
         for child in self.__childItems:
-            child.self.set_sensitive(True)
+            child.set_sensitive(True)
 
     def disable(self):
         for child in self.__childItems:
index 0ad5145..4276f39 100644 (file)
@@ -7,6 +7,8 @@ from kofoto.shelf import *
 from menuhandler import *
 from environment import env
 from objectselection import *
+from albumdialog import AlbumDialog
+from registerimagesdialog import RegisterImagesDialog
 
 class ObjectCollection(object):
 
@@ -64,9 +66,23 @@ class ObjectCollection(object):
     def getDestroyLabel(self):
         return "Destroy..."
 
-    def getActions(self):
-        return None
-        # TODO implement
+    def getCreateAlbumChildLabel(self):
+        return "Create album child..."
+
+    def getRegisterImagesLabel(self):
+        return "Register and add images..."
+
+    def getAlbumPropertiesLabel(self):
+        return "Album properties..."
+
+    def getOpenImageLabel(self):
+        return "Open image in external program..."
+
+    def getRotateImageLeftLabel(self):
+        return "Rotate image left"
+
+    def getRotateImageRightLabel(self):
+        return "Rotate image right"
 
     def getObjectMetadataMap(self):
         return self.__objectMetadataMap
@@ -296,7 +312,47 @@ class ObjectCollection(object):
             # TODO Show dialog error box?
             print "Not allowed to set album tag on image"
 
-    def rotate(self, widget, angle):
+    def createAlbumChild(self, widget, data):
+        dialog = AlbumDialog("Create album")
+        dialog.run(self._createAlbumChildHelper)
+
+    def _createAlbumChildHelper(self, tag, desc):
+        newAlbum = env.shelf.createAlbum(tag)
+        if len(desc) > 0:
+            newAlbum.setAttribute(u"title", desc)
+        selectedObjects = self.__objectSelection.getSelectedObjects()
+        selectedAlbum = selectedObjects[0]
+        children = list(selectedAlbum.getChildren())
+        children.append(newAlbum)
+        selectedAlbum.setChildren(children)
+        env.mainwindow.reloadAlbumTree()
+
+    def registerAndAddImages(self, widget, data):
+        selectedObjects = self.__objectSelection.getSelectedObjects()
+        assert len(selectedObjects) == 1 and selectedObjects[0].isAlbum()
+        selectedAlbum = selectedObjects[0]
+        dialog = RegisterImagesDialog(selectedAlbum)
+        dialog.run()
+
+    def albumProperties(self, widget, data):
+        selectedObjects = self.__objectSelection.getSelectedObjects()
+        assert len(selectedObjects) == 1 and selectedObjects[0].isAlbum()
+        selectedAlbumId = selectedObjects[0].getId()
+        dialog = AlbumDialog("Edit album", selectedAlbumId)
+        dialog.run(self._albumPropertiesHelper)
+
+    def _albumPropertiesHelper(self, tag, desc):
+        selectedObjects = self.__objectSelection.getSelectedObjects()
+        selectedAlbum = selectedObjects[0]
+        selectedAlbum.setTag(tag)
+        if len(desc) > 0:
+            selectedAlbum.setAttribute(u"title", desc)
+        else:
+            selectedAlbum.deleteAttribute(u"title")
+        env.mainwindow.reloadAlbumTree()
+        # TODO: Update objectCollection.
+
+    def rotateImage(self, widget, angle):
         for (rowNr, obj) in self.__objectSelection.getMap().items():
             if not obj.isAlbum():
                 location = obj.getLocation().encode(env.codeset)
@@ -313,7 +369,7 @@ class ObjectCollection(object):
                 else:
                     print "failed to execute:", command
 
-    def open(self, widget, data):
+    def openImage(self, widget, data):
         locations = ""
         for obj in self.__objectSelection.getSelectedObjects():
             if not obj.isAlbum():
index fcb4529..97a40d6 100644 (file)
@@ -87,8 +87,14 @@ class ObjectCollectionView:
         self.__clipboardMenuGroup = self.__createClipboardMenuGroup(objectCollection)
         for item in self.__clipboardMenuGroup:
             self._contextMenu.add(item)
-        self.__commandMenuGroup = self.__createCommandMenuGroup(objectCollection)
-        for item in self.__commandMenuGroup:
+        self.__objectMenuGroup = self.__createObjectMenuGroup(objectCollection)
+        for item in self.__objectMenuGroup:
+            self._contextMenu.add(item)
+        self.__albumMenuGroup = self.__createAlbumMenuGroup(objectCollection)
+        for item in self.__albumMenuGroup:
+            self._contextMenu.add(item)
+        self.__imageMenuGroup = self.__createImageMenuGroup(objectCollection)
+        for item in self.__imageMenuGroup:
             self._contextMenu.add(item)
         self.__sortMenuGroup = self.__createSortMenuGroup(objectCollection)
         self._contextMenu.add(self.__sortMenuGroup.createGroupMenuItem())
@@ -101,7 +107,7 @@ class ObjectCollectionView:
 
     def _updateContextMenu(self, *foo):
         env.debug("Updating context menu")
-        self.__clipboardMenuGroup[self._objectCollection.getDestroyLabel()].set_sensitive(False)
+        self.__objectMenuGroup[self._objectCollection.getDestroyLabel()].set_sensitive(False)
         mutable = self._objectCollection.isMutable()
         if env.clipboard.hasObjects():
             self.__clipboardMenuGroup[self._objectCollection.getPasteLabel()].set_sensitive(mutable)
@@ -112,37 +118,46 @@ class ObjectCollectionView:
             model = self._objectCollection.getModel()
             rootAlbumId = env.shelf.getRootAlbum().getId()
 
-            albumsSelected = False
-            imagesSelected = False
+            albumsSelected = 0
+            imagesSelected = 0
             rootAlbumSelected = False
             for position in objectSelection:
                 iterator = model.get_iter(position)
                 isAlbum = model.get_value(
                     iterator, self._objectCollection.COLUMN_IS_ALBUM)
                 if isAlbum:
-                    albumsSelected = True
+                    albumsSelected += 1
                     if rootAlbumId == model.get_value(
                         iterator, self._objectCollection.COLUMN_OBJECT_ID):
                         rootAlbumSelected = True
                 else:
-                    imagesSelected = True
+                    imagesSelected += 1
 
-            self.__clipboardMenuGroup[self._objectCollection.getCopyLabel()].set_sensitive(True)
             self.__clipboardMenuGroup[self._objectCollection.getCutLabel()].set_sensitive(mutable)
+            self.__clipboardMenuGroup[self._objectCollection.getCopyLabel()].set_sensitive(True)
             self.__clipboardMenuGroup[self._objectCollection.getDeleteLabel()].set_sensitive(mutable)
-            self.__clipboardMenuGroup[self._objectCollection.getDestroyLabel()].set_sensitive(
-                imagesSelected ^ albumsSelected and not rootAlbumSelected)
-            self.__commandMenuGroup["Open image"].set_sensitive(True)
-            self.__commandMenuGroup[90].set_sensitive(True)
-            self.__commandMenuGroup[270].set_sensitive(True)
+            self.__objectMenuGroup[self._objectCollection.getDestroyLabel()].set_sensitive(
+                (imagesSelected == 0) ^ (albumsSelected == 0) and not rootAlbumSelected)
+            if albumsSelected == 1 and imagesSelected == 0:
+                selectedAlbumId = model.get_value(
+                    iterator, self._objectCollection.COLUMN_OBJECT_ID)
+                selectedAlbum = env.shelf.getAlbum(selectedAlbumId)
+                if selectedAlbum.isMutable():
+                    self.__albumMenuGroup.enable()
+                else:
+                    self.__albumMenuGroup.disable()
+                    self.__albumMenuGroup[self._objectCollection.getAlbumPropertiesLabel()].set_sensitive(True)
+            else:
+                self.__albumMenuGroup.disable()
+            if albumsSelected == 0 and imagesSelected > 0:
+                self.__imageMenuGroup.enable()
+            else:
+                self.__imageMenuGroup.disable()
         else:
-            self.__clipboardMenuGroup[self._objectCollection.getCopyLabel()].set_sensitive(False)
-            self.__clipboardMenuGroup[self._objectCollection.getCutLabel()].set_sensitive(False)
-            self.__clipboardMenuGroup[self._objectCollection.getDeleteLabel()].set_sensitive(False)
-            self.__clipboardMenuGroup[self._objectCollection.getDestroyLabel()].set_sensitive(False)
-            self.__commandMenuGroup["Open image"].set_sensitive(False)
-            self.__commandMenuGroup[90].set_sensitive(False)
-            self.__commandMenuGroup[270].set_sensitive(False)
+            self.__clipboardMenuGroup.disable()
+            self.__objectMenuGroup.disable()
+            self.__albumMenuGroup.disable()
+            self.__imageMenuGroup.disable()
 
 ###############################################################################
 ### Private
@@ -193,14 +208,31 @@ class ObjectCollectionView:
         menuGroup.addMenuItem(oc.getPasteLabel(), oc.paste)
         menuGroup.addMenuItem(oc.getDeleteLabel(), oc.delete)
         menuGroup.addSeparator()
+        return menuGroup
+
+    def __createObjectMenuGroup(self, oc):
+        menuGroup = MenuGroup()
         menuGroup.addMenuItem(oc.getDestroyLabel(), oc.destroy)
         menuGroup.addSeparator()
         return menuGroup
 
-    def __createCommandMenuGroup(self, oc):
+    def __createAlbumMenuGroup(self, oc):
+        menuGroup = MenuGroup()
+        menuGroup.addMenuItem(
+            oc.getCreateAlbumChildLabel(), oc.createAlbumChild)
+        menuGroup.addMenuItem(
+            oc.getRegisterImagesLabel(), oc.registerAndAddImages)
+        menuGroup.addMenuItem(
+            oc.getAlbumPropertiesLabel(), oc.albumProperties)
+        menuGroup.addSeparator()
+        return menuGroup
+
+    def __createImageMenuGroup(self, oc):
         menuGroup = MenuGroup()
-        menuGroup.addMenuItem("Open image", oc.open)
-        menuGroup.addMenuItem("Rotate JPEG left", oc.rotate, 270)
-        menuGroup.addMenuItem("Rotate JPEG right", oc.rotate, 90)
+        menuGroup.addMenuItem(oc.getOpenImageLabel(), oc.openImage)
+        menuGroup.addMenuItem(
+            oc.getRotateImageLeftLabel(), oc.rotateImage, 270)
+        menuGroup.addMenuItem(
+            oc.getRotateImageRightLabel(), oc.rotateImage, 90)
         menuGroup.addSeparator()
         return menuGroup
index 7ae460c..1529e45 100644 (file)
@@ -5,19 +5,25 @@ from kofoto.shelf import ImageExistsError, NotAnImageError, makeValidTag
 from kofoto.clientutils import walk_files
 
 class RegisterImagesDialog(gtk.FileSelection):
-    def __init__(self):
+    def __init__(self, albumToAddTo=None):
         gtk.FileSelection.__init__(self, title="Register images")
+        self.__albumToAddTo = albumToAddTo
         self.set_select_multiple(True)
         self.ok_button.connect("clicked", self._ok)
         self.cancel_button.connect("clicked", self._cancel)
 
     def _ok(self, widget):
+        images = []
         for filepath in walk_files(self.get_selections()):
             try:
-                env.shelf.createImage(filepath.decode("utf-8"))
+                image = env.shelf.createImage(filepath.decode("utf-8"))
+                images.append(image)
             except (NotAnImageError, ImageExistsError):
                 pass
-        self.hide()
+        if self.__albumToAddTo:
+            children = list(self.__albumToAddTo.getChildren())
+            self.__albumToAddTo.setChildren(children + images)
+        self.destroy()
 
     def _cancel(self, widget):
         self.destroy()