~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/kcm_hotkeys/conditions/conditions_widget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2009 Michael Jansen <kde@michael-jansen.biz>
 
2
 
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
 
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU Library General Public License
 
14
   along with this library; see the file COPYING.LIB.  If not, write to
 
15
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
16
   Boston, MA 02110-1301, USA.
 
17
*/
 
18
 
 
19
#include "conditions/conditions_widget.h"
 
20
#include "conditions/conditions_list_base.h"
 
21
#include "conditions/conditions_list.h"
 
22
#include "conditions/condition.h"
 
23
#include "conditions/conditions.h"
 
24
#include "conditions/conditions_visitor.h"
 
25
#include "conditions/existing_window_condition.h"
 
26
#include "conditions/active_window_condition.h"
 
27
 
 
28
#include "windows_helper/window_selection_list.h"
 
29
 
 
30
#include "helper_widgets/window_definition_list_widget.h"
 
31
 
 
32
#include "condition_type_menu.h"
 
33
 
 
34
#include <QtCore/QStack>
 
35
 
 
36
#include <KDE/KDebug>
 
37
 
 
38
class BuildTree : public KHotKeys::ConditionsVisitor
 
39
    {
 
40
public:
 
41
 
 
42
    BuildTree(QTreeWidget *tree);
 
43
 
 
44
    void build();
 
45
 
 
46
    virtual void visitConditionsListBase(KHotKeys::Condition_list_base *list);
 
47
    virtual void visitConditionsList(KHotKeys::Condition_list *list);
 
48
    virtual void visitCondition( KHotKeys::Condition *condition );
 
49
 
 
50
    QMap<QTreeWidgetItem*, KHotKeys::Condition*> _items;
 
51
private:
 
52
 
 
53
    QTreeWidget *_tree;
 
54
    QStack<QTreeWidgetItem*> _stack;
 
55
    };
 
56
 
 
57
 
 
58
BuildTree::BuildTree( QTreeWidget *tree )
 
59
    :   _tree(tree)
 
60
    {
 
61
    _stack.push(_tree->invisibleRootItem());
 
62
    }
 
63
 
 
64
 
 
65
void BuildTree::visitCondition(KHotKeys::Condition *cond)
 
66
    {
 
67
    QTreeWidgetItem *item = new QTreeWidgetItem(_stack.top());
 
68
    item->setText(0, cond->description());
 
69
    _items.insert(item, cond);
 
70
    }
 
71
 
 
72
 
 
73
void BuildTree::visitConditionsList(KHotKeys::Condition_list *list)
 
74
    {
 
75
    Q_ASSERT(_stack.count()==1);
 
76
 
 
77
    QTreeWidgetItem *parent = _stack.top();
 
78
 
 
79
    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
 
80
    item->setText(0, i18nc("Add a new condition", "And"));
 
81
    _items.insert(item, list);
 
82
    _stack.push(item);
 
83
 
 
84
    for(KHotKeys::Condition_list_base::Iterator it = list->begin();
 
85
            it != list->end();
 
86
            ++it)
 
87
        {
 
88
        (*it)->visit(this);
 
89
        }
 
90
 
 
91
    _tree->expandAll();
 
92
    }
 
93
 
 
94
 
 
95
void BuildTree::visitConditionsListBase(KHotKeys::Condition_list_base *list)
 
96
    {
 
97
    QTreeWidgetItem *parent = _stack.top();
 
98
 
 
99
    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
 
100
    item->setText(0, list->description());
 
101
    _items.insert(item, list);
 
102
    _stack.push(item);
 
103
 
 
104
    for(KHotKeys::Condition_list_base::Iterator it = list->begin();
 
105
            it != list->end();
 
106
            ++it)
 
107
        {
 
108
        // QTreeWidgetItem *child = new QTreeWidgetItem(item);
 
109
        // child->setText(0, (*it)->description());
 
110
        // _items.insert(child, list);
 
111
        (*it)->visit(this);
 
112
        }
 
113
 
 
114
    _stack.pop();
 
115
    }
 
116
 
 
117
 
 
118
ConditionsWidget::ConditionsWidget(QWidget *parent)
 
119
    :   QWidget(parent)
 
120
        ,_working(NULL)
 
121
        ,_changed(false)
 
122
    {
 
123
    ui.setupUi(this);
 
124
 
 
125
    connect(ui.edit_button, SIGNAL(clicked(bool)),
 
126
            SLOT(slotEdit()));
 
127
 
 
128
    connect(ui.delete_button, SIGNAL(clicked(bool)),
 
129
            SLOT(slotDelete()));
 
130
 
 
131
    ui.new_button->setMenu(new ConditionTypeMenu(this));
 
132
    connect(ui.new_button->menu(), SIGNAL(triggered(QAction*)),
 
133
            SLOT(slotNew(QAction*)));
 
134
    }
 
135
 
 
136
 
 
137
ConditionsWidget::~ConditionsWidget()
 
