1 from __future__
import division
, absolute_import
, unicode_literals
3 from qtpy
import QtWidgets
4 from qtpy
.QtCore
import Signal
5 from qtpy
.QtCore
import QSize
11 from .standard
import TreeWidget
12 from .diff
import COMMITS_SELECTED
13 from .diff
import FILES_SELECTED
15 HISTORIES_SELECTED
= 'HISTORIES_SELECTED'
16 DIFFTOOL_SELECTED
= 'DIFFTOOL_SELECTED'
19 class FileWidget(TreeWidget
):
21 grab_file
= Signal(object)
23 def __init__(self
, context
, notifier
, parent
):
24 TreeWidget
.__init
__(self
, parent
)
25 self
.context
= context
26 self
.notifier
= notifier
28 labels
= [N_('Filename'), N_('Additions'), N_('Deletions')]
29 self
.setHeaderLabels(labels
)
31 notifier
.add_observer(COMMITS_SELECTED
, self
.commits_selected
)
33 self
.show_history_action
= qtutils
.add_action(
34 self
, N_('Show History'), self
.show_history
, hotkeys
.HISTORY
)
36 self
.launch_difftool_action
= qtutils
.add_action(
37 self
, N_('Launch Diff Tool'), self
.show_diff
)
39 self
.launch_editor_action
= qtutils
.add_action(
40 self
, N_('Launch Editor'), self
.edit_paths
, hotkeys
.EDIT
)
42 self
.grab_file_action
= qtutils
.add_action(
43 self
, N_('Grab File...'), self
._grab
_file
)
45 self
.itemSelectionChanged
.connect(self
.selection_changed
)
47 def selection_changed(self
):
48 items
= self
.selected_items()
49 self
.notifier
.notify_observers(
50 FILES_SELECTED
, [i
.path
for i
in items
])
52 def commits_selected(self
, commits
):
55 git
= self
.context
.git
58 status
, out
, _
= git
.show(oid
, z
=True, numstat
=True,
59 oneline
=True, no_renames
=True)
61 paths
= [f
for f
in out
.rstrip('\0').split('\0') if f
]
66 self
.list_files(paths
)
68 def list_files(self
, files_log
):
74 item
= FileTreeWidgetItem(f
)
76 self
.insertTopLevelItems(0, files
)
78 def adjust_columns(self
, size
, old_size
):
79 if size
.isValid() and old_size
.isValid():
80 width
= (self
.columnWidth(0) + size
.width() - old_size
.width())
81 self
.setColumnWidth(0, width
)
84 two_thirds
= (width
* 2) // 3
85 one_sixth
= width
// 6
86 self
.setColumnWidth(0, two_thirds
)
87 self
.setColumnWidth(1, one_sixth
)
88 self
.setColumnWidth(2, one_sixth
)
91 self
.adjust_columns(QSize(), QSize())
93 def resizeEvent(self
, e
):
94 self
.adjust_columns(e
.size(), e
.oldSize())
96 def contextMenuEvent(self
, event
):
97 menu
= qtutils
.create_menu(N_('Actions'), self
)
98 menu
.addAction(self
.grab_file_action
)
99 menu
.addAction(self
.show_history_action
)
100 menu
.addAction(self
.launch_difftool_action
)
101 menu
.addAction(self
.launch_editor_action
)
102 menu
.exec_(self
.mapToGlobal(event
.pos()))
105 self
.notifier
.notify_observers(DIFFTOOL_SELECTED
, self
.selected_paths())
107 def _grab_file(self
):
108 for path
in self
.selected_paths():
109 self
.grab_file
.emit(path
)
111 def selected_paths(self
):
112 return [i
.path
for i
in self
.selected_items()]
114 def edit_paths(self
):
115 cmds
.do(cmds
.Edit
, self
.context
, self
.selected_paths())
117 def show_history(self
):
118 items
= self
.selected_items()
119 paths
= [i
.path
for i
in items
]
120 self
.notifier
.notify_observers(HISTORIES_SELECTED
, paths
)
123 class FileTreeWidgetItem(QtWidgets
.QTreeWidgetItem
):
125 def __init__(self
, file_log
, parent
=None):
126 QtWidgets
.QTreeWidgetItem
.__init
__(self
, parent
)
127 texts
= file_log
.split('\t')
128 self
.path
= path
= texts
[2]
129 self
.setText(0, path
)
130 self
.setText(1, texts
[0])
131 self
.setText(2, texts
[1])