Skip to content

Commit a098be0

Browse files
committed
views.main*: Simplify menus, reduce code, and fix Ctrl+C in diff viewer
Remove the unnecessary 'Edit' menu from the main menu bar. The 'Options' action moved into the 'File' menu since the 'Edit' menu is empty without Cut/Paste/etc. Centralize all of the action handling code into views.mainwindow. Do not register any actions for Copy/Paste/etc. whatsoever so that the Qt defaults (which do the right thing) are used instead. This is the simpler thing to do, uses less code, makes a nicer UI, and fixes being able to use Ctrl+C in the diff viewer. It also reduces confusion because it is not clear for new users whether the edit menu operates on the commit message editor or on the diff viewer. Likewise, leaving the Ctrl+C, etc. keyboard shortcuts alone allows Qt to use the correct widget-context action (which happens by default). This is a net reduction of ~100 lines of code. yay :-) Closes #90 Signed-off-by: David Aguilar <davvid@gmail.com>
1 parent aadc1ac commit a098be0

File tree

2 files changed

+184
-284
lines changed

2 files changed

+184
-284
lines changed

cola/views/main.py

Lines changed: 3 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from PyQt4 import QtGui
66
from PyQt4 import QtCore
7-
from PyQt4.QtCore import Qt
87
from PyQt4.QtCore import SIGNAL
98

109
import cola
@@ -15,24 +14,11 @@
1514
from cola import qtutils
1615
from cola import settings
1716
from cola import signals
18-
from cola import resources
1917
from cola.compat import set
2018
from cola.qtutils import SLOT
21-
from cola.views import dag
22-
from cola.views import about
2319
from cola.views import actions as actionsmod
2420
from cola.views.syntax import DiffSyntaxHighlighter
2521
from cola.views.mainwindow import MainWindow
26-
from cola.controllers import classic
27-
from cola.controllers import compare
28-
from cola.controllers import createtag
29-
from cola.controllers import merge
30-
from cola.controllers import search as smod
31-
from cola.controllers import stash
32-
from cola.controllers.bookmark import manage_bookmarks
33-
from cola.controllers.bookmark import save_bookmark
34-
from cola.controllers.createbranch import create_new_branch
35-
from cola.controllers.options import update_options
3622

3723

3824
class MainView(MainWindow):
@@ -72,105 +58,9 @@ def __init__(self, parent=None):
7258
# Change this whenever dockwidgets are removed.
7359
self._widget_version = 1
7460

75-
self.model = cola.model()
7661
self.model.add_message_observer(self.model.message_updated,
7762
self._update_view)
7863

