• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

umbrello/umbrello

  • sources
  • kde-4.12
  • kdesdk
  • umbrello
  • umbrello
toolbarstateassociation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This program is free software; you can redistribute it and/or modify *
3  * it under the terms of the GNU General Public License as published by *
4  * the Free Software Foundation; either version 2 of the License, or *
5  * (at your option) any later version. *
6  * *
7  * copyright (C) 2004-2013 *
8  * Umbrello UML Modeller Authors <umbrello-devel@kde.org> *
9  ***************************************************************************/
10 
11 // own header
12 #include "toolbarstateassociation.h"
13 
14 // app includes
15 #include "assocrules.h"
16 #include "association.h"
17 #include "associationline.h"
18 #include "associationwidget.h"
19 #include "classifierwidget.h"
20 #include "debug_utils.h"
21 #include "folder.h"
22 #include "model_utils.h"
23 #include "uml.h"
24 #include "umldoc.h"
25 #include "umlobject.h"
26 #include "umlscene.h"
27 #include "umlview.h"
28 #include "umlwidget.h"
29 
30 // kde includes
31 #include <klocale.h>
32 #include <kmessagebox.h>
33 
39 ToolBarStateAssociation::ToolBarStateAssociation(UMLScene *umlScene)
40  : ToolBarStatePool(umlScene),
41  m_firstWidget(0),
42  m_associationLine(0)
43 {
44 }
45 
50 ToolBarStateAssociation::~ToolBarStateAssociation()
51 {
52  delete m_associationLine;
53 }
54 
58 void ToolBarStateAssociation::init()
59 {
60  ToolBarStatePool::init();
61  cleanAssociation();
62 }
63 
68 void ToolBarStateAssociation::cleanBeforeChange()
69 {
70  ToolBarStatePool::cleanBeforeChange();
71  cleanAssociation();
72 }
73 
79 void ToolBarStateAssociation::mouseMove(QGraphicsSceneMouseEvent* ome)
80 {
81  ToolBarStatePool::mouseMove(ome);
82 
83  if (m_associationLine) {
84  QPointF sp = m_associationLine->line().p1();
85  m_associationLine->setLine(sp.x(), sp.y(), m_pMouseEvent->scenePos().x(), m_pMouseEvent->scenePos().y());
86  }
87 }
88 
95 void ToolBarStateAssociation::slotWidgetRemoved(UMLWidget* widget)
96 {
97  ToolBarState::slotWidgetRemoved(widget);
98 
99  if (widget == m_firstWidget) {
100  cleanAssociation();
101  }
102 }
103 
111 void ToolBarStateAssociation::mouseReleaseAssociation()
112 {
113  if (m_pMouseEvent->button() != Qt::LeftButton ||
114  !m_firstWidget || m_firstWidget->baseType() != WidgetBase::wt_Class) {
115  cleanAssociation();
116  return;
117  }
118 
119  currentAssociation()->createAssocClassLine(
120  static_cast<ClassifierWidget*>(m_firstWidget),
121  currentAssociation()->associationLine()->closestSegmentIndex(m_pMouseEvent->scenePos()));
122  m_firstWidget->addAssoc(currentAssociation());
123  cleanAssociation();
124 }
125 
132 void ToolBarStateAssociation::mouseReleaseWidget()
133 {
134  if (m_pMouseEvent->button() != Qt::LeftButton) {
135  cleanAssociation();
136  return;
137  }
138 
139  // TODO In old code in ToolBarState there was a TODO that said: Should not
140  //be called by a Sequence message Association. Here's the check for that,
141  //although I don't know why it is needed, but it seems that it's not needed,
142  //as the old code worked fine without it...
143  if (getAssociationType() == Uml::AssociationType::Seq_Message) {
144  return;
145  }
146 
147  if (!m_firstWidget) {
148  setFirstWidget();
149  } else {
150  setSecondWidget();
151  }
152 }
153 
158 void ToolBarStateAssociation::mouseReleaseEmpty()
159 {
160  cleanAssociation();
161 }
162 
170 void ToolBarStateAssociation::setFirstWidget()
171 {
172  UMLWidget* widget = currentWidget();
173  Uml::AssociationType::Enum type = getAssociationType();
174 
175  if (!AssocRules::allowAssociation(type, widget)) {
176  //TODO improve error feedback: tell the user what are the valid type of associations for
177  //that widget
178  KMessageBox::error(0, i18n("Incorrect use of associations."), i18n("Association Error"));
179  return;
180  }
181  //set up position
182  QPoint pos;
183  pos.setX(widget->x() + (widget->width() / 2));
184  pos.setY(widget->y() + (widget->height() / 2));
185  //TODO why is this needed?
186  m_pUMLScene->setPos(pos);
187 
188  m_firstWidget = widget;
189 
190  m_associationLine = new QGraphicsLineItem();
191  m_pUMLScene->addItem(m_associationLine);
192  m_associationLine->setLine(pos.x(), pos.y(), pos.x(), pos.y());
193  m_associationLine->setPen(QPen(m_pUMLScene->lineColor(), m_pUMLScene->lineWidth(), Qt::DashLine));
194  m_associationLine->setVisible(true);
195 
196  m_pUMLScene->activeView()->viewport()->setMouseTracking(true);
197 }
198 
211 void ToolBarStateAssociation::setSecondWidget()
212 {
213  Uml::AssociationType::Enum type = getAssociationType();
214  UMLWidget* widgetA = m_firstWidget;
215  UMLWidget* widgetB = currentWidget();
216  WidgetBase::WidgetType at = widgetA->baseType();
217  bool valid = true;
218  if (type == Uml::AssociationType::Generalization) {
219  type = AssocRules::isGeneralisationOrRealisation(widgetA, widgetB);
220  }
221  if (widgetA == widgetB) {
222  valid = AssocRules::allowSelf(type, at);
223  if (valid && type == Uml::AssociationType::Association) {
224  type = Uml::AssociationType::Association_Self;
225  }
226  } else {
227  valid = AssocRules::allowAssociation(type, widgetA, widgetB);
228  }
229  if (valid) {
230  AssociationWidget *temp = AssociationWidget::create(m_pUMLScene, widgetA, type, widgetB);
231  if (addAssociationInViewAndDoc(temp)) {
232  if (type == Uml::AssociationType::Containment) {
233  UMLObject *newContainer = widgetA->umlObject();
234  UMLObject *objToBeMoved = widgetB->umlObject();
235  if (newContainer && objToBeMoved) {
236  Model_Utils::treeViewMoveObjectTo(newContainer, objToBeMoved);
237  }
238  }
239  UMLApp::app()->document()->setModified();
240  }
241  } else {
242  //TODO improve error feedback: tell the user what are the valid type of associations for
243  //the second widget using the first widget
244  KMessageBox::error(0, i18n("Incorrect use of associations."), i18n("Association Error"));
245  }
246 
247  cleanAssociation();
248 }
249 
255 Uml::AssociationType::Enum ToolBarStateAssociation::getAssociationType()
256 {
257  Uml::AssociationType::Enum at;
258 
259  switch(getButton()) {
260  case WorkToolBar::tbb_Anchor: at = Uml::AssociationType::Anchor; break;
261  case WorkToolBar::tbb_Association: at = Uml::AssociationType::Association; break;
262  case WorkToolBar::tbb_UniAssociation: at = Uml::AssociationType::UniAssociation; break;
263  case WorkToolBar::tbb_Generalization: at = Uml::AssociationType::Generalization; break;
264  case WorkToolBar::tbb_Composition: at = Uml::AssociationType::Composition; break;
265  case WorkToolBar::tbb_Aggregation: at = Uml::AssociationType::Aggregation; break;
266  case WorkToolBar::tbb_Relationship: at = Uml::AssociationType::Relationship; break;
267  case WorkToolBar::tbb_Dependency: at = Uml::AssociationType::Dependency; break;
268  case WorkToolBar::tbb_Containment: at = Uml::AssociationType::Containment; break;
269  case WorkToolBar::tbb_Seq_Message_Synchronous:
270  case WorkToolBar::tbb_Seq_Combined_Fragment:
271  case WorkToolBar::tbb_Seq_Precondition:
272  case WorkToolBar::tbb_Seq_Message_Asynchronous: at = Uml::AssociationType::Seq_Message; break;
273  case WorkToolBar::tbb_Coll_Message: at = Uml::AssociationType::Coll_Message; break;
274  case WorkToolBar::tbb_State_Transition: at = Uml::AssociationType::State; break;
275  case WorkToolBar::tbb_Activity_Transition: at = Uml::AssociationType::Activity; break;
276  case WorkToolBar::tbb_Exception: at = Uml::AssociationType::Exception; break;
277  case WorkToolBar::tbb_Category2Parent: at = Uml::AssociationType::Category2Parent; break;
278  case WorkToolBar::tbb_Child2Category: at = Uml::AssociationType::Child2Category; break;
279 
280  default: at = Uml::AssociationType::Unknown; break;
281  }
282 
283  return at;
284 }
285 
294 bool ToolBarStateAssociation::addAssociationInViewAndDoc(AssociationWidget* assoc)
295 {
296  // append in view
297  if (m_pUMLScene->addAssociation(assoc, false)) {
298  // if view went ok, then append in document
299  UMLAssociation *umla = assoc->association();
300  if (umla) {
301  // association with model representation in UMLDoc
302  Uml::ModelType::Enum m = Model_Utils::convert_DT_MT(m_pUMLScene->type());
303  UMLDoc *umldoc = UMLApp::app()->document();
304  umla->setUMLPackage(umldoc->rootFolder(m));
305  umldoc->addAssociation(umla);
306  }
307  return true;
308  } else {
309  uError() << "cannot addAssocInViewAndDoc(), deleting";
310  delete assoc;
311  return false;
312  }
313 }
314 
319 void ToolBarStateAssociation::cleanAssociation()
320 {
321  m_firstWidget = 0;
322 
323  delete m_associationLine;
324  m_associationLine = 0;
325 }
326 
327 #include "toolbarstateassociation.moc"
WorkToolBar::tbb_Anchor
Definition: worktoolbar.h:78
Uml::AssociationType::Aggregation
Definition: basictypes.h:101
AssociationWidget::create
static AssociationWidget * create(UMLScene *scene)
This constructor is really only for loading from XMI, otherwise it should not be allowed as it create...
Definition: associationwidget.cpp:111
WorkToolBar::tbb_Association
Definition: worktoolbar.h:61
associationline.h
umlobject.h
WorkToolBar::tbb_Coll_Message
Definition: worktoolbar.h:63
ToolBarStatePool
Base class for tools that can use the same state but with different button.
Definition: toolbarstatepool.h:22
AssocRules::isGeneralisationOrRealisation
static Uml::AssociationType::Enum isGeneralisationOrRealisation(UMLWidget *widgetA, UMLWidget *widgetB)
Returns whether an implements association should be a Realisation or a Generalisation.
Definition: assocrules.cpp:343
ToolBarState::m_pUMLScene
UMLScene * m_pUMLScene
The UMLScene.
Definition: toolbarstate.h:120
Uml::AssociationType::Generalization
Definition: basictypes.h:100
Uml::AssociationType::Seq_Message
Definition: basictypes.h:106
WidgetBase::umlObject
UMLObject * umlObject() const
Returns the UMLObject set to represent.
Definition: widgetbase.cpp:113
UMLScene::lineWidth
uint lineWidth() const
Returns the line width to use.
Definition: umlscene.cpp:342
association.h
Uml::AssociationType::Coll_Message
Definition: basictypes.h:105
ToolBarStateAssociation::ToolBarStateAssociation
ToolBarStateAssociation(UMLScene *umlScene)
Creates a new ToolBarStateAssociation.
Definition: toolbarstateassociation.cpp:39
Uml::AssociationType::Activity
Definition: basictypes.h:115
Uml::AssociationType::UniAssociation
Definition: basictypes.h:112
umlview.h
toolbarstateassociation.h
WorkToolBar::tbb_Dependency
Definition: worktoolbar.h:60
Model_Utils::convert_DT_MT
Uml::ModelType::Enum convert_DT_MT(Uml::DiagramType::Enum dt)
Return the Model_Type which corresponds to the given DiagramType.
Definition: model_utils.cpp:1037
WorkToolBar::tbb_Composition
Definition: worktoolbar.h:70
Uml::AssociationType::Child2Category
Definition: basictypes.h:118
umlscene.h
WorkToolBar::tbb_UniAssociation
Definition: worktoolbar.h:72
Uml::AssociationType::State
Definition: basictypes.h:114
UMLApp::app
static UMLApp * app()
Get the last created instance of this class.
Definition: uml.cpp:206
WorkToolBar::tbb_Aggregation
Definition: worktoolbar.h:59
Uml::AssociationType::Anchor
Definition: basictypes.h:113
UMLWidget::height
qreal height() const
Returns the height of widget.
Definition: umlwidget.h:122
ToolBarStateAssociation::mouseMove
virtual void mouseMove(QGraphicsSceneMouseEvent *ome)
Called when a mouse event happened.
Definition: toolbarstateassociation.cpp:79
ToolBarStateAssociation::init
virtual void init()
Goes back to the initial state.
Definition: toolbarstateassociation.cpp:58
WidgetBase::WidgetType
WidgetType
Definition: widgetbase.h:41
WorkToolBar::tbb_Seq_Message_Asynchronous
Definition: worktoolbar.h:65
UMLApp::document
UMLDoc * document() const
Returns a pointer to the current document connected to the KMainWindow instance.
Definition: uml.cpp:872
model_utils.h
debug_utils.h
Uml::AssociationType::Dependency
Definition: basictypes.h:102
UMLObject
This class is the non-graphical version of UMLWidget.
Definition: umlobject.h:41
AssociationWidget
This class represents an association inside a diagram.
Definition: associationwidget.h:50
UMLAssociation
This class contains the non-graphic representation of an association.
Definition: association.h:32
ToolBarState::mouseMove
virtual void mouseMove(QGraphicsSceneMouseEvent *ome)
Handler for mouse move events.
Definition: toolbarstate.cpp:175
assocrules.h
WorkToolBar::tbb_Category2Parent
Definition: worktoolbar.h:116
UMLScene::type
Uml::DiagramType::Enum type() const
Returns the type of the diagram.
Definition: umlscene.cpp:256
WorkToolBar::tbb_State_Transition
Definition: worktoolbar.h:73
WorkToolBar::tbb_Relationship
Definition: worktoolbar.h:71
WidgetBase::wt_Class
Definition: widgetbase.h:46
AssociationWidget::association
UMLAssociation * association() const
Returns the UMLAssociation representation of this object.
Definition: associationwidget.cpp:548
UMLScene::addAssociation
bool addAssociation(AssociationWidget *pAssoc, bool isPasteOperation=false)
Adds an association to the view from the given data.
Definition: umlscene.cpp:2119
UMLObject::setUMLPackage
bool setUMLPackage(UMLPackage *pPkg)
Sets the UMLPackage in which this class is located.
Definition: umlobject.cpp:545
umlwidget.h
ToolBarState::cleanBeforeChange
virtual void cleanBeforeChange()
Called when the current tool is changed to use another tool.
Definition: toolbarstate.cpp:59
ToolBarStatePool::getButton
WorkToolBar::ToolBar_Buttons getButton() const
Returns the current button.
Definition: toolbarstatepool.cpp:42
WorkToolBar::tbb_Containment
Definition: worktoolbar.h:62
Uml::AssociationType::Enum
Enum
Definition: basictypes.h:99
Uml::AssociationType::Association
Definition: basictypes.h:103
WorkToolBar::tbb_Generalization
Definition: worktoolbar.h:58
WorkToolBar::tbb_Activity_Transition
Definition: worktoolbar.h:74
UMLDoc::setModified
void setModified(bool modified=true)
Sets the modified flag for the document after a modifying action on the view connected to the documen...
Definition: umldoc.cpp:2607
ToolBarStateAssociation::~ToolBarStateAssociation
virtual ~ToolBarStateAssociation()
Destroys this ToolBarStateAssociation.
Definition: toolbarstateassociation.cpp:50
WorkToolBar::tbb_Seq_Precondition
Definition: worktoolbar.h:69
Uml::AssociationType::Composition
Definition: basictypes.h:110
classifierwidget.h
WorkToolBar::tbb_Seq_Message_Synchronous
Definition: worktoolbar.h:64
QGraphicsLineItem
folder.h
associationwidget.h
umldoc.h
ToolBarState::slotWidgetRemoved
virtual void slotWidgetRemoved(UMLWidget *widget)
A widget was removed from the UMLScene.
Definition: toolbarstate.cpp:236
ToolBarStateAssociation::slotWidgetRemoved
virtual void slotWidgetRemoved(UMLWidget *widget)
A widget was removed from the UMLScene.
Definition: toolbarstateassociation.cpp:95
UMLScene::activeView
UMLView * activeView() const
Returns the active view associated with this scene.
Definition: umlscene.cpp:193
Uml::AssociationType::Exception
Definition: basictypes.h:116
ToolBarState::init
virtual void init()
Goes back to the initial state.
Definition: toolbarstate.cpp:41
ToolBarStateAssociation::mouseReleaseWidget
virtual void mouseReleaseWidget()
Called when the release event happened on a widget.
Definition: toolbarstateassociation.cpp:132
AssocRules::allowSelf
static bool allowSelf(Uml::AssociationType::Enum assocType, WidgetBase::WidgetType widgetType)
Returns whether to allow an association to self for given variables.
Definition: assocrules.cpp:328
ToolBarState::m_pMouseEvent
QGraphicsSceneMouseEvent * m_pMouseEvent
The mouse event currently in use.
Definition: toolbarstate.h:121
Uml::AssociationType::Unknown
Definition: basictypes.h:120
Model_Utils::treeViewMoveObjectTo
void treeViewMoveObjectTo(UMLObject *container, UMLObject *object)
Move an object to a new container in the tree view.
Definition: model_utils.cpp:364
WidgetBase::baseType
WidgetType baseType() const
Read property of m_baseType.
Definition: widgetbase.cpp:76
UMLWidget::addAssoc
void addAssoc(AssociationWidget *pAssoc)
Adds an already created association to the list of associations that include this UMLWidget...
Definition: umlwidget.cpp:976
UMLWidget::width
qreal width() const
Returns the width of the widget.
Definition: umlwidget.h:129
ToolBarStateAssociation::cleanBeforeChange
virtual void cleanBeforeChange()
Called when the current tool is changed to use another tool.
Definition: toolbarstateassociation.cpp:68
Uml::AssociationType::Category2Parent
Definition: basictypes.h:117
uError
#define uError()
Definition: debug_utils.h:96
Uml::AssociationType::Relationship
Definition: basictypes.h:119
ToolBarState::currentWidget
virtual UMLWidget * currentWidget() const
Returns the widget currently in use.
Definition: toolbarstate.cpp:426
WorkToolBar::tbb_Child2Category
Definition: worktoolbar.h:117
ToolBarState::currentAssociation
virtual AssociationWidget * currentAssociation() const
Returns the association currently in use.
Definition: toolbarstate.cpp:450
UMLScene::lineColor
const QColor & lineColor() const
Returns the line color to use.
Definition: umlscene.cpp:323
Uml::AssociationType::Containment
Definition: basictypes.h:109
AssocRules::allowAssociation
static bool allowAssociation(Uml::AssociationType::Enum assocType, UMLWidget *widget)
Returns whether an association is going to be allowed for the given values.
Definition: assocrules.cpp:50
AssociationWidget::createAssocClassLine
void createAssocClassLine()
Creates the association class connecting line.
Definition: associationwidget.cpp:2799
Uml::ModelType::Enum
Enum
Definition: basictypes.h:38
Uml::AssociationType::Association_Self
Definition: basictypes.h:104
WorkToolBar::tbb_Exception
Definition: worktoolbar.h:112
UMLScene::setPos
void setPos(const QPointF &pos)
Sets the position of the diagram.
Definition: umlscene.cpp:296
WorkToolBar::tbb_Seq_Combined_Fragment
Definition: worktoolbar.h:68
ToolBarStateAssociation::mouseReleaseAssociation
virtual void mouseReleaseAssociation()
Called when the release event happened on an association.
Definition: toolbarstateassociation.cpp:111
UMLScene
UMLScene instances represent diagrams.
Definition: umlscene.h:70
ToolBarStateAssociation::mouseReleaseEmpty
virtual void mouseReleaseEmpty()
Called when the release event happened on an empty space.
Definition: toolbarstateassociation.cpp:158
uml.h
UMLDoc
UMLDoc provides a document object for a document-view model.
Definition: umldoc.h:63
UMLWidget
This is the base class for nearly all graphical widgets.
Definition: umlwidget.h:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:06:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

umbrello/umbrello

Skip menu "umbrello/umbrello"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal