annotate changesetdetailitem.cpp @ 124:1f27f71a7034

* Fixes to changeset details display; show on right instead of below to avoid disrupting flow
author Chris Cannam
date Mon, 29 Nov 2010 11:38:25 +0000
parents 9734fb0d6fff
children 63c2f3f61c79
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@117 8 Copyright (c) 2010 Chris Cannam
Chris@117 9 Copyright (c) 2010 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@117 52 void
Chris@117 53 ChangesetDetailItem::paint(QPainter *paint,
Chris@117 54 const QStyleOptionGraphicsItem *option,
Chris@117 55 QWidget *w)
Chris@117 56 {
Chris@117 57 paint->save();
Chris@117 58
Chris@117 59 ColourSet *colourSet = ColourSet::instance();
Chris@117 60 QColor branchColour = colourSet->getColourFor(m_changeset->branch());
Chris@117 61 QColor userColour = colourSet->getColourFor(m_changeset->author());
Chris@117 62
Chris@117 63 QFont f(m_font);
Chris@117 64
Chris@117 65 QTransform t = paint->worldTransform();
Chris@117 66 float scale = std::min(t.m11(), t.m22());
Chris@117 67 if (scale > 1.0) {
Chris@117 68 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@117 69 if (ps < 8) ps = 8;
Chris@117 70 f.setPixelSize(ps);
Chris@117 71 }
Chris@117 72
Chris@117 73 if (scale < 0.1) {
Chris@117 74 paint->setPen(QPen(branchColour, 0));
Chris@117 75 } else {
Chris@117 76 paint->setPen(QPen(branchColour, 2));
Chris@117 77 }
Chris@117 78
Chris@117 79 paint->setFont(f);
Chris@117 80 QFontMetrics fm(f);
Chris@117 81 int fh = fm.height();
Chris@117 82
Chris@117 83 int width = 350;
Chris@118 84 m_doc->setTextWidth(width);
Chris@118 85 int height = m_doc->size().height();
Chris@117 86
Chris@117 87 QRectF r(0.5, 0.5, width - 1, height - 1);
Chris@117 88 paint->setBrush(Qt::white);
Chris@117 89 paint->drawRect(r);
Chris@117 90
Chris@117 91 if (scale < 0.1) {
Chris@117 92 paint->restore();
Chris@117 93 return;
Chris@117 94 }
Chris@118 95
Chris@124 96 // little triangle connecting to its "owning" changeset item
Chris@124 97 paint->setBrush(branchColour);
Chris@124 98 QVector<QPointF> pts;
Chris@124 99 pts.push_back(QPointF(0, height/3 - 5));
Chris@124 100 pts.push_back(QPointF(0, height/3 + 5));
Chris@124 101 pts.push_back(QPointF(-10, height/3));
Chris@124 102 pts.push_back(QPointF(0, height/3 - 5));
Chris@124 103 paint->drawPolygon(QPolygonF(pts));
Chris@124 104
Chris@124 105 /*
Chris@118 106 paint->setBrush(branchColour);
Chris@118 107 QVector<QPointF> pts;
Chris@118 108 pts.push_back(QPointF(width/2 - 5, 0));
Chris@118 109 pts.push_back(QPointF(width/2 + 5, 0));
Chris@118 110 pts.push_back(QPointF(width/2, -10));
Chris@118 111 pts.push_back(QPointF(width/2 - 5, 0));
Chris@118 112 paint->drawPolygon(QPolygonF(pts));
Chris@124 113 */
Chris@118 114 m_doc->drawContents(paint, r);
Chris@118 115
Chris@117 116 paint->restore();
Chris@117 117 }
Chris@118 118
Chris@118 119 void
Chris@118 120 ChangesetDetailItem::makeDocument()
Chris@118 121 {
Chris@118 122 delete m_doc;
Chris@118 123
Chris@118 124 QString description;
Chris@118 125 QString rowTemplate = "<tr><td><b>%1</b></td><td>%2</td></tr>";
Chris@118 126
Chris@118 127 description = "<qt><table border=0>";
Chris@118 128
Chris@118 129 QString comment = m_changeset->comment().trimmed();
Chris@118 130 comment = comment.replace(QRegExp("^\""), "");
Chris@118 131 comment = comment.replace(QRegExp("\"$"), "");
Chris@118 132 comment = comment.replace("\\\"", "\"");
Chris@118 133 comment = xmlEncode(comment);
Chris@118 134 comment = comment.replace("\\n", "<br>");
Chris@118 135
Chris@118 136 QStringList propNames, propTexts;
Chris@118 137
Chris@118 138 propNames << "id"
Chris@118 139 << "author"
Chris@118 140 << "datetime"
Chris@118 141 << "branch"
Chris@118 142 << "tag"
Chris@118 143 << "comment";
Chris@118 144
Chris@118 145 propTexts << QObject::tr("Identifier")
Chris@118 146 << QObject::tr("Author")
Chris@118 147 << QObject::tr("Date")
Chris@118 148 << QObject::tr("Branch")
Chris@118 149 << QObject::tr("Tag")
Chris@118 150 << QObject::tr("Comment");
Chris@118 151
Chris@118 152 for (int i = 0; i < propNames.size(); ++i) {
Chris@118 153 QString prop = propNames[i];
Chris@118 154 QString value;
Chris@118 155 if (prop == "comment") value = comment;
Chris@118 156 else {
Chris@118 157 value = xmlEncode(m_changeset->property
Chris@118 158 (prop.toLocal8Bit().data()).toString());
Chris@118 159 }
Chris@118 160 if (value != "") {
Chris@118 161 description += rowTemplate
Chris@118 162 .arg(xmlEncode(propTexts[i]))
Chris@118 163 .arg(value);
Chris@118 164 }
Chris@118 165 }
Chris@118 166
Chris@118 167 description += "</table></qt>";
Chris@118 168
Chris@118 169 DEBUG << "ChangesetDetailItem: description = " << description << endl;
Chris@118 170
Chris@118 171 m_doc = new QTextDocument;
Chris@118 172 m_doc->setHtml(description);
Chris@118 173 }
Chris@118 174