1 from PyQt4
import QtGui
2 from PyQt4
import QtCore
3 from PyQt4
.QtCore
import Qt
4 from PyQt4
.QtCore
import SIGNAL
8 from cola
import qtutils
9 from cola
import gitcmds
10 from cola
.i18n
import N_
11 from cola
.models
import main
12 from cola
.models
import selection
13 from cola
.widgets
import completion
14 from cola
.widgets
import defs
15 from cola
.widgets
import standard
19 files
= selection
.selected_group()
22 s
= selection
.selection()
24 launch_with_head(files
, bool(s
.staged
), model
.head
)
27 def launch_with_head(filenames
, staged
, head
):
30 args
.append('--cached')
34 args
.extend(filenames
)
39 """Launches 'git difftool' with args"""
40 difftool_args
= ['git', 'difftool', '--no-prompt']
41 difftool_args
.extend(args
)
42 core
.fork(difftool_args
)
45 def diff_commits(parent
, a
, b
):
46 dlg
= FileDiffDialog(parent
, a
=a
, b
=b
)
49 return dlg
.exec_() == QtGui
.QDialog
.Accepted
52 def diff_expression(parent
, expr
,
53 create_widget
=False, hide_expr
=False):
54 dlg
= FileDiffDialog(parent
, expr
=expr
, hide_expr
=hide_expr
)
59 return dlg
.exec_() == QtGui
.QDialog
.Accepted
62 class FileDiffDialog(QtGui
.QDialog
):
64 def __init__(self
, parent
, a
=None, b
=None, expr
=None, title
=None,
66 QtGui
.QDialog
.__init
__(self
, parent
)
67 self
.setAttribute(Qt
.WA_MacMetalStyle
)
74 title
= N_('git-cola diff')
76 self
.setWindowTitle(title
)
77 self
.setWindowModality(QtCore
.Qt
.WindowModal
)
79 self
._expr
= completion
.GitRefLineEdit(parent
=self
)
81 self
._expr
.setText(expr
)
83 if expr
is None or hide_expr
:
86 self
._tree
= standard
.TreeWidget(self
)
87 self
._tree
.setSelectionMode(self
._tree
.ExtendedSelection
)
88 self
._tree
.setHeaderHidden(True)
90 self
._diff
_btn
= QtGui
.QPushButton(N_('Compare'))
91 self
._diff
_btn
.setIcon(qtutils
.ok_icon())
92 self
._diff
_btn
.setEnabled(False)
94 self
._close
_btn
= QtGui
.QPushButton(N_('Close'))
95 self
._close
_btn
.setIcon(qtutils
.close_icon())
97 self
._button
_layt
= QtGui
.QHBoxLayout()
98 self
._button
_layt
.setMargin(0)
99 self
._button
_layt
.addStretch()
100 self
._button
_layt
.addWidget(self
._diff
_btn
)
101 self
._button
_layt
.addWidget(self
._close
_btn
)
103 self
._layt
= QtGui
.QVBoxLayout()
104 self
._layt
.setMargin(defs
.margin
)
105 self
._layt
.setSpacing(defs
.spacing
)
107 self
._layt
.addWidget(self
._expr
)
108 self
._layt
.addWidget(self
._tree
)
109 self
._layt
.addLayout(self
._button
_layt
)
110 self
.setLayout(self
._layt
)
112 self
.connect(self
._tree
, SIGNAL('itemSelectionChanged()'),
113 self
._tree
_selection
_changed
)
115 self
.connect(self
._tree
,
116 SIGNAL('itemDoubleClicked(QTreeWidgetItem*,int)'),
117 self
._tree
_double
_clicked
)
119 self
.connect(self
._expr
, SIGNAL('textChanged(QString)'),
122 self
.connect(self
._expr
, SIGNAL('returnPressed()'),
125 qtutils
.connect_button(self
._diff
_btn
, self
.diff
)
126 qtutils
.connect_button(self
._close
_btn
, self
.close
)
127 qtutils
.add_close_action(self
)
129 self
.resize(720, 420)
132 def text_changed(self
, txt
):
133 self
.expr
= unicode(txt
)
137 if self
.expr
is not None:
138 self
.diff_arg
= utils
.shell_split(self
.expr
)
140 self
.diff_arg
= [self
.a
]
142 self
.diff_arg
= [self
.a
, self
.b
]
143 self
.refresh_filenames()
145 def refresh_filenames(self
):
148 if self
.a
and self
.b
is None:
149 filenames
= gitcmds
.diff_index_filenames(self
.a
)
151 filenames
= gitcmds
.diff(self
.diff_arg
)
155 icon
= qtutils
.file_icon()
157 for filename
in filenames
:
158 item
= QtGui
.QTreeWidgetItem()
159 item
.setIcon(0, icon
)
160 item
.setText(0, filename
)
161 item
.setData(0, QtCore
.Qt
.UserRole
, QtCore
.QVariant(filename
))
163 self
._tree
.addTopLevelItems(items
)
165 def _tree_selection_changed(self
):
166 self
._diff
_btn
.setEnabled(bool(self
._tree
.selectedItems()))
168 def _tree_double_clicked(self
, item
, column
):
169 path
= item
.data(0, QtCore
.Qt
.UserRole
).toPyObject()
170 launch(self
.diff_arg
+ ['--', unicode(path
)])
173 items
= self
._tree
.selectedItems()
176 paths
= [i
.data(0, QtCore
.Qt
.UserRole
).toPyObject() for i
in items
]
178 launch(self
.diff_arg
+ ['--', unicode(path
)])