kwin/tilinglayout.cpp

239 lines
5.2 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2009 Nikhil Marathe <nsm.nikhil@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "tilinglayout.h"
#include <QCursor>
#include "client.h"
#include "tile.h"
#include "workspace.h"
namespace KWin
{
2011-01-30 14:34:42 +00:00
TilingLayout::TilingLayout(Workspace *w)
: m_workspace(w)
{
}
TilingLayout::~TilingLayout()
2011-01-30 14:34:42 +00:00
{
qDeleteAll(m_tiles);
m_tiles.clear();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
int TilingLayout::findTilePos(Client *c) const
{
int i = 0;
2011-01-30 14:34:42 +00:00
foreach (Tile * t, m_tiles) {
if (t->client() == c)
return i;
i++;
}
2011-01-30 14:34:42 +00:00
return -1;
}
2011-01-30 14:34:42 +00:00
Tile* TilingLayout::findTile(Client *c) const
{
int i = findTilePos(c);
if (i != -1)
return m_tiles[ i ];
return NULL;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TilingLayout::clientMinimizeToggled(Client *c)
{
// just rearrange since that will check for state
2011-01-30 14:34:42 +00:00
Tile *t = findTile(c);
if (t)
arrange(layoutArea(t));
}
2011-01-30 14:34:42 +00:00
bool TilingLayout::clientResized(Client *c, const QRect &moveResizeGeom, const QRect &orig)
{
if (moveResizeGeom == orig)
return true;
2011-01-30 14:34:42 +00:00
Tile *t = findTile(c);
if (!t || t->ignoreGeometry()) {
c->setGeometry(moveResizeGeom);
return true;
2011-01-30 14:34:42 +00:00
}
return false;
2011-01-30 14:34:42 +00:00
}
// tries to swap the tile with the one in the new position right now
2011-01-30 14:34:42 +00:00
void TilingLayout::clientMoved(Client *c, const QRect &moveResizeGeom, const QRect &orig)
{
if (moveResizeGeom == orig)
return;
2011-01-30 14:34:42 +00:00
Tile *t = findTile(c);
if (!t) {
c->setGeometry(moveResizeGeom);
return;
2011-01-30 14:34:42 +00:00
}
if (t->floating()) {
t->setGeometry(moveResizeGeom);
t->commit();
return;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
Tile *r = findTileBelowPoint(QCursor::pos());
// TODO: if the client moved in from another desktop, don't swap, add
2011-01-30 14:34:42 +00:00
if (r && t) {
swapTiles(r, t);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TilingLayout::swapTiles(Tile *a, Tile *b)
{
if (a && b) {
// t is the tile the user requested a move of
// r is the tile below it
2011-01-30 14:34:42 +00:00
int a_index = tiles().indexOf(a);
int b_index = tiles().indexOf(b);
// use m_tiles since tiles() is const
// not sure how good an idea this is
2011-01-30 14:34:42 +00:00
m_tiles.replace(a_index, b);
m_tiles.replace(b_index, a);
arrange(layoutArea(a));
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TilingLayout::addTileNoArrange(Tile * t)
{
if (findTile(t->client()))
return;
2011-01-30 14:34:42 +00:00
m_tiles.append(t);
postAddTile(t);
}
2011-01-30 14:34:42 +00:00
void TilingLayout::addTile(Tile *t)
{
addTileNoArrange(t);
arrange(layoutArea(t));
}
2011-01-30 14:34:42 +00:00
void TilingLayout::addTile(Client *c)
{
Q_UNUSED(c)
}
2011-01-30 14:34:42 +00:00
void TilingLayout::removeTileNoArrange(Tile * t)
{
if (t == NULL)
return;
2011-01-30 14:34:42 +00:00
preRemoveTile(t);
m_tiles.removeOne(t);
}
2011-01-30 14:34:42 +00:00
const QRect TilingLayout::layoutArea(Tile *t) const
{
return m_workspace->clientArea(PlacementArea, t->client());
}
2011-01-30 14:34:42 +00:00
void TilingLayout::removeTile(Tile *t)
{
if (t == NULL)
return;
removeTileNoArrange(t);
if (!m_tiles.empty())
arrange(layoutArea(m_tiles.first()));
}
2011-01-30 14:34:42 +00:00
void TilingLayout::removeTile(Client *c)
{
removeTile(findTile(c));
}
2011-01-30 14:34:42 +00:00
void TilingLayout::toggleFloatTile(Client *c)
{
Tile *t = findTile(c);
if (t && t->floating())
t->unfloatTile();
2011-01-30 14:34:42 +00:00
else if (t)
t->floatTile();
2011-01-30 14:34:42 +00:00
if (t)
arrange(layoutArea(t));
}
void TilingLayout::reconfigureTiling()
2011-01-30 14:34:42 +00:00
{
//TODO also check 'untiled' windows to see if they are now requesting tiling
foreach (Tile * t, tiles()) {
if (t->client()->rules()->checkTilingOption(t->floating() ? 1 : 0) == 1)
t->floatTile();
else
t->unfloatTile();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (tiles().length() > 0)
arrange(layoutArea(tiles().first()));
2011-01-30 14:34:42 +00:00
foreach (Client * c, workspace()->stackingOrder()) {
if (c->rules()->checkTilingOption(0) == 1)
workspace()->createTile(c);
}
2011-01-30 14:34:42 +00:00
}
Tile* TilingLayout::findTileBelowPoint(const QPoint &p) const
{
foreach (Tile * t, tiles()) {
if (t->floating())
continue;
2011-01-30 14:34:42 +00:00
if (t->geometry().contains(p))
return t;
2011-01-30 14:34:42 +00:00
}
return NULL;
2011-01-30 14:34:42 +00:00
}
void TilingLayout::commit()
2011-01-30 14:34:42 +00:00
{
foreach (Tile * t, m_tiles)
t->commit();
}
/*
* Default is to allow no resizing
*/
2011-01-30 14:34:42 +00:00
KDecorationDefines::Position TilingLayout::resizeMode(Client *c, KDecorationDefines::Position currentMode) const
{
Tile *t = findTile(c);
// if not tiled, allow resize
2011-01-30 14:34:42 +00:00
if (!t)
return currentMode;
2011-01-30 14:34:42 +00:00
if (t && t->floating())
return currentMode;
// We return PositionCenter since it makes
// no sense in resizing.
return KDecorationDefines::PositionCenter;
2011-01-30 14:34:42 +00:00
}
} // end namespace