changeset 397:61bde1f0ff0a item_appearance_adjustments

Replace DateItems (background items for date shading) with a date range list in the scene and dedicated rendering for it in the graphics view: this way we can ensure the shading spans the full width and the dates are always visible
author Chris Cannam
date Wed, 25 May 2011 14:32:32 +0100
parents 1e73b5911631
children 4f3d96c1916f
files easyhg.pro src/changesetscene.cpp src/changesetscene.h src/changesetview.cpp src/changesetview.h src/dateitem.cpp src/dateitem.h src/grapher.cpp src/historywidget.cpp
diffstat 9 files changed, 188 insertions(+), 180 deletions(-) [+]
line wrap: on
line diff
--- a/easyhg.pro	Tue May 24 18:00:12 2011 +0100
+++ b/easyhg.pro	Wed May 25 14:32:32 2011 +0100
@@ -1,5 +1,5 @@
 
-CONFIG += release
+CONFIG += debug
 
 TEMPLATE = app
 TARGET = EasyMercurial
@@ -41,7 +41,6 @@
     src/panned.h \
     src/connectionitem.h \
     src/textabbrev.h \
-    src/dateitem.h \
     src/colourset.h \
     src/debug.h \
     src/recentfiles.h \
@@ -55,6 +54,7 @@
     src/hgaction.h \
     src/historywidget.h \
     src/changesetscene.h \
+    src/changesetview.h \
     src/incomingdialog.h \
     src/uncommitteditem.h \
     src/settingsdialog.h \
@@ -77,7 +77,6 @@
     src/panned.cpp \
     src/connectionitem.cpp \
     src/textabbrev.cpp \
-    src/dateitem.cpp \
     src/colourset.cpp \
     src/debug.cpp \
     src/recentfiles.cpp \
@@ -90,6 +89,7 @@
     src/confirmcommentdialog.cpp \
     src/historywidget.cpp \
     src/changesetscene.cpp \
+    src/changesetview.cpp \
     src/incomingdialog.cpp \
     src/uncommitteditem.cpp \
     src/settingsdialog.cpp \
--- a/src/changesetscene.cpp	Tue May 24 18:00:12 2011 +0100
+++ b/src/changesetscene.cpp	Wed May 25 14:32:32 2011 +0100
@@ -18,10 +18,16 @@
 #include "changesetscene.h"
 #include "changesetitem.h"
 #include "uncommitteditem.h"
-#include "dateitem.h"
+#include "debug.h"
+
+#include <QPainter>
+
 
 ChangesetScene::ChangesetScene()
-    : QGraphicsScene(), m_detailShown(0)
+    // Supply a non-NULL but trivial scene rect to inhibit automatic
+    // updates from QGraphicsScene, because we will set the rect
+    // explicitly in itemAddCompleted
+    : QGraphicsScene(QRectF(0, 0, 1, 1)), m_detailShown(0)
 {
 }
 
@@ -87,12 +93,30 @@
 }
 
 void
-ChangesetScene::addDateItem(DateItem *item)
+ChangesetScene::addDateRange(QString label, int minrow, int nrows, bool even)
 {
-    addItem(item);
+    DateRange dr;
+    dr.label = label;
+    dr.minrow = minrow;
+    dr.nrows = nrows;
+    dr.even = even;
+    m_dateRanges[minrow] = dr;
+}
 
-    connect(item, SIGNAL(clicked()),
-            this, SLOT(dateItemClicked()));
+void
+ChangesetScene::itemAddCompleted()
+{
+    QRectF r = itemsBoundingRect();
+    float minwidth = 300; //!!!
+    DEBUG << "ChangesetScene::itemAddCompleted: minwidth = " << minwidth
+          << ", r = " << r << endl;
+    if (r.width() < minwidth) {
+        float edgediff = (minwidth - r.width()) / 2;
+        r.setLeft(r.left() - edgediff);
+        r.setRight(r.right() + edgediff);
+    }
+    DEBUG << "ChangesetScene::itemAddCompleted: r now is " << r << endl;
+    setSceneRect(r);
 }
 
 void
