Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop mode switching feature of beginner and advanced #135

Merged
merged 2 commits into from
Apr 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 13 additions & 80 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
# - [high] Automatically add file suffix when saving.
# - [high] Add polygon movement with arrow keys
# - [high] Deselect shape when clicking and already selected(?)
# - [high] Sanitize shortcuts between beginner/advanced mode.
# - [medium] Zoom should keep the image centered.
# - [medium] Add undo button for vertex addition.
# - [low,maybe] Open images with drag & drop.
Expand Down Expand Up @@ -135,7 +134,6 @@ def __init__(self, filename=None, output=None, store_data=True,
self.dirty = False

self._noSelectionSlot = False
self._beginner = True
self.screencastViewer = "firefox"
self.screencast = "screencast.ogv"

Expand Down Expand Up @@ -254,14 +252,11 @@ def __init__(self, filename=None, output=None, store_data=True,

createMode = action('Create\nPolygo&ns', self.setCreateMode,
shortcuts['create_polygon'], 'objects',
'Start drawing polygons', enabled=False)
'Start drawing polygons', enabled=True)
editMode = action('&Edit\nPolygons', self.setEditMode,
shortcuts['edit_polygon'], 'edit',
'Move and edit polygons', enabled=False)

create = action('Create\nPolygo&n', self.createShape,
shortcuts['create_polygon'], 'objects',
'Draw a new polygon', enabled=False)
delete = action('Delete\nPolygon', self.deleteSelectedShape,
shortcuts['delete_polygon'], 'cancel',
'Delete', enabled=False)
Expand All @@ -273,10 +268,6 @@ def __init__(self, filename=None, output=None, store_data=True,
shortcuts['undo_last_point'], 'undoLastPoint',
'Undo last drawn point', enabled=False)

advancedMode = action('&Advanced Mode', self.toggleAdvancedMode,
'Ctrl+Shift+A', 'expert',
'Switch to advanced mode', checkable=True)

hideAll = action('&Hide\nPolygons',
functools.partial(self.togglePolygons, False),
icon='eye', tip='Hide all polygons', enabled=False)
Expand Down Expand Up @@ -352,25 +343,23 @@ def __init__(self, filename=None, output=None, store_data=True,
self.actions = struct(
save=save, saveAs=saveAs, open=open_, close=close,
lineColor=color1, fillColor=color2,
create=create, delete=delete, edit=edit, copy=copy,
delete=delete, edit=edit, copy=copy,
undoLastPoint=undoLastPoint,
createMode=createMode, editMode=editMode,
advancedMode=advancedMode,
shapeLineColor=shapeLineColor, shapeFillColor=shapeFillColor,
zoom=zoom, zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg,
fitWindow=fitWindow, fitWidth=fitWidth,
zoomActions=zoomActions,
fileMenuActions=(open_, opendir, save, saveAs, close, quit),
beginner=(), advanced=(),
tool=(),
editMenu=(edit, copy, delete, None, undoLastPoint,
None, color1, color2),
beginnerContext=(create, edit, copy, delete, undoLastPoint),
advancedContext=(
menu=(
createMode, editMode, edit, copy,
delete, shapeLineColor, shapeFillColor,
undoLastPoint,
),
onLoadActive=(close, create, createMode, editMode),
onLoadActive=(close, createMode, editMode),
onShapesPresent=(saveAs, hideAll, showAll),
)

Expand All @@ -387,30 +376,25 @@ def __init__(self, filename=None, output=None, store_data=True,
save, saveAs, close, None, quit))
addActions(self.menus.help, (help,))
addActions(self.menus.view, (
labels, advancedMode, None,
labels, None,
hideAll, showAll, None,
zoomIn, zoomOut, zoomOrg, None,
fitWindow, fitWidth))

self.menus.file.aboutToShow.connect(self.updateFileMenu)

# Custom context menu for the canvas widget:
addActions(self.canvas.menus[0], self.actions.beginnerContext)
addActions(self.canvas.menus[0], self.actions.menu)
addActions(self.canvas.menus[1], (
action('&Copy here', self.copyShape),
action('&Move here', self.moveShape)))

self.tools = self.toolbar('Tools')
self.actions.beginner = (
self.actions.tool = (
open_, opendir, openNextImg, openPrevImg, save,
None, create, copy, delete, None,
None, createMode, copy, delete, editMode, None,
zoomIn, zoom, zoomOut, fitWindow, fitWidth)

self.actions.advanced = (
open_, opendir, openNextImg, openPrevImg, save, None,
createMode, editMode, None,
hideAll, showAll)

self.statusBar().showMessage('%s started.' % __appname__)
self.statusBar().show()

Expand Down Expand Up @@ -452,10 +436,6 @@ def __init__(self, filename=None, output=None, store_data=True,
Shape.line_color = self.lineColor
Shape.fill_color = self.fillColor

if self.settings.value('advanced', False):
self.actions.advancedMode.setChecked(True)
self.toggleAdvancedMode()

# Populate the File menu dynamically.
self.updateFileMenu()
# Since loading the file may take some time,
Expand Down Expand Up @@ -509,40 +489,16 @@ def update_dict(target_dict, new_dict):
def noShapes(self):
return not self.labelList.itemsToShapes

def toggleAdvancedMode(self, value=True):
self._beginner = not value
self.canvas.setEditing(True)
self.populateModeActions()
self.editButton.setVisible(not value)
if value:
self.actions.createMode.setEnabled(True)
self.actions.editMode.setEnabled(False)
self.dock.setFeatures(self.dock.features() | self.dockFeatures)
else:
self.dock.setFeatures(self.dock.features() ^ self.dockFeatures)

def populateModeActions(self):
if self.beginner():
tool, menu = self.actions.beginner, self.actions.beginnerContext
else:
tool, menu = self.actions.advanced, self.actions.advancedContext
tool, menu = self.actions.tool, self.actions.menu
self.tools.clear()
addActions(self.tools, tool)
self.canvas.menus[0].clear()
addActions(self.canvas.menus[0], menu)
self.menus.edit.clear()
actions = (self.actions.create,) if self.beginner() else \
(self.actions.createMode, self.actions.editMode)
actions = (self.actions.createMode, self.actions.editMode)
addActions(self.menus.edit, actions + self.actions.editMenu)

def setBeginner(self):
self.tools.clear()
addActions(self.tools, self.actions.beginner)

def setAdvanced(self):
self.tools.clear()
addActions(self.tools, self.actions.advanced)

def setDirty(self):
self.dirty = True
self.actions.save.setEnabled(True)
Expand All @@ -554,7 +510,7 @@ def setDirty(self):
def setClean(self):
self.dirty = False
self.actions.save.setEnabled(False)
self.actions.create.setEnabled(True)
self.actions.createMode.setEnabled(True)
title = __appname__
if self.filename is not None:
title = '{} - {}'.format(title, self.filename)
Expand Down Expand Up @@ -593,46 +549,28 @@ def addRecentFile(self, filename):
self.recentFiles.pop()
self.recentFiles.insert(0, filename)

def beginner(self):
return self._beginner

def advanced(self):
return not self.beginner()

# Callbacks

def tutorial(self):
subprocess.Popen([self.screencastViewer, self.screencast])

def createShape(self):
assert self.beginner()
self.canvas.setEditing(False)
self.actions.create.setEnabled(False)

def toggleDrawingSensitive(self, drawing=True):
"""Toggle drawing sensitive.

In the middle of drawing, toggling between modes should be disabled.
"""
self.actions.editMode.setEnabled(not drawing)
self.actions.undoLastPoint.setEnabled(drawing)
if not drawing and self.beginner():
# Cancel creation.
self.canvas.setEditing(True)
self.canvas.restoreCursor()
self.actions.create.setEnabled(True)

def toggleDrawMode(self, edit=True):
self.canvas.setEditing(edit)
self.actions.createMode.setEnabled(edit)
self.actions.editMode.setEnabled(not edit)

def setCreateMode(self):
assert self.advanced()
self.toggleDrawMode(False)

def setEditMode(self):
assert self.advanced()
self.toggleDrawMode(True)

def updateFileMenu(self):
Expand Down Expand Up @@ -792,11 +730,7 @@ def newShape(self):
text = self.labelDialog.popUp(text)
if text is not None:
self.addLabel(self.canvas.setLastLabel(text))
if self.beginner(): # Switch to edit mode.
self.canvas.setEditing(True)
self.actions.create.setEnabled(True)
else:
self.actions.editMode.setEnabled(True)
self.actions.editMode.setEnabled(True)
self.setDirty()
else:
self.canvas.undoLastLine()
Expand Down Expand Up @@ -967,7 +901,6 @@ def closeEvent(self, event):
self.settings.setValue('line/color', self.lineColor)
self.settings.setValue('fill/color', self.fillColor)
self.settings.setValue('recentFiles', self.recentFiles)
self.settings.setValue('advanced', not self._beginner)
# ask the use for where to save the labels
# self.settings.setValue('window/geometry', self.saveGeometry())

Expand Down