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

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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
   Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "hotkeys_model.h"

#include "action_data/simple_action_data.h"
#include "action_data/menuentry_shortcut_action_data.h"
#include "action_data/action_data_group.h"

#include <typeinfo>

#include <QMimeData>

#include <KDE/KDebug>
#include <KDE/KLocale>
#include <KDE/KIcon>


static KHotKeys::ActionDataBase *findElement(
        void *ptr
        ,KHotKeys::ActionDataGroup *root)
    {
    Q_ASSERT(root);
    if (!root) return NULL;

    KHotKeys::ActionDataBase *match = NULL;

    Q_FOREACH( KHotKeys::ActionDataBase *element, root->children())
        {
        if (ptr == element)
            {
            match = element;
            break;
            }

        if (KHotKeys::ActionDataGroup *subGroup = dynamic_cast<KHotKeys::ActionDataGroup*>(element))
            {
            match = findElement(ptr, subGroup);
            if (match) break;
            }
        }

    return match;
    }



KHotkeysModel::KHotkeysModel( QObject *parent )
    : QAbstractItemModel(parent)
     ,_settings()
     ,_actions(0)
    {}


KHotkeysModel::~KHotkeysModel()
    {
    }


QModelIndex KHotkeysModel::addGroup( const QModelIndex & parent )
    {
    KHotKeys::ActionDataGroup *list;
    if (parent.isValid())
        {
        list = indexToActionDataGroup(parent);
        }
    else
        {
        list = _actions;
        }
    Q_ASSERT(list);

    beginInsertRows( parent, list->size(), list->size() );

    /* KHotKeys:: ActionDataGroup *action = */
    new KHotKeys::ActionDataGroup( list, i18n("New Group"), i18n("Comment"));

    endInsertRows();
    return index( list->size()-1, NameColumn, parent );
    }


// Add a group
QModelIndex KHotkeysModel::insertActionData(  KHotKeys::ActionDataBase *data, const QModelIndex & parent )
    {
    Q_ASSERT(data);

    KHotKeys::ActionDataGroup *list;
    if (parent.isValid())
        {
        list = indexToActionDataGroup(parent);
        }
    else
        {
        list = _actions;
        }
    Q_ASSERT(list);

    beginInsertRows( parent, list->size(), list->size() );

    list->add_child(data);

    endInsertRows();
    return index( list->size()-1, NameColumn, parent );
    }


int KHotkeysModel::columnCount( const QModelIndex & ) const
    {
    return 2;
    }


QVariant KHotkeysModel::data( const QModelIndex &index, int role ) const
    {
    // Check that the index is valid
    if (!index.isValid())
        {
        return QVariant();
        }

    // Get the item behind the index
    KHotKeys::ActionDataBase *action = indexToActionDataBase(index);
    Q_ASSERT(action);

    // Handle CheckStateRole
    if (role==Qt::CheckStateRole)
        {
        switch(index.column())
            {
            case EnabledColumn:
                // If the parent is enabled we display the state of the object.
                // If the parent is disabled this object is disabled too.
                if (action->parent() && !action->parent()->isEnabled())
                    {
                    return Qt::Unchecked;
                    }
                return action->isEnabled()
                    ? Qt::Checked
                    : Qt::Unchecked;

            default:
                return QVariant();
            }
        }

    // Display and Tooltip. Tooltip displays the complete name. That's nice if
    // there is not enough space
    else if (role==Qt::DisplayRole || role==Qt::ToolTipRole)
        {
        switch (index.column())
            {
            case NameColumn:
                return action->name();

            case EnabledColumn:
                return QVariant();

            case IsGroupColumn:
                return indexToActionDataGroup(index)!=0;

            case TypeColumn:
                {
                const std::type_info &ti = typeid(*action);
                if (ti==typeid(KHotKeys::SimpleActionData))
                    return KHotkeysModel::SimpleActionData;
                else if (ti==typeid(KHotKeys::MenuEntryShortcutActionData))
                    return KHotkeysModel::SimpleActionData;
                else if (ti==typeid(KHotKeys::ActionDataGroup))
                    return KHotkeysModel::ActionDataGroup;
                else
                    return KHotkeysModel::Other;
                }

            default:
                return QVariant();
            }
        }

    // Decoration role
    else if (role==Qt::DecorationRole)
        {
        switch (index.column())
            {
            // The 0 is correct here. We want to decorate that column
            // regardless of the content it has
            case 0:
                return dynamic_cast<KHotKeys::ActionDataGroup*>(action)
                    ? KIcon("folder")
                    : QVariant();

            default:
                return QVariant();
            }
        }

    //Providing the current action name on edit
    else if (role==Qt::EditRole)
        {
        switch (index.column())
            {
            case NameColumn:
                return action->name();

            default:
                return QVariant();
            }
        }

    else if (role==Qt::ForegroundRole)
        {
        QPalette pal;
        switch (index.column())
            {
            case NameColumn:
                if (!action->isEnabled())
                    {
                    return pal.color(QPalette::Disabled, QPalette::Foreground);
                    }

            default:
                return QVariant();
            }
        }

    // For everything else
    return QVariant();
    }


