kwin/effects/desktopgrid/desktopgrid.cpp

1469 lines
59 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2007 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2008 Lucas Murray <lmurray@undefinedfire.com>
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/>.
*********************************************************************/
#include "desktopgrid.h"
// KConfigSkeleton
#include "desktopgridconfig.h"
#include "../presentwindows/presentwindows_proxy.h"
#include <math.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <kdebug.h>
#include <klocale.h>
#include <netwm_def.h>
#include <QEvent>
#include <QMouseEvent>
#include <kglobalsettings.h>
#include <QtGui/QPainter>
#include <QtGui/QGraphicsLinearLayout>
#include <QtGui/QVector2D>
#include <Plasma/FrameSvg>
#include <Plasma/PushButton>
#include <Plasma/WindowEffects>
namespace KWin
{
// WARNING, TODO: This effect relies on the desktop layout being EWMH-compliant.
2011-01-30 14:34:42 +00:00
KWIN_EFFECT(desktopgrid, DesktopGridEffect)
DesktopGridEffect::DesktopGridEffect()
2011-01-30 14:34:42 +00:00
: activated(false)
, timeline()
2011-01-30 14:34:42 +00:00
, keyboardGrab(false)
, wasWindowMove(false)
, wasDesktopMove(false)
, isValidMove(false)
2011-01-30 14:34:42 +00:00
, windowMove(NULL)
, windowMoveDiff()
, gridSize()
2011-01-30 14:34:42 +00:00
, orientation(Qt::Horizontal)
, activeCell(1, 1)
, scale()
, unscaledBorder()
, scaledSize()
, scaledOffset()
2011-01-30 14:34:42 +00:00
, m_proxy(0)
{
// Load shortcuts
2011-01-30 14:34:42 +00:00
KActionCollection* actionCollection = new KActionCollection(this);
KAction* a = (KAction*) actionCollection->addAction("ShowDesktopGrid");
a->setText(i18n("Show Desktop Grid"));
a->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::Key_F8));
shortcut = a->globalShortcut();
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggle()));
connect(a, SIGNAL(globalShortcutChanged(QKeySequence)), this, SLOT(globalShortcutChanged(QKeySequence)));
connect(effects, SIGNAL(windowAdded(KWin::EffectWindow*)), this, SLOT(slotWindowAdded(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowDeleted(KWin::EffectWindow*)), this, SLOT(slotWindowDeleted(KWin::EffectWindow*)));
connect(effects, SIGNAL(numberDesktopsChanged(uint)), this, SLOT(slotNumberDesktopsChanged(uint)));
connect(effects, SIGNAL(windowGeometryShapeChanged(KWin::EffectWindow*,QRect)), this, SLOT(slotWindowGeometryShapeChanged(KWin::EffectWindow*,QRect)));
// Load all other configuration details
2011-01-30 14:34:42 +00:00
reconfigure(ReconfigureAll);
}
DesktopGridEffect::~DesktopGridEffect()
2011-01-30 14:34:42 +00:00
{
foreach (ElectricBorder border, borderActivate) {
effects->unreserveElectricBorder(border);
}
QHash< DesktopButtonsView*, EffectWindow* >::iterator i = m_desktopButtonsViews.begin();
while (i != m_desktopButtonsViews.end()) {
DesktopButtonsView *view = i.key();
i = m_desktopButtonsViews.erase(i);
view->deleteLater();
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::reconfigure(ReconfigureFlags)
{
DesktopGridConfig::self()->readConfig();
2011-01-30 14:34:42 +00:00
foreach (ElectricBorder border, borderActivate) {
effects->unreserveElectricBorder(border);
}
borderActivate.clear();
foreach (int i, DesktopGridConfig::borderActivate()) {
2011-01-30 14:34:42 +00:00
borderActivate.append(ElectricBorder(i));
effects->reserveElectricBorder(ElectricBorder(i));
}
// TODO: rename zoomDuration to duration
zoomDuration = animationTime(DesktopGridConfig::zoomDuration() != 0 ? DesktopGridConfig::zoomDuration() : 300);
timeline.setCurveShape(QTimeLine::EaseInOutCurve);
2011-01-30 14:34:42 +00:00
timeline.setDuration(zoomDuration);
border = DesktopGridConfig::borderWidth();
desktopNameAlignment = Qt::Alignment(DesktopGridConfig::desktopNameAlignment());
layoutMode = DesktopGridConfig::layoutMode();
customLayoutRows = DesktopGridConfig::customLayoutRows();
m_usePresentWindows = DesktopGridConfig::presentWindows();
2011-01-30 14:34:42 +00:00
}
//-----------------------------------------------------------------------------
// Screen painting
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::prePaintScreen(ScreenPrePaintData& data, int time)
{
if (timeline.currentValue() != 0 || activated || (isUsingPresentWindows() && isMotionManagerMovingWindows())) {
2011-01-30 14:34:42 +00:00
if (activated)
timeline.setCurrentTime(timeline.currentTime() + time);
else
timeline.setCurrentTime(timeline.currentTime() - time);
2011-01-30 14:34:42 +00:00
for (int i = 0; i < effects->numberOfDesktops(); i++) {
if (i == highlightedDesktop - 1)
hoverTimeline[i]->setCurrentTime(hoverTimeline[i]->currentTime() + time);
else
hoverTimeline[i]->setCurrentTime(hoverTimeline[i]->currentTime() - time);
2011-01-30 14:34:42 +00:00
}
if (isUsingPresentWindows()) {
QList<WindowMotionManager>::iterator i;
2011-01-30 14:34:42 +00:00
for (i = m_managers.begin(); i != m_managers.end(); ++i)
(*i).calculate(time);
}
// PAINT_SCREEN_BACKGROUND_FIRST is needed because screen will be actually painted more than once,
// so with normal screen painting second screen paint would erase parts of the first paint
if (timeline.currentValue() != 0 || (isUsingPresentWindows() && isMotionManagerMovingWindows()))
data.mask |= PAINT_SCREEN_TRANSFORMED | PAINT_SCREEN_BACKGROUND_FIRST;
if (!activated && timeline.currentValue() == 0 && !(isUsingPresentWindows() && isMotionManagerMovingWindows()))
finish();
}
2011-01-30 14:34:42 +00:00
effects->prePaintScreen(data, time);
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
if (timeline.currentValue() == 0 && !isUsingPresentWindows()) {
2011-01-30 14:34:42 +00:00
effects->paintScreen(mask, region, data);
return;
2011-01-30 14:34:42 +00:00
}
for (int desktop = 1; desktop <= effects->numberOfDesktops(); desktop++) {
ScreenPaintData d = data;
paintingDesktop = desktop;
2011-01-30 14:34:42 +00:00
effects->paintScreen(mask, region, d);
}
// paint the add desktop button
2011-01-30 14:34:42 +00:00
for (QHash< DesktopButtonsView*, EffectWindow*>::iterator it = m_desktopButtonsViews.begin();
it != m_desktopButtonsViews.end(); ++it) {
if (!it.value()) {
EffectWindow *view = effects->findWindow(it.key()->winId());
if (view) {
view->setData(WindowForceBlurRole, QVariant(true));
it.value() = view;
}
2011-01-30 14:34:42 +00:00
}
if (it.value()) {
WindowPaintData d(it.value());
d.multiplyOpacity(timeline.currentValue());
2011-01-30 14:34:42 +00:00
effects->drawWindow(it.value(), PAINT_WINDOW_TRANSLUCENT,
infiniteRegion(), d);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows() && windowMove && wasWindowMove) {
// the moving window has to be painted on top of all desktops
QPoint diff = cursorPos() - m_windowMoveStartPoint;
2011-01-30 14:34:42 +00:00
QRect geo = m_windowMoveGeometry.translated(diff);
WindowPaintData d(windowMove);
d *= QVector2D((qreal)geo.width() / (qreal)windowMove->width(), (qreal)geo.height() / (qreal)windowMove->height());
d += QPoint(qRound(geo.left() - windowMove->x()), qRound(geo.top() - windowMove->y()));
2011-01-30 14:34:42 +00:00
effects->drawWindow(windowMove, PAINT_WINDOW_TRANSFORMED | PAINT_WINDOW_LANCZOS, infiniteRegion(), d);
}
2011-01-30 14:34:42 +00:00
if (desktopNameAlignment) {
for (int screen = 0; screen < effects->numScreens(); screen++) {
QRect screenGeom = effects->clientArea(ScreenArea, screen, 0);
int desktop = 1;
2011-01-30 14:34:42 +00:00
foreach (EffectFrame * frame, desktopNames) {
QPointF posTL(scalePos(screenGeom.topLeft(), desktop, screen));
QPointF posBR(scalePos(screenGeom.bottomRight(), desktop, screen));
QRect textArea(posTL.x(), posTL.y(), posBR.x() - posTL.x(), posBR.y() - posTL.y());
textArea.adjust(textArea.width() / 10, textArea.height() / 10,
-textArea.width() / 10, -textArea.height() / 10);
int x, y;
2011-01-30 14:34:42 +00:00
if (desktopNameAlignment & Qt::AlignLeft)
x = textArea.x();
2011-01-30 14:34:42 +00:00
else if (desktopNameAlignment & Qt::AlignRight)
x = textArea.right();
else
x = textArea.center().x();
2011-01-30 14:34:42 +00:00
if (desktopNameAlignment & Qt::AlignTop)
y = textArea.y();
2011-01-30 14:34:42 +00:00
else if (desktopNameAlignment & Qt::AlignBottom)
y = textArea.bottom();
else
y = textArea.center().y();
2011-01-30 14:34:42 +00:00
frame->setPosition(QPoint(x, y));
frame->render(region, timeline.currentValue(), 0.7);
++desktop;
}
}
}
2011-01-30 14:34:42 +00:00
}
void DesktopGridEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
if (activated ? timeline.currentValue() != 1 : timeline.currentValue() != 0)
effects->addRepaintFull(); // Repaint during zoom
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows() && isMotionManagerMovingWindows())
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
if (activated) {
for (int i = 0; i < effects->numberOfDesktops(); i++) {
if (hoverTimeline[i]->currentValue() != 0.0 && hoverTimeline[i]->currentValue() != 1.0) {
2011-01-30 14:34:42 +00:00
// Repaint during soft highlighting
effects->addRepaintFull();
break;
}
}
}
2011-01-30 14:34:42 +00:00
effects->postPaintScreen();
}
//-----------------------------------------------------------------------------
// Window painting
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time)
{
if (timeline.currentValue() != 0 || (isUsingPresentWindows() && isMotionManagerMovingWindows())) {
2011-01-30 14:34:42 +00:00
if (w->isOnDesktop(paintingDesktop)) {
w->enablePainting(EffectWindow::PAINT_DISABLED_BY_DESKTOP);
if (w->isMinimized() && isUsingPresentWindows())
w->enablePainting(EffectWindow::PAINT_DISABLED_BY_MINIMIZE);
data.mask |= PAINT_WINDOW_TRANSFORMED;
// Split windows at screen edges
2011-01-30 14:34:42 +00:00
for (int screen = 0; screen < effects->numScreens(); screen++) {
QRect screenGeom = effects->clientArea(ScreenArea, screen, 0);
if (w->x() < screenGeom.x())
data.quads = data.quads.splitAtX(screenGeom.x() - w->x());
if (w->x() + w->width() > screenGeom.x() + screenGeom.width())
data.quads = data.quads.splitAtX(screenGeom.x() + screenGeom.width() - w->x());
if (w->y() < screenGeom.y())
data.quads = data.quads.splitAtY(screenGeom.y() - w->y());
if (w->y() + w->height() > screenGeom.y() + screenGeom.height())
data.quads = data.quads.splitAtY(screenGeom.y() + screenGeom.height() - w->y());
}
2011-01-30 14:34:42 +00:00
if (windowMove && wasWindowMove && windowMove->findModal() == w)
w->disablePainting(EffectWindow::PAINT_DISABLED_BY_DESKTOP);
} else
w->disablePainting(EffectWindow::PAINT_DISABLED_BY_DESKTOP);
}
2011-01-30 14:34:42 +00:00
effects->prePaintWindow(w, data, time);
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
{
if (timeline.currentValue() != 0 || (isUsingPresentWindows() && isMotionManagerMovingWindows())) {
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows() && w == windowMove && wasWindowMove) {
return; // will be painted on top of all other windows
2011-01-30 14:34:42 +00:00
}
if (m_desktopButtonsViews.values().contains(w))
return; // will be painted on top of all other windows
qreal xScale = data.xScale();
qreal yScale = data.yScale();
// Don't change brightness of windows on all desktops as this causes flickering
2011-01-30 14:34:42 +00:00
if (!w->isOnAllDesktops() || w->isDesktop())
data.multiplyBrightness(1.0 - (0.3 * (1.0 - hoverTimeline[paintingDesktop - 1]->currentValue())));
2011-01-30 14:34:42 +00:00
for (int screen = 0; screen < effects->numScreens(); screen++) {
// Assume desktop windows can never be on two screens at once (Plasma makes one window per screen)
2011-01-30 14:34:42 +00:00
if (w->isDesktop())
screen = w->screen();
2011-01-30 14:34:42 +00:00
QRect screenGeom = effects->clientArea(ScreenArea, screen, 0);
QRectF transformedGeo = w->geometry();
// Display all quads on the same screen on the same pass
WindowQuadList screenQuads;
bool quadsAdded = false;
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
WindowMotionManager& manager = m_managers[(paintingDesktop-1)*(effects->numScreens())+screen ];
if (manager.isManaging(w)) {
foreach (const WindowQuad & quad, data.quads)
screenQuads.append(quad);
transformedGeo = manager.transformedGeometry(w);
quadsAdded = true;
if (!manager.areWindowsMoving() && timeline.currentValue() == 1.0)
mask |= PAINT_WINDOW_LANCZOS;
2011-01-30 14:34:42 +00:00
} else if (w->screen() != screen)
quadsAdded = true; // we don't want parts of overlapping windows on the other screen
2011-01-30 14:34:42 +00:00
}
if (!quadsAdded) {
foreach (const WindowQuad & quad, data.quads) {
QRect quadRect(
w->x() + quad.left(), w->y() + quad.top(),
quad.right() - quad.left(), quad.bottom() - quad.top()
2011-01-30 14:34:42 +00:00
);
if (quadRect.intersects(screenGeom))
screenQuads.append(quad);
}
2011-01-30 14:34:42 +00:00
}
if (screenQuads.isEmpty())
continue; // Nothing is being displayed, don't bother
WindowPaintData d = data;
d.quads = screenQuads;
2011-01-30 14:34:42 +00:00
QPointF newPos = scalePos(transformedGeo.topLeft().toPoint(), paintingDesktop, screen);
double progress = timeline.currentValue();
d.setXScale(interpolate(1, xScale * scale[screen] * (float)transformedGeo.width() / (float)w->geometry().width(), progress));
d.setYScale(interpolate(1, yScale * scale[screen] * (float)transformedGeo.height() / (float)w->geometry().height(), progress));
d += QPoint(qRound(newPos.x() - w->x()), qRound(newPos.y() - w->y()));
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows() && (w->isDock() || w->isSkipSwitcher())) {
// fade out panels if present windows is used
d.multiplyOpacity((1.0 - timeline.currentValue()));
2011-01-30 14:34:42 +00:00
}
if (isUsingPresentWindows() && w->isMinimized()) {
d.multiplyOpacity(timeline.currentValue());
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (effects->compositingType() == XRenderCompositing) {
// More exact clipping as XRender displays the entire window instead of just the quad
QPointF screenPosF = scalePos(screenGeom.topLeft(), paintingDesktop).toPoint();
QPoint screenPos(
2011-01-30 14:34:42 +00:00
qRound(screenPosF.x()),
qRound(screenPosF.y())
);
QSize screenSize(
2011-01-30 14:34:42 +00:00
qRound(interpolate(screenGeom.width(), scaledSize[screen].width(), progress)),
qRound(interpolate(screenGeom.height(), scaledSize[screen].height(), progress))
);
PaintClipper pc(effects->clientArea(ScreenArea, screen, 0) & QRect(screenPos, screenSize));
effects->paintWindow(w, mask, region, d);
} else {
if (w->isDesktop() && timeline.currentValue() == 1.0) {
// desktop windows are not in a motion manager and can always be rendered with
// lanczos sampling except for animations
mask |= PAINT_WINDOW_LANCZOS;
}
effects->paintWindow(w, mask, effects->clientArea(ScreenArea, screen, 0), d);
2011-01-30 14:34:42 +00:00
}
// Assume desktop windows can never be on two screens at once (Plasma makes one window per screen)
2011-01-30 14:34:42 +00:00
if (w->isDesktop())
break;
}
2011-01-30 14:34:42 +00:00
} else
effects->paintWindow(w, mask, region, data);
}
//-----------------------------------------------------------------------------
// User interaction
void DesktopGridEffect::slotWindowAdded(EffectWindow* w)
2011-01-30 14:34:42 +00:00
{
if (!activated)
return;
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
if (!isRelevantWithPresentWindows(w))
return; // don't add
2011-01-30 14:34:42 +00:00
if (w->isOnAllDesktops()) {
for (int i = 0; i < effects->numberOfDesktops(); i++) {
WindowMotionManager& manager = m_managers[ i*effects->numScreens()+w->screen()];
manager.manage(w);
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
2011-01-30 14:34:42 +00:00
} else {
WindowMotionManager& manager = m_managers[(w->desktop()-1)*effects->numScreens()+w->screen()];
manager.manage(w);
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
void DesktopGridEffect::slotWindowClosed(EffectWindow* w)
2011-01-30 14:34:42 +00:00
{
if (!activated && timeline.currentValue() == 0)
return;
2011-01-30 14:34:42 +00:00
if (w == windowMove) {
effects->setElevatedWindow(windowMove, false);
windowMove = NULL;
2011-01-30 14:34:42 +00:00
}
if (isUsingPresentWindows()) {
if (w->isOnAllDesktops()) {
for (int i = 0; i < effects->numberOfDesktops(); i++) {
WindowMotionManager& manager = m_managers[i*effects->numScreens()+w->screen()];
manager.unmanage(w);
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
2011-01-30 14:34:42 +00:00
} else {
if (w->desktop() <= effects->numberOfDesktops()) {
WindowMotionManager& manager = m_managers[(w->desktop()-1)*effects->numScreens()+w->screen()];
manager.unmanage(w);
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
}
2011-01-30 14:34:42 +00:00
}
for (QHash< DesktopButtonsView*, EffectWindow*>::iterator it = m_desktopButtonsViews.begin();
it != m_desktopButtonsViews.end(); ++it) {
if (it.value() && it.value() == w) {
w->refWindow();
break;
}
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
void DesktopGridEffect::slotWindowDeleted(EffectWindow* w)
2011-01-30 14:34:42 +00:00
{
if (w == windowMove)
windowMove = 0;
2011-01-30 14:34:42 +00:00
for (QHash< DesktopButtonsView*, EffectWindow*>::iterator it = m_desktopButtonsViews.begin();
it != m_desktopButtonsViews.end(); ++it) {
if (it.value() && it.value() == w) {
it.key()->deleteLater();
2011-01-30 14:34:42 +00:00
m_desktopButtonsViews.erase(it);
break;
}
}
2011-01-30 14:34:42 +00:00
}
void DesktopGridEffect::slotWindowGeometryShapeChanged(EffectWindow* w, const QRect& old)
2011-01-30 14:34:42 +00:00
{
Q_UNUSED(old)
if (!activated)
return;
2011-01-30 14:34:42 +00:00
if (w == windowMove && wasWindowMove)
return;
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
if (w->isOnAllDesktops()) {
for (int i = 0; i < effects->numberOfDesktops(); i++) {
WindowMotionManager& manager = m_managers[i*effects->numScreens()+w->screen()];
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
2011-01-30 14:34:42 +00:00
} else {
WindowMotionManager& manager = m_managers[(w->desktop()-1)*effects->numScreens()+w->screen()];
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::windowInputMouseEvent(Window, QEvent* e)
{
if ((e->type() != QEvent::MouseMove
&& e->type() != QEvent::MouseButtonPress
&& e->type() != QEvent::MouseButtonRelease)
|| timeline.currentValue() != 1) // Block user input during animations
return;
2011-01-30 14:34:42 +00:00
QMouseEvent* me = static_cast< QMouseEvent* >(e);
for (QHash< DesktopButtonsView*, EffectWindow*>::iterator it = m_desktopButtonsViews.begin();
it != m_desktopButtonsViews.end(); ++it) {
DesktopButtonsView* view = it.key();
2011-01-30 14:34:42 +00:00
if (!wasWindowMove && !wasDesktopMove && view->geometry().contains(me->pos())) {
const QPoint widgetPos = view->mapFromGlobal(me->pos());
QMouseEvent event(me->type(), widgetPos, me->pos(), me->button(), me->buttons(), me->modifiers());
view->windowInputMouseEvent(&event);
return;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (e->type() == QEvent::MouseMove) {
int d = posToDesktop(me->pos());
if (windowMove != NULL &&
(me->pos() - dragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay()) {
// Handle window moving
if (!wasWindowMove) { // Activate on move
if (isUsingPresentWindows() && windowMove->isOnAllDesktops()) {
for (int i = 0; i < effects->numberOfDesktops(); ++i) {
WindowMotionManager& manager = m_managers[(i)*(effects->numScreens()) + windowMove->screen()];
if ((i + 1) == d) {
const QRectF transformedGeo = manager.transformedGeometry(windowMove);
const QPointF pos = scalePos(transformedGeo.topLeft().toPoint(), d, windowMove->screen());
const QSize size(scale[windowMove->screen()] *(float)transformedGeo.width(),
scale[windowMove->screen()] *(float)transformedGeo.height());
m_windowMoveGeometry = QRect(pos.toPoint(), size);
m_windowMoveStartPoint = me->pos();
}
2011-01-30 14:34:42 +00:00
manager.unmanage(windowMove);
if (EffectWindow* modal = windowMove->findModal()) {
if (manager.isManaging(modal))
manager.unmanage(modal);
}
m_proxy->calculateWindowTransformations(manager.managedWindows(), windowMove->screen(), manager);
}
2011-01-30 14:34:42 +00:00
} else if (isUsingPresentWindows()) {
WindowMotionManager& manager = m_managers[(windowMove->desktop()-1)*(effects->numScreens()) + windowMove->screen()];
const QRectF transformedGeo = manager.transformedGeometry(windowMove);
const QPointF pos = scalePos(transformedGeo.topLeft().toPoint(), windowMove->desktop(), windowMove->screen());
const QSize size(scale[windowMove->screen()] *(float)transformedGeo.width(),
scale[windowMove->screen()] *(float)transformedGeo.height());
m_windowMoveGeometry = QRect(pos.toPoint(), size);
m_windowMoveStartPoint = me->pos();
2011-01-30 14:34:42 +00:00
manager.unmanage(windowMove);
if (EffectWindow* modal = windowMove->findModal()) {
if (manager.isManaging(modal))
manager.unmanage(modal);
}
2011-01-30 14:34:42 +00:00
m_proxy->calculateWindowTransformations(manager.managedWindows(), windowMove->screen(), manager);
}
2011-01-30 14:34:42 +00:00
XDefineCursor(display(), input, QCursor(Qt::ClosedHandCursor).handle());
}
wasWindowMove = true;
2011-01-30 14:34:42 +00:00
if (windowMove->isMovable() && !isUsingPresentWindows()) {
int screen = effects->screenNumber(me->pos());
effects->moveWindow(windowMove, unscalePos(me->pos(), NULL) + windowMoveDiff, true, 1.0 / scale[screen]);
}
2011-01-30 14:34:42 +00:00
if (d != highlightedDesktop) {
if (!windowMove->isOnAllDesktops())
effects->windowToDesktop(windowMove, d); // Not true all desktop move
const int screen = effects->screenNumber(me->pos());
if (screen != windowMove->screen())
effects->windowToScreen(windowMove, screen);
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
} else if ((me->buttons() & Qt::LeftButton) && !wasDesktopMove &&
(me->pos() - dragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay()) {
wasDesktopMove = true;
XDefineCursor(display(), input, QCursor(Qt::ClosedHandCursor).handle());
}
if (d != highlightedDesktop) { // Highlight desktop
if ((me->buttons() & Qt::LeftButton) && isValidMove && !wasWindowMove && d <= effects->numberOfDesktops()) {
EffectWindowList windows = effects->stackingOrder();
EffectWindowList stack[3];
for (EffectWindowList::const_iterator it = windows.constBegin(),
end = windows.constEnd(); it != end; ++it) {
EffectWindow *w = const_cast<EffectWindow*>(*it); // we're not really touching it here but below
2011-01-30 14:34:42 +00:00
if (w->isOnAllDesktops())
continue;
if (w->isOnDesktop(highlightedDesktop))
stack[0] << w;
if (w->isOnDesktop(d))
stack[1] << w;
if (w->isOnDesktop(m_originalMovingDesktop))
stack[2] << w;
}
const int desks[4] = {highlightedDesktop, d, m_originalMovingDesktop, highlightedDesktop};
for (int i = 0; i < 3; ++i ) {
if (desks[i] == desks[i+1])
continue;
foreach (EffectWindow *w, stack[i]) {
effects->windowToDesktop(w, desks[i+1]);
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
m_managers[(desks[i]-1)*(effects->numScreens()) + w->screen()].unmanage(w);
m_managers[(desks[i+1]-1)*(effects->numScreens()) + w->screen()].manage(w);
}
}
2011-01-30 14:34:42 +00:00
}
if (isUsingPresentWindows()) {
for (int i = 0; i < effects->numScreens(); i++) {
for (int j = 0; j < 3; ++j) {
WindowMotionManager& manager = m_managers[(desks[j]-1)*(effects->numScreens()) + i ];
m_proxy->calculateWindowTransformations(manager.managedWindows(), i, manager);
}
}
effects->addRepaintFull();
}
}
2011-01-30 14:34:42 +00:00
setHighlightedDesktop(d);
}
2011-01-30 14:34:42 +00:00
}
if (e->type() == QEvent::MouseButtonPress) {
if (me->buttons() == Qt::LeftButton) {
isValidMove = true;
// QRect rect;
dragStartPos = me->pos();
bool isDesktop = (me->modifiers() & Qt::ControlModifier);
2011-01-30 14:34:42 +00:00
EffectWindow* w = isDesktop ? NULL : windowAt(me->pos());
if (w != NULL)
isDesktop = w->isDesktop();
if (isDesktop)
m_originalMovingDesktop = posToDesktop(me->pos());
2011-01-30 14:34:42 +00:00
if (w != NULL && !w->isDesktop() && (w->isMovable() || w->isMovableAcrossScreens() || isUsingPresentWindows())) {
// Prepare it for moving
windowMoveDiff = w->pos() - unscalePos(me->pos(), NULL);
windowMove = w;
2011-01-30 14:34:42 +00:00
effects->setElevatedWindow(windowMove, true);
}
2011-01-30 14:34:42 +00:00
} else if ((me->buttons() == Qt::MidButton || me->buttons() == Qt::RightButton) && windowMove == NULL) {
EffectWindow* w = windowAt(me->pos());
if (w != NULL) {
if (w->isOnAllDesktops()) {
const int desktop = posToDesktop(me->pos());
effects->windowToDesktop(w, desktop);
if (isUsingPresentWindows()) {
for (int i = 0; i < effects->numberOfDesktops(); i++) {
if (i != desktop - 1) {
WindowMotionManager& manager = m_managers[ i*effects->numScreens() + w->screen()];
manager.unmanage(w);
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
}
}
2011-01-30 14:34:42 +00:00
} else {
if (isUsingPresentWindows()) {
const int desktop = w->desktop();
2011-01-30 14:34:42 +00:00
for (int i = 0; i < effects->numberOfDesktops(); i++) {
if (i != desktop - 1) {
WindowMotionManager& manager = m_managers[ i*effects->numScreens() + w->screen()];
manager.manage(w);
m_proxy->calculateWindowTransformations(manager.managedWindows(), w->screen(), manager);
}
}
}
2011-01-30 14:34:42 +00:00
effects->windowToDesktop(w, NET::OnAllDesktops);
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
}
2011-01-30 14:34:42 +00:00
}
if (e->type() == QEvent::MouseButtonRelease && me->button() == Qt::LeftButton) {
isValidMove = false;
2011-01-30 14:34:42 +00:00
if (!wasWindowMove && !wasDesktopMove) {
const int desk = posToDesktop(me->pos());
if (desk > effects->numberOfDesktops())
return; // don't quit when missing desktop
setCurrentDesktop(desk);
2011-01-30 14:34:42 +00:00
if (windowMove)
effects->activateWindow(windowMove);
setActive(false);
}
if (windowMove) {
if (wasWindowMove) {
if (isUsingPresentWindows()) {
if (windowMove->isOnAllDesktops()) {
const int targetDesktop = posToDesktop(cursorPos());
for (int i = 0; i < effects->numberOfDesktops(); ++i) {
WindowMotionManager& manager = m_managers[(i)*(effects->numScreens()) + windowMove->screen()];
manager.manage(windowMove);
if (EffectWindow* modal = windowMove->findModal())
manager.manage(modal);
if (i + 1 == targetDesktop) {
// for the desktop the window is dropped on, we use the current geometry
2011-01-30 14:34:42 +00:00
manager.setTransformedGeometry(windowMove, moveGeometryToDesktop(targetDesktop));
}
2011-01-30 14:34:42 +00:00
m_proxy->calculateWindowTransformations(manager.managedWindows(), windowMove->screen(), manager);
}
2011-01-30 14:34:42 +00:00
} else {
WindowMotionManager& manager = m_managers[(windowMove->desktop()-1)*(effects->numScreens()) + windowMove->screen()];
manager.manage(windowMove);
if (EffectWindow* modal = windowMove->findModal())
manager.manage(modal);
manager.setTransformedGeometry(windowMove, moveGeometryToDesktop(windowMove->desktop()));
m_proxy->calculateWindowTransformations(manager.managedWindows(), windowMove->screen(), manager);
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
else
effects->activateWindow(windowMove);
}
2011-01-30 14:34:42 +00:00
effects->setElevatedWindow(windowMove, false);
windowMove = NULL;
XDefineCursor(display(), input, QCursor(Qt::PointingHandCursor).handle());
} else if (wasDesktopMove)
XDefineCursor(display(), input, QCursor(Qt::PointingHandCursor).handle());
wasWindowMove = false;
wasDesktopMove = false;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::grabbedKeyboardEvent(QKeyEvent* e)
{
if (timeline.currentValue() != 1) // Block user input during animations
return;
2011-01-30 14:34:42 +00:00
if (windowMove != NULL)
return;
2011-01-30 14:34:42 +00:00
if (e->type() == QEvent::KeyPress) {
// check for global shortcuts
// HACK: keyboard grab disables the global shortcuts so we have to check for global shortcut (bug 156155)
2011-01-30 14:34:42 +00:00
if (shortcut.contains(e->key() + e->modifiers())) {
toggle();
return;
2011-01-30 14:34:42 +00:00
}
int desktop = -1;
// switch by F<number> or just <number>
2011-01-30 14:34:42 +00:00
if (e->key() >= Qt::Key_F1 && e->key() <= Qt::Key_F35)
desktop = e->key() - Qt::Key_F1 + 1;
2011-01-30 14:34:42 +00:00
else if (e->key() >= Qt::Key_0 && e->key() <= Qt::Key_9)
desktop = e->key() == Qt::Key_0 ? 10 : e->key() - Qt::Key_0;
2011-01-30 14:34:42 +00:00
if (desktop != -1) {
if (desktop <= effects->numberOfDesktops()) {
setHighlightedDesktop(desktop);
setCurrentDesktop(desktop);
setActive(false);
}
2011-01-30 14:34:42 +00:00
return;
}
switch(e->key()) {
// Wrap only on autorepeat
case Qt::Key_Left:
setHighlightedDesktop(desktopToLeft(highlightedDesktop, !e->isAutoRepeat()));
break;
case Qt::Key_Right:
setHighlightedDesktop(desktopToRight(highlightedDesktop, !e->isAutoRepeat()));
break;
case Qt::Key_Up:
setHighlightedDesktop(desktopUp(highlightedDesktop, !e->isAutoRepeat()));
break;
case Qt::Key_Down:
setHighlightedDesktop(desktopDown(highlightedDesktop, !e->isAutoRepeat()));
break;
case Qt::Key_Escape:
setActive(false);
return;
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Space:
setCurrentDesktop(highlightedDesktop);
setActive(false);
return;
case Qt::Key_Plus:
slotAddDesktop();
break;
case Qt::Key_Minus:
slotRemoveDesktop();
break;
default:
break;
}
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
bool DesktopGridEffect::borderActivated(ElectricBorder border)
{
if (!borderActivate.contains(border))
return false;
2011-01-30 14:34:42 +00:00
if (effects->activeFullScreenEffect() && effects->activeFullScreenEffect() != this)
return true;
toggle();
return true;
2011-01-30 14:34:42 +00:00
}
//-----------------------------------------------------------------------------
// Helper functions
// Transform a point to its position on the scaled grid
2011-01-30 14:34:42 +00:00
QPointF DesktopGridEffect::scalePos(const QPoint& pos, int desktop, int screen) const
{
if (screen == -1)
screen = effects->screenNumber(pos);
QRect screenGeom = effects->clientArea(ScreenArea, screen, 0);
QPoint desktopCell;
2011-01-30 14:34:42 +00:00
if (orientation == Qt::Horizontal) {
desktopCell.setX((desktop - 1) % gridSize.width() + 1);
desktopCell.setY((desktop - 1) / gridSize.width() + 1);
} else {
desktopCell.setX((desktop - 1) / gridSize.height() + 1);
desktopCell.setY((desktop - 1) % gridSize.height() + 1);
}
double progress = timeline.currentValue();
QPointF point(
interpolate(
(
2011-01-30 14:34:42 +00:00
(screenGeom.width() + unscaledBorder[screen]) *(desktopCell.x() - 1)
- (screenGeom.width() + unscaledBorder[screen]) *(activeCell.x() - 1)
) + pos.x(),
(
2011-01-30 14:34:42 +00:00
(scaledSize[screen].width() + border) *(desktopCell.x() - 1)
+ scaledOffset[screen].x()
2011-01-30 14:34:42 +00:00
+ (pos.x() - screenGeom.x()) * scale[screen]
),
2011-01-30 14:34:42 +00:00
progress),
interpolate(
(
2011-01-30 14:34:42 +00:00
(screenGeom.height() + unscaledBorder[screen]) *(desktopCell.y() - 1)
- (screenGeom.height() + unscaledBorder[screen]) *(activeCell.y() - 1)
) + pos.y(),
(
2011-01-30 14:34:42 +00:00
(scaledSize[screen].height() + border) *(desktopCell.y() - 1)
+ scaledOffset[screen].y()
2011-01-30 14:34:42 +00:00
+ (pos.y() - screenGeom.y()) * scale[screen]
),
2011-01-30 14:34:42 +00:00
progress)
);
return point;
2011-01-30 14:34:42 +00:00
}
// Detransform a point to its position on the full grid
// TODO: Doesn't correctly interpolate (Final position is correct though), don't forget to copy to posToDesktop()
2011-01-30 14:34:42 +00:00
QPoint DesktopGridEffect::unscalePos(const QPoint& pos, int* desktop) const
{
int screen = effects->screenNumber(pos);
QRect screenGeom = effects->clientArea(ScreenArea, screen, 0);
//double progress = timeline.currentValue();
double scaledX = /*interpolate(
( pos.x() - screenGeom.x() + unscaledBorder[screen] / 2.0 ) / ( screenGeom.width() + unscaledBorder[screen] ) + activeCell.x() - 1,*/
2011-01-30 14:34:42 +00:00
(pos.x() - scaledOffset[screen].x() + double(border) / 2.0) / (scaledSize[screen].width() + border)/*,
progress )*/;
double scaledY = /*interpolate(
( pos.y() - screenGeom.y() + unscaledBorder[screen] / 2.0 ) / ( screenGeom.height() + unscaledBorder[screen] ) + activeCell.y() - 1,*/
2011-01-30 14:34:42 +00:00
(pos.y() - scaledOffset[screen].y() + double(border) / 2.0) / (scaledSize[screen].height() + border)/*,
progress )*/;
2011-01-30 14:34:42 +00:00
int gx = qBound(0, int(scaledX), gridSize.width() - 1); // Zero-based
int gy = qBound(0, int(scaledY), gridSize.height() - 1);
scaledX -= gx;
scaledY -= gy;
2011-01-30 14:34:42 +00:00
if (desktop != NULL) {
if (orientation == Qt::Horizontal)
*desktop = gy * gridSize.width() + gx + 1;
else
*desktop = gx * gridSize.height() + gy + 1;
2011-01-30 14:34:42 +00:00
}
return QPoint(
2011-01-30 14:34:42 +00:00
qBound(
screenGeom.x(),
qRound(
scaledX * (screenGeom.width() + unscaledBorder[screen])
- unscaledBorder[screen] / 2.0
+ screenGeom.x()
),
screenGeom.right()
),
qBound(
screenGeom.y(),
qRound(
scaledY * (screenGeom.height() + unscaledBorder[screen])
- unscaledBorder[screen] / 2.0
+ screenGeom.y()
),
screenGeom.bottom()
)
);
}
int DesktopGridEffect::posToDesktop(const QPoint& pos) const
{
// Copied from unscalePos()
int screen = effects->screenNumber(pos);
double scaledX = (pos.x() - scaledOffset[screen].x() + double(border) / 2.0) / (scaledSize[screen].width() + border);
double scaledY = (pos.y() - scaledOffset[screen].y() + double(border) / 2.0) / (scaledSize[screen].height() + border);
2011-01-30 14:34:42 +00:00
int gx = qBound(0, int(scaledX), gridSize.width() - 1); // Zero-based
int gy = qBound(0, int(scaledY), gridSize.height() - 1);
scaledX -= gx;
scaledY -= gy;
2011-01-30 14:34:42 +00:00
if (orientation == Qt::Horizontal)
return gy * gridSize.width() + gx + 1;
return gx * gridSize.height() + gy + 1;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
EffectWindow* DesktopGridEffect::windowAt(QPoint pos) const
{
// Get stacking order top first
EffectWindowList windows = effects->stackingOrder();
EffectWindowList::Iterator begin = windows.begin();
EffectWindowList::Iterator end = windows.end();
--end;
2011-01-30 14:34:42 +00:00
while (begin < end)
qSwap(*begin++, *end--);
int desktop;
2011-01-30 14:34:42 +00:00
pos = unscalePos(pos, &desktop);
if (desktop > effects->numberOfDesktops())
return NULL;
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
const int screen = effects->screenNumber(pos);
EffectWindow *w =
2011-01-30 14:34:42 +00:00
m_managers.at((desktop - 1) * (effects->numScreens()) + screen).windowAtPoint(pos, false);
if (w)
return w;
2011-01-30 14:34:42 +00:00
foreach (EffectWindow * w, windows) {
if (w->isOnDesktop(desktop) && w->isDesktop() && w->geometry().contains(pos))
return w;
}
2011-01-30 14:34:42 +00:00
} else {
foreach (EffectWindow * w, windows) {
if (w->isOnDesktop(desktop) && !w->isMinimized() && w->geometry().contains(pos))
return w;
}
}
2011-01-30 14:34:42 +00:00
return NULL;
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::setCurrentDesktop(int desktop)
{
if (orientation == Qt::Horizontal) {
activeCell.setX((desktop - 1) % gridSize.width() + 1);
activeCell.setY((desktop - 1) / gridSize.width() + 1);
} else {
activeCell.setX((desktop - 1) / gridSize.height() + 1);
activeCell.setY((desktop - 1) % gridSize.height() + 1);
}
2011-01-30 14:34:42 +00:00
if (effects->currentDesktop() != desktop)
effects->setCurrentDesktop(desktop);
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::setHighlightedDesktop(int d)
{
if (d == highlightedDesktop || d <= 0 || d > effects->numberOfDesktops())
return;
highlightedDesktop = d;
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
int DesktopGridEffect::desktopToRight(int desktop, bool wrap) const
{
// Copied from Workspace::desktopToRight()
int dt = desktop - 1;
2011-01-30 14:34:42 +00:00
if (orientation == Qt::Vertical) {
dt += gridSize.height();
2011-01-30 14:34:42 +00:00
if (dt >= effects->numberOfDesktops()) {
if (wrap)
dt -= effects->numberOfDesktops();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
} else {
int d = (dt % gridSize.width()) + 1;
if (d >= gridSize.width()) {
if (wrap)
d -= gridSize.width();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
dt = dt - (dt % gridSize.width()) + d;
}
2011-01-30 14:34:42 +00:00
return dt + 1;
}
2011-01-30 14:34:42 +00:00
int DesktopGridEffect::desktopToLeft(int desktop, bool wrap) const
{
// Copied from Workspace::desktopToLeft()
int dt = desktop - 1;
2011-01-30 14:34:42 +00:00
if (orientation == Qt::Vertical) {
dt -= gridSize.height();
2011-01-30 14:34:42 +00:00
if (dt < 0) {
if (wrap)
dt += effects->numberOfDesktops();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
} else {
int d = (dt % gridSize.width()) - 1;
if (d < 0) {
if (wrap)
d += gridSize.width();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
dt = dt - (dt % gridSize.width()) + d;
}
2011-01-30 14:34:42 +00:00
return dt + 1;
}
2011-01-30 14:34:42 +00:00
int DesktopGridEffect::desktopUp(int desktop, bool wrap) const
{
// Copied from Workspace::desktopUp()
int dt = desktop - 1;
2011-01-30 14:34:42 +00:00
if (orientation == Qt::Horizontal) {
dt -= gridSize.width();
2011-01-30 14:34:42 +00:00
if (dt < 0) {
if (wrap)
dt += effects->numberOfDesktops();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
} else {
int d = (dt % gridSize.height()) - 1;
if (d < 0) {
if (wrap)
d += gridSize.height();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
dt = dt - (dt % gridSize.height()) + d;
}
2011-01-30 14:34:42 +00:00
return dt + 1;
}
2011-01-30 14:34:42 +00:00
int DesktopGridEffect::desktopDown(int desktop, bool wrap) const
{
// Copied from Workspace::desktopDown()
int dt = desktop - 1;
2011-01-30 14:34:42 +00:00
if (orientation == Qt::Horizontal) {
dt += gridSize.width();
2011-01-30 14:34:42 +00:00
if (dt >= effects->numberOfDesktops()) {
if (wrap)
dt -= effects->numberOfDesktops();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
} else {
int d = (dt % gridSize.height()) + 1;
if (d >= gridSize.height()) {
if (wrap)
d -= gridSize.height();
else
return desktop;
}
2011-01-30 14:34:42 +00:00
dt = dt - (dt % gridSize.height()) + d;
}
2011-01-30 14:34:42 +00:00
return dt + 1;
}
//-----------------------------------------------------------------------------
// Activation
void DesktopGridEffect::toggle()
2011-01-30 14:34:42 +00:00
{
setActive(!activated);
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::setActive(bool active)
{
if (effects->activeFullScreenEffect() && effects->activeFullScreenEffect() != this)
return; // Only one fullscreen effect at a time thanks
2011-01-30 14:34:42 +00:00
if (active && isMotionManagerMovingWindows())
return; // Still moving windows from last usage - don't activate
2011-01-30 14:34:42 +00:00
if (activated == active)
return; // Already in that state
activated = active;
if (activated && timeline.currentValue() == 0)
setup();
2011-01-30 14:34:42 +00:00
if (!activated) {
if (isUsingPresentWindows()) {
QList<WindowMotionManager>::iterator it;
2011-01-30 14:34:42 +00:00
for (it = m_managers.begin(); it != m_managers.end(); ++it) {
foreach (EffectWindow * w, (*it).managedWindows()) {
(*it).moveWindow(w, w->geometry());
}
}
2011-01-30 14:34:42 +00:00
}
setHighlightedDesktop(effects->currentDesktop()); // Ensure selected desktop is highlighted
for (QHash< DesktopButtonsView*, EffectWindow*>::iterator it = m_desktopButtonsViews.begin();
it != m_desktopButtonsViews.end(); ++it) {
it.key()->hide();
}
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
void DesktopGridEffect::setup()
2011-01-30 14:34:42 +00:00
{
keyboardGrab = effects->grabKeyboard(this);
input = effects->createInputWindow(this, 0, 0, displayWidth(), displayHeight(),
Qt::PointingHandCursor);
effects->setActiveFullScreenEffect(this);
setHighlightedDesktop(effects->currentDesktop());
// Soft highlighting
qDeleteAll(hoverTimeline);
hoverTimeline.clear();
2011-01-30 14:34:42 +00:00
for (int i = 0; i < effects->numberOfDesktops(); i++) {
QTimeLine *newTimeline = new QTimeLine(zoomDuration, this);
newTimeline->setCurveShape(QTimeLine::EaseInOutCurve);
2011-01-30 14:34:42 +00:00
hoverTimeline.append(newTimeline);
}
hoverTimeline[effects->currentDesktop() - 1]->setCurrentTime(hoverTimeline[effects->currentDesktop() - 1]->duration());
// Create desktop name textures if enabled
2011-01-30 14:34:42 +00:00
if (desktopNameAlignment) {
QFont font;
2011-01-30 14:34:42 +00:00
font.setBold(true);
font.setPointSize(12);
for (int i = 0; i < effects->numberOfDesktops(); i++) {
EffectFrame* frame = effects->effectFrame(EffectFrameUnstyled, false);
frame->setFont(font);
frame->setText(effects->desktopName(i + 1));
frame->setAlignment(desktopNameAlignment);
desktopNames.append(frame);
}
2011-01-30 14:34:42 +00:00
}
setupGrid();
2011-01-30 14:34:42 +00:00
setCurrentDesktop(effects->currentDesktop());
// setup the motion managers
2011-01-30 14:34:42 +00:00
if (m_usePresentWindows)
m_proxy = static_cast<PresentWindowsEffectProxy*>(effects->getProxy("presentwindows"));
if (isUsingPresentWindows()) {
for (int i = 1; i <= effects->numberOfDesktops(); i++) {
for (int j = 0; j < effects->numScreens(); j++) {
WindowMotionManager manager;
2011-01-30 14:34:42 +00:00
foreach (EffectWindow * w, effects->stackingOrder()) {
if (w->isOnDesktop(i) && w->screen() == j &&isRelevantWithPresentWindows(w)) {
2011-01-30 14:34:42 +00:00
manager.manage(w);
}
2011-01-30 14:34:42 +00:00
}
m_proxy->calculateWindowTransformations(manager.managedWindows(), j, manager);
m_managers.append(manager);
}
}
2011-01-30 14:34:42 +00:00
}
bool enableAdd = effects->numberOfDesktops() < 20;
bool enableRemove = effects->numberOfDesktops() > 1;
2011-01-30 14:34:42 +00:00
for (int i = 0; i < effects->numScreens(); ++i) {
DesktopButtonsView* view = new DesktopButtonsView();
2011-01-30 14:34:42 +00:00
view->setAddDesktopEnabled(enableAdd);
view->setRemoveDesktopEnabled(enableRemove);
connect(view, SIGNAL(addDesktop()), SLOT(slotAddDesktop()));
connect(view, SIGNAL(removeDesktop()), SLOT(slotRemoveDesktop()));
const QRect screenRect = effects->clientArea(FullScreenArea, i, 1);
view->setGeometry(screenRect.right() + 1 - view->sceneRect().width(),
screenRect.bottom() + 1 - view->sceneRect().height(),
view->sceneRect().width(), view->sceneRect().height());
view->show();
2011-01-30 14:34:42 +00:00
m_desktopButtonsViews.insert(view, NULL);
}
2011-01-30 14:34:42 +00:00
}
void DesktopGridEffect::setupGrid()
2011-01-30 14:34:42 +00:00
{
// We need these variables for every paint so lets cache them
int x, y;
int numDesktops = effects->numberOfDesktops();
2011-01-30 14:34:42 +00:00
switch(layoutMode) {
default:
case LayoutPager:
orientation = Qt::Horizontal;
gridSize = effects->desktopGridSize();
// sanity check: pager may report incorrect size in case of one desktop
if (numDesktops == 1) {
gridSize = QSize(1, 1);
}
2011-01-30 14:34:42 +00:00
break;
case LayoutAutomatic:
y = sqrt(float(numDesktops)) + 0.5;
x = float(numDesktops) / float(y) + 0.5;
if (x * y < numDesktops)
x++;
orientation = Qt::Horizontal;
gridSize.setWidth(x);
gridSize.setHeight(y);
break;
case LayoutCustom:
orientation = Qt::Horizontal;
gridSize.setWidth(ceil(effects->numberOfDesktops() / double(customLayoutRows)));
gridSize.setHeight(customLayoutRows);
break;
}
scale.clear();
unscaledBorder.clear();
scaledSize.clear();
scaledOffset.clear();
2011-01-30 14:34:42 +00:00
for (int i = 0; i < effects->numScreens(); i++) {
QRect geom = effects->clientArea(ScreenArea, i, 0);
double sScale;
2011-01-30 14:34:42 +00:00
if (gridSize.width() > gridSize.height())
sScale = (geom.width() - border * (gridSize.width() + 1)) / double(geom.width() * gridSize.width());
else
2011-01-30 14:34:42 +00:00
sScale = (geom.height() - border * (gridSize.height() + 1)) / double(geom.height() * gridSize.height());
double sBorder = border / sScale;
QSizeF size(
2011-01-30 14:34:42 +00:00
double(geom.width()) * sScale,
double(geom.height()) * sScale
);
QPointF offset(
2011-01-30 14:34:42 +00:00
geom.x() + (geom.width() - size.width() * gridSize.width() - border *(gridSize.width() - 1)) / 2.0,
geom.y() + (geom.height() - size.height() * gridSize.height() - border *(gridSize.height() - 1)) / 2.0
);
scale.append(sScale);
unscaledBorder.append(sBorder);
scaledSize.append(size);
scaledOffset.append(offset);
}
2011-01-30 14:34:42 +00:00
}
void DesktopGridEffect::finish()
2011-01-30 14:34:42 +00:00
{
if (desktopNameAlignment) {
qDeleteAll(desktopNames);
desktopNames.clear();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (keyboardGrab)
effects->ungrabKeyboard();
keyboardGrab = false;
2011-01-30 14:34:42 +00:00
effects->destroyInputWindow(input);
effects->setActiveFullScreenEffect(0);
if (isUsingPresentWindows()) {
while (!m_managers.isEmpty()) {
m_managers.first().unmanageAll();
m_managers.removeFirst();
}
2011-01-30 14:34:42 +00:00
m_proxy = 0;
}
QHash< DesktopButtonsView*, EffectWindow* >::iterator i = m_desktopButtonsViews.begin();
2011-01-30 14:34:42 +00:00
while (i != m_desktopButtonsViews.end()) {
if (*i && (*i)->isDeleted())
(*i)->unrefWindow();
DesktopButtonsView *view = i.key();
i = m_desktopButtonsViews.erase(i);
view->deleteLater();
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::globalShortcutChanged(const QKeySequence& seq)
{
shortcut = KShortcut(seq);
}
bool DesktopGridEffect::isMotionManagerMovingWindows() const
2011-01-30 14:34:42 +00:00
{
if (isUsingPresentWindows()) {
QList<WindowMotionManager>::const_iterator it;
2011-01-30 14:34:42 +00:00
for (it = m_managers.begin(); it != m_managers.end(); ++it) {
if ((*it).areWindowsMoving())
return true;
}
}
2011-01-30 14:34:42 +00:00
return false;
}
bool DesktopGridEffect::isUsingPresentWindows() const
2011-01-30 14:34:42 +00:00
{
return (m_proxy != NULL);
2011-01-30 14:34:42 +00:00
}
// transforms the geometry of the moved window to a geometry on the desktop
// internal method only used when a window is dropped onto a desktop
2011-01-30 14:34:42 +00:00
QRectF DesktopGridEffect::moveGeometryToDesktop(int desktop) const
{
QPointF point = unscalePos(m_windowMoveGeometry.topLeft() + cursorPos() - m_windowMoveStartPoint);
const double scaleFactor = scale[ windowMove->screen()];
if (posToDesktop(m_windowMoveGeometry.topLeft() + cursorPos() - m_windowMoveStartPoint) != desktop) {
// topLeft is not on the desktop - check other corners
// if all corners are not on the desktop the window is bigger than the desktop - no matter what it will look strange
2011-01-30 14:34:42 +00:00
if (posToDesktop(m_windowMoveGeometry.topRight() + cursorPos() - m_windowMoveStartPoint) == desktop) {
point = unscalePos(m_windowMoveGeometry.topRight() + cursorPos() - m_windowMoveStartPoint) -
QPointF(m_windowMoveGeometry.width(), 0) / scaleFactor;
} else if (posToDesktop(m_windowMoveGeometry.bottomLeft() + cursorPos() - m_windowMoveStartPoint) == desktop) {
point = unscalePos(m_windowMoveGeometry.bottomLeft() + cursorPos() - m_windowMoveStartPoint) -
QPointF(0, m_windowMoveGeometry.height()) / scaleFactor;
} else if (posToDesktop(m_windowMoveGeometry.bottomRight() + cursorPos() - m_windowMoveStartPoint) == desktop) {
point = unscalePos(m_windowMoveGeometry.bottomRight() + cursorPos() - m_windowMoveStartPoint) -
QPointF(m_windowMoveGeometry.width(), m_windowMoveGeometry.height()) / scaleFactor;
}
}
2011-01-30 14:34:42 +00:00
return QRectF(point, m_windowMoveGeometry.size() / scaleFactor);
}
void DesktopGridEffect::slotAddDesktop()
2011-01-30 14:34:42 +00:00
{
effects->setNumberOfDesktops(effects->numberOfDesktops() + 1);
}
void DesktopGridEffect::slotRemoveDesktop()
2011-01-30 14:34:42 +00:00
{
effects->setNumberOfDesktops(effects->numberOfDesktops() - 1);
}
void DesktopGridEffect::slotNumberDesktopsChanged(uint old)
2011-01-30 14:34:42 +00:00
{
if (!activated)
return;
2013-01-08 10:04:07 +00:00
const uint desktop = effects->numberOfDesktops();
bool enableAdd = desktop < 20;
bool enableRemove = desktop > 1;
2011-01-30 14:34:42 +00:00
for (QHash< DesktopButtonsView*, EffectWindow* >::iterator it = m_desktopButtonsViews.begin();
it != m_desktopButtonsViews.end(); ++it) {
it.key()->setAddDesktopEnabled(enableAdd);
it.key()->setRemoveDesktopEnabled(enableRemove);
}
2011-01-30 14:34:42 +00:00
if (old < desktop)
desktopsAdded(old);
else
desktopsRemoved(old);
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::desktopsAdded(int old)
{
const int desktop = effects->numberOfDesktops();
2011-01-30 14:34:42 +00:00
for (int i = old; i <= effects->numberOfDesktops(); i++) {
// add a timeline for the new desktop
QTimeLine *newTimeline = new QTimeLine(zoomDuration, this);
newTimeline->setCurveShape(QTimeLine::EaseInOutCurve);
2011-01-30 14:34:42 +00:00
hoverTimeline.append(newTimeline);
}
// Create desktop name textures if enabled
2011-01-30 14:34:42 +00:00
if (desktopNameAlignment) {
QFont font;
2011-01-30 14:34:42 +00:00
font.setBold(true);
font.setPointSize(12);
for (int i = old; i < desktop; i++) {
EffectFrame* frame = effects->effectFrame(EffectFrameUnstyled, false);
frame->setFont(font);
frame->setText(effects->desktopName(i + 1));
frame->setAlignment(desktopNameAlignment);
desktopNames.append(frame);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
for (int i = old+1; i <= effects->numberOfDesktops(); ++i) {
for (int j = 0; j < effects->numScreens(); ++j) {
WindowMotionManager manager;
2011-01-30 14:34:42 +00:00
foreach (EffectWindow * w, effects->stackingOrder()) {
if (w->isOnDesktop(i) && w->screen() == j &&isRelevantWithPresentWindows(w)) {
2011-01-30 14:34:42 +00:00
manager.manage(w);
}
2011-01-30 14:34:42 +00:00
}
m_proxy->calculateWindowTransformations(manager.managedWindows(), j, manager);
m_managers.append(manager);
}
}
2011-01-30 14:34:42 +00:00
}
setupGrid();
// and repaint
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void DesktopGridEffect::desktopsRemoved(int old)
{
const int desktop = effects->numberOfDesktops();
2011-01-30 14:34:42 +00:00
for (int i = desktop; i < old; i++) {
delete hoverTimeline.takeLast();
2011-01-30 14:34:42 +00:00
if (desktopNameAlignment) {
delete desktopNames.last();
desktopNames.removeLast();
2011-01-30 14:34:42 +00:00
}
if (isUsingPresentWindows()) {
for (int j = 0; j < effects->numScreens(); ++j) {
WindowMotionManager& manager = m_managers.last();
manager.unmanageAll();
m_managers.removeLast();
}
}
2011-01-30 14:34:42 +00:00
}
// add removed windows to the last desktop
2011-01-30 14:34:42 +00:00
if (isUsingPresentWindows()) {
for (int j = 0; j < effects->numScreens(); ++j) {
WindowMotionManager& manager = m_managers[(desktop-1)*(effects->numScreens())+j ];
foreach (EffectWindow * w, effects->stackingOrder()) {
if (manager.isManaging(w))
continue;
if (w->isOnDesktop(desktop) && w->screen() == j && isRelevantWithPresentWindows(w)) {
2011-01-30 14:34:42 +00:00
manager.manage(w);
}
}
2011-01-30 14:34:42 +00:00
m_proxy->calculateWindowTransformations(manager.managedWindows(), j, manager);
}
2011-01-30 14:34:42 +00:00
}
setupGrid();
// and repaint
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
bool DesktopGridEffect::isActive() const
{
return timeline.currentValue() != 0 || activated || (isUsingPresentWindows() && isMotionManagerMovingWindows());
}
bool DesktopGridEffect::isRelevantWithPresentWindows(EffectWindow *w) const
{
return !(w->isDesktop() || w->isDock() || w->isSkipSwitcher()) &&
w->isCurrentTab() && w->isOnCurrentActivity();
}
/************************************************
* DesktopButtonView
************************************************/
2011-01-30 14:34:42 +00:00
DesktopButtonsView::DesktopButtonsView(QWidget* parent)
: QGraphicsView(parent)
{
setWindowFlags(Qt::X11BypassWindowManagerHint);
setAttribute(Qt::WA_TranslucentBackground);
setFrameShape(QFrame::NoFrame);
QPalette pal = palette();
2011-01-30 14:34:42 +00:00
pal.setColor(backgroundRole(), Qt::transparent);
setPalette(pal);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// setup the scene
2011-01-30 14:34:42 +00:00
QGraphicsScene* scene = new QGraphicsScene(this);
m_addDesktopButton = new Plasma::PushButton();
2011-01-30 14:34:42 +00:00
m_addDesktopButton->setIcon(KIcon("list-add"));
m_removeDesktopButton = new Plasma::PushButton();
m_removeDesktopButton->setIcon(KIcon("list-remove"));
2011-01-30 14:34:42 +00:00
scene->addItem(m_addDesktopButton);
scene->addItem(m_removeDesktopButton);
connect(m_addDesktopButton, SIGNAL(clicked()), SIGNAL(addDesktop()));
connect(m_removeDesktopButton, SIGNAL(clicked()), SIGNAL(removeDesktop()));
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
2011-01-30 14:34:42 +00:00
layout->addItem(m_addDesktopButton);
layout->addItem(m_removeDesktopButton);
QGraphicsWidget *form = new QGraphicsWidget;
2011-01-30 14:34:42 +00:00
form->setLayout(layout);
form->setGeometry(0, 0, 64 * 2, 64);
scene->addItem(form);
m_frame = new Plasma::FrameSvg(this);
m_frame->setImagePath("dialogs/background");
m_frame->setCacheAllRenderedFrames(true);
m_frame->setEnabledBorders(Plasma::FrameSvg::AllBorders);
qreal left, top, right, bottom;
2011-01-30 14:34:42 +00:00
m_frame->getMargins(left, top, right, bottom);
qreal width = form->size().width() + left + right;
qreal height = form->size().height() + top + bottom;
2011-01-30 14:34:42 +00:00
m_frame->resizeFrame(QSizeF(width, height));
Plasma::WindowEffects::enableBlurBehind(winId(), true, m_frame->mask());
Plasma::WindowEffects::overrideShadow(winId(), true);
form->setPos(left, top);
scene->setSceneRect(QRectF(QPointF(0, 0), QSizeF(width, height)));
setScene(scene);
}
void DesktopButtonsView::windowInputMouseEvent(QMouseEvent* e)
{
if (e->type() == QEvent::MouseMove) {
mouseMoveEvent(e);
} else if (e->type() == QEvent::MouseButtonPress) {
mousePressEvent(e);
} else if (e->type() == QEvent::MouseButtonDblClick) {
mouseDoubleClickEvent(e);
} else if (e->type() == QEvent::MouseButtonRelease) {
mouseReleaseEvent(e);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void DesktopButtonsView::setAddDesktopEnabled(bool enable)
{
m_addDesktopButton->setEnabled(enable);
}
2011-01-30 14:34:42 +00:00
void DesktopButtonsView::setRemoveDesktopEnabled(bool enable)
{
m_removeDesktopButton->setEnabled(enable);
}
2011-01-30 14:34:42 +00:00
void DesktopButtonsView::drawBackground(QPainter* painter, const QRectF& rect)
{
Q_UNUSED(rect)
painter->setRenderHint(QPainter::Antialiasing);
m_frame->paintFrame(painter);
}
} // namespace
#include "desktopgrid.moc"