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

« back to all changes in this revision

Viewing changes to libs/taskmanager/strategies/desktopsortingstrategy.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
/*****************************************************************
 
2
 
 
3
Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
of this software and associated documentation files (the "Software"), to deal
 
7
in the Software without restriction, including without limitation the rights
 
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
copies of the Software, and to permit persons to whom the Software is
 
10
furnished to do so, subject to the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
18
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
21
 
 
22
******************************************************************/
 
23
 
 
24
#include "desktopsortingstrategy.h"
 
25
 
 
26
#include <QMap>
 
27
#include <QString>
 
28
#include <QtAlgorithms>
 
29
#include <QList>
 
30
 
 
31
#include <KDebug>
 
32
 
 
33
#include "abstractgroupableitem.h"
 
34
 
 
35
 
 
36
namespace TaskManager
 
37
{
 
38
 
 
39
DesktopSortingStrategy::DesktopSortingStrategy(QObject *parent)
 
40
:AbstractSortingStrategy(parent)
 
41
{
 
42
    setType(GroupManager::DesktopSorting);
 
43
}
 
44
 
 
45
void DesktopSortingStrategy::sortItems(ItemList &items)
 
46
{
 
47
    qStableSort(items.begin(), items.end(), DesktopSortingStrategy::lessThan);
 
48
}
 
49
 
 
50
bool DesktopSortingStrategy::lessThan(const AbstractGroupableItem *left, const AbstractGroupableItem *right)
 
51
{
 
52
    /*
 
53
     * Sorting strategy is as follows:
 
54
     * For two items being compared
 
55
     *   - Startup items will be sorted out to the end of the list and sorted by name there
 
56
     *   - If both are not startup tasks first compare items by desktop number,
 
57
     *     and then for items which belong to the same desktop sort by their id.
 
58
     */
 
59
    if (left->isStartupItem()) {
 
60
        if (right->isStartupItem()) {
 
61
            return left->name().toLower() < right->name().toLower();
 
62
        }
 
63
        return false;
 
64
    }
 
65
 
 
66
    if (right->isStartupItem()) {
 
67
            return true;
 
68
    }
 
69
 
 
70
    if (left->itemType() == LauncherItemType) {
 
71
        if (right->itemType() == LauncherItemType) {
 
72
            return left->name().toLower() < right->name().toLower();
 
73
        }
 
74
        return true;
 
75
    }
 
76
 
 
77
    if (right->itemType() == LauncherItemType) {
 
78
        return false;
 
79
    }
 
80
 
 
81
    const int leftDesktop = left->desktop();
 
82
    const int rightDesktop = right->desktop();
 
83
    if (leftDesktop == rightDesktop) {
 
84
            return left->id() < right->id();
 
85
    }
 
86
 
 
87
    return leftDesktop < rightDesktop;
 
88
}
 
89
 
 
90
void DesktopSortingStrategy::handleItem(AbstractGroupableItem *item)
 
91
{
 
92
    disconnect(item, 0, this, 0); //To avoid duplicate connections
 
93
    connect(item, SIGNAL(changed(::TaskManager::TaskChanges)), this, SLOT(check()));
 
94
    AbstractSortingStrategy::handleItem(item);
 
95
}
 
96
 
 
97
} //namespace
 
98
 
 
99
#include "desktopsortingstrategy.moc"
 
100