annotate src/changesetdetailitem.cpp @ 490:cd3dd285d0ff

OS/X build fixes
author Chris Cannam
date Thu, 18 Aug 2011 12:57:35 +0100
parents 75003687f364
children 533519ebc0cb
rev   line source
Chris@117 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@117 2
Chris@117 3 /*
Chris@117 4 EasyMercurial
Chris@117 5
Chris@117 6 Based on HgExplorer by Jari Korhonen
Chris@117 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@117 10
Chris@117 11 This program is free software; you can redistribute it and/or
Chris@117 12 modify it under the terms of the GNU General Public License as
Chris@117 13 published by the Free Software Foundation; either version 2 of the
Chris@117 14 License, or (at your option) any later version. See the file
Chris@117 15 COPYING included with this distribution for more information.
Chris@117 16 */
Chris@117 17
Chris@117 18 #include "changesetdetailitem.h"
Chris@117 19 #include "changeset.h"
Chris@117 20 #include "textabbrev.h"
Chris@117 21 #include "colourset.h"
Chris@117 22 #include "debug.h"
Chris@118 23 #include "common.h"
Chris@117 24
Chris@118 25 #include <QTextDocument>
Chris@117 26 #include <QPainter>
Chris@117 27
Chris@117 28 ChangesetDetailItem::ChangesetDetailItem(Changeset *cs) :
Chris@118 29 m_changeset(cs), m_doc(0)
Chris@117 30 {
Chris@117 31 m_font = QFont();
Chris@117 32 m_font.setPixelSize(11);
Chris@117 33 m_font.setBold(false);
Chris@117 34 m_font.setItalic(false);
Chris@118 35
Chris@118 36 makeDocument();
Chris@118 37 }
Chris@118 38
Chris@118 39 ChangesetDetailItem::~ChangesetDetailItem()
Chris@118 40 {
Chris@118 41 delete m_doc;
Chris@117 42 }
Chris@117 43
Chris@117 44 QRectF
Chris@117 45 ChangesetDetailItem::boundingRect() const
Chris@117 46 {
Chris@118 47 int w = 350;
Chris@118 48 m_doc->setTextWidth(w);
Chris@124 49 return QRectF(-10, -10, w + 10, m_doc->size().height() + 10);
Chris@117 50 }
Chris@117 51
Chris@402 52 QVariant
Chris@402 53 ChangesetDetailItem::itemChange(GraphicsItemChange c, const QVariant &v)
Chris@402 54 {
Chris@402 55 if (c == ItemVisibleHasChanged) {
Chris@402 56 bool visible = v.toBool();
Chris@402 57 DEBUG << "ChangesetDetailItem::itemChange: visible = " << visible << endl;
Chris@402 58 if (visible && scene()) {
Chris@402 59 ensureVisible();
Chris@402 60 }
Chris@402 61 }
Chris@402 62 return v;
Chris@402 63 }
Chris@402 64
Chris@117 65 void
Chris@117 66 ChangesetDetailItem::paint(QPainter *paint,
Chris@117 67 const QStyleOptionGraphicsItem *option,
Chris@117 68 QWidget *w)
Chris@117 69 {
Chris@117 70 paint->save();
Chris@117 71
Chris@117 72 ColourSet *colourSet = ColourSet::instance();
Chris@117 73 QColor branchColour = colourSet->getColourFor(m_changeset->branch());
Chris@128 74 QColor userColour = colourSet->getColourFor(m_changeset->author());
Chris@117 75
Chris@117 76 QFont f(m_font);
Chris@117 77
Chris@117 78 QTransform t = paint->worldTransform();
Chris@117 79 float scale = std::min(t.m11(), t.m22());
Chris@117 80 if (scale > 1.0) {
Chris@117 81 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@117 82 if (ps < 8) ps = 8;
Chris@117 83 f.setPixelSize(ps);
Chris@117 84 }
Chris@117 85
Chris@117 86 if (scale < 0.1) {
Chris@117 87 paint->setPen(QPen(branchColour, 0));
Chris@117 88 } else {
Chris@117 89 paint->setPen(QPen(branchColour, 2));
Chris@117 90 }
Chris@117 91
Chris@117 92 paint->setFont(f);
Chris@117 93 QFontMetrics fm(f);
Chris@117 94 int fh = fm.height();
Chris@117 95
Chris@117 96 int width = 350;
Chris@118 97 m_doc->setTextWidth(width);
Chris@118 98 int height = m_doc->size().height();
Chris@117 99
Chris@117 100 QRectF r(0.5, 0.5, width - 1, height - 1);
Chris@117 101 paint->setBrush(Qt::white);
Chris@386 102 paint->drawRoundedRect(r, 10, 10);
Chris@117 103
Chris@117 104 if (scale < 0.1) {
Chris@117 105 paint->restore();
Chris@117 106 return;
Chris@117 107 }
Chris@118 108
Chris@124 109 // little triangle connecting to its "owning" changeset item
Chris@124 110 paint->setBrush(branchColour);
Chris@124 111 QVector<QPointF> pts;
Chris@124 112 pts.push_back(QPointF(0, height/3 - 5));
Chris@124 113 pts.push_back(QPointF(0, height/3 + 5));
Chris@124 114 pts.push_back(QPointF(-10, height/3));
Chris@124 115 pts.push_back(QPointF(0, height/3 - 5));
Chris@124 116 paint->drawPolygon(QPolygonF(pts));
Chris@124 117
Chris@124 118 /*
Chris@118 119 paint->setBrush(branchColour);
Chris@118 120 QVector<QPointF> pts;
Chris@118 121 pts.push_back(QPointF(width/2 - 5, 0));
Chris@118 122 pts.push_back(QPointF(width/2 + 5, 0));
Chris@118 123 pts.push_back(QPointF(width/2, -10));
Chris@118 124 pts.push_back(QPointF(width/2 - 5, 0));
Chris@118 125 paint->drawPolygon(QPolygonF(pts));
Chris@124 126 */
Chris@118 127 m_doc->drawContents(paint, r);
Chris@118 128
Chris@117 129 paint->restore();
Chris@117 130 }
Chris@118 131
Chris@118 132 void
Chris@118 133 ChangesetDetailItem::makeDocument()
Chris@118 134 {
Chris@118 135 delete m_doc;
Chris@118 136 m_doc = new QTextDocument;
Chris@125 137 m_doc->setHtml(m_changeset->formatHtml());
Chris@118 138 }
Chris@118 139