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