add more spacing
[personal-kdebase.git] / workspace / kwin / effects / thumbnailaside.cpp
blobdec03e6bb4585400fd8fbe11493970cf8406b25b
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2007 Lubos Lunak <l.lunak@kde.org>
6 Copyright (C) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *********************************************************************/
22 #include "thumbnailaside.h"
24 #include <kactioncollection.h>
25 #include <kaction.h>
26 #include <kconfiggroup.h>
27 #include <klocale.h>
29 namespace KWin
32 KWIN_EFFECT( thumbnailaside, ThumbnailAsideEffect )
34 ThumbnailAsideEffect::ThumbnailAsideEffect()
36 KActionCollection* actionCollection = new KActionCollection( this );
37 KAction* a = (KAction*)actionCollection->addAction( "ToggleCurrentThumbnail" );
38 a->setText( i18n("Toggle Thumbnail for Current Window" ));
39 a->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::META + Qt::Key_T));
40 connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleCurrentThumbnail()));
41 reconfigure( ReconfigureAll );
44 void ThumbnailAsideEffect::reconfigure( ReconfigureFlags )
46 KConfigGroup conf = EffectsHandler::effectConfig("ThumbnailAside");
47 maxwidth = conf.readEntry("MaxWidth", 200);
48 spacing = conf.readEntry("Spacing", 10);
49 opacity = conf.readEntry("Opacity", 50) / 100.0;
50 screen = conf.readEntry("Screen",-1); // Xinerama screen TODO add gui option
51 arrange();
54 void ThumbnailAsideEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
56 effects->paintScreen( mask, region, data );
57 foreach( const Data& d, windows )
59 if( region.contains( d.rect ))
61 WindowPaintData data( d.window );
62 data.opacity = opacity;
63 QRect region;
64 setPositionTransformations( data, region, d.window, d.rect, Qt::KeepAspectRatio );
65 effects->drawWindow( d.window, PAINT_WINDOW_OPAQUE | PAINT_WINDOW_TRANSLUCENT | PAINT_WINDOW_TRANSFORMED,
66 region, data );
71 void ThumbnailAsideEffect::windowDamaged( EffectWindow* w, const QRect& )
73 foreach( const Data& d, windows )
75 if( d.window == w )
76 effects->addRepaint( d.rect );
80 void ThumbnailAsideEffect::windowGeometryShapeChanged( EffectWindow* w, const QRect& old )
82 foreach( const Data& d, windows )
84 if( d.window == w )
86 if( w->size() == old.size())
87 effects->addRepaint( d.rect );
88 else
89 arrange();
90 return;
95 void ThumbnailAsideEffect::windowClosed( EffectWindow* w )
97 removeThumbnail( w );
100 void ThumbnailAsideEffect::toggleCurrentThumbnail()
102 EffectWindow* active = effects->activeWindow();
103 if( active == NULL )
104 return;
105 if( windows.contains( active ))
106 removeThumbnail( active );
107 else
108 addThumbnail( active );
111 void ThumbnailAsideEffect::addThumbnail( EffectWindow* w )
113 repaintAll(); // repaint old areas
114 Data d;
115 d.window = w;
116 d.index = windows.count();
117 windows[ w ] = d;
118 arrange();
121 void ThumbnailAsideEffect::removeThumbnail( EffectWindow* w )
123 if( !windows.contains( w ))
124 return;
125 repaintAll(); // repaint old areas
126 int index = windows[ w ].index;
127 windows.remove( w );
128 for( QHash< EffectWindow*, Data >::Iterator it = windows.begin();
129 it != windows.end();
130 ++it )
132 Data& d = *it;
133 if( d.index > index )
134 --d.index;
136 arrange();
139 void ThumbnailAsideEffect::arrange()
141 if( windows.size() == 0 )
142 return;
143 int height = 0;
144 QVector< int > pos( windows.size());
145 int mwidth = 0;
146 foreach( const Data& d, windows )
148 height += d.window->height();
149 mwidth = qMax( mwidth, d.window->width());
150 pos[ d.index ] = d.window->height();
152 QRect area = effects->clientArea( MaximizeArea, screen, effects->currentDesktop());
153 double scale = area.height() / double( height );
154 scale = qMin( scale, maxwidth / double( mwidth )); // don't be wider than maxwidth pixels
155 int add = 0;
156 for( int i = 0;
157 i < windows.size();
158 ++i )
160 pos[ i ] = int( pos[ i ] * scale );
161 pos[ i ] += spacing + add; // compute offset of each item
162 add = pos[ i ];
164 for( QHash< EffectWindow*, Data >::Iterator it = windows.begin();
165 it != windows.end();
166 ++it )
168 Data& d = *it;
169 int width = int( d.window->width() * scale );
170 d.rect = QRect( area.right() - width, area.bottom() - pos[ d.index ], width, int( d.window->height() * scale ));
172 repaintAll();
175 void ThumbnailAsideEffect::repaintAll()
177 foreach( const Data& d, windows )
178 effects->addRepaint( d.rect );
181 } // namespace
183 #include "thumbnailaside.moc"