00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "activitywidget.h"
00014
00015
00016 #include <qpainter.h>
00017
00018
00019 #include <klocale.h>
00020 #include <kdebug.h>
00021 #include <kinputdialog.h>
00022
00023
00024 #include "uml.h"
00025 #include "umldoc.h"
00026 #include "docwindow.h"
00027 #include "umlview.h"
00028 #include "listpopupmenu.h"
00029 #include "dialogs/activitydialog.h"
00030
00031 ActivityWidget::ActivityWidget(UMLView * view, ActivityType activityType, Uml::IDType id )
00032 : UMLWidget(view, id)
00033 {
00034 UMLWidget::setBaseType( Uml::wt_Activity );
00035 setActivityType( activityType );
00036 updateComponentSize();
00037 }
00038
00039 ActivityWidget::~ActivityWidget() {}
00040
00041 void ActivityWidget::draw(QPainter & p, int offsetX, int offsetY) {
00042 int w = width();
00043 int h = height();
00044 switch ( m_ActivityType )
00045 {
00046 case Normal :
00047 UMLWidget::setPen(p);
00048 if ( UMLWidget::getUseFillColour() ) {
00049 p.setBrush( UMLWidget::getFillColour() );
00050 }
00051 {
00052 const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00053 const int fontHeight = fm.lineSpacing();
00054
00055 int textStartY = (h / 2) - (fontHeight / 2);
00056 p.drawRoundRect(offsetX, offsetY, w, h, (h * 60) / w, 60);
00057 p.setPen(Qt::black);
00058 p.setFont( UMLWidget::getFont() );
00059 p.drawText(offsetX + ACTIVITY_MARGIN, offsetY + textStartY,
00060 w - ACTIVITY_MARGIN * 2, fontHeight, Qt::AlignCenter, getName());
00061 }
00062 UMLWidget::setPen(p);
00063 break;
00064 case Initial :
00065 p.setPen( QPen(m_LineColour, 1) );
00066 p.setBrush( WidgetBase::getLineColor() );
00067 p.drawEllipse( offsetX, offsetY, w, h );
00068 break;
00069 case End :
00070 p.setPen( QPen(m_LineColour, 1) );
00071 p.setBrush( WidgetBase::getLineColor() );
00072 p.drawEllipse( offsetX, offsetY, w, h );
00073 p.setBrush( Qt::white );
00074 p.drawEllipse( offsetX + 1, offsetY + 1, w - 2, h - 2 );
00075 p.setBrush( WidgetBase::getLineColor() );
00076 p.drawEllipse( offsetX + 3, offsetY + 3, w - 6, h - 6 );
00077 break;
00078 case Branch :
00079 UMLWidget::setPen(p);
00080 p.setBrush( UMLWidget::getFillColour() );
00081 {
00082 QPointArray array( 4 );
00083 array[ 0 ] = QPoint( offsetX + w / 2, offsetY );
00084 array[ 1 ] = QPoint( offsetX + w, offsetY + h / 2 );
00085 array[ 2 ] = QPoint( offsetX + w / 2, offsetY + h );
00086 array[ 3 ] = QPoint( offsetX, offsetY + h / 2 );
00087 p.drawPolygon( array );
00088 p.drawPolyline( array );
00089 }
00090 break;
00091 }
00092 if(m_bSelected)
00093 drawSelected(&p, offsetX, offsetY);
00094 }
00095
00096 void ActivityWidget::constrain(int& width, int& height) {
00097 if (m_ActivityType == Normal) {
00098 QSize minSize = calculateSize();
00099 if (width < minSize.width())
00100 width = minSize.width();
00101 if (height < minSize.height())
00102 height = minSize.height();
00103 return;
00104 }
00105 if (width > height)
00106 width = height;
00107 else if (height > width)
00108 height = width;
00109 if (m_ActivityType == Branch) {
00110 if (width < 20) {
00111 width = 20;
00112 height = 20;
00113 } else if (width > 50) {
00114 width = 50;
00115 height = 50;
00116 }
00117 } else {
00118 if (width < 15) {
00119 width = 15;
00120 height = 15;
00121 } else if (width > 30) {
00122 width = 30;
00123 height = 30;
00124 }
00125 }
00126 }
00127
00128 QSize ActivityWidget::calculateSize() {
00129 int width, height;
00130 if ( m_ActivityType == Normal ) {
00131 const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00132 const int fontHeight = fm.lineSpacing();
00133 const int textWidth = fm.width(getName());
00134 height = fontHeight;
00135 width = textWidth > ACTIVITY_WIDTH ? textWidth : ACTIVITY_WIDTH;
00136 height = height > ACTIVITY_HEIGHT ? height : ACTIVITY_HEIGHT;
00137 width += ACTIVITY_MARGIN * 2;
00138 height += ACTIVITY_MARGIN * 2;
00139 } else {
00140 width = height = 20;
00141 }
00142 return QSize(width, height);
00143 }
00144
00145 ActivityWidget::ActivityType ActivityWidget::getActivityType() const {
00146 return m_ActivityType;
00147 }
00148
00149 void ActivityWidget::setActivityType( ActivityType activityType ) {
00150 m_ActivityType = activityType;
00151 UMLWidget::m_bResizable = true;
00152 }
00153
00154 void ActivityWidget::slotMenuSelection(int sel) {
00155 bool done = false;
00156
00157 bool ok = false;
00158 QString name = m_Text;
00159
00160 switch( sel ) {
00161 case ListPopupMenu::mt_Rename:
00162 name = KInputDialog::getText( i18n("Enter Activity Name"), i18n("Enter the name of the new activity:"), m_Text, &ok );
00163 if( ok && name.length() > 0 )
00164 m_Text = name;
00165 done = true;
00166 break;
00167
00168 case ListPopupMenu::mt_Properties:
00169 showProperties();
00170 done = true;
00171 break;
00172 }
00173
00174 if( !done )
00175 UMLWidget::slotMenuSelection( sel );
00176 }
00177
00178 void ActivityWidget::showProperties() {
00179 DocWindow *docwindow = UMLApp::app()->getDocWindow();
00180 docwindow->updateDocumentation(false);
00181
00182 ActivityDialog dialog(m_pView, this);
00183 if (dialog.exec() && dialog.getChangesMade()) {
00184 docwindow->showDocumentation(this, true);
00185 UMLApp::app()->getDocument()->setModified(true);
00186 }
00187 }
00188
00189 bool ActivityWidget::isActivity(WorkToolBar::ToolBar_Buttons tbb,
00190 ActivityType& resultType)
00191 {
00192 bool status = true;
00193 switch (tbb) {
00194 case WorkToolBar::tbb_Initial_Activity:
00195 resultType = Initial;
00196 break;
00197 case WorkToolBar::tbb_Activity:
00198 resultType = Normal;
00199 break;
00200 case WorkToolBar::tbb_End_Activity:
00201 resultType = End;
00202 break;
00203 case WorkToolBar::tbb_Branch:
00204 resultType = Branch;
00205 break;
00206 default:
00207 status = false;
00208 break;
00209 }
00210 return status;
00211 }
00212
00213 void ActivityWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement ) {
00214 QDomElement activityElement = qDoc.createElement( "activitywidget" );
00215 UMLWidget::saveToXMI( qDoc, activityElement );
00216 activityElement.setAttribute( "activityname", m_Text );
00217 activityElement.setAttribute( "documentation", m_Doc );
00218 activityElement.setAttribute( "activitytype", m_ActivityType );
00219 qElement.appendChild( activityElement );
00220 }
00221
00222 bool ActivityWidget::loadFromXMI( QDomElement & qElement ) {
00223 if( !UMLWidget::loadFromXMI( qElement ) )
00224 return false;
00225 m_Text = qElement.attribute( "activityname", "" );
00226 m_Doc = qElement.attribute( "documentation", "" );
00227 QString type = qElement.attribute( "activitytype", "1" );
00228 setActivityType( (ActivityType)type.toInt() );
00229 return true;
00230 }
00231
00232
00233 #include "activitywidget.moc"
00234