umbrello API Documentation

umllistviewitem.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) 2002-2007                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "umllistviewitem.h"
00014 
00015 // system includes
00016 #include <cstdlib>
00017 
00018 // qt/kde includes
00019 #include <qfile.h>
00020 #include <qregexp.h>
00021 #include <kapplication.h>
00022 #include <klocale.h>
00023 #include <kmessagebox.h>
00024 #include <kdebug.h>
00025 
00026 // app includes
00027 #include "folder.h"
00028 #include "classifier.h"
00029 #include "template.h"
00030 #include "attribute.h"
00031 #include "operation.h"
00032 #include "umldoc.h"
00033 #include "umllistview.h"
00034 #include "umlobjectlist.h"
00035 #include "umlview.h"
00036 #include "model_utils.h"
00037 #include "uniqueid.h"
00038 #include "uml.h"
00039 
00040 UMLListView* UMLListViewItem::s_pListView = 0;
00041 
00042 UMLListViewItem::UMLListViewItem( UMLListView * parent, const QString &name,
00043                                   Uml::ListView_Type t, UMLObject* o)
00044         : QListViewItem(parent, name) {
00045     init(parent);
00046     m_Type = t;
00047     m_pObject = o;
00048     if (o)
00049         m_nId = o->getID();
00050     setIcon(Uml::it_Home);
00051     setText( name );
00052     setRenameEnabled( 0, false );
00053 }
00054 
00055 UMLListViewItem::UMLListViewItem(UMLListView * parent)
00056         : QListViewItem(parent) {
00057     init(parent);
00058     if (parent == NULL)
00059         kDebug() << "UMLListViewItem constructor called with a NULL listview parent" << endl;
00060 }
00061 
00062 UMLListViewItem::UMLListViewItem(UMLListViewItem * parent)
00063         : QListViewItem(parent)  {
00064     init();
00065 }
00066 
00067 UMLListViewItem::UMLListViewItem(UMLListViewItem * parent, const QString &name, Uml::ListView_Type t,UMLObject*o)
00068         : QListViewItem(parent, name) {
00069     init();
00070     m_Type = t;
00071     m_pObject = o;
00072     if( !o ) {
00073         m_nId = Uml::id_None;
00074         updateFolder();
00075     } else {
00076         UMLClassifierListItem *umlchild = dynamic_cast<UMLClassifierListItem*>(o);
00077         if (umlchild)
00078             parent->addClassifierListItem(umlchild, this);
00079         updateObject();
00080         m_nId = o->getID();
00081     }
00082     setRenameEnabled( 0, !Model_Utils::typeIsRootView(t) );
00083     setText( name );
00084 }
00085 
00086 UMLListViewItem::UMLListViewItem(UMLListViewItem * parent, const QString &name, Uml::ListView_Type t,Uml::IDType id)
00087         : QListViewItem(parent, name) {
00088     init();
00089     m_Type = t;
00090     m_nId = id;
00091     switch (m_Type) {
00092     case Uml::lvt_Collaboration_Diagram:
00093         setIcon(Uml::it_Diagram_Collaboration);
00094         break;
00095     case Uml::lvt_Class_Diagram:
00096         setIcon(Uml::it_Diagram_Class);
00097         break;
00098     case Uml::lvt_State_Diagram:
00099         setIcon(Uml::it_Diagram_State);
00100         break;
00101     case Uml::lvt_Activity_Diagram:
00102         setIcon(Uml::it_Diagram_Activity);
00103         break;
00104     case Uml::lvt_Sequence_Diagram:
00105         setIcon(Uml::it_Diagram_Sequence);
00106         break;
00107     case Uml::lvt_Component_Diagram:
00108         setIcon(Uml::it_Diagram_Component);
00109         break;
00110     case Uml::lvt_Deployment_Diagram:
00111         setIcon(Uml::it_Diagram_Deployment);
00112         break;
00113     case Uml::lvt_UseCase_Diagram:
00114         setIcon(Uml::it_Diagram_Usecase);
00115         break;
00116     default:
00117         setIcon(Uml::it_Diagram);
00118     }
00119     /*
00120         Constructor also used by folder so just make sure we don't need to
00121         to set pixmap to folder.  doesn't hurt diagrams.
00122     */
00123     updateFolder();
00124     setText( name );
00125     setRenameEnabled( 0, true );
00126 }
00127 
00128 UMLListViewItem::~UMLListViewItem() {}
00129 
00130 void UMLListViewItem::init(UMLListView * parent) {
00131     m_Type = Uml::lvt_Unknown;
00132     m_bCreating = false;
00133     m_pObject = NULL;
00134     m_nId = Uml::id_None;
00135     m_nChildren = 0;
00136     if (s_pListView == NULL && parent != NULL) {
00137         kDebug() << "UMLListViewItem::init: s_pListView still NULL, setting it now "
00138                   << endl;
00139         s_pListView = parent;
00140     }
00141 }
00142 
00143 Uml::ListView_Type UMLListViewItem::getType() const {
00144     return m_Type;
00145 }
00146 
00147 void UMLListViewItem::addClassifierListItem(UMLClassifierListItem *child, UMLListViewItem *childItem) {
00148     m_comap[child] = childItem;
00149 }
00150 
00151 void UMLListViewItem::deleteChildItem(UMLClassifierListItem *child) {
00152     UMLListViewItem *childItem = findChildObject(child);
00153     if (childItem == NULL) {
00154         kError() << "UMLListViewItem::deleteChildItem(" << child->getName()
00155                   << "): child listview item not found" << endl;
00156         return;
00157     }
00158     m_comap.remove(child);
00159     delete childItem;
00160 }
00161 
00162 Uml::IDType UMLListViewItem::getID() const {
00163     if (m_pObject)
00164         return m_pObject->getID();
00165     return m_nId;
00166 }
00167 
00168 void UMLListViewItem::setID(Uml::IDType id) {
00169     if (m_pObject) {
00170         Uml::IDType oid = m_pObject->getID();
00171         if (id != Uml::id_None && oid != id)
00172             kDebug() << "UMLListViewItem::setID: new id " << ID2STR(id)
00173                 << " does not agree with object id " << ID2STR(oid) << endl;
00174     }
00175     m_nId = id;
00176 }
00177 
00178 bool UMLListViewItem::isOwnParent(Uml::IDType listViewItemID) {
00179     QListViewItem *lvi = (QListViewItem*)s_pListView->findItem(listViewItemID);
00180     if (lvi == NULL) {
00181         kError() << "UMLListViewItem::isOwnParent: ListView->findItem("
00182             << ID2STR(listViewItemID) << ") returns NULL" << endl;
00183         return true;
00184     }
00185     for (QListViewItem *self = (QListViewItem*)this; self; self = self->parent()) {
00186         if (lvi == self)
00187             return true;
00188     }
00189     return false;
00190 }
00191 
00192 void UMLListViewItem::updateObject() {
00193     if( m_pObject == NULL )
00194         return;
00195 
00196     Uml::Visibility scope = m_pObject->getVisibility();
00197     Uml::Object_Type ot = m_pObject->getBaseType();
00198     QString modelObjText = m_pObject->getName();
00199     if (Model_Utils::isClassifierListitem(ot)) {
00200         UMLClassifierListItem *pNarrowed = static_cast<UMLClassifierListItem*>(m_pObject);
00201         modelObjText = pNarrowed->toString(Uml::st_SigNoVis);
00202     }
00203     setText(modelObjText);
00204 
00205     Uml::Icon_Type icon = Uml::it_Home;
00206     switch (ot) {
00207     case Uml::ot_Package:
00208         if (m_pObject->getStereotype() == "subsystem")
00209             icon = Uml::it_Subsystem;
00210         else
00211             icon = Uml::it_Package;
00212         break;
00213 /*
00214     case Uml::ot_Folder:
00215         {
00216             Uml::ListView_Type lvt = Model_Utils::convert_OT_LVT(m_pObject);
00217             icon = Model_Utils::convert_LVT_IT(lvt);
00218         }
00219         break;
00220  */
00221     case Uml::ot_Operation:
00222         if (scope == Uml::Visibility::Public)
00223             icon = Uml::it_Public_Method;
00224         else if (scope == Uml::Visibility::Private)
00225             icon = Uml::it_Private_Method;
00226         else if (scope == Uml::Visibility::Implementation)
00227             icon = Uml::it_Private_Method;
00228         else
00229             icon = Uml::it_Protected_Method;
00230         break;
00231 
00232     case Uml::ot_Attribute:
00233     case Uml::ot_EntityAttribute:
00234         if (scope == Uml::Visibility::Public)
00235             icon = Uml::it_Public_Attribute;
00236         else if (scope == Uml::Visibility::Private)
00237             icon = Uml::it_Private_Attribute;
00238         else if (scope == Uml::Visibility::Implementation)
00239             icon = Uml::it_Private_Attribute;
00240         else
00241             icon = Uml::it_Protected_Attribute;
00242         break;
00243     default:
00244         icon = Model_Utils::convert_LVT_IT(m_Type);
00245         break;
00246     }//end switch
00247     if (icon)
00248         setIcon(icon);
00249 }
00250 
00251 void UMLListViewItem::updateFolder() {
00252     Uml::Icon_Type icon = Model_Utils::convert_LVT_IT(m_Type);
00253     if (icon) {
00254         if (Model_Utils::typeIsFolder(m_Type))
00255             icon = (Uml::Icon_Type)((int)icon + (int)isOpen());
00256         setIcon(icon);
00257     }
00258 }
00259 
00260 void UMLListViewItem::setOpen( bool open ) {
00261     QListViewItem::setOpen( open );
00262     updateFolder();
00263 }
00264 
00265 void UMLListViewItem::setText(const QString &newText) {
00266     m_Label = newText;
00267     QListViewItem::setText(0, newText);
00268 }
00269 
00270 QString UMLListViewItem::getText() const {
00271     return m_Label;
00272 }
00273 
00274 void UMLListViewItem::setIcon(Uml::Icon_Type iconType) {
00275     setPixmap(0, s_pListView->getPixmap(iconType));
00276 }
00277 
00278 void UMLListViewItem::okRename( int col ) {
00279     QListViewItem::okRename( col );
00280     UMLDoc* doc = s_pListView->getDocument();
00281     if (m_bCreating) {
00282         m_bCreating = false;
00283         QString savedLabel = m_Label;
00284         m_Label = text(col);
00285         if ( s_pListView->itemRenamed( this, col ) ) {
00286             s_pListView->ensureItemVisible(this);
00287             doc->setModified(true);
00288         } else {
00289             delete this;
00290         }
00291         return;
00292     }
00293     QString newText = text( col );
00294     if ( newText == m_Label ) {
00295         return;
00296     }
00297     if( newText.isEmpty() ) {
00298         cancelRenameWithMsg();
00299         return;
00300     }
00301     switch( m_Type ) {
00302     case Uml::lvt_UseCase:
00303     case Uml::lvt_Actor:
00304     case Uml::lvt_Class:
00305     case Uml::lvt_Package:
00306     case Uml::lvt_UseCase_Folder:
00307     case Uml::lvt_Logical_Folder:
00308     case Uml::lvt_Component_Folder:
00309     case Uml::lvt_Deployment_Folder:
00310     case Uml::lvt_EntityRelationship_Folder:
00311     case Uml::lvt_Interface:
00312     case Uml::lvt_Datatype:
00313     case Uml::lvt_Enum:
00314     case Uml::lvt_Subsystem:
00315     case Uml::lvt_Component:
00316     case Uml::lvt_Node:
00317         if (m_pObject == NULL || !doc->isUnique(newText)) {
00318             cancelRenameWithMsg();
00319             return;
00320         }
00321         m_pObject -> setName( newText );
00322         doc->setModified(true);
00323         m_Label = newText;
00324         break;
00325 
00326     case Uml::lvt_Operation:
00327         {
00328             if (m_pObject == NULL) {
00329                 cancelRenameWithMsg();
00330                 return;
00331             }
00332             UMLOperation *op = static_cast<UMLOperation*>(m_pObject);
00333             UMLClassifier *parent = static_cast<UMLClassifier *>( op -> parent() );
00334             Model_Utils::OpDescriptor od;
00335             Model_Utils::Parse_Status st = Model_Utils::parseOperation(newText, od, parent);
00336             if (st == Model_Utils::PS_OK) {
00337                 // TODO: Check that no operation with the exact same profile exists.
00338                 op->setName( od.m_name );
00339                 op->setType( od.m_pReturnType );
00340                 UMLAttributeList parmList = op->getParmList();
00341                 const unsigned newParmListCount = parmList.count();
00342                 if (newParmListCount > od.m_args.count()) {
00343                     // Remove parameters at end of of list that no longer exist.
00344                     for (unsigned i = od.m_args.count(); i < newParmListCount; i++) {
00345                         UMLAttribute *a = parmList.at(i);
00346                         op->removeParm(a, false);
00347                     }
00348                 }
00349                 Model_Utils::NameAndType_ListIt lit = od.m_args.begin();
00350                 for (unsigned i = 0; lit != od.m_args.end(); ++lit, ++i) {
00351                     const Model_Utils::NameAndType& nm_tp = *lit;
00352                     UMLAttribute *a;
00353                     if (i < newParmListCount) {
00354                         a = parmList.at(i);
00355                     } else {
00356                         a = new UMLAttribute(op);
00357                         a->setID( UniqueID::gen() );
00358                     }
00359                     a->setName(nm_tp.m_name);
00360                     a->setType(nm_tp.m_type);
00361                     a->setParmKind(nm_tp.m_direction);
00362                     a->setInitialValue(nm_tp.m_initialValue);
00363                     if (i >= newParmListCount) {
00364                         op->addParm(a);
00365                     }
00366                 }
00367                 m_Label = op->toString(Uml::st_SigNoVis);
00368             } else {
00369                 KMessageBox::error( kapp->mainWidget(),
00370                                     Model_Utils::psText(st),
00371                                     i18n("Rename canceled") );
00372             }
00373             QListViewItem::setText(0, m_Label);
00374             break;
00375         }
00376 
00377     case Uml::lvt_Attribute:
00378     case Uml::lvt_EntityAttribute:
00379         {
00380             if (m_pObject == NULL) {
00381                 cancelRenameWithMsg();
00382                 return;
00383             }
00384             UMLClassifier *parent = static_cast<UMLClassifier*>(m_pObject->parent());
00385             Model_Utils::NameAndType nt;
00386             Uml::Visibility vis;
00387             Model_Utils::Parse_Status st;
00388             st = Model_Utils::parseAttribute(newText, nt, parent, &vis);
00389             if (st == Model_Utils::PS_OK) {
00390                 UMLObject *exists = parent->findChildObject(newText);
00391                 if (exists) {
00392                     cancelRenameWithMsg();
00393                     return;
00394                 }
00395                 m_pObject->setName(nt.m_name);
00396                 UMLAttribute *pAtt = static_cast<UMLAttribute*>(m_pObject);
00397                 pAtt->setType(nt.m_type);
00398                 pAtt->setVisibility(vis);
00399                 pAtt->setParmKind(nt.m_direction);
00400                 pAtt->setInitialValue(nt.m_initialValue);
00401                 m_Label = pAtt->toString(Uml::st_SigNoVis);
00402             } else {
00403                 KMessageBox::error( kapp->mainWidget(),
00404                                     Model_Utils::psText(st),
00405                                     i18n("Rename canceled") );
00406             }
00407             QListViewItem::setText(0, m_Label);
00408             break;
00409         }
00410 
00411     case Uml::lvt_Template:
00412         {
00413             if (m_pObject == NULL) {
00414                 cancelRenameWithMsg();
00415                 return;
00416             }
00417             UMLClassifier *parent = static_cast<UMLClassifier*>(m_pObject->parent());
00418             Model_Utils::NameAndType nt;
00419             Model_Utils::Parse_Status st = Model_Utils::parseTemplate(newText, nt, parent);
00420             if (st == Model_Utils::PS_OK) {
00421                 UMLObject *exists = parent->findChildObject(newText);
00422                 if (exists) {
00423                     cancelRenameWithMsg();
00424                     return;
00425                 }
00426                 m_pObject->setName(nt.m_name);
00427                 UMLTemplate *tmpl = static_cast<UMLTemplate*>(m_pObject);
00428                 tmpl->setType(nt.m_type);
00429                 m_Label = tmpl->toString(Uml::st_SigNoVis);
00430             } else {
00431                 KMessageBox::error( kapp->mainWidget(),
00432                                     Model_Utils::psText(st),
00433                                     i18n("Rename canceled") );
00434             }
00435             QListViewItem::setText(0, m_Label);
00436             break;
00437         }
00438 
00439     case Uml::lvt_UseCase_Diagram:
00440     case Uml::lvt_Class_Diagram:
00441     case Uml::lvt_Sequence_Diagram:
00442     case Uml::lvt_Collaboration_Diagram:
00443     case Uml::lvt_State_Diagram:
00444     case Uml::lvt_Activity_Diagram:
00445     case Uml::lvt_Component_Diagram:
00446     case Uml::lvt_Deployment_Diagram:
00447         {
00448             UMLView *view = doc -> findView( getID() );
00449             if (view == NULL) {
00450                 cancelRenameWithMsg();
00451                 return;
00452             }
00453             UMLView *anotherView = doc -> findView( view->getType(), newText );
00454             if( anotherView && anotherView -> getID() == getID() )
00455                 anotherView = 0;
00456             if (anotherView) {
00457                 cancelRenameWithMsg();
00458                 return;
00459             }
00460             view->setName( newText );
00461             setText(newText);
00462             doc->signalDiagramRenamed(view);
00463             break;
00464         }
00465     default:
00466         KMessageBox::error( kapp->mainWidget() ,
00467                             i18n("Renaming an item of listview type %1 is not yet implemented.").arg(m_Type),
00468                             i18n("Function Not Implemented") );
00469         QListViewItem::setText(0, m_Label);
00470         break;
00471     }
00472     doc->setModified(true);
00473 }
00474 
00475 void UMLListViewItem::cancelRenameWithMsg() {
00476     KMessageBox::error( kapp->mainWidget() ,
00477                         i18n("The name you entered was invalid.\nRenaming process has been canceled."),
00478                         i18n("Name Not Valid") );
00479     QListViewItem::setText(0, m_Label);
00480 }
00481 
00482 void UMLListViewItem::cancelRename(int col) {
00483     QListViewItem::cancelRename(col);
00484     if (m_bCreating) {
00485         s_pListView->cancelRename(this);
00486     }
00487 }
00488 
00489 // Sort the listview items by type and position within the corresponding list
00490 // of UMLObjects. If the item does not have an UMLObject then place it last.
00491 int UMLListViewItem::compare(QListViewItem *other, int col, bool ascending) const
00492 {
00493     UMLListViewItem *ulvi = static_cast<UMLListViewItem*>(other);
00494     Uml::ListView_Type ourType = getType();
00495     Uml::ListView_Type otherType = ulvi->getType();
00496 
00497     if ( ourType < otherType )
00498         return -1;
00499     if ( ourType > otherType )
00500         return 1;
00501     // ourType == otherType
00502     const bool subItem = Model_Utils::typeIsClassifierList(ourType);
00503     const int alphaOrder = key(col, ascending).compare(other->key(col, ascending));
00504     int retval = 0;
00505     QString dbgPfx = "compare(type=" + QString::number((int)ourType)
00506                    + ", self=" + getText() + ", other=" + ulvi->getText()
00507                    + "): return ";
00508     UMLObject *otherObj = ulvi->getUMLObject();
00509     if (m_pObject == NULL) {
00510         retval = (subItem ? 1 : alphaOrder);
00511 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00512         kDebug() << dbgPfx << retval << " because (m_pObject==NULL)" << endl;
00513 #endif
00514         return retval;
00515     }
00516     if (otherObj == NULL) {
00517         retval = (subItem ? -1 : alphaOrder);
00518 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00519         kDebug() << dbgPfx << retval << " because (otherObj==NULL)" << endl;
00520 #endif
00521         return retval;
00522     }
00523     UMLClassifier *ourParent = dynamic_cast<UMLClassifier*>(m_pObject->parent());
00524     UMLClassifier *otherParent = dynamic_cast<UMLClassifier*>(otherObj->parent());
00525     if (ourParent == NULL) {
00526         retval = (subItem ? 1 : alphaOrder);
00527 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00528         kDebug() << dbgPfx << retval << " because (ourParent==NULL)" << endl;
00529 #endif
00530         return retval;
00531     }
00532     if (otherParent == NULL) {
00533         retval = (subItem ? -1 : alphaOrder);
00534 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00535         kDebug() << dbgPfx << retval << " because (otherParent==NULL)" << endl;
00536 #endif
00537         return retval;
00538     }
00539     if (ourParent != otherParent) {
00540         retval = (subItem ? 0 : alphaOrder);
00541 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00542         kDebug() << dbgPfx << retval << " because (ourParent != otherParent)" << endl;
00543 #endif
00544         return retval;
00545     }
00546     UMLClassifierListItem *thisUmlItem = dynamic_cast<UMLClassifierListItem*>(m_pObject);
00547     UMLClassifierListItem *otherUmlItem = dynamic_cast<UMLClassifierListItem*>(otherObj);
00548     if (thisUmlItem == NULL) {
00549         retval = (subItem ? 1 : alphaOrder);
00550 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00551         kDebug() << dbgPfx << retval << " because (thisUmlItem==NULL)" << endl;
00552 #endif
00553         return retval;
00554     }
00555     if (otherUmlItem == NULL) {
00556         retval = (subItem ? -1 : alphaOrder);
00557 #ifdef DEBUG_LVITEM_INSERTION_ORDER
00558         kDebug() << dbgPfx << retval << " because (otherUmlItem==NULL)" << endl;
00559 #endif
00560         return retval;
00561     }
00562     UMLClassifierListItemList items = ourParent->getFilteredList(thisUmlItem->getBaseType());
00563     int myIndex = items.findRef(thisUmlItem);
00564     int otherIndex = items.findRef(otherUmlItem);
00565     if (myIndex < 0) {
00566         retval = (subItem ? -1 : alphaOrder);
00567         kError() << dbgPfx << retval << " because (myIndex < 0)" << endl;
00568         return retval;
00569     }
00570     if (otherIndex < 0) {
00571         retval = (subItem ? 1 : alphaOrder);
00572         kError() << dbgPfx << retval << " because (otherIndex < 0)" << endl;
00573         return retval;
00574     }
00575     return (myIndex < otherIndex ? -1 : myIndex > otherIndex ? 1 : 0);
00576 }
00577 
00578 UMLListViewItem* UMLListViewItem::deepCopy(UMLListViewItem *newParent) {
00579     QString nm = getText();
00580     Uml::ListView_Type t = getType();
00581     UMLObject *o = getUMLObject();
00582     UMLListViewItem* newItem;
00583     if (o)
00584         newItem = new UMLListViewItem(newParent, nm, t, o);
00585     else
00586         newItem = new UMLListViewItem(newParent, nm, t, m_nId);
00587     UMLListViewItem *childItem = static_cast<UMLListViewItem*>(firstChild());
00588     while (childItem) {
00589         childItem->deepCopy(newItem);
00590         childItem = static_cast<UMLListViewItem*>(childItem->nextSibling());
00591     }
00592     return newItem;
00593 }
00594 
00595 UMLListViewItem* UMLListViewItem::findUMLObject(const UMLObject *o) {
00596     if (m_pObject == o)
00597         return this;
00598     UMLListViewItem *childItem = static_cast<UMLListViewItem*>(firstChild());
00599     while (childItem) {
00600         UMLListViewItem *inner = childItem->findUMLObject(o);
00601         if (inner)
00602             return inner;
00603         childItem = static_cast<UMLListViewItem*>(childItem->nextSibling());
00604     }
00605     return NULL;
00606 }
00607 
00608 UMLListViewItem* UMLListViewItem::findChildObject(UMLClassifierListItem *cli) {
00609     ChildObjectMap::iterator it = m_comap.find(cli);
00610     if (it != m_comap.end()) {
00611         return *it;
00612     }
00613     return NULL;
00614 }
00615 
00616 UMLListViewItem * UMLListViewItem::findItem(Uml::IDType id) {
00617     if (getID() == id)
00618         return this;
00619     UMLListViewItem *childItem = static_cast<UMLListViewItem*>(firstChild());
00620     while (childItem) {
00621         UMLListViewItem *inner = childItem->findItem(id);
00622         if (inner)
00623             return inner;
00624         childItem = static_cast<UMLListViewItem*>(childItem->nextSibling());
00625     }
00626     return NULL;
00627 }
00628 
00629 void UMLListViewItem::saveToXMI( QDomDocument & qDoc, QDomElement & qElement) {
00630     QDomElement itemElement = qDoc.createElement( "listitem" );
00631     Uml::IDType id = getID();
00632     QString idStr = ID2STR(id);
00633     //kDebug() << "UMLListViewItem::saveToXMI: id = " << idStr
00634     //    << ", type = " << m_Type << endl;
00635     if (id != Uml::id_None)
00636         itemElement.setAttribute( "id", idStr );
00637     itemElement.setAttribute( "type", m_Type );
00638     UMLDoc *umldoc = s_pListView->getDocument();
00639     UMLFolder *extFolder = NULL;
00640     if (m_pObject == NULL) {
00641         if (! Model_Utils::typeIsDiagram(m_Type) && m_Type != Uml::lvt_View)
00642             kError() << "UMLListViewItem::saveToXMI(" << m_Label
00643                 << "): m_pObject is NULL" << endl;
00644         itemElement.setAttribute( "label", m_Label );
00645     } else if (m_pObject->getID() == Uml::id_None) {
00646         if (m_Label.isEmpty()) {
00647             kDebug() << "UMLListViewItem::saveToXMI(): Skipping empty item"
00648                 << endl;
00649             return;
00650         }
00651         kDebug() << "UMLListViewItem::saveToXMI(): saving local label "
00652             << m_Label << " because umlobject ID is not set" << endl;
00653         itemElement.setAttribute( "label", m_Label );
00654     } else if (m_pObject->getBaseType() == Uml::ot_Folder) {
00655         extFolder = static_cast<UMLFolder*>(m_pObject);
00656         if (!extFolder->getFolderFile().isEmpty()) {
00657             itemElement.setAttribute("open", "0");
00658             qElement.appendChild(itemElement);
00659             return;
00660         }
00661     }
00662     itemElement.setAttribute("open", isOpen());
00663     QDomElement folderRoot;
00664     UMLListViewItem *childItem = static_cast<UMLListViewItem*>( firstChild() );
00665     while (childItem) {
00666         childItem->saveToXMI(qDoc, itemElement);
00667         childItem = dynamic_cast<UMLListViewItem *> ( childItem->nextSibling() );
00668     }
00669     qElement.appendChild( itemElement );
00670 }
00671 
00672 bool UMLListViewItem::loadFromXMI(QDomElement& qElement) {
00673     QString id = qElement.attribute( "id", "-1" );
00674     QString type = qElement.attribute( "type", "-1" );
00675     QString label = qElement.attribute( "label", "" );
00676     QString open = qElement.attribute( "open", "1" );
00677     if (!label.isEmpty())
00678         setText( label );
00679     else if (id == "-1") {
00680         kError() << "UMLListViewItem::loadFromXMI: Item of type "
00681             << type << " has neither ID nor label" << endl;
00682         return false;
00683     }
00684 
00685     m_nChildren = qElement.childNodes().count();
00686 
00687     m_nId = STR2ID(id);
00688     if (m_nId != Uml::id_None)
00689         m_pObject = s_pListView->getDocument()->findObjectById( m_nId );
00690     m_Type = (Uml::ListView_Type)(type.toInt());
00691     if (m_pObject)
00692         updateObject();
00693     setOpen( (bool)open.toInt() );
00694     return true;
00695 }
00696 
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:08:01 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003