add more spacing
[personal-kdebase.git] / workspace / kwin / effects / magnifier.cpp
blob0bc65c669492795ba8cb7009bcda7c6e13892ba2
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 "magnifier.h"
24 #include <kwinconfig.h>
26 #include <kaction.h>
27 #include <kactioncollection.h>
28 #include <kconfiggroup.h>
29 #include <kstandardaction.h>
31 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
32 #include <GL/gl.h>
33 #endif
35 namespace KWin
38 KWIN_EFFECT( magnifier, MagnifierEffect )
40 const int FRAME_WIDTH = 5;
42 MagnifierEffect::MagnifierEffect()
43 : zoom( 1 )
44 , target_zoom( 1 )
46 KActionCollection* actionCollection = new KActionCollection( this );
47 KAction* a;
48 a = static_cast< KAction* >( actionCollection->addAction( KStandardAction::ZoomIn, this, SLOT( zoomIn())));
49 a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Equal));
50 a = static_cast< KAction* >( actionCollection->addAction( KStandardAction::ZoomOut, this, SLOT( zoomOut())));
51 a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Minus));
52 a = static_cast< KAction* >( actionCollection->addAction( KStandardAction::ActualSize, this, SLOT( toggle())));
53 a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_0));
54 reconfigure( ReconfigureAll );
57 void MagnifierEffect::reconfigure( ReconfigureFlags )
59 KConfigGroup conf = EffectsHandler::effectConfig("Magnifier");
60 int width, height;
61 width = conf.readEntry("Width", 200);
62 height = conf.readEntry("Height", 200);
63 magnifier_size = QSize( width, height );
66 void MagnifierEffect::prePaintScreen( ScreenPrePaintData& data, int time )
68 if( zoom != target_zoom )
70 double diff = time / animationTime( 500.0 );
71 if( target_zoom > zoom )
72 zoom = qMin( zoom * qMax( 1 + diff, 1.2 ), target_zoom );
73 else
74 zoom = qMax( zoom * qMin( 1 - diff, 0.8 ), target_zoom );
76 effects->prePaintScreen( data, time );
77 if( zoom != 1.0 )
78 data.paint |= magnifierArea().adjusted( -FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH );
81 void MagnifierEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
83 ScreenPaintData data2 = data;
84 effects->paintScreen( mask, region, data ); // paint normal screen
85 if( zoom != 1.0 )
86 { // paint magnifier
87 QRect area = magnifierArea();
88 PaintClipper::push( area ); // don't allow any painting outside of the area
89 mask |= PAINT_SCREEN_TRANSFORMED;
90 data2.xScale *= zoom;
91 data2.yScale *= zoom;
92 QPoint cursor = cursorPos();
93 // set the position so that the cursor is in the same position in the scaled view
94 data2.xTranslate = - int( cursor.x() * ( zoom - 1 ));
95 data2.yTranslate = - int( cursor.y() * ( zoom - 1 ));
96 effects->paintScreen( mask, region, data2 );
97 PaintClipper::pop( area );
98 // ## TODO this should be inside KWIN_HAVE_OPENGL_COMPOSITING
99 glPushAttrib( GL_CURRENT_BIT );
100 glColor4f( 0, 0, 0, 1 ); // black
101 for( PaintClipper::Iterator iterator;
102 !iterator.isDone();
103 iterator.next())
105 glBegin( GL_QUADS );
106 glVertex2i( area.left() - FRAME_WIDTH, area.top() - FRAME_WIDTH ); // top frame
107 glVertex2i( area.right() + FRAME_WIDTH, area.top() - FRAME_WIDTH );
108 glVertex2i( area.right() + FRAME_WIDTH, area.top() - 1 );
109 glVertex2i( area.left() - FRAME_WIDTH, area.top() - 1 );
110 glVertex2i( area.left() - FRAME_WIDTH, area.top() - FRAME_WIDTH ); // left frame
111 glVertex2i( area.left() - 1, area.top() - FRAME_WIDTH );
112 glVertex2i( area.left() - 1, area.bottom() + FRAME_WIDTH );
113 glVertex2i( area.left() - FRAME_WIDTH, area.bottom() + FRAME_WIDTH );
114 glVertex2i( area.right() + 1, area.top() - FRAME_WIDTH ); // right frame
115 glVertex2i( area.right() + FRAME_WIDTH, area.top() - FRAME_WIDTH );
116 glVertex2i( area.right() + FRAME_WIDTH, area.bottom() + FRAME_WIDTH );
117 glVertex2i( area.right() + 1, area.bottom() + FRAME_WIDTH );
118 glVertex2i( area.left() - FRAME_WIDTH, area.bottom() + 1 ); // bottom frame
119 glVertex2i( area.right() + FRAME_WIDTH, area.bottom() + 1 );
120 glVertex2i( area.right() + FRAME_WIDTH, area.bottom() + FRAME_WIDTH );
121 glVertex2i( area.left() - FRAME_WIDTH, area.bottom() + FRAME_WIDTH );
122 glEnd();
124 glPopAttrib();
128 void MagnifierEffect::postPaintScreen()
130 if( zoom != target_zoom )
132 QRect framedarea = magnifierArea().adjusted( -FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH );
133 effects->addRepaint( framedarea );
135 effects->postPaintScreen();
138 QRect MagnifierEffect::magnifierArea( QPoint pos ) const
140 return QRect( pos.x() - magnifier_size.width() / 2, pos.y() - magnifier_size.height() / 2,
141 magnifier_size.width(), magnifier_size.height());
144 void MagnifierEffect::zoomIn()
146 target_zoom *= 1.2;
147 effects->addRepaint( magnifierArea().adjusted( -FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH ));
150 void MagnifierEffect::zoomOut()
152 target_zoom /= 1.2;
153 if( target_zoom < 1 )
154 target_zoom = 1;
155 effects->addRepaint( magnifierArea().adjusted( -FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH ));
158 void MagnifierEffect::toggle()
160 if( target_zoom == 1.0 )
161 target_zoom = 2;
162 else
163 target_zoom = 1;
164 effects->addRepaint( magnifierArea().adjusted( -FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH ));
167 void MagnifierEffect::mouseChanged( const QPoint& pos, const QPoint& old,
168 Qt::MouseButtons, Qt::MouseButtons, Qt::KeyboardModifiers, Qt::KeyboardModifiers )
170 if( pos != old && zoom != 1 )
171 effects->addRepaint( magnifierArea( old ).adjusted( -FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH ));
174 } // namespace
176 #include "magnifier.moc"