umbrello API Documentation

umltemplatedialog.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-2006                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "umltemplatedialog.h"
00014 
00015 // qt includes
00016 #include <qlayout.h>
00017 #include <qgroupbox.h>
00018 #include <qcombobox.h>
00019 #include <qlabel.h>
00020 #include <qlineedit.h>
00021 
00022 // kde includes
00023 #include <kcombobox.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 #include <kdebug.h>
00027 
00028 // app includes
00029 #include "../template.h"
00030 #include "../classifier.h"
00031 #include "../umldoc.h"
00032 #include "../uml.h"
00033 #include "../dialog_utils.h"
00034 
00035 UMLTemplateDialog::UMLTemplateDialog(QWidget* pParent, UMLTemplate* pTemplate)
00036         : KDialogBase( Plain, i18n("Template Properties"), Help | Ok | Cancel , Ok, pParent, "_UMLTemplateDLG_", true, true) {
00037     m_pTemplate = pTemplate;
00038     setupDialog();
00039 }
00040 
00041 UMLTemplateDialog::~UMLTemplateDialog() {}
00042 
00043 void UMLTemplateDialog::setupDialog() {
00044     int margin = fontMetrics().height();
00045 
00046     QVBoxLayout* mainLayout = new QVBoxLayout( plainPage() );
00047 
00048     m_pValuesGB = new QGroupBox(i18n("General Properties"), plainPage() );
00049     QGridLayout* valuesLayout = new QGridLayout(m_pValuesGB, 3, 2);
00050     valuesLayout->setMargin(margin);
00051     valuesLayout->setSpacing(10);
00052 
00053     m_pTypeL = new QLabel(i18n("&Type:"), m_pValuesGB);
00054     valuesLayout->addWidget(m_pTypeL, 0, 0);
00055 
00056     m_pTypeCB = new KComboBox(m_pValuesGB);
00057     valuesLayout->addWidget(m_pTypeCB, 0, 1);
00058     m_pTypeL->setBuddy(m_pTypeCB);
00059 
00060     Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 1,
00061                                     m_pNameL, i18n("&Name:"),
00062                                     m_pNameLE, m_pTemplate->getName() );
00063 
00064     Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 2,
00065                                     m_pStereoTypeL, i18n("&Stereotype name:"),
00066                                     m_pStereoTypeLE, m_pTemplate->getStereotype() );
00067 
00068     mainLayout->addWidget(m_pValuesGB);
00069 
00070     // "class" is the nominal type of template parameter
00071     insertType( "class" );
00072     // Add the active data types to combo box
00073     UMLDoc *pDoc = UMLApp::app()->getDocument();
00074     UMLClassifierList namesList( pDoc->getConcepts() );
00075     UMLClassifier* obj = 0;
00076     for (obj = namesList.first(); obj; obj = namesList.next()) {
00077         insertType( obj->getName() );
00078     }
00079 
00080     m_pTypeCB->setEditable(true);
00081     m_pTypeCB->setDuplicatesEnabled(false);//only allow one of each type in box
00082     m_pTypeCB->setCompletionMode( KGlobalSettings::CompletionPopup );
00083 //    m_pTypeCB->setAutoCompletion(true);
00084 
00085     //work out which one to select
00086     int typeBoxCount = 0;
00087     bool foundType = false;
00088     while (typeBoxCount < m_pTypeCB->count() && foundType == false) {
00089         QString typeBoxString = m_pTypeCB->text(typeBoxCount);
00090         if ( typeBoxString == m_pTemplate->getTypeName() ) {
00091             foundType = true;
00092             m_pTypeCB->setCurrentItem(typeBoxCount);
00093         } else {
00094             typeBoxCount++;
00095         }
00096     }
00097 
00098     if (!foundType) {
00099         insertType( m_pTemplate->getTypeName(), 0 );
00100         m_pTypeCB->setCurrentItem(0);
00101     }
00102 
00103     m_pNameLE->setFocus();
00104 }
00105 
00106 void UMLTemplateDialog::insertType( const QString& type, int index )
00107 {
00108     m_pTypeCB->insertItem( type, index );
00109     m_pTypeCB->completionObject()->addItem( type );
00110 }
00111 
00112 bool UMLTemplateDialog::apply() {
00113     QString typeName = m_pTypeCB->currentText();
00114     UMLDoc *pDoc = UMLApp::app()->getDocument();
00115     UMLClassifierList namesList( pDoc->getConcepts() );
00116     UMLClassifier* obj = 0;
00117     for (obj = namesList.first(); obj; obj = namesList.next()) {
00118         if (typeName == obj->getName()) {
00119             m_pTemplate->setType( obj );
00120         }
00121     }
00122     if (obj == NULL) { // not found.
00123         // FIXME: This implementation is not good yet.
00124         m_pTemplate->setTypeName( typeName );
00125     }
00126     QString name = m_pNameLE->text();
00127     if( name.length() == 0 ) {
00128         KMessageBox::error(this, i18n("You have entered an invalid template name."),
00129                            i18n("Template Name Invalid"), false);
00130         m_pNameLE->setText( m_pTemplate->getName() );
00131         return false;
00132     }
00133 
00134     UMLClassifier * pClass = dynamic_cast<UMLClassifier *>( m_pTemplate->parent() );
00135     if (pClass) {
00136         UMLObject *o = pClass->findChildObject(name);
00137         if (o && o != m_pTemplate) {
00138             KMessageBox::error(this, i18n("The template parameter name you have chosen is already being used in this operation."),
00139                                i18n("Template Name Not Unique"), false);
00140             m_pNameLE->setText( m_pTemplate->getName() );
00141             return false;
00142         }
00143     }
00144     m_pTemplate->setName(name);
00145 
00146     m_pTemplate->setStereotype( m_pStereoTypeLE->text() );
00147 
00148     return true;
00149 }
00150 
00151 void UMLTemplateDialog::slotApply() {
00152     apply();
00153 }
00154 
00155 void UMLTemplateDialog::slotOk() {
00156     if ( apply() ) {
00157         accept();
00158     }
00159 }
00160 
00161 #include "umltemplatedialog.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:01 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003