annotate changesetitem.cpp @ 139:e8a481789607

* Avoid unnecessary duplicate layout; fix off-by-one in resetting changeset children
author Chris Cannam
date Tue, 30 Nov 2010 14:27:34 +0000
parents aaeab914f2a3
children bad40d7e7a2b
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@43 28
Chris@53 29 ChangesetItem::ChangesetItem(Changeset *cs) :
Chris@117 30 m_changeset(cs), m_detail(0),
Chris@133 31 m_showBranch(false), m_column(0), m_row(0), m_wide(false),
Chris@133 32 m_current(false), m_new(false)
Chris@53 33 {
Chris@53 34 m_font = QFont();
Chris@53 35 m_font.setPixelSize(11);
Chris@53 36 m_font.setBold(false);
Chris@53 37 m_font.setItalic(false);
Chris@53 38 }
Chris@53 39
Chris@43 40 QRectF
Chris@43 41 ChangesetItem::boundingRect() const
Chris@43 42 {
Chris@55 43 int w = 100;
Chris@55 44 if (m_wide) w = 180;
Chris@55 45 return QRectF(-((w-50)/2 - 1), -30, w - 3, 79);
Chris@43 46 }
Chris@43 47
Chris@43 48 void
Chris@119 49 ChangesetItem::showDetail()
Chris@117 50 {
Chris@119 51 if (m_detail) return;
Chris@117 52 m_detail = new ChangesetDetailItem(m_changeset);
Chris@117 53 m_detail->setZValue(zValue() + 1);
Chris@117 54 scene()->addItem(m_detail);
Chris@117 55 int w = 100;
Chris@117 56 if (m_wide) w = 180;
Chris@124 57 int h = 80;
Chris@124 58 // m_detail->moveBy(x() - (m_detail->boundingRect().width() - 50) / 2,
Chris@124 59 // y() + 60);
Chris@124 60 m_detail->moveBy(x() + (w + 50) / 2 + 10 + 0.5,
Chris@124 61 y() - (m_detail->boundingRect().height() - h) / 2 + 0.5);
Chris@119 62 emit detailShown();
Chris@119 63 }
Chris@119 64
Chris@119 65 void
Chris@119 66 ChangesetItem::hideDetail()
Chris@119 67 {
Chris@124 68 if (!m_detail) return;
Chris@124 69 scene()->removeItem(m_detail);
Chris@119 70 delete m_detail;
Chris@119 71 m_detail = 0;
Chris@119 72 emit detailHidden();
Chris@119 73 }
Chris@119 74
Chris@119 75 void
Chris@119 76 ChangesetItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
Chris@119 77 {
Chris@119 78 DEBUG << "ChangesetItem::mousePressEvent" << endl;
Chris@132 79 if (e->button() == Qt::LeftButton) {
Chris@132 80 if (m_detail) {
Chris@132 81 hideDetail();
Chris@132 82 } else {
Chris@132 83 showDetail();
Chris@132 84 }
Chris@119 85 }
Chris@117 86 }
Chris@117 87
Chris@117 88 void
Chris@43 89 ChangesetItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option,
Chris@43 90 QWidget *w)
Chris@43 91 {
Chris@53 92 paint->save();
Chris@53 93
Chris@53 94 ColourSet *colourSet = ColourSet::instance();
Chris@53 95 QColor branchColour = colourSet->getColourFor(m_changeset->branch());
Chris@128 96 QColor userColour = colourSet->getColourFor(m_changeset->author());
Chris@53 97
Chris@53 98 QFont f(m_font);
Chris@53 99
Chris@54 100 QTransform t = paint->worldTransform();
Chris@53 101 float scale = std::min(t.m11(), t.m22());
Chris@53 102 if (scale > 1.0) {
Chris@53 103 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@53 104 if (ps < 8) ps = 8;
Chris@53 105 f.setPixelSize(ps);
Chris@53 106 }
Chris@54 107
Chris@54 108 if (scale < 0.1) {
Chris@54 109 paint->setPen(QPen(branchColour, 0));
Chris@54 110 } else {
Chris@54 111 paint->setPen(QPen(branchColour, 2));
Chris@54 112 }
Chris@53 113
Chris@53 114 paint->setFont(f);
Chris@53 115 QFontMetrics fm(f);
Chris@53 116 int fh = fm.height();
Chris@55 117
Chris@55 118 int width = 100;
Chris@55 119 if (m_wide) width = 180;
Chris@55 120 int x0 = -((width - 50) / 2 - 1);
Chris@55 121
Chris@56 122 int height = 49;
Chris@56 123 QRectF r(x0, 0, width - 3, height);
Chris@53 124 paint->drawRect(r);
Chris@53 125
Chris@133 126 if (m_new) {
Chris@133 127 paint->save();
Chris@133 128 paint->setPen(Qt::yellow);
Chris@133 129 paint->drawRect(QRectF(x0 - 2, -2, width + 1, height + 4));
Chris@133 130 paint->restore();
Chris@133 131 }
Chris@133 132
Chris@128 133 if (m_current) {
Chris@128 134 paint->drawRect(QRectF(x0 - 4, -4, width + 5, height + 8));
Chris@128 135 }
Chris@128 136
Chris@53 137 if (scale < 0.1) {
Chris@53 138 paint->restore();
Chris@53 139 return;
Chris@53 140 }
Chris@53 141
Chris@55 142 paint->fillRect(QRectF(x0 + 0.5, 0.5, width - 4, fh - 0.5),
Chris@55 143 QBrush(userColour));
Chris@53 144
Chris@53 145 paint->setPen(QPen(Qt::white));
Chris@53 146
Chris@55 147 int wid = width - 5;
Chris@128 148 QString person = TextAbbrev::abbreviate(m_changeset->authorName(), fm, wid);
Chris@55 149 paint->drawText(x0 + 3, fm.ascent(), person);
Chris@53 150
Chris@53 151 paint->setPen(QPen(Qt::black));
Chris@53 152
Chris@128 153 QString tags = m_changeset->tags().join(" ").trimmed();
Chris@128 154 if (tags != "") {
Chris@128 155 int tw = fm.width(tags);
Chris@128 156 paint->fillRect(QRectF(x0 + width - 8 - tw, 1, tw + 4, fh - 1),
Chris@128 157 QBrush(Qt::yellow));
Chris@128 158 paint->drawText(x0 + width - 6 - tw, fm.ascent(), tags);
Chris@128 159 }
Chris@128 160
Chris@74 161 if (m_showBranch) {
Chris@56 162 // write branch name
Chris@56 163 f.setBold(true);
Chris@56 164 paint->setFont(f);
Chris@56 165 QString branch = m_changeset->branch();
Chris@74 166 if (branch == "") branch = "default";
Chris@56 167 int wid = width - 3;
Chris@56 168 branch = TextAbbrev::abbreviate(branch, QFontMetrics(f), wid);
Chris@56 169 paint->drawText(x0, -fh + fm.ascent() - 4, branch);
Chris@56 170 f.setBold(false);
Chris@56 171 }
Chris@56 172
Chris@56 173 // f.setItalic(true);
Chris@53 174 fm = QFontMetrics(f);
Chris@53 175 fh = fm.height();
Chris@53 176 paint->setFont(f);
Chris@53 177
Chris@53 178 QString comment = m_changeset->comment().trimmed();
Chris@66 179 comment = comment.replace("\\n", " ");
Chris@53 180 comment = comment.replace(QRegExp("^\"\\s*\\**\\s*"), "");
Chris@53 181 comment = comment.replace(QRegExp("\"$"), "");
Chris@53 182 comment = comment.replace("\\\"", "\"");
Chris@53 183
Chris@55 184 wid = width - 5;
Chris@56 185 int nlines = (height / fh) - 1;
Chris@56 186 if (nlines < 1) nlines = 1;
Chris@53 187 comment = TextAbbrev::abbreviate(comment, fm, wid, TextAbbrev::ElideEnd,
Chris@56 188 "...", nlines);
Chris@53 189
Chris@53 190 QStringList lines = comment.split('\n');
Chris@53 191 for (int i = 0; i < lines.size(); ++i) {
Chris@55 192 paint->drawText(x0 + 3, i * fh + fh + fm.ascent(), lines[i].trimmed());
Chris@53 193 }
Chris@53 194
Chris@53 195 paint->restore();
Chris@43 196 }