[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: Precision "display mode" for KoRuler
From:       "Aron Stansvik" <elvstone () gmail ! com>
Date:       2008-01-17 22:12:10
Message-ID: 751a4f870801171412x760e6e9cn7c45f626a62bd981 () mail ! gmail ! com
[Download RAW message or body]

Hi everybody.

Attached is a patch for this feature for review. Please try it out.
The actual activation of my display mode turned out to be more
complicated than I thought, with no really elegant solution, but after
having discussed it a bit in #koffice with Casper and others, we
decided this was the least ugly way to do it.

It only implements this feature for the horizontal rulers so far, as
the margins in the ruler can't be moved yet I had no way to test it
for the vertical one. Maybe I'll look into fixing moving of the
margins.

Typing this from a buzzing KDE 4.0 release event in Mountain View,
very exciting :)

Take care,
Aron

2007/11/3, Thomas Zander <zander@kde.org>:
> On Saturday 03 November 2007 12:24:35 Aron Stansvik wrote:
> > I think this is pure genius :) Thomas?
>
> Agreed
>
> --
> Thomas Zander
>
> _______________________________________________
> koffice-devel mailing list
> koffice-devel@kde.org
> https://mail.kde.org/mailman/listinfo/koffice-devel
>
>
>

["koruler-distances-paintingstrategy.diff" (text/x-patch)]

Index: KoRuler.cpp
===================================================================
--- KoRuler.cpp	(revision 762736)
+++ KoRuler.cpp	(arbetskopia)
@@ -191,7 +191,7 @@
 #endif
 }
 
