From d1f7b7c0450e02e3e184455b10ce36a0dd1fc1ce Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sat, 3 Oct 2009 15:22:02 -0700 Subject: [PATCH] cola: Integrate the SelectionModel Selection is now handled by a static SelectionModel instance. This allows individual tools to modify the selection and have the selection reflected in the corresponding menus and actions. Closes #19 Signed-off-by: David Aguilar --- cola/views/main.py | 27 +++------------------------ cola/views/repo.py | 39 ++++++++++++++++++++++++++++++++++++++- cola/views/status.py | 11 ++++++++++- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/cola/views/main.py b/cola/views/main.py index 04500217..fe8f915b 100644 --- a/cola/views/main.py +++ b/cola/views/main.py @@ -16,7 +16,6 @@ from cola import signals from cola import resources from cola.qtutils import SLOT from cola.views import about -from cola.views import status from cola.views.syntax import DiffSyntaxHighlighter from cola.views.mainwindow import MainWindow from cola.controllers import compare @@ -223,24 +222,6 @@ class MainView(MainWindow): self.display_text.setText(text) scrollbar.setValue(scrollvalue) - def single_selection(self): - """Scan across staged, modified, etc. and return a single item.""" - # TODO have selection in the model - staged, modified, unmerged, untracked = status.widget().selection() - s = None - m = None - um = None - ut = None - if staged: - s = staged[0] - elif modified: - m = modified[0] - elif unmerged: - um = unmerged[0] - elif untracked: - ut = untracked[0] - return s, m, um, ut - def action_cut(self): self.action_copy() self.action_delete() @@ -387,7 +368,7 @@ class MainView(MainWindow): if event.key() != QtCore.Qt.Key_H and event.key() != QtCore.Qt.Key_S: event.ignore() return - staged, modified, unmerged, untracked = self.single_selection() + staged, modified, unmerged, untracked = cola.single_selection() if event.key() == QtCore.Qt.Key_H: if self.mode == self.model.mode_worktree and modified: self.stage_hunk() @@ -437,8 +418,7 @@ class MainView(MainWindow): def stage(self): """Stage selected files.""" - # TODO move selection into the model so that we're widget-independent - paths = status.widget().unstaged() + paths = cola.selection_model().unstaged if not paths: cola.notifier().broadcast(signals.stage_modified) else: @@ -469,8 +449,7 @@ class MainView(MainWindow): def diff_context_menu_setup(self): """Set up the context menu for the diff display.""" menu = QtGui.QMenu(self) - # TODO selection in the model - staged, modified, unmerged, untracked = status.widget().selection() + staged, modified, unmerged, untracked = cola.selection() if self.mode == self.model.mode_worktree: if modified: diff --git a/cola/views/repo.py b/cola/views/repo.py index c6cef9f0..30471dc1 100644 --- a/cola/views/repo.py +++ b/cola/views/repo.py @@ -155,6 +155,9 @@ class RepoTreeView(QtGui.QTreeView): # instead of the original index. result = QtGui.QTreeView.keyPressEvent(self, event) + # Sync the selection model + self.sync_selection() + # Try to select the first item if the model index is invalid if not index.isValid(): index = self.model().index(0, 0, QtCore.QModelIndex()) @@ -181,12 +184,46 @@ class RepoTreeView(QtGui.QTreeView): return result + def mousePressEvent(self, event): + """Synchronize the selection on mouse-press.""" + result = QtGui.QTreeView.mousePressEvent(self, event) + self.sync_selection() + return result + + def sync_selection(self): + """Push selection into the selection model.""" + staged = [] + modified = [] + unmerged = [] + untracked = [] + paths = self.selected_paths() + + model = cola.model() + model_staged = set(model.staged) + model_modified = set(model.modified) + model_unmerged = set(model.unmerged) + model_untracked = set(model.untracked) + + for path in paths: + if path in model_unmerged: + unmerged.append(path) + elif path in model_untracked: + untracked.append(path) + elif path in model_staged: + staged.append(path) + elif path in model_modified: + modified.append(path) + # Push the new selection into the model. + cola.selection_model().set_selection(staged, modified, + unmerged, untracked) + return paths + def selectionChanged(self, old_selection, new_selection): """Override selectionChanged to update available actions.""" result = QtGui.QTreeView.selectionChanged(self, old_selection, new_selection) self.update_actions() + paths = self.sync_selection() - paths = self.selected_paths() if paths and self.model().path_is_interesting(paths[0]): cached = paths[0] in cola.model().staged cola.notifier().broadcast(signals.diff, paths, cached) diff --git a/cola/views/status.py b/cola/views/status.py index 0b6c9094..8a235d33 100644 --- a/cola/views/status.py +++ b/cola/views/status.py @@ -366,8 +366,13 @@ class StatusWidget(QtGui.QDialog): the same appropriate action. """ - # Get the item that was clicked result = QtGui.QTreeWidget.mousePressEvent(self.tree, event) + + # Sync the selection model + s, m, um, ut = self.selection() + cola.selection_model().set_selection(s, m, um, ut) + + # Get the item that was clicked item = self.tree.itemAt(event.pos()) if not item: # Nothing was clicked -- reset the display and return @@ -413,6 +418,10 @@ class StatusWidget(QtGui.QDialog): def tree_selection(self): """Show a data for the selected item.""" + # Sync the selection model + s, m, um, ut = self.selection() + cola.selection_model().set_selection(s, m, um, ut) + selection = self.selected_indexes() if not selection: return -- 2.11.4.GIT