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

« back to all changes in this revision

Viewing changes to kwin/tabbox/clientmodel.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
 
 
21
// own
 
22
#include "clientmodel.h"
 
23
// tabbox
 
24
#include "tabboxconfig.h"
 
25
#include "tabboxhandler.h"
 
26
// Qt
 
27
#include <QTextStream>
 
28
// KDE
 
29
#include <KLocale>
 
30
// other
 
31
#include <math.h>
 
32
 
 
33
namespace KWin
 
34
{
 
35
namespace TabBox
 
36
{
 
37
 
 
38
ClientModel::ClientModel(QObject* parent)
 
39
    : QAbstractItemModel(parent)
 
40
{
 
41
}
 
42
 
 
43
ClientModel::~ClientModel()
 
44
{
 
45
}
 
46
 
 
47
QVariant ClientModel::data(const QModelIndex& index, int role) const
 
48
{
 
49
    if (!index.isValid())
 
50
        return QVariant();
 
51
 
 
52
    if (m_clientList.isEmpty()) {
 
53
        if (role == EmptyRole)
 
54
            return true;
 
55
        else
 
56
            return i18n("*** No Windows ***");
 
57
    }
 
58
 
 
59
    int clientIndex = index.row() * columnCount() + index.column();
 
60
    if (clientIndex >= m_clientList.count())
 
61
        return QVariant();
 
62
    switch(role) {
 
63
    case Qt::DisplayRole:
 
64
    case CaptionRole:
 
65
        return m_clientList[ clientIndex ]->caption();
 
66
    case ClientRole:
 
67
        return qVariantFromValue((void*)m_clientList[ clientIndex ]);
 
68
    case DesktopNameRole: {
 
69
        return tabBox->desktopName(m_clientList[ clientIndex ]);
 
70
    }
 
71
    case EmptyRole:
 
72
        return false;
 
73
    case WIdRole:
 
74
        return qulonglong(m_clientList[ clientIndex ]->window());
 
75
    case MinimizedRole:
 
76
        return m_clientList[ clientIndex ]->isMinimized();
 
77
    default:
 
78
        return QVariant();
 
79
    }
 
80
}
 
81
 
 
82
int ClientModel::columnCount(const QModelIndex& parent) const
 
83
{
 
84
    Q_UNUSED(parent)
 
85
    int count = 1;
 
86
    switch(tabBox->config().layout()) {
 
87
    case TabBoxConfig::HorizontalLayout:
 
88
        count = m_clientList.count();
 
89
        break;
 
90
    case TabBoxConfig::VerticalLayout:
 
91
        count = 1;
 
92
        break;
 
93
    case TabBoxConfig::HorizontalVerticalLayout:
 
94
        count = qRound(sqrt(float(m_clientList.count())));
 
95
        if (count * count < m_clientList.count())
 
96
            count++;
 
97
        break;
 
98
    }
 
99
    return qMax(count, 1);
 
100
}
 
101
 
 
102
int ClientModel::rowCount(const QModelIndex& parent) const
 
103
{
 
104
    Q_UNUSED(parent)
 
105
    int count = 1;
 
106
    switch(tabBox->config().layout()) {
 
107
    case TabBoxConfig::HorizontalLayout:
 
108
        count = 1;
 
109
        break;
 
110
    case TabBoxConfig::VerticalLayout:
 
111
        count = m_clientList.count();
 
112
        break;
 
113
    case TabBoxConfig::HorizontalVerticalLayout:
 
114
        count = qRound(sqrt(float(m_clientList.count())));
 
115
        break;
 
116
    }
 
117
    return qMax(count, 1);
 
118
}
 
119
 
 
120
QModelIndex ClientModel::parent(const QModelIndex& child) const
 
121
{
 
122
    Q_UNUSED(child)
 
123
    return QModelIndex();
 
124
}
 
125
 
 
126
QModelIndex ClientModel::index(int row, int column, const QModelIndex& parent) const
 
127
{
 
128
    Q_UNUSED(parent)
 
129
    int index = row * columnCount() + column;
 
130
    if (index >= m_clientList.count() && !m_clientList.isEmpty())
 
131
        return QModelIndex();
 
132
    return createIndex(row, column);
 
133
}
 
134
 
 
135
QModelIndex ClientModel::index(TabBoxClient* client) const
 
136
{
 
137
    if (!m_clientList.contains(client))
 
138
        return QModelIndex();
 
139
    int index = m_clientList.indexOf(client);
 
140
    int row = index / columnCount();
 
141
    int column = index % columnCount();
 
142
    return createIndex(row, column);
 
143
}
 
144
 
 
145
void ClientModel::createClientList(bool partialReset)
 
146
{
 
147
    createClientList(tabBox->currentDesktop(), partialReset);
 
148
}
 
149
 
 
150
void ClientModel::createClientList(int desktop, bool partialReset)
 
151
{
 
152
    TabBoxClient* start = tabBox->activeClient();
 
153
    // TODO: new clients are not added at correct position
 
154
    if (partialReset && !m_clientList.isEmpty())
 
155
        start = m_clientList.first();
 
156
 
 
157
    m_clientList.clear();
 
158
 
 
159
    switch(tabBox->config().clientSwitchingMode()) {
 
160
    case TabBoxConfig::FocusChainSwitching: {
 
161
        TabBoxClient* c = tabBox->nextClientFocusChain(start);
 
162
        TabBoxClient* stop = c;
 
163
        while (c) {
 
164
            TabBoxClient* add = tabBox->clientToAddToList(c, desktop,
 
165
                                tabBox->config().clientListMode() == TabBoxConfig::AllDesktopsClientList ||
 
166
                                tabBox->config().clientListMode() == TabBoxConfig::AllDesktopsApplicationList);
 
167
            if (add != NULL) {
 
168
                if (start == add) {
 
169
                    m_clientList.removeAll(add);
 
170
                    m_clientList.prepend(add);
 
171
                } else
 
172
                    m_clientList += add;
 
173
            }
 
174
            c = tabBox->nextClientFocusChain(c);
 
175
 
 
176
            if (c == stop)
 
177
                break;
 
178
        }
 
179
        break;
 
180
    }
 
181
    case TabBoxConfig::StackingOrderSwitching: {
 
182
        // TODO: needs improvement
 
183
        TabBoxClientList stacking = tabBox->stackingOrder();
 
184
        TabBoxClient* c = stacking.first();
 
185
        TabBoxClient* stop = c;
 
186
        int index = 0;
 
187
        while (c) {
 
188
            TabBoxClient* add = tabBox->clientToAddToList(c, desktop,
 
189
                                tabBox->config().clientListMode() == TabBoxConfig::AllDesktopsClientList ||
 
190
                                tabBox->config().clientListMode() == TabBoxConfig::AllDesktopsApplicationList);
 
191
            if (add != NULL) {
 
192
                if (start == add) {
 
193
                    m_clientList.removeAll(add);
 
194
                    m_clientList.prepend(add);
 
195
                } else
 
196
                    m_clientList += add;
 
197
            }
 
198
            if (index >= stacking.size() - 1) {
 
199
                c = NULL;
 
200
            } else {
 
201
                c = stacking[++index];
 
202
            }
 
203
 
 
204
            if (c == stop)
 
205
                break;
 
206
        }
 
207
        break;
 
208
    }
 
209
    }
 
210
    if (tabBox->config().isShowDesktop()) {
 
211
        TabBoxClient* desktopClient = tabBox->desktopClient();
 
212
        if (desktopClient)
 
213
            m_clientList.append(desktopClient);
 
214
    }
 
215
    reset();
 
216
}
 
217
 
 
218
} // namespace Tabbox
 
219
} // namespace KWin