summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/dialogs/activitypage.cpp
blob: 7eac7f692943b279298e2be317aa69b8ed2c3e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2002-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

#include "activitypage.h"
#include "../statewidget.h"
#include "../listpopupmenu.h"
#include "../uml.h"

#include <kinputdialog.h>
#include <klocale.h>
#include <kbuttonbox.h>
#include <kdebug.h>
#include <tqlayout.h>
#include <tqstringlist.h>

ActivityPage::ActivityPage( TQWidget * pParent, StateWidget * pWidget ) : TQWidget( pParent ) {
    m_pStateWidget = pWidget;
    m_pMenu = 0;
    setupPage();
}

ActivityPage::~ActivityPage() {}

void ActivityPage::setupPage() {
    int margin = fontMetrics().height();
    
    TQVBoxLayout * mainLayout = new TQVBoxLayout( this );
    mainLayout -> setSpacing(10);

    m_pActivityGB = new TQGroupBox(i18n("Activities"), this );

    // vertical box layout for the activity lists, arrow buttons and the button box
    TQVBoxLayout* listVBoxLayout = new TQVBoxLayout( m_pActivityGB );
    listVBoxLayout -> setMargin(margin);
    listVBoxLayout -> setSpacing ( 10 );

    //horizontal box contains the list box and the move up/down buttons
    TQHBoxLayout* listHBoxLayout = new TQHBoxLayout( listVBoxLayout );
    
    m_pActivityLB = new TQListBox(m_pActivityGB );
   
    listHBoxLayout -> addWidget(m_pActivityLB);

    TQVBoxLayout * buttonLayout = new TQVBoxLayout( listHBoxLayout );

    m_pTopArrowB = new KArrowButton( m_pActivityGB );
    m_pTopArrowB -> setEnabled( false );
    buttonLayout -> addWidget( m_pTopArrowB );

    m_pUpArrowB = new KArrowButton( m_pActivityGB );
    m_pUpArrowB -> setEnabled( false );
    buttonLayout -> addWidget( m_pUpArrowB );

    m_pDownArrowB = new KArrowButton( m_pActivityGB, Qt::DownArrow );
    m_pDownArrowB -> setEnabled( false );
    buttonLayout -> addWidget( m_pDownArrowB );

    m_pBottomArrowB = new KArrowButton( m_pActivityGB, Qt::DownArrow );
    m_pBottomArrowB -> setEnabled( false );
    buttonLayout -> addWidget( m_pBottomArrowB );

   
    KButtonBox* buttonBox = new KButtonBox(m_pActivityGB);
    buttonBox->addButton( i18n("New Activity..."), TQT_TQOBJECT(this), TQT_SLOT(slotNewActivity()) );
    m_pDeleteActivityButton = buttonBox->addButton( i18n("Delete"),
                              TQT_TQOBJECT(this), TQT_SLOT(slotDelete()) );
    m_pRenameButton = buttonBox->addButton( i18n("Rename"), TQT_TQOBJECT(this), TQT_SLOT(slotRename()) );
    listVBoxLayout->addWidget(buttonBox);

    mainLayout -> addWidget( m_pActivityGB );

    //now fill activity list box
    TQStringList list = m_pStateWidget -> getActivityList();
    TQStringList::Iterator end(list.end());

    for( TQStringList::Iterator it(list.begin()); it != end; ++it ) {
        m_pActivityLB -> insertItem( *it );
    }

    //now setup the signals
    connect(m_pActivityLB, TQT_SIGNAL(clicked(TQListBoxItem *)), this, TQT_SLOT(slotClicked(TQListBoxItem *)));
    connect(m_pActivityLB, TQT_SIGNAL(rightButtonPressed(TQListBoxItem *, const TQPoint &)),
            this, TQT_SLOT(slotRightButtonPressed(TQListBoxItem *, const TQPoint &)));

    connect(m_pActivityLB, TQT_SIGNAL(rightButtonClicked(TQListBoxItem *, const TQPoint &)),
            this, TQT_SLOT(slotRightButtonClicked(TQListBoxItem *, const TQPoint &)));

    connect( m_pTopArrowB, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotTopClicked() ) );
    connect( m_pUpArrowB, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotUpClicked() ) );
    connect( m_pDownArrowB, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotDownClicked() ) );
    connect( m_pBottomArrowB, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotBottomClicked() ) );
     
    connect( m_pActivityLB, TQT_SIGNAL( doubleClicked( TQListBoxItem* ) ), this, TQT_SLOT( slotDoubleClicked( TQListBoxItem* ) ) );

    enableWidgets(false);
}