bool KHotkeysModel::dropMimeData(
        const QMimeData *data
        ,Qt::DropAction action
        ,int row
        ,int column
        ,const QModelIndex &parent)
    {
    Q_UNUSED(column);

    // We only support move actions and our own mime type
    if ( (action!=Qt::CopyAction)
            || !data->hasFormat("application/x-pointer"))
        {
        kDebug() << "Drop not supported " << data->formats();
        return false;
        }

    // Decode the stream
    QByteArray encodedData = data->data("application/x-pointer");
    QDataStream stream(&encodedData, QIODevice::ReadOnly);
    QList<quintptr> ptrs;
    while (!stream.atEnd())
        {
        quintptr ptr;
        stream >> ptr;
        ptrs << ptr;
        }

    // No pointers, nothing to do
    if (ptrs.empty()) return false;

    // Get the group we have to drop into. If the drop target is no group get
    // it's parent and drop behind it
    int position = row;
    QModelIndex dropIndex = parent;
    KHotKeys::ActionDataGroup *dropToGroup = indexToActionDataGroup(dropIndex);
    if (!dropToGroup)
        {
        dropIndex = parent.parent();
        dropToGroup = indexToActionDataGroup(dropIndex);
        position = dropToGroup->children().indexOf(indexToActionDataBase(parent));
        }

    if (position==-1)
        {
        position = dropToGroup->size();
        }

    // Do the moves
    Q_FOREACH(quintptr ptr, ptrs)
        {
        KHotKeys::ActionDataBase *element = findElement(
                reinterpret_cast<void*>(ptr),
                _actions);

        if (element) moveElement(element, dropToGroup, position);
        }

    return true;
    }


void KHotkeysModel::emitChanged(KHotKeys::ActionDataBase *item)
    {
    Q_ASSERT( item );

    KHotKeys::ActionDataGroup *parent = item->parent();
    QModelIndex topLeft;
    QModelIndex bottomRight;
    if (!parent)
        {
        topLeft = createIndex( 0, 0, _actions );
        bottomRight = createIndex( 0, 0, _actions );
        }
    else
        {
        int row = parent->children().indexOf(item);
        topLeft = createIndex( row, 0, parent );
        bottomRight = createIndex( row, columnCount(topLeft), parent );
        }

    emit dataChanged( topLeft, bottomRight );
    }


void KHotkeysModel::exportInputActions(
        const QModelIndex &index,
        KConfigBase &config,
        const QString& id,
        const KHotKeys::ActionState state,
        bool mergingAllowed)
    {
    KHotKeys::ActionDataBase  *element = indexToActionDataBase(index);
    KHotKeys::ActionDataGroup *group   = indexToActionDataGroup(index);

    settings()->exportTo(
            group ? group : element->parent(),
            config,
            id,
            state,
            mergingAllowed);
    }


