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

Gopala Krishna A krishna.ggk at gmail.com
Sun Jul 13 16:51:54 UTC 2008


SVN commit 831926 by gopala:

1) Popup menu can be used to add EnumLiteral to NewEnumWidget.
2) Removed some testing stuff currently from newenumwidget.cpp and test.cpp


 M  +74 -68    newenumwidget.cpp  
 M  +10 -38    newenumwidget.h  
 M  +21 -74    test.cpp  


--- branches/work/soc-umbrello/umbrello/newenumwidget.cpp #831925:831926
@@ -12,49 +12,35 @@
 // own header
 #include "newenumwidget.h"
 
-// qt/kde includes
-#include <QtGui/QPainter>
-#include <QtGui/QStyleOptionGraphicsItem>
-#include <kdebug.h>
-
 // app includes
 #include "classifier.h"
 #include "classifierlistitem.h"
 #include "enum.h"
 #include "enumliteral.h"
+#include "listpopupmenu.h"
+#include "object_factory.h"
 #include "optionstate.h"
+#include "textitem.h"
 #include "textitemgroup.h"
-#include "textitem.h"
+#include "uml.h"
 #include "umlclassifierlistitemlist.h"
+#include "umldoc.h"
 #include "umlscene.h"
 #include "widget_utils.h"
 
-QBrush randHoverBrush()
-{
-    QLinearGradient grad(QPointF(0,0), QPointF(0, 1));
-    int r, g, b, a;
-    r = qrand() % 255;
-    g = qrand() % 255;
-    b = qrand() % 255;
-    a = 10 + qrand() % 245; // set minimum to atleast 10
-    grad.setColorAt(0, QColor(r, g, b, a));
+// qt/kde includes
+#include <QtGui/QPainter>
+#include <QtGui/QStyleOptionGraphicsItem>
 
-    r = qrand() % 255;
-    g = qrand() % 255;
-    b = qrand() % 255;
-    a = 10 + qrand() % 245; // set minimum to atleast 10
-    grad.setColorAt(1, QColor(r, g, b, a));
+const qreal NewEnumWidget::Margin = 5.;
 
-    return QBrush(grad);
-}
-
 NewEnumWidget::NewEnumWidget(UMLObject* o) :
     NewUMLRectWidget(o),
     m_showPackage(false)
 {
     m_textItemGroup = new TextItemGroup(this);
     m_baseType = Uml::wt_Enum;
-    init();
+    // init();
 }
 
 NewEnumWidget::~NewEnumWidget()
@@ -62,28 +48,29 @@
     delete m_textItemGroup;
 }
 
