~ubuntu-branches/ubuntu/saucy/umbrello/saucy-proposed

« back to all changes in this revision

Viewing changes to umbrello/dialogs/activitypage.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:29:56 UTC
  • Revision ID: package-import@ubuntu.com-20130621022956-vfvg2xih76w1ydxn
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.                                   *
 
6
 *                                                                         *
 
7
 *   copyright (C) 2002-2011                                               *
 
8
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 
9
 ***************************************************************************/
 
10
 
 
11
#include "activitypage.h"
 
12
 
 
13
#include "debug_utils.h"
 
14
#include "listpopupmenu.h"
 
15
#include "statewidget.h"
 
16
#include "uml.h"
 
17
 
 
18
#include <kinputdialog.h>
 
19
#include <klocale.h>
 
20
#include <kdialogbuttonbox.h>
 
21
 
 
22
#include <QHBoxLayout>
 
23
#include <QGridLayout>
 
24
#include <QGroupBox>
 
25
#include <QLayout>
 
26
#include <QPushButton>
 
27
#include <QStringList>
 
28
#include <QToolButton>
 
29
#include <QVBoxLayout>
 
30
 
 
31
/**
 
32
 * Constructor.
 
33
 */
 
34
ActivityPage::ActivityPage( QWidget * pParent, StateWidget * pWidget ) : QWidget( pParent )
 
35
{
 
36
    m_pStateWidget = pWidget;
 
37
    m_pMenu = 0;
 
38
    setupPage();
 
39
}
 
40
 
 
41
/**
 
42
 * Destructor.
 
43
 */
 
44
ActivityPage::~ActivityPage()
 
45
{
 
46
}
 
47
 
 
48
/**
 
49
 * Sets up the page.
 
50
 */
 
51
void ActivityPage::setupPage()
 
52
{
 
53
    int margin = fontMetrics().height();
 
54
 
 
55
    QVBoxLayout * mainLayout = new QVBoxLayout( this );
 
56
    mainLayout->setSpacing(10);
 
57
 
 
58
    m_pActivityGB = new QGroupBox(i18n("Activities"), this );
 
59
 
 
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);
 
64
 
 
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 );
 
69
 
 
70
    m_pActivityLW = new QListWidget(m_pActivityGB );
 
71
    m_pActivityLW->setContextMenuPolicy(Qt::CustomContextMenu);
 
72
    listHBoxLayout->addWidget(m_pActivityLW);
 
73
 
 
74
    QVBoxLayout * buttonLayout = new QVBoxLayout();
 
75
    listHBoxLayout->addItem( buttonLayout );
 
76
 
 
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 );
 
82
 
 
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);
 
88
 
 
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);
 
94
 
 
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);
 
100
 
 
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);
 
109
 
 
110
    mainLayout->addWidget( m_pActivityGB );
 
111
 
 
112
    //now fill activity list box
 
113
    QStringList list = m_pStateWidget->activities();
 
114
    QStringList::ConstIterator end(list.end());
 
115
 
 
116
    for( QStringList::ConstIterator it(list.begin()); it != end; ++it ) {
 
117
        m_pActivityLW->addItem( *it );
 
118
    }
 
119
 
 
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)));
 
124
 
 
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()));
 
129
 
 
130
    connect(m_pActivityLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slotDoubleClicked(QListWidgetItem*)));
 
131
 
 
132
    enableWidgets(false);
 
133
}
 
134
 
 
135
/**
 
136
 * Sets the activities of the widget.
 
137
 */
 
138
void ActivityPage::updateActivities()
 
139
{
 
140
    QStringList list;
 
141
    int count = m_pActivityLW->count();
 
142
    for (int i = 0; i < count; ++i) {
 
143
        list.append( m_pActivityLW->item(i)->text() );
 
144
    }
 
145
    m_pStateWidget->setActivities( list );
 
146
}
 
147
 
 
148
/**
 
149
 * Popup menu item selected.
 
150
 */
 
151
void ActivityPage::slotMenuSelection(QAction* action)
 
