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

Gopala Krishna A krishna.ggk at gmail.com
Thu Jul 10 20:55:24 UTC 2008


SVN commit 830628 by gopala:

1) Ported PackageWidget to use TextItems
2) Fixed small alignment bug in TextItemGroup.


 M  +29 -5     datatypewidget.cpp  
 M  +2 -21     datatypewidget.h  
 M  +101 -92   packagewidget.cpp  
 M  +21 -45    packagewidget.h  
 M  +1 -1      textitemgroup.cpp  
 M  +2 -2      widget_factory.cpp  


--- branches/work/soc-umbrello/umbrello/datatypewidget.cpp #830627:830628
@@ -17,9 +17,30 @@
 #include "textitem.h"
 #include "textitemgroup.h"
 
-// qt/kde includes
+// qt includes
 #include <QtGui/QPainter>
 
+// Inline and class documentation
+
+/**
+ * @class DatatypeWidget
+ *
+ * Defines a graphical version of the datatype.  Most of the
+ * functionality will come from the @ref NewUMLRectWidget class from
+ * which class inherits from.
+ *
+ * @short A graphical version of an datatype.
+ * @author Jonathan Riddell
+ * @author Gopala Krishna (port using TextItems)
+ *
+ * @see NewUMLRectWidget
+ * Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
+ */
+
+// End inline and class documentation
+
+const qreal DatatypeWidget::Margin = 5.;
+
 DatatypeWidget::DatatypeWidget(UMLClassifier *d) :
     NewUMLRectWidget(d),
     m_minimumSize(100, 30)
@@ -53,7 +74,7 @@
     return NewUMLRectWidget::sizeHint(which);
 }
 
-void DatatypeWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
+void DatatypeWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
 {
     painter->setBrush(brush());
     painter->setPen(QPen(lineColor(), lineWidth()));
@@ -80,6 +101,9 @@
             while(m_textItemGroup->size() < totalItemCount) {
                 m_textItemGroup->appendTextItem(new TextItem(""));
             }
+            while(m_textItemGroup->size() > totalItemCount) {
+                m_textItemGroup->deleteTextItemAt(0);
+            }
         }
 
         TextItem *stereo = m_textItemGroup->textItemAt(DatatypeWidget::StereoTypeItemIndex);
@@ -93,7 +117,7 @@
         nameItem->setItalic(umlObject()->getAbstract());
 
         m_minimumSize = m_textItemGroup->calculateMinimumSize();
-        m_minimumSize.rwidth() += DATATYPE_MARGIN * 2;
+        m_minimumSize.rwidth() += DatatypeWidget::Margin * 2;
     }
 
     NewUMLRectWidget::updateGeometry();
@@ -101,9 +125,9 @@
 
 void DatatypeWidget::sizeHasChanged(const QSizeF& oldSize)
 {
-    QPointF offset(DATATYPE_MARGIN, 0);
+    QPointF offset(DatatypeWidget::Margin, 0);
     QSizeF groupSize = size();
-    groupSize.rwidth() -= 2 * DATATYPE_MARGIN;
+    groupSize.rwidth() -= 2 * DatatypeWidget::Margin;
 
     m_textItemGroup->alignVertically(groupSize);
     m_textItemGroup->setPos(offset);
--- branches/work/soc-umbrello/umbrello/datatypewidget.h #830627:830628
@@ -17,30 +17,9 @@
 class UMLClassifier;
 class TextItemGroup;
 
-#define DATATYPE_MARGIN 5
-
-/**
- * Defines a graphical version of the datatype.  Most of the
- * functionality will come from the @ref NewUMLRectWidget class from
- * which class inherits from.
- *
- * @short A graphical version of an datatype.
- * @author Jonathan Riddell
- * @author Gopala Krishna (port using TextItems)
- *
- * @see NewUMLRectWidget
- * Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
- */
 class DatatypeWidget : public NewUMLRectWidget
 {
 public:
-
-    /**
-     * Constructs an DatatypeWidget.
-     *
-     * @param view The parent of this DatatypeWidget.
-     * @param d    The UMLClassifier this will be representing.
-     */
     DatatypeWidget(UMLClassifier *d);
     virtual ~DatatypeWidget();
 
@@ -61,6 +40,8 @@
         NameItemIndex = 1
     };
 
+    static const qreal Margin;
+
     QSizeF m_minimumSize;
     TextItemGroup *m_textItemGroup;
 };
