1
/***************************************************************************
2
* This program is free software; you can redistribute it and/or modify *
3
* it under the terms of the GNU General Public License as published by *
4
* the Free Software Foundation; either version 2 of the License, or *
5
* (at your option) any later version. *
7
* copyright (C) 2002-2011 *
8
* Umbrello UML Modeller Authors <uml-devel@uml.sf.net> *
9
***************************************************************************/
11
#include "activitypage.h"
13
#include "debug_utils.h"
14
#include "listpopupmenu.h"
15
#include "statewidget.h"
18
#include <kinputdialog.h>
20
#include <kdialogbuttonbox.h>
22
#include <QHBoxLayout>
23
#include <QGridLayout>
26
#include <QPushButton>
27
#include <QStringList>
28
#include <QToolButton>
29
#include <QVBoxLayout>
34
ActivityPage::ActivityPage( QWidget * pParent, StateWidget * pWidget ) : QWidget( pParent )
36
m_pStateWidget = pWidget;
44
ActivityPage::~ActivityPage()
51
void ActivityPage::setupPage()
53
int margin = fontMetrics().height();
55
QVBoxLayout * mainLayout = new QVBoxLayout( this );
56
mainLayout->setSpacing(10);
58
m_pActivityGB = new QGroupBox(i18n("Activities"), this );
60
// vertical box layout for the activity lists, arrow buttons and the button box
61
QVBoxLayout* listVBoxLayout = new QVBoxLayout( m_pActivityGB );
62
listVBoxLayout->setMargin(margin);
63
listVBoxLayout->setSpacing(10);
65
//horizontal box contains the list box and the move up/down buttons
66
QHBoxLayout* listHBoxLayout = new QHBoxLayout();
67
listHBoxLayout->setSpacing(10);
68
listVBoxLayout->addItem( listHBoxLayout );
70
m_pActivityLW = new QListWidget(m_pActivityGB );
71
m_pActivityLW->setContextMenuPolicy(Qt::CustomContextMenu);
72
listHBoxLayout->addWidget(m_pActivityLW);
74
QVBoxLayout * buttonLayout = new QVBoxLayout();
75
listHBoxLayout->addItem( buttonLayout );
77
m_pTopArrowB = new QToolButton( m_pActivityGB );
78
m_pTopArrowB->setArrowType(Qt::UpArrow);
79
m_pTopArrowB->setEnabled( false );
80
m_pTopArrowB->setToolTip(i18n("Move selected item to the top"));
81
buttonLayout->addWidget( m_pTopArrowB );
83
m_pUpArrowB = new QToolButton(m_pActivityGB);
84
m_pUpArrowB->setArrowType(Qt::UpArrow);
85
m_pUpArrowB->setEnabled(false);
86
m_pUpArrowB->setToolTip(i18n("Move selected item up"));
87
buttonLayout->addWidget(m_pUpArrowB);
89
m_pDownArrowB = new QToolButton(m_pActivityGB);
90
m_pDownArrowB->setArrowType(Qt::DownArrow);
91
m_pDownArrowB->setEnabled(false);
92
m_pDownArrowB->setToolTip(i18n("Move selected item down"));
93
buttonLayout->addWidget(m_pDownArrowB);
95
m_pBottomArrowB = new QToolButton(m_pActivityGB);
96
m_pBottomArrowB->setArrowType(Qt::DownArrow);
97
m_pBottomArrowB->setEnabled(false);
98
m_pBottomArrowB->setToolTip(i18n("Move selected item to the bottom"));
99
buttonLayout->addWidget(m_pBottomArrowB);
101
KDialogButtonBox* buttonBox = new KDialogButtonBox(m_pActivityGB);
102
buttonBox->addButton( i18n("New Activity..."), KDialogButtonBox::ActionRole,
103
this, SLOT(slotNewActivity()) );
104
m_pDeleteActivityButton = buttonBox->addButton( i18n("Delete"), KDialogButtonBox::ActionRole,
105
this, SLOT(slotDelete()) );
106
m_pRenameButton = buttonBox->addButton( i18n("Rename"), KDialogButtonBox::ActionRole,
107
this, SLOT(slotRename()) );
108
listVBoxLayout->addWidget(buttonBox);
110
mainLayout->addWidget( m_pActivityGB );
112
//now fill activity list box
113
QStringList list = m_pStateWidget->activities();
114
QStringList::ConstIterator end(list.end());
116
for( QStringList::ConstIterator it(list.begin()); it != end; ++it ) {
117
m_pActivityLW->addItem( *it );
120
//now setup the signals
121
connect(m_pActivityLW, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotClicked(QListWidgetItem*)));
122
connect(m_pActivityLW, SIGNAL(customContextMenuRequested(QPoint)),
123
this, SLOT(slotRightButtonPressed(QPoint)));
125
connect(m_pTopArrowB, SIGNAL(clicked()), this, SLOT(slotTopClicked()));
126
connect(m_pUpArrowB, SIGNAL(clicked()), this, SLOT(slotUpClicked()));
127
connect(m_pDownArrowB, SIGNAL(clicked()), this, SLOT(slotDownClicked()));
128
connect(m_pBottomArrowB, SIGNAL(clicked()), this, SLOT(slotBottomClicked()));
130
connect(m_pActivityLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slotDoubleClicked(QListWidgetItem*)));
132
enableWidgets(false);
136
* Sets the activities of the widget.
138
void ActivityPage::updateActivities()
141
int count = m_pActivityLW->count();
142
for (int i = 0; i < count; ++i) {
143
list.append( m_pActivityLW->item(i)->text() );
145
m_pStateWidget->setActivities( list );
149
* Popup menu item selected.
151
void ActivityPage::slotMenuSelection(QAction* action)
153
ListPopupMenu::MenuType sel = m_pMenu->getMenuType(action);
155
case ListPopupMenu::mt_New_Activity:
159
case ListPopupMenu::mt_Delete:
163
case ListPopupMenu::mt_Rename:
168
uDebug() << "MenuType " << ListPopupMenu::toString(sel) << " not implemented";
172
void ActivityPage::slotNewActivity()
175
QString name = KInputDialog::getText( i18n("New Activity"),
176
i18n("Enter the name of the new activity:"), i18n("new activity"), &ok, UMLApp::app() );
177
if ( ok && name.length() > 0 ) {
178
m_pActivityLW->addItem(name);
179
m_pActivityLW->setCurrentRow(m_pActivityLW->count() - 1);
180
m_pStateWidget->addActivity(name);
181
slotClicked(m_pActivityLW->item(m_pActivityLW->count() - 1));
185
void ActivityPage::slotDelete()
187
QString name = m_pActivityLW->currentItem()->text();
188
m_pStateWidget->removeActivity(name);
189
m_pActivityLW->takeItem( m_pActivityLW->currentRow() );
193
void ActivityPage::slotRename()
196
QString name = m_pActivityLW->currentItem()->text();
197
QString oldName = name;
198
name = KInputDialog::getText( i18n("Rename Activity"), i18n("Enter the new name of the activity:"), name, &ok, UMLApp::app() );
199
if ( ok && name.length() > 0 ) {
200
QListWidgetItem* item = m_pActivityLW->currentItem();
202
m_pStateWidget->renameActivity( oldName, name );
207
void ActivityPage::slotRightButtonPressed(const QPoint & p)
209
ListPopupMenu::MenuType type = ListPopupMenu::mt_Undefined;
210
QListWidgetItem* item = m_pActivityLW->itemAt(p);
211
if ( item ) { //pressed on an item
212
type = ListPopupMenu::mt_Activity_Selected;
213
} else { //pressed into fresh air
214
type = ListPopupMenu::mt_New_Activity;
219
disconnect(m_pMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotMenuSelection(QAction*)));
223
m_pMenu = new ListPopupMenu(this, type);
224
connect(m_pMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotMenuSelection(QAction*)));
225
m_pMenu->exec(m_pActivityLW->mapToGlobal(p));
228
void ActivityPage::slotTopClicked()
230
int count = m_pActivityLW->count();
231
int index = m_pActivityLW->currentRow();
232
//shouldn't occur, but just in case
233
if ( count <= 1 || index <= 0 )
236
//swap the text around in the ListBox
237
QListWidgetItem* item = m_pActivityLW->takeItem(index);
238
m_pActivityLW->insertItem(0, item);
239
//set the moved item selected
240
m_pActivityLW->setCurrentRow(0);
242
slotClicked(m_pActivityLW->currentItem());
245
void ActivityPage::slotUpClicked()
247
int count = m_pActivityLW->count();
248
int index = m_pActivityLW->currentRow();
249
//shouldn't occur, but just in case
250
if ( count <= 1 || index <= 0 ) {
254
QListWidgetItem* item = m_pActivityLW->takeItem(index);
255
m_pActivityLW->insertItem(index - 1, item);
256
//set the moved atttribute selected
257
m_pActivityLW->setCurrentRow(index - 1);
259
slotClicked(m_pActivityLW->currentItem());
262
void ActivityPage::slotDownClicked()
264
int count = m_pActivityLW->count();
265
int index = m_pActivityLW->currentRow();
266
//shouldn't occur, but just in case
267
if ( count <= 1 || index >= count - 1 ) {
271
QListWidgetItem* item = m_pActivityLW->takeItem(index);
272
m_pActivityLW->insertItem(index + 1, item);
273
//set the moved atttribute selected
274
m_pActivityLW->setCurrentRow(index + 1);
276
slotClicked(m_pActivityLW->currentItem());
279
void ActivityPage::slotBottomClicked()
281
int count = m_pActivityLW->count();
282
int index = m_pActivityLW->currentRow();
283
//shouldn't occur, but just in case
284
if ( count <= 1 || index >= count - 1 )
287
QListWidgetItem* item = m_pActivityLW->takeItem(index);
288
m_pActivityLW->insertItem(m_pActivityLW->count(), item );
289
//set the moved item selected
290
m_pActivityLW->setCurrentRow(m_pActivityLW->count() - 1);
292
slotClicked(m_pActivityLW->currentItem());
295
void ActivityPage::slotClicked(QListWidgetItem *item)
297
//make sure clicked on an item
299
enableWidgets(false);
300
m_pActivityLW->clearSelection();
306
void ActivityPage::slotDoubleClicked(QListWidgetItem* item)
314
* Set the state of the widgets on the page with the given value.
315
* @param state The state to set the widgets as.
317
void ActivityPage::enableWidgets(bool state)
320
m_pTopArrowB->setEnabled( false );
321
m_pUpArrowB->setEnabled( false );
322
m_pDownArrowB->setEnabled( false );
323
m_pBottomArrowB->setEnabled( false );
324
m_pDeleteActivityButton->setEnabled(false);
325
m_pRenameButton->setEnabled(false);
328
/*now check the order buttons.
329
Double check an item is selected
330
If only one att. in list make sure there disabled.
331
If at top item,only allow down arrow to be enabled.
332
If at bottom item. only allow up arrow to be enabled.
334
int index = m_pActivityLW->currentRow();
335
if ( m_pActivityLW->count() == 1 || index == -1 ) {
336
m_pTopArrowB->setEnabled(false);
337
m_pUpArrowB->setEnabled(false);
338
m_pDownArrowB->setEnabled(false);
339
m_pBottomArrowB->setEnabled( false );
340
} else if ( index == 0 ) {
341
m_pTopArrowB->setEnabled( false );
342
m_pUpArrowB->setEnabled(false);
343
m_pDownArrowB->setEnabled(true);
344
m_pBottomArrowB->setEnabled(true);
345
} else if( index == (int)m_pActivityLW->count() - 1 ) {
346
m_pTopArrowB->setEnabled(true);
347
m_pUpArrowB->setEnabled(true);
348
m_pDownArrowB->setEnabled(false);
349
m_pBottomArrowB->setEnabled(false);
351
m_pTopArrowB->setEnabled(true);
352
m_pUpArrowB->setEnabled(true);
353
m_pDownArrowB->setEnabled(true);
354
m_pBottomArrowB->setEnabled(true);
356
m_pDeleteActivityButton->setEnabled(true);
357
m_pRenameButton->setEnabled(true);
360
#include "activitypage.moc"