umbrello API Documentation

umlattributedialog.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-2006                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "umlattributedialog.h"
00014 
00015 // qt includes
00016 #include <qlayout.h>
00017 #include <qlineedit.h>
00018 #include <qcheckbox.h>
00019 #include <qgroupbox.h>
00020 #include <qbuttongroup.h>
00021 #include <qradiobutton.h>
00022 #include <qlabel.h>
00023 
00024 // kde includes
00025 #include <kcombobox.h>
00026 #include <kcompletion.h>
00027 #include <klocale.h>
00028 #include <kmessagebox.h>
00029 #include <kdebug.h>
00030 
00031 // app includes
00032 #include "../attribute.h"
00033 #include "../classifier.h"
00034 #include "../template.h"
00035 #include "../umldoc.h"
00036 #include "../uml.h"
00037 #include "../dialog_utils.h"
00038 #include "../object_factory.h"
00039 #include "../codeimport/import_utils.h"
00040 
00041 UMLAttributeDialog::UMLAttributeDialog( QWidget * pParent, UMLAttribute * pAttribute )
00042         : KDialogBase( Plain, i18n("Attribute Properties"), Help | Ok | Cancel , Ok, pParent, "_UMLATTRIBUTEDLG_", true, true) {
00043     m_pAttribute = pAttribute;
00044     setupDialog();
00045 }
00046 
00047 UMLAttributeDialog::~UMLAttributeDialog() {}
00048 
00049 void UMLAttributeDialog::setupDialog() {
00050     UMLDoc * pDoc = UMLApp::app()->getDocument();
00051     int margin = fontMetrics().height();
00052 
00053     QVBoxLayout * mainLayout = new QVBoxLayout( plainPage() );
00054 
00055     m_pValuesGB = new QGroupBox(i18n("General Properties"), plainPage() );
00056     QGridLayout * valuesLayout = new QGridLayout(m_pValuesGB, 5, 2);
00057     valuesLayout -> setMargin(margin);
00058     valuesLayout -> setSpacing(10);
00059 
00060     m_pTypeL = new QLabel(i18n("&Type:"), m_pValuesGB);
00061     valuesLayout -> addWidget(m_pTypeL, 0, 0);
00062 
00063     m_pTypeCB = new KComboBox(true, m_pValuesGB);
00064     valuesLayout -> addWidget(m_pTypeCB, 0, 1);
00065     m_pTypeL->setBuddy(m_pTypeCB);
00066 
00067     Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 1,
00068                                     m_pNameL, i18n("&Name:"),
00069                                     m_pNameLE, m_pAttribute->getName() );
00070 
00071     Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 2,
00072                                     m_pInitialL, i18n("&Initial value:"),
00073                                     m_pInitialLE, m_pAttribute->getInitialValue() );
00074 
00075     Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 3,
00076                                     m_pStereoTypeL, i18n("Stereotype name:"),
00077                                     m_pStereoTypeLE, m_pAttribute->getStereotype() );
00078 
00079     m_pStaticCB = new QCheckBox( i18n("Classifier &scope (\"static\")"), m_pValuesGB );
00080     m_pStaticCB -> setChecked( m_pAttribute -> getStatic() );
00081     valuesLayout -> addWidget(m_pStaticCB, 4, 0);
00082 
00083 
00084     mainLayout -> addWidget(m_pValuesGB);
00085 
00086 
00087     m_pScopeBG = new QButtonGroup(i18n("Visibility"), plainPage() );
00088     QHBoxLayout * scopeLayout = new QHBoxLayout(m_pScopeBG);
00089     scopeLayout -> setMargin(margin);
00090 
00091     m_pPublicRB = new QRadioButton(i18n("&Public"), m_pScopeBG);
00092     scopeLayout -> addWidget(m_pPublicRB);
00093 
00094     m_pPrivateRB = new QRadioButton(i18n("P&rivate"), m_pScopeBG);
00095     scopeLayout -> addWidget(m_pPrivateRB);
00096 
00097     m_pProtectedRB = new QRadioButton(i18n("Prot&ected"), m_pScopeBG);
00098     scopeLayout -> addWidget(m_pProtectedRB);
00099 
00100     m_pImplementationRB = new QRadioButton(i18n("I&mplementation"), m_pScopeBG);
00101     scopeLayout -> addWidget(m_pImplementationRB);
00102 
00103     mainLayout -> addWidget(m_pScopeBG);
00104     Uml::Visibility scope = m_pAttribute -> getVisibility();
00105     if( scope == Uml::Visibility::Public )
00106         m_pPublicRB -> setChecked( true );
00107     else if( scope == Uml::Visibility::Private )
00108           m_pPrivateRB -> setChecked( true );
00109     else if( scope == Uml::Visibility::Protected )
00110           m_pProtectedRB -> setChecked( true );
00111     else if( scope == Uml::Visibility::Implementation )
00112           m_pImplementationRB -> setChecked( true );
00113 
00114     m_pTypeCB->setDuplicatesEnabled(false);//only allow one of each type in box
00115     m_pTypeCB->setCompletionMode( KGlobalSettings::CompletionPopup );
00116 
00117     //now add the Concepts
00118     UMLClassifierList namesList( pDoc->getConcepts() );
00119     UMLClassifier* obj;
00120     for (obj=namesList.first(); obj!=0; obj=namesList.next()) {
00121         insertType( obj->getFullyQualifiedName() );
00122     }
00123 
00124     //work out which one to select
00125     int typeBoxCount = 0;
00126     bool foundType = false;
00127     while (typeBoxCount < m_pTypeCB->count() && foundType == false) {
00128         QString typeBoxString = m_pTypeCB->text(typeBoxCount);
00129         if ( typeBoxString == m_pAttribute->getTypeName() ) {
00130             foundType = true;
00131             m_pTypeCB->setCurrentItem(typeBoxCount);
00132         } else {
00133             typeBoxCount++;
00134         }
00135     }
00136 
00137     if (!foundType) {
00138         insertType( m_pAttribute->getTypeName(), 0 );
00139         m_pTypeCB->setCurrentItem(0);
00140     }
00141 
00142     m_pNameLE->setFocus();
00143     connect( m_pNameLE, SIGNAL( textChanged ( const QString & ) ), SLOT( slotNameChanged( const QString & ) ) );
00144     slotNameChanged(m_pNameLE->text() );
00145 }
00146 
00147 void UMLAttributeDialog::slotNameChanged( const QString &_text )
00148 {
00149     enableButtonOK( !_text.isEmpty() );
00150 }
00151 
00152 bool UMLAttributeDialog::apply() {
00153     QString name = m_pNameLE->text();
00154     if (name.isEmpty()) {
00155         KMessageBox::error(this, i18n("You have entered an invalid attribute name."),
00156                            i18n("Attribute Name Invalid"), false);
00157         m_pNameLE->setText( m_pAttribute->getName() );
00158         return false;
00159     }
00160     UMLClassifier * pConcept = dynamic_cast<UMLClassifier *>( m_pAttribute->parent() );
00161     UMLObject *o = pConcept->findChildObject(name);
00162     if (o && o != m_pAttribute) {
00163         KMessageBox::error(this, i18n("The attribute name you have chosen is already being used in this operation."),
00164                            i18n("Attribute Name Not Unique"), false);
00165         m_pNameLE->setText( m_pAttribute->getName() );
00166         return false;
00167     }
00168     m_pAttribute->setName(name);
00169     Uml::Visibility scope = Uml::Visibility::Protected;
00170     if ( m_pPublicRB->isChecked() ) {
00171         scope = Uml::Visibility::Public;
00172     } else if ( m_pPrivateRB->isChecked() ) {
00173         scope = Uml::Visibility::Private;
00174     } else if ( m_pImplementationRB->isChecked() ) {
00175         scope = Uml::Visibility::Implementation;
00176     }
00177     m_pAttribute->setVisibility(scope);
00178     // Set the scope as the default in the option state
00179     Settings::OptionState optionState = Settings::getOptionState();
00180     optionState.classState.defaultAttributeScope = scope;
00181     Settings::setOptionState(optionState);
00182 
00183     m_pAttribute->setInitialValue( m_pInitialLE->text() );
00184     m_pAttribute->setStereotype( m_pStereoTypeLE->text() );
00185     m_pAttribute->setStatic( m_pStaticCB->isChecked() );
00186 
00187     QString typeName = m_pTypeCB->currentText();
00188     UMLTemplate *tmplParam = pConcept->findTemplate(typeName);
00189     if (tmplParam) {
00190         m_pAttribute->setType(tmplParam);
00191         return true;
00192     }
00193     UMLDoc * pDoc = UMLApp::app()->getDocument();
00194     UMLObject *obj = pDoc->findUMLObject(typeName);
00195     UMLClassifier *classifier = dynamic_cast<UMLClassifier*>(obj);
00196     if (classifier == NULL) {
00197         Uml::Programming_Language pl = UMLApp::app()->getActiveLanguage();
00198         if (pl == Uml::pl_Cpp || pl == Uml::pl_Java) {
00199             // Import_Utils::createUMLObject works better for C++ namespace and java package than Object_Factory::createUMLObject
00200 
00201             Import_Utils::setRelatedClassifier(pConcept);
00202             obj = Import_Utils::createUMLObject(Uml::ot_UMLObject, typeName);
00203             Import_Utils::setRelatedClassifier(NULL);
00204         } else {
00205             // If it's obviously a pointer type (C++) then create a datatype.
00206             // Else we don't know what it is so as a compromise create a class.
00207             Uml::Object_Type ot = (typeName.contains('*') ? Uml::ot_Datatype : Uml::ot_Class);
00208             obj = Object_Factory::createUMLObject(ot, typeName);
00209         }
00210         if (obj == NULL)
00211             return false;
00212         classifier = static_cast<UMLClassifier*>(obj);
00213     }
00214     m_pAttribute->setType( classifier );
00215     return true;
00216 }
00217 
00218 void UMLAttributeDialog::slotApply() {
00219     apply();
00220 }
00221 
00222 void UMLAttributeDialog::slotOk() {
00223     if ( apply() ) {
00224         accept();
00225     }
00226 }
00227 
00228 void UMLAttributeDialog::insertType( const QString& type, int index )
00229 {
00230     m_pTypeCB->insertItem( type, index );
00231     m_pTypeCB->completionObject()->addItem( type );
00232 }
00233 
00234 
00235 #include "umlattributedialog.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:08:00 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003