entitywidget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "entitywidget.h"
00014
00015
00016 #include <qpainter.h>
00017 #include <kdebug.h>
00018
00019
00020 #include "entity.h"
00021 #include "entityattribute.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 EntityWidget::EntityWidget(UMLView* view, UMLObject* o): UMLWidget(view, o) {
00033 init();
00034 }
00035
00036 void EntityWidget::init() {
00037 UMLWidget::setBaseType(Uml::wt_Entity);
00038 setSize(100, 30);
00039 m_pMenu = 0;
00040
00041 if (m_pView) {
00042
00043 const Settings::OptionState& ops = m_pView->getOptionState();
00044 }
00045 if (! UMLApp::app()->getDocument()->loading())
00046 updateComponentSize();
00047 }
00048
00049 EntityWidget::~EntityWidget() {}
00050
00051 void EntityWidget::draw(QPainter& p, int offsetX, int offsetY) {
00052 UMLWidget::setPen(p);
00053 if(UMLWidget::getUseFillColour())
00054 p.setBrush(UMLWidget::getFillColour());
00055 else
00056 p.setBrush(m_pView -> viewport() -> backgroundColor());
00057
00058 const int w = width();
00059 const int h = height();
00060
00061 const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00062 int fontHeight = fm.lineSpacing();
00063 const QString name = this->getName();
00064
00065 p.drawRect(offsetX, offsetY, w, h);
00066 p.setPen(QPen(Qt::black));
00067
00068 QFont font = UMLWidget::getFont();
00069 font.setBold(true);
00070 p.setFont(font);
00071 int y = 0;
00072 if ( !m_pObject->getStereotype().isEmpty() ) {
00073 p.drawText(offsetX + ENTITY_MARGIN, offsetY,
00074 w - ENTITY_MARGIN * 2,fontHeight,
00075 Qt::AlignCenter, m_pObject->getStereotype(true));
00076 font.setItalic( m_pObject -> getAbstract() );
00077 p.setFont(font);
00078 p.drawText(offsetX + ENTITY_MARGIN, offsetY + fontHeight,
00079 w - ENTITY_MARGIN * 2, fontHeight, Qt::AlignCenter, name);
00080 font.setBold(false);
00081 font.setItalic(false);
00082 p.setFont(font);
00083 y = fontHeight * 2;
00084 } else {
00085 font.setItalic( m_pObject -> getAbstract() );
00086 p.setFont(font);
00087 p.drawText(offsetX + ENTITY_MARGIN, offsetY,
00088 w - ENTITY_MARGIN * 2, fontHeight, Qt::AlignCenter, name);
00089 font.setBold(false);
00090 font.setItalic(false);
00091 p.setFont(font);
00092
00093 y = fontHeight;
00094 }
00095
00096 UMLWidget::setPen(p);
00097
00098 p.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y);
00099
00100 QFontMetrics fontMetrics(font);
00101 UMLClassifier *classifier = (UMLClassifier*)m_pObject;
00102 UMLClassifierListItem* entityattribute = 0;
00103 UMLClassifierListItemList list = classifier->getFilteredList(Uml::ot_EntityAttribute);
00104 for (entityattribute = list.first(); entityattribute; entityattribute = list.next()) {
00105 QString text = entityattribute->getName();
00106 p.setPen( QPen(Qt::black) );
00107 UMLEntityAttribute* casted = dynamic_cast<UMLEntityAttribute*>( entityattribute );
00108 if( casted && casted->getIndexType() == Uml::Primary )
00109 {
00110 font.setUnderline( true );
00111 p.setFont( font );
00112 font.setUnderline( false );
00113 }
00114 p.drawText(offsetX + ENTITY_MARGIN, offsetY + y,
00115 fontMetrics.width(text), fontHeight, Qt::AlignVCenter, text);
00116 p.setFont( font );
00117 y+=fontHeight;
00118 }
00119
00120 if (m_bSelected) {
00121 drawSelected(&p, offsetX, offsetY);
00122 }
00123 }
00124
00125 QSize EntityWidget::calculateSize() {
00126 if (!m_pObject) {
00127 return UMLWidget::calculateSize();
00128 }
00129
00130 int width, height;
00131 QFont font = UMLWidget::getFont();
00132 font.setItalic(false);
00133 font.setUnderline(false);
00134 font.setBold(false);
00135 const QFontMetrics fm(font);
00136
00137 const int fontHeight = fm.lineSpacing();
00138
00139 int lines = 1;
00140 if ( !m_pObject->getStereotype().isEmpty() ) {
00141 lines++;
00142 }
00143
00144 const int numberOfEntityAttributes = ((UMLEntity*)m_pObject)->entityAttributes();
00145
00146 height = width = 0;
00147
00148
00149 lines += numberOfEntityAttributes;
00150 if (numberOfEntityAttributes == 0) {
00151 height += fontHeight / 2;
00152 }
00153
00154 height += lines * fontHeight;
00155
00156
00157
00158
00159
00160 width = getFontMetrics(FT_BOLD_ITALIC).boundingRect(' ' + getName() + ' ').width();
00161
00162 const int w = getFontMetrics(FT_BOLD).boundingRect(m_pObject->getStereotype(true)).width();
00163
00164 width = w > width?w:width;
00165
00166 UMLClassifier* classifier = (UMLClassifier*)m_pObject;
00167 UMLClassifierListItemList list = classifier->getFilteredList(Uml::ot_EntityAttribute);
00168 UMLClassifierListItem* listItem = 0;
00169 for (listItem = list.first(); listItem; listItem = list.next()) {
00170 int w = fm.width( listItem->getName() );
00171 width = w > width?w:width;
00172 }
00173
00174
00175 width += ENTITY_MARGIN * 2;
00176
00177 return QSize(width, height);
00178 }
00179
00180 void EntityWidget::slotMenuSelection(int sel) {
00181 switch(sel) {
00182 case ListPopupMenu::mt_EntityAttribute:
00183 if (Object_Factory::createChildObject(static_cast<UMLClassifier*>(m_pObject),
00184 Uml::ot_EntityAttribute) ) {
00185 UMLApp::app()->getDocument()->setModified();
00186 }
00187 break;
00188 }
00189 UMLWidget::slotMenuSelection(sel);
00190 }
00191
00192 void EntityWidget::saveToXMI( QDomDocument& qDoc, QDomElement& qElement ) {
00193 QDomElement conceptElement = qDoc.createElement("entitywidget");
00194 UMLWidget::saveToXMI(qDoc, conceptElement);
00195 qElement.appendChild(conceptElement);
00196 }
00197
00198 bool EntityWidget::loadFromXMI( QDomElement & qElement ) {
00199 if ( !UMLWidget::loadFromXMI(qElement) ) {
00200 return false;
00201 }
00202 return true;
00203 }
00204
This file is part of the documentation for umbrello Version 3.1.0.