00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "activitypage.h"
00013 #include "../statewidget.h"
00014 #include "../listpopupmenu.h"
00015 #include "../uml.h"
00016
00017 #include <kinputdialog.h>
00018 #include <klocale.h>
00019 #include <kbuttonbox.h>
00020 #include <kdebug.h>
00021 #include <qlayout.h>
00022 #include <qstringlist.h>
00023
00024 ActivityPage::ActivityPage( QWidget * pParent, StateWidget * pWidget ) : QWidget( pParent ) {
00025 m_pStateWidget = pWidget;
00026 m_pMenu = 0;
00027 setupPage();
00028 }
00029
00030 ActivityPage::~ActivityPage() {}
00031
00032 void ActivityPage::setupPage() {
00033 int margin = fontMetrics().height();
00034
00035 QVBoxLayout * mainLayout = new QVBoxLayout( this );
00036 mainLayout -> setSpacing(10);
00037
00038 m_pActivityGB = new QGroupBox(i18n("Activities"), this );
00039
00040
00041 QVBoxLayout* listVBoxLayout = new QVBoxLayout( m_pActivityGB );
00042 listVBoxLayout -> setMargin(margin);
00043 listVBoxLayout -> setSpacing ( 10 );
00044
00045
00046 QHBoxLayout* listHBoxLayout = new QHBoxLayout( listVBoxLayout );
00047
00048 m_pActivityLB = new QListBox(m_pActivityGB );
00049
00050 listHBoxLayout -> addWidget(m_pActivityLB);
00051
00052 QVBoxLayout * buttonLayout = new QVBoxLayout( listHBoxLayout );
00053
00054 m_pTopArrowB = new KArrowButton( m_pActivityGB );
00055 m_pTopArrowB -> setEnabled( false );
00056 buttonLayout -> addWidget( m_pTopArrowB );
00057
00058 m_pUpArrowB = new KArrowButton( m_pActivityGB );
00059 m_pUpArrowB -> setEnabled( false );
00060 buttonLayout -> addWidget( m_pUpArrowB );
00061
00062 m_pDownArrowB = new KArrowButton( m_pActivityGB, Qt::DownArrow );
00063 m_pDownArrowB -> setEnabled( false );
00064 buttonLayout -> addWidget( m_pDownArrowB );
00065
00066 m_pBottomArrowB = new KArrowButton( m_pActivityGB, Qt::DownArrow );
00067 m_pBottomArrowB -> setEnabled( false );
00068 buttonLayout -> addWidget( m_pBottomArrowB );
00069
00070
00071 KButtonBox* buttonBox = new KButtonBox(m_pActivityGB);
00072 buttonBox->addButton( i18n("New Activity..."), this, SLOT(slotNewActivity()) );
00073 m_pDeleteActivityButton = buttonBox->addButton( i18n("Delete"),
00074 this, SLOT(slotDelete()) );
00075 m_pRenameButton = buttonBox->addButton( i18n("Rename"), this, SLOT(slotRename()) );
00076 listVBoxLayout->addWidget(buttonBox);
00077
00078 mainLayout -> addWidget( m_pActivityGB );
00079
00080
00081 QStringList list = m_pStateWidget -> getActivityList();
00082 QStringList::Iterator end(list.end());
00083
00084 for( QStringList::Iterator it(list.begin()); it != end; ++it ) {
00085 m_pActivityLB -> insertItem( *it );
00086 }
00087
00088
00089 connect(m_pActivityLB, SIGNAL(clicked(QListBoxItem *)), this, SLOT(slotClicked(QListBoxItem *)));
00090 connect(m_pActivityLB, SIGNAL(rightButtonPressed(QListBoxItem *, const QPoint &)),
00091 this, SLOT(slotRightButtonPressed(QListBoxItem *, const QPoint &)));
00092
00093 connect(m_pActivityLB, SIGNAL(rightButtonClicked(QListBoxItem *, const QPoint &)),
00094 this, SLOT(slotRightButtonClicked(QListBoxItem *, const QPoint &)));
00095
00096 connect( m_pTopArrowB, SIGNAL( clicked() ), this, SLOT( slotTopClicked() ) );
00097 connect( m_pUpArrowB, SIGNAL( clicked() ), this, SLOT( slotUpClicked() ) );
00098 connect( m_pDownArrowB, SIGNAL( clicked() ), this, SLOT( slotDownClicked() ) );
00099 connect( m_pBottomArrowB, SIGNAL( clicked() ), this, SLOT( slotBottomClicked() ) );
00100
00101 connect( m_pActivityLB, SIGNAL( doubleClicked( QListBoxItem* ) ), this, SLOT( slotDoubleClicked( QListBoxItem* ) ) );
00102
00103 enableWidgets(false);
00104 }
00105
00106 void ActivityPage::updateActivities() {
00107 QStringList list;
00108 int count = m_pActivityLB -> count();
00109 for( int i = 0; i < count; i++ ) {
00110 list.append( m_pActivityLB -> text( i ) );
00111 }
00112 m_pStateWidget -> setActivities( list );
00113 }
00114
00115 void ActivityPage::slotMenuSelection( int sel ) {
00116 switch( sel ) {
00117 case ListPopupMenu::mt_New_Activity:
00118 slotNewActivity();
00119 break;
00120
00121 case ListPopupMenu::mt_Delete:
00122 slotDelete();
00123 break;
00124
00125 case ListPopupMenu::mt_Rename:
00126 slotRename();
00127 break;
00128 }
00129 }
00130
00131 void ActivityPage::slotNewActivity() {
00132 bool ok = false;
00133 QString name = m_pActivityLB->currentText();
00134 name = KInputDialog::getText( i18n("New Activity"), i18n("Enter the name of the new activity:"),
00135 i18n("new activity"), &ok, UMLApp::app() );
00136 if( ok && name.length() > 0 ) {
00137 m_pActivityLB->insertItem( name );
00138 m_pStateWidget->addActivity( name );
00139 }
00140 }
00141
00142 void ActivityPage::slotDelete() {
00143 QString name = m_pActivityLB->currentText();
00144 m_pStateWidget->removeActivity(name);
00145 m_pActivityLB->removeItem( m_pActivityLB->currentItem() );
00146 slotClicked(0);
00147 }
00148
00149 void ActivityPage::slotRename() {
00150 bool ok = false;
00151 QString name = m_pActivityLB -> currentText();
00152 QString oldName = name;
00153 name = KInputDialog::getText( i18n("Rename Activity"), i18n("Enter the new name of the activity:"), name, &ok, UMLApp::app() );
00154 if( ok && name.length() > 0 ) {
00155 m_pActivityLB -> changeItem( name, m_pActivityLB -> currentItem());
00156 m_pStateWidget -> renameActivity( oldName, name );
00157 }
00158 }
00159
00160 void ActivityPage::slotRightButtonClicked(QListBoxItem * , const QPoint &) {
00161 if(m_pMenu) {
00162 m_pMenu->hide();
00163 disconnect(m_pMenu, SIGNAL(activated(int)), this, SLOT(slotMenuSelection(int)));
00164 delete m_pMenu;
00165 m_pMenu = 0;
00166 }
00167 }
00168
00169 void ActivityPage::slotRightButtonPressed(QListBoxItem * item, const QPoint & p)
00170 {
00171 ListPopupMenu::Menu_Type type = ListPopupMenu::mt_Undefined;
00172 if( item ) {
00173 type = ListPopupMenu::mt_Activity_Selected;
00174 } else {
00175 type = ListPopupMenu::mt_New_Activity;
00176 }
00177
00178 if(m_pMenu) {
00179 m_pMenu -> hide();
00180 disconnect(m_pMenu, SIGNAL(activated(int)), this, SLOT(slotMenuSelection(int)));
00181 delete m_pMenu;
00182 m_pMenu = 0;
00183 }
00184 m_pMenu = new ListPopupMenu(this, type);
00185 m_pMenu->popup(p);
00186 connect(m_pMenu, SIGNAL(activated(int)), this, SLOT(slotMenuSelection(int)));
00187 }
00188
00189
00190 void ActivityPage::slotTopClicked() {
00191 int count = m_pActivityLB->count();
00192 int index = m_pActivityLB->currentItem();
00193
00194 if( count <= 1 || index <= 0 )
00195 return;
00196
00197
00198 QString currentString = m_pActivityLB->text( index );
00199 m_pActivityLB->removeItem( index );
00200 m_pActivityLB->insertItem( currentString, 0 );
00201
00202 QListBoxItem* item = m_pActivityLB->item( 0 );
00203 m_pActivityLB->setSelected( item, true );
00204
00205 slotClicked(item);
00206 }
00207
00208 void ActivityPage::slotUpClicked() {
00209 int count = m_pActivityLB -> count();
00210 int index = m_pActivityLB -> currentItem();
00211
00212 if( count <= 1 || index <= 0 ) {
00213 return;
00214 }
00215
00216
00217 QString aboveString = m_pActivityLB -> text( index - 1 );
00218 QString currentString = m_pActivityLB -> text( index );
00219 m_pActivityLB -> changeItem( currentString, index -1 );
00220 m_pActivityLB -> changeItem( aboveString, index );
00221
00222 QListBoxItem * item = m_pActivityLB -> item( index - 1 );
00223 m_pActivityLB -> setSelected( item, true );
00224 slotClicked(item);
00225 }
00226
00227 void ActivityPage::slotDownClicked() {
00228 int count = m_pActivityLB -> count();
00229 int index = m_pActivityLB -> currentItem();
00230
00231 if( count <= 1 || index >= count - 1 ) {
00232 return;
00233 }
00234
00235
00236 QString belowString = m_pActivityLB -> text( index + 1 );
00237 QString currentString = m_pActivityLB -> text( index );
00238 m_pActivityLB -> changeItem( currentString, index + 1 );
00239 m_pActivityLB -> changeItem( belowString, index );
00240
00241 QListBoxItem * item = m_pActivityLB -> item( index + 1 );
00242 m_pActivityLB -> setSelected( item, true );
00243 slotClicked(item);
00244 }
00245
00246
00247 void ActivityPage::slotBottomClicked() {
00248 int count = m_pActivityLB->count();
00249 int index = m_pActivityLB->currentItem();
00250
00251 if( count <= 1 || index >= count - 1 )
00252 return;
00253
00254
00255 QString currentString = m_pActivityLB->text( index );
00256 m_pActivityLB->removeItem( index );
00257 m_pActivityLB->insertItem( currentString, m_pActivityLB->count() );
00258
00259 QListBoxItem* item = m_pActivityLB->item( m_pActivityLB->count() - 1 );
00260 m_pActivityLB->setSelected( item, true );
00261
00262 slotClicked( item );
00263 }
00264
00265
00266 void ActivityPage::slotClicked(QListBoxItem *item) {
00267
00268 if(!item) {
00269 enableWidgets(false);
00270 m_pActivityLB -> clearSelection();
00271 } else {
00272 enableWidgets(true);
00273 }
00274 }
00275
00276 void ActivityPage::slotDoubleClicked(QListBoxItem* item) {
00277 if (item) {
00278 slotRename();
00279 }
00280 }
00281
00282 void ActivityPage::enableWidgets(bool state) {
00283 if( !state ) {
00284 m_pTopArrowB->setEnabled( false );
00285 m_pUpArrowB->setEnabled( false );
00286 m_pDownArrowB->setEnabled( false );
00287 m_pBottomArrowB->setEnabled( false );
00288 m_pDeleteActivityButton->setEnabled(false);
00289 m_pRenameButton->setEnabled(false);
00290 return;
00291 }
00292
00293
00294
00295
00296
00297
00298 int index = m_pActivityLB->currentItem();
00299 if( m_pActivityLB->count() == 1 || index == -1 ) {
00300 m_pTopArrowB->setEnabled(false);
00301 m_pUpArrowB->setEnabled(false);
00302 m_pDownArrowB->setEnabled(false);
00303 m_pBottomArrowB->setEnabled( false );
00304 } else if( index == 0 ) {
00305 m_pTopArrowB->setEnabled( false );
00306 m_pUpArrowB->setEnabled(false);
00307 m_pDownArrowB->setEnabled(true);
00308 m_pBottomArrowB->setEnabled(true);
00309 } else if( index == (int)m_pActivityLB->count() - 1 ) {
00310 m_pTopArrowB->setEnabled(true);
00311 m_pUpArrowB->setEnabled(true);
00312 m_pDownArrowB->setEnabled(false);
00313 m_pBottomArrowB->setEnabled(false);
00314 } else {
00315 m_pTopArrowB->setEnabled(true);
00316 m_pUpArrowB->setEnabled(true);
00317 m_pDownArrowB->setEnabled(true);
00318 m_pBottomArrowB->setEnabled(true);
00319 }
00320 m_pDeleteActivityButton->setEnabled(true);
00321 m_pRenameButton->setEnabled(true);
00322 }
00323
00324
00325 #include "activitypage.moc"