--- branches/work/soc-umbrello/umbrello/packagewidget.cpp #830627:830628
@@ -18,123 +18,132 @@
 
 // app includes
 #include "package.h"
+#include "textitem.h"
+#include "textitemgroup.h"
 #include "uml.h"
-#include "umldoc.h"
-#include "umlview.h"
-#include "umlobject.h"
-#include "umlscene.h"
 
+// Inline and class documentation
 
-PackageWidget::PackageWidget(UMLScene * view, UMLPackage *o)
-  : NewUMLRectWidget(view, o) {
-    init();
+/**
+ * Defines a graphical version of the Package.  Most of the
+ * functionality will come from the @ref UMLPackage class.
+ *
+ * @short A graphical version of a Package.
+ * @author Jonathan Riddell
+ * @author Gopala Krishna (ported to use TextItem)
+ *
+ * @see NewUMLRectWidget
+ * Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
+ */
+
+// End Inline and class documentation
+
+const qreal PackageWidget::Margin = 5.;
+
+PackageWidget::PackageWidget(UMLPackage *o) :
+    NewUMLRectWidget(o),
+    m_minimumSize(100, 30)
+{
+    m_baseType = Uml::wt_Package;
+    m_textItemGroup = new TextItemGroup(this);
 }
 
-void PackageWidget::init() {
-    NewUMLRectWidget::setBaseType(Uml::wt_Package);
-    setSize(100, 30);
-    setZ(m_origZ = 1);  // above box but below NewUMLRectWidget because may embed widgets
-    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() && !UMLApp::app()->getDocument()->loading())
-        updateComponentSize();
+PackageWidget::~PackageWidget()
+{
 }
 
-PackageWidget::~PackageWidget() {}
+void PackageWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement)
+{
+    QDomElement conceptElement = qDoc.createElement("packagewidget");
+    NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
+    qElement.appendChild(conceptElement);
+}
 
