[Uml-devel] branches/work/soc-umbrello/umbrello
Gopala Krishna A
krishna.ggk at gmail.com
Sun Jul 20 19:11:05 UTC 2008
SVN commit 835555 by gopala:
Implemented all the interface methods and attributes(port) for CategoryWidget.
Also a bug where CategoryWidget's circle overflows the widget size is fixed.
This widget is the fastest port till now!
M +83 -46 categorywidget.cpp
M +18 -48 categorywidget.h
M +2 -2 widget_factory.cpp
--- branches/work/soc-umbrello/umbrello/categorywidget.cpp #835554:835555
@@ -11,50 +11,68 @@
// own header file
#include "categorywidget.h"
-// system includes
-#include <qpainter.h>
-#include <kdebug.h>
+
// local includes
#include "category.h"
-#include "umlview.h"
#include "listpopupmenu.h"
-CategoryWidget::CategoryWidget(UMLScene * scene, UMLCategory *o) : NewUMLRectWidget(scene, o)
+// qt includes
+#include <QtGui/QPainter>
+
+const qreal CategoryWidget::Margin = 5.;
+
+/**
+ * Creates a Category widget.
+ *
+ * @param o The UMLCategory to represent.
+ */
+CategoryWidget::CategoryWidget(UMLCategory *o) : NewUMLRectWidget(o)
{
- NewUMLRectWidget::setBaseType(Uml::wt_Category);
- //updateComponentSize(); Doing this during loadFromXMI() gives futile updates.
- // Instead, it is done afterwards by NewUMLRectWidget::activate()
+ m_baseType = Uml::wt_Category;
}
-CategoryWidget::~CategoryWidget() {}
+/// Destructor
+CategoryWidget::~CategoryWidget()
+{
+}
+/// Reimplemented from NewUMLRectWidget::sizeHint
+QSizeF CategoryWidget::sizeHint(Qt::SizeHint which)
+{
+ if(which == Qt::MinimumSize) {
+ return m_minimumSize;
+ }
+ return NewUMLRectWidget::sizeHint(which);
+}
+
+/// Reimplemented from NewUMLRectWidget::paint to draw this widget.
void CategoryWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *)
{
- QPainter &p = *painter;
- qreal offsetX = 0, offsetY = 0;
+ if(!umlObject()) {
+ uWarning() << "No UMLCategory object for this widget to paint";
+ return;
+ }
- NewUMLRectWidget::setPenFromSettings(p);
- if ( NewUMLRectWidget::getUseFillColour() )
- p.setBrush( NewUMLRectWidget::getFillColour() );
- QFont font = NewUMLRectWidget::getFont();
- font.setUnderline(false);
- font.setBold(false);
- font.setItalic( umlObject()->getAbstract() );
- p.setFont( font );
- const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
- const qreal fontHeight = fm.lineSpacing();
- // the height is our radius
- const qreal h = getHeight();
- const qreal w = getWidth();
- const qreal r = h > w ? h : w;
+ QFont fnt = font();
+ fnt.setItalic( umlObject()->getAbstract() );
- //qreal middleX = w / 2;
- const qreal textStartY = (r / 2) - (fontHeight / 2);
+ painter->setPen(QPen(lineColor(), lineWidth()));
+ painter->setBrush(brush());
+ painter->setFont(fnt);
+ const QSizeF sz = size();
+ const qreal radius = qMin(sz.width(), sz.height());
+
// draw a circle
- p.drawEllipse(offsetX, offsetY, r, r);
- p.setPen(Qt::black);
+ QRectF circle(0, 0, radius, radius);
+ // adjust circle to be at center (0 + width / 2, 0 + height / 2)
+ circle.moveCenter(QPointF(sz.width(), sz.height()) * .5);
+ painter->drawEllipse(circle);
+
+ // Now draw the letter inside circle.
+ painter->setPen(fontColor());
+
QString letterType('D');
switch( static_cast<UMLCategory*>( umlObject() )->getType() ) {
case UMLCategory::ct_Disjoint_Specialisation:
@@ -70,31 +88,49 @@
break;
}
- p.drawText(offsetX + UC_MARGIN, offsetY + textStartY, r - UC_MARGIN * 2, fontHeight, Qt::AlignCenter, letterType );
- NewUMLRectWidget::setPenFromSettings(p);
- if(isSelected()) {
- drawSelected(&p, offsetX, offsetY);
- }
+ painter->drawText(circle, Qt::AlignCenter, letterType );
}
-QSizeF CategoryWidget::calculateSize() {
- const NewUMLRectWidget::FontType ft = ( umlObject()->getAbstract() ? FT_BOLD_ITALIC : FT_BOLD );
- const QFontMetrics &fm = NewUMLRectWidget::getFontMetrics(ft);
- const qreal fontHeight = fm.lineSpacing();
- qreal radius = UC_RADIUS + fontHeight + UC_MARGIN;
-
- return QSizeF(radius, radius);
-}
-
-void CategoryWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement ) {
+/// Reimplemented from NewUMLRectWidget::saveToXMI to save CategoyWidget
+void CategoryWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement )
+{
QDomElement categoryElement = qDoc.createElement( "categorywidget" );
NewUMLRectWidget::saveToXMI( qDoc, categoryElement );
qElement.appendChild( categoryElement );
}
-void CategoryWidget::slotMenuSelection(QAction* action){
+void CategoryWidget::updateGeometry()
+{
+ QFontMetrics fm(font());
+ qreal minRadius = fm.lineSpacing() + CategoryWidget::Margin;
+ m_minimumSize = QSizeF(minRadius, minRadius);
+
+ NewUMLRectWidget::updateGeometry();
+}
+
+void CategoryWidget::sizeHasChanged(const QSizeF& oldSize)
+{
+ NewUMLRectWidget::sizeHasChanged(oldSize);
+}
+
+/**
+ * Will be called when a menu selection has been made from the
+ * popup menu.
+ *
+ * @param action The action that has been selected.
+ */
+void CategoryWidget::slotMenuSelection(QAction* action)
+{
UMLCategory* catObj = static_cast<UMLCategory*>(umlObject());
- ListPopupMenu::Menu_Type sel = m_pMenu->getMenuType(action);
+ if(!catObj) {
+ uWarning() << "No UMLCategory for this widget.";
+ return;
+ }
+
+ // menu is passed in as parent .
+ ListPopupMenu *menu = qobject_cast<ListPopupMenu*>(action->parent());
+ ListPopupMenu::Menu_Type sel = menu->getMenuType(action);
+
switch(sel) {
case ListPopupMenu::mt_DisjointSpecialisation:
catObj->setType(UMLCategory::ct_Disjoint_Specialisation);
@@ -113,3 +149,4 @@
}
}
+#include "categorywidget.moc"
--- branches/work/soc-umbrello/umbrello/categorywidget.h #835554:835555
@@ -11,74 +11,44 @@
#ifndef CATEGORYWIDGET_H
#define CATEGORYWIDGET_H
-#include "umlwidget.h"
-#define UC_MARGIN 5
-#define UC_RADIUS 30
+#include "newumlrectwidget.h"
-
class UMLCategory;
/**
- * This class is the graphical version of a UMLCategory. A CategoryWidget is created
- * by a @ref UMLView. An CategoryWidget belongs to only one @ref UMLView instance.
- * When the @ref UMLView instance that this class belongs to, it will be automatically deleted.
+ * This class is the graphical version of a UMLCategory. The
+ * CategoryWidget class inherits from the @ref NewUMLRectWidget class
+ * which adds most of the functionality to this class.
*
- * If the Category class that this CategoryWidget is displaying is deleted, the @ref UMLView will
- * make sure that this instance is also deleted.
- *
- * The CategoryWidget class inherits from the @ref NewUMLRectWidget class which adds most of the functionality
- * to this class.
- *
* @short A graphical version of a UMLCategory.
* @author Sharan Rao
+ * @author Gopala Krishna
* Bugs and comments to uml-devel at lists.sf.net or http://bugs.kde.org
*/
class CategoryWidget : public NewUMLRectWidget
{
+ Q_OBJECT
public:
+ CategoryWidget(UMLCategory *o);
+ virtual ~CategoryWidget();
- /**
- * Creates a Category widget.
- *
- * @param view The parent of the widget.
- * @param o The UMLObject to represent.
- */
- CategoryWidget(UMLScene * scene, UMLCategory *o);
+ virtual QSizeF sizeHint(Qt::SizeHint which);
+ virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
+ // For loading , NewUMLRectWidget::loadFromXMI() is used.
+ virtual void saveToXMI( QDomDocument & qDoc, QDomElement & qElement );
- /**
- * destructor
- */
- virtual ~CategoryWidget();
-
- /**
- * Overrides the standard paint event.
- */
- void paint(QPainter *p, const QStyleOptionGraphicsItem *item, QWidget *w);
-
- /**
- * Saves this Category to file.
- */
- void saveToXMI( QDomDocument & qDoc, QDomElement & qElement );
-
- // For loading we can use the loadFromXMI() inherited from NewUMLRectWidget.
-
protected:
- /**
- * Overrides method from NewUMLRectWidget
- */
- QSizeF calculateSize();
+ virtual void updateGeometry();
+ virtual void sizeHasChanged(const QSizeF& oldSize);
-public slots:
- /**
- * Will be called when a menu selection has been made from the
- * popup menu.
- *
- * @param action The action that has been selected.
- */
+public Q_SLOTS:
void slotMenuSelection(QAction* action);
+private:
+ static const qreal Margin;
+ QSizeF m_minimumSize;
};
#endif
--- branches/work/soc-umbrello/umbrello/widget_factory.cpp #835554:835555
@@ -142,7 +142,7 @@
}
break;
case Uml::ot_Category:
- newWidget = new CategoryWidget(scene, static_cast<UMLCategory*>(o));
+ newWidget = new CategoryWidget(static_cast<UMLCategory*>(o));
break;
default:
uWarning() << "trying to create an invalid widget";
@@ -265,7 +265,7 @@
widget = new EntityWidget(scene, static_cast<UMLEntity*>(o));
} else if (tag == "categorywidget") {
if (validateObjType(Uml::ot_Category, o, id))
- widget = new CategoryWidget(scene, static_cast<UMLCategory*>(o));
+ widget = new CategoryWidget(static_cast<UMLCategory*>(o));
} else if (tag == "objectwidget" || tag == "UML:ObjectWidget") {
widget = new ObjectWidget(scene, o );
} else {
More information about the umbrello-devel
mailing list