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