Logo Search packages:      
Sourcecode: kdesdk version File versions

bool UMLView::addWidget ( UMLWidget pWidget,
bool  isPasteOperation = false 
)

Adds a widget to the view from the given data. Use this method when pasting.

Definition at line 1593 of file umlview.cpp.

References IDChangeLog::addIDChange(), UMLDoc::assignNewID(), IDChangeLog::findNewID(), findWidget(), LinkWidget::getBaseType(), FloatingText::getDisplayText(), MessageWidget::getFloatingText(), getID(), UMLWidget::getID(), getLocalID(), UMLWidget::getName(), UMLDoc::getUniqueID(), m_MessageList, m_pDoc, m_pIDChangesLog, m_WidgetList, UMLWidget::setID(), setID(), UMLWidget::setX(), and UMLWidget::setY().

Referenced by addAssociation(), UMLClipboard::pasteClip4(), AssociationWidget::setChangeWidget(), AssociationWidget::setMulti(), AssociationWidget::setName(), and AssociationWidget::setRoleName().

                                                                     {
      if( !pWidget ) {
            return false;
      }
      if (!isPasteOperation && findWidget(pWidget->getID())) {
            kdDebug() << "UMLView::addWidget: Not adding "
                    << "(id=" << pWidget->getID()
                    << "/type=" << pWidget->getBaseType()
                    << "/name=" << pWidget->getName()
                    << ") because it's already there" << endl;
            return false;
      }
      UMLWidget_Type type = pWidget->getBaseType();
      //kdDebug() << "UMLView::addWidget called for basetype " << type << endl;
      IDChangeLog * log = m_pDoc -> getChangeLog();
      if( isPasteOperation && (!log || !m_pIDChangesLog)) {
            kdError()<<" Cant addWidget to view in paste op because a log is not open"<<endl;
            return false;
      }
      int wX = pWidget -> getX();
      int wY = pWidget -> getY();
      bool xIsOutOfRange = (wX <= 0 || wX >= FloatingText::restrictPositionMax);
      bool yIsOutOfRange = (wY <= 0 || wY >= FloatingText::restrictPositionMax);
      if (xIsOutOfRange || yIsOutOfRange) {
            QString name = pWidget->getName();
            if (name.isEmpty()) {
                  FloatingText *ft = dynamic_cast<FloatingText*>(pWidget);
                  if (ft)
                        name = ft->getDisplayText();
            }
            kdDebug() << "UMLView::addWidget (" << name << " type="
                    << pWidget->getBaseType() << "): position (" << wX << ","
                    << wY << ") is out of range" << endl;
            if (xIsOutOfRange) {
                  pWidget->setX(0);
                  wX = 0;
            }
            if (yIsOutOfRange) {
                  pWidget->setY(0);
                  wY = 0;
            }
      }
      if( wX < m_Pos.x() )
            m_Pos.setX( wX );
      if( wY < m_Pos.y() )
            m_Pos.setY( wY );

      //see if we need a new id to match object
      switch( type ) {

            case wt_Class:
            case wt_Package:
            case wt_Component:
            case wt_Node:
            case wt_Artifact:
            case wt_Interface:
            case wt_Enum:
            case wt_Datatype:
            case wt_Actor:
            case wt_UseCase:
                  {
                        int id = pWidget -> getID();
                        int newID = log->findNewID( id );
                        if( newID == -1 ) {  // happens after a cut
                              if (id == -1)
                                    return false;
                              newID = id; //don't stop paste
                        } else
                              pWidget -> setID( newID );
                        UMLObject * pObject = m_pDoc -> findUMLObject( newID );
                        if( !pObject ) {
                              kdDebug() << "addWidget: Can't find UMLObject for id "
                                      << newID << endl;
                              return false;
                        }
                        pWidget -> setUMLObject( pObject );
                        //make sure it doesn't already exist.
                        if( findWidget( newID ) )
                              return true;//don't stop paste just because widget found.
                        m_WidgetList.append( pWidget );
                  }
                  break;

            case wt_Message:
            case wt_Note:
            case wt_Box:
            case wt_Text:
            case wt_State:
            case wt_Activity:
                  {
                        int newID = m_pDoc->assignNewID( pWidget->getID() );
                        pWidget->setID(newID);
                        if (type != wt_Message) {
                              m_WidgetList.append( pWidget );
                              return true;
                        }
                        // CHECK
                        // Handling of wt_Message:
                        MessageWidget *pMessage = static_cast<MessageWidget *>( pWidget );
                        if (pMessage == NULL) {
                              kdDebug() << "UMLView::addWidget(): pMessage is NULL" << endl;
                              return false;
                        }
                        ObjectWidget *objWidgetA = pMessage -> getWidget(A);
                        ObjectWidget *objWidgetB = pMessage -> getWidget(B);
                        int waID = objWidgetA -> getLocalID();
                        int wbID = objWidgetB -> getLocalID();
                        int newWAID = m_pIDChangesLog ->findNewID( waID );
                        int newWBID = m_pIDChangesLog ->findNewID( wbID );
                        if( newWAID == -1 || newWBID == -1 ) {
                              kdDebug() << "Error with ids : " << newWAID << " " << newWBID << endl;
                              return false;
                        }
                        // Assumption here is that the A/B objectwidgets and the textwidget
                        // are pristine in the sense that we may freely change their local IDs.
                        objWidgetA -> setLocalID( newWAID );
                        objWidgetB -> setLocalID( newWBID );
                        FloatingText *ft = pMessage->getFloatingText();
                        if (ft == NULL)
                              kdDebug() << "UMLView::addWidget: FloatingText of Message is NULL" << endl;
                        else if (ft->getID() == -1)
                              ft->setID( m_pDoc->getUniqueID() );
                        else {
                              int newTextID = m_pDoc->assignNewID( ft->getID() );
                              ft->setID( newTextID );
                        }
                        m_MessageList.append( pMessage );
                  }
                  break;

            case wt_Object:
                  {
                        ObjectWidget* pObjectWidget = static_cast<ObjectWidget*>(pWidget);
                        if (pObjectWidget == NULL) {
                              kdDebug() << "UMLView::addWidget(): pObjectWidget is NULL" << endl;
                              return false;
                        }
                        int newID = log->findNewID( pWidget -> getID() );
                        if (newID == -1) {
                              return false;
                        }
                        pObjectWidget -> setID( newID );
                        int nNewLocalID = getLocalID();
                        int nOldLocalID = pObjectWidget -> getLocalID();
                        m_pIDChangesLog->addIDChange( nOldLocalID, nNewLocalID );
                        pObjectWidget -> setLocalID( nNewLocalID );
                        UMLObject *pObject = m_pDoc -> findUMLObject( newID );
                        if( !pObject ) {
                              kdDebug() << "addWidget::Can't find UMLObject" << endl;
                              return false;
                        }
                        pWidget -> setUMLObject( pObject );
                        m_WidgetList.append( pWidget );
                  }
                  break;

            default:
                  kdDebug() << "Trying to add an invalid widget type" << endl;
                  return false;
                  break;
      }

      return true;
}


Generated by  Doxygen 1.6.0   Back to index