umbrello API Documentation

packagewidget.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 "packagewidget.h"
00014 
00015 // qt/kde includes
00016 #include <qpainter.h>
00017 #include <kdebug.h>
00018 
00019 // app includes
00020 #include "package.h"
00021 #include "uml.h"
00022 #include "umldoc.h"
00023 #include "umlview.h"
00024 #include "umlobject.h"
00025 
00026 
00027 PackageWidget::PackageWidget(UMLView * view, UMLPackage *o)
00028   : UMLWidget(view, o) {
00029     init();
00030 }
00031 
00032 void PackageWidget::init() {
00033     UMLWidget::setBaseType(Uml::wt_Package);
00034     setSize(100, 30);
00035     setZ(m_origZ = 1);  // above box but below UMLWidget because may embed widgets
00036     m_pMenu = 0;
00037     //set defaults from m_pView
00038     if (m_pView) {
00039         //check to see if correct
00040         const Settings::OptionState& ops = m_pView->getOptionState();
00041         m_bShowStereotype = ops.classState.showStereoType;
00042     }
00043     //maybe loading and this may not be set.
00044     if (m_pObject && !UMLApp::app()->getDocument()->loading())
00045         updateComponentSize();
00046 }
00047 
00048 PackageWidget::~PackageWidget() {}
00049 
00050 void PackageWidget::draw(QPainter & p, int offsetX, int offsetY) {
00051     UMLWidget::setPen(p);
00052     if ( UMLWidget::getUseFillColour() )
00053         p.setBrush( UMLWidget::getFillColour() );
00054     else
00055         p.setBrush(m_pView -> viewport() -> backgroundColor());
00056 
00057     int w = width();
00058     int h = height();
00059     QFont font = UMLWidget::getFont();
00060     font.setBold(true);
00061     //FIXME italic is true when a package is first created until you click elsewhere, not sure why
00062     font.setItalic(false);
00063     const QFontMetrics &fm = getFontMetrics(FT_BOLD);
00064     const int fontHeight  = fm.lineSpacing();
00065     QString name = getName();
00066 
00067     p.drawRect(offsetX, offsetY, 50, fontHeight);
00068     if (m_pObject->getStereotype() == "subsystem") {
00069         const int fHalf = fontHeight / 2;
00070         const int symY = offsetY + fHalf;
00071         const int symX = offsetX + 38;
00072         p.drawLine(symX, symY, symX, symY + fHalf - 2);          // left leg
00073         p.drawLine(symX + 8, symY, symX + 8, symY + fHalf - 2);  // right leg
00074         p.drawLine(symX, symY, symX + 8, symY);                  // waist
00075         p.drawLine(symX + 4, symY, symX + 4, symY - fHalf + 2);  // head
00076     }
00077     p.drawRect(offsetX, offsetY + fontHeight - 1, w, h - fontHeight);
00078 
00079     p.setPen( QPen(Qt::black) );
00080     p.setFont(font);
00081 
00082     int lines = 1;
00083     if (m_pObject != NULL) {
00084         QString stereotype = m_pObject->getStereotype();
00085         if (!stereotype.isEmpty()) {
00086             p.drawText(offsetX, offsetY + fontHeight + PACKAGE_MARGIN,
00087                        w, fontHeight, Qt::AlignCenter, m_pObject->getStereotype(true));
00088             lines = 2;
00089         }
00090     }
00091 
00092     p.drawText(offsetX, offsetY + (fontHeight*lines) + PACKAGE_MARGIN,
00093                w, fontHeight, Qt::AlignCenter, name );
00094 
00095     if(m_bSelected) {
00096         drawSelected(&p, offsetX, offsetY);
00097     }
00098 }
00099 
00100 QSize PackageWidget::calculateSize() {
00101     if ( !m_pObject ) {
00102         return UMLWidget::calculateSize();
00103     }
00104 
00105     const QFontMetrics &fm = getFontMetrics(FT_BOLD_ITALIC);
00106     const int fontHeight = fm.lineSpacing();
00107 
00108     int lines = 1;
00109 
00110     int width = fm.width( m_pObject->getName() );
00111 
00112     int tempWidth = 0;
00113     if (!m_pObject->getStereotype().isEmpty()) {
00114         tempWidth = fm.width(m_pObject->getStereotype(true));
00115         lines = 2;
00116     }
00117     if (tempWidth > width)
00118         width = tempWidth;
00119     width += PACKAGE_MARGIN * 2;
00120     if (width < 70)
00121         width = 70;  // minumin width of 70
00122 
00123     int height = (lines*fontHeight) + fontHeight + (PACKAGE_MARGIN * 2);
00124 
00125     return QSize(width, height);
00126 }
00127 
00128 void PackageWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement) {
00129     QDomElement conceptElement = qDoc.createElement("packagewidget");
00130     UMLWidget::saveToXMI(qDoc, conceptElement);
00131     qElement.appendChild(conceptElement);
00132 }
00133 
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:58 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003