clone: clone into the parent directory by default
[git-cola.git] / cola / widgets / recent.py
blob4c1a838848f27dbfe9c2335a0f2e8aa60d5c427c
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 self.tree.selection_changed.connect(self.tree_selection_changed)
100 self.tree.path_chosen.connect(self.edit_file)
101 self.count.valueChanged.connect(self.count_changed)
102 self.count.editingFinished.connect(self.refresh)
104 thread = self.update_thread
105 thread.result.connect(self.set_filenames, type=Qt.QueuedConnection)
107 qtutils.connect_button(self.refresh_button, self.refresh)
108 qtutils.connect_button(self.expand_button, self.tree.expandAll)
109 qtutils.connect_button(self.collapse_button, self.tree.collapseAll)
110 qtutils.connect_button(self.close_button, self.accept)
111 qtutils.connect_button(self.edit_button, self.edit_selected)
113 qtutils.add_action(self, N_('Refresh'), self.refresh, hotkeys.REFRESH)
115 self.update_thread.start()
116 self.init_size(parent=parent)
118 def edit_selected(self):
119 filenames = self.tree.selected_files()
120 if not filenames:
121 return
122 self.edit_files(filenames)
124 def edit_files(self, filenames):
125 cmds.do(cmds.Edit, self.context, filenames)
127 def edit_file(self, filename):
128 self.edit_files([filename])
130 def refresh(self):
131 self.refresh_button.setEnabled(False)
132 self.count.setEnabled(False)
133 self.tree_model.clear()
134 self.tree.setEnabled(False)
136 self.update_thread.count = get(self.count)
137 self.update_thread.start()
139 def count_changed(self, _value):
140 self.refresh_button.setEnabled(True)
142 def tree_selection_changed(self):
143 """Update actions based on the current selection"""
144 filenames = self.tree.selected_files()
145 self.edit_button.setEnabled(bool(filenames))
147 def set_filenames(self, filenames):
148 self.count.setEnabled(True)
149 self.tree.setEnabled(True)
150 self.tree_model.clear()
151 self.tree_model.add_files(filenames)
152 self.tree.expandAll()
153 self.tree.select_first_file()
154 self.tree.setFocus()