void ActivityPage::updateActivities() {
    TQStringList list;
    int count = m_pActivityLB -> count();
    for( int i = 0; i < count; i++ ) {
        list.append( m_pActivityLB -> text( i ) );
    }
    m_pStateWidget -> setActivities( list );
}

void ActivityPage::slotMenuSelection( int sel ) {
    switch( sel ) {
    case ListPopupMenu::mt_New_Activity:
        slotNewActivity();
        break;

    case ListPopupMenu::mt_Delete:
        slotDelete();
        break;

    case ListPopupMenu::mt_Rename:
        slotRename();
        break;
    }
}

void ActivityPage::slotNewActivity() {
    bool ok = false;
    TQString name = m_pActivityLB->currentText();
    name = KInputDialog::getText( i18n("New Activity"), i18n("Enter the name of the new activity:"),
                                  i18n("new activity"), &ok, UMLApp::app() );
    if( ok && name.length() > 0 ) {
        m_pActivityLB->insertItem( name );
        m_pStateWidget->addActivity( name );
    }
}

void ActivityPage::slotDelete() {
    TQString name = m_pActivityLB->currentText();
    m_pStateWidget->removeActivity(name);
    m_pActivityLB->removeItem( m_pActivityLB->currentItem() );
    slotClicked(0);
}

void ActivityPage::slotRename() {
    bool ok = false;
    TQString name = m_pActivityLB -> currentText();
    TQString oldName = name;
    name = KInputDialog::getText( i18n("Rename Activity"), i18n("Enter the new name of the activity:"), name, &ok, UMLApp::app() );
    if( ok && name.length() > 0 ) {
        m_pActivityLB -> changeItem( name, m_pActivityLB -> currentItem());
        m_pStateWidget -> renameActivity( oldName, name );
    }
}

void ActivityPage::slotRightButtonClicked(TQListBoxItem * /*item*/, const TQPoint &/* p*/) {
    if(m_pMenu) {
        m_pMenu->hide();
        disconnect(m_pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotMenuSelection(int)));
        delete m_pMenu;
        m_pMenu = 0;
    }
}

void ActivityPage::slotRightButtonPressed(TQListBoxItem * item, const TQPoint & p)
{
    ListPopupMenu::Menu_Type type = ListPopupMenu::mt_Undefined;
    if( item ) { //pressed on an item
        type = ListPopupMenu::mt_Activity_Selected;
    } else { //pressed into fresh air
        type = ListPopupMenu::mt_New_Activity;
    }

    if(m_pMenu) {
        m_pMenu -> hide();
        disconnect(m_pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotMenuSelection(int)));
        delete m_pMenu;
        m_pMenu = 0;
    }
    m_pMenu = new ListPopupMenu(this, type);
    m_pMenu->popup(p);
    connect(m_pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotMenuSelection(int)));
}


void ActivityPage::slotTopClicked() {
    int count = m_pActivityLB->count();
    int index = m_pActivityLB->currentItem();
    //shouldn't occur, but just in case
    if( count <= 1 || index <= 0 )
        return;

    //swap the text around in the ListBox
    TQString currentString = m_pActivityLB->text( index );
    m_pActivityLB->removeItem( index );
    m_pActivityLB->insertItem( currentString, 0 );
    //set the moved item selected
    TQListBoxItem* item = m_pActivityLB->item( 0 );
    m_pActivityLB->setSelected( item, true );

    slotClicked(item);
}

