Skip to content

Commit

Permalink
Add keys:
Browse files Browse the repository at this point in the history
  ESC shifts focus out of any focused text entry.
  Ctrl-U clears any existing tags.
When moving to next image, copy tags from previous image automatically.
  • Loading branch information
akkana committed Feb 17, 2013
1 parent 2985c2a commit cc6953f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
3 changes: 3 additions & 0 deletions MetaPho/MetaPho.py
Expand Up @@ -266,6 +266,9 @@ def removeTag(self, tag, img) :
except :
pass

def clearTags(self, img) :
img.tags = []

def toggleTag(self, tagno, img) :
'''Toggle tag number tagno for the given img.'''
self.changed = True
Expand Down
20 changes: 20 additions & 0 deletions MetaPho/gtkpho.py
Expand Up @@ -64,6 +64,26 @@ def change_tag(self, tagno, newstr) :
newtag = self.addTag(newstr, self.cur_img)
self.highlightTag(newtag, True)

def clearTags(self, img) :
MetaPho.Tagger.clearTags(self, img)

# also update the UI
for i in xrange(len((self.entries))) :
self.highlightTag(i, False)

# leave nothing focused
self.focus_none()

def focus_none(self) :
# if focus was in a text entry, un-highlight that entry
focused = self.parentwin.get_focus()
if (type(focused) is gtk.Entry) :
entryno = self.entries.index(focused)
self.highlightTag(entryno, False)

# Make sure we're leaving nothing focused:
self.parentwin.set_focus(None)

def focus_out(self, entry, event, tagno) :
entry_text = entry.get_text()
# Ignore blank entries
Expand Down
43 changes: 32 additions & 11 deletions metapho
Expand Up @@ -22,11 +22,6 @@ class MetaPhoWindow(object):
MetaPho.Image.gImageList.append(MetaPho.Image(filename))
self.imgno = 0

# Window size.
# Eventually, calculate these somehow or read from prefs
#self.width = 1024
#self.height = 768

# The size of the image viewing area:
self.imgwidth = 640
self.imgheight = 600
Expand All @@ -52,8 +47,6 @@ class MetaPhoWindow(object):
self.tagger = MetaPho.gtkpho.TagViewer(self.win)
main_hbox.pack_start(self.tagger, expand=True)

#self.win.set_default_size(self.width, self.height)

self.win.add(main_hbox)

self.win.connect("key-press-event", self.key_press_event)
Expand Down Expand Up @@ -100,6 +93,16 @@ class MetaPhoWindow(object):
Tell the viewer to load and show the image.
'''
loaded = False

# Save the tags of the current image, so we can copy them
# into the next image if it doesn't have any yet.
oldtags = None
try :
if MetaPho.Image.gImageList[self.imgno].tags :
oldtags = MetaPho.Image.gImageList[self.imgno].tags
except :
pass

while self.imgno < len(MetaPho.Image.gImageList)-1 and not loaded :
self.imgno += 1
img = MetaPho.Image.gImageList[self.imgno]
Expand All @@ -120,6 +123,11 @@ class MetaPhoWindow(object):
#self.imgno -= 1

if loaded :
# If we have an image, and it has no tags set yet,
# clone the tags from the previous image:
if oldtags and not MetaPho.Image.gImageList[self.imgno].tags :
MetaPho.Image.gImageList[self.imgno].tags = oldtags[:]

self.tagger.setImage(MetaPho.Image.gImageList[self.imgno])

else : # couldn't load anything in the list
Expand Down Expand Up @@ -182,20 +190,26 @@ class MetaPhoWindow(object):
def key_press_event(self, widget, event) :
entry_focused = (type(self.win.get_focus()) is gtk.Entry)

# ESC or ctrl-space shifts focus to the next tag entry,
# ctrl-space shifts focus to the next tag entry,
# or out of the entries if we're already typing in one.
# Ctrl-space also goes to the next image.
if event.keyval == gtk.keysyms.Escape or \
(event.keyval == gtk.keysyms.space and \
if (event.keyval == gtk.keysyms.space and \
event.state & gtk.gdk.CONTROL_MASK) :
if entry_focused :
self.win.set_focus(None)
self.tagger.focus_none()
else :
self.tagger.focus_next_entry()

if (event.keyval == gtk.keysyms.space) :
self.nextImage()
return True

# ESC shifts focus out of the current entry (if any)
# and makes sure nothing is focused.
if event.keyval == gtk.keysyms.Escape :
self.tagger.focus_none()
return True

# Return shifts focus to the next tag entry (never out of the entries).
if event.keyval == gtk.keysyms.Return :
self.tagger.focus_next_entry()
Expand All @@ -221,6 +235,13 @@ class MetaPhoWindow(object):
self.nextImage()
return True

# Ctrl-U: clear tags, then leave focus in the first empty tag field.
if event.keyval == gtk.keysyms.u and \
event.state & gtk.gdk.CONTROL_MASK :
self.tagger.clearTags(MetaPho.Image.gImageList[self.imgno])
self.tagger.focus_next_entry()
return True

# Eventually we may want to require ctrl-q to quit,
# freeing q for tags/categories.
if event.keyval == gtk.keysyms.q and \
Expand Down

0 comments on commit cc6953f

Please sign in to comment.