annotate uncommitteditem.cpp @ 262:a7843b03b4cf easyhg_v0.3.1

Merge
author Chris Cannam
date Wed, 12 Jan 2011 15:54:08 +0000
parents be483734bde5
children 4811eb34e819
rev   line source
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@244 8 Copyright (c) 2011 Chris Cannam
Chris@244 9 Copyright (c) 2011 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@153 32 m_showBranch(false), m_column(0), m_row(0), m_wide(false)
Chris@129 33 {
Chris@129 34 m_font = QFont();
Chris@129 35 m_font.setPixelSize(11);
Chris@129 36 m_font.setBold(false);
Chris@129 37 m_font.setItalic(false);
Chris@168 38 setCursor(Qt::ArrowCursor);
Chris@129 39 }
Chris@129 40
Chris@129 41 QRectF
Chris@129 42 UncommittedItem::boundingRect() const
Chris@129 43 {
Chris@131 44 //!!! this stuff is gross, refactor with changesetitem and connectionitem
Chris@129 45 int w = 100;
Chris@129 46 if (m_wide) w = 180;
Chris@131 47 return QRectF(-((w-50)/2 - 1), -30, w - 3, 79 + 40);
Chris@129 48 }
Chris@129 49
Chris@129 50 void
Chris@141 51 UncommittedItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
Chris@141 52 {
Chris@141 53 DEBUG << "UncommittedItem::mousePressEvent" << endl;
Chris@141 54 if (e->button() == Qt::RightButton) {
Chris@141 55 activateMenu();
Chris@141 56 }
Chris@141 57 }
Chris@141 58
Chris@141 59 void
Chris@153 60 UncommittedItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e)
Chris@153 61 {
Chris@153 62 DEBUG << "UncommittedItem::mouseDoubleClickEvent" << endl;
Chris@153 63 if (e->button() == Qt::LeftButton) {
Chris@153 64 emit showWork();
Chris@153 65 }
Chris@153 66 }
Chris@153 67
Chris@153 68 void
Chris@141 69 UncommittedItem::activateMenu()
Chris@141 70 {
Chris@141 71 QMenu *menu = new QMenu;
Chris@165 72 QLabel *label = new QLabel(tr("<qt><b>&nbsp;Uncommitted changes</b></qt>"));
Chris@141 73 QWidgetAction *wa = new QWidgetAction(menu);
Chris@141 74 wa->setDefaultWidget(label);
Chris@141 75 menu->addAction(wa);
Chris@141 76 menu->addSeparator();
Chris@141 77
Chris@153 78 QAction *dif = menu->addAction(tr("Diff"));
Chris@153 79 connect(dif, SIGNAL(triggered()), this, SIGNAL(diff()));
Chris@169 80 QAction *stat = menu->addAction(tr("Summarise changes"));
Chris@168 81 connect(stat, SIGNAL(triggered()), this, SIGNAL(showSummary()));
Chris@153 82
Chris@153 83 menu->addSeparator();
Chris@153 84
Chris@141 85 QAction *commit = menu->addAction(tr("Commit..."));
Chris@141 86 connect(commit, SIGNAL(triggered()), this, SIGNAL(commit()));
Chris@141 87 QAction *revert = menu->addAction(tr("Revert..."));
Chris@141 88 connect(revert, SIGNAL(triggered()), this, SIGNAL(revert()));
Chris@141 89
Chris@141 90 menu->exec(QCursor::pos());
Chris@141 91
Chris@141 92 ungrabMouse();
Chris@141 93 }
Chris@141 94
Chris@141 95 void
Chris@129 96 UncommittedItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option,
Chris@129 97 QWidget *w)
Chris@129 98 {
Chris@129 99 paint->save();
Chris@129 100
Chris@129 101 ColourSet *colourSet = ColourSet::instance();
Chris@129 102 QColor branchColour = colourSet->getColourFor(m_branch);
Chris@129 103
Chris@129 104 QFont f(m_font);
Chris@129 105
Chris@129 106 QTransform t = paint->worldTransform();
Chris@129 107 float scale = std::min(t.m11(), t.m22());
Chris@129 108 if (scale > 1.0) {
Chris@129 109 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@129 110 if (ps < 8) ps = 8;
Chris@129 111 f.setPixelSize(ps);
Chris@129 112 }
Chris@129 113
Chris@129 114 if (scale < 0.1) {
Chris@129 115 paint->setPen(QPen(branchColour, 0, Qt::DashLine));
Chris@129 116 } else {
Chris@129 117 paint->setPen(QPen(branchColour, 2, Qt::DashLine));
Chris@129 118 }
Chris@129 119
Chris@129 120 paint->setFont(f);
Chris@129 121 QFontMetrics fm(f);
Chris@129 122 int fh = fm.height();
Chris@129 123
Chris@129 124 int width = 100;
Chris@129 125 if (m_wide) width = 180;
Chris@129 126 int x0 = -((width - 50) / 2 - 1);
Chris@129 127
Chris@129 128 int height = 49;
Chris@129 129 QRectF r(x0, 0, width - 3, height);
Chris@250 130 paint->setBrush(Qt::white);
Chris@129 131 paint->drawRect(r);
Chris@129 132
Chris@145 133 if (m_wide) {
Chris@145 134 QString label = tr("Uncommitted changes");
Chris@145 135 paint->drawText(-(fm.width(label) - 50)/2,
Chris@145 136 25 - fm.height()/2 + fm.ascent(),
Chris@145 137 label);
Chris@145 138 } else {
Chris@145 139 QString label = tr("Uncommitted");
Chris@145 140 paint->drawText(-(fm.width(label) - 50)/2,
Chris@145 141 25 - fm.height() + fm.ascent(),
Chris@145 142 label);
Chris@145 143 label = tr("changes");
Chris@145 144 paint->drawText(-(fm.width(label) - 50)/2,
Chris@145 145 25 + fm.ascent(),
Chris@145 146 label);
Chris@145 147 }
Chris@129 148
Chris@153 149 if (m_showBranch && m_branch != "") {
Chris@153 150 // write branch name
Chris@153 151 f.setBold(true);
Chris@153 152 paint->setFont(f);
Chris@153 153 int wid = width - 3;
Chris@153 154 QString b = TextAbbrev::abbreviate(m_branch, QFontMetrics(f), wid);
Chris@153 155 paint->drawText(x0, -fh + fm.ascent() - 4, b);
Chris@153 156 f.setBold(false);
Chris@153 157 }
Chris@153 158
Chris@129 159 paint->restore();
Chris@129 160 return;
Chris@129 161 }