Qt::ItemFlags KHotkeysModel::flags( const QModelIndex &index ) const
    {
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);

    Q_ASSERT(!(flags & Qt::ItemIsDropEnabled));
    Q_ASSERT(!(flags & Qt::ItemIsDragEnabled));

    if (!index.isValid())
        {
            return flags | Qt::ItemIsDropEnabled;
        }

    KHotKeys::ActionDataBase  *element = indexToActionDataBase(index);
    KHotKeys::ActionDataGroup *actionGroup = indexToActionDataGroup(index);
    if (!actionGroup) actionGroup = element->parent();

    Q_ASSERT(element);
    Q_ASSERT(actionGroup);

    // We do not allow dragging for system groups and their elements
    // We do not allow dropping into systemgroups
    if (!actionGroup->is_system_group())
        {
        flags |= Qt::ItemIsDragEnabled;
        flags |= Qt::ItemIsDropEnabled;
        }

    // Show a checkbox in column 1 whatever is shown there.
    switch (index.column())
        {
        case 1:
            return flags
                | Qt::ItemIsUserCheckable;

        default:
            return flags
                | Qt::ItemIsEditable;
        }
    }


// Get header data for section
QVariant KHotkeysModel::headerData( int section, Qt::Orientation, int role ) const
    {
    if (role!=Qt::DisplayRole)
        {
        return QVariant();
        }

    switch (section)
        {
        case NameColumn:
            return QVariant(i18nc("action name", "Name"));

        case EnabledColumn:
            return QVariant();
            return QVariant(i18nc("action enabled", "Enabled"));

        case IsGroupColumn:
            return QVariant(i18n("Type"));

        default:
            return QVariant();
        }
    }


void KHotkeysModel::importInputActions(const QModelIndex &index, KConfigBase const &config)
    {
    KHotKeys::ActionDataGroup *group = indexToActionDataGroup(index);
    QModelIndex groupIndex = index;
    if (!group)
        {
        group = indexToActionDataBase(index)->parent();
        groupIndex = index.parent();
        }

    if (settings()->importFrom(group, config, KHotKeys::ImportAsk, KHotKeys::Retain))
        {
        kDebug();
        reset();
        save();
        }
    }


QModelIndex KHotkeysModel::index( int row, int column, const QModelIndex &parent ) const
    {
    KHotKeys::ActionDataGroup *actionGroup = indexToActionDataGroup(parent);
    if (!actionGroup || row>=actionGroup->children().size() )
        {
        return QModelIndex();
        }

    KHotKeys::ActionDataBase *action =  actionGroup->children().at(row);
    Q_ASSERT( action );
    return createIndex( row, column, action );
    }


// Convert index to ActionDataBase
KHotKeys::ActionDataBase *KHotkeysModel::indexToActionDataBase( const QModelIndex &index ) const
    {
    if (!index.isValid())
        {
        return _actions;
        }
    return static_cast<KHotKeys::ActionDataBase*>( index.internalPointer() );
    }


// Convert index to ActionDataGroup
KHotKeys::ActionDataGroup *KHotkeysModel::indexToActionDataGroup( const QModelIndex &index ) const
    {
    if (!index.isValid())
        {
        return _actions;
        }
    return dynamic_cast<KHotKeys::ActionDataGroup*>( indexToActionDataBase(index) );
    }


void KHotkeysModel::load()
    {
    _settings.reread_settings(true);
    _actions = _settings.actions();
    reset();
    }


QMimeData *KHotkeysModel::mimeData(const QModelIndexList &indexes) const
    {
    QMimeData * mimeData = new QMimeData();
    QByteArray encodedData;

    QDataStream stream(&encodedData, QIODevice::WriteOnly);

    Q_FOREACH (const QModelIndex &index, indexes)
        {
        if (index.isValid() and index.column() == 0)
            {
            KHotKeys::ActionDataBase *element = indexToActionDataBase(index);
            // We use the pointer as id.
            stream << reinterpret_cast<quintptr>(element);
            }
        }

    mimeData->setData("application/x-pointer", encodedData);
    return mimeData;
    }


QStringList KHotkeysModel::mimeTypes() const
    {
    QStringList types;
    types << "application/x-pointer";
    return types;
    }


