bookmarks: avoid duplicates between the recent and bookmarked lists
[git-cola.git] / cola / widgets / recent.py
blob7c48b59cce8244a06acfaa0fffca9d0da9eb8e9c
1 from PyQt4 import QtGui
2 from PyQt4 import QtCore
3 from PyQt4.QtCore import Qt
4 from PyQt4.QtCore import SIGNAL
6 from cola import cmds
7 from cola import gitcmds
8 from cola import qtutils
9 from cola.i18n import N_
10 from cola.widgets import defs
11 from cola.widgets import standard
12 from cola.widgets.browse import GitTreeWidget
13 from cola.widgets.browse import GitFileTreeModel
16 def browse_recent_files():
17 parent = qtutils.active_window()
18 dialog = RecentFileDialog(parent)
19 dialog.resize(parent.width(), min(parent.height(), 420))
20 dialog.show()
23 class UpdateFileListThread(QtCore.QThread):
24 def __init__(self, count):
25 QtCore.QThread.__init__(self)
26 self.count = count
28 def run(self):
29 ref = 'HEAD~%d' % self.count
30 filenames = gitcmds.diff_index_filenames(ref)
31 self.emit(SIGNAL('filenames'), filenames)
34 class RecentFileDialog(standard.Dialog):
35 def __init__(self, parent):
36 standard.Dialog.__init__(self, parent)
37 self.setWindowTitle(N_('Recently Modified Files'))
38 self.setWindowModality(Qt.WindowModal)
40 count = 8
41 self.update_thread = UpdateFileListThread(count)
43 self.count = QtGui.QSpinBox()
44 self.count.setMinimum(0)
45 self.count.setMaximum(10000)
46 self.count.setValue(count)
47 self.count.setSuffix(N_(' commits ago'))
49 self.count_label = QtGui.QLabel()
50 self.count_label.setText(N_('Showing changes since'))
52 self.refresh_button = QtGui.QPushButton()
53 self.refresh_button.setText(N_('Refresh'))
54 self.refresh_button.setIcon(qtutils.reload_icon())
55 self.refresh_button.setEnabled(False)
57 self.tree = GitTreeWidget(parent=self)
58 self.tree_model = GitFileTreeModel(self)
59 self.tree.setModel(self.tree_model)
61 self.expand_button = QtGui.QPushButton()
62 self.expand_button.setText(N_('Expand'))
63 self.expand_button.setIcon(qtutils.open_icon())
65 self.collapse_button = QtGui.QPushButton()
66 self.collapse_button.setText(N_('Collapse'))
67 self.collapse_button.setIcon(qtutils.dir_close_icon())
69 self.edit_button = QtGui.QPushButton()
70 self.edit_button.setText(N_('Edit'))
71 self.edit_button.setIcon(qtutils.apply_icon())
72 self.edit_button.setDefault(True)
73 self.edit_button.setEnabled(False)
75 self.close_button = QtGui.QPushButton()
76 self.close_button.setText(N_('Close'))
78 toplayout = QtGui.QHBoxLayout()
79 toplayout.setMargin(0)
80 toplayout.setSpacing(defs.spacing)
81 toplayout.addWidget(self.count_label)
82 toplayout.addWidget(self.count)
83 toplayout.addStretch()
84 toplayout.addWidget(self.refresh_button)
86 btnlayout = QtGui.QHBoxLayout()
87 btnlayout.setMargin(0)
88 btnlayout.setSpacing(defs.spacing)
89 btnlayout.addWidget(self.expand_button)
90 btnlayout.addWidget(self.collapse_button)
91 btnlayout.addStretch()
92 btnlayout.addWidget(self.edit_button)
93 btnlayout.addWidget(self.close_button)
95 layout = QtGui.QVBoxLayout()
96 layout.setMargin(defs.margin)
97 layout.setSpacing(defs.spacing)
98 layout.addLayout(toplayout)
99 layout.addWidget(self.tree)
100 layout.addLayout(btnlayout)
101 self.setLayout(layout)
103 self.connect(self.tree, SIGNAL('selectionChanged()'),
104 self.selection_changed)
106 self.connect(self.tree, SIGNAL('path_chosen'), self.edit_file)
108 self.connect(self.count, SIGNAL('valueChanged(int)'),
109 self.count_changed)
111 self.connect(self.count, SIGNAL('editingFinished()'), self.refresh)
113 self.connect(self.update_thread, SIGNAL('filenames'),
114 self.set_filenames)
116 qtutils.connect_button(self.refresh_button, self.refresh)
117 qtutils.connect_button(self.expand_button, self.tree.expandAll)
118 qtutils.connect_button(self.collapse_button, self.tree.collapseAll)
119 qtutils.connect_button(self.close_button, self.accept)
120 qtutils.connect_button(self.edit_button, self.edit_selected)
122 qtutils.add_action(self, N_('Refresh'), self.refresh, 'Ctrl+R')
124 self.update_thread.start()
126 def edit_selected(self):
127 filenames = self.tree.selected_files()
128 if not filenames:
129 return
130 self.edit_files(filenames)
132 def edit_files(self, filenames):
133 cmds.do(cmds.Edit, filenames)
135 def edit_file(self, filename):
136 self.edit_files([filename])
138 def refresh(self):
139 self.refresh_button.setEnabled(False)
140 self.count.setEnabled(False)
141 self.tree_model.clear()
142 self.tree.setEnabled(False)
144 self.update_thread.count = self.count.value()
145 self.update_thread.start()
147 def count_changed(self, value):
148 self.refresh_button.setEnabled(True)
150 def selection_changed(self):
151 """Update actions based on the current selection"""
152 filenames = self.tree.selected_files()
153 self.edit_button.setEnabled(bool(filenames))
155 def set_filenames(self, filenames):
156 self.count.setEnabled(True)
157 self.tree.setEnabled(True)
158 self.tree_model.clear()
159 self.tree_model.add_files(filenames)
160 self.tree.expandAll()
161 self.tree.select_first_file()
162 self.tree.setFocus()