doc: add Thomas to the credits
[git-cola.git] / cola / widgets / filelist.py
blob9ca04ffc838e70606b50d74b568ce5ff69a12eee
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
7 from .. import cmds
8 from .. import hotkeys
9 from .. import qtutils
10 from ..i18n import N_
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):
53 if not commits:
54 return
55 git = self.context.git
56 commit = commits[0]
57 oid = commit.oid
58 status, out, _ = git.show(oid, z=True, numstat=True,
59 oneline=True, no_renames=True)
60 if status == 0:
61 paths = [f for f in out.rstrip('\0').split('\0') if f]
62 if paths:
63 paths = paths[1:]
64 else:
65 paths = []
66 self.list_files(paths)
68 def list_files(self, files_log):
69 self.clear()
70 if not files_log:
71 return
72 files = []
73 for f in files_log:
74 item = FileTreeWidgetItem(f)
75 files.append(item)
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)
82 else:
83 width = self.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)
90 def show(self):
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()))
104 def show_diff(self):
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])