bool KHotkeysModel::moveElement(
        KHotKeys::ActionDataBase   *element
        ,KHotKeys::ActionDataGroup *newGroup
        ,int position)
    {
    Q_ASSERT(element && newGroup);
    if (!element || !newGroup) return false;

    // TODO: Make this logic more advanced
    // We do not allow moving into our systemgroup
    if (newGroup->is_system_group()) return false;

    // Make sure we don't move a group to one of it's children or
    // itself.
    KHotKeys::ActionDataGroup *tmp = newGroup;
    do  {
        if (tmp == element)
            {
            kDebug() << "Forbidden move" << tmp->name();
            return false;
            }
        }
        while((tmp = tmp->parent()));

    KHotKeys::ActionDataGroup *oldParent = element->parent();

    // TODO: Make this logic more advanced
    // We do not allow moving from our systemgroup
    if (oldParent->is_system_group()) return false;

    // Adjust position if oldParent and newGroup are identical
    if (oldParent == newGroup)
        {
        if (oldParent->children().indexOf(element) < position)
            {
            --position;
            }
        }

    emit layoutAboutToBeChanged();

    // Remove it from it's current place
    oldParent->remove_child(element);
    newGroup->add_child(element, position);

    emit layoutChanged();

    return true;
    }


// Get parent object for index
QModelIndex KHotkeysModel::parent( const QModelIndex &index ) const
    {
    KHotKeys::ActionDataBase *action = indexToActionDataBase(index);
    if (!action)
        {
        return QModelIndex();
        }

    KHotKeys::ActionDataGroup *parent = action->parent();
    if (!parent)
        {
        return QModelIndex();
        }

    KHotKeys::ActionDataGroup *grandparent = parent->parent();
    if (!grandparent)
        {
        return QModelIndex();
        }

    int row = grandparent->children().indexOf(parent);
    return createIndex( row, 0, parent );
    }


// Remove rows ( items )
bool KHotkeysModel::removeRows( int row, int count, const QModelIndex &parent )
    {
    Q_ASSERT( count == 1 );

    beginRemoveRows( parent, row, row+count-1 );

    KHotKeys::ActionDataGroup *list;
    if (parent.isValid())
        {
        list = indexToActionDataGroup(parent);
        }
    else
        {
        list = _actions;
        }
    Q_ASSERT(list);

    KHotKeys::ActionDataBase *action = indexToActionDataBase(index(row,0,parent));

    action->aboutToBeErased();
    delete action;

    endRemoveRows();
    return true;
    }


// Number of rows for index
int KHotkeysModel::rowCount( const QModelIndex &index ) const
    {
    KHotKeys::ActionDataGroup *group = indexToActionDataGroup(index);
    if (!group)
        {
        return 0;
        }

    return group->children().count();
    }


void KHotkeysModel::save()
    {
    _settings.write();
    }


// Set data
bool KHotkeysModel::setData( const QModelIndex &index, const QVariant &value, int role )
    {

    if ( !index.isValid() )
        {
        return false;
        }

    KHotKeys::ActionDataBase *action = indexToActionDataBase(index);
    Q_ASSERT( action );

    // Handle CheckStateRole
    if ( role == Qt::CheckStateRole )
        {
        switch(index.column())
            {
            case EnabledColumn:
                {
                // If the parent is enabled we display the state of the object.
                // If the parent is disabled this object is disabled too.
                if (action->parent() && !action->parent()->isEnabled())
                    {
                    // TODO: Either show a message box or enhance the gui to
                    // show this item cannot be enabled
                    return false;
                    }

                value.toInt() == Qt::Checked
                    ? action->enable()
                    : action->disable();

                // If this is a group we have to inform the view that all our
                // childs have changed. They are all disabled now
                KHotKeys::ActionDataGroup *actionGroup = indexToActionDataGroup(index);
                if (actionGroup && actionGroup->size())
                    {
                    Q_EMIT dataChanged(
                            createIndex(0, 0, actionGroup),
                            createIndex(actionGroup->size(), columnCount(index), actionGroup));
                    }
                }
                break;

            default:
                return false;
            }
        }
    else if ( role == Qt::EditRole )
        {
        switch ( index.column() )
            {
            case NameColumn:
                {
                action->set_name( value.toString() );
                }
                break;

            default:
                return false;
            }
        }
    else
        return false;

    emit dataChanged( index, index );
    return true;
    }


KHotKeys::Settings *KHotkeysModel::settings()
    {
    return &_settings;
    }


#include "moc_hotkeys_model.cpp"