138
    {
 
139
    delete _working; _working = NULL;
 
140
    }
 
141
 
 
142
 
 
143
void ConditionsWidget::copyFromObject()
 
144
    {
 
145
    Q_ASSERT(_conditions_list);
 
146
 
 
147
    // Clear the tree and delete our old working copy
 
148
    ui.tree->clear();
 
149
    if (_working) delete _working;
 
150
 
 
151
    // Create the working copy
 
152
    _working = _conditions_list->copy();
 
153
    Q_ASSERT(_working->count() == _conditions_list->count());
 
154
 
 
155
    kDebug() << _working->count();
 
156
    kDebug() << _conditions_list->count();
 
157
 
 
158
    // Build the tree
 
159
    BuildTree builder(ui.tree);
 
160
    _working->visit(&builder);
 
161
    _items = builder._items;
 
162
 
 
163
    _changed = false;
 
164
    }
 
165
 
 
166
 
 
167
void ConditionsWidget::copyToObject()
 
168
    {
 
169
    kDebug();
 
170
 
 
171
    Q_ASSERT(_conditions_list);
 
172
    if (!_conditions_list) return;
 
173
 
 
174
    // Just copy the content from our working copy to the original
 
175
 
 
176
    // Remove the old content
 
177
    qDeleteAll(*_conditions_list);
 
178
    _conditions_list->clear();
 
179
 
 
180
    for (KHotKeys::Condition_list::Iterator it = _working->begin();
 
181
            it != _working->end();
 
182
            ++it)
 
183
        {
 
184
        kDebug();
 
185
        _conditions_list->append((*it)->copy());
 
186
        }
 
187
 
 
188
    Q_ASSERT(_working->count() == _conditions_list->count());
 
189
    _changed = false;
 
190
    }
 
191
 
 
192
 
 
193
void ConditionsWidget::emitChanged(bool chgd)
 
194
    {
 
195
    if (_changed == ( chgd || _changed ))
 
196
        return;
 
197
 
 
198
    // Once changed always changed
 
199
    _changed = chgd || _changed;
 
200
 
 
201
    emit changed(_changed);
 
202
    }
 
203
 
 
204
 
 
205
bool ConditionsWidget::hasChanges() const
 
206
    {
 
207
    return _changed;
 
208
    }
 
209
 
 
210
 
 
211
void ConditionsWidget::setConditionsList( KHotKeys::Condition_list *list)
 
212
    {
 
213
    Q_ASSERT(list);
 
214
    _conditions_list = list;
 
215
    }
 
216
 
 
217
 
 
218
void ConditionsWidget::slotDelete()
 
219
    {
 
220
    QTreeWidgetItem *citem = ui.tree->currentItem();
 
221
 
 
222
    // If no item is selected just return
 
223
    if (!citem) return;
 
224
 
 
225
    // TODO: Ask for confirmation before deleting
 
226
 
 
227
    // Get the currently select condition
 
228
    KHotKeys::Condition *cond = _items.value(citem);
 
229
 
 
230
    // Do not allow deleting the root item
 
231
    if (cond==_working) return;
 
232
 
 
233
    delete cond;
 
234
    delete citem;
 
235
    emitChanged(true);
 
236
    }
 
237
 
 
238
 
 
239
void ConditionsWidget::slotEdit()
 
240
    {
 
241
    // Get the currently select condition
 
242
    QTreeWidgetItem *citem = ui.tree->currentItem();
 
243
 
 
244
    // If no item is selected just return
 
245
    if (!citem) return;
 
246
 
 
247
    KHotKeys::Condition *cond = _items.value(citem);
 
248
 
 
249
    // Currently we only allow editing existing and active window conditions.
 
250
    // TODO: Disable buttons according to active item
 
251
 
 
252
    KHotKeys::Existing_window_condition *ewcond = 
 
253
        dynamic_cast<KHotKeys::Existing_window_condition*>(cond);
 
254
    if (ewcond)
 
255
        {
 
256
        WindowDefinitionListDialog dialog(ewcond->window());
 
257
        switch (dialog.exec())
 
258
            {
 
259
            case QDialog::Accepted:
 
260
                {
 
261
                citem->setText(0, ewcond->description());
 
262
                emitChanged(true);
 
263
                }
 
264
                break;
 
265
 
 
266
            case QDialog::Rejected:
 
267
                // Nothing to do
 
268
                return;
 
269
 
 
270
            default:
 
271
                Q_ASSERT(false);
 
272
                return;
 
273
            }
 
274
        }
 
275
 
 
276
    KHotKeys::Active_window_condition *awcond = 
 
277
        dynamic_cast<KHotKeys::Active_window_condition*>(cond);
 
278
    if (awcond)
 
279
        {
 
280
        WindowDefinitionListDialog dialog(awcond->window());
 
281
        switch (dialog.exec())
 
282
            {
 
283
            case QDialog::Accepted:
 
284
                {
 
285
                citem->setText(0, awcond->description());
 
286
                emitChanged(true);
 
287
                }
 
288
                break;
 
289
 
 
290
            case QDialog::Rejected:
 
291
                // Nothing to do
 
292
                return;
 
293
 
 
294
            default:
 
295
                Q_ASSERT(false);
 
296
                return;
 
297
            }
 
298
        }
 
299
 
 
300
    return;
 
301
    }
 
