umbrello API Documentation

artifactwidget.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 "artifactwidget.h"
00014 
00015 // qt/kde includes
00016 #include <qpainter.h>
00017 #include <qpointarray.h>
00018 #include <kdebug.h>
00019 
00020 // app includes
00021 #include "artifact.h"
00022 #include "umlview.h"
00023 
00024 
00025 ArtifactWidget::ArtifactWidget(UMLView *view, UMLArtifact *a) : UMLWidget(view, a) {
00026     init();
00027     setSize(100, 30);
00028     updateComponentSize();
00029 }
00030 
00031 
00032 void ArtifactWidget::init() {
00033     UMLWidget::setBaseType( Uml::wt_Artifact );
00034     m_pMenu = 0;
00035 }
00036 
00037 ArtifactWidget::~ArtifactWidget() {}
00038 
00039 void ArtifactWidget::drawAsNormal(QPainter& p, int offsetX, int offsetY) {
00040     int w = width();
00041     int h = height();
00042     QFont font = UMLWidget::getFont();
00043     font.setBold(true);
00044     const QFontMetrics &fm = getFontMetrics(FT_BOLD);
00045     const int fontHeight  = fm.lineSpacing();
00046     QString name = getName();
00047     QString stereotype = m_pObject->getStereotype();
00048 
00049     p.drawRect(offsetX, offsetY, w, h);
00050 
00051     p.setPen( QPen(Qt::black) );
00052     p.setFont(font);
00053 
00054     if (!stereotype.isEmpty()) {
00055         p.drawText(offsetX + ARTIFACT_MARGIN, offsetY + (h/2) - fontHeight,
00056                    w, fontHeight, Qt::AlignCenter, m_pObject->getStereotype(true));
00057     }
00058 
00059     int lines;
00060     if (!stereotype.isEmpty()) {
00061         lines = 2;
00062     } else {
00063         lines = 1;
00064     }
00065 
00066     if (lines == 1) {
00067         p.drawText(offsetX, offsetY + (h/2) - (fontHeight/2),
00068                    w, fontHeight, Qt::AlignCenter, name);
00069     } else {
00070         p.drawText(offsetX, offsetY + (h/2),
00071                    w, fontHeight, Qt::AlignCenter, name);
00072     }
00073 
00074     if(m_bSelected) {
00075         drawSelected(&p, offsetX, offsetY);
00076     }
00077 }
00078 
00079 void ArtifactWidget::drawAsFile(QPainter& p, int offsetX, int offsetY) {
00080     const int w = width();
00081     const int h = height();
00082     QFont font = UMLWidget::getFont();
00083     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00084     const int fontHeight  = fm.lineSpacing();
00085     const QString name = getName();
00086 
00087     int startX = offsetX + (w/2) - 25;
00088     int iconHeight = h - fontHeight;
00089     QPointArray pointArray(5);
00090     pointArray.setPoint(0, startX, offsetY);
00091     pointArray.setPoint(1, startX + 40, offsetY);
00092     pointArray.setPoint(2, startX + 50, offsetY + 10);
00093     pointArray.setPoint(3, startX + 50, offsetY + iconHeight);
00094     pointArray.setPoint(4, startX, offsetY + iconHeight);
00095     p.drawPolygon(pointArray);
00096 
00097     p.drawLine(startX + 40, offsetY, startX + 40, offsetY + 10);
00098     p.drawLine(startX + 40, offsetY + 10, startX + 50, offsetY + 10);
00099     p.drawLine(startX + 40, offsetY, startX + 50, offsetY + 10);
00100 
00101     p.setPen( QPen(Qt::black) );
00102     p.setFont(font);
00103 
00104     p.drawText(offsetX, offsetY + h - fontHeight,
00105                w, fontHeight, Qt::AlignCenter, name);
00106 
00107     if(m_bSelected) {
00108         drawSelected(&p, offsetX, offsetY);
00109     }
00110 }
00111 
00112 void ArtifactWidget::drawAsLibrary(QPainter& p, int offsetX, int offsetY) {
00113     //FIXME this should have gears on it
00114     const int w = width();
00115     const int h = height();
00116     const QFont font = UMLWidget::getFont();
00117     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00118     const int fontHeight  = fm.lineSpacing();
00119     const QString name = getName();
00120 
00121     const int startX = offsetX + (w/2) - 25;
00122     const int iconHeight = h - fontHeight;
00123     QPointArray pointArray(5);
00124     pointArray.setPoint(0, startX, offsetY);
00125     pointArray.setPoint(1, startX + 40, offsetY);
00126     pointArray.setPoint(2, startX + 50, offsetY + 10);
00127     pointArray.setPoint(3, startX + 50, offsetY + iconHeight);
00128     pointArray.setPoint(4, startX, offsetY + iconHeight);
00129     p.drawPolygon(pointArray);
00130 
00131     p.drawLine(startX + 40, offsetY, startX + 40, offsetY + 10);
00132     p.drawLine(startX + 40, offsetY + 10, startX + 50, offsetY + 10);
00133     p.drawLine(startX + 40, offsetY, startX + 50, offsetY + 10);
00134 
00135     p.setPen( QPen(Qt::black) );
00136     p.setFont(font);
00137 
00138     p.drawText(offsetX, offsetY + h - fontHeight,
00139                w, fontHeight, Qt::AlignCenter, name);
00140 
00141     if(m_bSelected) {
00142         drawSelected(&p, offsetX, offsetY);
00143     }
00144 }
00145 
00146 void ArtifactWidget::drawAsTable(QPainter& p, int offsetX, int offsetY) {
00147     const int w = width();
00148     const int h = height();
00149     const QFont font = UMLWidget::getFont();
00150     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00151     const int fontHeight  = fm.lineSpacing();
00152     const QString name = getName();
00153 
00154     const int startX = offsetX + (w/2) - 25;
00155     const int iconHeight = h - fontHeight;
00156 
00157     p.drawRect(startX, offsetY, 50, h - fontHeight + 1);
00158     p.drawLine(startX + 20, offsetY, startX + 20, offsetY + iconHeight);
00159     p.drawLine(startX + 30, offsetY, startX + 30, offsetY + iconHeight);
00160     p.drawLine(startX + 40, offsetY, startX + 40, offsetY + iconHeight);
00161     p.drawLine(startX, offsetY + (iconHeight/2), startX + 49, offsetY + (iconHeight/2));
00162     p.drawLine(startX, offsetY + (iconHeight/2) + (iconHeight/4),
00163                startX + 49, offsetY + (iconHeight/2) + (iconHeight/4));
00164 
00165     QPen thickerPen = p.pen();
00166     thickerPen.setWidth(2);
00167     p.setPen(thickerPen);
00168     p.drawLine(startX + 10, offsetY, startX + 10, offsetY + iconHeight);
00169     p.drawLine(startX, offsetY + (iconHeight/4), startX + 50, offsetY + (iconHeight/4));
00170 
00171     p.setPen( QPen(Qt::black) );
00172     p.setFont(font);
00173 
00174     p.drawText(offsetX, offsetY + h - fontHeight,
00175                w, fontHeight, Qt::AlignCenter, name);
00176 
00177     if(m_bSelected) {
00178         drawSelected(&p, offsetX, offsetY);
00179     }
00180 }
00181 
00182 void ArtifactWidget::draw(QPainter& p, int offsetX, int offsetY) {
00183     UMLWidget::setPen(p);
00184     if ( UMLWidget::getUseFillColour() ) {
00185         p.setBrush( UMLWidget::getFillColour() );
00186     } else {
00187         p.setBrush( m_pView->viewport()->backgroundColor() );
00188     }
00189 
00190     UMLArtifact *umlart = static_cast<UMLArtifact*>(m_pObject);
00191     UMLArtifact::Draw_Type drawType = umlart->getDrawAsType();
00192     switch (drawType) {
00193     case UMLArtifact::defaultDraw:
00194         return drawAsNormal(p, offsetX, offsetY);
00195         break;
00196     case UMLArtifact::file:
00197         return drawAsFile(p, offsetX, offsetY);
00198         break;
00199     case UMLArtifact::library:
00200         return drawAsLibrary(p, offsetX, offsetY);
00201         break;
00202     case UMLArtifact::table:
00203         return drawAsTable(p, offsetX, offsetY);
00204         break;
00205     default:
00206         kWarning() << "Artifact drawn as unknown type" << endl;
00207         break;
00208     }
00209 }
00210 
00211 QSize ArtifactWidget::calculateIconSize() {
00212     const QFontMetrics &fm = getFontMetrics(FT_BOLD_ITALIC);
00213     const int fontHeight  = fm.lineSpacing();
00214 
00215     int width = fm.width( m_pObject->getName() );
00216 
00217     width = width<50 ? 50 : width;
00218 
00219     int height = 50 + fontHeight;
00220 
00221     return QSize(width, height);
00222 }
00223 
00224 QSize ArtifactWidget::calculateNormalSize() {
00225     const QFontMetrics &fm = getFontMetrics(FT_BOLD_ITALIC);
00226     const int fontHeight  = fm.lineSpacing();
00227 
00228     int width = fm.width( m_pObject->getName() );
00229 
00230     int tempWidth = 0;
00231     if(!m_pObject->getStereotype().isEmpty()) {
00232         tempWidth = fm.width( m_pObject->getStereotype(true) );
00233     }
00234     width = tempWidth>width ? tempWidth : width;
00235     width += ARTIFACT_MARGIN * 2;
00236 
00237     int height = (2*fontHeight) + (ARTIFACT_MARGIN * 2);
00238 
00239     return QSize(width, height);
00240 }
00241 
00242 QSize ArtifactWidget::calculateSize() {
00243     if ( !m_pObject) {
00244         return UMLWidget::calculateSize();
00245     }
00246     UMLArtifact *umlart = static_cast<UMLArtifact*>(m_pObject);
00247     if (umlart->getDrawAsType() == UMLArtifact::defaultDraw) {
00248         return calculateNormalSize();
00249     } else {
00250         return calculateIconSize();
00251     }
00252 }
00253 
00254 void ArtifactWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement) {
00255     QDomElement conceptElement = qDoc.createElement("artifactwidget");
00256     UMLWidget::saveToXMI(qDoc, conceptElement);
00257     qElement.appendChild(conceptElement);
00258 }
00259 
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:54 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003