1 """A GUI for selecting commits"""
3 from qtpy
import QtWidgets
4 from qtpy
.QtCore
import Qt
9 from ..icons
import folder
10 from ..interaction
import Interaction
11 from ..models
import prefs
12 from . import completion
14 from .diff
import DiffTextEdit
15 from .standard
import Dialog
18 def select_commits(context
, title
, revs
, summaries
, multiselect
=True):
19 """Use the SelectCommits to select commits from a list."""
20 model
= Model(revs
, summaries
)
21 parent
= qtutils
.active_window()
22 dialog
= SelectCommits(context
, model
, parent
, title
, multiselect
=multiselect
)
23 return dialog
.select_commits()
26 def select_commits_and_output(context
, title
, revs
, summaries
, multiselect
=True):
27 """Select commits from a list and output path"""
28 model
= Model(revs
, summaries
)
29 parent
= qtutils
.active_window()
30 dialog
= SelectCommitsAndOutput(
31 context
, model
, parent
, title
, multiselect
=multiselect
33 return dialog
.select_commits_and_output()
37 def __init__(self
, revs
, summaries
):
39 self
.summaries
= summaries
42 class SelectCommits(Dialog
):
43 def __init__(self
, context
, model
, parent
=None, title
=None, multiselect
=True):
44 Dialog
.__init
__(self
, parent
)
45 self
.context
= context
48 self
.setWindowTitle(title
)
51 mode
= QtWidgets
.QAbstractItemView
.ExtendedSelection
53 mode
= QtWidgets
.QAbstractItemView
.SingleSelection
54 commits
= self
.commits
= QtWidgets
.QListWidget()
55 commits
.setSelectionMode(mode
)
56 commits
.setAlternatingRowColors(True)
58 self
.commit_text
= DiffTextEdit(context
, self
, whitespace
=False)
60 self
.revision_label
= QtWidgets
.QLabel()
61 self
.revision_label
.setText(N_('Revision Expression:'))
62 self
.revision
= completion
.GitRefLineEdit(context
)
63 self
.revision
.setReadOnly(True)
65 self
.search_label
= QtWidgets
.QLabel()
66 self
.search_label
.setText(N_('Search:'))
67 self
.search
= QtWidgets
.QLineEdit()
68 self
.search
.setReadOnly(False)
69 self
.search
.textChanged
.connect(self
.search_list
)
71 self
.select_button
= qtutils
.ok_button(N_('Select'), enabled
=False)
73 # Make the list widget slightly larger
74 self
.splitter
= qtutils
.splitter(Qt
.Vertical
, self
.commits
, self
.commit_text
)
75 self
.splitter
.setSizes([100, 150])
77 self
.input_layout
= qtutils
.hbox(
88 self
.main_layout
= qtutils
.vbox(
89 defs
.margin
, defs
.margin
, self
.input_layout
, self
.splitter
91 self
.setLayout(self
.main_layout
)
93 commits
.itemSelectionChanged
.connect(self
.commit_oid_selected
)
94 commits
.itemDoubleClicked
.connect(self
.commit_oid_double_clicked
)
96 qtutils
.connect_button(self
.select_button
, self
.accept
)
98 self
.init_state(None, self
.resize_widget
, parent
)
100 def resize_widget(self
, parent
):
101 """Set the initial size of the widget"""
102 width
, height
= qtutils
.default_size(parent
, 720, 480)
103 self
.resize(width
, height
)
105 def selected_commit(self
):
106 return qtutils
.selected_item(self
.commits
, self
.model
.revisions
)
108 def selected_commits(self
):
109 return qtutils
.selected_items(self
.commits
, self
.model
.revisions
)
111 def select_commits(self
):
112 summaries
= self
.model
.summaries
114 msg
= N_('No commits exist in this branch.')
117 qtutils
.set_items(self
.commits
, summaries
)
119 if self
.exec_() != QtWidgets
.QDialog
.Accepted
:
121 return self
.selected_commits()
123 def commit_oid_selected(self
):
124 context
= self
.context
125 oid
= self
.selected_commit()
126 selected
= oid
is not None
127 self
.select_button
.setEnabled(selected
)
129 self
.commit_text
.set_value('')
130 self
.revision
.setText('')
132 self
.revision
.setText(oid
)
133 self
.revision
.selectAll()
134 # Display the oid's commit
135 commit_diff
= gitcmds
.commit_diff(context
, oid
)
136 self
.commit_text
.setText(commit_diff
)
138 def commit_oid_double_clicked(self
, _item
):
139 oid
= self
.selected_commit()
143 def search_list(self
, text
):
145 for i
in range(self
.commits
.count()):
146 self
.commits
.item(i
).setHidden(True)
147 search_items
= self
.commits
.findItems(text
, Qt
.MatchContains
)
148 for items
in search_items
:
149 items
.setHidden(False)
152 class SelectCommitsAndOutput(SelectCommits
):
153 def __init__(self
, context
, model
, parent
=None, title
=None, multiselect
=True):
154 SelectCommits
.__init
__(self
, context
, model
, parent
, title
, multiselect
)
156 self
.output_dir
= prefs
.patches_directory(context
)
157 self
.select_output
= qtutils
.create_button(
158 tooltip
=N_('Select output dir'), icon
=folder()
160 self
.output_text
= QtWidgets
.QLineEdit()
161 self
.output_text
.setReadOnly(True)
162 self
.output_text
.setText(self
.output_dir
)
164 output_layout
= qtutils
.hbox(
165 defs
.no_margin
, defs
.no_spacing
, self
.select_output
, self
.output_text
168 self
.input_layout
.insertLayout(1, output_layout
)
169 qtutils
.connect_button(self
.select_output
, self
.show_output_dialog
)
171 def select_commits_and_output(self
):
172 to_export
= SelectCommits
.select_commits(self
)
173 output
= self
.output_dir
175 return {'to_export': to_export
, 'output': output
}
177 def show_output_dialog(self
):
178 self
.output_dir
= qtutils
.opendir_dialog(
179 N_('Select output directory'), self
.output_dir
181 if not self
.output_dir
:
184 self
.output_text
.setText(self
.output_dir
)