[Uml-devel] branches/work/soc-umbrello/umbrello

Gopala Krishna A krishna.ggk at gmail.com
Fri Jul 25 13:54:19 UTC 2008


SVN commit 837696 by gopala:

Ported ComponentWidget using TextItems.


 M  +92 -108   componentwidget.cpp  
 M  +23 -37    componentwidget.h  
 M  +0 -1      newumlwidget.cpp  
 M  +3 -4      widget_factory.cpp  


--- branches/work/soc-umbrello/umbrello/componentwidget.cpp #837695:837696
@@ -12,138 +12,122 @@
 // own header
 #include "componentwidget.h"
 
-// qt/kde includes
-#include <qpainter.h>
-
 // app includes
-#include <kdebug.h>
 #include "component.h"
-#include "umlview.h"
-#include "umlscene.h"
+#include "textitem.h"
+#include "textitemgroup.h"
 
+// qt/kde includes
+#include <QtGui/QPainter>
 
-ComponentWidget::ComponentWidget(UMLScene * scene, UMLComponent *c)
-  : NewUMLRectWidget(scene, c) {
-    init();
+/**
+ * Constructs a ComponentWidget.
+ *
+ * @param c The UMLComponent this will be representing.
+ */
+ComponentWidget::ComponentWidget(UMLComponent *c)
+	: NewUMLRectWidget(0, c)
+{
+	m_baseType = Uml::wt_Component;
+    setMargin(10); // override default of 5 for other widgets.
+	createTextItemGroup();
 }
 
-void ComponentWidget::init() {
-    NewUMLRectWidget::setBaseType(Uml::wt_Component);
-    setSize(100, 30);
-    m_pMenu = 0;
-    //set defaults from umlScene()
-    if (umlScene()) {
-        //check to see if correct
-        const Settings::OptionState& ops = umlScene()->getOptionState();
-        setShowStereotype(ops.classState.showStereoType);
-    }
-    //maybe loading and this may not be set.
-    if (umlObject()) {
-        updateComponentSize();
-        update();
-    }
+/// Destructor
+ComponentWidget::~ComponentWidget()
+{
 }
 
-ComponentWidget::~ComponentWidget() {}
-
-void ComponentWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
+/**
+ * Reimplemented from NewUMLRectWidget::paint to paint component
+ * widget.
+ */
+void ComponentWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
 {
-	QPainter &p = *painter;
-	qreal offsetX = 0, offsetY = 0;
+	UMLComponent *umlcomp = static_cast<UMLComponent*>(umlObject());
 
-    UMLComponent *umlcomp = static_cast<UMLComponent*>(umlObject());
-    if (umlcomp == NULL)
-        return;
-    setPenFromSettings(p);
-    if ( umlcomp->getExecutable() ) {
-        QPen thickerPen = p.pen();
-        thickerPen.setWidth(2);
-        p.setPen(thickerPen);
-    }
-    if ( NewUMLRectWidget::getUseFillColour() ) {
-        p.setBrush( NewUMLRectWidget::getFillColour() );
-    } else {
-        // [PORT] Replace with styleoption based code.
-        //p.setBrush( umlScene()->viewport()->palette().color(QPalette::Background) );
-    }
+	QPen pen(lineColor(), lineWidth());
+	if (umlcomp && umlcomp->getExecutable()) {
+		pen.setWidth(lineWidth() + 2);
+	}
+	painter->setPen(pen);
+	painter->setBrush(brush());
 
-    const qreal w = getWidth();
-    const qreal h = getHeight();
-    QFont font = NewUMLRectWidget::getFont();
-    font.setBold(true);
-    const QFontMetrics &fm = getFontMetrics(FT_BOLD);
-    const qreal fontHeight = fm.lineSpacing();
-    QString name = getName();
-    const QString stereotype = umlObject()->getStereotype();
+	painter->drawRects(m_rects, 3);
+}
 
-    p.drawRect(offsetX + 2*COMPONENT_MARGIN, offsetY, w - 2*COMPONENT_MARGIN, h);
-    p.drawRect(offsetX, offsetY + h/2 - fontHeight/2 - fontHeight, COMPONENT_MARGIN*4, fontHeight);
-    p.drawRect(offsetX, offsetY + h/2 + fontHeight/2, COMPONENT_MARGIN*4, fontHeight);
+void ComponentWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement)
+{
+    QDomElement conceptElement = qDoc.createElement("componentwidget");
+    NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
+    qElement.appendChild(conceptElement);
+}
 
-    p.setPen( QPen(Qt::black) );
-    p.setFont(font);
+void ComponentWidget::updateGeometry()
+{
+	TextItemGroup *grp = textItemGroupAt(GroupIndex);
+	QSizeF minSize = grp->minimumSize();
 
-    int lines = 1;
+	minSize.rwidth() += 4 * margin();
+	if(minSize.width() < 70) {
+		// atleast minwidth = 70
+		minSize.setWidth(70);
+	}
 
-    if (!stereotype.isEmpty()) {
-        p.drawText(offsetX + (COMPONENT_MARGIN*4), offsetY + (h/2) - fontHeight,
-                   w - (COMPONENT_MARGIN*4), fontHeight, Qt::AlignCenter,
-                   umlObject()->getStereotype(true));
-        lines = 2;
-    }
+	qreal minHeight = 3 * QFontMetricsF(grp->font()).lineSpacing();
+	if(minSize.height() < minHeight) {
+		minSize.setHeight(minHeight);
+	}
 
-    if ( NewUMLRectWidget::getIsInstance() ) {
-        font.setUnderline(true);
-        p.setFont(font);
-        name = NewUMLRectWidget::getInstanceName() + " : " + name;
-    }
+	// Note: Adds 2 * margin() to both width and height!
+	setMinimumSize(minSize);
 
-    if (lines == 1) {
-        p.drawText(offsetX + (COMPONENT_MARGIN*4), offsetY + (h/2) - (fontHeight/2),
-                   w - (COMPONENT_MARGIN*4), fontHeight, Qt::AlignCenter, name );
-    } else {
-        p.drawText(offsetX + (COMPONENT_MARGIN*4), offsetY + (h/2),
-                   w - (COMPONENT_MARGIN*4), fontHeight, Qt::AlignCenter, name );
-    }
-
-    if(isSelected()) {
-        drawSelected(&p, offsetX, offsetY);
-    }
+	NewUMLRectWidget::updateGeometry();
 }
 
-QSizeF ComponentWidget::calculateSize()
+void ComponentWidget::updateTextItemGroups()
 {
-    if ( !umlObject()) {
-        return QSizeF(70, 70);
-    }
-    const QFontMetrics &fm = getFontMetrics(FT_BOLD_ITALIC);
-    const qreal fontHeight = fm.lineSpacing();
+	TextItemGroup *grp = textItemGroupAt(GroupIndex);
+	grp->setTextItemCount(TextItemCount);
 
-    QString name = umlObject()->getName();
-    if ( NewUMLRectWidget::getIsInstance() ) {
-        name = NewUMLRectWidget::getInstanceName() + " : " + name;
-    }
+	if(umlObject()) {
+		TextItem *stereo = grp->textItemAt(StereoItemIndex);
+		stereo->setText(umlObject()->getStereotype(true));
+		stereo->setBold(true);
+		stereo->setVisible(umlObject()->getStereotype(false).isEmpty() == false);
 
-    qreal width = fm.width(name);
+		TextItem *nameItem = grp->textItemAt(NameItemIndex);
+		nameItem->setBold(true);
+		QString nameText = name();
+		bool underline = false;
+		if(this->isInstance()) {
+			nameText.prepend(':');
+			nameText.prepend(instanceName());
+			underline = true;
+		}
+		nameItem->setText(nameText);
+		nameItem->setUnderline(underline);
+	}
 
-    qreal stereoWidth = 0;
-    if (!umlObject()->getStereotype().isEmpty()) {
-        stereoWidth = fm.width(umlObject()->getStereotype(true));
-    }
-    if (stereoWidth > width)
-        width = stereoWidth;
-    width += COMPONENT_MARGIN * 6;
-    width = 70>width ? 70 : width; //minumin width of 70
-
-    qreal height = (2*fontHeight) + (COMPONENT_MARGIN * 3);
-
-    return QSizeF(width, height);
+	NewUMLRectWidget::updateTextItemGroups();
 }
 
-void ComponentWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement)
+QVariant ComponentWidget::attributeChange(WidgetAttributeChange change, const QVariant& oldValue)
 {
-    QDomElement conceptElement = qDoc.createElement("componentwidget");
-    NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
-    qElement.appendChild(conceptElement);
-}
+	if (change == SizeHasChanged) {
+		TextItemGroup *grp = textItemGroupAt(GroupIndex);
+		const qreal m = margin();
+		const qreal fontHeight = QFontMetricsF(grp->font()).lineSpacing();
+		qreal w = size().width();
+		qreal h = size().height();
 
+		m_rects[0] = QRectF(2 * m, 0, w - 2 * m, h);
+		m_rects[1] = QRectF(0, h/2 - fontHeight/2 - fontHeight, m * 4, fontHeight);
+		m_rects[2] = QRectF(0, h/2 + fontHeight/2, m * 4, fontHeight);
+
+		QRectF grpRect(m*4, m, w - 4 * m, h - 2 * m);
+		grp->setGroupGeometry(grpRect);
+    }
+
+	return NewUMLRectWidget::attributeChange(change, oldValue);
+}
--- branches/work/soc-umbrello/umbrello/componentwidget.h #837695:837696
@@ -12,63 +12,49 @@
 #ifndef COMPONENTWIDGET_H
 #define COMPONENTWIDGET_H
 
