1 from qtpy
import QtWidgets
2 from qtpy
.QtCore
import Signal
3 from qtpy
.QtCore
import QSize
4 from qtpy
.QtCore
import Qt
10 from .standard
import TreeWidget
13 class FileWidget(TreeWidget
):
14 files_selected
= Signal(object)
15 difftool_selected
= Signal(object)
16 histories_selected
= Signal(object)
17 grab_file
= Signal(object)
18 grab_file_from_parent
= Signal(object)
19 remark_toggled
= Signal(object, object)
21 def __init__(self
, context
, parent
, remarks
=False):
22 TreeWidget
.__init
__(self
, parent
)
23 self
.context
= context
25 labels
= [N_('Filename'), N_('Additions'), N_('Deletions')]
26 self
.setHeaderLabels(labels
)
28 self
.show_history_action
= qtutils
.add_action(
29 self
, N_('Show History'), self
.show_history
, hotkeys
.HISTORY
31 self
.launch_difftool_action
= qtutils
.add_action(
32 self
, N_('Launch Diff Tool'), self
.show_diff
34 self
.launch_editor_action
= qtutils
.add_action(
35 self
, N_('Launch Editor'), self
.edit_paths
, hotkeys
.EDIT
37 self
.grab_file_action
= qtutils
.add_action(
38 self
, N_('Grab File...'), self
._grab
_file
40 self
.grab_file_from_parent_action
= qtutils
.add_action(
41 self
, N_('Grab File from Parent Commit...'), self
._grab
_file
_from
_parent
45 self
.toggle_remark_actions
= tuple(
49 lambda remark
=r
: self
.toggle_remark(remark
),
50 hotkeys
.hotkey(Qt
.CTRL |
getattr(Qt
, 'Key_' + r
)),
52 for r
in map(str, range(10))
55 self
.toggle_remark_actions
= tuple()
57 self
.itemSelectionChanged
.connect(self
.selection_changed
)
59 def selection_changed(self
):
60 items
= self
.selected_items()
61 self
.files_selected
.emit([i
.path
for i
in items
])
63 def commits_selected(self
, commits
):
68 git
= self
.context
.git
72 # Get a list of changed files for a commit range.
73 start
= commits
[0].oid
+ '~'
75 status
, out
, _
= git
.diff(start
, end
, z
=True, numstat
=True, no_renames
=True)
77 paths
= [f
for f
in out
.rstrip('\0').split('\0') if f
]
79 # Get the list of changed files in a single commit.
82 status
, out
, _
= git
.show(
83 oid
, z
=True, numstat
=True, oneline
=True, no_renames
=True
86 paths
= [f
for f
in out
.rstrip('\0').split('\0') if f
]
88 paths
= paths
[1:] # Skip over the summary on the first line.
90 self
.list_files(paths
)
92 def list_files(self
, files_log
):
97 for filename
in files_log
:
98 item
= FileTreeWidgetItem(filename
)
100 self
.insertTopLevelItems(0, files
)
102 def adjust_columns(self
, size
, old_size
):
103 if size
.isValid() and old_size
.isValid():
104 width
= self
.columnWidth(0) + size
.width() - old_size
.width()
105 self
.setColumnWidth(0, width
)
108 two_thirds
= (width
* 2) // 3
109 one_sixth
= width
// 6
110 self
.setColumnWidth(0, two_thirds
)
111 self
.setColumnWidth(1, one_sixth
)
112 self
.setColumnWidth(2, one_sixth
)
115 self
.adjust_columns(QSize(), QSize())
117 def resizeEvent(self
, event
):
118 self
.adjust_columns(event
.size(), event
.oldSize())
120 def contextMenuEvent(self
, event
):
121 menu
= qtutils
.create_menu(N_('Actions'), self
)
122 menu
.addAction(self
.grab_file_action
)
123 menu
.addAction(self
.grab_file_from_parent_action
)
124 menu
.addAction(self
.show_history_action
)
125 menu
.addAction(self
.launch_difftool_action
)
126 menu
.addAction(self
.launch_editor_action
)
127 if self
.toggle_remark_actions
:
128 menu_toggle_remark
= menu
.addMenu(N_('Toggle remark of touching commits'))
129 tuple(map(menu_toggle_remark
.addAction
, self
.toggle_remark_actions
))
130 menu
.exec_(self
.mapToGlobal(event
.pos()))
133 self
.difftool_selected
.emit(self
.selected_paths())
135 def _grab_file(self
):
136 for path
in self
.selected_paths():
137 self
.grab_file
.emit(path
)
139 def _grab_file_from_parent(self
):
140 for path
in self
.selected_paths():
141 self
.grab_file_from_parent
.emit(path
)
143 def selected_paths(self
):
144 return [i
.path
for i
in self
.selected_items()]
146 def edit_paths(self
):
147 cmds
.do(cmds
.Edit
, self
.context
, self
.selected_paths())
149 def show_history(self
):
150 items
= self
.selected_items()
151 paths
= [i
.path
for i
in items
]
152 self
.histories_selected
.emit(paths
)
154 def toggle_remark(self
, remark
):
155 items
= self
.selected_items()
156 paths
= tuple(i
.path
for i
in items
)
157 self
.remark_toggled
.emit(remark
, paths
)
160 class FileTreeWidgetItem(QtWidgets
.QTreeWidgetItem
):
161 def __init__(self
, file_log
, parent
=None):
162 QtWidgets
.QTreeWidgetItem
.__init
__(self
, parent
)
163 texts
= file_log
.split('\t')
164 self
.path
= path
= texts
[2]
165 self
.setText(0, path
)
166 self
.setText(1, texts
[0])
167 self
.setText(2, texts
[1])