annotate changesetitem.cpp @ 204:d256cc418fa3

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