-#include "umlwidget.h"
+#include "newumlrectwidget.h"
 
 class UMLComponent;
 
-#define COMPONENT_MARGIN 10
-
 /**
  * Defines a graphical version of the Component.  Most of the functionality
  * will come from the @ref UMLComponent class.
  *
  * @short A graphical version of a Component.
  * @author Jonathan Riddell
+ * @author Gopala Krishna
  * @see NewUMLRectWidget
  * Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
  */
-class ComponentWidget : public NewUMLRectWidget {
+class ComponentWidget : public NewUMLRectWidget
+{
 public:
+	ComponentWidget(UMLComponent *c);
+	virtual ~ComponentWidget();
 
-    /**
-     * Constructs a ComponentWidget.
-     *
-     * @param view      The parent of this ComponentWidget.
-     * @param c The UMLComponent this will be representing.
-     */
-    ComponentWidget(UMLScene * scene, UMLComponent *c);
+    virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
 
-    /**
-     * destructor
-     */
-    virtual ~ComponentWidget();
+	// Uses NewUMLRectWidget::loadFromXMI to load info.
+    virtual void saveToXMI(QDomDocument& qDoc, QDomElement& qElement);
 
-    /**
-     * Overrides standard method
-     */
-    void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
-
-    /**
-     * Saves to the "componentwidget" XMI element.
-     */
-    void saveToXMI(QDomDocument& qDoc, QDomElement& qElement);
-
 protected:
-    /**
-     * Overrides method from NewUMLRectWidget.
-     */
-    QSizeF calculateSize();
+	virtual void updateGeometry();
+	virtual void updateTextItemGroups();
+	virtual QVariant attributeChange(WidgetAttributeChange change, const QVariant& oldValue);
 
 private:
-    /**
-     * Initializes key variables of the class.
-     */
-    void init();
+	// Hard coded indices used for TextItemGroup and TextItems.
+	enum {
+		GroupIndex
+	};
+	enum {
+		StereoItemIndex,
+		NameItemIndex,
+		TextItemCount
+	};
 
-    /**
-     * The right mouse button menu
-     */
-    ListPopupMenu* m_pMenu;
+	/// The rectangles to be drawn (calculated in attributeChange)
+	QRectF m_rects[3];
 };
 
 #endif
--- branches/work/soc-umbrello/umbrello/newumlwidget.cpp #837695:837696
@@ -726,7 +726,6 @@
 void NewUMLWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
 {
     ListPopupMenu menu(0, this, false, false);
-    uDebug() << (void*)(&menu);
     QAction *triggered = menu.exec(event->screenPos());
     if(triggered) {
         triggered->setParent(&menu);
--- branches/work/soc-umbrello/umbrello/widget_factory.cpp #837695:837696
@@ -88,10 +88,9 @@
         newWidget = new PackageWidget(static_cast<UMLPackage*>(o));
         break;
     case Uml::ot_Component:
-        newWidget = new ComponentWidget(scene, static_cast<UMLComponent*>(o));
+        newWidget = new ComponentWidget(static_cast<UMLComponent*>(o));
         if (diagramType == Uml::dt_Deployment) {
-            // [PORT]
-            //newWidget->setIsInstance(true);
+            newWidget->setIsInstance(true);
         }
         break;
     case Uml::ot_Node:
@@ -243,7 +242,7 @@
                 widget = new PackageWidget(static_cast<UMLPackage*>(o));
         } else if (tag == "componentwidget") {
             if (validateObjType(Uml::ot_Component, o, id))
-                widget = new ComponentWidget(scene, static_cast<UMLComponent*>(o));
+                widget = new ComponentWidget(static_cast<UMLComponent*>(o));
         } else if (tag == "nodewidget") {
             if (validateObjType(Uml::ot_Node, o, id))
                 widget = new NodeWidget(scene, static_cast<UMLNode*>(o));




More information about the umbrello-devel mailing list