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

« back to all changes in this revision

Viewing changes to kwin/tabbox/desktopmodel.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
 KWin - the KDE window manager
 
3
 This file is part of the KDE project.
 
4
 
 
5
Copyright (C) 2009 Martin Gräßlin <kde@martin-graesslin.com>
 
6
 
 
7
This program is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation; either version 2 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
This program is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
*********************************************************************/
 
20
// own
 
21
#include "desktopmodel.h"
 
22
// tabbox
 
23
#include "clientmodel.h"
 
24
#include "tabboxconfig.h"
 
25
#include "tabboxhandler.h"
 
26
 
 
27
#include <math.h>
 
28
 
 
29
namespace KWin
 
30
{
 
31
namespace TabBox
 
32
{
 
33
 
 
34
DesktopModel::DesktopModel(QObject* parent)
 
35
    : QAbstractItemModel(parent)
 
36
{
 
37
}
 
38
 
 
39
DesktopModel::~DesktopModel()
 
40
{
 
41
}
 
42
 
 
43
QVariant DesktopModel::data(const QModelIndex& index, int role) const
 
44
{
 
45
    if (!index.isValid())
 
46
        return QVariant();
 
47
 
 
48
    int desktopIndex = index.row() * columnCount() + index.column();
 
49
    if (desktopIndex >= m_desktopList.count())
 
50
        return QVariant();
 
51
    switch(role) {
 
52
    case Qt::DisplayRole:
 
53
    case DesktopNameRole:
 
54
        return tabBox->desktopName(m_desktopList[ desktopIndex ]);
 
55
    case DesktopRole:
 
56
        return m_desktopList[ desktopIndex ];
 
57
    case ClientModelRole:
 
58
        return qVariantFromValue((void*)m_clientModels[ m_desktopList[ desktopIndex ] ]);
 
59
    default:
 
60
        return QVariant();
 
61
    }
 
62
}
 
63
 
 
64
int DesktopModel::columnCount(const QModelIndex& parent) const
 
65
{
 
66
    Q_UNUSED(parent)
 
67
    int count = 1;
 
68
    switch(tabBox->config().layout()) {
 
69
    case TabBoxConfig::HorizontalLayout:
 
70
        count = m_desktopList.count();
 
71
        break;
 
72
    case TabBoxConfig::VerticalLayout:
 
73
        count = 1;
 
74
        break;
 
75
    case TabBoxConfig::HorizontalVerticalLayout:
 
76
        count = qRound(sqrt(float(m_desktopList.count())));
 
77
        if (count * count < m_desktopList.count())
 
78
            count++;
 
79
        // TODO: pager layout?
 
80
        break;
 
81
    }
 
82
    return qMax(count, 1);
 
83
}
 
84
 
 
85
int DesktopModel::rowCount(const QModelIndex& parent) const
 
86
{
 
87
    Q_UNUSED(parent)
 
88
    int count = 1;
 
89
    switch(tabBox->config().layout()) {
 
90
    case TabBoxConfig::HorizontalLayout:
 
91
        count = 1;
 
92
        break;
 
93
    case TabBoxConfig::VerticalLayout:
 
94
        count = m_desktopList.count();
 
95
        break;
 
96
    case TabBoxConfig::HorizontalVerticalLayout:
 
97
        count = qRound(sqrt(float(m_desktopList.count())));
 
98
        // TODO: pager layout?
 
99
        break;
 
100
    }
 
101
    return qMax(count, 1);
 
102
}
 
103
 
 
104
QModelIndex DesktopModel::parent(const QModelIndex& child) const
 
105
{
 
106
    Q_UNUSED(child)
 
107
    return QModelIndex();
 
108
}
 
109
 
 
110
QModelIndex DesktopModel::index(int row, int column, const QModelIndex& parent) const
 
111
{
 
112
    Q_UNUSED(parent)
 
113
    int index = row * columnCount() + column;
 
114
    if (index > m_desktopList.count() || m_desktopList.isEmpty())
 
115
        return QModelIndex();
 
116
    return createIndex(row, column);
 
117
}
 
118
 
 
119
QModelIndex DesktopModel::desktopIndex(int desktop) const
 
120
{
 
121
    if (desktop > m_desktopList.count())
 
122
        return QModelIndex();
 
123
    int index = m_desktopList.indexOf(desktop);
 
124
    int row = index / columnCount();
 
125
    int column = index % columnCount();
 
126
    return createIndex(row, column);
 
127
}
 
128
 
 
129
void DesktopModel::createDesktopList()
 
130
{
 
131
    m_desktopList.clear();
 
132
    qDeleteAll(m_clientModels);
 
133
    m_clientModels.clear();
 
134
 
 
135
    switch(tabBox->config().desktopSwitchingMode()) {
 
136
    case TabBoxConfig::MostRecentlyUsedDesktopSwitching: {
 
137
        int desktop = tabBox->currentDesktop();
 
138
        do {
 
139
            m_desktopList.append(desktop);
 
140
            ClientModel* clientModel = new ClientModel(this);
 
141
            clientModel->createClientList(desktop);
 
142
            m_clientModels.insert(desktop, clientModel);
 
143
            desktop = tabBox->nextDesktopFocusChain(desktop);
 
144
        } while (desktop != tabBox->currentDesktop());
 
145
        break;
 
146
    }
 
147
    case TabBoxConfig::StaticDesktopSwitching: {
 
148
        for (int i = 1; i <= tabBox->numberOfDesktops(); i++) {
 
149
            m_desktopList.append(i);
 
150
            ClientModel* clientModel = new ClientModel(this);
 
151
            clientModel->createClientList(i);
 
152
            m_clientModels.insert(i, clientModel);
 
153
        }
 
154
        break;
 
155
    }
 
156
    }
 
157
    reset();
 
158
}
 
159
 
 
160
} // namespace Tabbox
 
161
} // namespace KWin