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

« back to all changes in this revision

Viewing changes to kwin/desktoplayout.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 Lucas Murray <lmurray@undefinedfire.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
#include "workspace.h"
 
22
 
 
23
#include "assert.h"
 
24
 
 
25
namespace KWin
 
26
{
 
27
 
 
28
void Workspace::updateDesktopLayout()
 
29
{
 
30
    // TODO: Is there a sane way to avoid overriding the existing grid?
 
31
    int width = rootInfo->desktopLayoutColumnsRows().width();
 
32
    int height = rootInfo->desktopLayoutColumnsRows().height();
 
33
    if (width == 0 && height == 0)   // Not given, set default layout
 
34
        height = 2;
 
35
    setNETDesktopLayout(
 
36
        rootInfo->desktopLayoutOrientation() == NET::OrientationHorizontal ? Qt::Horizontal : Qt::Vertical,
 
37
        width, height, 0 //rootInfo->desktopLayoutCorner() // Not really worth implementing right now.
 
38
    );
 
39
}
 
40
 
 
41
void Workspace::setNETDesktopLayout(Qt::Orientation orientation, int width, int height,
 
42
                                    int startingCorner)
 
43
{
 
44
    Q_UNUSED(startingCorner);   // Not really worth implementing right now.
 
45
 
 
46
    // Calculate valid grid size
 
47
    assert(width > 0 || height > 0);
 
48
    if ((width <= 0) && (height > 0))
 
49
        width = (desktopCount_ + height - 1) / height;
 
50
    else if ((height <= 0) && (width > 0))
 
51
        height = (desktopCount_ + width - 1) / width;
 
52
    while (width * height < desktopCount_) {
 
53
        if (orientation == Qt::Horizontal)
 
54
            ++width;
 
55
        else
 
56
            ++height;
 
57
    }
 
58
 
 
59
    // Set private variables
 
60
    delete[] desktopGrid_;
 
61
    desktopGridSize_ = QSize(width, height);
 
62
    int size = width * height;
 
63
    desktopGrid_ = new int[size];
 
64
 
 
65
    // Populate grid
 
66
    int desktop = 1;
 
67
    if (orientation == Qt::Horizontal)
 
68
        for (int y = 0; y < height; y++)
 
69
            for (int x = 0; x < width; x++)
 
70
                desktopGrid_[y * width + x] = (desktop <= desktopCount_ ? desktop++ : 0);
 
71
    else
 
72
        for (int x = 0; x < width; x++)
 
73
            for (int y = 0; y < height; y++)
 
74
                desktopGrid_[y * width + x] = (desktop <= desktopCount_ ? desktop++ : 0);
 
75
}
 
76
 
 
77
QPoint Workspace::desktopGridCoords(int id) const
 
78
{
 
79
    for (int y = 0; y < desktopGridSize_.height(); y++)
 
80
        for (int x = 0; x < desktopGridSize_.width(); x++)
 
81
            if (desktopGrid_[y * desktopGridSize_.width() + x] == id)
 
82
                return QPoint(x, y);
 
83
    return QPoint(-1, -1);
 
84
}
 
85
 
 
86
QPoint Workspace::desktopCoords(int id) const
 
87
{
 
88
    QPoint coords = desktopGridCoords(id);
 
89
    if (coords.x() == -1)
 
90
        return QPoint(-1, -1);
 
91
    return QPoint(coords.x() * displayWidth(), coords.y() * displayHeight());
 
92
}
 
93
 
 
94
int Workspace::desktopAbove(int id, bool wrap) const
 
95
{
 
96
    if (id == 0)
 
97
        id = currentDesktop();
 
98
    QPoint coords = desktopGridCoords(id);
 
99
    assert(coords.x() >= 0);
 
100
    for (;;) {
 
101
        coords.ry()--;
 
102
        if (coords.y() < 0) {
 
103
            if (wrap)
 
104
                coords.setY(desktopGridSize_.height() - 1);
 
105
            else
 
106
                return id; // Already at the top-most desktop
 
107
        }
 
108
        int desktop = desktopAtCoords(coords);
 
109
        if (desktop > 0)
 
110
            return desktop;
 
111
    }
 
112
}
 
113
 
 
114
int Workspace::desktopToRight(int id, bool wrap) const
 
115
{
 
116
    if (id == 0)
 
117
        id = currentDesktop();
 
118
    QPoint coords = desktopGridCoords(id);
 
119
    assert(coords.x() >= 0);
 
120
    for (;;) {
 
121
        coords.rx()++;
 
122
        if (coords.x() >= desktopGridSize_.width()) {
 
123
            if (wrap)
 
124
                coords.setX(0);
 
125
            else
 
126
                return id; // Already at the right-most desktop
 
127
        }
 
128
        int desktop = desktopAtCoords(coords);
 
129
        if (desktop > 0)
 
130
            return desktop;
 
131
    }
 
132
}
 
133
 
 
134
int Workspace::desktopBelow(int id, bool wrap) const
 
135
{
 
136
    if (id == 0)
 
137
        id = currentDesktop();
 
138
    QPoint coords = desktopGridCoords(id);
 
139
    assert(coords.x() >= 0);
 
140
    for (;;) {
 
141
        coords.ry()++;
 
142
        if (coords.y() >= desktopGridSize_.height()) {
 
143
            if (wrap)
 
144
                coords.setY(0);
 
145
            else
 
146
                return id; // Already at the bottom-most desktop
 
147
        }
 
148
        int desktop = desktopAtCoords(coords);
 
149
        if (desktop > 0)
 
150
            return desktop;
 
151
    }
 
152
}
 
153
 
 
154
int Workspace::desktopToLeft(int id, bool wrap) const
 
155
{
 
156
    if (id == 0)
 
157
        id = currentDesktop();
 
158
    QPoint coords = desktopGridCoords(id);
 
159
    assert(coords.x() >= 0);
 
160
    for (;;) {
 
161
        coords.rx()--;
 
162
        if (coords.x() < 0) {
 
163
            if (wrap)
 
164
                coords.setX(desktopGridSize_.width() - 1);
 
165
            else
 
166
                return id; // Already at the left-most desktop
 
167
        }
 
168
        int desktop = desktopAtCoords(coords);
 
169
        if (desktop > 0)
 
170
            return desktop;
 
171
    }
 
172
}
 
173
 
 
174
} // namespace