Chris@129: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@129: Chris@129: /* Chris@129: EasyMercurial Chris@129: Chris@129: Based on HgExplorer by Jari Korhonen Chris@129: Copyright (c) 2010 Jari Korhonen Chris@244: Copyright (c) 2011 Chris Cannam Chris@244: Copyright (c) 2011 Queen Mary, University of London Chris@129: Chris@129: This program is free software; you can redistribute it and/or Chris@129: modify it under the terms of the GNU General Public License as Chris@129: published by the Free Software Foundation; either version 2 of the Chris@129: License, or (at your option) any later version. See the file Chris@129: COPYING included with this distribution for more information. Chris@129: */ Chris@129: Chris@129: #include "uncommitteditem.h" Chris@129: #include "colourset.h" Chris@129: #include "debug.h" Chris@153: #include "textabbrev.h" Chris@129: Chris@129: #include Chris@129: #include Chris@141: #include Chris@141: #include Chris@141: #include Chris@141: #include Chris@141: #include Chris@129: Chris@129: UncommittedItem::UncommittedItem() : Chris@311: m_showBranch(false), m_isNewBranch(false), Chris@311: m_column(0), m_row(0), m_wide(false) Chris@129: { Chris@129: m_font = QFont(); Chris@129: m_font.setPixelSize(11); Chris@129: m_font.setBold(false); Chris@129: m_font.setItalic(false); Chris@168: setCursor(Qt::ArrowCursor); Chris@129: } Chris@129: Chris@129: QRectF Chris@129: UncommittedItem::boundingRect() const Chris@129: { Chris@131: //!!! this stuff is gross, refactor with changesetitem and connectionitem Chris@129: int w = 100; Chris@129: if (m_wide) w = 180; Chris@131: return QRectF(-((w-50)/2 - 1), -30, w - 3, 79 + 40); Chris@129: } Chris@129: Chris@129: void Chris@141: UncommittedItem::mousePressEvent(QGraphicsSceneMouseEvent *e) Chris@141: { Chris@141: DEBUG << "UncommittedItem::mousePressEvent" << endl; Chris@141: if (e->button() == Qt::RightButton) { Chris@141: activateMenu(); Chris@141: } Chris@141: } Chris@141: Chris@141: void Chris@153: UncommittedItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e) Chris@153: { Chris@153: DEBUG << "UncommittedItem::mouseDoubleClickEvent" << endl; Chris@153: if (e->button() == Qt::LeftButton) { Chris@153: emit showWork(); Chris@153: } Chris@153: } Chris@153: Chris@153: void Chris@141: UncommittedItem::activateMenu() Chris@141: { Chris@141: QMenu *menu = new QMenu; Chris@165: QLabel *label = new QLabel(tr(" Uncommitted changes")); Chris@141: QWidgetAction *wa = new QWidgetAction(menu); Chris@141: wa->setDefaultWidget(label); Chris@141: menu->addAction(wa); Chris@141: menu->addSeparator(); Chris@141: Chris@153: QAction *dif = menu->addAction(tr("Diff")); Chris@153: connect(dif, SIGNAL(triggered()), this, SIGNAL(diff())); Chris@169: QAction *stat = menu->addAction(tr("Summarise changes")); Chris@168: connect(stat, SIGNAL(triggered()), this, SIGNAL(showSummary())); Chris@153: Chris@153: menu->addSeparator(); Chris@153: Chris@141: QAction *commit = menu->addAction(tr("Commit...")); Chris@141: connect(commit, SIGNAL(triggered()), this, SIGNAL(commit())); Chris@141: QAction *revert = menu->addAction(tr("Revert...")); Chris@141: connect(revert, SIGNAL(triggered()), this, SIGNAL(revert())); Chris@141: Chris@311: menu->addSeparator(); Chris@311: Chris@311: QAction *branch = menu->addAction(tr("Start new branch...")); Chris@311: connect(branch, SIGNAL(triggered()), this, SIGNAL(newBranch())); Chris@311: QAction *nobranch = menu->addAction(tr("Cancel new branch")); Chris@311: nobranch->setEnabled(m_isNewBranch); Chris@311: connect(nobranch, SIGNAL(triggered()), this, SIGNAL(noBranch())); Chris@311: Chris@141: menu->exec(QCursor::pos()); Chris@141: Chris@141: ungrabMouse(); Chris@141: } Chris@141: Chris@141: void Chris@129: UncommittedItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *option, Chris@129: QWidget *w) Chris@129: { Chris@129: paint->save(); Chris@129: Chris@129: ColourSet *colourSet = ColourSet::instance(); Chris@129: QColor branchColour = colourSet->getColourFor(m_branch); Chris@129: Chris@129: QFont f(m_font); Chris@129: Chris@129: QTransform t = paint->worldTransform(); Chris@129: float scale = std::min(t.m11(), t.m22()); Chris@129: if (scale > 1.0) { Chris@129: int ps = int((f.pixelSize() / scale) + 0.5); Chris@129: if (ps < 8) ps = 8; Chris@129: f.setPixelSize(ps); Chris@129: } Chris@129: Chris@129: if (scale < 0.1) { Chris@129: paint->setPen(QPen(branchColour, 0, Qt::DashLine)); Chris@129: } else { Chris@129: paint->setPen(QPen(branchColour, 2, Qt::DashLine)); Chris@129: } Chris@129: Chris@129: paint->setFont(f); Chris@129: QFontMetrics fm(f); Chris@129: int fh = fm.height(); Chris@129: Chris@129: int width = 100; Chris@129: if (m_wide) width = 180; Chris@129: int x0 = -((width - 50) / 2 - 1); Chris@129: Chris@129: int height = 49; Chris@129: QRectF r(x0, 0, width - 3, height); Chris@250: paint->setBrush(Qt::white); Chris@129: paint->drawRect(r); Chris@129: Chris@145: if (m_wide) { Chris@145: QString label = tr("Uncommitted changes"); Chris@145: paint->drawText(-(fm.width(label) - 50)/2, Chris@145: 25 - fm.height()/2 + fm.ascent(), Chris@145: label); Chris@145: } else { Chris@145: QString label = tr("Uncommitted"); Chris@145: paint->drawText(-(fm.width(label) - 50)/2, Chris@145: 25 - fm.height() + fm.ascent(), Chris@145: label); Chris@145: label = tr("changes"); Chris@145: paint->drawText(-(fm.width(label) - 50)/2, Chris@145: 25 + fm.ascent(), Chris@145: label); Chris@145: } Chris@129: Chris@153: if (m_showBranch && m_branch != "") { Chris@153: // write branch name Chris@153: f.setBold(true); Chris@153: paint->setFont(f); Chris@153: int wid = width - 3; Chris@153: QString b = TextAbbrev::abbreviate(m_branch, QFontMetrics(f), wid); Chris@153: paint->drawText(x0, -fh + fm.ascent() - 4, b); Chris@153: f.setBold(false); Chris@153: } Chris@153: Chris@129: paint->restore(); Chris@129: return; Chris@129: }