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

Gopala Krishna A krishna.ggk at gmail.com
Fri Jul 25 15:28:48 UTC 2008


SVN commit 837736 by gopala:

Ported ForkJoinWidget.
The following two important changes are to be noted

1) Use Qt::Orientation instead of bool drawVertical flag so that code
   is more readable.

2) Incorporated "Fill color" option into context menu of
   ForkJoinWidget which means users can now change color of
   ForkJoinWidget.

Sidenote: This is the first widget to use
          NewUMLRectWidget::setMaximumSize and there was no need for
          any explicit code to manage widget constraints.  
	  I love abstraction!



 M  +79 -83    forkjoinwidget.cpp  
 M  +22 -63    forkjoinwidget.h  
 M  +6 -2      listpopupmenu.cpp  
 M  +1 -1      toolbarstateother.cpp  
 M  +1 -1      widget_factory.cpp  


--- branches/work/soc-umbrello/umbrello/forkjoinwidget.cpp #837735:837736
@@ -11,111 +11,107 @@
 
 // own header
 #include "forkjoinwidget.h"
-//qt includes
-#include <qdom.h>
-//kde includes
-#include <kcursor.h>
-#include <kdebug.h>
+
 //app includes
-#include "umlview.h"
 #include "listpopupmenu.h"
-#include "umlscene.h"
 
-ForkJoinWidget::ForkJoinWidget(UMLScene * scene, bool drawVertical, Uml::IDType id)
-    : BoxWidget(id), m_drawVertical(drawVertical)
+
+/**
+ * Constructs a ForkJoinWidget.
+ * @param o The orientation of ForkJoinWidget.
+ * @param id ID of the widget. (-1 for new id)
+ */
+ForkJoinWidget::ForkJoinWidget(Qt::Orientation o, Uml::IDType id)
+    : BoxWidget(id),
+	  m_orientation(o)
 {
-    init();
+	m_baseType = Uml::wt_ForkJoin;
+	setMargin(0);
+	setBrush(QBrush(Qt::black));
 }
 
-void ForkJoinWidget::init()
+/// Destructor
+ForkJoinWidget::~ForkJoinWidget()
 {
-    NewUMLWidget::setBaseType( Uml::wt_ForkJoin );
-    NewUMLRectWidget::updateComponentSize();
 }
 
