comparison src/uncommitteditem.cpp @ 370:b9c153e00e84

Move source files to src/
author Chris Cannam
date Thu, 24 Mar 2011 10:27:51 +0000
parents uncommitteditem.cpp@4811eb34e819
children 7ef46fb73b48
comparison
equal deleted inserted replaced
369:19cce6d2c470 370:b9c153e00e84
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 EasyMercurial
5
6 Based on HgExplorer by Jari Korhonen
7 Copyright (c) 2010 Jari Korhonen
8 Copyright (c) 2011 Chris Cannam
9 Copyright (c) 2011 Queen Mary, University of London
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information.
16 */
17
18 #include "uncommitteditem.h"
19 #include "colourset.h"
20 #include "debug.h"
21 #include "textabbrev.h"
22
23 #include <QPainter>
24 #include <QGraphicsScene>
25 #include <QGraphicsSceneMouseEvent>
26 #include <QMenu>
27 #include <QAction>
28 #include <QLabel>
29 #include <QWidgetAction>
30
31 UncommittedItem::UncommittedItem() :
32 m_showBranch(false), m_isNewBranch(false),
33 m_column(0), m_row(0), m_wide(false)
34 {
35 m_font = QFont();
36 m_font.setPixelSize(11);
37 m_font.setBold(false);
38 m_font.setItalic(false);
39 setCursor(Qt::ArrowCursor);
40 }
41
42 QRectF
43 UncommittedItem::boundingRect() const
44 {
45 //!!! this stuff is gross, refactor with changesetitem and connectionitem
46 int w = 100;
47 if (m_wide) w = 180;
48 return QRectF(-((w-50)/2 - 1), -30, w - 3, 79 + 40);
49 }
50
51 void
52 UncommittedItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
53 {
54 DEBUG << "UncommittedItem::mousePressEvent" << endl;
55 if (e->button() == Qt::RightButton) {
56 activateMenu();
57 }
58 }
59
60 void
61 UncommittedItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e)
62 {
63 DEBUG << "UncommittedItem::mouseDoubleClickEvent" << endl;
64 if (e->button() == Qt::LeftButton) {
65 emit showWork();
66 }
67 }
68
69 void
70 UncommittedItem::activateMenu()
71 {
72 QMenu *menu = new QMenu;
73 QLabel *label = new QLabel(tr("<qt><b>&nbsp;Uncommitted changes</b></qt>"));
74 QWidgetAction *wa = new QWidgetAction(menu);
75 wa->setDefaultWidget(label);
76 menu->addAction(wa);
77 menu->addSeparator();
78
79 QAction *dif = menu->addAction(tr("Diff"));
80 connect(dif, SIGNAL(triggered()), this, SIGNAL(diff()));
81 QAction *stat = menu->addAction(tr("Summarise changes"));
82 connect(stat, SIGNAL(triggered()), this, SIGNAL(showSummary()));
83
84 menu->addSeparator();
85
86 QAction *commit = menu->addAction(tr("Commit..."));
87 connect(commit, SIGNAL(triggered()), this, SIGNAL(commit()));
88 QAction *revert = menu->addAction(tr("Revert..."));
89 connect(revert, SIGNAL(triggered()), this, SIGNAL(revert()));
90
91 menu->addSeparator();
92
93 QAction *branch = menu->addAction(tr("Start new branch..."));
94 connect(branch, SIGNAL(triggered()), this, SIGNAL(newBranch()));
95 QAction *nobranch = menu->addAction(tr("Cancel new branch"));
96 nobranch->setEnabled(m_isNewBranch);
97 connect(nobranch, SIGNAL(triggered()), this, SIGNAL(noBranch()));
98
99 menu->exec(QCursor::pos());
100
101 ungrabMouse();
102 }
103
104 void
105 UncommittedItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option,
106 QWidget *w)
107 {
108 paint->save();
109
110 ColourSet *colourSet = ColourSet::instance();
111 QColor branchColour = colourSet->getColourFor(m_branch);
112
113 QFont f(m_font);
114
115 QTransform t = paint->worldTransform();
116 float scale = std::min(t.m11(), t.m22());
117 if (scale > 1.0) {
118 int ps = int((f.pixelSize() / scale) + 0.5);
119 if (ps < 8) ps = 8;
120 f.setPixelSize(ps);
121 }
122
123 if (scale < 0.1) {
124 paint->setPen(QPen(branchColour, 0, Qt::DashLine));
125 } else {
126 paint->setPen(QPen(branchColour, 2, Qt::DashLine));
127 }
128
129 paint->setFont(f);
130 QFontMetrics fm(f);
131 int fh = fm.height();
132
133 int width = 100;
134 if (m_wide) width = 180;
135 int x0 = -((width - 50) / 2 - 1);
136
137 int height = 49;
138 QRectF r(x0, 0, width - 3, height);
139 paint->setBrush(Qt::white);
140 paint->drawRect(r);
141
142 if (m_wide) {
143 QString label = tr("Uncommitted changes");
144 paint->drawText(-(fm.width(label) - 50)/2,
145 25 - fm.height()/2 + fm.ascent(),
146 label);
147 } else {
148 QString label = tr("Uncommitted");
149 paint->drawText(-(fm.width(label) - 50)/2,
150 25 - fm.height() + fm.ascent(),
151 label);
152 label = tr("changes");
153 paint->drawText(-(fm.width(label) - 50)/2,
154 25 + fm.ascent(),
155 label);
156 }
157
158 if (m_showBranch && m_branch != "") {
159 // write branch name
160 f.setBold(true);
161 paint->setFont(f);
162 int wid = width - 3;
163 QString b = TextAbbrev::abbreviate(m_branch, QFontMetrics(f), wid);
164 paint->drawText(x0, -fh + fm.ascent() - 4, b);
165 f.setBold(false);
166 }
167
168 paint->restore();
169 return;
170 }