-void HorizontalPaintingStrategy::drawRulerStripes(const KoRulerPrivate *d, QPainter \
&painter, const QRectF &rectangle) { +void \
HorizontalPaintingStrategy::drawMeasurements(const KoRulerPrivate *d, QPainter \
&painter, const QRectF &rectangle) {  double numberStep = d->numberStepForUnit(); // \
number step in unit  QRectF activeRangeRectangle;
     int numberStepPixel = \
qRound(d->viewConverter->documentToViewX(d->unit.fromUserValue(numberStep))); @@ \
-406,7 +406,7 @@  return rectangle;
 }
 
-void VerticalPaintingStrategy::drawRulerStripes(const KoRulerPrivate *d, QPainter \
&painter, const QRectF &rectangle) { +void \
VerticalPaintingStrategy::drawMeasurements(const KoRulerPrivate *d, QPainter \
&painter, const QRectF &rectangle) {  double numberStep = d->numberStepForUnit(); // \
                number step in unit
     int numberStepPixel = qRound(d->viewConverter->documentToViewY( \
d->unit.fromUserValue(numberStep)));  QFontMetrics \
fontMetrics(KGlobalSettings::toolBarFont()); @@ -523,6 +523,72 @@
     return size;
 }
 
+
+void HorizontalDistancesPaintingStrategy::drawDistanceLine(const KoRulerPrivate *d, \
QPainter &painter, const double start, const double end) { +
+    // Don't draw too short lines
+    if (qMax(start, end) - qMin(start, end) < 1)
+        return;
+
+    painter.save();
+    painter.translate(d->offset, d->ruler->height() / 2);
+    painter.setPen(d->ruler->palette().color(QPalette::Text));
+    painter.setBrush(d->ruler->palette().color(QPalette::Text));
+
+    QLineF line(QPointF(d->viewConverter->documentToViewX(start), 0),
+            QPointF(d->viewConverter->documentToViewX(end), 0));
+    QPointF midPoint = line.pointAt(0.5);
+
+    // Draw the label text
+    QFont font = KGlobalSettings::smallestReadableFont();
+    font.setPointSize(6);
+    QFontMetrics fontMetrics(font);
+    QString label = d->unit.toUserStringValue(
+            d->viewConverter->viewToDocumentX(line.length())) + " " + \
KoUnit::unitName(d->unit); +    QPointF labelPosition = QPointF(midPoint.x() - \
fontMetrics.width(label)/2, +            midPoint.y() + fontMetrics.ascent()/2);
+    painter.setFont(font);
+    painter.drawText(labelPosition, label);
+
+    // Draw the arrow lines
+    double arrowLength = (line.length() - fontMetrics.width(label)) / 2 - 2;
+    arrowLength = qMax(0.0, arrowLength);
+    QLineF startArrow(line.p1(), line.pointAt(arrowLength / line.length()));
+    QLineF endArrow(line.p2(), line.pointAt(1.0 - arrowLength / line.length()));
+    painter.drawLine(startArrow);
+    painter.drawLine(endArrow);
+
+    // Draw the arrow heads
+    QPolygonF arrowHead;
+    arrowHead << line.p1() << QPointF(line.x1()+3, line.y1()-3)
+        << QPointF(line.x1()+3, line.y1()+3);
+    painter.drawPolygon(arrowHead);
+    arrowHead.clear();
+    arrowHead << line.p2() << QPointF(line.x2()-3, line.y2()-3)
+        << QPointF(line.x2()-3, line.y2()+3);
+    painter.drawPolygon(arrowHead);
+
+    painter.restore();
+}
+
+void HorizontalDistancesPaintingStrategy::drawMeasurements(const KoRulerPrivate *d, \
QPainter &painter, const QRectF&) { +    QList<double> points;
+    points << 0.0;
+    points << d->activeRangeStart + d->paragraphIndent + d->firstLineIndent;
+    points << d->activeRangeStart + d->paragraphIndent;
+    points << d->activeRangeEnd - d->endIndent;
+    points << d->activeRangeStart;
+    points << d->activeRangeEnd;
+    points << d->rulerLength;
+    qSort(points.begin(), points.end());
+    QListIterator<double> i(points);
+    i.next();
+    while (i.hasNext() && i.hasPrevious()) {
+        drawDistanceLine(d, painter, i.peekPrevious(), i.peekNext());
+        i.next();
+    }
+}
+
 KoRulerPrivate::KoRulerPrivate(KoRuler *parent, const KoViewConverter *vc, \
Qt::Orientation o)  : unit(KoUnit(KoUnit::Point)),
     orientation(o),
@@ -545,6 +611,7 @@
     rightToLeft(false),
     selected(None),
     selectOffset(0),
+    lastModifiers(Qt::NoModifier),
     tabChooser(0),
     paintingStrategy(o == Qt::Horizontal ?
             (PaintingStrategy*)new HorizontalPaintingStrategy() : \
(PaintingStrategy*)new VerticalPaintingStrategy()), @@ -701,7 +768,7 @@
     QRectF rectangle = d->paintingStrategy->drawBackground(d, painter);
     painter.restore();
     painter.save();
-    d->paintingStrategy->drawRulerStripes(d, painter, rectangle);
+    d->paintingStrategy->drawMeasurements(d, painter, rectangle);
     painter.restore();
     if (d->showIndents) {
         painter.save();
@@ -842,6 +909,11 @@
         return;
     }
 
+    if (d->orientation == Qt::Horizontal && (ev->modifiers() & Qt::ShiftModifier)) {
+        delete d->paintingStrategy;
+        d->paintingStrategy = (PaintingStrategy*)new \
HorizontalDistancesPaintingStrategy(); +    }
+
     QPoint pos = ev->pos();
 
 #if QT_VERSION >= KDE_MAKE_VERSION(4,4,0)
@@ -929,6 +1001,14 @@
     if( d->selected == KoRulerPrivate::Tab)
         emit tabsChanged(true);
 
+    if( d->orientation == Qt::Horizontal) {
+        delete d->paintingStrategy;
+        d->paintingStrategy = (PaintingStrategy*)new HorizontalPaintingStrategy();
+    } else {
+        delete d->paintingStrategy;
+        d->paintingStrategy = (PaintingStrategy*)new VerticalPaintingStrategy();
+    }
+
     d->selected = KoRulerPrivate::None;
 }
 
@@ -946,8 +1026,25 @@
         else
             d->firstLineIndent = d->viewConverter->viewToDocumentX(pos.x() + \
                d->selectOffset
                 - d->offset) - d->activeRangeStart - d->paragraphIndent;
-        if( ! (ev->modifiers() & Qt::ShiftModifier))
+        if( ! (ev->modifiers() & Qt::ShiftModifier)) {
             d->firstLineIndent = d->doSnapping(d->firstLineIndent);
+            if (ev->modifiers() != d->lastModifiers) {
+                if (d->orientation == Qt::Horizontal) {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
HorizontalPaintingStrategy(); +                } else {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
VerticalPaintingStrategy(); +                }
+            }
+        } else {
+            if (ev->modifiers() != d->lastModifiers) {
+                if (d->orientation == Qt::Horizontal) {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
HorizontalDistancesPaintingStrategy(); +                }
+            }
+        }
 
         emit indentsChanged(false);
         break;
@@ -958,8 +1055,26 @@
         else
             d->paragraphIndent = d->viewConverter->viewToDocumentX(pos.x() + \
                d->selectOffset
                 - d->offset) - d->activeRangeStart;
-        if( ! (ev->modifiers() & Qt::ShiftModifier))
+        if( ! (ev->modifiers() & Qt::ShiftModifier)) {
             d->paragraphIndent = d->doSnapping(d->paragraphIndent);
+            if (ev->modifiers() != d->lastModifiers) {
+                if (d->orientation == Qt::Horizontal) {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
HorizontalPaintingStrategy(); +                } else {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
VerticalPaintingStrategy(); +                }
+            }
+        } else {
+            if (ev->modifiers() != d->lastModifiers) {
+                if (d->orientation == Qt::Horizontal) {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
HorizontalDistancesPaintingStrategy(); +                }
+            }
+        }
+
         if (d->paragraphIndent < 0)
             d->paragraphIndent = 0;
         if (d->paragraphIndent + d->endIndent > activeLength)
@@ -973,8 +1088,26 @@
         else
             d->endIndent = d->activeRangeEnd - \
d->viewConverter->viewToDocumentX(pos.x()  + d->selectOffset - d->offset);
-        if( ! (ev->modifiers() & Qt::ShiftModifier))
+        if( ! (ev->modifiers() & Qt::ShiftModifier)) {
             d->endIndent = d->doSnapping(d->endIndent);
+            if (ev->modifiers() != d->lastModifiers) {
+                if (d->orientation == Qt::Horizontal) {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
HorizontalPaintingStrategy(); +                } else {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
VerticalPaintingStrategy(); +                }
+            }
+        } else {
+            if (ev->modifiers() != d->lastModifiers) {
+                if (d->orientation == Qt::Horizontal) {
+                    delete d->paintingStrategy;
+                    d->paintingStrategy = (PaintingStrategy*)new \
HorizontalDistancesPaintingStrategy(); +                }
+            }
+        }
+
         if (d->endIndent < 0)
             d->endIndent = 0;
         if (d->paragraphIndent + d->endIndent > activeLength)
@@ -1039,6 +1172,7 @@
         }
         setToolTip(text);
     }
