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@43: #include "changeset.h" Chris@53: #include "textabbrev.h" Chris@53: #include "colourset.h" Chris@43: Chris@43: #include Chris@43: Chris@53: ChangesetItem::ChangesetItem(Changeset *cs) : Chris@74: m_changeset(cs), m_showBranch(false), m_column(0), m_row(0), m_wide(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@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@53: 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@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@53: 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@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: }