umbrello API Documentation

enumwidget.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-2007                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "enumwidget.h"
00014 
00015 // qt/kde includes
00016 #include <qpainter.h>
00017 #include <kdebug.h>
00018 
00019 // app includes
00020 #include "enum.h"
00021 #include "enumliteral.h"
00022 #include "classifier.h"
00023 #include "umlclassifierlistitemlist.h"
00024 #include "classifierlistitem.h"
00025 #include "umlview.h"
00026 #include "umldoc.h"
00027 #include "uml.h"
00028 #include "listpopupmenu.h"
00029 #include "object_factory.h"
00030 
00031 
00032 EnumWidget::EnumWidget(UMLView* view, UMLObject* o) : UMLWidget(view, o) {
00033     init();
00034 }
00035 
00036 void EnumWidget::init() {
00037     UMLWidget::setBaseType(Uml::wt_Enum);
00038     setSize(100, 30);
00039     m_pMenu = 0;
00040     //set defaults from m_pView
00041     if (m_pView) {
00042         //check to see if correct
00043         const Settings::OptionState& ops = m_pView->getOptionState();
00044         m_bShowPackage = ops.classState.showPackage;
00045     } else {
00046         // For completeness only. Not supposed to happen.
00047         m_bShowPackage = false;
00048     }
00049     if (! UMLApp::app()->getDocument()->loading())
00050         updateComponentSize();
00051 }
00052 
00053 EnumWidget::~EnumWidget() {}
00054 
00055 void EnumWidget::draw(QPainter& p, int offsetX, int offsetY) {
00056     UMLWidget::setPen(p);
00057     if(UMLWidget::getUseFillColour())
00058         p.setBrush(UMLWidget::getFillColour());
00059     else
00060         p.setBrush(m_pView -> viewport() -> backgroundColor());
00061 
00062     const int w = width();
00063     const int h = height();
00064 
00065     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00066     const int fontHeight  = fm.lineSpacing();
00067     QString name;
00068     if ( m_bShowPackage ) {
00069         name = m_pObject->getFullyQualifiedName();
00070     } else {
00071         name = this -> getName();
00072     }
00073 
00074     p.drawRect(offsetX, offsetY, w, h);
00075     p.setPen(QPen(Qt::black));
00076 
00077     QFont font = UMLWidget::getFont();
00078     font.setBold(true);
00079     p.setFont(font);
00080     p.drawText(offsetX + ENUM_MARGIN, offsetY,
00081                w - ENUM_MARGIN * 2,fontHeight,
00082                Qt::AlignCenter, m_pObject->getStereotype(true));
00083 
00084     font.setItalic( m_pObject -> getAbstract() );
00085     p.setFont(font);
00086     p.drawText(offsetX + ENUM_MARGIN, offsetY + fontHeight,
00087                w - ENUM_MARGIN * 2, fontHeight, Qt::AlignCenter, name);
00088     font.setBold(false);
00089     font.setItalic(false);
00090     p.setFont(font);
00091 
00092     int y = fontHeight * 2;
00093 
00094     UMLWidget::setPen(p);
00095 
00096     p.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y);
00097 
00098     QFontMetrics fontMetrics(font);
00099     UMLClassifier *classifier = (UMLClassifier*)m_pObject;
00100     UMLClassifierListItem* enumLiteral = 0;
00101     UMLClassifierListItemList list = classifier->getFilteredList(Uml::ot_EnumLiteral);
00102     for (enumLiteral = list.first(); enumLiteral; enumLiteral = list.next()) {
00103         QString text = enumLiteral->getName();
00104         p.setPen( QPen(Qt::black) );
00105         p.drawText(offsetX + ENUM_MARGIN, offsetY + y,
00106                    fontMetrics.width(text), fontHeight, Qt::AlignVCenter, text);
00107         y+=fontHeight;
00108     }
00109 
00110     if (m_bSelected) {
00111         drawSelected(&p, offsetX, offsetY);
00112     }
00113 }
00114 
00115 QSize EnumWidget::calculateSize() {
00116     if (!m_pObject) {
00117         return UMLWidget::calculateSize();
00118     }
00119 
00120     int width, height;
00121     QFont font = UMLWidget::getFont();
00122     font.setItalic(false);
00123     font.setUnderline(false);
00124     font.setBold(false);
00125     const QFontMetrics fm(font);
00126 
00127     const int fontHeight = fm.lineSpacing();
00128 
00129     int lines = 1;//always have one line - for name
00130     lines++; //for the stereotype
00131 
00132     const int numberOfEnumLiterals = ((UMLEnum*)m_pObject)->enumLiterals();
00133 
00134     height = width = 0;
00135     //set the height of the enum
00136 
00137     lines += numberOfEnumLiterals;
00138     if (numberOfEnumLiterals == 0) {
00139         height += fontHeight / 2; //no enum literals, so just add a bit of space
00140     }
00141 
00142     height += lines * fontHeight;
00143 
00144     //now set the width of the concept
00145     //set width to name to start with
00146     if (m_bShowPackage)  {
00147         width = getFontMetrics(FT_BOLD_ITALIC).boundingRect(m_pObject->getFullyQualifiedName()).width();
00148     } else {
00149         width = getFontMetrics(FT_BOLD_ITALIC).boundingRect(getName()).width();
00150     }
00151     int w = getFontMetrics(FT_BOLD).boundingRect(m_pObject->getStereotype(true)).width();
00152 
00153 
00154     width = w > width?w:width;
00155 
00156     UMLClassifier *classifier = (UMLClassifier*)m_pObject;
00157     UMLClassifierListItemList list = classifier->getFilteredList(Uml::ot_EnumLiteral);
00158     UMLClassifierListItem* listItem = 0;
00159     for (listItem = list.first(); listItem; listItem = list.next()) {
00160         int w = fm.width( listItem->getName() );
00161         width = w > width?w:width;
00162     }
00163 
00164     //allow for width margin
00165     width += ENUM_MARGIN * 2;
00166 
00167     return QSize(width, height);
00168 }
00169 
00170 void EnumWidget::slotMenuSelection(int sel) {
00171     switch(sel) {
00172     case ListPopupMenu::mt_EnumLiteral:
00173         if (Object_Factory::createChildObject(static_cast<UMLClassifier*>(m_pObject),
00174                                               Uml::ot_EnumLiteral) )  {
00175             UMLApp::app()->getDocument()->setModified();
00176         }
00177         break;
00178     }
00179     UMLWidget::slotMenuSelection(sel);
00180 }
00181 
00182 void EnumWidget::setShowPackage(bool _status) {
00183     m_bShowPackage = _status;
00184     updateComponentSize();
00185     update();
00186 }
00187 
00188 bool EnumWidget::getShowPackage() const {
00189     return m_bShowPackage;
00190 }
00191 
00192 void EnumWidget::saveToXMI( QDomDocument& qDoc, QDomElement& qElement ) {
00193     QDomElement conceptElement = qDoc.createElement("enumwidget");
00194     UMLWidget::saveToXMI(qDoc, conceptElement);
00195 
00196     conceptElement.setAttribute("showpackage", m_bShowPackage);
00197     qElement.appendChild(conceptElement);
00198 }
00199 
00200 bool EnumWidget::loadFromXMI( QDomElement & qElement ) {
00201     if ( !UMLWidget::loadFromXMI(qElement) ) {
00202         return false;
00203     }
00204     QString showpackage = qElement.attribute("showpackage", "0");
00205 
00206     m_bShowPackage = (bool)showpackage.toInt();
00207 
00208     return true;
00209 }
00210 
00211 void EnumWidget::toggleShowPackage() {
00212     m_bShowPackage = !m_bShowPackage;
00213     updateComponentSize();
00214     update();
00215 
00216     return;
00217 }
00218 
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:07:57 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003