guicmds: Replace repobrowser with the simpler browse dialog
[git-cola.git] / cola / difftool.py
blobe8a55fcbd502eed5ac542f036d3ec58b8e4d63de
1 from PyQt4 import QtGui
2 from PyQt4 import QtCore
3 from PyQt4.QtCore import SIGNAL
5 from cola import utils
6 from cola import qtutils
7 from cola import gitcmds
10 def launch(args):
11 """Launches 'git difftool' with args"""
12 difftool_args = ['git', 'difftool', '--no-prompt']
13 difftool_args.extend(args)
14 utils.fork(difftool_args)
17 def diff_commits(parent, a, b):
18 dlg = FileDiffDialog(parent, a, b)
19 dlg.show()
20 dlg.raise_()
21 return dlg.exec_() == QtGui.QDialog.Accepted
24 class FileDiffDialog(QtGui.QDialog):
25 def __init__(self, parent, a, b):
26 QtGui.QDialog.__init__(self, parent)
27 self.a = a
28 self.b = b
30 self.setWindowTitle('Select File(s)')
31 self._tree = QtGui.QTreeWidget(self)
32 self._tree.setAlternatingRowColors(True)
33 self._tree.setRootIsDecorated(False)
34 self._tree.setSelectionMode(self._tree.ExtendedSelection)
35 self._tree.setUniformRowHeights(True)
36 self._tree.setAllColumnsShowFocus(True)
37 self._tree.setHeaderHidden(True)
39 self._diff_btn = QtGui.QPushButton('Compare')
40 self._diff_btn.setIcon(qtutils.ok_icon())
41 self._diff_btn.setEnabled(False)
43 self._close_btn = QtGui.QPushButton('Close')
44 self._close_btn.setIcon(qtutils.close_icon())
46 self._button_layt = QtGui.QHBoxLayout()
47 self._button_layt.setMargin(0)
48 self._button_layt.addStretch()
49 self._button_layt.addWidget(self._diff_btn)
50 self._button_layt.addWidget(self._close_btn)
52 self._layt = QtGui.QVBoxLayout()
53 self._layt.setMargin(4)
54 self._layt.addWidget(self._tree)
55 self._layt.addLayout(self._button_layt)
56 self.setLayout(self._layt)
58 qtutils.add_close_action(self)
60 self.connect(self._tree, SIGNAL('itemSelectionChanged()'),
61 self._tree_selection_changed)
63 self.connect(self._tree,
64 SIGNAL('itemDoubleClicked(QTreeWidgetItem*,int)'),
65 self._tree_double_clicked)
67 self.connect(self._diff_btn, SIGNAL('clicked()'), self.diff)
68 self.connect(self._close_btn, SIGNAL('clicked()'), self.close)
70 self.diff_arg = '%s..%s' % (self.a, self.b)
72 self.resize(720, 420)
75 def exec_(self):
76 filenames = gitcmds.diff_filenames(self.diff_arg)
77 if not filenames:
78 details = ('"git diff --name-only %s" returned an empty list' %
79 self.diff_arg)
80 self.hide()
81 qtutils.information('git cola',
82 message='No changes to diff',
83 details=details,
84 parent=self)
85 self.close()
86 return self.Accepted
88 icon = qtutils.file_icon()
89 items = []
90 for filename in filenames:
91 item = QtGui.QTreeWidgetItem()
92 item.setIcon(0, icon)
93 item.setText(0, filename)
94 item.setData(0, QtCore.Qt.UserRole, QtCore.QVariant(filename))
95 items.append(item)
96 self._tree.addTopLevelItems(items)
98 return QtGui.QDialog.exec_(self)
100 def _tree_selection_changed(self):
101 self._diff_btn.setEnabled(bool(self._tree.selectedItems()))
103 def _tree_double_clicked(self, item, column):
104 path = item.data(0, QtCore.Qt.UserRole).toPyObject()
105 launch([self.diff_arg, '--', unicode(path)])
107 def diff(self):
108 items = self._tree.selectedItems()
109 if not items:
110 return
111 paths = [i.data(0, QtCore.Qt.UserRole).toPyObject() for i in items]
112 for path in paths:
113 launch([self.diff_arg, '--', unicode(path)])