kwin/tabbox/tabboxhandler.cpp

536 lines
15 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2009 Martin Gräßlin <kde@martin-graesslin.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/>.
*********************************************************************/
// own
#include "tabboxhandler.h"
// tabbox
#include "clientmodel.h"
#include "declarative.h"
#include "desktopmodel.h"
#include "tabboxconfig.h"
// Qt
#include <QKeyEvent>
#include <QModelIndex>
#include <QTimer>
#include <QX11Info>
#include <X11/Xlib.h>
// KDE
#include <KDebug>
#include <KWindowSystem>
namespace KWin
{
namespace TabBox
{
class TabBoxHandlerPrivate
2011-01-30 14:34:42 +00:00
{
public:
TabBoxHandlerPrivate(TabBoxHandler *q);
~TabBoxHandlerPrivate();
/**
* Updates the currently shown outline.
*/
void updateOutline();
/**
* Updates the current highlight window state
*/
void updateHighlightWindows();
/**
* Ends window highlighting
*/
void endHighlightWindows(bool abort = false);
ClientModel* clientModel() const;
DesktopModel* desktopModel() const;
TabBoxHandler *q; // public pointer
// members
TabBoxConfig config;
DeclarativeView *m_declarativeView;
DeclarativeView *m_declarativeDesktopView;
ClientModel* m_clientModel;
DesktopModel* m_desktopModel;
2011-01-30 14:34:42 +00:00
QModelIndex index;
/**
* Indicates if the tabbox is shown.
* Used to determine if the outline has to be updated, etc.
*/
bool isShown;
TabBoxClient *lastRaisedClient, *lastRaisedClientSucc;
WId m_embedded;
QPoint m_embeddedOffset;
QSize m_embeddedSize;
Qt::Alignment m_embeddedAlignment;
2011-01-30 14:34:42 +00:00
};
TabBoxHandlerPrivate::TabBoxHandlerPrivate(TabBoxHandler *q)
: m_declarativeView(NULL)
, m_declarativeDesktopView(NULL)
, m_embedded(0)
, m_embeddedOffset(QPoint(0, 0))
, m_embeddedSize(QSize(0, 0))
2011-01-30 14:34:42 +00:00
{
this->q = q;
isShown = false;
lastRaisedClient = 0;
lastRaisedClientSucc = 0;
config = TabBoxConfig();
m_clientModel = new ClientModel(q);
m_desktopModel = new DesktopModel(q);
2011-01-30 14:34:42 +00:00
}
TabBoxHandlerPrivate::~TabBoxHandlerPrivate()
2011-01-30 14:34:42 +00:00
{
delete m_declarativeView;
delete m_declarativeDesktopView;
2011-01-30 14:34:42 +00:00
}
ClientModel* TabBoxHandlerPrivate::clientModel() const
2011-01-30 14:34:42 +00:00
{
return m_clientModel;
2011-01-30 14:34:42 +00:00
}
DesktopModel* TabBoxHandlerPrivate::desktopModel() const
2011-01-30 14:34:42 +00:00
{
return m_desktopModel;
2011-01-30 14:34:42 +00:00
}
void TabBoxHandlerPrivate::updateOutline()
2011-01-30 14:34:42 +00:00
{
if (config.tabBoxMode() != TabBoxConfig::ClientTabBox)
return;
2011-01-30 14:34:42 +00:00
// if ( c == NULL || !m_isShown || !c->isShown( true ) || !c->isOnCurrentDesktop())
if (!isShown) {
q->hideOutline();
return;
2011-01-30 14:34:42 +00:00
}
TabBoxClient* c = static_cast< TabBoxClient* >(m_clientModel->data(index, ClientModel::ClientRole).value<void *>());
q->showOutline(QRect(c->x(), c->y(), c->width(), c->height()));
2011-01-30 14:34:42 +00:00
}
void TabBoxHandlerPrivate::updateHighlightWindows()
2011-01-30 14:34:42 +00:00
{
if (!isShown || config.tabBoxMode() != TabBoxConfig::ClientTabBox)
return;
Display *dpy = QX11Info::display();
2011-01-30 14:34:42 +00:00
TabBoxClient *currentClient = q->client(index);
QWidget *w = NULL;
if (m_declarativeView && m_declarativeView->isVisible()) {
w = m_declarativeView;
}
2011-01-30 14:34:42 +00:00
if (KWindowSystem::compositingActive()) {
if (lastRaisedClient)
q->elevateClient(lastRaisedClient, m_declarativeView ? m_declarativeView->winId() : 0, false);
lastRaisedClient = currentClient;
if (currentClient)
q->elevateClient(currentClient, m_declarativeView ? m_declarativeView->winId() : 0, true);
} else {
2011-01-30 14:34:42 +00:00
if (lastRaisedClient) {
if (lastRaisedClientSucc)
q->restack(lastRaisedClient, lastRaisedClientSucc);
// TODO lastRaisedClient->setMinimized( lastRaisedClientWasMinimized );
}
lastRaisedClient = currentClient;
2011-01-30 14:34:42 +00:00
if (lastRaisedClient) {
// TODO if ( (lastRaisedClientWasMinimized = lastRaisedClient->isMinimized()) )
// lastRaisedClient->setMinimized( false );
TabBoxClientList order = q->stackingOrder();
2011-01-30 14:34:42 +00:00
int succIdx = order.indexOf(lastRaisedClient) + 1; // this is likely related to the index parameter?!
lastRaisedClientSucc = (succIdx < order.count()) ? order.at(succIdx) : 0;
q->raiseClient(lastRaisedClient);
}
2011-01-30 14:34:42 +00:00
}
WId wId;
QVector< WId > data;
if (config.isShowTabBox() && w) {
wId = w->winId();
data.resize(2);
data[ 1 ] = wId;
2011-01-30 14:34:42 +00:00
} else {
wId = QX11Info::appRootWindow();
data.resize(1);
}
data[ 0 ] = currentClient ? currentClient->window() : 0L;
2011-01-30 14:34:42 +00:00
if (config.isShowOutline()) {
QVector<Window> outlineWindows = q->outlineWindowIds();
data.resize(2+outlineWindows.size());
for (int i=0; i<outlineWindows.size(); ++i) {
data[2+i] = outlineWindows[i];
}
2011-01-30 14:34:42 +00:00
}
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_HIGHLIGHT", False);
XChangeProperty(dpy, wId, atom, atom, 32, PropModeReplace,
reinterpret_cast<unsigned char *>(data.data()), data.size());
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TabBoxHandlerPrivate::endHighlightWindows(bool abort)
{
TabBoxClient *currentClient = q->client(index);
if (currentClient)
q->elevateClient(currentClient, m_declarativeView ? m_declarativeView->winId() : 0, false);
2011-01-30 14:34:42 +00:00
if (abort && lastRaisedClient && lastRaisedClientSucc)
q->restack(lastRaisedClient, lastRaisedClientSucc);
lastRaisedClient = 0;
lastRaisedClientSucc = 0;
// highlight windows
Display *dpy = QX11Info::display();
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_HIGHLIGHT", False);
XDeleteProperty(dpy, config.isShowTabBox() && m_declarativeView ? m_declarativeView->winId() : QX11Info::appRootWindow(), atom);
2011-01-30 14:34:42 +00:00
}
/***********************************************
* TabBoxHandler
***********************************************/
TabBoxHandler::TabBoxHandler()
: QObject()
2011-01-30 14:34:42 +00:00
{
KWin::TabBox::tabBox = this;
2011-01-30 14:34:42 +00:00
d = new TabBoxHandlerPrivate(this);
}
TabBoxHandler::~TabBoxHandler()
2011-01-30 14:34:42 +00:00
{
delete d;
2011-01-30 14:34:42 +00:00
}
const KWin::TabBox::TabBoxConfig& TabBoxHandler::config() const
2011-01-30 14:34:42 +00:00
{
return d->config;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TabBoxHandler::setConfig(const TabBoxConfig& config)
{
d->config = config;
emit configChanged();
2011-01-30 14:34:42 +00:00
}
void TabBoxHandler::show()
2011-01-30 14:34:42 +00:00
{
d->isShown = true;
d->lastRaisedClient = 0;
d->lastRaisedClientSucc = 0;
// show the outline
2011-01-30 14:34:42 +00:00
if (d->config.isShowOutline()) {
d->updateOutline();
2011-01-30 14:34:42 +00:00
}
if (d->config.isShowTabBox()) {
if (d->config.tabBoxMode() == TabBoxConfig::ClientTabBox) {
// use declarative view
if (!d->m_declarativeView) {
d->m_declarativeView = new DeclarativeView(d->clientModel(), TabBoxConfig::ClientTabBox);
}
d->m_declarativeView->show();
d->m_declarativeView->setCurrentIndex(d->index, true);
} else {
if (!d->m_declarativeDesktopView) {
d->m_declarativeDesktopView = new DeclarativeView(d->desktopModel(), TabBoxConfig::DesktopTabBox);
}
d->m_declarativeDesktopView->show();
d->m_declarativeDesktopView->setCurrentIndex(d->index);
}
2011-01-30 14:34:42 +00:00
}
if (d->config.isHighlightWindows()) {
XSync(QX11Info::display(), false);
// TODO this should be
// QMetaObject::invokeMethod(this, "updateHighlightWindows", Qt::QueuedConnection);
// but we somehow need to cross > 1 event cycle (likely because of queued invocation in the effects)
// to ensure the EffectWindow is present when updateHighlightWindows, thus elevating the window/tabbox
QTimer::singleShot(1, this, SLOT(updateHighlightWindows()));
}
2011-01-30 14:34:42 +00:00
}
void TabBoxHandler::updateHighlightWindows()
{
d->updateHighlightWindows();
}
2011-01-30 14:34:42 +00:00
void TabBoxHandler::hide(bool abort)
{
d->isShown = false;
2011-01-30 14:34:42 +00:00
if (d->config.isHighlightWindows()) {
d->endHighlightWindows(abort);
}
if (d->config.isShowOutline()) {
hideOutline();
}
if (d->m_declarativeView) {
d->m_declarativeView->hide();
}
if (d->m_declarativeDesktopView) {
d->m_declarativeDesktopView->hide();
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QModelIndex TabBoxHandler::nextPrev(bool forward) const
{
QModelIndex ret;
QAbstractItemModel* model;
2011-01-30 14:34:42 +00:00
switch(d->config.tabBoxMode()) {
case TabBoxConfig::ClientTabBox:
model = d->clientModel();
break;
case TabBoxConfig::DesktopTabBox:
model = d->desktopModel();
break;
default:
return d->index;
}
if (forward) {
int column = d->index.column() + 1;
int row = d->index.row();
2011-01-30 14:34:42 +00:00
if (column == model->columnCount()) {
column = 0;
row++;
2011-01-30 14:34:42 +00:00
if (row == model->rowCount())
row = 0;
}
2011-01-30 14:34:42 +00:00
ret = model->index(row, column);
if (!ret.isValid())
ret = model->index(0, 0);
} else {
int column = d->index.column() - 1;
int row = d->index.row();
2011-01-30 14:34:42 +00:00
if (column < 0) {
column = model->columnCount() - 1;
row--;
2011-01-30 14:34:42 +00:00
if (row < 0)
row = model->rowCount() - 1;
2011-01-30 14:34:42 +00:00
}
ret = model->index(row, column);
if (!ret.isValid()) {
row = model->rowCount() - 1;
2011-01-30 14:34:42 +00:00
for (int i = model->columnCount() - 1; i >= 0; i--) {
ret = model->index(row, i);
if (ret.isValid())
break;
}
}
2011-01-30 14:34:42 +00:00
}
if (ret.isValid())
return ret;
else
return d->index;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QModelIndex TabBoxHandler::desktopIndex(int desktop) const
{
if (d->config.tabBoxMode() != TabBoxConfig::DesktopTabBox)
return QModelIndex();
2011-01-30 14:34:42 +00:00
return d->desktopModel()->desktopIndex(desktop);
}
QList< int > TabBoxHandler::desktopList() const
2011-01-30 14:34:42 +00:00
{
if (d->config.tabBoxMode() != TabBoxConfig::DesktopTabBox)
return QList< int >();
return d->desktopModel()->desktopList();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
int TabBoxHandler::desktop(const QModelIndex& index) const
{
if (!index.isValid() || (d->config.tabBoxMode() != TabBoxConfig::DesktopTabBox))
return -1;
2011-01-30 14:34:42 +00:00
QVariant ret = d->desktopModel()->data(index, DesktopModel::DesktopRole);
if (ret.isValid())
return ret.toInt();
else
return -1;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TabBoxHandler::setCurrentIndex(const QModelIndex& index)
{
if (d->index == index) {
return;
}
if (!index.isValid()) {
return;
}
if (d->m_declarativeView) {
d->m_declarativeView->setCurrentIndex(index);
}
if (d->m_declarativeDesktopView) {
d->m_declarativeDesktopView->setCurrentIndex(index);
}
d->index = index;
2011-01-30 14:34:42 +00:00
if (d->config.tabBoxMode() == TabBoxConfig::ClientTabBox) {
if (d->config.isShowOutline()) {
d->updateOutline();
2011-01-30 14:34:42 +00:00
}
if (d->config.isHighlightWindows()) {
d->updateHighlightWindows();
}
}
emit selectedIndexChanged();
2011-01-30 14:34:42 +00:00
}
const QModelIndex& TabBoxHandler::currentIndex() const
{
return d->index;
}
void TabBoxHandler::grabbedKeyEvent(QKeyEvent* event) const
2011-01-30 14:34:42 +00:00
{
if (d->m_declarativeView && d->m_declarativeView->isVisible()) {
d->m_declarativeView->sendKeyEvent(event);
} else if (d->m_declarativeDesktopView && d->m_declarativeDesktopView->isVisible()) {
d->m_declarativeDesktopView->sendKeyEvent(event);
2011-01-30 14:34:42 +00:00
}
}
2011-01-30 14:34:42 +00:00
bool TabBoxHandler::containsPos(const QPoint& pos) const
{
QWidget *w = NULL;
if (d->m_declarativeView && d->m_declarativeView->isVisible()) {
w = d->m_declarativeView;
} else if (d->m_declarativeDesktopView && d->m_declarativeDesktopView->isVisible()) {
w = d->m_declarativeDesktopView;
} else {
return false;
}
return w->geometry().contains(pos);
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QModelIndex TabBoxHandler::index(KWin::TabBox::TabBoxClient* client) const
{
return d->clientModel()->index(client);
}
TabBoxClientList TabBoxHandler::clientList() const
2011-01-30 14:34:42 +00:00
{
if (d->config.tabBoxMode() != TabBoxConfig::ClientTabBox)
return TabBoxClientList();
return d->clientModel()->clientList();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
TabBoxClient* TabBoxHandler::client(const QModelIndex& index) const
{
if ((!index.isValid()) ||
(d->config.tabBoxMode() != TabBoxConfig::ClientTabBox))
return NULL;
TabBoxClient* c = static_cast< TabBoxClient* >(
2011-01-30 14:34:42 +00:00
d->clientModel()->data(index, ClientModel::ClientRole).value<void *>());
return c;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TabBoxHandler::createModel(bool partialReset)
{
switch(d->config.tabBoxMode()) {
case TabBoxConfig::ClientTabBox:
d->clientModel()->createClientList(partialReset);
if (d->lastRaisedClient && !stackingOrder().contains(d->lastRaisedClient))
d->lastRaisedClient = 0;
if (d->lastRaisedClientSucc && !stackingOrder().contains(d->lastRaisedClientSucc))
d->lastRaisedClientSucc = 0;
2011-01-30 14:34:42 +00:00
break;
case TabBoxConfig::DesktopTabBox:
d->desktopModel()->createDesktopList();
break;
}
2011-01-30 14:34:42 +00:00
}
QModelIndex TabBoxHandler::first() const
2011-01-30 14:34:42 +00:00
{
QAbstractItemModel* model;
2011-01-30 14:34:42 +00:00
switch(d->config.tabBoxMode()) {
case TabBoxConfig::ClientTabBox:
model = d->clientModel();
break;
case TabBoxConfig::DesktopTabBox:
model = d->desktopModel();
break;
default:
return QModelIndex();
}
2011-01-30 14:34:42 +00:00
return model->index(0, 0);
}
WId TabBoxHandler::embedded() const
{
return d->m_embedded;
}
void TabBoxHandler::setEmbedded(WId wid)
{
d->m_embedded = wid;
emit embeddedChanged(wid != 0);
}
void TabBoxHandler::setEmbeddedOffset(const QPoint &offset)
{
d->m_embeddedOffset = offset;
}
void TabBoxHandler::setEmbeddedSize(const QSize &size)
{
d->m_embeddedSize = size;
}
const QPoint &TabBoxHandler::embeddedOffset() const
{
return d->m_embeddedOffset;
}
const QSize &TabBoxHandler::embeddedSize() const
{
return d->m_embeddedSize;
}
Qt::Alignment TabBoxHandler::embeddedAlignment() const
{
return d->m_embeddedAlignment;
}
void TabBoxHandler::setEmbeddedAlignment(Qt::Alignment alignment)
{
d->m_embeddedAlignment = alignment;
}
void TabBoxHandler::resetEmbedded()
{
if (d->m_embedded == 0) {
return;
}
d->m_embedded = 0;
d->m_embeddedOffset = QPoint(0, 0);
d->m_embeddedSize = QSize(0, 0);
emit embeddedChanged(false);
}
TabBoxHandler* tabBox = 0;
TabBoxClient::TabBoxClient()
2011-01-30 14:34:42 +00:00
{
}
TabBoxClient::~TabBoxClient()
2011-01-30 14:34:42 +00:00
{
}
} // namespace TabBox
} // namespace KWin