File indexing completed on 2024-05-19 15:27:46

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 /*
0020  * Canvas Subgraph (subgraph node view)
0021  */
0022 
0023 #ifndef CANVAS_ELEMENT_H
0024 #define CANVAS_ELEMENT_H
0025 
0026 #include <QAbstractGraphicsShapeItem>
0027 #include <QBrush>
0028 #include <QPen>
0029 
0030 #include "dotgrammar.h"
0031 
0032 class QMenu;
0033 class QGraphicsScene;
0034 
0035 namespace KGraphViewer
0036 {
0037 class GraphElement;
0038 class DotGraphView;
0039 
0040 class CanvasElement : public QObject, public QAbstractGraphicsShapeItem
0041 {
0042     Q_OBJECT
0043 public:
0044     CanvasElement(DotGraphView *v, GraphElement *s, QGraphicsScene *c, QGraphicsItem *parent = nullptr);
0045 
0046     ~CanvasElement() override;
0047 
0048     GraphElement *element()
0049     {
0050         return m_element;
0051     }
0052 
0053     void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
0054 
0055     QRectF boundingRect() const override;
0056 
0057     void computeBoundingRect();
0058 
0059     void initialize(qreal scaleX, qreal scaleY, qreal xMargin, qreal yMargin, qreal gh);
0060 
0061     inline void setGh(qreal gh)
0062     {
0063         m_gh = gh;
0064     }
0065 
0066 protected:
0067     void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
0068     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
0069     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
0070     void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
0071     void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
0072 
0073     qreal m_scaleX, m_scaleY;
0074     qreal m_xMargin, m_yMargin, m_gh;
0075     GraphElement *m_element;
0076     DotGraphView *m_view;
0077     QFont *m_font;
0078     QPen m_pen;
0079     QBrush m_brush;
0080     QRectF m_boundingRect;
0081     QMenu *m_popup;
0082 
0083     bool m_hovered;
0084 
0085     quint32 m_lastRenderOpRev;
0086     typedef QHash<int, QPair<int, int>> FontSizeCache;
0087     FontSizeCache m_fontSizeCache;
0088 Q_SIGNALS:
0089     void selected(CanvasElement *, Qt::KeyboardModifiers);
0090     void elementContextMenuEvent(const QString &, const QPoint &);
0091     void hoverEnter(CanvasElement *);
0092     void hoverLeave(CanvasElement *);
0093 
0094 public Q_SLOTS:
0095     void modelChanged();
0096     void slotRemoveElement();
0097 };
0098 
0099 }
0100 
0101 #endif // CANVAS_ELEMENT_H