152
{
 
153
    ListPopupMenu::MenuType sel = m_pMenu->getMenuType(action);
 
154
    switch(sel) {
 
155
    case ListPopupMenu::mt_New_Activity:
 
156
        slotNewActivity();
 
157
        break;
 
158
 
 
159
    case ListPopupMenu::mt_Delete:
 
160
        slotDelete();
 
161
        break;
 
162
 
 
163
    case ListPopupMenu::mt_Rename:
 
164
        slotRename();
 
165
        break;
 
166
 
 
167
    default:
 
168
        uDebug() << "MenuType " << ListPopupMenu::toString(sel) << " not implemented";
 
169
    }
 
170
}
 
171
 
 
172
void ActivityPage::slotNewActivity()
 
173
{
 
174
    bool ok = false;
 
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));
 
182
    }
 
183
}
 
184
 
 
185
void ActivityPage::slotDelete()
 
186
{
 
187
    QString name = m_pActivityLW->currentItem()->text();
 
188
    m_pStateWidget->removeActivity(name);
 
189
    m_pActivityLW->takeItem( m_pActivityLW->currentRow() );
 
190
    slotClicked(0);
 
191
}
 
192
 
 
193
void ActivityPage::slotRename()
 
194
{
 
195
    bool ok = false;
 
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();
 
201
        item->setText(name);
 
202
        m_pStateWidget->renameActivity( oldName, name );
 
203
        slotClicked(item);
 
204
    }
 
205
}
 
206
 
 
207
void ActivityPage::slotRightButtonPressed(const QPoint & p)
 
208
{
 
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;
 
215
    }
 
216
 
 
217
    if (m_pMenu) {
 
218
        m_pMenu->hide();
 
219
        disconnect(m_pMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotMenuSelection(QAction*)));
 
220
        delete m_pMenu;
 
221
        m_pMenu = 0;
 
222
    }
 
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));
 
226
}
 
227
 
 
228
void ActivityPage::slotTopClicked()
 
229
{
 
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 )
 
234
        return;
 
235
 
 
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);
 
241
 
 
242
    slotClicked(m_pActivityLW->currentItem());
 
243
}
 
244
 
 
245
void ActivityPage::slotUpClicked()
 
246
{
 
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 ) {
 
251
        return;
 
252
    }
 
253
 
 
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);
 
258
 
 
259
    slotClicked(m_pActivityLW->currentItem());
 
260
}
 
261
 
 
262
void ActivityPage::slotDownClicked()
 
263
{
 
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 ) {
 
268
        return;
 
269
    }
 
270
 
 
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);
 
275
 
 
276
    slotClicked(m_pActivityLW->currentItem());
 
277
}
 
278
 
 
279
void ActivityPage::slotBottomClicked()
 
280
{
 
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 )
 
285
        return;
 
286
 
 
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);
 
291
 
 
292
   slotClicked(m_pActivityLW->currentItem());
 
293
}
 
294
 
 
295
void ActivityPage::slotClicked(QListWidgetItem *item)
 
296
{
 
297
    //make sure clicked on an item
 
298
    if (!item) {
 
299
        enableWidgets(false);
 
300
        m_pActivityLW->clearSelection();
 
301
    } else {
 
302
        enableWidgets(true);
 
303
    }
 
304
}
 
305
 
 
306
void ActivityPage::slotDoubleClicked(QListWidgetItem* item)
 
307
{
 
308
    if (item) {
 
309
        slotRename();
 
310
    }
 
311
}
 
312
 
 
313
/**
 
314
 * Set the state of the widgets on the page with the given value.
 
315
 * @param state The state to set the widgets as.
 
316
 */
 
317
void ActivityPage::enableWidgets(bool state)
 
318
{
 
319
    if ( !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);
 
326
        return;
 
327
    }
 
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.
 
333
    */
 
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);
 
350
   } else {
 
351
        m_pTopArrowB->setEnabled(true);
 
352
        m_pUpArrowB->setEnabled(true);
 
353
        m_pDownArrowB->setEnabled(true);
 
354
        m_pBottomArrowB->setEnabled(true);
 
355
    }
 
356
    m_pDeleteActivityButton->setEnabled(true);
 
357
    m_pRenameButton->setEnabled(true);
 
358
}
 
359
 
 
360
#include "activitypage.moc"