annotate src/changesetitem.cpp @ 389:496f2042155a item_appearance_adjustments

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