annotate changesetitem.cpp @ 288:ca6edd303c9d

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