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

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
0003    SPDX-FileCopyrightText: 2006 Peter Simonsson <peter.simonsson@gmail.com>
0004    SPDX-FileCopyrightText: 2007 C. Boemann <cbo@boemann.dk>
0005    SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
0006 
0007    SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef koRuler_h
0011 #define koRuler_h
0012 
0013 
0014 #include "kritawidgets_export.h"
0015 
0016 #include <QWidget>
0017 #include <QTextOption>
0018 
0019 class QPaintEvent;
0020 
0021 class KoViewConverter;
0022 class KoCanvasBase;
0023 class KoRulerPrivate;
0024 class KoUnit;
0025 
0026 /**
0027  * Decorator widget to draw a single ruler around a canvas.
0028  */
0029 class KRITAWIDGETS_EXPORT KoRuler : public QWidget
0030 {
0031 Q_OBJECT
0032 public:
0033     /**
0034      * Creates a ruler with the orientation @p orientation
0035      * @param parent parent widget
0036      * @param orientation the orientation of the ruler
0037      * @param viewConverter the view converter used to convert from point to pixel
0038      */
0039     KoRuler(QWidget* parent, Qt::Orientation orientation, const KoViewConverter* viewConverter);
0040     ~KoRuler() override;
0041 
0042     /// For paragraphs each tab definition is represented by this struct.
0043     struct Tab {
0044         qreal position { 0.0 };    ///< distance in point from the start of the text-shape
0045         QTextOption::TabType type { QTextOption::LeftTab };       ///< Determine which type is used.
0046     };
0047 
0048     /// The ruler's unit
0049     KoUnit unit() const;
0050 
0051     /// The length of the ruler in points (pt)
0052     qreal rulerLength() const;
0053 
0054     /// The orientation of the ruler
0055     Qt::Orientation orientation() const;
0056 
0057     /// The start indent of the first line
0058     qreal firstLineIndent() const;
0059 
0060     /// The start indent of the rest of the lines
0061     qreal paragraphIndent() const;
0062 
0063     /// The end indent of all lines
0064     qreal endIndent() const;
0065 
0066     /// The tab chooser widget, which you must put into a layout along with the ruler.
0067     /// Returns 0 for vertical rulers,
0068     QWidget *tabChooser();
0069 
0070     /**
0071      * set a list of actions that will be shown in a popup should the user right click on this ruler.
0072      * @param popupActionList the list of actions
0073      * @see popupActionList()
0074      */
0075     void setPopupActionList(const QList<QAction*> &popupActionList);
0076     /**
0077      * Return the actions list.
0078      * @see setPopupActionList()
0079      */
0080     QList<QAction*> popupActionList() const;
0081 
0082     /// reimplemented
0083     QSize minimumSizeHint() const override;
0084 
0085     /// reimplemented
0086     QSize sizeHint() const override;
0087 
0088 public Q_SLOTS:
0089     /// Set the unit of the ruler
0090     void setUnit(const KoUnit &unit);
0091 
0092     /** Set the offset. Use this function to sync the ruler with
0093       * the canvas' position on screen
0094       * @param offset The offset in pixels
0095       */
0096     void setOffset(int offset);
0097 
0098     /// Sets the length of the ruler to @p length in points (pt)
0099     void setRulerLength(qreal length);
0100 
0101     /** Set the active range, ie the part of the ruler that is most likely used.
0102       * set to 0, 0 when there is no longer any active range
0103       * @param start the start of the range in pt
0104       * @param end the end of the range in pt
0105       */
0106     void setActiveRange(qreal start, qreal end);
0107 
0108     /** Set the override active range, ie the part of the ruler that is most likely used.
0109       * set to 0, 0 when there is no longer any active range
0110       * The override, means that if set it takes precedence over the normal active range.
0111       * @param start the start of the range in pt
0112       * @param end the end of the range in pt
0113       */
0114     void setOverrideActiveRange(qreal start, qreal end);
0115 
0116     /** Set the state of the ruler so that it shows everything in right to left mode.
0117       * @param isRightToLeft state of right to left mode. Default is false.
0118       */
0119     void setRightToLeft(bool isRightToLeft);
0120 
0121     /** Set if the ruler should show indents as used in textditors.
0122       * Set the indents with setFirstLineIndent(), setParagraphIndent(), setEndIndent() .
0123       * @param show show indents if true. Default is false.
0124       */
0125     void setShowIndents(bool show);
0126 
0127     /** Set the position of the first line start indent relative to the active range.
0128       * If Right To left is set the indent is relative to the right side of the active range .
0129       * @param indent the value relative to the active range.
0130       */
0131     void setFirstLineIndent(qreal indent);
0132 
0133     /** Set the position of the rest of the lines start indent relative to the active range.
0134       * If Right To left is set the indent is relative to the right side of the active range .
0135       * @param indent the value relative to the active range.
0136       */
0137     void setParagraphIndent(qreal indent);
0138 
0139     /** Set the position of the end indent relative to the active range.
0140       * If Right To left is set the indent is relative to the left side of the active range .
0141       * @param indent the value relative to the active range.
0142       */
0143     void setEndIndent(qreal indent);
0144 
0145     /** Set whether the ruler should show the current mouse position.
0146       * Update the position with updateMouseCoordinate().
0147       * @param show show mouse position if true. Default is false.
0148       */
0149     void setShowMousePosition(bool show);
0150 
0151     /**
0152       * \see setShowMousePosition
0153       */
0154     bool showMousePosition() const;
0155 
0156     /** Update the current position of the mouse pointer, repainting if changed.
0157       * The ruler offset will be applied before painting.
0158       * @param coordinate Either the x or y coordinate of the mouse depending
0159       *                   of the orientation of the ruler.
0160       */
0161     void updateMouseCoordinate(int coordinate);
0162 
0163     /**
0164      * Set whether the ruler should show the selection borders
0165      * @param show show selection borders if true, default is false.
0166      */
0167     void setShowSelectionBorders(bool show);
0168 
0169     /**
0170      * Update the selection borders
0171      * @param first the first selection border in points
0172      * @param second the other selection border in points
0173      */
0174     void updateSelectionBorders(qreal first, qreal second);
0175 
0176     /**
0177      * Set whether the ruler should show tabs
0178      * @param show show selection borders if true, default is false.
0179      */
0180     void setShowTabs(bool show);
0181 
0182     /**
0183      * Set whether the tabs is relative to the paragraph indent
0184      * @param relative tabs are relative to paragraph indent if true, default is false.
0185      */
0186     void setRelativeTabs(bool relative);
0187 
0188     /**
0189      * Update the tabs
0190      * @param tabs a list of tabs that is shown on the ruler
0191      * @param tabDistance the distance between regular interval tabs
0192      */
0193     void updateTabs(const QList<Tab> &tabs, qreal tabDistance);
0194 
0195     /***
0196      * Return the list of tabs set on this ruler.
0197      */
0198     QList<Tab> tabs() const;
0199 
0200     /**
0201      * Clear all previously set hotspots.
0202      * A hotspot is a position on the ruler that the user can manipulate by dragging.
0203      */
0204     void clearHotSpots();
0205 
0206     /**
0207      * Add or set a hotspot.
0208      * A hotspot is a position on the ruler that the user can manipulate by dragging.
0209      * @param position the new position of the hotspot.
0210      * @param id the unique id for the hotspot. If the id has not been set before, it will be added.
0211      */
0212     void setHotSpot(qreal position, int id = -1);
0213 
0214     /**
0215      * Remove a previously set hotspot, returning true if one is actually returned.
0216      * @param id the unique id for the hotspot.
0217      * A hotspot is a position on the ruler that the user can manipulate by dragging.
0218      */
0219     bool removeHotSpot(int id);
0220 
0221     /**
0222      * Connect the ruler to a guides tool
0223      * This allows the user to drag a guide out of the ruler and get in one smooth operation
0224      * the guide tool to draw and position the guide line.
0225      * @param canvas the canvas that has had the KoToolManager create the tool for previously.
0226      */
0227     void createGuideToolConnection(KoCanvasBase *canvas);
0228 
0229     void setUnitPixelMultiple2(bool enabled);
0230 
0231 Q_SIGNALS:
0232     /**
0233      * emitted when any of the indents is moved by the user.
0234      * @param final false until the user releases the mouse. So you can implement live update.
0235      */
0236     void indentsChanged(bool final);
0237 
0238     /**
0239      * Emitted when any of the tabs are moved, deleted or inserted by the user.
0240      * @param originalTabIndex the index in the list of tabs before the user interaction
0241      *          started, or -1 if this is a new tab
0242      * @param tab the new tab, or zero when the tab has been removed.
0243      */
0244     void tabChanged(int originalTabIndex, KoRuler::Tab *tab);
0245 
0246     /// emitted when there the user is about to change a tab or hotspot
0247     void aboutToChange();
0248 
0249     void hotSpotChanged(int id, qreal newPosition);
0250 
0251     /// emitted when the mouse is drag+released outside the ruler
0252     void guideLineCreated(Qt::Orientation orientation, qreal viewPosition);
0253 
0254     void guideCreationInProgress(Qt::Orientation orientation, const QPoint &globalPos);
0255     void guideCreationFinished(Qt::Orientation orientation, const QPoint &globalPos);
0256 
0257 protected:
0258     /// reimplemented
0259     void paintEvent(QPaintEvent* event) override;
0260     /// reimplemented
0261     void mousePressEvent(QMouseEvent *ev) override;
0262     /// reimplemented
0263     void mouseReleaseEvent(QMouseEvent *ev) override;
0264     /// reimplemented
0265     void mouseMoveEvent(QMouseEvent *ev) override;
0266 
0267 private:
0268     KoRulerPrivate * const d;
0269     friend class KoRulerPrivate;
0270 };
0271 
0272 #endif