File indexing completed on 2024-05-12 16:02:11

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #ifndef KORULER_P_H
0007 #define KORULER_P_H
0008 
0009 #include <KoUnit.h>
0010 
0011 class RulerTabChooser : public QWidget
0012 {
0013 public:
0014     RulerTabChooser(QWidget *parent) : QWidget(parent), m_type(QTextOption::LeftTab), m_showTabs(false) {}
0015     ~RulerTabChooser() override {}
0016 
0017     inline QTextOption::TabType type() {return m_type;}
0018     void setShowTabs(bool showTabs) { if (m_showTabs == showTabs) return; m_showTabs = showTabs; update(); }
0019     void mousePressEvent(QMouseEvent *) override;
0020 
0021     void paintEvent(QPaintEvent *) override;
0022 
0023 private:
0024     QTextOption::TabType m_type { QTextOption::LeftTab };
0025     bool m_showTabs :1;
0026 };
0027 
0028 class PaintingStrategy
0029 {
0030 public:
0031     /// constructor
0032     PaintingStrategy() {}
0033     /// destructor
0034     virtual ~PaintingStrategy() {}
0035 
0036     /**
0037      * Draw the background of the ruler.
0038      * @param ruler the ruler to draw on.
0039      * @param painter the painter we can paint with.
0040      */
0041     virtual QRectF drawBackground(const KoRulerPrivate *ruler, QPainter &painter) = 0;
0042 
0043     /**
0044      * Draw the indicators for text-tabs.
0045      * @param ruler the ruler to draw on.
0046      * @param painter the painter we can paint with.
0047      */
0048     virtual void drawTabs(const KoRulerPrivate *ruler, QPainter &painter) = 0;
0049 
0050     /**
0051      * Draw the indicators for the measurements which typically are drawn every [unit].
0052      * @param ruler the ruler to draw on.
0053      * @param painter the painter we can paint with.
0054      * @param rectangle
0055      */
0056     virtual void drawMeasurements(const KoRulerPrivate *ruler, QPainter &painter, const QRectF &rectangle) = 0;
0057 
0058     /**
0059      * Draw the indicators for the indents of a text paragraph
0060      * @param ruler the ruler to draw on.
0061      * @param painter the painter we can paint with.
0062      */
0063     virtual void drawIndents(const KoRulerPrivate *ruler, QPainter &painter) = 0;
0064 
0065     /**
0066      *returns the size suggestion for a ruler with this strategy.
0067      */
0068     virtual QSize sizeHint() = 0;
0069 };
0070 
0071 class HorizontalPaintingStrategy : public PaintingStrategy
0072 {
0073 public:
0074     HorizontalPaintingStrategy() : lengthInPixel(1) {}
0075 
0076     QRectF drawBackground(const KoRulerPrivate *ruler, QPainter &painter) override;
0077     void drawTabs(const KoRulerPrivate *ruler, QPainter &painter) override;
0078     void drawMeasurements(const KoRulerPrivate *ruler, QPainter &painter, const QRectF &rectangle) override;
0079     void drawIndents(const KoRulerPrivate *ruler, QPainter &painter) override;
0080     QSize sizeHint() override;
0081 
0082 private:
0083     qreal lengthInPixel {0.0};
0084 };
0085 
0086 class VerticalPaintingStrategy : public PaintingStrategy
0087 {
0088 public:
0089     VerticalPaintingStrategy() : lengthInPixel(1) {}
0090 
0091     QRectF drawBackground(const KoRulerPrivate *ruler, QPainter &painter) override;
0092     void drawTabs(const KoRulerPrivate *, QPainter &) override {}
0093     void drawMeasurements(const KoRulerPrivate *ruler, QPainter &painter, const QRectF &rectangle) override;
0094     void drawIndents(const KoRulerPrivate *, QPainter &) override { }
0095     QSize sizeHint() override;
0096 
0097 private:
0098     qreal lengthInPixel {0.0};
0099 };
0100 
0101 class HorizontalDistancesPaintingStrategy : public HorizontalPaintingStrategy
0102 {
0103 public:
0104     HorizontalDistancesPaintingStrategy() {}
0105 
0106     void drawMeasurements(const KoRulerPrivate *ruler, QPainter &painter, const QRectF &rectangle) override;
0107 
0108 private:
0109     void drawDistanceLine(const KoRulerPrivate *d, QPainter &painter, const qreal start, const qreal end);
0110 };
0111 
0112 class KoRulerPrivate
0113 {
0114 public:
0115     KoRulerPrivate(KoRuler *parent, const KoViewConverter *vc, Qt::Orientation orientation);
0116     ~KoRulerPrivate();
0117 
0118     void emitTabChanged();
0119 
0120     KoUnit unit;
0121     const Qt::Orientation orientation;
0122     const KoViewConverter * const viewConverter;
0123 
0124     int offset { 0 };
0125     qreal rulerLength {0.0};
0126     qreal activeRangeStart {0.0};
0127     qreal activeRangeEnd {0.0};
0128     qreal activeOverrideRangeStart {0.0};
0129     qreal activeOverrideRangeEnd {0.0};
0130 
0131     int mouseCoordinate {0};
0132     int showMousePosition {0};
0133 
0134     bool showSelectionBorders {false};
0135     qreal firstSelectionBorder {0.0};
0136     qreal secondSelectionBorder {0.0};
0137 
0138     bool showIndents {false};
0139     qreal firstLineIndent {0.0};
0140     qreal paragraphIndent {0.0};
0141     qreal endIndent {0.0};
0142 
0143     bool showTabs {false};
0144     bool relativeTabs {false};
0145     bool tabMoved {false}; // set to true on first move of a selected tab
0146     QList<KoRuler::Tab> tabs;
0147     int originalIndex {0}; //index of selected tab before we started dragging it.
0148     int currentIndex {0}; //index of selected tab or selected HotSpot - only valid when selected indicates tab or hotspot
0149     KoRuler::Tab deletedTab;
0150     qreal tabDistance {0.0};
0151 
0152     struct HotSpotData {
0153         qreal position;
0154         int id;
0155     };
0156     QList<HotSpotData> hotspots;
0157 
0158     bool rightToLeft {false};
0159     enum Selection {
0160         None,
0161         Tab,
0162         FirstLineIndent,
0163         ParagraphIndent,
0164         EndIndent,
0165         HotSpot
0166     };
0167     Selection selected {None};
0168     int selectOffset {0};
0169 
0170     QList<QAction*> popupActions;
0171 
0172     RulerTabChooser *tabChooser {0};
0173 
0174     // Cached painting strategies
0175     PaintingStrategy * normalPaintingStrategy {0};
0176     PaintingStrategy * distancesPaintingStrategy {0};
0177 
0178     // Current painting strategy
0179     PaintingStrategy * paintingStrategy {0};
0180 
0181     KoRuler *ruler {0};
0182 
0183     bool guideCreationStarted {false};
0184 
0185     qreal pixelStep {0.0};
0186 
0187     qreal numberStepForUnit() const;
0188     /// @return The rounding of value to the nearest multiple of stepValue
0189     qreal doSnapping(const qreal value) const;
0190     Selection selectionAtPosition(const QPoint & pos, int *selectOffset = 0);
0191     int hotSpotIndex(const QPoint & pos);
0192     qreal effectiveActiveRangeStart() const;
0193     qreal effectiveActiveRangeEnd() const;
0194 
0195     friend class VerticalPaintingStrategy;
0196     friend class HorizontalPaintingStrategy;
0197 };
0198 
0199 #endif