4 from PyQt4
.QtGui
import QDialog
5 from PyQt4
.QtGui
import QFont
8 from cola
import qtutils
9 from cola
.model
import Model
10 from cola
.views
import ListView
11 from cola
.views
import ComboView
12 from cola
.views
import CommitView
13 from cola
.views
import OptionsView
14 from cola
.views
import LogView
15 from cola
.qobserver
import QObserver
17 def set_diff_font(model
, widget
):
18 if model
.has_param('global_cola_fontdiff'):
19 font
= model
.get_param('global_cola_fontdiff')
26 def choose_from_combo(title
, parent
, branches
):
27 return ComboView(parent
, title
, branches
).get_selected()
29 def choose_from_list(title
, parent
, items
=[]):
30 return ListView(parent
, title
, items
).get_selected()
32 #+-------------------------------------------------------------
33 def select_commits(model
, parent
, title
, revs
, summaries
):
34 """Use the CommitView to select commits from a list."""
36 model
.set_revisions(revs
)
37 model
.set_summaries(summaries
)
38 view
= CommitView(parent
, title
)
39 ctl
= SelectCommitsController(model
, view
)
40 return ctl
.select_commits()
42 class SelectCommitsController(QObserver
):
43 def init(self
, model
, view
):
44 set_diff_font(model
, view
.commit_text
)
45 self
.connect(view
.commit_list
,
46 'itemSelectionChanged()',
47 self
.commit_sha1_selected
)
49 def select_commits(self
):
50 summaries
= self
.model
.get_summaries()
52 msg
= self
.tr('No commits exist in this branch.')
53 qtutils
.show_output(msg
)
55 qtutils
.set_items(self
.view
.commit_list
, summaries
)
57 if self
.view
.exec_() != QDialog
.Accepted
:
59 revs
= self
.model
.get_revisions()
60 list_widget
= self
.view
.commit_list
61 return qtutils
.get_selection_list(list_widget
, revs
)
63 def commit_sha1_selected(self
):
64 row
, selected
= qtutils
.get_selected_row(self
.view
.commit_list
)
66 self
.view
.commit_text
.setText('')
67 self
.view
.revision
.setText('')
69 # Get the sha1 and put it in the revision line
70 sha1
= self
.model
.get_revision_sha1(row
)
71 self
.view
.revision
.setText(sha1
)
72 self
.view
.revision
.selectAll()
74 # Lookup the sha1's commit
75 commit_diff
= self
.model
.get_commit_diff(sha1
)
76 self
.view
.commit_text
.setText(commit_diff
)
78 # Copy the sha1 into the clipboard
79 qtutils
.set_clipboard(sha1
)
81 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
82 #+ The Options GUI Controller
83 def update_options(model
, parent
):
84 view
= OptionsView(parent
)
85 ctl
= OptionsController(model
,view
)
87 return view
.exec_() == QDialog
.Accepted
89 class OptionsController(QObserver
):
91 This controller assumes that the view's widgets are named
92 the same as the model parameters."""
94 def init(self
,model
,view
):
95 # used for telling about interactive font changes
96 self
.original_model
= model
98 self
.add_observables('local_user_email',
100 'local_merge_summary',
101 'local_merge_diffstat',
102 'local_merge_verbosity',
103 'local_gui_diffcontext',
106 'global_merge_summary',
107 'global_merge_diffstat',
108 'global_merge_verbosity',
110 'global_gui_diffeditor',
111 'global_gui_diffcontext',
112 'global_gui_historybrowser',
113 'global_cola_fontdiffsize',
114 'global_cola_fontdiff',
115 'global_cola_fontuisize',
116 'global_cola_fontui',
117 'global_cola_savewindowsettings',
118 'global_cola_saveatexit')
119 self
.add_actions(global_cola_fontdiffsize
= self
.update_size
)
120 self
.add_actions(global_cola_fontuisize
= self
.update_size
)
121 self
.add_actions(global_cola_fontdiff
= self
.tell_parent_model
)
122 self
.add_actions(global_cola_fontui
= self
.tell_parent_model
)
123 self
.add_callbacks(save_button
= self
.save_settings
)
124 self
.connect(self
.view
, 'rejected()', self
.restore_settings
)
127 self
.backup_model
= self
.model
.clone()
129 def refresh_view(self
):
130 font
= self
.model
.get_param('global_cola_fontui')
132 size
= int(font
.split(',')[1])
133 self
.view
.global_cola_fontuisize
.setValue(size
)
134 self
.model
.set_global_cola_fontuisize(size
)
136 fontui
.fromString(font
)
137 self
.view
.global_cola_fontui
.setCurrentFont(fontui
)
139 font
= self
.model
.get_global_cola_fontdiff()
141 size
= int(font
.split(',')[1])
142 self
.view
.global_cola_fontdiffsize
.setValue(size
)
143 self
.model
.set_global_cola_fontdiffsize(size
)
145 fontdiff
.fromString(font
)
146 self
.view
.global_cola_fontdiff
.setCurrentFont(fontdiff
)
148 self
.view
.local_groupbox
.setTitle(unicode(self
.tr('%s Repository'))
149 % self
.model
.get_project())
150 QObserver
.refresh_view(self
)
153 def save_settings(self
):
155 params
= self
.model
.get_config_params()
157 value
= self
.model
.get_param(param
)
158 backup
= self
.backup_model
.get_param(param
)
160 params_to_save
.append(param
)
161 for param
in params_to_save
:
162 self
.model
.save_config_param(param
)
164 self
.original_model
.copy_params(self
.model
, params_to_save
)
165 self
.view
.done(QDialog
.Accepted
)
167 # cancel button -> undo changes
168 def restore_settings(self
):
169 params
= self
.backup_model
.get_config_params()
170 self
.model
.copy_params(self
.backup_model
, params
)
171 self
.tell_parent_model()
173 def tell_parent_model(self
,*rest
):
174 params
= ('global_cola_fontdiff',
175 'global_cola_fontui',
176 'global_cola_fontdiffsize',
177 'global_cola_fontuisize',
178 'global_cola_savewindowsettings',
179 'global_cola_saveatexit')
181 self
.original_model
.set_param(
182 param
, self
.model
.get_param(param
))
184 def update_size(self
, *rest
):
185 combo
= self
.view
.global_cola_fontui
186 param
= str(combo
.objectName())
187 default
= str(combo
.currentFont().toString())
188 self
.model
.apply_font_size(param
, default
)
190 combo
= self
.view
.global_cola_fontdiff
191 param
= str(combo
.objectName())
192 default
= str(combo
.currentFont().toString())
193 self
.model
.apply_font_size(param
, default
)
195 self
.tell_parent_model()
197 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
198 #+ The Log GUI Controller
200 model
= Model(search_text
= '')
202 ctl
= LogController(model
,view
)
205 class LogController(QObserver
):
206 def init(self
, model
, view
):
207 self
.add_observables('search_text')
208 self
.add_actions(search_text
= self
.insta_search
)
209 self
.add_callbacks(clear_button
= self
.clear
,
210 next_button
= self
.next
,
211 prev_button
= self
.prev
)
212 self
.connect(self
.view
.output_text
,
213 'cursorPositionChanged()',
214 self
.cursor_position_changed
)
215 self
.search_offset
= 0
217 def insta_search(self
,*rest
):
218 self
.search_offset
= 0
219 txt
= self
.model
.get_search_text().lower()
223 cursor
= self
.view
.output_text
.textCursor()
224 cursor
.clearSelection()
225 self
.view
.output_text
.setTextCursor(cursor
)
228 self
.view
.output_text
.clear()
229 self
.search_offset
= 0
232 text
= self
.model
.get_search_text().lower().strip()
235 output
= str(self
.view
.output_text
.toPlainText())
236 if self
.search_offset
+ len(text
) > len(output
):
237 title
= unicode(self
.tr('%s not found')) % text
238 question
= unicode(self
.tr("Could not find '%s'.\n"
239 'Search from the beginning?')) % text
240 if qtutils
.question(self
.view
, title
, question
, default
=False):
241 self
.search_offset
= 0
245 find_in
= output
[self
.search_offset
:].lower()
247 index
= find_in
.index(text
)
249 self
.search_offset
= 0
250 title
= unicode(self
.tr("%s not found")) % text
251 question
= unicode(self
.tr("Could not find '%s'.\n"
252 'Search from the beginning?')) % text
253 if qtutils
.question(self
.view
, title
, default
=False):
256 cursor
= self
.view
.output_text
.textCursor()
257 offset
= self
.search_offset
+ index
258 new_offset
= offset
+ len(text
)
260 cursor
.setPosition(offset
)
261 cursor
.setPosition(new_offset
, cursor
.KeepAnchor
)
263 self
.view
.output_text
.setTextCursor(cursor
)
264 self
.search_offset
= new_offset
267 text
= self
.model
.get_search_text().lower().strip()
270 output
= str(self
.view
.output_text
.toPlainText())
271 if self
.search_offset
== 0:
272 self
.search_offset
= len(output
)
274 find_in
= output
[:self
.search_offset
].lower()
276 offset
= find_in
.rindex(text
)
278 self
.search_offset
= 0
279 title
= unicode(self
.tr('%s not found')) % text
280 question
= unicode(self
.tr("Could not find '%s'.\n"
281 'Search from the end?')) % text
282 if qtutils
.question(self
.view
, title
, question
):
285 cursor
= self
.view
.output_text
.textCursor()
286 new_offset
= offset
+ len(text
)
288 cursor
.setPosition(offset
)
289 cursor
.setPosition(new_offset
, cursor
.KeepAnchor
)
291 self
.view
.output_text
.setTextCursor(cursor
)
292 self
.search_offset
= offset
294 def cursor_position_changed(self
):
295 cursor
= self
.view
.output_text
.textCursor()
296 self
.search_offset
= cursor
.selectionStart()