summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/enum.cpp
blob: 90682d514324c3ec0ac877383de789ccebca3483 (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
/***************************************************************************
 *                                                                         *
 *   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) 2003-2007                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

// own header
#include "enum.h"
// qt/kde includes
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
// app includes
#include "enumliteral.h"
#include "umldoc.h"
#include "uml.h"
#include "uniqueid.h"
#include "clipboard/idchangelog.h"

UMLEnum::UMLEnum(const TQString& name, Uml::IDType id) : UMLClassifier(name, id) {
    init();
}

UMLEnum::~UMLEnum() {
    m_List.clear();
}

bool UMLEnum::operator==( UMLEnum & rhs ) {
    return UMLClassifier::operator==(rhs);
}

void UMLEnum::copyInto(UMLEnum *rhs) const
{
    UMLClassifier::copyInto(rhs);
}

UMLObject* UMLEnum::clone() const
{
    UMLEnum *clone = new UMLEnum();
    copyInto(clone);

    return clone;
}

void UMLEnum::init() {
    m_BaseType = Uml::ot_Enum;
    setStereotype( "enum" );
}

UMLObject* UMLEnum::createEnumLiteral(const TQString& name) {
    Uml::IDType id = UniqueID::gen();
    TQString currentName;
    if (name.isNull())  {
        currentName = uniqChildName(Uml::ot_EnumLiteral);
    } else {
        currentName = name;
    }

    UMLEnumLiteral* newEnumLiteral = new UMLEnumLiteral(this, currentName);

    bool ok = true;
    bool goodName = false;

    //check for name.isNull() stops dialog being shown
    //when creating enum literal via list view
    while (ok && !goodName && name.isNull()) {
        ok = newEnumLiteral->showPropertiesDialog( UMLApp::app() );
        TQString name = newEnumLiteral->getName();

        if(name.length() == 0) {
            KMessageBox::error(0, i18n("That is an invalid name."), i18n("Invalid Name"));
        } else {
            goodName = true;
        }
    }

    if (!ok) {
        delete newEnumLiteral;
        return NULL;
    }

    addEnumLiteral(newEnumLiteral);

    UMLDoc *umldoc = UMLApp::app()->getDocument();
    umldoc->signalUMLObjectCreated(newEnumLiteral);
    return newEnumLiteral;
}

UMLObject* UMLEnum::addEnumLiteral(const TQString &name, Uml::IDType id) {
    UMLObject *el = UMLCanvasObject::findChildObject(name);
    if (el != NULL) {
        kDebug() << "UMLEnum::addEnumLiteral: " << name
                  << " is already present" << endl;
        return el;
    }
    UMLEnumLiteral* literal = new UMLEnumLiteral(this, name, id);
    m_List.append(literal);
    UMLObject::emitModified();
    emit enumLiteralAdded(literal);
    connect(literal,TQT_SIGNAL(modified()),this,TQT_SIGNAL(modified()));
    return literal;
}

bool UMLEnum::addEnumLiteral(UMLEnumLiteral* literal, IDChangeLog* Log /* = 0*/) {
    TQString name = (TQString)literal->getName();
    if (findChildObject(name) == NULL) {
        literal->parent()->removeChild(literal);
        this->insertChild(literal);
        m_List.append(literal);
        UMLObject::emitModified();
        emit enumLiteralAdded(literal);
        connect(literal,TQT_SIGNAL(modified()),this,TQT_SIGNAL(modified()));
        return true;
    } else if (Log) {
        Log->removeChangeByNewID( literal->getID() );
        delete literal;
    }
    return false;
}

bool UMLEnum::addEnumLiteral(UMLEnumLiteral* literal, int position) {
    TQString name = (TQString)literal->getName();
    if (findChildObject(name) == NULL) {
        literal->parent()->removeChild(literal);
        this->insertChild(literal);
        if ( position >= 0 && position <= (int)m_List.count() )  {
            m_List.insert(position,literal);
        } else {
            m_List.append(literal);
        }
        UMLObject::emitModified();
        emit enumLiteralAdded(literal);
        connect(literal,TQT_SIGNAL(modified()),this,TQT_SIGNAL(modified()));
        return true;
    }
    return false;
}

int UMLEnum::removeEnumLiteral(UMLEnumLiteral* literal) {
    if (!m_List.remove(literal)) {
        kDebug() << "can't find att given in list" << endl;
        return -1;
    }
    emit enumLiteralRemoved(literal);
    UMLObject::emitModified();
    // If we are deleting the object, then we don't need to disconnect..this is done auto-magically
    // for us by TQObject. -b.t.
    // disconnect(a,TQT_SIGNAL(modified()),this,TQT_SIGNAL(modified()));
    delete literal;
    return m_List.count();
}

int UMLEnum::enumLiterals() {
    return m_List.count();
}

void UMLEnum::signalEnumLiteralRemoved(UMLClassifierListItem *elit) {
    emit enumLiteralRemoved(elit);
}

void UMLEnum::saveToXMI(TQDomDocument& qDoc, TQDomElement& qElement) {
    TQDomElement enumElement = UMLObject::save("UML:Enumeration", qDoc);
    // save enum literals
    UMLClassifierListItemList enumLiterals = getFilteredList(Uml::ot_EnumLiteral);
    UMLClassifierListItem* pEnumLiteral = 0;
    for (UMLClassifierListItemListIt it(enumLiterals);
         (pEnumLiteral = it.current()) != NULL; ++it) {
        pEnumLiteral->saveToXMI(qDoc, enumElement);
    }
    qElement.appendChild(enumElement);
}

bool UMLEnum::load(TQDomElement& element) {
    TQDomNode node = element.firstChild();
    while( !node.isNull() ) {
        if (node.isComment()) {
            node = node.nextSibling();
            continue;
        }
        TQDomElement tempElement = node.toElement();
        TQString tag = tempElement.tagName();
        if (Uml::tagEq(tag, "EnumerationLiteral") ||
                Uml::tagEq(tag, "EnumLiteral")) {   // for backward compatibility
            UMLEnumLiteral* pEnumLiteral = new UMLEnumLiteral(this);
            if( !pEnumLiteral->loadFromXMI(tempElement) ) {
                return false;
            }
            m_List.append(pEnumLiteral);
        } else if (tag == "stereotype") {
            kDebug() << "UMLEnum::load(" << m_Name
            << "): losing old-format stereotype." << endl;
        } else {
            kWarning() << "unknown child type in UMLEnum::load" << endl;
        }
        node = node.nextSibling();
    }//end while
    return true;
}


#include "enum.moc"