umbrello API Documentation

entity.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2003-2007                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "entity.h"
00014 // qt/kde includes
00015 #include <kdebug.h>
00016 #include <klocale.h>
00017 #include <kmessagebox.h>
00018 // app includes
00019 #include "entityattribute.h"
00020 #include "umldoc.h"
00021 #include "uml.h"
00022 #include "uniqueid.h"
00023 #include "clipboard/idchangelog.h"
00024 #include "dialogs/umlentityattributedialog.h"
00025 
00026 UMLEntity::UMLEntity(const QString& name, Uml::IDType id) : UMLClassifier(name, id) {
00027     init();
00028 }
00029 
00030 UMLEntity::~UMLEntity() {
00031     m_List.clear();
00032 }
00033 
00034 bool UMLEntity::operator==( UMLEntity& rhs ) {
00035     return UMLClassifier::operator==(rhs);
00036 }
00037 
00038 void UMLEntity::copyInto(UMLEntity *rhs) const
00039 {
00040     UMLClassifier::copyInto(rhs);
00041 }
00042 
00043 UMLObject* UMLEntity::clone() const
00044 {
00045     UMLEntity* clone = new UMLEntity();
00046     copyInto(clone);
00047 
00048     return clone;
00049 }
00050 
00051 void UMLEntity::init() {
00052     m_BaseType = Uml::ot_Entity;
00053 }
00054 
00055 UMLAttribute* UMLEntity::createAttribute(const QString &name /*=null*/, UMLObject *type /*=NULL*/) {
00056     Uml::IDType id = UniqueID::gen();
00057     QString currentName;
00058     if (name.isNull())  {
00059         currentName = uniqChildName(Uml::ot_EntityAttribute);
00060     } else {
00061         currentName = name;
00062     }
00063     const Settings::OptionState optionState = Settings::getOptionState();
00064     Uml::Visibility scope = optionState.classState.defaultAttributeScope;
00065     UMLEntityAttribute* newAttribute = new UMLEntityAttribute(this, currentName, id, scope, type);
00066 
00067     int button = QDialog::Accepted;
00068     bool goodName = false;
00069 
00070     //check for name.isNull() stops dialog being shown
00071     //when creating attribute via list view
00072     while (button==QDialog::Accepted && !goodName && name.isNull()) {
00073         UMLEntityAttributeDialog attributedialog(0, newAttribute);
00074         button = attributedialog.exec();
00075         QString name = newAttribute->getName();
00076 
00077         if(name.length() == 0) {
00078             KMessageBox::error(0, i18n("That is an invalid name."), i18n("Invalid Name"));
00079         } else if ( findChildObject(name) != NULL ) {
00080             KMessageBox::error(0, i18n("That name is already being used."), i18n("Not a Unique Name"));
00081         } else {
00082             goodName = true;
00083         }
00084     }
00085 
00086     if (button != QDialog::Accepted) {
00087         delete newAttribute;
00088         return NULL;
00089     }
00090 
00091     addEntityAttribute(newAttribute);
00092 
00093     UMLDoc *umldoc = UMLApp::app()->getDocument();
00094     umldoc->signalUMLObjectCreated(newAttribute);
00095     return newAttribute;
00096 }
00097 
00098 UMLObject* UMLEntity::addEntityAttribute(const QString& name, Uml::IDType id) {
00099     UMLEntityAttribute* literal = new UMLEntityAttribute(this, name, id);
00100     m_List.append(literal);
00101     UMLObject::emitModified();
00102     emit entityAttributeAdded(literal);
00103     connect(literal,SIGNAL(modified()),this,SIGNAL(modified()));
00104     return literal;
00105 }
00106 
00107 bool UMLEntity::addEntityAttribute(UMLEntityAttribute* attribute, IDChangeLog* Log /* = 0*/) {
00108     QString name = (QString)attribute->getName();
00109     if (findChildObject(name) == NULL) {
00110         attribute->parent()->removeChild(attribute);
00111         this->insertChild(attribute);
00112         m_List.append(attribute);
00113         UMLObject::emitModified();
00114         emit entityAttributeAdded(attribute);
00115         connect(attribute,SIGNAL(modified()),this,SIGNAL(modified()));
00116         return true;
00117     } else if (Log) {
00118         Log->removeChangeByNewID( attribute->getID() );
00119         delete attribute;
00120     }
00121     return false;
00122 }
00123 
00124 bool UMLEntity::addEntityAttribute(UMLEntityAttribute* attribute, int position) {
00125     QString name = (QString)attribute->getName();
00126     if (findChildObject(name) == NULL) {
00127         attribute->parent()->removeChild(attribute);
00128         this->insertChild(attribute);
00129         if ( position >= 0 && position <= (int)m_List.count() )  {
00130             m_List.insert(position,attribute);
00131         } else {
00132             m_List.append(attribute);
00133         }
00134         UMLObject::emitModified();
00135         emit entityAttributeAdded(attribute);
00136         connect(attribute,SIGNAL(modified()),this,SIGNAL(modified()));
00137         return true;
00138     }
00139     return false;
00140 }
00141 
00142 int UMLEntity::removeEntityAttribute(UMLClassifierListItem* literal) {
00143     if (!m_List.remove((UMLEntityAttribute*)literal)) {
00144         kDebug() << "can't find att given in list" << endl;
00145         return -1;
00146     }
00147     emit entityAttributeRemoved(literal);
00148     UMLObject::emitModified();
00149     // If we are deleting the object, then we don't need to disconnect..this is done auto-magically
00150     // for us by QObject. -b.t.
00151     // disconnect(a,SIGNAL(modified()),this,SIGNAL(modified()));
00152     delete literal;
00153     return m_List.count();
00154 }
00155 
00156 int UMLEntity::entityAttributes() {
00157     UMLClassifierListItemList entityAttributes = getFilteredList(Uml::ot_EntityAttribute);
00158     return entityAttributes.count();
00159 }
00160 
00161 void UMLEntity::signalEntityAttributeRemoved(UMLClassifierListItem *eattr) {
00162     emit entityAttributeRemoved(eattr);
00163 }
00164 
00165 bool UMLEntity::resolveRef() {
00166     bool success = UMLClassifier::resolveRef();
00167     for (UMLObjectListIt oit(m_List); oit.current(); ++oit) {
00168         UMLObject* obj = oit.current();
00169         if (obj->resolveRef()) {
00170             UMLClassifierListItem *cli = static_cast<UMLClassifierListItem*>(obj);
00171             if (cli->getBaseType() == Uml::ot_EntityAttribute)
00172                 emit entityAttributeAdded(cli);
00173         }
00174     }
00175     return success;
00176 }
00177 
00178 void UMLEntity::saveToXMI(QDomDocument& qDoc, QDomElement& qElement) {
00179     QDomElement entityElement = UMLObject::save("UML:Entity", qDoc);
00180     //save operations
00181     UMLClassifierListItemList entityAttributes = getFilteredList(Uml::ot_EntityAttribute);
00182     UMLClassifierListItem* pEntityAttribute = 0;
00183     for (UMLClassifierListItemListIt it(entityAttributes);
00184          (pEntityAttribute = it.current()) != NULL; ++it) {
00185         pEntityAttribute->saveToXMI(qDoc, entityElement);
00186     }
00187     qElement.appendChild(entityElement);
00188 }
00189 
00190 bool UMLEntity::load(QDomElement& element) {
00191     QDomNode node = element.firstChild();
00192     while( !node.isNull() ) {
00193         if (node.isComment()) {
00194             node = node.nextSibling();
00195             continue;
00196         }
00197         QDomElement tempElement = node.toElement();
00198         QString tag = tempElement.tagName();
00199         if (Uml::tagEq(tag, "EntityAttribute")) {   // for backward compatibility
00200             UMLEntityAttribute* pEntityAttribute = new UMLEntityAttribute(this);
00201             if( !pEntityAttribute->loadFromXMI(tempElement) ) {
00202                 return false;
00203             }
00204             m_List.append(pEntityAttribute);
00205         } else if (tag == "stereotype") {
00206             kDebug() << "UMLEntity::load(" << m_Name
00207             << "): losing old-format stereotype." << endl;
00208         } else {
00209             kWarning() << "unknown child type in UMLEntity::load" << endl;
00210         }
00211         node = node.nextSibling();
00212     }//end while
00213     return true;
00214 }
00215 
00216 
00217 #include "entity.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:57 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003