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

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 /* This file was callgraphview.h, part of KCachegrind.
0020    Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0021 
0022    KCachegrind is free software; you can redistribute it and/or
0023    modify it under the terms of the GNU General Public
0024    License as published by the Free Software Foundation, version 2.
0025 */
0026 
0027 /*
0028  * Callgraph View
0029  */
0030 
0031 #ifndef DOTGRAPHVIEW_H
0032 #define DOTGRAPHVIEW_H
0033 
0034 #include <kactioncollection.h>
0035 #include <kconfig.h>
0036 #include <kconfiggroup.h>
0037 
0038 #include <QGraphicsView>
0039 #include <QSet>
0040 
0041 #include <graphviz/types.h>
0042 
0043 #include "kgraphviewer_export.h"
0044 #include "kgraphviewer_interface.h"
0045 
0046 class KSelectAction;
0047 
0048 class QKeyEvent;
0049 class QMouseEvent;
0050 class QFocusEvent;
0051 class QResizeEvent;
0052 class QWheelEvent;
0053 class QContextMenuEvent;
0054 class QWidget;
0055 
0056 namespace KGraphViewer
0057 {
0058 class GraphElement;
0059 class CanvasElement;
0060 class CanvasEdge;
0061 class DotGraph;
0062 
0063 #define DEFAULT_DETAILLEVEL 1
0064 
0065 class DotGraphViewPrivate;
0066 /**
0067  * A CanvasView showing a part of the call graph
0068  * and another zoomed out CanvasView in a border acting as
0069  * a panner to select to visible part (only if needed)
0070  */
0071 class KGRAPHVIEWER_EXPORT DotGraphView : public QGraphicsView
0072 {
0073     Q_OBJECT
0074 
0075 public:
0076     enum EditingMode { None, AddNewElement, AddNewEdge, DrawNewEdge, SelectingElements };
0077     enum ScrollDirection { Here, Left, Right, Top, Bottom };
0078 
0079     explicit KGRAPHVIEWER_EXPORT DotGraphView(KActionCollection *actions, QWidget *parent = nullptr);
0080     ~DotGraphView() override;
0081 
0082     bool KGRAPHVIEWER_EXPORT loadDot(const QString &dotFileName);
0083     bool KGRAPHVIEWER_EXPORT loadLibrary(const QString &dotFileName);
0084     bool KGRAPHVIEWER_EXPORT loadLibrarySync(const QString &dotFileName);
0085     bool loadLibrary(graph_t *graph, const QString &layoutCommand = "dot");
0086 
0087     void readViewConfig();
0088     void saveViewConfig();
0089 
0090     // TODO: rename zoomPos -> bev / panner, but _please_ make it consistent...
0091     KGraphViewerInterface::PannerPosition zoomPos() const;
0092     static KGraphViewerInterface::PannerPosition zoomPos(const QString &);
0093     static QString zoomPosString(KGraphViewerInterface::PannerPosition);
0094     void setPannerEnabled(bool enabled);
0095 
0096     static KConfigGroup *configGroup(KConfig *, const QString &prefix, const QString &postfix);
0097     static void writeConfigEntry(KConfigGroup *, const char *pKey, const QString &value, const char *def);
0098     static void writeConfigEntry(KConfigGroup *, const char *pKey, int value, int def);
0099     static void writeConfigEntry(KConfigGroup *, const char *pKey, bool value, bool def);
0100     static void writeConfigEntry(KConfigGroup *, const char *pKey, double value, double def);
0101 
0102     /// multiplies current zoom factor with @p factor
0103     void applyZoom(double factor);
0104     /// sets zoom factor to @p factor
0105     void setZoomFactor(double factor);
0106 
0107     void setLayoutCommand(const QString &command);
0108 
0109     const QString &dotFileName();
0110 
0111     void hideToolsWindows();
0112     double zoom() const;
0113     KSelectAction *bevPopup();
0114 
0115     DotGraph *graph();
0116     const DotGraph *graph() const;
0117 
0118     const GraphElement *defaultNewElement() const;
0119     QPixmap defaultNewElementPixmap() const;
0120 
0121     void setDefaultNewElement(GraphElement *elem);
0122     void setDefaultNewElementPixmap(const QPixmap &pm);
0123 
0124     void prepareAddNewElement(QMap<QString, QString> attribs);
0125     void prepareAddNewEdge(QMap<QString, QString> attribs);
0126     void prepareSelectElements();
0127 
0128     void createNewEdgeDraftFrom(CanvasElement *node);
0129     void finishNewEdgeTo(CanvasElement *node);
0130 
0131     EditingMode editingMode() const;
0132 
0133     void KGRAPHVIEWER_EXPORT setReadOnly();
0134     void KGRAPHVIEWER_EXPORT setReadWrite();
0135     bool isReadWrite() const;
0136     bool isReadOnly() const;
0137 
0138     void removeSelectedNodes();
0139     void removeSelectedEdges();
0140     void removeSelectedSubgraphs();
0141     void removeSelectedElements();
0142 
0143     bool highlighting();
0144     void setHighlighting(bool highlightingValue);
0145 
0146     // public so that the panner view can bubble through
0147     void contextMenuEvent(QContextMenuEvent *) override;
0148 
0149     void setBackgroundColor(const QColor &color);
0150 
0151 Q_SIGNALS:
0152     void zoomed(double factor);
0153     void sigViewBevEnabledToggled(bool value);
0154     void sigViewBevActivated(int newPos);
0155     void graphLoaded();
0156     void newNodeAdded(const QString &);
0157     void newEdgeAdded(const QString &, const QString &);
0158     /** signals that the user has activated a remove edge command */
0159     void removeEdge(const QString &);
0160     /** signals that the user has activated a remove edge command */
0161     void removeNodeNamed(const QString &);
0162     /** signals that the user has activated a remove element command */
0163     void removeElement(const QString &);
0164     /** signals the content of the new selection */
0165     void selectionIs(const QList<QString>, const QPoint &);
0166     /** let the application tweak the created edge if necessary */
0167     void newEdgeFinished(const QString &, const QString &, const QMap<QString, QString> &);
0168     void contextMenuEvent(const QString &, const QPoint &);
0169     void hoverEnter(const QString &);
0170     void hoverLeave(const QString &);
0171 
0172 public Q_SLOTS:
0173     void zoomIn();
0174     void zoomOut();
0175     void zoomRectMovedTo(QPointF newZoomPos);
0176     void zoomRectMoveFinished();
0177     bool initEmpty();
0178     bool slotLoadLibrary(graph_t *graph);
0179     bool reload();
0180     void dirty(const QString &dotFileName);
0181     void pageSetup();
0182     void print();
0183     void printPreview();
0184     void viewBevActivated(int newPos);
0185     void slotExportImage();
0186     void slotSelectLayoutAlgo(const QString &text);
0187     void slotLayoutSpecify();
0188     void slotLayoutReset();
0189     void slotSelectLayoutDot();
0190     void slotSelectLayoutNeato();
0191     void slotSelectLayoutTwopi();
0192     void slotSelectLayoutFdp();
0193     void slotSelectLayoutCirco();
0194     void slotBevToggled();
0195     void slotBevTopLeft();
0196     void slotBevTopRight();
0197     void slotBevBottomLeft();
0198     void slotBevBottomRight();
0199     void slotBevAutomatic();
0200     void slotUpdate();
0201     bool displayGraph();
0202     void slotEdgeSelected(CanvasEdge *, Qt::KeyboardModifiers);
0203     void slotElementSelected(CanvasElement *, Qt::KeyboardModifiers);
0204     void slotSelectionChanged();
0205     void slotContextMenuEvent(const QString &, const QPoint &);
0206     void slotElementHoverEnter(CanvasElement *);
0207     void slotElementHoverLeave(CanvasElement *);
0208     void slotElementHoverEnter(CanvasEdge *);
0209     void slotElementHoverLeave(CanvasEdge *);
0210     void slotSelectNode(const QString &nodeName);
0211     void centerOnNode(const QString &nodeId);
0212 
0213 protected:
0214     void scrollViewPercent(bool horizontal, int percent);
0215 
0216     void scrollContentsBy(int dx, int dy) override;
0217     void resizeEvent(QResizeEvent *) override;
0218     void mousePressEvent(QMouseEvent *) override;
0219     void mouseMoveEvent(QMouseEvent *) override;
0220     void mouseReleaseEvent(QMouseEvent *) override;
0221     void mouseDoubleClickEvent(QMouseEvent *) override;
0222     void keyPressEvent(QKeyEvent *) override;
0223     void wheelEvent(QWheelEvent *e) override;
0224     void focusInEvent(QFocusEvent *) override;
0225     void focusOutEvent(QFocusEvent *) override;
0226 
0227     void timerEvent(QTimerEvent *event) override;
0228     void leaveEvent(QEvent *event) override;
0229 
0230 #if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
0231     void enterEvent(QEnterEvent *event) override;
0232 #else
0233     void enterEvent(QEvent *event) override;
0234 #endif
0235 
0236 private Q_SLOTS:
0237     void slotAGraphReadFinished();
0238     void slotAGraphLayoutFinished();
0239 
0240 protected:
0241     DotGraphViewPrivate *const d_ptr;
0242 
0243 private:
0244     Q_DECLARE_PRIVATE(DotGraphView);
0245 };
0246 
0247 }
0248 
0249 #endif // DOTGRAPHVIEW_H