-void NewEnumWidget::init()
-{
-    if(umlScene()) {
-        const Settings::OptionState& ops = umlScene()->getOptionState();
-        m_showPackage = ops.classState.showPackage;
-    }
+// void NewEnumWidget::init()
+// {
+//     if(umlScene()) {
+//         const Settings::OptionState& ops = umlScene()->getOptionState();
+//         m_showPackage = ops.classState.showPackage;
+//     }
 
-    updateGeometry();
-}
+//     updateGeometry();
+// }
 
 QSizeF NewEnumWidget::sizeHint(Qt::SizeHint which)
 {
-    switch(which)
-    {
-    case Qt::MinimumSize:
+    if(which == Qt::MinimumSize) {
         return m_minimumSize;
-
-    default:
-        return NewUMLRectWidget::sizeHint(which);
     }
+    return NewUMLRectWidget::sizeHint(which);
 }
 
+/**
+ * Set whether to show package or not.
+ *
+ * @param b True to show package, false to hide.
+ */
 void NewEnumWidget::setShowPackage(bool b)
 {
     m_showPackage = b;
@@ -100,12 +87,14 @@
     // First draw the outer rectangle with the pen and brush of this widget.
     painter->drawRect(rect());
 
+    // Now get the position to draw the line.
     const TextItem *item = m_textItemGroup->textItemAt(NameItemIndex);
     const QPointF bottomLeft = item->mapToParent(item->boundingRect().bottomLeft());
     const qreal y = bottomLeft.y();
     painter->drawLine(QLineF(0, y, size().width() - 1, y));
 }
 
+/// Loads from an "enumwidget" XMI element.
 bool NewEnumWidget::loadFromXMI( QDomElement & qElement )
 {
     if( !NewUMLRectWidget::loadFromXMI(qElement) ) {
@@ -116,7 +105,9 @@
     return true;
 }
 
-void NewEnumWidget::saveToXMI( QDomDocument& qDoc, QDomElement& qElement ) {
+/// Saves to the "enumwidget" XMI element.
+void NewEnumWidget::saveToXMI( QDomDocument& qDoc, QDomElement& qElement )
+{
     QDomElement conceptElement = qDoc.createElement("enumwidget");
     NewUMLRectWidget::saveToXMI(qDoc, conceptElement);
 
@@ -124,74 +115,89 @@
     qElement.appendChild(conceptElement);
 }
 
+void NewEnumWidget::slotMenuSelection(QAction *action)
+{
+    ListPopupMenu *menu = qobject_cast<ListPopupMenu*>(action->parent());
+    if (!menu) {
+        uError() << "ListPopupMenu's pointer should be the parent of the action parameter of this slot";
+        return;
+    }
+
+    ListPopupMenu::Menu_Type sel = menu->getMenuType(action);
+
+    if (sel == ListPopupMenu::mt_EnumLiteral) {
+        if (!umlObject()) {
+            uWarning() << "There is no UMLObject for this widget to create the literal!";
+        }
+        else if (Object_Factory::createChildObject(static_cast<UMLClassifier*>(umlObject()),
+                                              Uml::ot_EnumLiteral) )  {
+            UMLApp::app()->getDocument()->setModified();
+        }
+        return;
+    }
+
+    NewUMLRectWidget::slotMenuSelection(action);
+}
+
+/**
+ * Overidden to update the literals, enum name and stereotype text
+ * display on modifying the same.
+ */
 void NewEnumWidget::updateGeometry()
 {
     if(umlObject()) {
         UMLClassifier *classifier = static_cast<UMLClassifier*>(umlObject());
         UMLClassifierListItemList list = classifier->getFilteredList(Uml::ot_EnumLiteral);
         int totalTextItems = list.size() + 2; // +2 because stereo text + name text.
-        bool shouldAlign = false;
 
-        QBrush hoverBrush = randHoverBrush();
-
-        m_textItemGroup->unparent();
-
+        // Create a dummy item with the common properties set on
+        // it. We can then use TextItem::copyAttributesTo method to
+        // copy these common attributes, just to avoid some code
+        // duplication.
         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() != totalTextItems) {
-            while(m_textItemGroup->size() < totalTextItems) {
-                m_textItemGroup->appendTextItem(new TextItem(""));
-            }
+        // Ensure there are appropriate number of textitems.
+        m_textItemGroup->ensureTextItemCount(totalTextItems);
 
-            while(m_textItemGroup->size() > totalTextItems) {
-                int lastIndex = m_textItemGroup->textItems().size() - 1;
-                m_textItemGroup->deleteTextItemAt(lastIndex);
-            }
-            shouldAlign = true;
-        }
-
-        TextItem *stereo = m_textItemGroup->textItemAt(StereoTypeItemIndex);
+        TextItem *stereo = m_textItemGroup->textItemAt(NewEnumWidget::StereoTypeItemIndex);
         stereo->setText(classifier->getStereotype(true));
         dummy.copyAttributesTo(stereo);
         stereo->setBold(true);
-        stereo->setToolTip(stereo->text());
 
-        TextItem *nameItem = m_textItemGroup->textItemAt(NameItemIndex);
+        TextItem *nameItem = m_textItemGroup->textItemAt(NewEnumWidget::NameItemIndex);
         nameItem->setText(m_showPackage ?
                           classifier->getFullyQualifiedName() :
                           name());
         dummy.copyAttributesTo(nameItem);
         nameItem->setBold(true);
         nameItem->setItalic(classifier->getAbstract());
-        nameItem->setToolTip(nameItem->text());
 
-        int index = NameItemIndex + 1;
+        int index = NewEnumWidget::EnumLiteralStartIndex;
         foreach(UMLClassifierListItem* enumLiteral, list) {
             TextItem *literal = m_textItemGroup->textItemAt(index);
             literal->setText(enumLiteral->getName());
             dummy.copyAttributesTo(literal);
-            literal->setToolTip(literal->text());
             ++index;
         }
 
         m_minimumSize = m_textItemGroup->calculateMinimumSize();
-        m_minimumSize.rwidth() += ENUM_MARGIN * 2;
-
-        m_textItemGroup->reparent();
+        m_minimumSize += QSizeF(NewEnumWidget::Margin * 2, NewEnumWidget::Margin * 2);
     }
     NewUMLRectWidget::updateGeometry();
 }
 
 void NewEnumWidget::sizeHasChanged(const QSizeF& oldSize)
 {
-    m_textItemGroup->alignVertically(size().width() - 2*ENUM_MARGIN, size().height());
-    m_textItemGroup->setPos(QPointF(ENUM_MARGIN, 0));
+    QSizeF groupSize = size();
+    groupSize -= QSizeF(NewEnumWidget::Margin * 2, NewEnumWidget::Margin * 2);
+    QPointF offset(NewEnumWidget::Margin, NewEnumWidget::Margin);
 
+    m_textItemGroup->alignVertically(groupSize);
+    m_textItemGroup->setPos(offset);
+
     NewUMLRectWidget::sizeHasChanged(oldSize);
 }
--- branches/work/soc-umbrello/umbrello/newenumwidget.h #831925:831926
@@ -23,7 +23,6 @@
 
 #include "newumlrectwidget.h"
 
-#define ENUM_MARGIN 5
 class TextItemGroup;
 
 /**
@@ -35,75 +34,48 @@
  */
 class NewEnumWidget : public NewUMLRectWidget
 {
+    Q_OBJECT
 public:
-
     /**
      * Constructs an instance of NewEnumWidget.
      * @param o The NewUMLObject this will be representing.
      */
     explicit NewEnumWidget(UMLObject* o);
-
     ~NewEnumWidget();
 
-    /**
-     * Do some initialization which cannot be done inside constructor
-     * as it involves calling virtual methods.
-     */
-    void init();
-
     QSizeF sizeHint(Qt::SizeHint which);
 
-    /**
-     * @return True if package is shown , false otherwise.
-     */
+    ///  @return True if package is shown , false otherwise.
     bool showPackage() const {
         return m_showPackage;
     }
-
-    /**
-     * Set whether to show package or not.
-     *
-     * @param b True to show package, false not to show.
-     */
     void setShowPackage(bool b);
-
-    /**
-     * Toggles the status of package show.
-     */
+    /// Toggles the status of package show.
     void toggleShowPackage() {
         setShowPackage(!m_showPackage);
     }
 
-    /**
-     * Reimplemented to draw as needed.
-     * Draws the enum as a rectangle with a box underneith with a list of literals
-     */
     void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *widget);
 
-    /**
-     * Loads from an "enumwidget" XMI element.
-     */
     bool loadFromXMI(QDomElement& qElement);
-
-    /**
-     * Saves to the "enumwidget" XMI element.
-     */
     void saveToXMI(QDomDocument& qDoc, QDomElement& qElement);
 
+public slots:
+    void slotMenuSelection(QAction *action);
+
 protected:
-    /**
-     * Overrides method from NewUMLRectWidget.
-     */
     void updateGeometry();
-
     void sizeHasChanged(const QSizeF& oldSize);
 
 private:
     enum {
         StereoTypeItemIndex = 0,
-        NameItemIndex = 1
+        NameItemIndex = 1,
+        EnumLiteralStartIndex
     };
 
+    static const qreal Margin;
+
     QSizeF m_minimumSize;
     bool m_showPackage;
 
--- branches/work/soc-umbrello/umbrello/test.cpp #831925:831926
@@ -35,17 +35,30 @@
 
 Test* Test::m_self = 0;
 
+QBrush randHoverBrush()
+{
+    QLinearGradient grad(QPointF(0,0), QPointF(0, 1));
+    int r, g, b, a;
+    r = qrand() % 255;
+    g = qrand() % 255;
+    b = qrand() % 255;
+    a = 10 + qrand() % 245; // set minimum to atleast 10
+    grad.setColorAt(0, QColor(r, g, b, a));
+
+    r = qrand() % 255;
+    g = qrand() % 255;
+    b = qrand() % 255;
+    a = 10 + qrand() % 245; // set minimum to atleast 10
+    grad.setColorAt(1, QColor(r, g, b, a));
+
+    return QBrush(grad);
+}
+
 struct TestPrivate
 {
-    TestPrivate() : scene(0), enumWidget(0), enumObject(0), count(0)
+    TestPrivate()
     {
     }
-
-    UMLScene *scene;
-    QString xml;
-    NewEnumWidget *enumWidget;
-    UMLEnum *enumObject;
-    int count;
 };
 
 Test::Test() :
@@ -64,59 +77,6 @@
 
 void Test::testScene(UMLScene *scene)
 {
-    d->scene = scene;
-
-    BoxWidget *w = new BoxWidget("131313");
-    scene->addItem(w);
-
-    TextItem *item = new TextItem("Notice loading of gradient brush from xmi in 3 secs");
-
-    item->setFlag(QGraphicsItem::ItemIsMovable, true);
-    item->setAcceptHoverEvents(true);
-    item->setHoverBrush(Qt::cyan);
-    item->setBackgroundBrush(Qt::darkCyan);
-    item->setDefaultTextColor(Qt::darkBlue);
-    item->setTextWidth(100);
-
-    scene->addItem(item);
-
-    uDebug() << "entered test";
-
-    UMLEnum *en = new UMLEnum("Qt::SizeHint");
-    d->enumObject = en;
-    en->createEnumLiteral("MinimumSize");
-    en->createEnumLiteral("MaximumSize");
-    en->createEnumLiteral("PreferredSize");
-    en->createEnumLiteral("MaximumSize");
-    NewEnumWidget *wid = new NewEnumWidget(en);
-
-    wid->setFontColor(Qt::darkBlue);
-    wid->setLineColor(Qt::darkGreen);
-    wid->init();
-    scene->addItem(wid);
-    wid->setPos(40, 40);
-
-    wid->setBrush(randomGradientBrush());
-    wid->setSize(220, 120);
-
-    QDomDocument doc("TEST");
-    //doc
-    QDomElement ele = doc.createElement("TopLevel");
-    doc.appendChild(ele);
-
-    wid->saveToXMI(doc, ele);
-
-    d->xml = doc.toString();
-    d->enumWidget = wid;
-    wid->setBrush(randomGradientBrush());
-    uDebug() << "-------------------";
-    uDebug() << d->xml;
-    uDebug() << "-------------------";
-    wid->setShowPackage(true);
-    en->createEnumLiteral("MaximumSizeHintTester");
-    uDebug() << "leaving test";
-
-    startTimer(3 * 1000);
 }
 
 QBrush Test::randomGradientBrush()
@@ -161,20 +121,7 @@
 
 void Test::timerEvent(QTimerEvent *event)
 {
-    if(d->count == 0) {
-        QDomDocument doc("TEST");
-        doc.setContent(d->xml);
-
-        QDomElement ele = doc.documentElement().firstChild().toElement();
-        d->enumWidget->loadFromXMI(ele);
-    }
-    else if(d->count < 8) {
-        d->enumObject->createEnumLiteral("MaximumSize" + QString::number(qrand() % 100));
-    }
-    else {
-        killTimer(event->timerId());
-    }
-    ++d->count;
+    return QObject::timerEvent(event);
 }
 
 #include "test.moc"




More information about the umbrello-devel mailing list