bookmarks: avoid duplicates between the recent and bookmarked lists
[git-cola.git] / cola / widgets / bookmarks.py
blob9e2f3fc361c43de1a0823d2bc3d376cc3746f9fb
1 """Provides widgets related to bookmarks"""
2 import os
3 import sys
5 from PyQt4 import QtGui
6 from PyQt4.QtCore import Qt
7 from PyQt4.QtCore import SIGNAL
10 from cola import cmds
11 from cola import core
12 from cola import qtutils
13 from cola import settings
14 from cola.compat import set
15 from cola.i18n import N_
16 from cola.settings import Settings
17 from cola.widgets import defs
18 from cola.widgets import standard
21 def manage_bookmarks():
22 dlg = BookmarksDialog(qtutils.active_window())
23 dlg.show()
24 dlg.exec_()
25 return dlg
28 class BookmarksDialog(standard.Dialog):
29 def __init__(self, parent):
30 standard.Dialog.__init__(self, parent=parent)
31 self.model = settings.Settings()
33 self.resize(494, 238)
34 self.setWindowTitle(N_('Bookmarks'))
35 if parent is not None:
36 self.setWindowModality(Qt.WindowModal)
37 self.layt = QtGui.QVBoxLayout(self)
38 self.layt.setMargin(defs.margin)
39 self.layt.setSpacing(defs.spacing)
41 self.bookmarks = QtGui.QListWidget(self)
42 self.bookmarks.setAlternatingRowColors(True)
43 self.bookmarks.setSelectionMode(QtGui.QAbstractItemView
44 .ExtendedSelection)
46 self.layt.addWidget(self.bookmarks)
47 self.button_layout = QtGui.QHBoxLayout()
49 self.open_button = qtutils.create_button(text=N_('Open'),
50 icon=qtutils.open_icon())
51 self.open_button.setEnabled(False)
52 self.button_layout.addWidget(self.open_button)
54 self.add_button = qtutils.create_button(text=N_('Add'),
55 icon=qtutils.add_icon())
56 self.button_layout.addWidget(self.add_button)
58 self.delete_button = QtGui.QPushButton(self)
59 self.delete_button.setText(N_('Delete'))
60 self.delete_button.setIcon(qtutils.discard_icon())
61 self.delete_button.setEnabled(False)
62 self.button_layout.addWidget(self.delete_button)
63 self.button_layout.addStretch()
65 self.save_button = QtGui.QPushButton(self)
66 self.save_button.setText(N_('Save'))
67 self.save_button.setIcon(qtutils.save_icon())
68 self.save_button.setEnabled(False)
69 self.button_layout.addWidget(self.save_button)
71 self.close_button = QtGui.QPushButton(self)
72 self.close_button.setText(N_('Close'))
73 self.button_layout.addWidget(self.close_button)
75 self.layt.addLayout(self.button_layout)
77 self.connect(self.bookmarks, SIGNAL('itemSelectionChanged()'),
78 self.item_selection_changed)
80 qtutils.connect_button(self.open_button, self.open_repo)
81 qtutils.connect_button(self.add_button, self.add)
82 qtutils.connect_button(self.delete_button, self.delete)
83 qtutils.connect_button(self.save_button, self.save)
84 qtutils.connect_button(self.close_button, self.accept)
86 self.update_bookmarks()
88 def update_bookmarks(self):
89 self.bookmarks.clear()
90 self.bookmarks.addItems(self.model.bookmarks)
92 def selection(self):
93 return qtutils.selection_list(self.bookmarks, self.model.bookmarks)
95 def item_selection_changed(self):
96 has_selection = bool(self.selection())
97 self.open_button.setEnabled(has_selection)
98 self.delete_button.setEnabled(has_selection)
100 def save(self):
101 """Saves the bookmarks settings and exits"""
102 self.model.save()
103 self.save_button.setEnabled(False)
105 def add(self):
106 path, ok = qtutils.prompt(N_('Path to git repository'),
107 title=N_('Enter Git Repository'),
108 text=core.getcwd())
109 if not ok:
110 return
111 self.model.bookmarks.append(path)
112 self.update_bookmarks()
113 self.save()
115 def open_repo(self):
116 """Opens a new git-cola session on a bookmark"""
117 for repo in self.selection():
118 core.fork([sys.executable, sys.argv[0], '--repo', repo])
120 def delete(self):
121 """Removes a bookmark from the bookmarks list"""
122 selection = self.selection()
123 if not selection:
124 return
125 for repo in selection:
126 self.model.remove_bookmark(repo)
127 self.update_bookmarks()
128 self.save_button.setEnabled(True)
131 class BookmarksWidget(QtGui.QWidget):
133 def __init__(self, parent=None):
134 QtGui.QWidget.__init__(self, parent)
136 self.tree = BookmarksTreeWidget(parent=self)
137 self.open_button = qtutils.create_action_button(
138 N_('Open'), qtutils.open_icon())
139 self.open_button.setEnabled(False)
141 self.open_action = qtutils.add_action(self,
142 N_('Open'), self.open_repo, 'Return')
143 self.open_action.setEnabled(False)
145 self.edit_button = qtutils.create_action_button(
146 N_('Bookmarks...'), qtutils.add_icon())
148 qtutils.connect_button(self.open_button, self.open_repo)
149 qtutils.connect_button(self.edit_button, self.manage_bookmarks)
151 self.connect(self.tree, SIGNAL('itemSelectionChanged()'),
152 self._tree_selection_changed)
154 self.connect(self.tree,
155 SIGNAL('itemDoubleClicked(QTreeWidgetItem*,int)'),
156 self._tree_double_clicked)
158 self.button_layout = QtGui.QHBoxLayout()
159 self.button_layout.setMargin(defs.no_margin)
160 self.button_layout.setSpacing(defs.spacing)
161 self.button_layout.addWidget(self.open_button)
162 self.button_layout.addWidget(self.edit_button)
164 self.layout = QtGui.QVBoxLayout()
165 self.layout.setMargin(defs.no_margin)
166 self.layout.setSpacing(defs.spacing)
167 self.layout.addWidget(self.tree)
168 self.setLayout(self.layout)
170 self.corner_widget = QtGui.QWidget(self)
171 self.corner_widget.setLayout(self.button_layout)
172 titlebar = parent.titleBarWidget()
173 titlebar.add_corner_widget(self.corner_widget)
174 self.setFocusProxy(self.tree)
176 def _tree_selection_changed(self):
177 enabled = bool(self.tree.selected_item())
178 self.open_button.setEnabled(enabled)
179 self.open_action.setEnabled(enabled)
181 def open_repo(self):
182 item = self.tree.selected_item()
183 if not item:
184 return
185 cmds.do(cmds.OpenRepo, item.path)
187 def _tree_double_clicked(self, item, column):
188 cmds.do(cmds.OpenRepo, item.path)
190 def manage_bookmarks(self):
191 manage_bookmarks()
192 self.refresh()
194 def refresh(self):
195 self.tree.refresh()
198 class BookmarksTreeWidget(standard.TreeWidget):
200 def __init__(self, parent=None):
201 standard.TreeWidget.__init__(self, parent=parent)
202 self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
203 self.setHeaderHidden(True)
204 self.refresh()
206 def refresh(self):
207 self.clear()
208 settings = Settings()
209 items = []
210 icon = qtutils.dir_icon()
211 recents = set(settings.recent)
212 for path in settings.recent:
213 item = BookmarksTreeWidgetItem(path, icon)
214 items.append(item)
215 for path in settings.bookmarks:
216 if path in recents: # avoid duplicates
217 continue
218 item = BookmarksTreeWidgetItem(path, icon)
219 items.append(item)
220 self.addTopLevelItems(items)
223 class BookmarksTreeWidgetItem(QtGui.QTreeWidgetItem):
225 def __init__(self, path, icon):
226 QtGui.QTreeWidgetItem.__init__(self)
227 self.path = path
228 self.setIcon(0, icon)
229 self.setText(0, os.path.basename(path))
230 self.setToolTip(0, path)