annotate uncommitteditem.cpp @ 148:2fef6b0dfbe8

* Some implementation of right-button menu actions on changeset item
author Chris Cannam
date Wed, 01 Dec 2010 21:02:53 +0000
parents 644bd31e8301
children 70fe12873106
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@129 8 Copyright (c) 2010 Chris Cannam
Chris@129 9 Copyright (c) 2010 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@129 21
Chris@129 22 #include <QPainter>
Chris@129 23 #include <QGraphicsScene>
Chris@141 24 #include <QGraphicsSceneMouseEvent>
Chris@141 25 #include <QMenu>
Chris@141 26 #include <QAction>
Chris@141 27 #include <QLabel>
Chris@141 28 #include <QWidgetAction>
Chris@129 29
Chris@129 30 UncommittedItem::UncommittedItem() :
Chris@129 31 m_column(0), m_row(0), m_wide(false)
Chris@129 32 {
Chris@129 33 m_font = QFont();
Chris@129 34 m_font.setPixelSize(11);
Chris@129 35 m_font.setBold(false);
Chris@129 36 m_font.setItalic(false);
Chris@129 37 }
Chris@129 38
Chris@129 39 QRectF
Chris@129 40 UncommittedItem::boundingRect() const
Chris@129 41 {
Chris@131 42 //!!! this stuff is gross, refactor with changesetitem and connectionitem
Chris@129 43 int w = 100;
Chris@129 44 if (m_wide) w = 180;
Chris@131 45 return QRectF(-((w-50)/2 - 1), -30, w - 3, 79 + 40);
Chris@129 46 }
Chris@129 47
Chris@129 48 void
Chris@141 49 UncommittedItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
Chris@141 50 {
Chris@141 51 DEBUG << "UncommittedItem::mousePressEvent" << endl;
Chris@141 52 if (e->button() == Qt::RightButton) {
Chris@141 53 activateMenu();
Chris@141 54 }
Chris@141 55 }
Chris@141 56
Chris@141 57 void
Chris@141 58 UncommittedItem::activateMenu()
Chris@141 59 {
Chris@141 60 QMenu *menu = new QMenu;
Chris@141 61 QLabel *label = new QLabel(tr("<qt><b>Uncommitted changes</b></qt>"));
Chris@141 62 QWidgetAction *wa = new QWidgetAction(menu);
Chris@141 63 wa->setDefaultWidget(label);
Chris@141 64 menu->addAction(wa);
Chris@141 65 menu->addSeparator();
Chris@141 66
Chris@141 67 QAction *commit = menu->addAction(tr("Commit..."));
Chris@141 68 connect(commit, SIGNAL(triggered()), this, SIGNAL(commit()));
Chris@141 69 QAction *revert = menu->addAction(tr("Revert..."));
Chris@141 70 connect(revert, SIGNAL(triggered()), this, SIGNAL(revert()));
Chris@141 71 QAction *dif = menu->addAction(tr("Diff"));
Chris@141 72 connect(dif, SIGNAL(triggered()), this, SIGNAL(diff()));
Chris@141 73
Chris@141 74 menu->exec(QCursor::pos());
Chris@141 75
Chris@141 76 ungrabMouse();
Chris@141 77 }
Chris@141 78
Chris@141 79 void
Chris@129 80 UncommittedItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option,
Chris@129 81 QWidget *w)
Chris@129 82 {
Chris@129 83 paint->save();
Chris@129 84
Chris@129 85 ColourSet *colourSet = ColourSet::instance();
Chris@129 86 QColor branchColour = colourSet->getColourFor(m_branch);
Chris@129 87
Chris@129 88 QFont f(m_font);
Chris@129 89
Chris@129 90 QTransform t = paint->worldTransform();
Chris@129 91 float scale = std::min(t.m11(), t.m22());
Chris@129 92 if (scale > 1.0) {
Chris@129 93 int ps = int((f.pixelSize() / scale) + 0.5);
Chris@129 94 if (ps < 8) ps = 8;
Chris@129 95 f.setPixelSize(ps);
Chris@129 96 }
Chris@129 97
Chris@129 98 if (scale < 0.1) {
Chris@129 99 paint->setPen(QPen(branchColour, 0, Qt::DashLine));
Chris@129 100 } else {
Chris@129 101 paint->setPen(QPen(branchColour, 2, Qt::DashLine));
Chris@129 102 }
Chris@129 103
Chris@129 104 paint->setFont(f);
Chris@129 105 QFontMetrics fm(f);
Chris@129 106 int fh = fm.height();
Chris@129 107
Chris@129 108 int width = 100;
Chris@129 109 if (m_wide) width = 180;
Chris@129 110 int x0 = -((width - 50) / 2 - 1);
Chris@129 111
Chris@129 112 int height = 49;
Chris@129 113 QRectF r(x0, 0, width - 3, height);
Chris@129 114 paint->drawRect(r);
Chris@129 115
Chris@145 116 if (m_wide) {
Chris@145 117 QString label = tr("Uncommitted changes");
Chris@145 118 paint->drawText(-(fm.width(label) - 50)/2,
Chris@145 119 25 - fm.height()/2 + fm.ascent(),
Chris@145 120 label);
Chris@145 121 } else {
Chris@145 122 QString label = tr("Uncommitted");
Chris@145 123 paint->drawText(-(fm.width(label) - 50)/2,
Chris@145 124 25 - fm.height() + fm.ascent(),
Chris@145 125 label);
Chris@145 126 label = tr("changes");
Chris@145 127 paint->drawText(-(fm.width(label) - 50)/2,
Chris@145 128 25 + fm.ascent(),
Chris@145 129 label);
Chris@145 130 }
Chris@129 131
Chris@129 132 paint->restore();
Chris@129 133 return;
Chris@129 134 }