Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@244
|
8 Copyright (c) 2011 Chris Cannam
|
Chris@244
|
9 Copyright (c) 2011 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
Chris@57
|
17
|
Chris@43
|
18 #include "changesetitem.h"
|
Chris@117
|
19 #include "changesetdetailitem.h"
|
Chris@43
|
20 #include "changeset.h"
|
Chris@53
|
21 #include "textabbrev.h"
|
Chris@53
|
22 #include "colourset.h"
|
Chris@117
|
23 #include "debug.h"
|
Chris@43
|
24
|
Chris@43
|
25 #include <QPainter>
|
Chris@117
|
26 #include <QGraphicsScene>
|
Chris@132
|
27 #include <QGraphicsSceneMouseEvent>
|
Chris@140
|
28 #include <QMenu>
|
Chris@140
|
29 #include <QAction>
|
Chris@140
|
30 #include <QLabel>
|
Chris@140
|
31 #include <QWidgetAction>
|
Chris@153
|
32 #include <QApplication>
|
Chris@153
|
33 #include <QClipboard>
|
Chris@43
|
34
|
Chris@53
|
35 ChangesetItem::ChangesetItem(Changeset *cs) :
|
Chris@117
|
36 m_changeset(cs), m_detail(0),
|
Chris@133
|
37 m_showBranch(false), m_column(0), m_row(0), m_wide(false),
|
Chris@133
|
38 m_current(false), m_new(false)
|
Chris@53
|
39 {
|
Chris@53
|
40 m_font = QFont();
|
Chris@53
|
41 m_font.setPixelSize(11);
|
Chris@53
|
42 m_font.setBold(false);
|
Chris@53
|
43 m_font.setItalic(false);
|
Chris@168
|
44 setCursor(Qt::ArrowCursor);
|
Chris@53
|
45 }
|
Chris@53
|
46
|
Chris@141
|
47 QString
|
Chris@141
|
48 ChangesetItem::getId()
|
Chris@141
|
49 {
|
Chris@141
|
50 return m_changeset->id();
|
Chris@141
|
51 }
|
Chris@141
|
52
|
Chris@43
|
53 QRectF
|
Chris@43
|
54 ChangesetItem::boundingRect() const
|
Chris@43
|
55 {
|
Chris@55
|
56 int w = 100;
|
Chris@55
|
57 if (m_wide) w = 180;
|
Chris@250
|
58 return QRectF(-((w-50)/2 - 1), -30, w - 3, 90);
|
Chris@43
|
59 }
|
Chris@43
|
60
|
Chris@43
|
61 void
|
Chris@119
|
62 ChangesetItem::showDetail()
|
Chris@117
|
63 {
|
Chris@119
|
64 if (m_detail) return;
|
Chris@117
|
65 m_detail = new ChangesetDetailItem(m_changeset);
|
Chris@117
|
66 m_detail->setZValue(zValue() + 1);
|
Chris@117
|
67 scene()->addItem(m_detail);
|
Chris@117
|
68 int w = 100;
|
Chris@117
|
69 if (m_wide) w = 180;
|
Chris@124
|
70 int h = 80;
|
Chris@124
|
71 // m_detail->moveBy(x() - (m_detail->boundingRect().width() - 50) / 2,
|
Chris@124
|
72 // y() + 60);
|
Chris@124
|
73 m_detail->moveBy(x() + (w + 50) / 2 + 10 + 0.5,
|
Chris@124
|
74 y() - (m_detail->boundingRect().height() - h) / 2 + 0.5);
|
Chris@119
|
75 emit detailShown();
|
Chris@119
|
76 }
|
Chris@119
|
77
|
Chris@119
|
78 void
|
Chris@119
|
79 ChangesetItem::hideDetail()
|
Chris@119
|
80 {
|
Chris@124
|
81 if (!m_detail) return;
|
Chris@124
|
82 scene()->removeItem(m_detail);
|
Chris@119
|
83 delete m_detail;
|
Chris@119
|
84 m_detail = 0;
|
Chris@119
|
85 emit detailHidden();
|
Chris@119
|
86 }
|
Chris@119
|
87
|
Chris@119
|
88 void
|
Chris@119
|
89 ChangesetItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
|
Chris@119
|
90 {
|
Chris@119
|
91 DEBUG << "ChangesetItem::mousePressEvent" << endl;
|
Chris@132
|
92 if (e->button() == Qt::LeftButton) {
|
Chris@132
|
93 if (m_detail) {
|
Chris@132
|
94 hideDetail();
|
Chris@132
|
95 } else {
|
Chris@132
|
96 showDetail();
|
Chris@132
|
97 }
|
Chris@140
|
98 } else if (e->button() == Qt::RightButton) {
|
Chris@141
|
99 if (m_detail) {
|
Chris@141
|
100 hideDetail();
|
Chris@141
|
101 }
|
Chris@140
|
102 activateMenu();
|
Chris@119
|
103 }
|
Chris@117
|
104 }
|
Chris@117
|
105
|
Chris@117
|
106 void
|
Chris@140
|
107 ChangesetItem::activateMenu()
|
Chris@140
|
108 {
|
Chris@153
|
109 m_parentDiffActions.clear();
|
Chris@153
|
110
|
Chris@140
|
111 QMenu *menu = new QMenu;
|
Chris@165
|
112 QLabel *label = new QLabel(tr("<qt><b> Revision: </b>%1</qt>")
|
Chris@165
|
113 .arg(Changeset::hashOf(m_changeset->id())));
|
Chris@141
|
114 QWidgetAction *wa = new QWidgetAction(menu);
|
Chris@140
|
115 wa->setDefaultWidget(label);
|
Chris@140
|
116 menu->addAction(wa);
|
Chris@140
|
117 menu->addSeparator();
|
Chris@141
|
118
|
Chris@153
|
119 QAction *copyId = menu->addAction(tr("Copy identifier to clipboard"));
|
Chris@153
|
120 connect(copyId, SIGNAL(triggered()), this, SLOT(copyIdActivated()));
|
Chris@141
|
121
|
Chris@140
|
122 menu->addSeparator();
|
Chris@141
|
123
|
Chris@153
|
124 if (m_changeset->parents().size() > 1) {
|
Chris@153
|
125
|
Chris@153
|
126 foreach (QString parentId, m_changeset->parents()) {
|
Chris@153
|
127 QAction *diffParent =
|
Chris@165
|
128 menu->addAction(tr("Diff to parent %1")
|
Chris@165
|
129 .arg(Changeset::hashOf(parentId)));
|
Chris@153
|
130 connect(diffParent, SIGNAL(triggered()),
|
Chris@153
|
131 this, SLOT(diffToParentActivated()));
|
Chris@153
|
132 m_parentDiffActions[diffParent] = parentId;
|
Chris@153
|
133 }
|
Chris@153
|
134
|
Chris@153
|
135 } else {
|
Chris@153
|
136
|
Chris@153
|
137 QAction *diffParent =
|
Chris@153
|
138 menu->addAction(tr("Diff to parent"));
|
Chris@153
|
139 connect(diffParent, SIGNAL(triggered()),
|
Chris@153
|
140 this, SLOT(diffToParentActivated()));
|
Chris@153
|
141 }
|
Chris@153
|
142
|
Chris@153
|
143 QAction *diffCurrent = menu->addAction(tr("Diff to current working folder"));
|
Chris@141
|
144 connect(diffCurrent, SIGNAL(triggered()), this, SLOT(diffToCurrentActivated()));
|
Chris@141
|
145
|
Chris@140
|
146 menu->addSeparator();
|
Chris@141
|
147
|
Chris@153
|
148 QAction *update = menu->addAction(tr("Update to this revision"));
|
Chris@153
|
149 connect(update, SIGNAL(triggered()), this, SLOT(updateActivated()));
|
Chris@153
|
150
|
Chris@140
|
151 QAction *merge = menu->addAction(tr("Merge from here to current"));
|
Chris@141
|
152 connect(merge, SIGNAL(triggered()), this, SLOT(mergeActivated()));
|
Chris@153
|
153
|
Chris@153
|
154 menu->addSeparator();
|
Chris@153
|
155
|
Chris@153
|
156 QAction *tag = menu->addAction(tr("Add tag..."));
|
Chris@141
|
157 connect(tag, SIGNAL(triggered()), this, SLOT(tagActivated()));
|
Chris@141
|
158
|
Chris@148
|
159 menu->exec(QCursor::pos());
|
Chris@148
|
160
|
Chris@141
|
161 ungrabMouse();
|
Chris@140
|
162 }
|
Chris@140
|
163
|
Chris@153
|
164 void
|
Chris@153
|
165 ChangesetItem::copyIdActivated()
|
Chris@153
|
166 {
|
Chris@153
|
167 QClipboard *clipboard = QApplication::clipboard();
|
Chris@153
|
168 clipboard->setText(Changeset::hashOf(m_changeset->id()));
|
Chris@153
|
169 }
|
Chris@153
|
170
|
Chris@153
|
171 void ChangesetItem::diffToParentActivated()
|
Chris@153
|
172 {
|
Chris@153
|
173 QAction *a = qobject_cast<QAction *>(sender());
|
Chris@153
|
174 QString parentId;
|
Chris@153
|
175 if (m_parentDiffActions.contains(a)) {
|
Chris@153
|
176 parentId = m_parentDiffActions[a];
|
Chris@153
|
177 DEBUG << "ChangesetItem::diffToParentActivated: specific parent "
|
Chris@153
|
178 << parentId << " selected" << endl;
|
Chris@153
|
179 } else {
|
Chris@153
|
180 parentId = m_changeset->parents()[0];
|
Chris@153
|
181 DEBUG << "ChangesetItem::diffToParentActivated: "
|
Chris@153
|
182 << "no specific parent selected, using first parent "
|
Chris@153
|
183 << parentId << endl;
|
Chris@153
|
184 }
|
Chris@153
|
185
|
Chris@153
|
186 emit diffToParent(getId(), parentId);
|
Chris@153
|
187 }
|
Chris@153
|
188
|
Chris@141
|
189 void ChangesetItem::updateActivated() { emit updateTo(getId()); }
|
Chris@141
|
190 void ChangesetItem::diffToCurrentActivated() { emit diffToCurrent(getId()); }
|
Chris@141
|
191 void ChangesetItem::mergeActivated() { emit mergeFrom(getId()); }
|
Chris@141
|
192 void ChangesetItem::tagActivated() { emit tag(getId()); }
|
Chris@141
|
193
|
Chris@140
|
194 void
|
Chris@43
|
195 ChangesetItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option,
|
Chris@43
|
196 QWidget *w)
|
Chris@43
|
197 {
|
Chris@53
|
198 paint->save();
|
Chris@53
|
199
|
Chris@53
|
200 ColourSet *colourSet = ColourSet::instance();
|
Chris@53
|
201 QColor branchColour = colourSet->getColourFor(m_changeset->branch());
|
Chris@128
|
202 QColor userColour = colourSet->getColourFor(m_changeset->author());
|
Chris@53
|
203
|
Chris@53
|
204 QFont f(m_font);
|
Chris@53
|
205
|
Chris@54
|
206 QTransform t = paint->worldTransform();
|
Chris@53
|
207 float scale = std::min(t.m11(), t.m22());
|
Chris@53
|
208 if (scale > 1.0) {
|
Chris@53
|
209 int ps = int((f.pixelSize() / scale) + 0.5);
|
Chris@53
|
210 if (ps < 8) ps = 8;
|
Chris@53
|
211 f.setPixelSize(ps);
|
Chris@53
|
212 }
|
Chris@54
|
213
|
Chris@250
|
214 bool showText = (scale >= 0.2);
|
Chris@250
|
215 bool showProperLines = (scale >= 0.1);
|
Chris@250
|
216
|
Chris@250
|
217 if (!showProperLines) {
|
Chris@54
|
218 paint->setPen(QPen(branchColour, 0));
|
Chris@54
|
219 } else {
|
Chris@54
|
220 paint->setPen(QPen(branchColour, 2));
|
Chris@54
|
221 }
|
Chris@53
|
222
|
Chris@53
|
223 paint->setFont(f);
|
Chris@53
|
224 QFontMetrics fm(f);
|
Chris@53
|
225 int fh = fm.height();
|
Chris@55
|
226
|
Chris@55
|
227 int width = 100;
|
Chris@55
|
228 if (m_wide) width = 180;
|
Chris@55
|
229 int x0 = -((width - 50) / 2 - 1);
|
Chris@55
|
230
|
Chris@250
|
231 int textwid = width - 7;
|
Chris@250
|
232
|
Chris@250
|
233 QString comment;
|
Chris@250
|
234 QStringList lines;
|
Chris@250
|
235 int lineCount = 3;
|
Chris@250
|
236
|
Chris@250
|
237 if (showText) {
|
Chris@250
|
238
|
Chris@250
|
239 comment = m_changeset->comment().trimmed();
|
Chris@250
|
240 comment = comment.replace("\\n", " ");
|
Chris@250
|
241 comment = comment.replace(QRegExp("^\"\\s*\\**\\s*"), "");
|
Chris@250
|
242 comment = comment.replace(QRegExp("\"$"), "");
|
Chris@250
|
243 comment = comment.replace("\\\"", "\"");
|
Chris@250
|
244
|
Chris@250
|
245 comment = TextAbbrev::abbreviate(comment, fm, textwid,
|
Chris@250
|
246 TextAbbrev::ElideEnd, "...", 3);
|
Chris@250
|
247 // abbreviate() changes this (ouch!), restore it
|
Chris@250
|
248 textwid = width - 5;
|
Chris@250
|
249
|
Chris@250
|
250 lines = comment.split('\n');
|
Chris@250
|
251 lineCount = lines.size();
|
Chris@250
|
252
|
Chris@250
|
253 if (lineCount < 2) lineCount = 2;
|
Chris@250
|
254 }
|
Chris@250
|
255
|
Chris@250
|
256 int height = (lineCount + 1) * fh + 2;
|
Chris@56
|
257 QRectF r(x0, 0, width - 3, height);
|
Chris@250
|
258
|
Chris@250
|
259 if (showProperLines) {
|
Chris@250
|
260
|
Chris@250
|
261 paint->setBrush(Qt::white);
|
Chris@250
|
262
|
Chris@250
|
263 if (m_current) {
|
Chris@250
|
264 paint->drawRect(QRectF(x0 - 4, -4, width + 5, height + 8));
|
Chris@250
|
265 }
|
Chris@250
|
266
|
Chris@250
|
267 if (m_new) {
|
Chris@250
|
268 paint->save();
|
Chris@250
|
269 paint->setPen(Qt::yellow);
|
Chris@250
|
270 paint->setBrush(Qt::NoBrush);
|
Chris@250
|
271 paint->drawRect(QRectF(x0 - 2, -2, width + 1, height + 4));
|
Chris@250
|
272 paint->restore();
|
Chris@250
|
273 }
|
Chris@250
|
274 }
|
Chris@250
|
275
|
Chris@53
|
276 paint->drawRect(r);
|
Chris@53
|
277
|
Chris@250
|
278 if (!showText) {
|
Chris@53
|
279 paint->restore();
|
Chris@53
|
280 return;
|
Chris@53
|
281 }
|
Chris@53
|
282
|
Chris@55
|
283 paint->fillRect(QRectF(x0 + 0.5, 0.5, width - 4, fh - 0.5),
|
Chris@55
|
284 QBrush(userColour));
|
Chris@53
|
285
|
Chris@53
|
286 paint->setPen(QPen(Qt::white));
|
Chris@53
|
287
|
Chris@250
|
288 QString person = TextAbbrev::abbreviate(m_changeset->authorName(),
|
Chris@250
|
289 fm, textwid);
|
Chris@55
|
290 paint->drawText(x0 + 3, fm.ascent(), person);
|
Chris@53
|
291
|
Chris@53
|
292 paint->setPen(QPen(Qt::black));
|
Chris@53
|
293
|
Chris@147
|
294 QStringList tags = m_changeset->tags();
|
Chris@147
|
295 if (!tags.empty()) {
|
Chris@147
|
296 QStringList nonTipTags;
|
Chris@147
|
297 foreach (QString t, tags) {
|
Chris@147
|
298 // I'm not convinced that showing the tip tag really
|
Chris@147
|
299 // works; I think perhaps it confuses as much as it
|
Chris@147
|
300 // illuminates. But also, our current implementation
|
Chris@147
|
301 // doesn't interact well with it because it moves -- it's
|
Chris@147
|
302 // the one thing that can actually (in normal use) change
|
Chris@147
|
303 // inside an existing changeset record even during an
|
Chris@147
|
304 // incremental update
|
Chris@147
|
305 if (t != "tip") nonTipTags.push_back(t);
|
Chris@147
|
306 }
|
Chris@147
|
307 if (!nonTipTags.empty()) {
|
Chris@147
|
308 QString tagText = nonTipTags.join(" ").trimmed();
|
Chris@147
|
309 int tw = fm.width(tagText);
|
Chris@147
|
310 paint->fillRect(QRectF(x0 + width - 8 - tw, 1, tw + 4, fh - 1),
|
Chris@147
|
311 QBrush(Qt::yellow));
|
Chris@147
|
312 paint->drawText(x0 + width - 6 - tw, fm.ascent(), tagText);
|
Chris@147
|
313 }
|
Chris@128
|
314 }
|
Chris@128
|
315
|
Chris@74
|
316 if (m_showBranch) {
|
Chris@56
|
317 // write branch name
|
Chris@153
|
318 paint->save();
|
Chris@56
|
319 f.setBold(true);
|
Chris@56
|
320 paint->setFont(f);
|
Chris@153
|
321 paint->setPen(QPen(branchColour));
|
Chris@56
|
322 QString branch = m_changeset->branch();
|
Chris@74
|
323 if (branch == "") branch = "default";
|
Chris@56
|
324 int wid = width - 3;
|
Chris@56
|
325 branch = TextAbbrev::abbreviate(branch, QFontMetrics(f), wid);
|
Chris@56
|
326 paint->drawText(x0, -fh + fm.ascent() - 4, branch);
|
Chris@56
|
327 f.setBold(false);
|
Chris@153
|
328 paint->restore();
|
Chris@56
|
329 }
|
Chris@56
|
330
|
Chris@53
|
331 paint->setFont(f);
|
Chris@53
|
332
|
Chris@53
|
333 for (int i = 0; i < lines.size(); ++i) {
|
Chris@55
|
334 paint->drawText(x0 + 3, i * fh + fh + fm.ascent(), lines[i].trimmed());
|
Chris@53
|
335 }
|
Chris@53
|
336
|
Chris@53
|
337 paint->restore();
|
Chris@43
|
338 }
|