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