summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/toolbarstate.cpp
blob: d539c512a44d03034e061fe9007cb1a586b91211 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2004-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

// own header
#include "toolbarstate.h"

// qt includes
#include <tqwmatrix.h> // need for inverseWorldMatrix.map

// app includes
#include "associationwidget.h"
#include "messagewidget.h"
#include "uml.h"
#include "umlview.h"
#include "umlwidget.h"

ToolBarState::~ToolBarState() {
    delete m_pMouseEvent;
}

void ToolBarState::init() {
    m_pUMLView->viewport()->setMouseTracking(false);
    m_pMouseEvent = 0;
    m_currentWidget = 0;
    m_currentAssociation = 0;

    connect(m_pUMLView, TQ_SIGNAL(sigAssociationRemoved(AssociationWidget*)),
            this, TQ_SLOT(slotAssociationRemoved(AssociationWidget*)));
    connect(m_pUMLView, TQ_SIGNAL(sigWidgetRemoved(UMLWidget*)),
            this, TQ_SLOT(slotWidgetRemoved(UMLWidget*)));
}

void ToolBarState::cleanBeforeChange() {
    disconnect(m_pUMLView, TQ_SIGNAL(sigAssociationRemoved(AssociationWidget*)),
               this, TQ_SLOT(slotAssociationRemoved(AssociationWidget*)));
    disconnect(m_pUMLView, TQ_SIGNAL(sigWidgetRemoved(UMLWidget*)),
               this, TQ_SLOT(slotWidgetRemoved(UMLWidget*)));
}

void ToolBarState::mousePress(TQMouseEvent* ome) {
    setMouseEvent(ome, TQEvent::MouseButtonPress);

    m_pUMLView->viewport()->setMouseTracking(true);

    //TODO Doesn't another way of emiting the signal exist? A method only for
    //that seems a bit dirty.
    m_pUMLView->emitRemovePopupMenu();

    // TODO: Check who needs this.
    m_pUMLView->setPos(m_pMouseEvent->pos());

    //TODO check why
    m_pUMLView->setPaste(false);

    setCurrentElement();

    if (getCurrentWidget()) {
        mousePressWidget();
    } else if (getCurrentAssociation()) {
        mousePressAssociation();
    } else {
        mousePressEmpty();
    }
}

void ToolBarState::mouseRelease(TQMouseEvent* ome) {
    setMouseEvent(ome, TQEvent::MouseButtonRelease);

    // Set the position of the mouse
    // TODO, should only be available in this state?
    m_pUMLView->setPos(m_pMouseEvent->pos());

    m_pUMLView->viewport()->setMouseTracking(false);

    if (getCurrentWidget()) {
        mouseReleaseWidget();
        setCurrentWidget(0);
    } else if (getCurrentAssociation()) {
        mouseReleaseAssociation();
        setCurrentAssociation(0);
    } else {
        mouseReleaseEmpty();
    }

    // Default, rightbutton changes the tool.
    // The arrow tool overrides the changeTool() function.
    changeTool();
}

void ToolBarState::mouseDoubleClick(TQMouseEvent* ome) {
    setMouseEvent(ome, TQEvent::MouseButtonDblClick);

    UMLWidget* currentWidget = m_pUMLView->getWidgetAt(m_pMouseEvent->pos());
    AssociationWidget* currentAssociation = getAssociationAt(m_pMouseEvent->pos());
    if (currentWidget) {
        setCurrentWidget(currentWidget);
        mouseDoubleClickWidget();
        setCurrentWidget(0);
    } else if (currentAssociation) {
        setCurrentAssociation(currentAssociation);
        mouseDoubleClickAssociation();
        setCurrentAssociation(0);
    } else {
        mouseDoubleClickEmpty();
    }
}

