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

Gopala Krishna A krishna.ggk at gmail.com
Tue Jul 8 14:57:52 UTC 2008


SVN commit 829465 by gopala:

Ported DatatypeWidget to use TextItemGroup and TextItem.

 M  +71 -78    datatypewidget.cpp  
 M  +16 -31    datatypewidget.h  
 M  +2 -2      newenumwidget.cpp  
 M  +0 -2      newumlrectwidget.cpp  
 M  +4 -0      textitemgroup.h  


--- branches/work/soc-umbrello/umbrello/datatypewidget.cpp #829464:829465
@@ -12,108 +12,101 @@
 // own header
 #include "datatypewidget.h"
 
-// qt/kde includes
-#include <qpainter.h>
-#include <kdebug.h>
-
 // app includes
 #include "classifier.h"
-#include "operation.h"
-#include "classifierlistitem.h"
-#include "umlview.h"
-#include "umldoc.h"
-#include "umlscene.h"
+#include "textitem.h"
+#include "textitemgroup.h"
 
+// qt/kde includes
+#include <QtGui/QPainter>
 
-#define CIRCLE_SIZE 30
+DatatypeWidget::DatatypeWidget(UMLScene* scene, UMLClassifier *d) :
+    NewUMLRectWidget(scene, d),
+    m_minimumSize(100, 30)
+{
+    m_textItemGroup = new TextItemGroup(this);
+    m_baseType = Uml::wt_Datatype;
+}
 
