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>
27 #include <kactioncollection.h>
28 #include <kconfiggroup.h>
29 #include <kstandardaction.h>
31 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
38 KWIN_EFFECT( magnifier
, MagnifierEffect
)
40 const int FRAME_WIDTH
= 5;
42 MagnifierEffect::MagnifierEffect()
46 KActionCollection
* actionCollection
= new KActionCollection( this );
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");
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
);
74 zoom
= qMax( zoom
* qMin( 1 - diff
, 0.8 ), target_zoom
);
76 effects
->prePaintScreen( data
, time
);
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
87 QRect area
= magnifierArea();
88 PaintClipper::push( area
); // don't allow any painting outside of the area
89 mask
|= PAINT_SCREEN_TRANSFORMED
;
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
;
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
);
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()
147 effects
->addRepaint( magnifierArea().adjusted( -FRAME_WIDTH
, -FRAME_WIDTH
, FRAME_WIDTH
, FRAME_WIDTH
));
150 void MagnifierEffect::zoomOut()
153 if( 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 )
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
));
176 #include "magnifier.moc"