void ToolBarState::mouseMove(TQMouseEvent* ome) {
    setMouseEvent(ome, TQEvent::MouseMove);

    if (getCurrentWidget()) {
        mouseMoveWidget();
    } else if (getCurrentAssociation()) {
        mouseMoveAssociation();
    } else {
        mouseMoveEmpty();
    }

    //Scrolls the view
    int vx = ome->x();
    int vy = ome->y();
    int contsX = m_pUMLView->contentsX();
    int contsY = m_pUMLView->contentsY();
    int visw = m_pUMLView->visibleWidth();
    int vish = m_pUMLView->visibleHeight();
    int dtr = visw - (vx-contsX);
    int dtb = vish - (vy-contsY);
    int dtt =  (vy-contsY);
    int dtl =  (vx-contsX);
    if (dtr < 30) m_pUMLView->scrollBy(30-dtr,0);
    if (dtb < 30) m_pUMLView->scrollBy(0,30-dtb);
    if (dtl < 30) m_pUMLView->scrollBy(-(30-dtl),0);
    if (dtt < 30) m_pUMLView->scrollBy(0,-(30-dtt));
}

void ToolBarState::slotAssociationRemoved(AssociationWidget* association) {
    if (association == getCurrentAssociation()) {
        setCurrentAssociation(0);
    }
}

void ToolBarState::slotWidgetRemoved(UMLWidget* widget) {
    if (widget == getCurrentWidget()) {
        setCurrentWidget(0);
    }
}

ToolBarState::ToolBarState(UMLView *umlView) : TQObject(umlView), m_pUMLView(umlView) {
    m_pMouseEvent = NULL;
    init();
}

void ToolBarState::setCurrentElement() {
    // Check associations.
    AssociationWidget* association = getAssociationAt(m_pMouseEvent->pos());
    if (association) {
        setCurrentAssociation(association);
        return;
    }

    // Check messages.
    //TODO check why message widgets are treated different
    MessageWidget* message = getMessageAt(m_pMouseEvent->pos());
    if (message) {
        setCurrentWidget(message);
        return;
    }

    // Check widgets.
    UMLWidget *widget = m_pUMLView->getWidgetAt(m_pMouseEvent->pos());
    if (widget) {
        setCurrentWidget(widget);
        return;
    }
}

void ToolBarState::mousePressAssociation() {
}

void ToolBarState::mousePressWidget() {
}

void ToolBarState::mousePressEmpty() {
    m_pUMLView->clearSelected();
}

void ToolBarState::mouseReleaseAssociation() {
}

void ToolBarState::mouseReleaseWidget() {
}

void ToolBarState::mouseReleaseEmpty() {
}

void ToolBarState::mouseDoubleClickAssociation() {
}

void ToolBarState::mouseDoubleClickWidget() {
}

void ToolBarState::mouseDoubleClickEmpty() {
    m_pUMLView->clearSelected();
}

void ToolBarState::mouseMoveAssociation() {
}

void ToolBarState::mouseMoveWidget() {
}

void ToolBarState::mouseMoveEmpty() {
}

void ToolBarState::changeTool() {
    if (m_pMouseEvent->state() == TQt::RightButton) {
        UMLApp::app()->getWorkToolBar()->setDefaultTool();
    }
}

void ToolBarState::setMouseEvent(TQMouseEvent* ome, const TQEvent::Type &type) {
    if (m_pMouseEvent) delete m_pMouseEvent;

    m_pMouseEvent = new TQMouseEvent(type, m_pUMLView->inverseWorldMatrix().map(ome->pos()),
                                    ome->button(),ome->state());
}

MessageWidget* ToolBarState::getMessageAt(const TQPoint& pos) {
    MessageWidget* message = 0;
    for (MessageWidgetListIt it(m_pUMLView->getMessageList());
                                (message = it.current()) != 0; ++it) {
        if (message->isVisible() && message->onWidget(pos)) {
            return message;
        }
    }

    return message;
}

AssociationWidget* ToolBarState::getAssociationAt(const TQPoint& pos) {
    AssociationWidget* association = 0;
    for (AssociationWidgetListIt it(m_pUMLView->getAssociationList());
                                (association = it.current()) != 0; ++it) {
        if (association->onAssociation(pos)) {
            return association;
        }
    }

    return association;
}

#include "toolbarstate.moc"