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