Chris@129
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@129
|
2
|
Chris@129
|
3 /*
|
Chris@129
|
4 EasyMercurial
|
Chris@129
|
5
|
Chris@129
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@129
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@644
|
8 Copyright (c) 2013 Chris Cannam
|
Chris@644
|
9 Copyright (c) 2013 Queen Mary, University of London
|
Chris@129
|
10
|
Chris@129
|
11 This program is free software; you can redistribute it and/or
|
Chris@129
|
12 modify it under the terms of the GNU General Public License as
|
Chris@129
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@129
|
14 License, or (at your option) any later version. See the file
|
Chris@129
|
15 COPYING included with this distribution for more information.
|
Chris@129
|
16 */
|
Chris@129
|
17
|
Chris@129
|
18 #include "uncommitteditem.h"
|
Chris@129
|
19 #include "colourset.h"
|
Chris@129
|
20 #include "debug.h"
|
Chris@153
|
21 #include "textabbrev.h"
|
Chris@129
|
22
|
Chris@129
|
23 #include <QPainter>
|
Chris@129
|
24 #include <QGraphicsScene>
|
Chris@141
|
25 #include <QGraphicsSceneMouseEvent>
|
Chris@141
|
26 #include <QMenu>
|
Chris@141
|
27 #include <QAction>
|
Chris@141
|
28 #include <QLabel>
|
Chris@141
|
29 #include <QWidgetAction>
|
Chris@129
|
30
|
Chris@129
|
31 UncommittedItem::UncommittedItem() :
|
Chris@399
|
32 m_showBranch(false), m_isNewBranch(false), m_isMerge(false),
|
Chris@311
|
33 m_column(0), m_row(0), m_wide(false)
|
Chris@129
|
34 {
|
Chris@129
|
35 m_font = QFont();
|
Chris@129
|
36 m_font.setPixelSize(11);
|
Chris@129
|
37 m_font.setBold(false);
|
Chris@129
|
38 m_font.setItalic(false);
|
Chris@168
|
39 setCursor(Qt::ArrowCursor);
|
Chris@129
|
40 }
|
Chris@129
|
41
|
Chris@129
|
42 QRectF
|
Chris@129
|
43 UncommittedItem::boundingRect() const
|
Chris@129
|
44 {
|
Chris@131
|
45 //!!! this stuff is gross, refactor with changesetitem and connectionitem
|
Chris@129
|
46 int w = 100;
|
Chris@129
|
47 if (m_wide) w = 180;
|
Chris@131
|
48 return QRectF(-((w-50)/2 - 1), -30, w - 3, 79 + 40);
|
Chris@129
|
49 }
|
Chris@129
|
50
|
Chris@129
|
51 void
|
Chris@141
|
52 UncommittedItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
|
Chris@141
|
53 {
|
Chris@141
|
54 DEBUG << "UncommittedItem::mousePressEvent" << endl;
|
Chris@141
|
55 if (e->button() == Qt::RightButton) {
|
Chris@141
|
56 activateMenu();
|
Chris@141
|
57 }
|
Chris@141
|
58 }
|
Chris@141
|
59
|
Chris@141
|
60 void
|
Chris@153
|
61 UncommittedItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e)
|
Chris@153
|
62 {
|
Chris@153
|
63 DEBUG << "UncommittedItem::mouseDoubleClickEvent" << endl;
|
Chris@153
|
64 if (e->button() == Qt::LeftButton) {
|
Chris@153
|
65 emit showWork();
|
Chris@153
|
66 }
|
Chris@153
|
67 }
|
Chris@153
|
68
|
Chris@153
|
69 void
|
Chris@141
|
70 UncommittedItem::activateMenu()
|
Chris@141
|
71 {
|
Chris@141
|
72 QMenu *menu = new QMenu;
|
Chris@399
|
73 QLabel *label = new QLabel
|
Chris@399
|
74 (m_isMerge ?
|
Chris@399
|
75 tr("<qt><b> Uncommitted merge</b></qt>") :
|
Chris@399
|
76 tr("<qt><b> Uncommitted changes</b></qt>"));
|
Chris@141
|
77 QWidgetAction *wa = new QWidgetAction(menu);
|
Chris@141
|
78 wa->setDefaultWidget(label);
|
Chris@141
|
79 menu->addAction(wa);
|
Chris@141
|
80 menu->addSeparator();
|
Chris@141
|
81
|
Chris@153
|
82 QAction *dif = menu->addAction(tr("Diff"));
|
Chris@153
|
83 connect(dif, SIGNAL(triggered()), this, SIGNAL(diff()));
|
Chris@169
|
84 QAction *stat = menu->addAction(tr("Summarise changes"));
|
Chris@168
|
85 connect(stat, SIGNAL(triggered()), this, SIGNAL(showSummary()));
|
Chris@153
|
86
|
Chris@153
|
87 menu->addSeparator();
|
Chris@153
|
88
|
Chris@141
|
89 QAction *commit = menu->addAction(tr("Commit..."));
|
Chris@141
|
90 connect(commit, SIGNAL(triggered()), this, SIGNAL(commit()));
|
Chris@141
|
91 QAction *revert = menu->addAction(tr("Revert..."));
|
Chris@141
|
92 connect(revert, SIGNAL(triggered()), this, SIGNAL(revert()));
|
Chris@141
|
93
|
Chris@311
|
94 menu->addSeparator();
|
Chris@311
|
95
|
Chris@311
|
96 QAction *branch = menu->addAction(tr("Start new branch..."));
|
Chris@311
|
97 connect(branch, SIGNAL(triggered()), this, SIGNAL(newBranch()));
|
Chris@311
|
98 QAction *nobranch = menu->addAction(tr("Cancel new branch"));
|
Chris@311
|
99 nobranch->setEnabled(m_isNewBranch);
|
Chris@311
|
100 connect(nobranch, SIGNAL(triggered()), this, SIGNAL(noBranch()));
|
Chris@311
|
101
|
Chris@141
|
102 menu->exec(QCursor::pos());
|
Chris@141
|
103
|
Chris@141
|
104 ungrabMouse();
|
Chris@141
|
105 }
|
Chris@141
|
106
|
Chris@141
|
107 void
|
Chris@399
|
108 UncommittedItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *, QWidget *)
|
Chris@399
|
109 {
|
Chris@399
|
110 if (isMerge()) {
|
Chris@399
|
111 paintMerge(paint);
|
Chris@399
|
112 } else {
|
Chris@399
|
113 paintNormal(paint);
|
Chris@399
|
114 }
|
Chris@399
|
115 }
|
Chris@399
|
116
|
Chris@399
|
117 void
|
Chris@399
|
118 UncommittedItem::paintNormal(QPainter *paint)
|
Chris@129
|
119 {
|
Chris@129
|
120 paint->save();
|
Chris@129
|
121
|
Chris@129
|
122 ColourSet *colourSet = ColourSet::instance();
|
Chris@129
|
123 QColor branchColour = colourSet->getColourFor(m_branch);
|
Chris@129
|
124
|
Chris@129
|
125 QFont f(m_font);
|
Chris@129
|
126
|
Chris@129
|
127 QTransform t = paint->worldTransform();
|
Chris@129
|
128 float scale = std::min(t.m11(), t.m22());
|
Chris@129
|
129 if (scale > 1.0) {
|
Chris@129
|
130 int ps = int((f.pixelSize() / scale) + 0.5);
|
Chris@129
|
131 if (ps < 8) ps = 8;
|
Chris@129
|
132 f.setPixelSize(ps);
|
Chris@682
|
133 #ifdef Q_OS_WIN32
|
Chris@682
|
134 f.setHintingPreference(QFont::PreferVerticalHinting);
|
Chris@682
|
135 #endif
|
Chris@129
|
136 }
|
Chris@129
|
137
|
Chris@129
|
138 if (scale < 0.1) {
|
Chris@129
|
139 paint->setPen(QPen(branchColour, 0, Qt::DashLine));
|
Chris@129
|
140 } else {
|
Chris@129
|
141 paint->setPen(QPen(branchColour, 2, Qt::DashLine));
|
Chris@129
|
142 }
|
Chris@129
|
143
|
Chris@129
|
144 paint->setFont(f);
|
Chris@129
|
145 QFontMetrics fm(f);
|
Chris@129
|
146 int fh = fm.height();
|
Chris@129
|
147
|
Chris@129
|
148 int width = 100;
|
Chris@129
|
149 if (m_wide) width = 180;
|
Chris@129
|
150 int x0 = -((width - 50) / 2 - 1);
|
Chris@129
|
151
|
Chris@129
|
152 int height = 49;
|
Chris@129
|
153 QRectF r(x0, 0, width - 3, height);
|
Chris@250
|
154 paint->setBrush(Qt::white);
|
Chris@386
|
155 paint->drawRoundedRect(r, 7, 7);
|
Chris@129
|
156
|
Chris@145
|
157 if (m_wide) {
|
Chris@145
|
158 QString label = tr("Uncommitted changes");
|
Chris@145
|
159 paint->drawText(-(fm.width(label) - 50)/2,
|
Chris@145
|
160 25 - fm.height()/2 + fm.ascent(),
|
Chris@145
|
161 label);
|
Chris@145
|
162 } else {
|
Chris@145
|
163 QString label = tr("Uncommitted");
|
Chris@145
|
164 paint->drawText(-(fm.width(label) - 50)/2,
|
Chris@145
|
165 25 - fm.height() + fm.ascent(),
|
Chris@145
|
166 label);
|
Chris@145
|
167 label = tr("changes");
|
Chris@145
|
168 paint->drawText(-(fm.width(label) - 50)/2,
|
Chris@145
|
169 25 + fm.ascent(),
|
Chris@145
|
170 label);
|
Chris@145
|
171 }
|
Chris@129
|
172
|
Chris@153
|
173 if (m_showBranch && m_branch != "") {
|
Chris@153
|
174 // write branch name
|
Chris@153
|
175 f.setBold(true);
|
Chris@153
|
176 paint->setFont(f);
|
Chris@153
|
177 int wid = width - 3;
|
Chris@153
|
178 QString b = TextAbbrev::abbreviate(m_branch, QFontMetrics(f), wid);
|
Chris@153
|
179 paint->drawText(x0, -fh + fm.ascent() - 4, b);
|
Chris@153
|
180 f.setBold(false);
|
Chris@153
|
181 }
|
Chris@153
|
182
|
Chris@129
|
183 paint->restore();
|
Chris@129
|
184 return;
|
Chris@129
|
185 }
|
Chris@399
|
186
|
Chris@399
|
187 void
|
Chris@399
|
188 UncommittedItem::paintMerge(QPainter *paint)
|
Chris@399
|
189 {
|
Chris@399
|
190 paint->save();
|
Chris@399
|
191
|
Chris@399
|
192 ColourSet *colourSet = ColourSet::instance();
|
Chris@399
|
193 QColor branchColour = colourSet->getColourFor(m_branch);
|
Chris@399
|
194
|
Chris@399
|
195 QFont f(m_font);
|
Chris@399
|
196
|
Chris@399
|
197 QTransform t = paint->worldTransform();
|
Chris@399
|
198 float scale = std::min(t.m11(), t.m22());
|
Chris@399
|
199 if (scale > 1.0) {
|
Chris@399
|
200 int ps = int((f.pixelSize() / scale) + 0.5);
|
Chris@399
|
201 if (ps < 8) ps = 8;
|
Chris@399
|
202 f.setPixelSize(ps);
|
Chris@682
|
203 #ifdef Q_OS_WIN32
|
Chris@682
|
204 f.setHintingPreference(QFont::PreferVerticalHinting);
|
Chris@682
|
205 #endif
|
Chris@399
|
206 }
|
Chris@399
|
207
|
Chris@399
|
208 if (scale < 0.1) {
|
Chris@399
|
209 paint->setPen(QPen(branchColour, 0, Qt::DashLine));
|
Chris@399
|
210 } else {
|
Chris@399
|
211 paint->setPen(QPen(branchColour, 2, Qt::DashLine));
|
Chris@399
|
212 }
|
Chris@399
|
213
|
Chris@399
|
214 paint->setFont(f);
|
Chris@399
|
215 QFontMetrics fm(f);
|
Chris@399
|
216 int fh = fm.height();
|
Chris@399
|
217
|
Chris@399
|
218 int size = fh * 2;
|
Chris@399
|
219 int x0 = -size/2 + 25;
|
Chris@399
|
220
|
Chris@399
|
221 paint->setBrush(Qt::white);
|
Chris@399
|
222 paint->drawEllipse(QRectF(x0, fh, size, size));
|
Chris@399
|
223
|
Chris@399
|
224 if (m_wide) {
|
Chris@399
|
225 QString label = tr("Uncommitted merge");
|
Chris@399
|
226 paint->drawText(size/2 + 28,
|
Chris@399
|
227 25 - fm.height()/2 + fm.ascent(),
|
Chris@399
|
228 label);
|
Chris@399
|
229 } else {
|
Chris@399
|
230 QString label = tr("Uncommitted");
|
Chris@399
|
231 paint->drawText(size/2 + 28,
|
Chris@399
|
232 25 - fm.height() + fm.ascent(),
|
Chris@399
|
233 label);
|
Chris@399
|
234 label = tr("merge");
|
Chris@399
|
235 paint->drawText(size/2 + 28,
|
Chris@399
|
236 25 + fm.ascent(),
|
Chris@399
|
237 label);
|
Chris@399
|
238 }
|
Chris@399
|
239
|
Chris@399
|
240 if (m_showBranch && m_branch != "") {
|
Chris@399
|
241 // write branch name
|
Chris@399
|
242 f.setBold(true);
|
Chris@399
|
243 paint->setFont(f);
|
Chris@399
|
244 int wid = size * 3;
|
Chris@399
|
245 QString branch = TextAbbrev::abbreviate(m_branch, QFontMetrics(f), wid);
|
Chris@399
|
246 paint->drawText(-wid/2 + 25, fm.ascent() - 4, branch);
|
Chris@399
|
247 }
|
Chris@399
|
248
|
Chris@399
|
249 paint->restore();
|
Chris@399
|
250 return;
|
Chris@399
|
251 }
|