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