[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-19 18:55:43
Message-ID: 751a4f870801191055ie8dc3f6qc9dcbac5c81b3938 () mail ! gmail ! com
[Download RAW message or body]

2008/1/18, Aron Stansvik <elvstone@gmail.com>:
> 2008/1/18, Aron Stansvik <elvstone@gmail.com>:
> > 2008/1/17, C. Boemann <cbr@boemann.dk>:
> > > from a quick glance I think it looks ok, however:
> > >
> > > - why not cache the strategies rather than create and delete them everytime
> > > shift is pressed or released
> >
> > Hm. You mean have a KoRulerPrivate::lastStrategy which holds the last
> > strategy and just flip back and forth? Sorry if I'm getting this
> > straight away :)
>
> err, s/if i'm/if i'm not/

Okay. Attached is a new updated patch that caches the painting
strategies. Please have a look at it.

Aron

> >
> > > - please be sure this mode only happens when there is actually something to
> > > be moved
> >
> > Sorry, my mistake. Fixed.
> >
> > Aron
> >
> > >
> > > best regards
> > > Casper
> > >
> > > ----- Original Message -----
> > > From: "Aron Stansvik" <elvstone@gmail.com>
> > > To: "For developer's discussion about KOffice" <koffice-devel@kde.org>
> > > Sent: Thursday, January 17, 2008 11:12 PM
> > > Subject: Re: Precision "display mode" for KoRuler
> > >
> > >
> > > > 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
> > > >>
> > > >>
> > > >>
> > > >
> > >
> > >
> > > --------------------------------------------------------------------------------
> > >
> > >
> > > > _______________________________________________
> > > > koffice-devel mailing list
> > > > koffice-devel@kde.org
> > > > https://mail.kde.org/mailman/listinfo/koffice-devel
> > > >
> > >
> > > _______________________________________________
> > > 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 762855)
+++ 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),
@@ -546,8 +612,10 @@
     selected(None),
     selectOffset(0),
     tabChooser(0),
-    paintingStrategy(o == Qt::Horizontal ?
+    normalPaintingStrategy(o == Qt::Horizontal ?
             (PaintingStrategy*)new HorizontalPaintingStrategy() : \
(PaintingStrategy*)new VerticalPaintingStrategy()), +    \
distancesPaintingStrategy((PaintingStrategy*)new \
HorizontalDistancesPaintingStrategy()), +    \
paintingStrategy(normalPaintingStrategy),  ruler(parent)
 {
 #if QT_VERSION >= KDE_MAKE_VERSION(4,4,0)
@@ -558,7 +626,8 @@
 
 KoRulerPrivate::~KoRulerPrivate()
 {
-    delete paintingStrategy;
+    delete normalPaintingStrategy;
+    delete distancesPaintingStrategy;
 }
 
 double KoRulerPrivate::numberStepForUnit() const
@@ -701,7 +770,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();
@@ -917,6 +986,11 @@
         update();
     }
 #endif
+    if (d->orientation == Qt::Horizontal && (ev->modifiers() & Qt::ShiftModifier) &&
+            (d->selected == KoRulerPrivate::FirstLineIndent ||
+             d->selected == KoRulerPrivate::ParagraphIndent ||
+             d->selected == KoRulerPrivate::EndIndent))
+        d->paintingStrategy = d->distancesPaintingStrategy;
 }
 
 void KoRuler::mouseReleaseEvent ( QMouseEvent* ev )
@@ -929,6 +1003,7 @@
     if( d->selected == KoRulerPrivate::Tab)
         emit tabsChanged(true);
 
+    d->paintingStrategy = d->normalPaintingStrategy;
     d->selected = KoRulerPrivate::None;
 }
 
@@ -946,8 +1021,13 @@
         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);
+            d->paintingStrategy = d->normalPaintingStrategy;
+        } else {
+            if (d->orientation == Qt::Horizontal)
+                d->paintingStrategy = d->distancesPaintingStrategy;
+        }
 
         emit indentsChanged(false);
         break;
@@ -958,8 +1038,14 @@
         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);
+            d->paintingStrategy = d->normalPaintingStrategy;
+        } else {
+            if (d->orientation == Qt::Horizontal)
+                d->paintingStrategy = d->distancesPaintingStrategy;
+        }
+
         if (d->paragraphIndent < 0)
             d->paragraphIndent = 0;
         if (d->paragraphIndent + d->endIndent > activeLength)
@@ -973,8 +1059,14 @@
         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);
+            d->paintingStrategy = d->normalPaintingStrategy;
+        } else {
+            if (d->orientation == Qt::Horizontal)
+                d->paintingStrategy = d->distancesPaintingStrategy;
+        }
+
         if (d->endIndent < 0)
             d->endIndent = 0;
         if (d->paragraphIndent + d->endIndent > activeLength)
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); @@ -127,7 +137,14 @@
     QList<QAction*> popupActions;
 
     RulerTabChooser *tabChooser;
-    PaintingStrategy * const paintingStrategy;
+
+    // Cached painting strategies
+    PaintingStrategy * normalPaintingStrategy;
+    PaintingStrategy * distancesPaintingStrategy;
+
+    // Current painting strategy
+    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