79-
# Listen for text and amend messages
80-
cola.notifier().connect(signals.diff_text, self.set_display)
81-
cola.notifier().connect(signals.mode, self._mode_changed)
82-
cola.notifier().connect(signals.amend, self.amend_checkbox.setChecked)
83-
84-
# Broadcast the amend mode
85-
self.connect(self.amend_checkbox, SIGNAL('toggled(bool)'),
86-
SLOT(signals.amend_mode))
87-
88-
# Add button callbacks
89-
self._relay_button(self.alt_button, signals.reset_mode)
90-
self._relay_button(self.rescan_button, signals.rescan)
91-
92-
self._connect_button(self.signoff_button, self.signoff)
93-
self._connect_button(self.stage_button, self.stage)
94-
self._connect_button(self.unstage_button, self.unstage)
95-
self._connect_button(self.commit_button, self.commit)
96-
self._connect_button(self.fetch_button, guicmds.fetch_slot(self))
97-
self._connect_button(self.push_button, guicmds.push_slot(self))
98-
self._connect_button(self.pull_button, guicmds.pull_slot(self))
99-
self._connect_button(self.stash_button, lambda: stash.stash(parent=self))
100-
101-
# Menu actions
102-
actions = [
103-
(self.menu_quit, self.close),
104-
(self.menu_branch_compare, compare.branch_compare),
105-
(self.menu_branch_diff, guicmds.branch_diff),
106-
(self.menu_branch_review, guicmds.review_branch),
107-
(self.menu_browse_branch, guicmds.browse_current),
108-
(self.menu_browse_other_branch, guicmds.browse_other),
109-
(self.menu_browse_commits, guicmds.browse_commits),
110-
(self.menu_create_tag, createtag.create_tag),
111-
(self.menu_create_branch, create_new_branch),
112-
(self.menu_checkout_branch, guicmds.checkout_branch),
113-
(self.menu_delete_branch, guicmds.branch_delete),
114-
(self.menu_rebase_branch, guicmds.rebase),
115-
(self.menu_clone_repo, guicmds.clone_repo),
116-
(self.menu_commit_compare, compare.compare),
117-
(self.menu_commit_compare_file, compare.compare_file),
118-
(self.menu_cherry_pick, guicmds.cherry_pick),
119-
(self.menu_diff_expression, guicmds.diff_expression),
120-
(self.menu_diff_branch, guicmds.diff_branch),
121-
(self.menu_export_patches, guicmds.export_patches),
122-
(self.menu_help_about, about.launch_about_dialog),
123-
(self.menu_help_docs,
124-
lambda: self.model.git.web__browse(resources.html_docs())),
125-
(self.menu_load_commitmsg, guicmds.load_commitmsg_slot(self)),
126-
(self.menu_load_commitmsg_template,
127-
SLOT(signals.load_commit_template)),
128-
(self.menu_manage_bookmarks, manage_bookmarks),
129-
(self.menu_save_bookmark, save_bookmark),
130-
(self.menu_merge_local, merge.local_merge),
131-
(self.menu_merge_abort, merge.abort_merge),
132-
(self.menu_fetch, guicmds.fetch_slot(self)),
133-
(self.menu_push, guicmds.push_slot(self)),
134-
(self.menu_pull, guicmds.pull_slot(self)),
135-
(self.menu_open_repo, guicmds.open_repo_slot(self)),
136-
(self.menu_options, update_options),
137-
(self.menu_rescan, SLOT(signals.rescan)),
138-
(self.menu_grep, guicmds.grep),
139-
(self.menu_search_commits, smod.search),
140-
(self.menu_show_diffstat, SLOT(signals.diffstat)),
141-
(self.menu_stash, lambda: stash.stash(parent=self)),
142-
(self.menu_stage_modified, SLOT(signals.stage_modified)),
143-
(self.menu_stage_untracked, SLOT(signals.stage_untracked)),
144-
(self.menu_unstage_selected, SLOT(signals.unstage_selected)),
145-
(self.menu_unstage_all, SLOT(signals.unstage_all)),
146-
(self.menu_visualize_all, SLOT(signals.visualize_all)),
147-
(self.menu_visualize_current, SLOT(signals.visualize_current)),
148-
# TODO This edit menu stuff should/could be command objects
149-
(self.menu_cut, self.action_cut),
150-
(self.menu_copy, self.action_copy),
151-
(self.menu_paste, self.commitmsg.paste),
152-
(self.menu_delete, self.action_delete),
153-
(self.menu_select_all, self.commitmsg.selectAll),
154-
(self.menu_undo, self.commitmsg.undo),
155-
(self.menu_redo, self.commitmsg.redo),
156-
(self.menu_classic, classic.cola_classic),
157-
(self.menu_dag, lambda: dag.git_dag(self.model, self)),
158-
]
159-
160-
# Diff Actions
161-
if hasattr(Qt, 'WidgetWithChildrenShortcut'):
162-
# We can only enable this shortcut on newer versions of Qt
163-
# that support WidgetWithChildrenShortcut otherwise we get
164-
# an ambiguous shortcut error.
165-
self.diff_copy = QtGui.QAction(self.display_text)
166-
self.diff_copy.setShortcut(QtGui.QKeySequence.Copy)
167-
self.diff_copy.setShortcutContext(Qt.WidgetWithChildrenShortcut)
168-
self.display_text.addAction(self.diff_copy)
169-
actions.append((self.diff_copy, self.copy_display,))
170-
171-
for menu, callback in actions:
172-
self.connect(menu, SIGNAL('triggered()'), callback)
173-
17464
# Install UI wrappers for command objects
17565
actionsmod.install_command_wrapper(self)
17666
guicmds.install_command_wrapper(self)
@@ -223,13 +113,6 @@ def _install_config_actions(self, names):
223113
for name in names:
224114
menu.addAction(name, SLOT(signals.run_config_action, name))
225115

226-
def _relay_button(self, button, signal):
227-
callback = SLOT(signal)
228-
self._connect_button(button, callback)
229-
230-
def _connect_button(self, button, callback):
231-
self.connect(button, SIGNAL('clicked()'), callback)
232-
233116
def _update_view(self):
234117
self.emit(SIGNAL('update'))
235118

@@ -266,42 +149,6 @@ def _update_callback(self):
266149
cola.notifier().broadcast(signals.load_commit_message,
267150
core.decode(merge_msg_path))
268151

269-
def _mode_changed(self, mode):
270-
"""React to mode changes; hide/show the "Exit Diff Mode" button."""
271-
if mode in (self.model.mode_review,
272-
self.model.mode_diff,
273-
self.model.mode_diff_expr):
274-
self.alt_button.setMinimumHeight(40)
275-
self.alt_button.show()
276-
else:
277-
self.alt_button.setMinimumHeight(1)
278-
self.alt_button.hide()
279-
280-
def set_display(self, text):
281-
"""Set the diff text display."""
282-
scrollbar = self.display_text.verticalScrollBar()
283-
scrollvalue = scrollbar.value()
284-
if text is not None:
285-
self.display_text.setPlainText(text)
286-
scrollbar.setValue(scrollvalue)
287-
288-
def action_cut(self):
289-
self.action_copy()
290-
self.action_delete()
291-
292-
def action_copy(self):
293-
cursor = self.commitmsg.textCursor()
294-
selection = cursor.selection().toPlainText()
295-
qtutils.set_clipboard(selection)
296-
297-
def action_delete(self):
298-
self.commitmsg.textCursor().removeSelectedText()
299-
300-
def copy_display(self):
301-
cursor = self.display_text.textCursor()
302-
selection = cursor.selection().toPlainText()
303-
qtutils.set_clipboard(selection)
304-
305152
def diff_selection(self):
306153
cursor = self.display_text.textCursor()
307154
offset = cursor.position()
@@ -525,7 +372,9 @@ def diff_context_menu_setup(self):
525372
lambda: guicmds.goto_grep(self.selected_line()))
526373

527374
menu.addSeparator()
528-
menu.addAction(self.tr('Copy'), self.copy_display)
375+
menu.addAction('Copy', self.display_text.copy)
376+
menu.addAction('Select All', self.display_text.selectAll)
377+
529378
return menu
530379

531380
def signoff(self):

0 commit comments

Comments
 (0)