diagramscenemodel.h 6.97 KB
Newer Older
Jochen Becher's avatar
Jochen Becher committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef QMT_DIAGRAMSCENEMODEL_H
#define QMT_DIAGRAMSCENEMODEL_H

#include <QObject>
#include "qmt/infrastructure/qmt_global.h"

#include <QList>
#include <QHash>
#include <QSet>

QT_BEGIN_NAMESPACE
class QGraphicsItem;
class QPointF;
class QGraphicsScene;
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE


namespace qmt {

class DiagramGraphicsScene;
class LatchController;

class Uid;
class DiagramController;
class DiagramSceneController;
class StereotypeController;
class StyleController;
class DSelection;
class MDiagram;
class DElement;
class DObject;

class QMT_EXPORT DiagramSceneModel :
        public QObject
{
    Q_OBJECT

    class CreationVisitor;
    class UpdateVisitor;
    class OriginItem;

    friend class UpdateVisitor;
    friend class DiagramGraphicsScene;

public:

    enum CollidingMode {
79 80 81
        CollidingInnerItems,
        CollidingItems,
        CollidingOuterItems
Jochen Becher's avatar
Jochen Becher committed
82 83 84 85 86 87 88 89 90 91 92 93 94
    };


public:

    DiagramSceneModel(QObject *parent = 0);

    ~DiagramSceneModel();

signals:

    void diagramSceneActivated(const MDiagram *diagram);

95
    void selectionHasChanged(const MDiagram *diagram);
Jochen Becher's avatar
Jochen Becher committed
96 97 98

public:

99
    DiagramController *diagramController() const { return m_diagramController; }
Jochen Becher's avatar
Jochen Becher committed
100

101
    void setDiagramController(DiagramController *diagramController);
Jochen Becher's avatar
Jochen Becher committed
102

103
    DiagramSceneController *diagramSceneController() const { return m_diagramSceneController; }
Jochen Becher's avatar
Jochen Becher committed
104

105
    void setDiagramSceneController(DiagramSceneController *diagramSceneController);
Jochen Becher's avatar
Jochen Becher committed
106

107
    StyleController *styleController() const { return m_styleController; }
Jochen Becher's avatar
Jochen Becher committed
108

109
    void setStyleController(StyleController *styleController);
Jochen Becher's avatar
Jochen Becher committed
110

111
    StereotypeController *stereotypeController() const { return m_stereotypeController; }
Jochen Becher's avatar
Jochen Becher committed
112

113
    void setStereotypeController(StereotypeController *stereotypeController);
Jochen Becher's avatar
Jochen Becher committed
114

115
    MDiagram *diagram() const { return m_diagram; }
Jochen Becher's avatar
Jochen Becher committed
116 117 118

    void setDiagram(MDiagram *diagram);

119
    QGraphicsScene *graphicsScene() const;
Jochen Becher's avatar
Jochen Becher committed
120 121 122 123 124 125 126

public:

    bool hasSelection() const;

    bool hasMultiObjectsSelection() const;

127
    DSelection selectedElements() const;
Jochen Becher's avatar
Jochen Becher committed
128

129
    DElement *findTopmostElement(const QPointF &scenePos) const;
Jochen Becher's avatar
Jochen Becher committed
130 131 132

public:

133
    QList<QGraphicsItem *> graphicsItems() const { return m_graphicsItems; }
Jochen Becher's avatar
Jochen Becher committed
134

135
    QGraphicsItem *graphicsItem(DElement *element) const;
Jochen Becher's avatar
Jochen Becher committed
136

137
    QGraphicsItem *graphicsItem(const Uid &uid) const;
Jochen Becher's avatar
Jochen Becher committed
138

139
    QGraphicsItem *focusItem() const { return m_focusItem; }
Jochen Becher's avatar
Jochen Becher committed
140 141 142

    bool isSelectedItem(QGraphicsItem *item) const;

143
    QSet<QGraphicsItem *> selectedItems() const { return m_selectedItems; }
Jochen Becher's avatar
Jochen Becher committed
144

145
    DElement *element(QGraphicsItem *item) const;
Jochen Becher's avatar
Jochen Becher committed
146 147 148 149 150 151 152 153 154 155 156 157 158

    bool isElementEditable(const DElement *element) const;

public:

    void selectAllElements();

    void selectElement(DElement *element);

    void editElement(DElement *element);

    void copyToClipboard();

159
    bool exportPng(const QString &fileName);
Jochen Becher's avatar
Jochen Becher committed
160

161
    void exportPdf(const QString &fileName);
Jochen Becher's avatar
Jochen Becher committed
162 163 164

public:

165
    void selectItem(QGraphicsItem *item, bool multiSelect);
Jochen Becher's avatar
Jochen Becher committed
166

167
    void moveSelectedItems(QGraphicsItem *grabbedItem, const QPointF &delta);
Jochen Becher's avatar
Jochen Becher committed
168 169 170 171 172

    void alignSelectedItemsPositionOnRaster();

    void onDoubleClickedItem(QGraphicsItem *item);

173
    QList<QGraphicsItem *> collectCollidingObjectItems(const QGraphicsItem *item, CollidingMode collidingMode) const;
Jochen Becher's avatar
Jochen Becher committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

private:

    void sceneActivated();

    void mousePressEvent(QGraphicsSceneMouseEvent *event);

    void mousePressEventReparenting(QGraphicsSceneMouseEvent *event);

    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

    void mouseMoveEventReparenting(QGraphicsSceneMouseEvent *event);

    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    void mouseReleaseEventReparenting(QGraphicsSceneMouseEvent *event);

private slots:

    void onBeginResetAllDiagrams();

    void onEndResetAllDiagrams();

    void onBeginResetDiagram(const MDiagram *diagram);

    void onEndResetDiagram(const MDiagram *diagram);

    void onBeginUpdateElement(int row, const MDiagram *diagram);

    void onEndUpdateElement(int row, const MDiagram *diagram);

    void onBeginInsertElement(int row, const MDiagram *diagram);

    void onEndInsertElement(int row, const MDiagram *diagram);

    void onBeginRemoveElement(int row, const MDiagram *diagram);

    void onEndRemoveElement(int row, const MDiagram *diagram);

private slots:

    void onSelectionChanged();

private:

    void clearGraphicsScene();

    void removeExtraSceneItems();

    void addExtraSceneItems();

    QGraphicsItem *createGraphicsItem(DElement *element);

    void updateGraphicsItem(QGraphicsItem *item, DElement *element);

    void deleteGraphicsItem(QGraphicsItem *item, DElement *element);

231
    void updateFocusItem(const QSet<QGraphicsItem *> &selectedItems);
Jochen Becher's avatar
Jochen Becher committed
232 233 234

    void unsetFocusItem();

235
    bool isInFrontOf(const QGraphicsItem *frontItem, const QGraphicsItem *backItem);
Jochen Becher's avatar
Jochen Becher committed
236 237 238 239

private:

    enum Busy {
240 241 242 243 244
        NotBusy,
        ResetDiagram,
        UpdateElement,
        InsertElement,
        RemoveElement
Jochen Becher's avatar
Jochen Becher committed
245 246 247 248
    };

private:

249
    DiagramController *m_diagramController;
Jochen Becher's avatar
Jochen Becher committed
250

251
    DiagramSceneController *m_diagramSceneController;
Jochen Becher's avatar
Jochen Becher committed
252

253
    StyleController *m_styleController;
Jochen Becher's avatar
Jochen Becher committed
254

255
    StereotypeController *m_stereotypeController;
Jochen Becher's avatar
Jochen Becher committed
256

257
    MDiagram *m_diagram;
Jochen Becher's avatar
Jochen Becher committed
258

259
    DiagramGraphicsScene *m_graphicsScene;
Jochen Becher's avatar
Jochen Becher committed
260

261
    LatchController *m_latchController;
Jochen Becher's avatar
Jochen Becher committed
262

263
    QList<QGraphicsItem *> m_graphicsItems;
Jochen Becher's avatar
Jochen Becher committed
264

265
    QHash<const QGraphicsItem *, DElement *> m_itemToElementMap;
Jochen Becher's avatar
Jochen Becher committed
266

267
    QHash<const DElement *, QGraphicsItem *> m_elementToItemMap;
Jochen Becher's avatar
Jochen Becher committed
268

269
    QSet<QGraphicsItem *> m_selectedItems;
Jochen Becher's avatar
Jochen Becher committed
270

271
    QSet<QGraphicsItem *> m_secondarySelectedItems;
Jochen Becher's avatar
Jochen Becher committed
272

273
    Busy m_busy;
Jochen Becher's avatar
Jochen Becher committed
274

275
    OriginItem *m_originItem;
Jochen Becher's avatar
Jochen Becher committed
276

277
    QGraphicsItem *m_focusItem;
Jochen Becher's avatar
Jochen Becher committed
278 279 280 281 282
};

}

#endif // QMT_DIAGRAMSCENEMODEL_H