annotate changesetitem.cpp @ 309:978b8c03118a

Make a further couple of dialogs use MoreInformationDialog
author Chris Cannam
date Mon, 28 Feb 2011 14:31:57 +0000
parents 37f67999b661
children 5b4aa1c24407
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@289 124 QAction *stat = menu->addAction(tr("Summarise changes"));
Chris@289 125 connect(stat, SIGNAL(triggered()), this, SLOT(showSummaryActivated()));
Chris@289 126
Chris@289 127 menu->addSeparator();
Chris@289 128
Chris@281 129 QStringList parents = m_changeset->parents();
Chris@153 130
Chris@288 131 QString leftId, rightId;
Chris@288 132 bool havePositions = false;
Chris@288 133
Chris@281 134 if (parents.size() > 1) {
Chris@281 135 ChangesetScene *cs = dynamic_cast<ChangesetScene *>(scene());
Chris@281 136 if (cs && parents.size() == 2) {
Chris@281 137 ChangesetItem *i0 = cs->getItemById(parents[0]);
Chris@281 138 ChangesetItem *i1 = cs->getItemById(parents[1]);
Chris@281 139 if (i0 && i1) {
Chris@281 140 if (i0->x() < i1->x()) {
Chris@281 141 leftId = parents[0];
Chris@281 142 rightId = parents[1];
Chris@281 143 } else {
Chris@281 144 leftId = parents[1];
Chris@281 145 rightId = parents[0];
Chris@281 146 }
Chris@281 147 havePositions = true;
Chris@281 148 }
Chris@281 149 }
Chris@288 150 }
Chris@281 151
Chris@288 152 if (parents.size() > 1) {
Chris@288 153 if (havePositions) {
Chris@281 154
Chris@288 155 QAction *diff = menu->addAction(tr("Diff to left parent"));
Chris@288 156 connect(diff, SIGNAL(triggered()), this, SLOT(diffToParentActivated()));
Chris@288 157 m_parentDiffActions[diff] = leftId;
Chris@281 158
Chris@288 159 diff = menu->addAction(tr("Diff to right parent"));
Chris@288 160 connect(diff, SIGNAL(triggered()), this, SLOT(diffToParentActivated()));
Chris@288 161 m_parentDiffActions[diff] = rightId;
Chris@281 162
Chris@281 163 } else {
Chris@281 164
Chris@281 165 foreach (QString parentId, parents) {
Chris@288 166 QString text = tr("Diff to parent %1").arg(Changeset::hashOf(parentId));
Chris@288 167 QAction *diff = menu->addAction(text);
Chris@288 168 connect(diff, SIGNAL(triggered()), this, SLOT(diffToParentActivated()));
Chris@288 169 m_parentDiffActions[diff] = parentId;
Chris@281 170 }
Chris@153 171 }
Chris@153 172
Chris@153 173 } else {
Chris@153 174
Chris@288 175 QAction *diff = menu->addAction(tr("Diff to parent"));
Chris@288 176 connect(diff, SIGNAL(triggered()), this, SLOT(diffToParentActivated()));
Chris@153 177 }
Chris@153 178
Chris@153 179 QAction *diffCurrent = menu->addAction(tr("Diff to current working folder"));
Chris@141 180 connect(diffCurrent, SIGNAL(triggered()), this, SLOT(diffToCurrentActivated()));
Chris@141 181
Chris@140 182 menu->addSeparator();
Chris@141 183
Chris@153 184 QAction *update = menu->addAction(tr("Update to this revision"));
Chris@153 185 connect(update, SIGNAL(triggered()), this, SLOT(updateActivated()));
Chris@153 186
Chris@140 187 QAction *merge = menu->addAction(tr("Merge from here to current"));
Chris@141 188 connect(merge, SIGNAL(triggered()), this, SLOT(mergeActivated()));
Chris@153 189
Chris@153 190 menu->addSeparator();
Chris@153 191
Chris@153 192 QAction *tag = menu->addAction(tr("Add tag..."));
Chris@141 193 connect(tag, SIGNAL(triggered()), this, SLOT(tagActivated()));
Chris@141 194
Chris@148 195 menu->exec(QCursor::pos());
Chris@148 196
Chris@141 197 ungrabMouse();
Chris@140 198 }
Chris@140 199
Chris@153 200 void
Chris@153 201 ChangesetItem::copyIdActivated()
Chris@153 202 {
Chris@153 203 QClipboard *clipboard = QApplication::clipboard();
Chris@153 204 clipboard->setText(Changeset::hashOf(m_changeset->id()));
Chris@153 205 }
Chris@153 206
Chris@153 207 void ChangesetItem::diffToParentActivated()
Chris@153 208 {
Chris@153 209 QAction *a = qobject_cast<QAction *>(sender());
Chris@153 210 QString parentId;
Chris@153 211 if (m_parentDiffActions.contains(a)) {
Chris@153 212 parentId = m_parentDiffActions[a];
Chris@153 213 DEBUG << "ChangesetItem::diffToParentActivated: specific parent "
Chris@153 214 << parentId << " selected" << endl;
Chris@153 215 } else {
Chris@153 216 parentId = m_changeset->parents()[0];
Chris@153 217 DEBUG << "ChangesetItem::diffToParentActivated: "
Chris@153 218 << "no specific parent selected, using first parent "
Chris@153 219 << parentId << endl;
Chris@153 220 }
Chris@153 221
Chris@153 222 emit diffToParent(getId(), parentId);
Chris@153 223 }
Chris@153 224
Chris@289 225 void ChangesetItem::showSummaryActivated()
Chris@288 226 {
Chris@289 227 emit showSummary(m_changeset);
Chris@288 228 }
Chris@288 229
Chris@141 230 void ChangesetItem::updateActivated() { emit updateTo(getId()); }
Chris@141 231 void ChangesetItem::diffToCurrentActivated() { emit diffToCurrent(getId()); }
Chris@141 232 void ChangesetItem::mergeActivated() { emit mergeFrom(getId()); }
Chris@141 233 void ChangesetItem::tagActivated() { emit tag(getId()); }
Chris@141 234
Chris@140 235 void
Chris@288 236 ChangesetItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *, QWidget *)
Chris@43 237 {
Chris@53 238 paint->save();
Chris@53 239
Chris@53 240 ColourSet *colourSet = ColourSet::instance();
Chris@53 241 QColor branchColour = colourSet->getColourFor(m_changeset->branch());
Chris@128 242 QColor userColour = colourSet->getColourFor(m_changeset->author());
Chris@53 243
Chris@53 244 QFont f(m_font);
Chris@53 245
Chris@54 246 QTransform t = paint->worldTransform();
Chris@53 247 float scale = std::min(t.m11(), t.m22());
Chris@53 248 if (scale > 1.0) {
Chris@53 249 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@53 250 if (ps < 8) ps = 8;
Chris@53 251 f.setPixelSize(ps);
Chris@53 252 }
Chris@54 253
Chris@250 254 bool showText = (scale >= 0.2);
Chris@250 255 bool showProperLines = (scale >= 0.1);
Chris@250 256
Chris@250 257 if (!showProperLines) {
Chris@54 258 paint->setPen(QPen(branchColour, 0));
Chris@54 259 } else {
Chris@54 260 paint->setPen(QPen(branchColour, 2));
Chris@54 261 }
Chris@53 262
Chris@53 263 paint->setFont(f);
Chris@53 264 QFontMetrics fm(f);
Chris@53 265 int fh = fm.height();
Chris@55 266
Chris@55 267 int width = 100;
Chris@55 268 if (m_wide) width = 180;
Chris@55 269 int x0 = -((width - 50) / 2 - 1);
Chris@55 270
Chris@250 271 int textwid = width - 7;
Chris@250 272
Chris@250 273 QString comment;
Chris@250 274 QStringList lines;
Chris@250 275 int lineCount = 3;
Chris@250 276
Chris@250 277 if (showText) {
Chris@250 278
Chris@250 279 comment = m_changeset->comment().trimmed();
Chris@250 280 comment = comment.replace("\\n", " ");
Chris@250 281 comment = comment.replace(QRegExp("^\"\\s*\\**\\s*"), "");
Chris@250 282 comment = comment.replace(QRegExp("\"$"), "");
Chris@250 283 comment = comment.replace("\\\"", "\"");
Chris@250 284
Chris@250 285 comment = TextAbbrev::abbreviate(comment, fm, textwid,
Chris@250 286 TextAbbrev::ElideEnd, "...", 3);
Chris@250 287 // abbreviate() changes this (ouch!), restore it
Chris@250 288 textwid = width - 5;
Chris@250 289
Chris@250 290 lines = comment.split('\n');
Chris@250 291 lineCount = lines.size();
Chris@250 292
Chris@250 293 if (lineCount < 2) lineCount = 2;
Chris@250 294 }
Chris@250 295
Chris@250 296 int height = (lineCount + 1) * fh + 2;
Chris@56 297 QRectF r(x0, 0, width - 3, height);
Chris@250 298
Chris@250 299 if (showProperLines) {
Chris@250 300
Chris@250 301 paint->setBrush(Qt::white);
Chris@250 302
Chris@250 303 if (m_current) {
Chris@250 304 paint->drawRect(QRectF(x0 - 4, -4, width + 5, height + 8));
Chris@250 305 }
Chris@250 306
Chris@250 307 if (m_new) {
Chris@250 308 paint->save();
Chris@250 309 paint->setPen(Qt::yellow);
Chris@250 310 paint->setBrush(Qt::NoBrush);
Chris@250 311 paint->drawRect(QRectF(x0 - 2, -2, width + 1, height + 4));
Chris@250 312 paint->restore();
Chris@250 313 }
Chris@250 314 }
Chris@250 315
Chris@53 316 paint->drawRect(r);
Chris@53 317
Chris@250 318 if (!showText) {
Chris@53 319 paint->restore();
Chris@53 320 return;
Chris@53 321 }
Chris@53 322
Chris@55 323 paint->fillRect(QRectF(x0 + 0.5, 0.5, width - 4, fh - 0.5),
Chris@55 324 QBrush(userColour));
Chris@53 325
Chris@53 326 paint->setPen(QPen(Qt::white));
Chris@53 327
Chris@250 328 QString person = TextAbbrev::abbreviate(m_changeset->authorName(),
Chris@250 329 fm, textwid);
Chris@55 330 paint->drawText(x0 + 3, fm.ascent(), person);
Chris@53 331
Chris@53 332 paint->setPen(QPen(Qt::black));
Chris@53 333
Chris@147 334 QStringList tags = m_changeset->tags();
Chris@147 335 if (!tags.empty()) {
Chris@147 336 QStringList nonTipTags;
Chris@147 337 foreach (QString t, tags) {
Chris@147 338 // I'm not convinced that showing the tip tag really
Chris@147 339 // works; I think perhaps it confuses as much as it
Chris@147 340 // illuminates. But also, our current implementation
Chris@147 341 // doesn't interact well with it because it moves -- it's
Chris@147 342 // the one thing that can actually (in normal use) change
Chris@147 343 // inside an existing changeset record even during an
Chris@147 344 // incremental update
Chris@147 345 if (t != "tip") nonTipTags.push_back(t);
Chris@147 346 }
Chris@147 347 if (!nonTipTags.empty()) {
Chris@147 348 QString tagText = nonTipTags.join(" ").trimmed();
Chris@147 349 int tw = fm.width(tagText);
Chris@147 350 paint->fillRect(QRectF(x0 + width - 8 - tw, 1, tw + 4, fh - 1),
Chris@147 351 QBrush(Qt::yellow));
Chris@147 352 paint->drawText(x0 + width - 6 - tw, fm.ascent(), tagText);
Chris@147 353 }
Chris@128 354 }
Chris@128 355
Chris@74 356 if (m_showBranch) {
Chris@56 357 // write branch name
Chris@153 358 paint->save();
Chris@56 359 f.setBold(true);
Chris@56 360 paint->setFont(f);
Chris@153 361 paint->setPen(QPen(branchColour));
Chris@56 362 QString branch = m_changeset->branch();
Chris@74 363 if (branch == "") branch = "default";
Chris@56 364 int wid = width - 3;
Chris@56 365 branch = TextAbbrev::abbreviate(branch, QFontMetrics(f), wid);
Chris@56 366 paint->drawText(x0, -fh + fm.ascent() - 4, branch);
Chris@56 367 f.setBold(false);
Chris@153 368 paint->restore();
Chris@56 369 }
Chris@56 370
Chris@53 371 paint->setFont(f);
Chris@53 372
Chris@53 373 for (int i = 0; i < lines.size(); ++i) {
Chris@55 374 paint->drawText(x0 + 3, i * fh + fh + fm.ascent(), lines[i].trimmed());
Chris@53 375 }
Chris@53 376
Chris@53 377 paint->restore();
Chris@43 378 }