-void PackageWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
+QSizeF PackageWidget::sizeHint(Qt::SizeHint which)
 {
-	QPainter &p = *painter;
-	qreal offsetX = 0, offsetY = 0;
-
-    setPenFromSettings(p);
-    if ( NewUMLRectWidget::getUseFillColour() )
-        p.setBrush( NewUMLRectWidget::getFillColour() );
-    else {
-        // [PORT]
-        // p.setBrush( umlScene()->viewport()->palette().color(QPalette::Background) );
+    if(which == Qt::MinimumSize) {
+        return m_minimumSize;
     }
 
-    qreal w = getWidth();
-    qreal h = getHeight();
-    QFont font = NewUMLRectWidget::getFont();
-    font.setBold(true);
-    //FIXME italic is true when a package is first created until you click elsewhere, not sure why
-    font.setItalic(false);
-    const QFontMetrics &fm = getFontMetrics(FT_BOLD);
-    const qreal fontHeight  = fm.lineSpacing();
-    QString name = getName();
+    return NewUMLRectWidget::sizeHint(which);
+}
 
-    p.drawRect(offsetX, offsetY, 50, fontHeight);
+void PackageWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
+{
+    painter->setBrush(brush());
+    painter->setPen(QPen(lineColor(), lineWidth()));
+
+    painter->drawRect(m_topRect);
+    painter->drawRect(m_packageTextRect);
     if (umlObject()->getStereotype() == "subsystem") {
-        const qreal fHalf = fontHeight / 2;
-        const qreal symY = offsetY + fHalf;
-        const qreal symX = offsetX + 38;
-        p.drawLine(symX, symY, symX, symY + fHalf - 2);          // left leg
-        p.drawLine(symX + 8, symY, symX + 8, symY + fHalf - 2);  // right leg
-        p.drawLine(symX, symY, symX + 8, symY);                  // waist
-        p.drawLine(symX + 4, symY, symX + 4, symY - fHalf + 2);  // head
+        const qreal fHalf = m_topRect.height() / 2;
+        const qreal symY = fHalf;
+        const qreal symX = 38;
+        painter->drawLine(symX, symY, symX, symY + fHalf - 2);          // left leg
+        painter->drawLine(symX + 8, symY, symX + 8, symY + fHalf - 2);  // right leg
+        painter->drawLine(symX, symY, symX + 8, symY);                  // waist
+        painter->drawLine(symX + 4, symY, symX + 4, symY - fHalf + 2);  // head
     }
-    p.drawRect(offsetX, offsetY + fontHeight - 1, w, h - fontHeight);
+}
 
-    p.setPen( QPen(Qt::black) );
-    p.setFont(font);
+void PackageWidget::updateGeometry()
+{
+    if(umlObject()) {
+        int totalItemCount = 2; // Stereotype and name
 
-    qreal lines = 1;
-    if (umlObject() != NULL) {
-        QString stereotype = umlObject()->getStereotype();
-        if (!stereotype.isEmpty()) {
-            p.drawText(offsetX, offsetY + fontHeight + PACKAGE_MARGIN,
-                       w, fontHeight, Qt::AlignCenter, umlObject()->getStereotype(true));
-            lines = 2;
+        TextItem dummy("");
+        dummy.setDefaultTextColor(fontColor());
+        dummy.setFont(font());
+        dummy.setAcceptHoverEvents(true);
+        // dummy.setHoverBrush(hoverBrush);
+        dummy.setAlignment(Qt::AlignCenter);
+        dummy.setBackgroundBrush(Qt::NoBrush);
+
+        if(m_textItemGroup->size() != totalItemCount) {
+            while(m_textItemGroup->size() < totalItemCount) {
+                m_textItemGroup->appendTextItem(new TextItem(""));
+            }
+            while(m_textItemGroup->size() > totalItemCount) {
+                m_textItemGroup->deleteTextItemAt(0);
+            }
         }
-    }
 
-    p.drawText(offsetX, offsetY + (fontHeight*lines) + PACKAGE_MARGIN,
-               w, fontHeight, Qt::AlignCenter, name );
+        TextItem *stereo = m_textItemGroup->textItemAt(PackageWidget::StereoTypeItemIndex);
+        stereo->setText(umlObject()->getStereotype(true));
+        dummy.copyAttributesTo(stereo); // apply the attributes
+        stereo->setBold(true);
+        stereo->setVisible(umlObject()->getStereotype().isEmpty() == false);
 
-    if(isSelected()) {
-        drawSelected(&p, offsetX, offsetY);
-    }
-}
+        TextItem *nameItem = m_textItemGroup->textItemAt(PackageWidget::NameItemIndex);
+        nameItem->setText(name());
+        dummy.copyAttributesTo(nameItem); // apply the attributes
 
-QSizeF PackageWidget::calculateSize() {
-    if ( !umlObject() ) {
-        return NewUMLRectWidget::calculateSize();
-    }
+        m_minimumSize = m_textItemGroup->calculateMinimumSize();
+        m_minimumSize.rwidth() += PackageWidget::Margin * 2;
+        if(m_minimumSize.width() < 70) {
+            m_minimumSize.rwidth() = 70;
+        }
 
-    const QFontMetrics &fm = getFontMetrics(FT_BOLD_ITALIC);
-    const qreal fontHeight = fm.lineSpacing();
+        m_minimumSize.rheight() += stereo->height();
 
-    qreal lines = 1;
-
-    qreal width = fm.width( umlObject()->getName() );
-
-    qreal tempWidth = 0;
-    if (!umlObject()->getStereotype().isEmpty()) {
-        tempWidth = fm.width(umlObject()->getStereotype(true));
-        lines = 2;
+        m_topRect.setRect(0, 0, 50, stereo->height());
+        m_packageTextRect.setTopLeft(QPointF(0, m_topRect.bottom()));
     }
-    if (tempWidth > width)
-        width = tempWidth;
-    width += PACKAGE_MARGIN * 2;
-    if (width < 70)
-        width = 70;  // minumin width of 70
+    NewUMLRectWidget::updateGeometry();
+}
 
-    qreal height = (lines*fontHeight) + fontHeight + (PACKAGE_MARGIN * 2);
+void PackageWidget::sizeHasChanged(const QSizeF& oldSize)
+{
+    m_packageTextRect.setBottomRight(rect().bottomRight());
+    QPointF offset(PackageWidget::Margin, m_packageTextRect.top());
+    uDebug() << offset;
+    QSizeF groupSize = m_packageTextRect.size();
+    groupSize.rwidth() -= 2 * PackageWidget::Margin;
 
-    return QSizeF(width, height);
-}
+    m_textItemGroup->alignVertically(groupSize);
+    m_textItemGroup->setPos(offset);
 
-void PackageWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement) {
-    QDomElement conceptElement = qDoc.createElement("packagewidget");
-    NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
-    qElement.appendChild(conceptElement);
+    NewUMLRectWidget::sizeHasChanged(oldSize);
 }
 
+
--- branches/work/soc-umbrello/umbrello/packagewidget.h #830627:830628
@@ -12,63 +12,39 @@
 #ifndef PACKAGEWIDGET_H
 #define PACKAGEWIDGET_H
 
-#include "umlwidget.h"
+#include "newumlrectwidget.h"
 
 class UMLPackage;
+class TextItemGroup;
 
-#define PACKAGE_MARGIN 5
-
-/**
- * Defines a graphical version of the Package.  Most of the functionality
- * will come from the @ref UMLPackage class.
- *
- * @short A graphical version of a Package.
- * @author Jonathan Riddell
- * @see NewUMLRectWidget
- * Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
- */
-class PackageWidget : public NewUMLRectWidget {
+class PackageWidget : public NewUMLRectWidget
+{
 public:
-
-    /**
-     * Constructs a PackageWidget.
-     *
-     * @param view              The parent of this PackageWidget.
-     * @param o         The UMLObject this will be representing.
-     */
-    PackageWidget(UMLScene * view, UMLPackage * o);
-
-    /**
-     * destructor
-     */
+    PackageWidget(UMLPackage * o);
     virtual ~PackageWidget();
 
-    /**
-     * Overrides standard method.
-     */
-    void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
-
-    /**
-     * Saves to the "packagewidget" XMI element.
-     */
     void saveToXMI(QDomDocument& qDoc, 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;
+    static const qreal Margin;
+
+    QRectF m_topRect;
+    QRectF m_packageTextRect;
+    QSizeF m_minimumSize;
+    TextItemGroup *m_textItemGroup;
 };
 
 #endif
--- branches/work/soc-umbrello/umbrello/textitemgroup.cpp #830627:830628
@@ -158,7 +158,7 @@
 
     qreal spacing = (currentHeight - minimumSize.height()) / visibleItems.count();
     qreal x = m_pos.x();
-    qreal y = 0;
+    qreal y = m_pos.y();
 
     foreach(TextItem *item, visibleItems) {
         item->setTextWidth(currentWidth);
--- branches/work/soc-umbrello/umbrello/widget_factory.cpp #830627:830628
@@ -85,7 +85,7 @@
         newWidget = new UseCaseWidget(scene, static_cast<UMLUseCase*>(o));
         break;
     case Uml::ot_Package:
-        newWidget = new PackageWidget(scene, static_cast<UMLPackage*>(o));
+        newWidget = new PackageWidget(static_cast<UMLPackage*>(o));
         break;
     case Uml::ot_Component:
         newWidget = new ComponentWidget(scene, static_cast<UMLComponent*>(o));
@@ -240,7 +240,7 @@
                 widget = new ClassifierWidget(scene, static_cast<UMLClassifier*>(o));
         } else if (tag == "packagewidget") {
             if (validateObjType(Uml::ot_Package, o, id))
-                widget = new PackageWidget(scene, static_cast<UMLPackage*>(o));
+                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));




More information about the umbrello-devel mailing list