componentwidget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "componentwidget.h"
00014
00015
00016 #include <qpainter.h>
00017
00018
00019 #include <kdebug.h>
00020 #include "component.h"
00021 #include "umlview.h"
00022
00023
00024 ComponentWidget::ComponentWidget(UMLView * view, UMLComponent *c)
00025 : UMLWidget(view, c) {
00026 init();
00027 }
00028
00029 void ComponentWidget::init() {
00030 UMLWidget::setBaseType(Uml::wt_Component);
00031 setSize(100, 30);
00032 m_pMenu = 0;
00033
00034 if (m_pView) {
00035
00036 const Settings::OptionState& ops = m_pView->getOptionState();
00037 m_bShowStereotype = ops.classState.showStereoType;
00038 }
00039
00040 if (m_pObject) {
00041 updateComponentSize();
00042 update();
00043 }
00044 }
00045
00046 ComponentWidget::~ComponentWidget() {}
00047
00048 void ComponentWidget::draw(QPainter & p, int offsetX, int offsetY) {
00049 UMLComponent *umlcomp = static_cast<UMLComponent*>(m_pObject);
00050 if (umlcomp == NULL)
00051 return;
00052 UMLWidget::setPen(p);
00053 if ( umlcomp->getExecutable() ) {
00054 QPen thickerPen = p.pen();
00055 thickerPen.setWidth(2);
00056 p.setPen(thickerPen);
00057 }
00058 if ( UMLWidget::getUseFillColour() ) {
00059 p.setBrush( UMLWidget::getFillColour() );
00060 } else {
00061 p.setBrush( m_pView->viewport()->backgroundColor() );
00062 }
00063
00064 const int w = width();
00065 const int h = height();
00066 QFont font = UMLWidget::getFont();
00067 font.setBold(true);
00068 const QFontMetrics &fm = getFontMetrics(FT_BOLD);
00069 const int fontHeight = fm.lineSpacing();
00070 QString name = getName();
00071 const QString stereotype = m_pObject->getStereotype();
00072
00073 p.drawRect(offsetX + 2*COMPONENT_MARGIN, offsetY, w - 2*COMPONENT_MARGIN, h);
00074 p.drawRect(offsetX, offsetY + h/2 - fontHeight/2 - fontHeight, COMPONENT_MARGIN*4, fontHeight);
00075 p.drawRect(offsetX, offsetY + h/2 + fontHeight/2, COMPONENT_MARGIN*4, fontHeight);
00076
00077 p.setPen( QPen(Qt::black) );
00078 p.setFont(font);
00079
00080 int lines = 1;
00081
00082 if (!stereotype.isEmpty()) {
00083 p.drawText(offsetX + (COMPONENT_MARGIN*4), offsetY + (h/2) - fontHeight,
00084 w - (COMPONENT_MARGIN*4), fontHeight, Qt::AlignCenter,
00085 m_pObject->getStereotype(true));
00086 lines = 2;
00087 }
00088
00089 if ( UMLWidget::getIsInstance() ) {
00090 font.setUnderline(true);
00091 p.setFont(font);
00092 name = UMLWidget::getInstanceName() + " : " + name;
00093 }
00094
00095 if (lines == 1) {
00096 p.drawText(offsetX + (COMPONENT_MARGIN*4), offsetY + (h/2) - (fontHeight/2),
00097 w - (COMPONENT_MARGIN*4), fontHeight, Qt::AlignCenter, name );
00098 } else {
00099 p.drawText(offsetX + (COMPONENT_MARGIN*4), offsetY + (h/2),
00100 w - (COMPONENT_MARGIN*4), fontHeight, Qt::AlignCenter, name );
00101 }
00102
00103 if(m_bSelected) {
00104 drawSelected(&p, offsetX, offsetY);
00105 }
00106 }
00107
00108 QSize ComponentWidget::calculateSize() {
00109 if ( !m_pObject) {
00110 return QSize(70, 70);
00111 }
00112 const QFontMetrics &fm = getFontMetrics(FT_BOLD_ITALIC);
00113 const int fontHeight = fm.lineSpacing();
00114
00115 QString name = m_pObject->getName();
00116 if ( UMLWidget::getIsInstance() ) {
00117 name = UMLWidget::getInstanceName() + " : " + name;
00118 }
00119
00120 int width = fm.width(name);
00121
00122 int stereoWidth = 0;
00123 if (!m_pObject->getStereotype().isEmpty()) {
00124 stereoWidth = fm.width(m_pObject->getStereotype(true));
00125 }
00126 if (stereoWidth > width)
00127 width = stereoWidth;
00128 width += COMPONENT_MARGIN * 6;
00129 width = 70>width ? 70 : width;
00130
00131 int height = (2*fontHeight) + (COMPONENT_MARGIN * 3);
00132
00133 return QSize(width, height);
00134 }
00135
00136 void ComponentWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement) {
00137 QDomElement conceptElement = qDoc.createElement("componentwidget");
00138 UMLWidget::saveToXMI(qDoc, conceptElement);
00139 qElement.appendChild(conceptElement);
00140 }
00141
This file is part of the documentation for umbrello Version 3.1.0.