widgets.remote: retain focus when showing remote messages
[git-cola.git] / cola / widgets / recent.py
blob6573b4159ae93ad204344424a0fff4bc47ffbd42
1 from qtpy import QtCore
2 from qtpy import QtWidgets
3 from qtpy.QtCore import Qt
4 from qtpy.QtCore import Signal
6 from ..i18n import N_
7 from ..qtutils import get
8 from .. import cmds
9 from .. import gitcmds
10 from .. import hotkeys
11 from .. import icons
12 from .. import qtutils
13 from .browse import GitTreeWidget
14 from .browse import GitFileTreeModel
15 from . import defs
16 from . import standard
19 def browse_recent_files(context):
20 dialog = RecentFiles(context, parent=qtutils.active_window())
21 dialog.show()
22 return dialog
25 class UpdateFileListThread(QtCore.QThread):
26 result = Signal(object)
28 def __init__(self, context, count):
29 QtCore.QThread.__init__(self)
30 self.context = context
31 self.count = count
33 def run(self):
34 context = self.context
35 ref = 'HEAD~%d' % self.count
36 filenames = gitcmds.diff_index_filenames(context, ref)
37 self.result.emit(filenames)
40 class RecentFiles(standard.Dialog):
41 def __init__(self, context, parent=None):
42 standard.Dialog.__init__(self, parent=parent)
43 self.context = context
44 self.setWindowTitle(N_('Recently Modified Files'))
45 if parent is not None:
46 self.setWindowModality(Qt.WindowModal)
48 count = 8
49 self.update_thread = UpdateFileListThread(context, count)
51 self.count = standard.SpinBox(
52 value=count, maxi=10000, suffix=N_(' commits ago')
55 self.count_label = QtWidgets.QLabel()
56 self.count_label.setText(N_('Showing changes since'))
58 self.refresh_button = qtutils.refresh_button(enabled=False)
60 self.tree = GitTreeWidget(parent=self)
61 self.tree_model = GitFileTreeModel(self)
62 self.tree.setModel(self.tree_model)
64 self.expand_button = qtutils.create_button(
65 text=N_('Expand all'), icon=icons.unfold()
68 self.collapse_button = qtutils.create_button(
69 text=N_('Collapse all'), icon=icons.fold()
72 self.edit_button = qtutils.edit_button(enabled=False, default=True)
73 self.close_button = qtutils.close_button()
75 self.top_layout = qtutils.hbox(
76 defs.no_margin,
77 defs.spacing,
78 self.count_label,
79 self.count,
80 qtutils.STRETCH,
81 self.refresh_button,
84 self.button_layout = qtutils.hbox(
85 defs.no_margin,
86 defs.spacing,
87 self.expand_button,
88 self.collapse_button,
89 qtutils.STRETCH,
90 self.close_button,
91 self.edit_button,
94 self.main_layout = qtutils.vbox(
95 defs.margin, defs.spacing, self.top_layout, self.tree, self.button_layout
97 self.setLayout(self.main_layout)
99 # pylint: disable=no-member
100 self.tree.selection_changed.connect(self.tree_selection_changed)
101 self.tree.path_chosen.connect(self.edit_file)
102 self.count.valueChanged.connect(self.count_changed)
103 self.count.editingFinished.connect(self.refresh)
105 thread = self.update_thread
106 thread.result.connect(self.set_filenames, type=Qt.QueuedConnection)
108 qtutils.connect_button(self.refresh_button, self.refresh)
109 qtutils.connect_button(self.expand_button, self.tree.expandAll)
110 qtutils.connect_button(self.collapse_button, self.tree.collapseAll)
111 qtutils.connect_button(self.close_button, self.accept)
112 qtutils.connect_button(self.edit_button, self.edit_selected)
114 qtutils.add_action(self, N_('Refresh'), self.refresh, hotkeys.REFRESH)
116 self.update_thread.start()
117 self.init_size(parent=parent)
119 def edit_selected(self):
120 filenames = self.tree.selected_files()
121 if not filenames:
122 return
123 self.edit_files(filenames)
125 def edit_files(self, filenames):
126 cmds.do(cmds.Edit, self.context, filenames)
128 def edit_file(self, filename):
129 self.edit_files([filename])
131 def refresh(self):
132 self.refresh_button.setEnabled(False)
133 self.count.setEnabled(False)
134 self.tree_model.clear()
135 self.tree.setEnabled(False)
137 self.update_thread.count = get(self.count)
138 self.update_thread.start()
140 def count_changed(self, _value):
141 self.refresh_button.setEnabled(True)
143 def tree_selection_changed(self):
144 """Update actions based on the current selection"""
145 filenames = self.tree.selected_files()
146 self.edit_button.setEnabled(bool(filenames))
148 def set_filenames(self, filenames):
149 self.count.setEnabled(True)
150 self.tree.setEnabled(True)
151 self.tree_model.clear()
152 self.tree_model.add_files(filenames)
153 self.tree.expandAll()
154 self.tree.select_first_file()
155 self.tree.setFocus()