1 from qtpy
import QtWidgets
2 from qtpy
.QtCore
import Signal
3 from qtpy
.QtCore
import QSize
9 from .standard
import TreeWidget
12 # pylint: disable=too-many-ancestors
13 class FileWidget(TreeWidget
):
14 files_selected
= Signal(object)
15 difftool_selected
= Signal(object)
16 histories_selected
= Signal(object)
17 grab_file
= Signal(object)
19 def __init__(self
, context
, parent
):
20 TreeWidget
.__init
__(self
, parent
)
21 self
.context
= context
23 labels
= [N_('Filename'), N_('Additions'), N_('Deletions')]
24 self
.setHeaderLabels(labels
)
26 self
.show_history_action
= qtutils
.add_action(
27 self
, N_('Show History'), self
.show_history
, hotkeys
.HISTORY
30 self
.launch_difftool_action
= qtutils
.add_action(
31 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
38 self
.grab_file_action
= qtutils
.add_action(
39 self
, N_('Grab File...'), self
._grab
_file
42 # pylint: disable=no-member
43 self
.itemSelectionChanged
.connect(self
.selection_changed
)
45 def selection_changed(self
):
46 items
= self
.selected_items()
47 self
.files_selected
.emit([i
.path
for i
in items
])
49 def commits_selected(self
, commits
):
54 git
= self
.context
.git
58 # Get a list of changed files for a commit range.
59 start
= commits
[0].oid
+ '~'
61 status
, out
, _
= git
.diff(start
, end
, z
=True, numstat
=True, no_renames
=True)
63 paths
= [f
for f
in out
.rstrip('\0').split('\0') if f
]
65 # Get the list of changed files in a single commit.
68 status
, out
, _
= git
.show(
69 oid
, z
=True, numstat
=True, oneline
=True, no_renames
=True
72 paths
= [f
for f
in out
.rstrip('\0').split('\0') if f
]
74 paths
= paths
[1:] # Skip over the summary on the first line.
76 self
.list_files(paths
)
78 def list_files(self
, files_log
):
83 for filename
in files_log
:
84 item
= FileTreeWidgetItem(filename
)
86 self
.insertTopLevelItems(0, files
)
88 def adjust_columns(self
, size
, old_size
):
89 if size
.isValid() and old_size
.isValid():
90 width
= self
.columnWidth(0) + size
.width() - old_size
.width()
91 self
.setColumnWidth(0, width
)
94 two_thirds
= (width
* 2) // 3
95 one_sixth
= width
// 6
96 self
.setColumnWidth(0, two_thirds
)
97 self
.setColumnWidth(1, one_sixth
)
98 self
.setColumnWidth(2, one_sixth
)
101 self
.adjust_columns(QSize(), QSize())
103 def resizeEvent(self
, event
):
104 self
.adjust_columns(event
.size(), event
.oldSize())
106 def contextMenuEvent(self
, event
):
107 menu
= qtutils
.create_menu(N_('Actions'), self
)
108 menu
.addAction(self
.grab_file_action
)
109 menu
.addAction(self
.show_history_action
)
110 menu
.addAction(self
.launch_difftool_action
)
111 menu
.addAction(self
.launch_editor_action
)
112 menu
.exec_(self
.mapToGlobal(event
.pos()))
115 self
.difftool_selected
.emit(self
.selected_paths())
117 def _grab_file(self
):
118 for path
in self
.selected_paths():
119 self
.grab_file
.emit(path
)
121 def selected_paths(self
):
122 return [i
.path
for i
in self
.selected_items()]
124 def edit_paths(self
):
125 cmds
.do(cmds
.Edit
, self
.context
, self
.selected_paths())
127 def show_history(self
):
128 items
= self
.selected_items()
129 paths
= [i
.path
for i
in items
]
130 self
.histories_selected
.emit(paths
)
133 class FileTreeWidgetItem(QtWidgets
.QTreeWidgetItem
):
134 def __init__(self
, file_log
, parent
=None):
135 QtWidgets
.QTreeWidgetItem
.__init
__(self
, parent
)
136 texts
= file_log
.split('\t')
137 self
.path
= path
= texts
[2]
138 self
.setText(0, path
)
139 self
.setText(1, texts
[0])
140 self
.setText(2, texts
[1])