| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QGRAPHICSWIDGET_H |
| 5 | #define QGRAPHICSWIDGET_H |
| 6 | |
| 7 | #include <QtWidgets/qtwidgetsglobal.h> |
| 8 | #include <QtGui/qfont.h> |
| 9 | #if QT_CONFIG(action) |
| 10 | # include <QtGui/qaction.h> |
| 11 | #endif |
| 12 | #include <QtWidgets/qgraphicslayoutitem.h> |
| 13 | #include <QtWidgets/qgraphicsitem.h> |
| 14 | #include <QtGui/qpalette.h> |
| 15 | |
| 16 | QT_REQUIRE_CONFIG(graphicsview); |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | class QFont; |
| 21 | class QFontMetrics; |
| 22 | class QGraphicsLayout; |
| 23 | class QGraphicsSceneMoveEvent; |
| 24 | class QGraphicsWidgetPrivate; |
| 25 | class QGraphicsSceneResizeEvent; |
| 26 | class QStyle; |
| 27 | class QStyleOption; |
| 28 | |
| 29 | class QGraphicsWidgetPrivate; |
| 30 | |
| 31 | class Q_WIDGETS_EXPORT QGraphicsWidget : public QGraphicsObject, public QGraphicsLayoutItem |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | Q_INTERFACES(QGraphicsItem QGraphicsLayoutItem) |
| 35 | Q_PROPERTY(QPalette palette READ palette WRITE setPalette) |
| 36 | Q_PROPERTY(QFont font READ font WRITE setFont) |
| 37 | Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection |
| 38 | RESET unsetLayoutDirection) |
| 39 | Q_PROPERTY(QSizeF size READ size WRITE resize NOTIFY geometryChanged) |
| 40 | Q_PROPERTY(QSizeF minimumSize READ minimumSize WRITE setMinimumSize) |
| 41 | Q_PROPERTY(QSizeF preferredSize READ preferredSize WRITE setPreferredSize) |
| 42 | Q_PROPERTY(QSizeF maximumSize READ maximumSize WRITE setMaximumSize) |
| 43 | Q_PROPERTY(QSizePolicy sizePolicy READ sizePolicy WRITE setSizePolicy) |
| 44 | Q_PROPERTY(Qt::FocusPolicy focusPolicy READ focusPolicy WRITE setFocusPolicy) |
| 45 | Q_PROPERTY(Qt::WindowFlags windowFlags READ windowFlags WRITE setWindowFlags) |
| 46 | Q_PROPERTY(QString windowTitle READ windowTitle WRITE setWindowTitle) |
| 47 | Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry NOTIFY geometryChanged) |
| 48 | Q_PROPERTY(bool autoFillBackground READ autoFillBackground WRITE setAutoFillBackground) |
| 49 | Q_PROPERTY(QGraphicsLayout* layout READ layout WRITE setLayout NOTIFY layoutChanged) |
| 50 | public: |
| 51 | QGraphicsWidget(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags()); |
| 52 | ~QGraphicsWidget(); |
| 53 | QGraphicsLayout *layout() const; |
| 54 | void setLayout(QGraphicsLayout *layout); |
| 55 | void adjustSize(); |
| 56 | |
| 57 | Qt::LayoutDirection layoutDirection() const; |
| 58 | void setLayoutDirection(Qt::LayoutDirection direction); |
| 59 | void unsetLayoutDirection(); |
| 60 | |
| 61 | QStyle *style() const; |
| 62 | void setStyle(QStyle *style); |
| 63 | |
| 64 | QFont font() const; |
| 65 | void setFont(const QFont &font); |
| 66 | |
| 67 | QPalette palette() const; |
| 68 | void setPalette(const QPalette &palette); |
| 69 | |
| 70 | bool autoFillBackground() const; |
| 71 | void setAutoFillBackground(bool enabled); |
| 72 | |
| 73 | void resize(const QSizeF &size); |
| 74 | inline void resize(qreal w, qreal h) { resize(size: QSizeF(w, h)); } |
| 75 | QSizeF size() const; |
| 76 | |
| 77 | void setGeometry(const QRectF &rect) override; |
| 78 | inline void setGeometry(qreal x, qreal y, qreal w, qreal h); |
| 79 | inline QRectF rect() const { return QRectF(QPointF(), size()); } |
| 80 | |
| 81 | void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom); |
| 82 | void setContentsMargins(QMarginsF margins); |
| 83 | void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const override; |
| 84 | |
| 85 | void setWindowFrameMargins(qreal left, qreal top, qreal right, qreal bottom); |
| 86 | void setWindowFrameMargins(QMarginsF margins); |
| 87 | void getWindowFrameMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const; |
| 88 | void unsetWindowFrameMargins(); |
| 89 | QRectF windowFrameGeometry() const; |
| 90 | QRectF windowFrameRect() const; |
| 91 | |
| 92 | // Window handling |
| 93 | Qt::WindowFlags windowFlags() const; |
| 94 | Qt::WindowType windowType() const; |
| 95 | void setWindowFlags(Qt::WindowFlags wFlags); |
| 96 | bool isActiveWindow() const; |
| 97 | void setWindowTitle(const QString &title); |
| 98 | QString windowTitle() const; |
| 99 | |
| 100 | // Focus handling |
| 101 | Qt::FocusPolicy focusPolicy() const; |
| 102 | void setFocusPolicy(Qt::FocusPolicy policy); |
| 103 | static void setTabOrder(QGraphicsWidget *first, QGraphicsWidget *second); |
| 104 | QGraphicsWidget *focusWidget() const; |
| 105 | |
| 106 | #ifndef QT_NO_SHORTCUT |
| 107 | int grabShortcut(const QKeySequence &sequence, Qt::ShortcutContext context = Qt::WindowShortcut); |
| 108 | void releaseShortcut(int id); |
| 109 | void setShortcutEnabled(int id, bool enabled = true); |
| 110 | void setShortcutAutoRepeat(int id, bool enabled = true); |
| 111 | #endif |
| 112 | |
| 113 | #ifndef QT_NO_ACTION |
| 114 | //actions |
| 115 | void addAction(QAction *action); |
| 116 | void addActions(const QList<QAction*> &actions); |
| 117 | void insertActions(QAction *before, const QList<QAction*> &actions); |
| 118 | void insertAction(QAction *before, QAction *action); |
| 119 | void removeAction(QAction *action); |
| 120 | QList<QAction*> actions() const; |
| 121 | #endif |
| 122 | |
| 123 | void setAttribute(Qt::WidgetAttribute attribute, bool on = true); |
| 124 | bool testAttribute(Qt::WidgetAttribute attribute) const; |
| 125 | |
| 126 | enum { |
| 127 | Type = 11 |
| 128 | }; |
| 129 | int type() const override; |
| 130 | |
| 131 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override; |
| 132 | virtual void paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr); |
| 133 | QRectF boundingRect() const override; |
| 134 | QPainterPath shape() const override; |
| 135 | |
| 136 | #if 0 |
| 137 | void dumpFocusChain(); |
| 138 | #endif |
| 139 | |
| 140 | using QObject::children; |
| 141 | |
| 142 | Q_SIGNALS: |
| 143 | void geometryChanged(); |
| 144 | void layoutChanged(); |
| 145 | |
| 146 | public Q_SLOTS: |
| 147 | bool close(); |
| 148 | |
| 149 | protected: |
| 150 | virtual void initStyleOption(QStyleOption *option) const; |
| 151 | |
| 152 | QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override; |
| 153 | void updateGeometry() override; |
| 154 | |
| 155 | // Notification |
| 156 | QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; |
| 157 | virtual QVariant propertyChange(const QString &propertyName, const QVariant &value); |
| 158 | |
| 159 | // Scene events |
| 160 | bool sceneEvent(QEvent *event) override; |
| 161 | virtual bool windowFrameEvent(QEvent *e); |
| 162 | virtual Qt::WindowFrameSection windowFrameSectionAt(const QPointF& pos) const; |
| 163 | |
| 164 | // Base event handlers |
| 165 | bool event(QEvent *event) override; |
| 166 | //virtual void actionEvent(QActionEvent *event); |
| 167 | virtual void changeEvent(QEvent *event); |
| 168 | virtual void closeEvent(QCloseEvent *event); |
| 169 | //void create(WId window = 0, bool initializeWindow = true, bool destroyOldWindow = true); |
| 170 | //void destroy(bool destroyWindow = true, bool destroySubWindows = true); |
| 171 | void focusInEvent(QFocusEvent *event) override; |
| 172 | virtual bool focusNextPrevChild(bool next); |
| 173 | void focusOutEvent(QFocusEvent *event) override; |
| 174 | virtual void hideEvent(QHideEvent *event); |
| 175 | //virtual int metric(PaintDeviceMetric m ) const; |
| 176 | virtual void moveEvent(QGraphicsSceneMoveEvent *event); |
| 177 | virtual void polishEvent(); |
| 178 | //void resetInputContext (); |
| 179 | virtual void resizeEvent(QGraphicsSceneResizeEvent *event); |
| 180 | virtual void showEvent(QShowEvent *event); |
| 181 | //virtual void tabletEvent(QTabletEvent *event); |
| 182 | virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override; |
| 183 | virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override; |
| 184 | virtual void grabMouseEvent(QEvent *event); |
| 185 | virtual void ungrabMouseEvent(QEvent *event); |
| 186 | virtual void grabKeyboardEvent(QEvent *event); |
| 187 | virtual void ungrabKeyboardEvent(QEvent *event); |
| 188 | QGraphicsWidget(QGraphicsWidgetPrivate &, QGraphicsItem *parent, Qt::WindowFlags wFlags = Qt::WindowFlags()); |
| 189 | |
| 190 | private: |
| 191 | Q_DISABLE_COPY(QGraphicsWidget) |
| 192 | Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QGraphicsWidget) |
| 193 | friend class QGraphicsScene; |
| 194 | friend class QGraphicsScenePrivate; |
| 195 | friend class QGraphicsView; |
| 196 | friend class QGraphicsItem; |
| 197 | friend class QGraphicsItemPrivate; |
| 198 | friend class QGraphicsLayout; |
| 199 | friend class QWidget; |
| 200 | friend class QApplication; |
| 201 | }; |
| 202 | |
| 203 | inline void QGraphicsWidget::setGeometry(qreal ax, qreal ay, qreal aw, qreal ah) |
| 204 | { setGeometry(QRectF(ax, ay, aw, ah)); } |
| 205 | |
| 206 | QT_END_NAMESPACE |
| 207 | |
| 208 | #endif |
| 209 | |
| 210 | |