-ForkJoinWidget::~ForkJoinWidget() {
+/// Sets the orientation of this widget to \a o
+void ForkJoinWidget::setOrientation(Qt::Orientation o)
+{
+	m_orientation = o;
+	updateGeometry();
 }
 
-QSizeF ForkJoinWidget::calculateSize()
+/**
+ * Reimplemented from NewUMLRectWidget::paint to draw the plate of
+ * fork join.
+ */
+void ForkJoinWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
 {
-    if (m_drawVertical) {
-        return QSizeF(4, 40);
-    } else {
-        return QSizeF(40, 4);
-    }
+    painter->fillRect(rect(), brush());
 }
 
-void ForkJoinWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
+/**
+ * Reimplemented from NewUMLRectWidget::loadFromXMI to load widget
+ * info from XMI element - 'forkjoin'.
+ */
+bool ForkJoinWidget::loadFromXMI(QDomElement& qElement)
 {
-	QPainter &p = *painter;
-	qreal offsetX = 0, offsetY = 0;
-
-    p.fillRect( offsetX, offsetY, getWidth(), getHeight(), QBrush( Qt::black ));
-
-    if (isSelected()) {
-        drawSelected(&p, offsetX, offsetY);
+    if ( !NewUMLRectWidget::loadFromXMI(qElement) ) {
+        return false;
     }
+    QString orientation = qElement.attribute("orientation",
+											 QString::number(Qt::Horizontal));
+    setOrientation( (Qt::Orientation)orientation.toInt() );
+    return true;
 }
 
-void ForkJoinWidget::drawSelected(QPainter *, qreal /*offsetX*/, qreal /*offsetY*/) {
-}
-
-void ForkJoinWidget::constrain(qreal& width, qreal& height) {
-    if (m_drawVertical) {
-        if (width < 4)
-            width = 4;
-        else if (width > 10)
-            width = 10;
-        if (height < 40)
-            height = 40;
-        else if (height > 100)
-            height = 100;
-    } else {
-        if (height < 4)
-            height = 4;
-        else if (height > 10)
-            height = 10;
-        if (width < 40)
-            width = 40;
-        else if (width > 100)
-            width = 100;
-    }
-}
-
-void ForkJoinWidget::slotMenuSelection(QAction* action) {
-    ListPopupMenu::Menu_Type sel = m_pMenu->getMenuType(action);
-    switch (sel) {
-    case ListPopupMenu::mt_Flip:
-        setDrawVertical(!m_drawVertical);
-        break;
-    default:
-        break;
-    }
-}
-
-void ForkJoinWidget::setDrawVertical(bool to) {
-    m_drawVertical = to;
-    updateComponentSize();
-    NewUMLRectWidget::adjustAssocs( getX(), getY() );
-}
-
-bool ForkJoinWidget::getDrawVertical() const {
-    return m_drawVertical;
-}
-
-void ForkJoinWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement) {
+/**
+ * Reimplemented from NewUMLRectWidget::saveToXMI to save widget info
+ * into XMI element - 'forkjoin'.
+ */
+void ForkJoinWidget::saveToXMI(QDomDocument& qDoc, QDomElement& qElement)
+{
     QDomElement fjElement = qDoc.createElement("forkjoin");
     NewUMLRectWidget::saveToXMI(qDoc, fjElement);
-    fjElement.setAttribute("drawvertical", m_drawVertical);
+    fjElement.setAttribute("orientation", m_orientation);
     qElement.appendChild(fjElement);
 }
 
-bool ForkJoinWidget::loadFromXMI(QDomElement& qElement) {
-    if ( !NewUMLRectWidget::loadFromXMI(qElement) ) {
-        return false;
-    }
-    QString drawVertical = qElement.attribute("drawvertical", "0");
-    setDrawVertical( (bool)drawVertical.toInt() );
-    return true;
+/**
+ * Reimplemented form NewUMLRectWidget::slotMenuSelection to handle
+ * Flip action.
+ */
+void ForkJoinWidget::slotMenuSelection(QAction* action)
+{
+	// Menu is passed as parent of action
+	ListPopupMenu *menu = qobject_cast<ListPopupMenu*>(action->parent());
+	if (menu->getMenuType(action) == ListPopupMenu::mt_Flip) {
+        setOrientation(m_orientation == Qt::Horizontal ?
+					   Qt::Vertical : Qt::Horizontal);
+	}
+	else {
+		BoxWidget::slotMenuSelection(action);
+	}
 }
 
+/**
+ * Reimplemented from NewUMLRectWidget::updateGeometry to update the
+ * minimum and maximum size of the widget based on current
+ * orientation.
+ */
+void ForkJoinWidget::updateGeometry()
+{
+	if(m_orientation == Qt::Horizontal) {
+		setMinimumSize(QSizeF(40, 4));
+		setMaximumSize(QSizeF(100, 10));
+	}
+	else {
+		setMinimumSize(QSizeF(4, 40));
+		setMaximumSize(QSizeF(10, 100));
+	}
+
+	BoxWidget::updateGeometry();
+}
+
+#include "forkjoinwidget.moc"
--- branches/work/soc-umbrello/umbrello/forkjoinwidget.h #837735:837736
@@ -11,93 +11,52 @@
 
 #ifndef FORKJOINWIDGET_H
 #define FORKJOINWIDGET_H
-//qt includes
-#include <QtGui/QPainter>
+
 //app includes
 #include "boxwidget.h"
 
-// fwd decl.
-class UMLView;
-
 /**
  * @short Displays a fork/join plate in a state diagram.
+ *
  * @author Oliver Kellogg  <okellogg at users.sourceforge.net>
+ * @author Gopala Krishna
+ *
  * @see NewUMLRectWidget
  * Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
  */
-class ForkJoinWidget : public BoxWidget {
+class ForkJoinWidget : public BoxWidget
+{
+	Q_OBJECT
 public:
-
     /**
      * Constructs a ForkJoinWidget.
      *
-     * @param view          The parent to this widget.
      * @param drawVertical  Whether to draw the plate horizontally or vertically.
      * @param id            The ID to assign (-1 will prompt a new ID.)
      */
-    explicit ForkJoinWidget(UMLScene * scene, bool drawVertical = false, Uml::IDType id = Uml::id_None);
+    explicit ForkJoinWidget(Qt::Orientation o = Qt::Horizontal, Uml::IDType id = Uml::id_None);
+	virtual ~ForkJoinWidget();
 
-    /**
-     * destructor
-     */
-    virtual ~ForkJoinWidget();
+	/// @return Whether to draw plate vertically or not.
+	Qt::Orientation orientation() const {
+		return m_orientation;
+	}
+    void setOrientation(Qt::Orientation o);
 
-    /**
-     * Set whether to draw the plate vertically.
-     */
-    void setDrawVertical(bool to);
-    /**
-     * Get whether to draw the plate vertically.
-     */
-    bool getDrawVertical() const;
+    virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
 
-    /**
-     * Overrides the function from NewUMLRectWidget.
-     *
-     * @param action  The action to be executed.
-     */
-    void slotMenuSelection(QAction* action);
+    virtual bool loadFromXMI(QDomElement & qElement);
+    virtual void saveToXMI(QDomDocument& qDoc, QDomElement& qElement);
 
-    /**
-     * Draws a slim solid black rectangle.
-     */
-    void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
+public Q_SLOTS:
+    virtual void slotMenuSelection(QAction* action);
 
-    /**
-     * Saves the widget to the "forkjoinwidget" XMI element.
-     */
-    void saveToXMI(QDomDocument& qDoc, QDomElement& qElement);
-
-    /**
-     * Loads the widget from the "forkjoinwidget" XMI element.
-     */
-    bool loadFromXMI(QDomElement & qElement);
-
 protected:
-    /**
-     * Reimplement method from NewUMLRectWidget to suppress the resize corner.
-     * Although the ForkJoinWidget supports resizing, we suppress the
-     * resize corner because it is too large for this very slim widget.
-     */
-    void drawSelected(QPainter * p, qreal offsetX, qreal offsetY);
+	virtual void updateGeometry();
 
-    /**
-    * Overrides the function from NewUMLRectWidget.
-    */
-    QSizeF calculateSize();
-
-    /**
-     * Reimplement method from NewUMLRectWidget.
-     */
-    void constrain(qreal& width, qreal& height);
-
 private:
-    /**
-     * Initializes key variables for the class.
-     */
-    void init();
-
-    bool m_drawVertical;   ///< whether to draw the plate horizontally or vertically
+	/// whether to draw the plate horizontally or vertically
+    Qt::Orientation m_orientation;
 };
 
 #endif
--- branches/work/soc-umbrello/umbrello/listpopupmenu.cpp #837735:837736
@@ -411,10 +411,14 @@
     case Uml::wt_ForkJoin:
         {
             ForkJoinWidget *pForkJoin = static_cast<ForkJoinWidget*>(object);
-            if (pForkJoin->getDrawVertical())
+            if (pForkJoin->orientation() == Qt::Vertical) {
                 insert(mt_Flip, i18n("Flip Horizontal"));
-            else
+			}
+            else {
                 insert(mt_Flip, i18n("Flip Vertical"));
+			}
+			m_actions[mt_Fill_Color] = addAction(Icon_Utils::SmallIcon(Icon_Utils::it_Color_Fill),
+												 i18n("Fill Color..."));
         }
         break;
 
--- branches/work/soc-umbrello/umbrello/toolbarstateother.cpp #837735:837736
@@ -131,7 +131,7 @@
 
         case WorkToolBar::tbb_Fork:
         case WorkToolBar::tbb_StateFork:
-            umlWidget = new ForkJoinWidget(m_pUMLScene);
+            umlWidget = new ForkJoinWidget();
             break;
 
         case WorkToolBar::tbb_Initial_State:
--- branches/work/soc-umbrello/umbrello/widget_factory.cpp #837735:837736
@@ -199,7 +199,7 @@
     } else if (tag == "messagewidget") {
         widget = new MessageWidget(scene, Uml::sequence_message_asynchronous, Uml::id_Reserved);
     } else if (tag == "forkjoin") {
-        widget = new ForkJoinWidget(scene, false, Uml::id_Reserved);
+        widget = new ForkJoinWidget(Qt::Horizontal, Uml::id_Reserved);
     } else if (tag == "preconditionwidget") {
         widget = new PreconditionWidget(scene, NULL, Uml::id_Reserved);
     } else if (tag == "combinedFragmentwidget") {




More information about the umbrello-devel mailing list