1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29//
30// W A R N I N G
31// -------------
32//
33// This file is not part of the Qt API. It exists for the convenience
34// of Qt Designer. This header
35// file may change from version to version without notice, or even be removed.
36//
37// We mean it.
38//
39
40#ifndef ZOOMWIDGET_H
41#define ZOOMWIDGET_H
42
43#include "shared_global_p.h"
44
45#include <QtWidgets/qgraphicsview.h>
46#include <QtWidgets/qgraphicsproxywidget.h>
47#include <QtCore/qvector.h>
48
49QT_BEGIN_NAMESPACE
50
51class QGraphicsScene;
52class QMenu;
53class QAction;
54class QActionGroup;
55
56namespace qdesigner_internal {
57
58// A checkable zoom menu action group. Operates in percent.
59
60class QDESIGNER_SHARED_EXPORT ZoomMenu : public QObject {
61 Q_OBJECT
62 Q_DISABLE_COPY_MOVE(ZoomMenu)
63
64public:
65 ZoomMenu(QObject *parent = nullptr);
66 void addActions(QMenu *m);
67
68 int zoom() const;
69
70 // Return a list of available zoom values.
71 static QVector<int> zoomValues();
72
73public slots:
74 void setZoom(int percent);
75
76signals:
77 void zoomChanged(int);
78
79private slots:
80 void slotZoomMenu(QAction *);
81
82private:
83 static int zoomOf(const QAction *a);
84
85 QActionGroup *m_menuActions;
86};
87
88/* Zoom view: A QGraphicsView with a zoom menu */
89
90class QDESIGNER_SHARED_EXPORT ZoomView : public QGraphicsView
91{
92 Q_PROPERTY(int zoom READ zoom WRITE setZoom DESIGNABLE true SCRIPTABLE true)
93 Q_PROPERTY(bool zoomContextMenuEnabled READ isZoomContextMenuEnabled WRITE setZoomContextMenuEnabled DESIGNABLE true SCRIPTABLE true)
94 Q_OBJECT
95 Q_DISABLE_COPY_MOVE(ZoomView)
96public:
97 ZoomView(QWidget *parent = nullptr);
98
99 /* Zoom in percent (for easily implementing menus) and qreal zoomFactor
100 * in sync */
101 int zoom() const; // in percent
102 qreal zoomFactor() const;
103
104 // Zoom Menu on QGraphicsView.
105 bool isZoomContextMenuEnabled() const;
106 void setZoomContextMenuEnabled(bool e);
107
108 QGraphicsScene &scene() { return *m_scene; }
109 const QGraphicsScene &scene() const { return *m_scene; }
110
111 // Helpers for menu
112 ZoomMenu *zoomMenu();
113
114 QPoint scrollPosition() const;
115 void setScrollPosition(const QPoint& pos);
116 void scrollToOrigin();
117
118public slots:
119 void setZoom(int percent);
120 void showContextMenu(const QPoint &globalPos);
121
122protected:
123 void contextMenuEvent(QContextMenuEvent *event) override;
124
125 // Overwrite for implementing additional behaviour when doing setZoom();
126 virtual void applyZoom();
127
128private:
129 QGraphicsScene *m_scene;
130 int m_zoom = 100;
131 qreal m_zoomFactor = 1;
132
133 bool m_zoomContextMenuEnabled = false;
134 ZoomMenu *m_zoomMenu = nullptr;
135};
136
137/* The proxy widget used in ZoomWidget. It refuses to move away from 0,0,
138 * This behaviour is required for Windows only. */
139
140class QDESIGNER_SHARED_EXPORT ZoomProxyWidget : public QGraphicsProxyWidget {
141 Q_DISABLE_COPY_MOVE(ZoomProxyWidget)
142public:
143 explicit ZoomProxyWidget(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = {});
144
145protected:
146 QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
147};
148
149/* Zoom widget: A QGraphicsView-based container for a widget that allows for
150 * zooming it. Communicates changes in size in the following ways:
151 * 1) Embedded widget wants to resize: Listen for its resize in event filter
152 * and resize
153 * 2) Zoom is changed: resize to fully display the embedded widget
154 * 3) Outer widget changes (by manually resizing the window:
155 * Pass the scaled change on to the embedded widget.
156 * Provides helper functions for a zoom context menu on the widget. */
157
158class QDESIGNER_SHARED_EXPORT ZoomWidget : public ZoomView
159{
160 Q_PROPERTY(bool widgetZoomContextMenuEnabled READ isWidgetZoomContextMenuEnabled WRITE setWidgetZoomContextMenuEnabled DESIGNABLE true SCRIPTABLE true)
161 Q_PROPERTY(bool itemAcceptDrops READ itemAcceptDrops WRITE setItemAcceptDrops DESIGNABLE true SCRIPTABLE true)
162 Q_OBJECT
163 Q_DISABLE_COPY_MOVE(ZoomWidget)
164
165public:
166 ZoomWidget(QWidget *parent = nullptr);
167 void setWidget(QWidget *w, Qt::WindowFlags wFlags = {});
168
169 const QGraphicsProxyWidget *proxy() const { return m_proxy; }
170 QGraphicsProxyWidget *proxy() { return m_proxy; }
171
172 /* Enable the zoom Menu on the Widget (as opposed ZoomView's menu which
173 * is on the canvas). */
174 bool isWidgetZoomContextMenuEnabled() const;
175 void setWidgetZoomContextMenuEnabled(bool e);
176
177 void setItemAcceptDrops(bool);
178 bool itemAcceptDrops() const;
179
180 QSize minimumSizeHint() const override;
181 QSize sizeHint() const override;
182
183 bool zoomedEventFilter(QObject *watched, QEvent *event);
184
185public slots:
186 // debug state
187 void dump() const;
188
189protected:
190 void resizeEvent(QResizeEvent * event) override;
191
192 // Overwritten from ZoomView
193 void applyZoom() override;
194 // Overwrite to actually perform a resize. This is required if we are in a layout. Default does resize().
195 virtual void doResize(const QSize &s);
196
197private:
198 // Factory function for QGraphicsProxyWidgets which can be overwritten. Default creates a ZoomProxyWidget
199 virtual QGraphicsProxyWidget *createProxyWidget(QGraphicsItem *parent = nullptr,
200 Qt::WindowFlags wFlags = {}) const;
201 QSize widgetSizeToViewSize(const QSize &s, bool *ptrToValid = nullptr) const;
202
203 void resizeToWidgetSize();
204 QSize viewPortMargin() const;
205 QSize widgetSize() const;
206 QSizeF widgetDecorationSizeF() const;
207
208 QGraphicsProxyWidget *m_proxy = nullptr;
209 bool m_viewResizeBlocked = false;
210 bool m_widgetResizeBlocked = false;
211 bool m_widgetZoomContextMenuEnabled = false;
212};
213
214} // namespace qdesigner_internal
215
216QT_END_NAMESPACE
217
218#endif
219

source code of qttools/src/designer/src/lib/shared/zoomwidget_p.h