302
 
 
303
 
 
304
void ConditionsWidget::slotNew(QAction* action)
 
305
    {
 
306
    QTreeWidgetItem *citem = ui.tree->currentItem();
 
307
 
 
308
    KHotKeys::Condition *cond;
 
309
    if (!citem)
 
310
        {
 
311
        // If no item is selected create the new condition as a top level
 
312
        // condition
 
313
        cond = _working;
 
314
        citem = ui.tree->invisibleRootItem()->child(0);
 
315
        Q_ASSERT(citem);
 
316
        }
 
317
    else
 
318
        {
 
319
        // Get the currently select condition
 
320
        cond = _items.value(citem);
 
321
        }
 
322
 
 
323
    // get the nearest list
 
324
    KHotKeys::Condition_list_base *parent = dynamic_cast<KHotKeys::Condition_list_base*>(cond);
 
325
 
 
326
    if (!parent)
 
327
        {
 
328
        parent = cond->parent();
 
329
        citem = citem->parent();
 
330
        }
 
331
 
 
332
    Q_ASSERT(parent);
 
333
 
 
334
    switch (action->data().toInt())
 
335
        {
 
336
        case ConditionTypeMenu::ACTIVE_WINDOW:
 
337
            {
 
338
            KHotKeys::Windowdef_list *list = new KHotKeys::Windowdef_list();
 
339
            WindowDefinitionListDialog dialog(list);
 
340
            switch (dialog.exec())
 
341
                {
 
342
                case QDialog::Accepted:
 
343
                    {
 
344
                    KHotKeys::Active_window_condition *cond =
 
345
                        new KHotKeys::Active_window_condition(list, parent);
 
346
                    QTreeWidgetItem *item = new QTreeWidgetItem(citem);
 
347
                    item->setText(0, cond->description());
 
348
                    _items.insert(item, cond);
 
349
                    }
 
350
                    break;
 
351
 
 
352
                case QDialog::Rejected:
 
353
                    delete list;
 
354
                    return;
 
355
 
 
356
                default:
 
357
                    Q_ASSERT(false);
 
358
                    delete list;
 
359
                    return;
 
360
                }
 
361
            }
 
362
            break;
 
363
 
 
364
        case ConditionTypeMenu::EXISTING_WINDOW:
 
365
            {
 
366
            KHotKeys::Windowdef_list *list = new KHotKeys::Windowdef_list();
 
367
            WindowDefinitionListDialog dialog(list);
 
368
            switch (dialog.exec())
 
369
                {
 
370
                case QDialog::Accepted:
 
371
                    {
 
372
                    KHotKeys::Existing_window_condition *cond =
 
373
                        new KHotKeys::Existing_window_condition(list, parent);
 
374
                    QTreeWidgetItem *item = new QTreeWidgetItem(citem);
 
375
                    item->setText(0, cond->description());
 
376
                    _items.insert(item, cond);
 
377
                    }
 
378
                    break;
 
379
 
 
380
                case QDialog::Rejected:
 
381
                    delete list;
 
382
                    return;
 
383
 
 
384
                default:
 
385
                    Q_ASSERT(false);
 
386
                    delete list;
 
387
                    return;
 
388
                }
 
389
            }
 
390
            break;
 
391
 
 
392
        case ConditionTypeMenu::AND:
 
393
            {
 
394
            KHotKeys::And_condition *cond = new KHotKeys::And_condition(parent);
 
395
            QTreeWidgetItem *item = new QTreeWidgetItem(citem);
 
396
            item->setText(0, cond->description());
 
397
            _items.insert(item, cond);
 
398
            }
 
399
            break;
 
400
 
 
401
        case ConditionTypeMenu::OR:
 
402
            {
 
403
            KHotKeys::Or_condition *cond = new KHotKeys::Or_condition(parent);
 
404
            QTreeWidgetItem *item = new QTreeWidgetItem(citem);
 
405
            item->setText(0, cond->description());
 
406
            _items.insert(item, cond);
 
407
            }
 
408
            break;
 
409
 
 
410
        case ConditionTypeMenu::NOT:
 
411
            {
 
412
            KHotKeys::Not_condition *cond = new KHotKeys::Not_condition(parent);
 
413
            QTreeWidgetItem *item = new QTreeWidgetItem(citem);
 
414
            item->setText(0, cond->description());
 
415
            _items.insert(item, cond);
 
416
            }
 
417
            break;
 
418
 
 
419
        default:
 
420
            Q_ASSERT(false);
 
421
            break;
 
422
        }
 
423
 
 
424
    emitChanged(true);
 
425
    }
 
426
 
 
427
#include "moc_conditions_widget.cpp"