umbrello API Documentation

statewidget.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 "statewidget.h"
00014 
00015 // qt includes
00016 #include <qevent.h>
00017 
00018 // kde includes
00019 #include <klocale.h>
00020 #include <kdebug.h>
00021 #include <kinputdialog.h>
00022 
00023 // app includes
00024 #include "uml.h"
00025 #include "umldoc.h"
00026 #include "docwindow.h"
00027 #include "umlwidget.h"
00028 #include "umlview.h"
00029 #include "dialogs/statedialog.h"
00030 #include "listpopupmenu.h"
00031 
00032 StateWidget::StateWidget(UMLView * view, StateType stateType, Uml::IDType id)
00033         : UMLWidget(view, id) {
00034     UMLWidget::setBaseType(Uml::wt_State);
00035     m_StateType = stateType;
00036     m_Text = "State";
00037     updateComponentSize();
00038 }
00039 
00040 StateWidget::~StateWidget() {}
00041 
00042 void StateWidget::draw(QPainter & p, int offsetX, int offsetY) {
00043     UMLWidget::setPen(p);
00044     const int w = width();
00045     const int h = height();
00046     switch (m_StateType)
00047     {
00048     case Normal :
00049         if(UMLWidget::getUseFillColour())
00050             p.setBrush(UMLWidget::getFillColour());
00051         {
00052             const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00053             const int fontHeight  = fm.lineSpacing();
00054             int textStartY = (h / 2) - (fontHeight / 2);
00055             const int count = m_Activities.count();
00056             if( count == 0 ) {
00057                 p.drawRoundRect(offsetX, offsetY, w, h, (h*40)/w, (w*40)/h);
00058                 p.setPen(Qt::black);
00059                 QFont font = UMLWidget::getFont();
00060                 font.setBold( false );
00061                 p.setFont( font );
00062                 p.drawText(offsetX + STATE_MARGIN, offsetY + textStartY,
00063                            w - STATE_MARGIN * 2, fontHeight,
00064                            Qt::AlignCenter, getName());
00065                 UMLWidget::setPen(p);
00066             } else {
00067                 p.drawRoundRect(offsetX, offsetY, w, h, (h*40)/w, (w*40)/h);
00068                 textStartY = offsetY + STATE_MARGIN;
00069                 p.setPen(Qt::black);
00070                 QFont font = UMLWidget::getFont();
00071                 font.setBold( true );
00072                 p.setFont( font );
00073                 p.drawText(offsetX + STATE_MARGIN, textStartY, w - STATE_MARGIN * 2,
00074                            fontHeight, Qt::AlignCenter, getName());
00075                 font.setBold( false );
00076                 p.setFont( font );
00077                 UMLWidget::setPen(p);
00078                 int linePosY = textStartY + fontHeight;
00079 
00080                 QStringList::Iterator end(m_Activities.end());
00081                 for( QStringList::Iterator it(m_Activities.begin()); it != end; ++it ) {
00082                     textStartY += fontHeight;
00083                     p.drawLine( offsetX, linePosY, offsetX + w - 1, linePosY );
00084                     p.setPen(Qt::black);
00085                     p.drawText(offsetX + STATE_MARGIN, textStartY, w - STATE_MARGIN * 2 - 1,
00086                                fontHeight, Qt::AlignCenter, *it);
00087                     UMLWidget::setPen(p);
00088                     linePosY += fontHeight;
00089                 }//end for
00090             }//end else
00091         }
00092         break;
00093     case Initial :
00094         p.setBrush( WidgetBase::getLineColor() );
00095         p.drawEllipse( offsetX, offsetY, w, h );
00096         break;
00097     case End :
00098         p.setBrush( WidgetBase::getLineColor() );
00099         p.drawEllipse( offsetX, offsetY, w, h );
00100         p.setBrush( Qt::white );
00101         p.drawEllipse( offsetX + 1, offsetY + 1, w - 2, h - 2 );
00102         p.setBrush( WidgetBase::getLineColor() );
00103         p.drawEllipse( offsetX + 3, offsetY + 3, w - 6, h - 6 );
00104         break;
00105     default:
00106         kWarning() << "Unknown state type:" << m_StateType << endl;
00107         break;
00108     }
00109     if(m_bSelected)
00110         drawSelected(&p, offsetX, offsetY);
00111 }
00112 
00113 QSize StateWidget::calculateSize() {
00114     int width = 10, height = 10;
00115     if ( m_StateType == Normal ) {
00116         const QFontMetrics &fm = getFontMetrics(FT_BOLD);
00117         const int fontHeight  = fm.lineSpacing();
00118         int textWidth = fm.width(getName());
00119         const int count = m_Activities.count();
00120         height = fontHeight;
00121         if( count > 0 ) {
00122             height = fontHeight * ( count + 1);
00123 
00124             QStringList::Iterator end(m_Activities.end());
00125             for( QStringList::Iterator it(m_Activities.begin()); it != end; ++it ) {
00126                 int w = fm.width( *it );
00127                 if( w > textWidth )
00128                     textWidth = w;
00129             }//end for
00130         }//end if
00131         width = textWidth > STATE_WIDTH?textWidth:STATE_WIDTH;
00132         height = height > STATE_HEIGHT?height:STATE_HEIGHT;
00133         width += STATE_MARGIN * 2;
00134         height += STATE_MARGIN * 2;
00135     }
00136 
00137     return QSize(width, height);
00138 }
00139 
00140 void StateWidget::setName(const QString &strName) {
00141     m_Text = strName;
00142     updateComponentSize();
00143     adjustAssocs( getX(), getY() );
00144 }
00145 
00146 QString StateWidget::getName() const {
00147     return m_Text;
00148 }
00149 
00150 StateWidget::StateType StateWidget::getStateType() const {
00151     return m_StateType;
00152 }
00153 
00154 void StateWidget::setStateType( StateType stateType ) {
00155     m_StateType = stateType;
00156 }
00157 
00158 void StateWidget::slotMenuSelection(int sel) {
00159     bool done = false;
00160     bool ok = false;
00161     QString name = getName();
00162 
00163     switch( sel ) {
00164     case ListPopupMenu::mt_Rename:
00165         name = KInputDialog::getText( i18n("Enter State Name"), i18n("Enter the name of the new state:"), getName(), &ok );
00166         if( ok && name.length() > 0 )
00167             setName( name );
00168         done = true;
00169         break;
00170 
00171     case ListPopupMenu::mt_Properties:
00172         showProperties();
00173         done = true;
00174         break;
00175     case ListPopupMenu::mt_New_Activity:
00176         name = KInputDialog::getText( i18n("Enter Activity"), i18n("Enter the name of the new activity:"), i18n("new activity"), &ok );
00177         if( ok && name.length() > 0 )
00178             addActivity( name );
00179         done = true;
00180         break;
00181     }
00182 
00183     if( !done )
00184         UMLWidget::slotMenuSelection( sel );
00185 }
00186 
00187 bool StateWidget::addActivity( const QString &activity ) {
00188     m_Activities.append( activity );
00189     updateComponentSize();
00190     return true;
00191 }
00192 
00193 bool StateWidget::removeActivity( const QString &activity ) {
00194     int index = - 1;
00195     if( ( index = m_Activities.findIndex( activity ) ) == -1 )
00196         return false;
00197     m_Activities.remove( m_Activities.at( index ) );
00198     updateComponentSize();
00199     return true;
00200 }
00201 
00202 void StateWidget::setActivities( QStringList & list ) {
00203     m_Activities = list;
00204     updateComponentSize();
00205 }
00206 
00207 QStringList & StateWidget::getActivityList() {
00208     return m_Activities;
00209 }
00210 
00211 bool StateWidget::renameActivity( const QString &activity, const QString &newName ) {
00212     int index = - 1;
00213     if( ( index = m_Activities.findIndex( activity ) ) == -1 )
00214         return false;
00215     m_Activities[ index ] = newName;
00216     return true;
00217 }
00218 
00219 void StateWidget::showProperties() {
00220     DocWindow *docwindow = UMLApp::app()->getDocWindow();
00221     docwindow->updateDocumentation(false);
00222 
00223     StateDialog dialog(m_pView, this);
00224     if (dialog.exec() && dialog.getChangesMade()) {
00225         docwindow->showDocumentation(this, true);
00226         UMLApp::app()->getDocument()->setModified(true);
00227     }
00228 }
00229 
00230 bool StateWidget::isState(WorkToolBar::ToolBar_Buttons tbb, StateType& resultType)
00231 {
00232     bool status = true;
00233     switch (tbb) {
00234     case WorkToolBar::tbb_Initial_State:
00235         resultType = Initial;
00236         break;
00237     case WorkToolBar::tbb_State:
00238         resultType = Normal;
00239         break;
00240     case WorkToolBar::tbb_End_State:
00241         resultType = End;
00242         break;
00243     default:
00244         status = false;
00245         break;
00246     }
00247     return status;
00248 }
00249 
00250 void StateWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement ) {
00251     QDomElement stateElement = qDoc.createElement( "statewidget" );
00252     UMLWidget::saveToXMI( qDoc, stateElement );
00253     stateElement.setAttribute( "statename", m_Text );
00254     stateElement.setAttribute( "documentation", m_Doc );
00255     stateElement.setAttribute( "statetype", m_StateType );
00256     //save states activities
00257     QDomElement activitiesElement = qDoc.createElement( "Activities" );
00258 
00259     QStringList::Iterator end(m_Activities.end());
00260     for( QStringList::Iterator it(m_Activities.begin()); it != end; ++it ) {
00261         QDomElement tempElement = qDoc.createElement( "Activity" );
00262         tempElement.setAttribute( "name", *it );
00263         activitiesElement.appendChild( tempElement );
00264     }//end for
00265     stateElement.appendChild( activitiesElement );
00266     qElement.appendChild( stateElement );
00267 }
00268 
00269 bool StateWidget::loadFromXMI( QDomElement & qElement ) {
00270     if( !UMLWidget::loadFromXMI( qElement ) )
00271         return false;
00272     m_Text = qElement.attribute( "statename", "" );
00273     m_Doc = qElement.attribute( "documentation", "" );
00274     QString type = qElement.attribute( "statetype", "1" );
00275     m_StateType = (StateType)type.toInt();
00276     //load states activities
00277     QDomNode node = qElement.firstChild();
00278     QDomElement tempElement = node.toElement();
00279     if( !tempElement.isNull() && tempElement.tagName() == "Activities" ) {
00280         QDomNode node = tempElement.firstChild();
00281         QDomElement activityElement = node.toElement();
00282         while( !activityElement.isNull() ) {
00283             if( activityElement.tagName() == "Activity" ) {
00284                 QString name = activityElement.attribute( "name", "" );
00285                 if( !name.isEmpty() )
00286                     m_Activities.append( name );
00287             }//end if
00288             node = node.nextSibling();
00289             activityElement = node.toElement();
00290         }//end while
00291     }//end if
00292     return true;
00293 }
00294 
00295 
00296 #include "statewidget.moc"
00297 
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