annotate changesetitem.cpp @ 278:f7cdd5b31aed new-branches

Add "Start new branch" function. I'm not quite satisfied with the location of this one
author Chris Cannam
date Wed, 09 Feb 2011 12:03:15 +0000
parents be483734bde5
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@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@250 58 return QRectF(-((w-50)/2 - 1), -30, w - 3, 90);
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@278 156 QAction *branch = menu->addAction(tr("Start new branch..."));
Chris@278 157 branch->setEnabled(m_current);
Chris@278 158 connect(branch, SIGNAL(triggered()), this, SLOT(newBranchActivated()));
Chris@278 159
Chris@153 160 QAction *tag = menu->addAction(tr("Add tag..."));
Chris@141 161 connect(tag, SIGNAL(triggered()), this, SLOT(tagActivated()));
Chris@141 162
Chris@148 163 menu->exec(QCursor::pos());
Chris@148 164
Chris@141 165 ungrabMouse();
Chris@140 166 }
Chris@140 167
Chris@153 168 void
Chris@153 169 ChangesetItem::copyIdActivated()
Chris@153 170 {
Chris@153 171 QClipboard *clipboard = QApplication::clipboard();
Chris@153 172 clipboard->setText(Changeset::hashOf(m_changeset->id()));
Chris@153 173 }
Chris@153 174
Chris@153 175 void ChangesetItem::diffToParentActivated()
Chris@153 176 {
Chris@153 177 QAction *a = qobject_cast<QAction *>(sender());
Chris@153 178 QString parentId;
Chris@153 179 if (m_parentDiffActions.contains(a)) {
Chris@153 180 parentId = m_parentDiffActions[a];
Chris@153 181 DEBUG << "ChangesetItem::diffToParentActivated: specific parent "
Chris@153 182 << parentId << " selected" << endl;
Chris@153 183 } else {
Chris@153 184 parentId = m_changeset->parents()[0];
Chris@153 185 DEBUG << "ChangesetItem::diffToParentActivated: "
Chris@153 186 << "no specific parent selected, using first parent "
Chris@153 187 << parentId << endl;
Chris@153 188 }
Chris@153 189
Chris@153 190 emit diffToParent(getId(), parentId);
Chris@153 191 }
Chris@153 192
Chris@141 193 void ChangesetItem::updateActivated() { emit updateTo(getId()); }
Chris@141 194 void ChangesetItem::diffToCurrentActivated() { emit diffToCurrent(getId()); }
Chris@141 195 void ChangesetItem::mergeActivated() { emit mergeFrom(getId()); }
Chris@141 196 void ChangesetItem::tagActivated() { emit tag(getId()); }
Chris@278 197 void ChangesetItem::newBranchActivated() { emit newBranch(getId()); }
Chris@141 198
Chris@140 199 void
Chris@43 200 ChangesetItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option,
Chris@43 201 QWidget *w)
Chris@43 202 {
Chris@53 203 paint->save();
Chris@53 204
Chris@53 205 ColourSet *colourSet = ColourSet::instance();
Chris@53 206 QColor branchColour = colourSet->getColourFor(m_changeset->branch());
Chris@128 207 QColor userColour = colourSet->getColourFor(m_changeset->author());
Chris@53 208
Chris@53 209 QFont f(m_font);
Chris@53 210
Chris@54 211 QTransform t = paint->worldTransform();
Chris@53 212 float scale = std::min(t.m11(), t.m22());
Chris@53 213 if (scale > 1.0) {
Chris@53 214 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@53 215 if (ps < 8) ps = 8;
Chris@53 216 f.setPixelSize(ps);
Chris@53 217 }
Chris@54 218
Chris@250 219 bool showText = (scale >= 0.2);
Chris@250 220 bool showProperLines = (scale >= 0.1);
Chris@250 221
Chris@250 222 if (!showProperLines) {
Chris@54 223 paint->setPen(QPen(branchColour, 0));
Chris@54 224 } else {
Chris@54 225 paint->setPen(QPen(branchColour, 2));
Chris@54 226 }
Chris@53 227
Chris@53 228 paint->setFont(f);
Chris@53 229 QFontMetrics fm(f);
Chris@53 230 int fh = fm.height();
Chris@55 231
Chris@55 232 int width = 100;
Chris@55 233 if (m_wide) width = 180;
Chris@55 234 int x0 = -((width - 50) / 2 - 1);
Chris@55 235
Chris@250 236 int textwid = width - 7;
Chris@250 237
Chris@250 238 QString comment;
Chris@250 239 QStringList lines;
Chris@250 240 int lineCount = 3;
Chris@250 241
Chris@250 242 if (showText) {
Chris@250 243
Chris@250 244 comment = m_changeset->comment().trimmed();
Chris@250 245 comment = comment.replace("\\n", " ");
Chris@250 246 comment = comment.replace(QRegExp("^\"\\s*\\**\\s*"), "");
Chris@250 247 comment = comment.replace(QRegExp("\"$"), "");
Chris@250 248 comment = comment.replace("\\\"", "\"");
Chris@250 249
Chris@250 250 comment = TextAbbrev::abbreviate(comment, fm, textwid,
Chris@250 251 TextAbbrev::ElideEnd, "...", 3);
Chris@250 252 // abbreviate() changes this (ouch!), restore it
Chris@250 253 textwid = width - 5;
Chris@250 254
Chris@250 255 lines = comment.split('\n');
Chris@250 256 lineCount = lines.size();
Chris@250 257
Chris@250 258 if (lineCount < 2) lineCount = 2;
Chris@250 259 }
Chris@250 260
Chris@250 261 int height = (lineCount + 1) * fh + 2;
Chris@56 262 QRectF r(x0, 0, width - 3, height);
Chris@250 263
Chris@250 264 if (showProperLines) {
Chris@250 265
Chris@250 266 paint->setBrush(Qt::white);
Chris@250 267
Chris@250 268 if (m_current) {
Chris@250 269 paint->drawRect(QRectF(x0 - 4, -4, width + 5, height + 8));
Chris@250 270 }
Chris@250 271
Chris@250 272 if (m_new) {
Chris@250 273 paint->save();
Chris@250 274 paint->setPen(Qt::yellow);
Chris@250 275 paint->setBrush(Qt::NoBrush);
Chris@250 276 paint->drawRect(QRectF(x0 - 2, -2, width + 1, height + 4));
Chris@250 277 paint->restore();
Chris@250 278 }
Chris@250 279 }
Chris@250 280
Chris@53 281 paint->drawRect(r);
Chris@53 282
Chris@250 283 if (!showText) {
Chris@53 284 paint->restore();
Chris@53 285 return;
Chris@53 286 }
Chris@53 287
Chris@55 288 paint->fillRect(QRectF(x0 + 0.5, 0.5, width - 4, fh - 0.5),
Chris@55 289 QBrush(userColour));
Chris@53 290
Chris@53 291 paint->setPen(QPen(Qt::white));
Chris@53 292
Chris@250 293 QString person = TextAbbrev::abbreviate(m_changeset->authorName(),
Chris@250 294 fm, textwid);
Chris@55 295 paint->drawText(x0 + 3, fm.ascent(), person);
Chris@53 296
Chris@53 297 paint->setPen(QPen(Qt::black));
Chris@53 298
Chris@147 299 QStringList tags = m_changeset->tags();
Chris@147 300 if (!tags.empty()) {
Chris@147 301 QStringList nonTipTags;
Chris@147 302 foreach (QString t, tags) {
Chris@147 303 // I'm not convinced that showing the tip tag really
Chris@147 304 // works; I think perhaps it confuses as much as it
Chris@147 305 // illuminates. But also, our current implementation
Chris@147 306 // doesn't interact well with it because it moves -- it's
Chris@147 307 // the one thing that can actually (in normal use) change
Chris@147 308 // inside an existing changeset record even during an
Chris@147 309 // incremental update
Chris@147 310 if (t != "tip") nonTipTags.push_back(t);
Chris@147 311 }
Chris@147 312 if (!nonTipTags.empty()) {
Chris@147 313 QString tagText = nonTipTags.join(" ").trimmed();
Chris@147 314 int tw = fm.width(tagText);
Chris@147 315 paint->fillRect(QRectF(x0 + width - 8 - tw, 1, tw + 4, fh - 1),
Chris@147 316 QBrush(Qt::yellow));
Chris@147 317 paint->drawText(x0 + width - 6 - tw, fm.ascent(), tagText);
Chris@147 318 }
Chris@128 319 }
Chris@128 320
Chris@74 321 if (m_showBranch) {
Chris@56 322 // write branch name
Chris@153 323 paint->save();
Chris@56 324 f.setBold(true);
Chris@56 325 paint->setFont(f);
Chris@153 326 paint->setPen(QPen(branchColour));
Chris@56 327 QString branch = m_changeset->branch();
Chris@74 328 if (branch == "") branch = "default";
Chris@56 329 int wid = width - 3;
Chris@56 330 branch = TextAbbrev::abbreviate(branch, QFontMetrics(f), wid);
Chris@56 331 paint->drawText(x0, -fh + fm.ascent() - 4, branch);
Chris@56 332 f.setBold(false);
Chris@153 333 paint->restore();
Chris@56 334 }
Chris@56 335
Chris@53 336 paint->setFont(f);
Chris@53 337
Chris@53 338 for (int i = 0; i < lines.size(); ++i) {
Chris@55 339 paint->drawText(x0 + 3, i * fh + fh + fm.ascent(), lines[i].trimmed());
Chris@53 340 }
Chris@53 341
Chris@53 342 paint->restore();
Chris@43 343 }