1 """A GUI for selecting commits"""
2 from __future__
import division
, absolute_import
, unicode_literals
4 from qtpy
import QtWidgets
5 from qtpy
.QtCore
import Qt
10 from ..icons
import folder
11 from ..interaction
import Interaction
13 from .diff
import DiffTextEdit
14 from .standard
import Dialog
17 def select_commits(context
, title
, revs
, summaries
, multiselect
=True):
18 """Use the SelectCommits to select commits from a list."""
19 model
= Model(revs
, summaries
)
20 parent
= qtutils
.active_window()
21 dialog
= SelectCommits(
22 context
, model
, parent
, title
, multiselect
=multiselect
)
23 return dialog
.select_commits()
26 def select_commits_and_output(context
, title
, revs
, summaries
,
28 """Select commits from a list and output path"""
29 model
= Model(revs
, summaries
)
30 parent
= qtutils
.active_window()
31 dialog
= SelectCommitsAndOutput(context
, model
, parent
, title
,
32 multiselect
=multiselect
)
33 return dialog
.select_commits_and_output()
38 def __init__(self
, revs
, summaries
):
40 self
.summaries
= summaries
43 class SelectCommits(Dialog
):
45 def __init__(self
, context
, model
,
46 parent
=None, title
=None, multiselect
=True):
47 Dialog
.__init
__(self
, parent
)
48 self
.context
= context
51 self
.setWindowTitle(title
)
54 mode
= QtWidgets
.QAbstractItemView
.ExtendedSelection
56 mode
= QtWidgets
.QAbstractItemView
.SingleSelection
57 commits
= self
.commits
= QtWidgets
.QListWidget()
58 commits
.setSelectionMode(mode
)
59 commits
.setAlternatingRowColors(True)
61 self
.commit_text
= DiffTextEdit(context
, self
, whitespace
=False)
63 self
.label
= QtWidgets
.QLabel()
64 self
.label
.setText(N_('Revision Expression:'))
65 self
.revision
= QtWidgets
.QLineEdit()
66 self
.revision
.setReadOnly(True)
68 self
.select_button
= qtutils
.ok_button(N_('Select'), enabled
=False)
69 self
.close_button
= qtutils
.close_button()
71 # Make the list widget slightly larger
72 self
.splitter
= qtutils
.splitter(Qt
.Vertical
,
73 self
.commits
, self
.commit_text
)
74 self
.splitter
.setSizes([100, 150])
76 self
.input_layout
= qtutils
.hbox(defs
.no_margin
, defs
.button_spacing
,
79 self
.label
, self
.revision
,
82 self
.main_layout
= qtutils
.vbox(defs
.margin
, defs
.margin
,
83 self
.splitter
, self
.input_layout
)
84 self
.setLayout(self
.main_layout
)
86 commits
.itemSelectionChanged
.connect(self
.commit_oid_selected
)
87 commits
.itemDoubleClicked
.connect(self
.commit_oid_double_clicked
)
89 qtutils
.connect_button(self
.select_button
, self
.accept
)
90 qtutils
.connect_button(self
.close_button
, self
.reject
)
92 self
.init_state(None, self
.resize_widget
, parent
)
94 def resize_widget(self
, parent
):
95 """Set the initial size of the widget"""
96 width
, height
= qtutils
.default_size(parent
, 720, 480)
97 self
.resize(width
, height
)
99 def selected_commit(self
):
100 return qtutils
.selected_item(self
.commits
, self
.model
.revisions
)
102 def selected_commits(self
):
103 return qtutils
.selected_items(self
.commits
, self
.model
.revisions
)
105 def select_commits(self
):
106 summaries
= self
.model
.summaries
108 msg
= N_('No commits exist in this branch.')
111 qtutils
.set_items(self
.commits
, summaries
)
113 if self
.exec_() != QtWidgets
.QDialog
.Accepted
:
115 return self
.selected_commits()
117 def commit_oid_selected(self
):
118 context
= self
.context
119 oid
= self
.selected_commit()
120 selected
= oid
is not None
121 self
.select_button
.setEnabled(selected
)
123 self
.commit_text
.set_value('')
124 self
.revision
.setText('')
126 self
.revision
.setText(oid
)
127 self
.revision
.selectAll()
128 # Display the oid's commit
129 commit_diff
= gitcmds
.commit_diff(context
, oid
)
130 self
.commit_text
.setText(commit_diff
)
132 def commit_oid_double_clicked(self
, _item
):
133 oid
= self
.selected_commit()
138 class SelectCommitsAndOutput(SelectCommits
):
140 def __init__(self
, context
, model
,
141 parent
=None, title
=None, multiselect
=True):
142 SelectCommits
.__init
__(
143 self
, context
, model
, parent
, title
, multiselect
)
145 self
.output_dir
= 'output'
146 self
.select_output
= qtutils
.create_button(
147 tooltip
=N_('Select output dir'), icon
=folder())
148 self
.output_text
= QtWidgets
.QLineEdit()
149 self
.output_text
.setReadOnly(True)
150 self
.output_text
.setText(self
.output_dir
)
152 output_layout
= qtutils
.hbox(defs
.no_margin
, defs
.no_spacing
,
156 self
.input_layout
.insertLayout(1, output_layout
)
157 qtutils
.connect_button(self
.select_output
, self
.show_output_dialog
)
159 def select_commits_and_output(self
):
160 to_export
= SelectCommits
.select_commits(self
)
161 output
= self
.output_dir
163 return {'to_export': to_export
, 'output': output
}
165 def show_output_dialog(self
):
166 self
.output_dir
= qtutils
.opendir_dialog(N_('Select output directory'),
168 if not self
.output_dir
:
171 self
.output_text
.setText(self
.output_dir
)