void ActivityPage::slotUpClicked() {
    int count = m_pActivityLB -> count();
    int index = m_pActivityLB -> currentItem();
    //shouldn't occur, but just in case
    if( count <= 1 || index <= 0 ) {
        return;
    }

    //swap the text around ( meaning attributes )
    TQString aboveString = m_pActivityLB -> text( index - 1 );
    TQString currentString = m_pActivityLB -> text( index );
    m_pActivityLB -> changeItem( currentString, index -1 );
    m_pActivityLB -> changeItem( aboveString, index );
    //set the moved atttribute selected
    TQListBoxItem * item = m_pActivityLB -> item( index - 1 );
    m_pActivityLB -> setSelected( item, true );
    slotClicked(item);
}

void ActivityPage::slotDownClicked() {
    int count = m_pActivityLB -> count();
    int index = m_pActivityLB -> currentItem();
    //shouldn't occur, but just in case
    if( count <= 1 || index >= count - 1 ) {
        return;
    }

    //swap the text around ( meaning attributes )
    TQString belowString = m_pActivityLB -> text( index + 1 );
    TQString currentString = m_pActivityLB -> text( index );
    m_pActivityLB -> changeItem( currentString, index + 1 );
    m_pActivityLB -> changeItem( belowString, index );
    //set the moved atttribute selected
    TQListBoxItem * item = m_pActivityLB -> item( index + 1 );
    m_pActivityLB -> setSelected( item, true );
    slotClicked(item);
}


void ActivityPage::slotBottomClicked() {
    int count = m_pActivityLB->count();
    int index = m_pActivityLB->currentItem();
    //shouldn't occur, but just in case
    if( count <= 1 || index >= count - 1 )
        return;
   
    //swap the text around in the ListBox
    TQString currentString = m_pActivityLB->text( index );
    m_pActivityLB->removeItem( index );
    m_pActivityLB->insertItem( currentString, m_pActivityLB->count() );
    //set the moved item selected
    TQListBoxItem* item = m_pActivityLB->item( m_pActivityLB->count() - 1 );
    m_pActivityLB->setSelected( item, true );

   slotClicked( item );
}


void ActivityPage::slotClicked(TQListBoxItem *item) {
    //make sure clicked on an item
    if(!item) {
        enableWidgets(false);
        m_pActivityLB -> clearSelection();
    } else {
        enableWidgets(true);
    }
}

void ActivityPage::slotDoubleClicked(TQListBoxItem* item) {
    if (item) {
        slotRename();
    }
}

void ActivityPage::enableWidgets(bool state) {
    if( !state ) {
        m_pTopArrowB->setEnabled( false );
        m_pUpArrowB->setEnabled( false );
        m_pDownArrowB->setEnabled( false );
        m_pBottomArrowB->setEnabled( false );
        m_pDeleteActivityButton->setEnabled(false);
        m_pRenameButton->setEnabled(false);
        return;
    }
    /*now check the order buttons.
        Double check an item is selected
       If only one att. in list make sure there disabled.
        If at top item,only allow down arrow to be enabled.
        If at bottom item. only allow up arrow to be enabled.
    */
    int index = m_pActivityLB->currentItem();
    if( m_pActivityLB->count() == 1 || index == -1 ) {
        m_pTopArrowB->setEnabled(false);
        m_pUpArrowB->setEnabled(false);
        m_pDownArrowB->setEnabled(false);
        m_pBottomArrowB->setEnabled( false );
    } else if( index == 0 ) {
        m_pTopArrowB->setEnabled( false );
        m_pUpArrowB->setEnabled(false);
        m_pDownArrowB->setEnabled(true);
        m_pBottomArrowB->setEnabled(true);
    } else if( index == (int)m_pActivityLB->count() - 1 ) {
        m_pTopArrowB->setEnabled(true);
        m_pUpArrowB->setEnabled(true);
        m_pDownArrowB->setEnabled(false);
        m_pBottomArrowB->setEnabled(false); 
   } else {
        m_pTopArrowB->setEnabled(true);
        m_pUpArrowB->setEnabled(true);
        m_pDownArrowB->setEnabled(true);
        m_pBottomArrowB->setEnabled(true);
    }
    m_pDeleteActivityButton->setEnabled(true);
    m_pRenameButton->setEnabled(true);
}


#include "activitypage.moc"