File indexing completed on 2024-04-28 15:51:51

0001 /*
0002     SPDX-FileCopyrightText: 2005 Enrico Ros <eros.kde@email.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef _OKULAR_PAGEVIEWANNOTATOR_H_
0008 #define _OKULAR_PAGEVIEWANNOTATOR_H_
0009 
0010 #include <QObject>
0011 #include <qdom.h>
0012 
0013 #include <KActionCollection>
0014 
0015 #include "annotationtools.h"
0016 #include "pageviewutils.h"
0017 
0018 class QKeyEvent;
0019 class QMouseEvent;
0020 class QPainter;
0021 class AnnotationActionHandler;
0022 
0023 namespace Okular
0024 {
0025 class Document;
0026 }
0027 
0028 // engines are defined and implemented in the cpp
0029 class AnnotatorEngine;
0030 class AnnotationTools;
0031 class PageView;
0032 
0033 /**
0034  * @short PageView object devoted to annotation creation/handling.
0035  *
0036  * PageViewAnnotator is the okular class used for visually creating annotations.
0037  * It uses internal 'engines' for interacting with user events and attaches
0038  * the newly created annotation to the document when the creation is complete.
0039  * In the meanwhile all PageView events (actually mouse/paint ones) are routed
0040  * to this class that performs a rough visual representation of what the
0041  * annotation will become when finished.
0042  *
0043  * m_builtinToolsDefinition is a AnnotationTools object that wraps a DOM object that
0044  * contains Annotations/Engine association for the items placed in the toolbar.
0045  * The XML is parsed after selecting a toolbar item, in which case an Ann is
0046  * initialized with the values in the XML and an engine is created to handle
0047  * that annotation. m_builtinToolsDefinition is created in reparseConfig according to
0048  * user configuration. m_builtinToolsDefinition is updated (and saved to disk) (1) each
0049  * time a property of an annotation (color, font, etc) is changed by the user,
0050  * and (2) each time a "quick annotation" is selected, in which case the properties
0051  * of the selected quick annotation are written over those of the corresponding
0052  * builtin tool
0053  */
0054 class PageViewAnnotator : public QObject
0055 {
0056     Q_OBJECT
0057 public:
0058     static const int STAMP_TOOL_ID;
0059 
0060     PageViewAnnotator(PageView *parent, Okular::Document *storage);
0061     ~PageViewAnnotator() override;
0062 
0063     // methods used when creating the annotation
0064     // @return Is a tool currently selected?
0065     bool active() const;
0066     // @return Are we currently annotating (using the selected tool)?
0067     bool annotating() const;
0068 
0069     void setSignatureMode(bool enabled);
0070     bool signatureMode() const;
0071 
0072     // returns the preferred cursor for the current tool. call this only
0073     // if active() == true
0074     QCursor cursor() const;
0075 
0076     QRect routeMouseEvent(QMouseEvent *event, PageViewItem *item);
0077     QRect routeTabletEvent(QTabletEvent *event, PageViewItem *item, const QPoint localOriginInGlobal);
0078     QRect performRouteMouseOrTabletEvent(const AnnotatorEngine::EventType eventType, const AnnotatorEngine::Button button, const AnnotatorEngine::Modifiers modifiers, const QPointF pos, PageViewItem *item);
0079     bool routeKeyEvent(QKeyEvent *event);
0080     bool routePaints(const QRect wantedRect) const;
0081     void routePaint(QPainter *painter, const QRect paintRect);
0082 
0083     void reparseConfig();
0084 
0085     static QString defaultToolName(const QDomElement &toolElement);
0086     static QPixmap makeToolPixmap(const QDomElement &toolElement);
0087 
0088     // methods related to the annotation actions
0089     void setupActions(KActionCollection *ac);
0090     // setup those actions that first require the GUI is fully created
0091     void setupActionsPostGUIActivated();
0092     // @return Is continuous mode active (pin annotation)?
0093     bool continuousMode();
0094     /**
0095      * State of constrain ratio and angle action.
0096      * While annotating, this value is XOR-ed with the Shift modifier.
0097      */
0098     bool constrainRatioAndAngleActive();
0099     // enable/disable the annotation actions
0100     void setToolsEnabled(bool enabled);
0101     // enable/disable the text-selection annotation actions
0102     void setTextToolsEnabled(bool enabled);
0103 
0104     enum class ShowTip { Yes, No };
0105     // selects the active tool
0106     void selectBuiltinTool(int toolId, ShowTip showTip);
0107     // selects a stamp tool and sets the stamp symbol
0108     void selectStampTool(const QString &stampSymbol);
0109     // selects the active quick tool
0110     void selectQuickTool(int toolId);
0111     // selects the last used tool
0112     void selectLastTool();
0113     // deselects the tool and uncheck all the annotation actions
0114     void detachAnnotation();
0115 
0116     // returns the builtin annotation tool with the given Id
0117     QDomElement builtinTool(int toolId);
0118     // returns the quick annotation tool with the given Id
0119     QDomElement quickTool(int toolId);
0120 
0121     // methods that write the properties
0122     void setAnnotationWidth(double width);
0123     void setAnnotationColor(const QColor &color);
0124     void setAnnotationInnerColor(const QColor &color);
0125     void setAnnotationOpacity(double opacity);
0126     void setAnnotationFont(const QFont &font);
0127 
0128 public Q_SLOTS:
0129     void setContinuousMode(bool enabled);
0130     /**
0131      * State of constrain ratio and angle action.
0132      * While annotating, this value is XOR-ed with the Shift modifier.
0133      */
0134     void setConstrainRatioAndAngle(bool enabled);
0135     void addToQuickAnnotations();
0136     void slotAdvancedSettings();
0137 
0138 Q_SIGNALS:
0139     /**
0140      * This signal is emitted whenever an annotation tool is activated or all the tools get deactivated
0141      */
0142     void toolActive(bool active);
0143     void requestOpenFile(const QString &filePath, int pageNumber);
0144 
0145 private:
0146     void reparseBuiltinToolsConfig();
0147     void reparseQuickToolsConfig();
0148     // save the builtin annotation tools to Okular settings
0149     void saveBuiltinAnnotationTools();
0150     // selects the active tool
0151     void selectTool(AnnotationTools *toolsDefinition, int toolId, ShowTip showTip);
0152     // returns the engine QDomElement of the the currently active tool
0153     QDomElement currentEngineElement();
0154     // returns the annotation QDomElement of the the currently active tool
0155     QDomElement currentAnnotationElement();
0156 
0157     // global class pointers
0158     Okular::Document *m_document;
0159     PageView *m_pageView;
0160     AnnotationActionHandler *m_actionHandler;
0161     AnnotatorEngine *m_engine;
0162     AnnotationTools *m_builtinToolsDefinition;
0163     AnnotationTools *m_quickToolsDefinition;
0164     bool m_continuousMode;
0165     bool m_constrainRatioAndAngle;
0166     bool m_signatureMode;
0167 
0168     // creation related variables
0169     AnnotationTools *m_lastToolsDefinition;
0170     int m_lastToolId;
0171     QRect m_lastDrawnRect;
0172     PageViewItem *m_lockedItem;
0173     // selected annotation name
0174     // QString m_selectedAnnotationName;
0175 };
0176 
0177 #endif
0178 
0179 /* kate: replace-tabs on; indent-width 4; */