+    d->lastModifiers = ev->modifiers();
     update();
 }
 
Index: KoRuler_p.h
===================================================================
--- KoRuler_p.h	(revision 762736)
+++ KoRuler_p.h	(arbetskopia)
@@ -42,7 +42,7 @@
 
     virtual QRectF drawBackground(const KoRulerPrivate *ruler, QPainter &painter) = \
                0;
     virtual void drawTabs(const KoRulerPrivate *ruler, QPainter &painter) = 0;
-    virtual void drawRulerStripes(const KoRulerPrivate *ruler, QPainter &painter, \
const QRectF &rectangle) = 0; +    virtual void drawMeasurements(const KoRulerPrivate \
                *ruler, QPainter &painter, const QRectF &rectangle) = 0;
     virtual void drawIndents(const KoRulerPrivate *ruler, QPainter &painter) = 0;
     virtual QSize sizeHint() = 0;
 
@@ -54,7 +54,7 @@
 
     virtual QRectF drawBackground(const KoRulerPrivate *ruler, QPainter &painter);
     virtual void drawTabs(const KoRulerPrivate *ruler, QPainter &painter);
-    virtual void drawRulerStripes(const KoRulerPrivate *ruler, QPainter &painter, \
const QRectF &rectangle); +    virtual void drawMeasurements(const KoRulerPrivate \
                *ruler, QPainter &painter, const QRectF &rectangle);
     virtual void drawIndents(const KoRulerPrivate *ruler, QPainter &painter);
     virtual QSize sizeHint();
 
@@ -68,7 +68,7 @@
 
     virtual QRectF drawBackground(const KoRulerPrivate *ruler, QPainter &painter);
     virtual void drawTabs(const KoRulerPrivate *, QPainter &) {}
-    virtual void drawRulerStripes(const KoRulerPrivate *ruler, QPainter &painter, \
const QRectF &rectangle); +    virtual void drawMeasurements(const KoRulerPrivate \
*ruler, QPainter &painter, const QRectF &rectangle);  virtual void drawIndents(const \
KoRulerPrivate *, QPainter &) { }  virtual QSize sizeHint();
 
@@ -76,6 +76,16 @@
     double lengthInPixel;
 };
 
+class HorizontalDistancesPaintingStrategy : public HorizontalPaintingStrategy {
+public:
+    HorizontalDistancesPaintingStrategy() {}
+
+    virtual void drawMeasurements(const KoRulerPrivate *ruler, QPainter &painter, \
const QRectF &rectangle); +
+private:
+    void drawDistanceLine(const KoRulerPrivate *d, QPainter &painter, const double \
start, const double end); +};
+
 class KoRulerPrivate {
 public:
     KoRulerPrivate(KoRuler *parent, const KoViewConverter *vc, Qt::Orientation \
orientation); @@ -123,11 +133,12 @@
     };
     Selection selected;
     int selectOffset;
+    Qt::KeyboardModifiers lastModifiers;
 
     QList<QAction*> popupActions;
 
     RulerTabChooser *tabChooser;
-    PaintingStrategy * const paintingStrategy;
+    PaintingStrategy * paintingStrategy;
     KoRuler *ruler;
 
     double numberStepForUnit() const;



_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic