widgets.recent: Refresh after editing the commit count
[git-cola.git] / cola / widgets / recent.py
blob05600c33aeebfe77bb14a62860cddfe3d236457c
1 from PyQt4 import QtGui
2 from PyQt4 import QtCore
3 from PyQt4.QtCore import SIGNAL
5 import cola
6 from cola import gitcmds
7 from cola import qtutils
8 from cola import signals
9 from cola.widgets import defs
10 from cola.widgets.browse import GitTreeWidget
11 from cola.widgets.browse import GitFileTreeModel
12 from cola.views import standard
15 def browse_recent():
16 parent = qtutils.active_window()
17 dialog = RecentFileDialog(parent)
18 dialog.resize(parent.width(), min(parent.height(), 420))
19 dialog.show()
22 class UpdateFileListThread(QtCore.QThread):
23 def __init__(self):
24 QtCore.QThread.__init__(self)
25 self.count = 0
27 def run(self):
28 ref = 'HEAD~%d' % self.count
29 filenames = gitcmds.diff_index_filenames(ref)
30 self.emit(SIGNAL('filenames'), filenames)
33 class RecentFileDialog(standard.Dialog):
34 def __init__(self, parent):
35 standard.Dialog.__init__(self, parent)
36 self.setWindowTitle(self.tr('Recently Modified Files'))
37 self.setWindowModality(QtCore.Qt.WindowModal)
39 self.update_thread = UpdateFileListThread()
41 self.count = QtGui.QSpinBox()
42 self.count.setMinimum(0)
43 self.count.setMaximum(10000)
44 self.count.setValue(0)
45 self.count.setSuffix(self.tr(' commits ago'))
47 self.count_label = QtGui.QLabel()
48 self.count_label.setText(self.tr('Showing changes since'))
50 self.refresh_button = QtGui.QPushButton()
51 self.refresh_button.setText('Refresh')
52 self.refresh_button.setIcon(qtutils.reload_icon())
53 self.refresh_button.setEnabled(False)
55 self.tree = GitTreeWidget(None, parent=self)
56 self.tree_model = GitFileTreeModel(self)
57 self.tree.setModel(self.tree_model)
59 self.expand_button = QtGui.QPushButton()
60 self.expand_button.setText('Expand')
61 self.expand_button.setIcon(qtutils.open_icon())
63 self.collapse_button = QtGui.QPushButton()
64 self.collapse_button.setText('Collapse')
65 self.collapse_button.setIcon(qtutils.dir_close_icon())
67 self.edit_button = QtGui.QPushButton()
68 self.edit_button.setText('Edit')
69 self.edit_button.setIcon(qtutils.apply_icon())
70 self.edit_button.setDefault(True)
71 self.edit_button.setEnabled(False)
73 self.close_button = QtGui.QPushButton()
74 self.close_button.setText('Close')
76 toplayout = QtGui.QHBoxLayout()
77 toplayout.setMargin(0)
78 toplayout.setSpacing(defs.spacing)
79 toplayout.addWidget(self.count_label)
80 toplayout.addWidget(self.count)
81 toplayout.addStretch()
82 toplayout.addWidget(self.refresh_button)
84 btnlayout = QtGui.QHBoxLayout()
85 btnlayout.setMargin(0)
86 btnlayout.setSpacing(defs.spacing)
87 btnlayout.addWidget(self.expand_button)
88 btnlayout.addWidget(self.collapse_button)
89 btnlayout.addStretch()
90 btnlayout.addWidget(self.edit_button)
91 btnlayout.addWidget(self.close_button)
93 layout = QtGui.QVBoxLayout()
94 layout.setMargin(defs.margin)
95 layout.setSpacing(defs.spacing)
96 layout.addLayout(toplayout)
97 layout.addWidget(self.tree)
98 layout.addLayout(btnlayout)
99 self.setLayout(layout)
101 self.connect(self.tree, SIGNAL('selectionChanged()'),
102 self.selection_changed)
104 self.connect(self.tree, SIGNAL('path_chosen'), self.edit_file)
106 self.connect(self.count, SIGNAL('valueChanged(int)'),
107 self.count_changed)
109 self.connect(self.count, SIGNAL('editingFinished()'), self.refresh)
111 self.connect(self.refresh_button, SIGNAL('clicked()'), self.refresh)
113 self.connect(self.update_thread, SIGNAL('filenames'),
114 self.set_filenames)
116 self.connect(self.expand_button, SIGNAL('clicked()'),
117 self.tree.expandAll)
119 self.connect(self.collapse_button, SIGNAL('clicked()'),
120 self.tree.collapseAll)
122 self.connect(self.close_button, SIGNAL('clicked()'), self.accept)
123 self.connect(self.edit_button, SIGNAL('clicked()'), self.edit_selected)
125 qtutils.add_action(self, 'Refresh', self.refresh, 'Ctrl+R')
127 self.update_thread.count = 0
128 self.update_thread.start()
130 def edit_selected(self):
131 filenames = self.tree.selected_files()
132 if not filenames:
133 return
134 self.edit_files(filenames)
136 def edit_files(self, filenames):
137 cola.notifier().broadcast(signals.edit, filenames)
139 def edit_file(self, filename):
140 self.edit_files([filename])
142 def refresh(self):
143 self.refresh_button.setEnabled(False)
144 self.count.setEnabled(False)
145 self.tree_model.clear()
146 self.tree.setEnabled(False)
148 self.update_thread.count = self.count.value()
149 self.update_thread.start()
151 def count_changed(self, value):
152 self.refresh_button.setEnabled(True)
154 def selection_changed(self):
155 """Update actions based on the current selection"""
156 filenames = self.tree.selected_files()
157 self.edit_button.setEnabled(bool(filenames))
159 def set_filenames(self, filenames):
160 self.count.setEnabled(True)
161 self.tree.setEnabled(True)
162 self.tree_model.clear()
163 self.tree_model.add_files(filenames)
164 self.tree.expandAll()
165 self.tree.setFocus(True)