File indexing completed on 2024-05-12 03:48:20

0001 /*
0002     File                 : ResizeItem.h
0003     Project              : LabPlot
0004     Description          : Item allowing to resize worksheet elements with the mouse
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2021 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef RESIZEITEM_H
0011 #define RESIZEITEM_H
0012 
0013 #include <QGraphicsItem>
0014 #include <QGraphicsRectItem>
0015 
0016 class WorksheetElementContainer;
0017 class QGraphicsSceneHoverEvent;
0018 
0019 class ResizeItem : public QGraphicsItem {
0020 public:
0021     explicit ResizeItem(WorksheetElementContainer*);
0022     virtual ~ResizeItem();
0023     void setRect(QRectF);
0024 
0025 private:
0026     enum Position {
0027         Top = 0x1,
0028         Bottom = 0x2,
0029         Left = 0x4,
0030         TopLeft = Top | Left,
0031         BottomLeft = Bottom | Left,
0032         Right = 0x8,
0033         TopRight = Top | Right,
0034         BottomRight = Bottom | Right
0035     };
0036 
0037     class HandleItem : public QGraphicsRectItem {
0038     public:
0039         HandleItem(int positionFlags, ResizeItem*);
0040         int position() const;
0041 
0042     protected:
0043         QVariant itemChange(GraphicsItemChange, const QVariant&) override;
0044         void mousePressEvent(QGraphicsSceneMouseEvent*) override;
0045         void mouseReleaseEvent(QGraphicsSceneMouseEvent*) override;
0046         void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
0047         void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
0048 
0049     private:
0050         QPointF restrictPosition(const QPointF&);
0051         int m_position;
0052         ResizeItem* m_parent;
0053         QRectF m_oldRect;
0054     };
0055 
0056     QRectF boundingRect() const override;
0057     void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget* widget = nullptr) override;
0058     WorksheetElementContainer* container();
0059 
0060     void setTopLeft(const QPointF&);
0061     void setTop(qreal);
0062     void setTopRight(const QPointF&);
0063     void setRight(qreal);
0064     void setBottomRight(const QPointF&);
0065     void setBottom(qreal);
0066     void setBottomLeft(const QPointF&);
0067     void setLeft(qreal);
0068 
0069 private:
0070     void updateHandleItemPositions();
0071 
0072     QVector<HandleItem*> m_handleItems;
0073     QRectF m_rect;
0074     WorksheetElementContainer* m_container;
0075 };
0076 
0077 #endif // SIZEGRIPITEM_H