@@ -114,12 +138,11 @@
 }
 
 void
-ChangesetScene::dateItemClicked()
+ChangesetScene::drawBackground(QPainter *paint, const QRectF &rect)
 {
-    if (m_detailShown) {
-        m_detailShown->hideDetail();
-    }
+    QGraphicsScene::drawBackground(paint, rect);
 }
+        
 
 ChangesetItem *
 ChangesetScene::getItemById(QString id)
--- a/src/changesetscene.h	Tue May 24 18:00:12 2011 +0100
+++ b/src/changesetscene.h	Wed May 25 14:32:32 2011 +0100
@@ -19,11 +19,11 @@
 #define CHANGESETSCENE_H
 
 #include <QGraphicsScene>
+#include <QMap>
 
 class ChangesetItem;
 class Changeset;
 class UncommittedItem;
-class DateItem;
 
 class ChangesetScene : public QGraphicsScene
 {
@@ -34,7 +34,20 @@
 
     void addChangesetItem(ChangesetItem *item);
     void addUncommittedItem(UncommittedItem *item);
-    void addDateItem(DateItem *item);
+
+    void addDateRange(QString label, int minrow, int nrows, bool even);
+
+    struct DateRange {
+        QString label;
+        int minrow;
+        int nrows;
+        bool even;
+    };
+
+    typedef QMap<int, DateRange> DateRanges; // key is minrow
+    DateRanges getDateRanges() const { return m_dateRanges; }
+
+    void itemAddCompleted(); // recalculate scene rect
 
     ChangesetItem *getItemById(QString id); // Slow: traversal required
 
@@ -58,10 +71,13 @@
 private slots:
     void changesetDetailShown();
     void changesetDetailHidden();
-    void dateItemClicked();
+
+protected:
+    void drawBackground(QPainter *, const QRectF &);
 
 private:
     ChangesetItem *m_detailShown;
+    DateRanges m_dateRanges;
 };
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/changesetview.cpp	Wed May 25 14:32:32 2011 +0100
@@ -0,0 +1,89 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    EasyMercurial
+
+    Based on HgExplorer by Jari Korhonen
+    Copyright (c) 2010 Jari Korhonen
+    Copyright (c) 2011 Chris Cannam
+    Copyright (c) 2011 Queen Mary, University of London
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "changesetview.h"
+#include "changesetscene.h"
+#include "colourset.h"
+#include "debug.h"
+
+#include <QScrollBar>
+
+ChangesetView::ChangesetView() :
+    Panned()
+{
+    connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
+	    this, SLOT(horizontalScrollHappened()));
+}
+
+void
+ChangesetView::horizontalScrollHappened()
+{
+    DEBUG << "ChangesetView::horizontalScrollHappened" << endl;
+    invalidateScene(rect(), QGraphicsScene::BackgroundLayer);
+    viewport()->update();
+}
+
+void
+ChangesetView::drawBackground(QPainter *paint, const QRectF &rect)
+{
+    DEBUG << "ChangesetView::drawBackground" << endl;
+
+    ChangesetScene *cs = qobject_cast<ChangesetScene *>(scene());
+
+    if (!cs) {
+	QGraphicsView::drawBackground(paint, rect);
+	return;
+    }
+
+    DEBUG << "ChangesetView::drawBackground: have scene" << endl;
+
+    ChangesetScene::DateRanges ranges = cs->getDateRanges();
+
+    paint->setClipRect(rect);
+
+    DEBUG << "clip rect is " << rect << endl;
+
+    paint->save();
+    QFont f(paint->font());
+    f.setBold(true);
+    paint->setFont(f);
+
+    float x = mapToScene(0, 0).x();
+    float w = mapToScene(width(), 0).x() - x;
+    float px = mapToScene(5, 0).x();
+
+    QBrush oddBrush(QColor::fromRgb(250, 250, 250));
+    QBrush evenBrush(QColor::fromRgb(240, 240, 240));
+
+    //!!! todo: select only the ranges actually within range!
+    
+    for (ChangesetScene::DateRanges::const_iterator i = ranges.begin();
+         i != ranges.end(); ++i) {
+
+	ChangesetScene::DateRange range = i.value();
+
+        QRectF r = QRectF(x, range.minrow * 90 - 25,
+			  w, range.nrows * 90).normalized();
+
+	paint->fillRect(r, range.even ? evenBrush : oddBrush);
+        paint->drawText(px, range.minrow * 90 - 10, range.label);
+    }
+
+    paint->restore();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/changesetview.h	Wed May 25 14:32:32 2011 +0100
@@ -0,0 +1,37 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    EasyMercurial
+
+    Based on HgExplorer by Jari Korhonen
+    Copyright (c) 2010 Jari Korhonen
+    Copyright (c) 2011 Chris Cannam
+    Copyright (c) 2011 Queen Mary, University of London
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#ifndef CHANGESETVIEW_H
+#define CHANGESETVIEW_H
+
+#include "panned.h"
+
+class ChangesetView : public Panned
+{
+    Q_OBJECT
+    
+public:
+    ChangesetView();
+
+private slots:
+    void horizontalScrollHappened();
+
+protected:
+    void drawBackground(QPainter *, const QRectF &);
+};
+
+#endif
--- a/src/dateitem.cpp	Tue May 24 18:00:12 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,91 +0,0 @@
-/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
-
-/*
-    EasyMercurial
-
-    Based on HgExplorer by Jari Korhonen
-    Copyright (c) 2010 Jari Korhonen
-    Copyright (c) 2011 Chris Cannam
-    Copyright (c) 2011 Queen Mary, University of London
-    
-    This program is free software; you can redistribute it and/or
-    modify it under the terms of the GNU General Public License as
-    published by the Free Software Foundation; either version 2 of the
-    License, or (at your option) any later version.  See the file
-    COPYING included with this distribution for more information.
-*/
-
-#include "dateitem.h"
-
-#include "debug.h"
-
-#include <QPainter>
-#include <QBrush>
-#include <QFont>
-#include <QGraphicsSceneMouseEvent>
-
-DateItem::DateItem() :
-    m_minrow(0), m_maxrow(0),
-    m_mincol(0), m_maxcol(0),
-    m_even(false)
-{
-}
-
-void
-DateItem::setRows(int minrow, int n)
-{
-    m_minrow = minrow;
-    m_maxrow = minrow + n - 1;
-    setY(m_minrow * 90);
-}
-
-void
-DateItem::setCols(int mincol, int n)
-{
-    m_mincol = mincol;
-    m_maxcol = mincol + n - 1;
-    setX(m_mincol * 100);
-}
-
-void
-DateItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
-{
-    DEBUG << "DateItem::mousePressEvent" << endl;
-    if (e->button() == Qt::LeftButton) {
-        emit clicked();
-    }
-    e->ignore();
-}
-
-QRectF
-DateItem::boundingRect() const
-{
-    return QRectF(-75, -25,
-		  (m_maxcol - m_mincol + 1) * 100 + 100,
-		  (m_maxrow - m_minrow + 1) * 90).normalized();
-}
-
-void
-DateItem::paint(QPainter *paint, const QStyleOptionGraphicsItem *opt, QWidget *w)
-{
-    QBrush brush;
-
-    if (m_even) {
-	QColor c(QColor::fromRgb(240, 240, 240));
-	brush = QBrush(c);
-    } else {
-	QColor c(QColor::fromRgb(250, 250, 250));
-	brush = QBrush(c);
-    }
-
-    paint->fillRect(boundingRect(), brush);
-
-    paint->save();
-    QFont f(paint->font());
-    f.setBold(true);
-    paint->setFont(f);
-    paint->drawText(-70, -10, m_dateString);
-    paint->restore();
-}
-
-
--- a/src/dateitem.h	Tue May 24 18:00:12 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
-
-/*
-    EasyMercurial
-
-    Based on HgExplorer by Jari Korhonen
-    Copyright (c) 2010 Jari Korhonen
-    Copyright (c) 2011 Chris Cannam
-    Copyright (c) 2011 Queen Mary, University of London
-    
-    This program is free software; you can redistribute it and/or
-    modify it under the terms of the GNU General Public License as
-    published by the Free Software Foundation; either version 2 of the
-    License, or (at your option) any later version.  See the file
-    COPYING included with this distribution for more information.
-*/
-
-#ifndef DATEITEM_H
-#define DATEITEM_H
-
-#include <QGraphicsObject>
-
-class DateItem : public QGraphicsObject
-{
-    Q_OBJECT
-
-public:
-    DateItem();
-
-    virtual QRectF boundingRect() const;
-    virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
-
-    void setRows(int minrow, int n);
-    void setCols(int mincol, int n);
-
-    void setEven(bool e) { m_even = e; }
-
-    QString dateString() const { return m_dateString; }
-    void setDateString(QString s) { m_dateString = s; }
-
-signals:
-    void clicked();
-
-protected:
-    virtual void mousePressEvent(QGraphicsSceneMouseEvent *);
-
-private:
-    QString m_dateString;
-    int m_minrow;
-    int m_maxrow;
-    int m_mincol;
-    int m_maxcol;
-    bool m_even;
-};
-
-#endif // DATEITEM_H
--- a/src/grapher.cpp	Tue May 24 18:00:12 2011 +0100
+++ b/src/grapher.cpp	Wed May 25 14:32:32 2011 +0100
@@ -17,7 +17,6 @@
 
 #include "grapher.h"
 #include "connectionitem.h"
-#include "dateitem.h"
 #include "debug.h"
 #include "changesetscene.h"
 
@@ -581,13 +580,7 @@
 
         if (date != prevDate) {
             if (prevDate != "") {
-                DateItem *item = new DateItem();
-                item->setDateString(prevDate);
-                item->setCols(datemincol, datemaxcol - datemincol + 1);
-                item->setRows(changeRow, n);
-                item->setEven(even);
-                item->setZValue(-2);
-                m_scene->addDateItem(item);
+                m_scene->addDateRange(prevDate, changeRow, n, even);
                 even = !even;
             }
             prevDate = date;
@@ -597,14 +590,10 @@
     }
     
     if (n > 0) {
-        DateItem *item = new DateItem();
-        item->setDateString(prevDate);
-        item->setCols(datemincol, datemaxcol - datemincol + 1);
-        item->setRows(changeRow, n+1);
-        item->setEven(even);
-        item->setZValue(-2);
-        m_scene->addDateItem(item);
+        m_scene->addDateRange(prevDate, changeRow, n+1, even);
         even = !even;
     }
+
+    m_scene->itemAddCompleted();
 }
 
--- a/src/historywidget.cpp	Tue May 24 18:00:12 2011 +0100
+++ b/src/historywidget.cpp	Wed May 25 14:32:32 2011 +0100
@@ -18,7 +18,7 @@
 #include "historywidget.h"
 
 #include "changesetscene.h"
-#include "panned.h"
+#include "changesetview.h"
 #include "panner.h"
 #include "grapher.h"
 #include "debug.h"
@@ -32,11 +32,12 @@
     m_showUncommitted(false),
     m_refreshNeeded(false)
 {
-    m_panned = new Panned;
+    m_panned = new ChangesetView;
     m_panner = new Panner;
 
     m_panned->setDragMode(QGraphicsView::ScrollHandDrag);
     m_panned->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
+    m_panned->setCacheMode(QGraphicsView::CacheNone);
 
     QGridLayout *layout = new QGridLayout;
     layout->addWidget(m_panned, 0, 0);