File indexing completed on 2024-05-19 04:36:35

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013-2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef TIKZ_UI_PATH_HANDLE_H
0021 #define TIKZ_UI_PATH_HANDLE_H
0022 
0023 #include <tikz/core/tikz.h>
0024 
0025 #include "TikzItem.h"
0026 
0027 class QPainter;
0028 class QGraphicsView;
0029 
0030 namespace tikz {
0031 namespace ui {
0032 
0033 class Handle : public TikzItem
0034 {
0035     Q_OBJECT
0036 
0037     public:
0038         enum Position {
0039             TopLeftCorner = 0,
0040             TopRightCorner,
0041             BottomLeftCorner,
0042             BottomRightCorner,
0043 
0044             LeftBorder,
0045             TopBorder,
0046             RightBorder,
0047             BottomBorder,
0048 
0049             ResizePos,
0050 
0051             Center,
0052 
0053             // used for lines
0054             StartPos,
0055             EndPos,
0056 
0057             UserPos
0058         };
0059 
0060         enum Type {
0061             MoveHandle,
0062             ResizeHandle,
0063             RotateHandle,
0064             AnchorHandle
0065         };
0066 
0067     public:
0068         /**
0069          * Constructor.
0070          */
0071         Handle(Type type = ResizeHandle, Position position = Position::UserPos);
0072 
0073         /**
0074          * Destructor
0075          */
0076         virtual ~Handle();
0077 
0078         /**
0079          * Reimplment to return a proper UserType + 5.
0080          */
0081         int type() const override;
0082 
0083         /**
0084          * Get the handle position.
0085          */
0086         Position handlePos() const;
0087 
0088         /**
0089          * Get the handle type.
0090          */
0091         Type handleType() const;
0092 
0093     //
0094     // geometry
0095     //
0096     public:
0097         /**
0098          * Returns the rect in local coordinates
0099          */
0100         QRectF rect() const;
0101 
0102     public Q_SLOTS:
0103         /**
0104          * Sets this Handle's rect to @p rect
0105          */
0106         void setRect(const QRectF & rect);
0107 
0108     //
0109     // active flag
0110     //
0111     public:
0112         /**
0113          * Check whether the handle is set active or not.
0114          *
0115          * The handle is active either when the mouse is pressed, or if you
0116          * manually call activate().
0117          *
0118          * Depending on isActive(), handles can change the appearance.
0119          *
0120          * @see activate(), deactivate()
0121          */
0122         bool isActive() const;
0123 
0124     public Q_SLOTS:
0125         /**
0126          * Activates this item.
0127          *
0128          * @see isActive()
0129          */
0130         void activate();
0131 
0132         /**
0133          * Deactivates this item.
0134          *
0135          * @see isActive()
0136          */
0137         void deactivate();
0138 
0139     Q_SIGNALS:
0140         /**
0141          * This signal is emitted whenever the position of this handle
0142          * changed through user interaction.
0143          * @param handle the handle object that sent this signal
0144          * @param pos position in scene coordinates
0145          * @param view the view the user interacted with
0146          */
0147         void positionChanged(tikz::ui::Handle * handle, const QPointF & pos, QGraphicsView * view);
0148 
0149         /**
0150          * This signal is emitted whenever the mouse is pressed on the handle.
0151          * @param handle the handle object that sent this signal
0152          * @param pos position in scene coordinates
0153          * @param view the view the user interacted with
0154          */
0155         void mousePressed(tikz::ui::Handle * handle, const QPointF & pos, QGraphicsView * view);
0156 
0157         /**
0158          * This signal is emitted whenever the mouse is released on the handle.
0159          * @param handle the handle object that sent this signal
0160          * @param pos position in scene coordinates
0161          * @param view the view the user interacted with
0162          */
0163         void mouseReleased(tikz::ui::Handle * handle, const QPointF & pos, QGraphicsView * view);
0164 
0165     //
0166     // reimplemented from QGraphicsItem
0167     //
0168     public:
0169         /**
0170          * Paint this item.
0171          */
0172         void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
0173 
0174         /**
0175          * Returns the bounding rect of this item.
0176          */
0177         QRectF boundingRect() const override;
0178 
0179     //
0180     // protected overrides
0181     //
0182     protected:
0183         /**
0184          * Reimplement to emit signal mousePressed()
0185          */
0186         void mouseMoveEvent(QGraphicsSceneMouseEvent * event) override;
0187 
0188         /**
0189          * Reimplement to emit signal positionChanged()
0190          */
0191         void mousePressEvent(QGraphicsSceneMouseEvent * event) override;
0192 
0193         /**
0194          * Reimplement to emit signal mouseReleased()
0195          */
0196         void mouseReleaseEvent(QGraphicsSceneMouseEvent * event) override;
0197 
0198     //
0199     // private data
0200     //
0201     private:
0202         Type m_type;
0203         Position m_position;
0204         QRectF m_handleRect;
0205         bool m_active = false;
0206 };
0207 
0208 }
0209 }
0210 
0211 #endif // TIKZ_UI_PATH_HANDLE_H
0212 
0213 // kate: indent-width 4; replace-tabs on;