-DatatypeWidget::DatatypeWidget(UMLScene* scene, UMLClassifier *d) : NewUMLRectWidget(scene, d) {
-    init();
+DatatypeWidget::~DatatypeWidget()
+{
 }
 
-DatatypeWidget::~DatatypeWidget() {}
-
-void DatatypeWidget::init() {
-    NewUMLRectWidget::setBaseType(Uml::wt_Datatype);
-    setSize(100, 30);
-    m_pMenu = 0;
+bool DatatypeWidget::loadFromXMI(QDomElement &qElement)
+{
+    return NewUMLRectWidget::loadFromXMI(qElement);
 }
 
-void DatatypeWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
+void DatatypeWidget::saveToXMI(QDomDocument &qDoc, QDomElement &qElement)
 {
-	QPainter &p = *painter;
-	qreal offsetX = 0, offsetY = 0;
+    QDomElement conceptElement = qDoc.createElement("datatypewidget");
+    NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
+    qElement.appendChild(conceptElement);
+}
 
-    setPenFromSettings(p);
-    if (NewUMLRectWidget::getUseFillColour())  {
-        p.setBrush(NewUMLRectWidget::getFillColour());
-    } else {
-        // [PORT] Replace with styleOption based code
-        //p.setBrush( m_pView->viewport()->palette().color(QPalette::Background) );
+QSizeF DatatypeWidget::sizeHint(Qt::SizeHint which)
+{
+    if(which == Qt::MinimumSize) {
+        return m_minimumSize;
     }
 
-    qreal w = getWidth();
-    qreal h = getHeight();
+    return NewUMLRectWidget::sizeHint(which);
+}
 
-    QFontMetrics &fm = getFontMetrics(FT_NORMAL);
-    qreal fontHeight  = fm.lineSpacing();
-    QString name = getName();
+void DatatypeWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
+{
+    painter->setBrush(brush());
+    painter->setPen(QPen(lineColor(), lineWidth()));
 
-    p.drawRect(offsetX, offsetY, w, h);
-    p.setPen(QPen(Qt::black));
-
-    QFont font = NewUMLRectWidget::getFont();
-    font.setBold(true);
-    p.setFont(font);
-    p.drawText(offsetX + DATATYPE_MARGIN, offsetY,
-               w - DATATYPE_MARGIN* 2,fontHeight,
-               Qt::AlignCenter, umlObject()->getStereotype(true));
-
-    font.setItalic( umlObject()->getAbstract() );
-    p.setFont(font);
-    p.drawText(offsetX + DATATYPE_MARGIN, offsetY + fontHeight,
-               w - DATATYPE_MARGIN * 2, fontHeight, Qt::AlignCenter, name);
-
-    if (isSelected()) {
-        drawSelected(&p, offsetX, offsetY);
-    }
+    painter->drawRect(rect());
 }
 
-QSizeF DatatypeWidget::calculateSize() {
-    if (!umlObject())  {
-        return NewUMLRectWidget::calculateSize();
-    }
-    qreal width, height;
-    const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
-    const qreal fontHeight = fm.lineSpacing();
+void DatatypeWidget::updateGeometry()
+{
+    if(umlObject()) {
+        int totalItemCount = 2; // SteroType and name
 
-    int lines = 1;//always have one line - for name
-    lines++; //for the stereotype
+        // Create a dummy item, to store the properties so that it can
+        // easily be used to copy the properties to other text items.
+        TextItem dummy("");
+        dummy.setDefaultTextColor(fontColor());
+        dummy.setFont(font());
+        dummy.setAcceptHoverEvents(true);
+        // dummy.setHoverBrush(hoverBrush);
+        dummy.setAlignment(Qt::AlignCenter);
+        dummy.setBackgroundBrush(Qt::NoBrush);
 
-    height = width = 0;
-    height += lines * fontHeight;
+        if(m_textItemGroup->size() != totalItemCount) {
+            while(m_textItemGroup->size() < totalItemCount) {
+                m_textItemGroup->appendTextItem(new TextItem(""));
+            }
+        }
 
-    //now set the width of the concept
-    //set width to name to start with
-    //set width to name to start with
-    width = getFontMetrics(FT_BOLD_ITALIC).boundingRect(umlObject()->getFullyQualifiedName()).width();
-    qreal w = getFontMetrics(FT_BOLD).boundingRect(umlObject()->getStereotype(true)).width();
+        TextItem *stereo = m_textItemGroup->textItemAt(DatatypeWidget::StereoTypeItemIndex);
+        stereo->setText(umlObject()->getStereotype(true));
+        dummy.copyAttributesTo(stereo); // apply the attributes
+        stereo->setBold(true);
 
-    width = w > width?w:width;
+        TextItem *nameItem = m_textItemGroup->textItemAt(DatatypeWidget::NameItemIndex);
+        nameItem->setText(name());
+        dummy.copyAttributesTo(nameItem); // apply the attributes
+        nameItem->setItalic(umlObject()->getAbstract());
 
-    //allow for width margin
-    width += DATATYPE_MARGIN * 2;
+        m_minimumSize = m_textItemGroup->calculateMinimumSize();
+        m_minimumSize.rwidth() += DATATYPE_MARGIN * 2;
+    }
 
-    return QSize(width, height);
+    NewUMLRectWidget::updateGeometry();
 }
 
-void DatatypeWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement ) {
-    QDomElement conceptElement = qDoc.createElement("datatypewidget");
-    NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
-    qElement.appendChild(conceptElement);
-}
+void DatatypeWidget::sizeHasChanged(const QSizeF& oldSize)
+{
+    QPointF offset(DATATYPE_MARGIN, 0);
+    QSizeF groupSize = size();
+    groupSize.rwidth() -= 2 * DATATYPE_MARGIN;
 
-bool DatatypeWidget::loadFromXMI( QDomElement & qElement ) {
-    return NewUMLRectWidget::loadFromXMI(qElement);
-}
+    m_textItemGroup->alignVertically(groupSize);
+    m_textItemGroup->setPos(offset);
 
+    NewUMLRectWidget::sizeHasChanged(oldSize);
+}
--- branches/work/soc-umbrello/umbrello/datatypewidget.h #829464:829465
@@ -12,9 +12,10 @@
 #ifndef DATATYPEWIDGET_H
 #define DATATYPEWIDGET_H
 
-#include "umlwidget.h"
+#include "newumlrectwidget.h"
 
 class UMLClassifier;
+class TextItemGroup;
 
 #define DATATYPE_MARGIN 5
 
@@ -34,47 +35,31 @@
     /**
      * Constructs an DatatypeWidget.
      *
-     * @param view              The parent of this DatatypeWidget.
-     * @param d         The UMLClassifier this will be representing.
+     * @param view The parent of this DatatypeWidget.
+     * @param d    The UMLClassifier this will be representing.
      */
     DatatypeWidget(UMLScene* scene, UMLClassifier *d);
-
-    /**
-     * Standard deconstructor.
-     */
     virtual ~DatatypeWidget();
 
-    /**
-     * Overrides standard method.
-     */
-    void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
-
-    /**
-     * Saves to the "datatypewidget" XMI element.
-     */
+    bool loadFromXMI(QDomElement& qElement);
     void saveToXMI(QDomDocument& qDoc, QDomElement& qElement);
 
-    /**
-     * Loads from a "datatypewidget" XMI element.
-     */
-    bool loadFromXMI(QDomElement& qElement);
+    QSizeF sizeHint(Qt::SizeHint which);
 
+    void paint(QPainter *p, const QStyleOptionGraphicsItem *opt, QWidget *w);
+
 protected:
-    /**
-     * Overrides method from NewUMLRectWidget.
-     */
-    QSizeF calculateSize();
+    void updateGeometry();
+    void sizeHasChanged(const QSizeF& oldSize);
 
 private:
-    /**
-     * Initializes key variables of the class.
-     */
-    void init();
+    enum {
+        StereoTypeItemIndex = 0,
+        NameItemIndex = 1
+    };
 
-    /**
-     * The right mouse button menu.
-     */
-    ListPopupMenu* m_pMenu;
+    QSizeF m_minimumSize;
+    TextItemGroup *m_textItemGroup;
 };
 
 #endif
--- branches/work/soc-umbrello/umbrello/newenumwidget.cpp #829464:829465
@@ -188,10 +188,10 @@
     NewUMLRectWidget::updateGeometry();
 }
 
-void NewEnumWidget::sizeHasChanged(const QSizeF& sz)
+void NewEnumWidget::sizeHasChanged(const QSizeF& oldSize)
 {
     m_textItemGroup->alignVertically(size().width() - 2*ENUM_MARGIN, size().height());
     m_textItemGroup->setPos(QPointF(ENUM_MARGIN, 0));
 
-    NewUMLRectWidget::sizeHasChanged(sz);
+    NewUMLRectWidget::sizeHasChanged(oldSize);
 }
--- branches/work/soc-umbrello/umbrello/newumlrectwidget.cpp #829464:829465
@@ -155,8 +155,6 @@
     m_size = minSize.expandedTo(size);
     m_size = maxSize.boundedTo(m_size);
 
-    uDebug() << "Old : " << oldSize << "New : " << m_size;
-
     QRectF boundRect = rect();
     // Adjust bounding rect with half the pen width(lineWidth).
     qreal hpw = 0.5 * lineWidth();
--- branches/work/soc-umbrello/umbrello/textitemgroup.h #829464:829465
@@ -22,6 +22,7 @@
 
 #include <QtCore/QList>
 #include <QtCore/QPointF>
+#include <QtCore/QSizeF>
 
 class QGraphicsItem;
 class TextItem;
@@ -62,6 +63,9 @@
     void setLineBreakWidth(qreal w);
 
     void alignVertically(qreal currentWidth, qreal currentHeight);
+    void alignVertically(const QSizeF& size) {
+        alignVertically(size.width(), size.height());
+    }
 
     QPointF pos() const {
         return m_pos;




More information about the umbrello-devel mailing list