tools/designer/src/lib/shared/connectionedit_p.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
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 
       
    54 #ifndef CONNECTIONEDIT_H
       
    55 #define CONNECTIONEDIT_H
       
    56 
       
    57 #include "shared_global_p.h"
       
    58 
       
    59 #include <QtCore/QMultiMap>
       
    60 #include <QtCore/QList>
       
    61 #include <QtCore/QPointer>
       
    62 
       
    63 #include <QtGui/QWidget>
       
    64 #include <QtGui/QPixmap>
       
    65 #include <QtGui/QPolygonF>
       
    66 
       
    67 #include <QtGui/QUndoCommand>
       
    68 
       
    69 QT_BEGIN_NAMESPACE
       
    70 
       
    71 class QDesignerFormWindowInterface;
       
    72 class QUndoStack;
       
    73 class QMenu;
       
    74 
       
    75 namespace qdesigner_internal {
       
    76 
       
    77 class Connection;
       
    78 class ConnectionEdit;
       
    79 
       
    80 class QDESIGNER_SHARED_EXPORT CETypes
       
    81 {
       
    82 public:
       
    83     typedef QList<Connection*> ConnectionList;
       
    84     typedef QMap<Connection*, Connection*> ConnectionSet;
       
    85     typedef QMap<QWidget*, QWidget*> WidgetSet;
       
    86 
       
    87     class EndPoint {
       
    88     public:
       
    89         enum Type { Source, Target };
       
    90         EndPoint(Connection *_con = 0, Type _type = Source) : con(_con), type(_type) {}
       
    91         bool isNull() const { return con == 0; }
       
    92         bool operator == (const EndPoint &other) const { return con == other.con && type == other.type; }
       
    93         bool operator != (const EndPoint &other) const { return !operator == (other); }
       
    94         Connection *con;
       
    95         Type type;
       
    96     };
       
    97     enum LineDir { UpDir = 0, DownDir, RightDir, LeftDir };
       
    98 };
       
    99 
       
   100 class QDESIGNER_SHARED_EXPORT Connection : public CETypes
       
   101 {
       
   102 public:
       
   103     explicit Connection(ConnectionEdit *edit);
       
   104     explicit Connection(ConnectionEdit *edit, QObject *source, QObject *target);
       
   105     virtual ~Connection() {}
       
   106 
       
   107     QObject *object(EndPoint::Type type) const
       
   108     {
       
   109         return (type == EndPoint::Source ? m_source : m_target);
       
   110     }
       
   111 
       
   112     QWidget *widget(EndPoint::Type type) const
       
   113     {
       
   114         return qobject_cast<QWidget*>(object(type));
       
   115     }
       
   116 
       
   117     QPoint endPointPos(EndPoint::Type type) const;
       
   118     QRect endPointRect(EndPoint::Type) const;
       
   119     void setEndPoint(EndPoint::Type type, QObject *w, const QPoint &pos)
       
   120         { type == EndPoint::Source ? setSource(w, pos) : setTarget(w, pos); }
       
   121 
       
   122     bool isVisible() const;
       
   123     virtual void updateVisibility();
       
   124     void setVisible(bool b);
       
   125 
       
   126     virtual QRegion region() const;
       
   127     bool contains(const QPoint &pos) const;
       
   128     virtual void paint(QPainter *p) const;
       
   129 
       
   130     void update(bool update_widgets = true) const;
       
   131     void checkWidgets();
       
   132 
       
   133     QString label(EndPoint::Type type) const
       
   134         { return type == EndPoint::Source ? m_source_label : m_target_label; }
       
   135     void setLabel(EndPoint::Type type, const QString &text);
       
   136     QRect labelRect(EndPoint::Type type) const;
       
   137     QPixmap labelPixmap(EndPoint::Type type) const
       
   138         { return type == EndPoint::Source ? m_source_label_pm : m_target_label_pm; }
       
   139 
       
   140     ConnectionEdit *edit() const { return m_edit; }
       
   141 
       
   142     virtual void inserted() {}
       
   143     virtual void removed() {}
       
   144 
       
   145 private:
       
   146     QPoint m_source_pos, m_target_pos;
       
   147     QObject *m_source, *m_target;
       
   148     QList<QPoint> m_knee_list;
       
   149     QPolygonF m_arrow_head;
       
   150     ConnectionEdit *m_edit;
       
   151     QString m_source_label, m_target_label;
       
   152     QPixmap m_source_label_pm, m_target_label_pm;
       
   153     QRect m_source_rect, m_target_rect;
       
   154     bool m_visible;
       
   155 
       
   156     void setSource(QObject *source, const QPoint &pos);
       
   157     void setTarget(QObject *target, const QPoint &pos);
       
   158     void updateKneeList();
       
   159     void trimLine();
       
   160     void updatePixmap(EndPoint::Type type);
       
   161     LineDir labelDir(EndPoint::Type type) const;
       
   162     bool ground() const;
       
   163     QRect groundRect() const;
       
   164 };
       
   165 
       
   166 class QDESIGNER_SHARED_EXPORT ConnectionEdit : public QWidget, public CETypes
       
   167 {
       
   168     Q_OBJECT
       
   169 public:
       
   170     ConnectionEdit(QWidget *parent, QDesignerFormWindowInterface *form);
       
   171     virtual ~ConnectionEdit();
       
   172 
       
   173     inline const QPointer<QWidget> &background() const { return m_bg_widget; }
       
   174 
       
   175     void setSelected(Connection *con, bool sel);
       
   176     bool selected(const Connection *con) const;
       
   177 
       
   178     int connectionCount() const { return m_con_list.size(); }
       
   179     Connection *connection(int i) const { return m_con_list.at(i); }
       
   180     int indexOfConnection(Connection *con) const { return m_con_list.indexOf(con); }
       
   181 
       
   182     virtual void setSource(Connection *con, const QString &obj_name);
       
   183     virtual void setTarget(Connection *con, const QString &obj_name);
       
   184 
       
   185     QUndoStack *undoStack() const { return m_undo_stack; }
       
   186 
       
   187     void clear();
       
   188 
       
   189     void showEvent(QShowEvent * /*e*/)
       
   190     {
       
   191         updateBackground();
       
   192     }
       
   193 
       
   194 signals:
       
   195     void aboutToAddConnection(int idx);
       
   196     void connectionAdded(Connection *con);
       
   197     void aboutToRemoveConnection(Connection *con);
       
   198     void connectionRemoved(int idx);
       
   199     void connectionSelected(Connection *con);
       
   200     void widgetActivated(QWidget *wgt);
       
   201     void connectionChanged(Connection *con);
       
   202 
       
   203 public slots:
       
   204     void selectNone();
       
   205     void selectAll();
       
   206     virtual void deleteSelected();
       
   207     virtual void setBackground(QWidget *background);
       
   208     virtual void updateBackground();
       
   209     virtual void widgetRemoved(QWidget *w);
       
   210     virtual void objectRemoved(QObject *o);
       
   211 
       
   212     void updateLines();
       
   213     void enableUpdateBackground(bool enable);
       
   214 
       
   215 protected:
       
   216     virtual void paintEvent(QPaintEvent *e);
       
   217     virtual void mouseMoveEvent(QMouseEvent *e);
       
   218     virtual void mousePressEvent(QMouseEvent *e);
       
   219     virtual void mouseReleaseEvent(QMouseEvent *e);
       
   220     virtual void keyPressEvent(QKeyEvent *e);
       
   221     virtual void mouseDoubleClickEvent(QMouseEvent *e);
       
   222     virtual void resizeEvent(QResizeEvent *e);
       
   223     virtual void contextMenuEvent(QContextMenuEvent * event);
       
   224 
       
   225     virtual Connection *createConnection(QWidget *source, QWidget *target);
       
   226     virtual void modifyConnection(Connection *con);
       
   227 
       
   228     virtual QWidget *widgetAt(const QPoint &pos) const;
       
   229     virtual void createContextMenu(QMenu &menu);
       
   230     void addConnection(Connection *con);
       
   231     QRect widgetRect(QWidget *w) const;
       
   232 
       
   233     enum State { Editing, Connecting, Dragging };
       
   234     State state() const;
       
   235 
       
   236     virtual void endConnection(QWidget *target, const QPoint &pos);
       
   237 
       
   238     const ConnectionList &connectionList() const { return m_con_list; }
       
   239     const ConnectionSet &selection()  const      { return m_sel_con_set; }
       
   240     Connection *takeConnection(Connection *con);
       
   241     Connection *newlyAddedConnection()           { return m_tmp_con; }
       
   242     void clearNewlyAddedConnection();
       
   243 
       
   244     void findObjectsUnderMouse(const QPoint &pos);
       
   245 
       
   246 private:
       
   247     void startConnection(QWidget *source, const QPoint &pos);
       
   248     void continueConnection(QWidget *target, const QPoint &pos);
       
   249     void abortConnection();
       
   250 
       
   251     void startDrag(const EndPoint &end_point, const QPoint &pos);
       
   252     void continueDrag(const QPoint &pos);
       
   253     void endDrag(const QPoint &pos);
       
   254     void adjustHotSopt(const EndPoint &end_point, const QPoint &pos);
       
   255     Connection *connectionAt(const QPoint &pos) const;
       
   256     EndPoint endPointAt(const QPoint &pos) const;
       
   257     void paintConnection(QPainter *p, Connection *con,
       
   258                          WidgetSet *heavy_highlight_set,
       
   259                          WidgetSet *light_highlight_set) const;
       
   260     void paintLabel(QPainter *p, EndPoint::Type type, Connection *con);
       
   261 
       
   262 
       
   263     QPointer<QWidget> m_bg_widget;
       
   264     QUndoStack *m_undo_stack;
       
   265     bool m_enable_update_background;
       
   266 
       
   267     Connection *m_tmp_con; // the connection we are currently editing
       
   268     ConnectionList m_con_list;
       
   269     bool m_start_connection_on_drag;
       
   270     EndPoint m_end_point_under_mouse;
       
   271     QPointer<QWidget> m_widget_under_mouse;
       
   272 
       
   273     EndPoint m_drag_end_point;
       
   274     QPoint m_old_source_pos, m_old_target_pos;
       
   275     ConnectionSet m_sel_con_set;
       
   276     const QColor m_inactive_color;
       
   277     const QColor m_active_color;
       
   278 
       
   279 private:
       
   280     friend class Connection;
       
   281     friend class AddConnectionCommand;
       
   282     friend class DeleteConnectionsCommand;
       
   283     friend class SetEndPointCommand;
       
   284 };
       
   285 
       
   286 class QDESIGNER_SHARED_EXPORT CECommand : public QUndoCommand, public CETypes
       
   287 {
       
   288 public:
       
   289    explicit  CECommand(ConnectionEdit *edit)
       
   290         : m_edit(edit) {}
       
   291 
       
   292     virtual bool mergeWith(const QUndoCommand *) { return false; }
       
   293 
       
   294     ConnectionEdit *edit() const { return m_edit; }
       
   295 
       
   296 private:
       
   297     ConnectionEdit *m_edit;
       
   298 };
       
   299 
       
   300 class QDESIGNER_SHARED_EXPORT AddConnectionCommand : public CECommand
       
   301 {
       
   302 public:
       
   303     AddConnectionCommand(ConnectionEdit *edit, Connection *con);
       
   304     virtual void redo();
       
   305     virtual void undo();
       
   306 private:
       
   307     Connection *m_con;
       
   308 };
       
   309 
       
   310 class QDESIGNER_SHARED_EXPORT DeleteConnectionsCommand : public CECommand
       
   311 {
       
   312 public:
       
   313     DeleteConnectionsCommand(ConnectionEdit *edit, const ConnectionList &con_list);
       
   314     virtual void redo();
       
   315     virtual void undo();
       
   316 private:
       
   317     ConnectionList m_con_list;
       
   318 };
       
   319 
       
   320 } // namespace qdesigner_internal
       
   321 
       
   322 QT_END_NAMESPACE
       
   323 
       
   324 #endif // CONNECTIONEDIT_H