pylint: rename variables for readability
[git-cola.git] / cola / widgets / filelist.py
blob3cef1d854145b32cfb71b255ff464606256f64be
1 from __future__ import absolute_import, division, print_function, 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
14 # pylint: disable=too-many-ancestors
15 class FileWidget(TreeWidget):
17 files_selected = Signal(object)
18 difftool_selected = Signal(object)
19 histories_selected = Signal(object)
20 grab_file = Signal(object)
22 def __init__(self, context, parent):
23 TreeWidget.__init__(self, parent)
24 self.context = context
26 labels = [N_('Filename'), N_('Additions'), N_('Deletions')]
27 self.setHeaderLabels(labels)
29 self.show_history_action = qtutils.add_action(
30 self, N_('Show History'), self.show_history, hotkeys.HISTORY
33 self.launch_difftool_action = qtutils.add_action(
34 self, N_('Launch Diff Tool'), self.show_diff
37 self.launch_editor_action = qtutils.add_action(
38 self, N_('Launch Editor'), self.edit_paths, hotkeys.EDIT
41 self.grab_file_action = qtutils.add_action(
42 self, N_('Grab File...'), self._grab_file
45 # pylint: disable=no-member
46 self.itemSelectionChanged.connect(self.selection_changed)
48 def selection_changed(self):
49 items = self.selected_items()
50 self.files_selected.emit([i.path for i in items])
52 def commits_selected(self, commits):
53 if not commits:
54 self.clear()
55 return
57 git = self.context.git
58 paths = []
60 if len(commits) > 1:
61 # Get a list of changed files for a commit range.
62 start = commits[0].oid + '~'
63 end = commits[-1].oid
64 status, out, _ = git.diff(start, end, z=True, numstat=True, no_renames=True)
65 if status == 0:
66 paths = [f for f in out.rstrip('\0').split('\0') if f]
67 else:
68 # Get the list of changed files in a single commit.
69 commit = commits[0]
70 oid = commit.oid
71 status, out, _ = git.show(
72 oid, z=True, numstat=True, oneline=True, no_renames=True
74 if status == 0:
75 paths = [f for f in out.rstrip('\0').split('\0') if f]
76 if paths:
77 paths = paths[1:] # Skip over the summary on the first line.
79 self.list_files(paths)
81 def list_files(self, files_log):
82 self.clear()
83 if not files_log:
84 return
85 files = []
86 for filename in files_log:
87 item = FileTreeWidgetItem(filename)
88 files.append(item)
89 self.insertTopLevelItems(0, files)
91 def adjust_columns(self, size, old_size):
92 if size.isValid() and old_size.isValid():
93 width = self.columnWidth(0) + size.width() - old_size.width()
94 self.setColumnWidth(0, width)
95 else:
96 width = self.width()
97 two_thirds = (width * 2) // 3
98 one_sixth = width // 6
99 self.setColumnWidth(0, two_thirds)
100 self.setColumnWidth(1, one_sixth)
101 self.setColumnWidth(2, one_sixth)
103 def show(self):
104 self.adjust_columns(QSize(), QSize())
106 def resizeEvent(self, event):
107 self.adjust_columns(event.size(), event.oldSize())
109 def contextMenuEvent(self, event):
110 menu = qtutils.create_menu(N_('Actions'), self)
111 menu.addAction(self.grab_file_action)
112 menu.addAction(self.show_history_action)
113 menu.addAction(self.launch_difftool_action)
114 menu.addAction(self.launch_editor_action)
115 menu.exec_(self.mapToGlobal(event.pos()))
117 def show_diff(self):
118 self.difftool_selected.emit(self.selected_paths())
120 def _grab_file(self):
121 for path in self.selected_paths():
122 self.grab_file.emit(path)
124 def selected_paths(self):
125 return [i.path for i in self.selected_items()]
127 def edit_paths(self):
128 cmds.do(cmds.Edit, self.context, self.selected_paths())
130 def show_history(self):
131 items = self.selected_items()
132 paths = [i.path for i in items]
133 self.histories_selected.emit(paths)
136 class FileTreeWidgetItem(QtWidgets.QTreeWidgetItem):
137 def __init__(self, file_log, parent=None):
138 QtWidgets.QTreeWidgetItem.__init__(self, parent)
139 texts = file_log.split('\t')
140 self.path = path = texts[2]
141 self.setText(0, path)
142 self.setText(1, texts[0])
143 self.setText(2, texts[1])