kwin/effects/thumbnailaside/thumbnailaside.cpp

183 lines
5.7 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) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
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 "thumbnailaside.h"
#include <kactioncollection.h>
#include <kaction.h>
#include <kconfiggroup.h>
#include <klocale.h>
namespace KWin
{
2011-01-30 14:34:42 +00:00
KWIN_EFFECT(thumbnailaside, ThumbnailAsideEffect)
ThumbnailAsideEffect::ThumbnailAsideEffect()
2011-01-30 14:34:42 +00:00
{
KActionCollection* actionCollection = new KActionCollection(this);
KAction* a = (KAction*)actionCollection->addAction("ToggleCurrentThumbnail");
a->setText(i18n("Toggle Thumbnail for Current Window"));
a->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::META + Qt::Key_T));
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleCurrentThumbnail()));
connect(effects, SIGNAL(windowClosed(EffectWindow*)), this, SLOT(slotWindowClosed(EffectWindow*)));
connect(effects, SIGNAL(windowGeometryShapeChanged(EffectWindow*,QRect)), this, SLOT(slotWindowGeometryShapeChanged(EffectWindow*,QRect)));
2011-03-12 14:04:22 +00:00
connect(effects, SIGNAL(windowDamaged(EffectWindow*,QRect)), this, SLOT(slotWindowDamaged(EffectWindow*,QRect)));
2011-01-30 14:34:42 +00:00
reconfigure(ReconfigureAll);
}
2011-01-30 14:34:42 +00:00
void ThumbnailAsideEffect::reconfigure(ReconfigureFlags)
{
KConfigGroup conf = EffectsHandler::effectConfig("ThumbnailAside");
maxwidth = conf.readEntry("MaxWidth", 200);
spacing = conf.readEntry("Spacing", 10);
opacity = conf.readEntry("Opacity", 50) / 100.0;
2011-01-30 14:34:42 +00:00
screen = conf.readEntry("Screen", -1); // Xinerama screen TODO add gui option
arrange();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void ThumbnailAsideEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
effects->paintScreen(mask, region, data);
foreach (const Data & d, windows) {
if (region.contains(d.rect)) {
WindowPaintData data(d.window);
data.opacity = opacity;
QRect region;
2011-01-30 14:34:42 +00:00
setPositionTransformations(data, region, d.window, d.rect, Qt::KeepAspectRatio);
effects->drawWindow(d.window, PAINT_WINDOW_OPAQUE | PAINT_WINDOW_TRANSLUCENT | PAINT_WINDOW_TRANSFORMED | PAINT_WINDOW_LANCZOS,
region, data);
}
}
2011-01-30 14:34:42 +00:00
}
2011-03-12 14:04:22 +00:00
void ThumbnailAsideEffect::slotWindowDamaged(EffectWindow* w, const QRect&)
2011-01-30 14:34:42 +00:00
{
foreach (const Data & d, windows) {
if (d.window == w)
effects->addRepaint(d.rect);
}
2011-01-30 14:34:42 +00:00
}
void ThumbnailAsideEffect::slotWindowGeometryShapeChanged(EffectWindow* w, const QRect& old)
2011-01-30 14:34:42 +00:00
{
foreach (const Data & d, windows) {
if (d.window == w) {
if (w->size() == old.size())
effects->addRepaint(d.rect);
else
arrange();
return;
}
}
2011-01-30 14:34:42 +00:00
}
void ThumbnailAsideEffect::slotWindowClosed(EffectWindow* w)
2011-01-30 14:34:42 +00:00
{
removeThumbnail(w);
}
void ThumbnailAsideEffect::toggleCurrentThumbnail()
2011-01-30 14:34:42 +00:00
{
EffectWindow* active = effects->activeWindow();
2011-01-30 14:34:42 +00:00
if (active == NULL)
return;
2011-01-30 14:34:42 +00:00
if (windows.contains(active))
removeThumbnail(active);
else
2011-01-30 14:34:42 +00:00
addThumbnail(active);
}
2011-01-30 14:34:42 +00:00
void ThumbnailAsideEffect::addThumbnail(EffectWindow* w)
{
repaintAll(); // repaint old areas
Data d;
d.window = w;
d.index = windows.count();
windows[ w ] = d;
arrange();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void ThumbnailAsideEffect::removeThumbnail(EffectWindow* w)
{
if (!windows.contains(w))
return;
repaintAll(); // repaint old areas
int index = windows[ w ].index;
2011-01-30 14:34:42 +00:00
windows.remove(w);
for (QHash< EffectWindow*, Data >::Iterator it = windows.begin();
it != windows.end();
++it) {
Data& d = *it;
2011-01-30 14:34:42 +00:00
if (d.index > index)
--d.index;
}
2011-01-30 14:34:42 +00:00
arrange();
}
void ThumbnailAsideEffect::arrange()
2011-01-30 14:34:42 +00:00
{
if (windows.size() == 0)
return;
int height = 0;
2011-01-30 14:34:42 +00:00
QVector< int > pos(windows.size());
int mwidth = 0;
2011-01-30 14:34:42 +00:00
foreach (const Data & d, windows) {
height += d.window->height();
2011-01-30 14:34:42 +00:00
mwidth = qMax(mwidth, d.window->width());
pos[ d.index ] = d.window->height();
2011-01-30 14:34:42 +00:00
}
QRect area = effects->clientArea(MaximizeArea, screen, effects->currentDesktop());
double scale = area.height() / double(height);
scale = qMin(scale, maxwidth / double(mwidth)); // don't be wider than maxwidth pixels
int add = 0;
2011-01-30 14:34:42 +00:00
for (int i = 0;
i < windows.size();
++i) {
pos[ i ] = int(pos[ i ] * scale);
pos[ i ] += spacing + add; // compute offset of each item
add = pos[ i ];
2011-01-30 14:34:42 +00:00
}
for (QHash< EffectWindow*, Data >::Iterator it = windows.begin();
it != windows.end();
++it) {
Data& d = *it;
2011-01-30 14:34:42 +00:00
int width = int(d.window->width() * scale);
d.rect = QRect(area.right() - width, area.bottom() - pos[ d.index ], width, int(d.window->height() * scale));
}
2011-01-30 14:34:42 +00:00
repaintAll();
}
void ThumbnailAsideEffect::repaintAll()
2011-01-30 14:34:42 +00:00
{
foreach (const Data & d, windows)
effects->addRepaint(d.rect);
}
bool ThumbnailAsideEffect::isActive() const
{
return !windows.isEmpty();
}
} // namespace
#include "thumbnailaside.moc"