Chris@57: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@57: Chris@57: /* Chris@57: EasyMercurial Chris@57: Chris@57: Based on HgExplorer by Jari Korhonen Chris@57: Copyright (c) 2010 Jari Korhonen Chris@57: Copyright (c) 2010 Chris Cannam Chris@57: Copyright (c) 2010 Queen Mary, University of London Chris@57: Chris@57: This program is free software; you can redistribute it and/or Chris@57: modify it under the terms of the GNU General Public License as Chris@57: published by the Free Software Foundation; either version 2 of the Chris@57: License, or (at your option) any later version. See the file Chris@57: COPYING included with this distribution for more information. Chris@57: */ Chris@57: Chris@43: #include "changesetitem.h" Chris@117: #include "changesetdetailitem.h" Chris@43: #include "changeset.h" Chris@53: #include "textabbrev.h" Chris@53: #include "colourset.h" Chris@117: #include "debug.h" Chris@43: Chris@43: #include Chris@117: #include Chris@43: Chris@53: ChangesetItem::ChangesetItem(Changeset *cs) : Chris@117: m_changeset(cs), m_detail(0), Chris@128: m_showBranch(false), m_column(0), m_row(0), m_wide(false), m_current(false) Chris@53: { Chris@53: m_font = QFont(); Chris@53: m_font.setPixelSize(11); Chris@53: m_font.setBold(false); Chris@53: m_font.setItalic(false); Chris@53: } Chris@53: Chris@43: QRectF Chris@43: ChangesetItem::boundingRect() const Chris@43: { Chris@55: int w = 100; Chris@55: if (m_wide) w = 180; Chris@55: return QRectF(-((w-50)/2 - 1), -30, w - 3, 79); Chris@43: } Chris@43: Chris@43: void Chris@119: ChangesetItem::showDetail() Chris@117: { Chris@119: if (m_detail) return; Chris@117: m_detail = new ChangesetDetailItem(m_changeset); Chris@117: m_detail->setZValue(zValue() + 1); Chris@117: scene()->addItem(m_detail); Chris@117: int w = 100; Chris@117: if (m_wide) w = 180; Chris@124: int h = 80; Chris@124: // m_detail->moveBy(x() - (m_detail->boundingRect().width() - 50) / 2, Chris@124: // y() + 60); Chris@124: m_detail->moveBy(x() + (w + 50) / 2 + 10 + 0.5, Chris@124: y() - (m_detail->boundingRect().height() - h) / 2 + 0.5); Chris@119: emit detailShown(); Chris@119: } Chris@119: Chris@119: void Chris@119: ChangesetItem::hideDetail() Chris@119: { Chris@124: if (!m_detail) return; Chris@124: scene()->removeItem(m_detail); Chris@119: delete m_detail; Chris@119: m_detail = 0; Chris@119: emit detailHidden(); Chris@119: } Chris@119: Chris@119: void Chris@119: ChangesetItem::mousePressEvent(QGraphicsSceneMouseEvent *e) Chris@119: { Chris@119: DEBUG << "ChangesetItem::mousePressEvent" << endl; Chris@119: if (m_detail) { Chris@119: hideDetail(); Chris@119: } else { Chris@119: showDetail(); Chris@119: } Chris@117: } Chris@117: Chris@117: void Chris@43: ChangesetItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option, Chris@43: QWidget *w) Chris@43: { Chris@53: paint->save(); Chris@53: Chris@53: ColourSet *colourSet = ColourSet::instance(); Chris@53: QColor branchColour = colourSet->getColourFor(m_changeset->branch()); Chris@128: QColor userColour = colourSet->getColourFor(m_changeset->author()); Chris@53: Chris@53: QFont f(m_font); Chris@53: Chris@54: QTransform t = paint->worldTransform(); Chris@53: float scale = std::min(t.m11(), t.m22()); Chris@53: if (scale > 1.0) { Chris@53: int ps = int((f.pixelSize() / scale) + 0.5); Chris@53: if (ps < 8) ps = 8; Chris@53: f.setPixelSize(ps); Chris@53: } Chris@54: Chris@54: if (scale < 0.1) { Chris@54: paint->setPen(QPen(branchColour, 0)); Chris@54: } else { Chris@54: paint->setPen(QPen(branchColour, 2)); Chris@54: } Chris@53: Chris@53: paint->setFont(f); Chris@53: QFontMetrics fm(f); Chris@53: int fh = fm.height(); Chris@55: Chris@55: int width = 100; Chris@55: if (m_wide) width = 180; Chris@55: int x0 = -((width - 50) / 2 - 1); Chris@55: Chris@56: int height = 49; Chris@56: QRectF r(x0, 0, width - 3, height); Chris@53: paint->drawRect(r); Chris@53: Chris@128: if (m_current) { Chris@128: paint->drawRect(QRectF(x0 - 4, -4, width + 5, height + 8)); Chris@128: } Chris@128: Chris@53: if (scale < 0.1) { Chris@53: paint->restore(); Chris@53: return; Chris@53: } Chris@53: Chris@55: paint->fillRect(QRectF(x0 + 0.5, 0.5, width - 4, fh - 0.5), Chris@55: QBrush(userColour)); Chris@53: Chris@53: paint->setPen(QPen(Qt::white)); Chris@53: Chris@55: int wid = width - 5; Chris@128: QString person = TextAbbrev::abbreviate(m_changeset->authorName(), fm, wid); Chris@55: paint->drawText(x0 + 3, fm.ascent(), person); Chris@53: Chris@53: paint->setPen(QPen(Qt::black)); Chris@53: Chris@128: QString tags = m_changeset->tags().join(" ").trimmed(); Chris@128: if (tags != "") { Chris@128: int tw = fm.width(tags); Chris@128: paint->fillRect(QRectF(x0 + width - 8 - tw, 1, tw + 4, fh - 1), Chris@128: QBrush(Qt::yellow)); Chris@128: paint->drawText(x0 + width - 6 - tw, fm.ascent(), tags); Chris@128: } Chris@128: Chris@74: if (m_showBranch) { Chris@56: // write branch name Chris@56: f.setBold(true); Chris@56: paint->setFont(f); Chris@56: QString branch = m_changeset->branch(); Chris@74: if (branch == "") branch = "default"; Chris@56: int wid = width - 3; Chris@56: branch = TextAbbrev::abbreviate(branch, QFontMetrics(f), wid); Chris@56: paint->drawText(x0, -fh + fm.ascent() - 4, branch); Chris@56: f.setBold(false); Chris@56: } Chris@56: Chris@56: // f.setItalic(true); Chris@53: fm = QFontMetrics(f); Chris@53: fh = fm.height(); Chris@53: paint->setFont(f); Chris@53: Chris@53: QString comment = m_changeset->comment().trimmed(); Chris@66: comment = comment.replace("\\n", " "); Chris@53: comment = comment.replace(QRegExp("^\"\\s*\\**\\s*"), ""); Chris@53: comment = comment.replace(QRegExp("\"$"), ""); Chris@53: comment = comment.replace("\\\"", "\""); Chris@53: Chris@55: wid = width - 5; Chris@56: int nlines = (height / fh) - 1; Chris@56: if (nlines < 1) nlines = 1; Chris@53: comment = TextAbbrev::abbreviate(comment, fm, wid, TextAbbrev::ElideEnd, Chris@56: "...", nlines); Chris@53: Chris@53: QStringList lines = comment.split('\n'); Chris@53: for (int i = 0; i < lines.size(); ++i) { Chris@55: paint->drawText(x0 + 3, i * fh + fh + fm.ascent(), lines[i].trimmed()); Chris@53: } Chris@53: Chris@53: paint->restore(); Chris@43: }