kwin/effects/magnifier/magnifier.cpp

207 lines
7.6 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 "magnifier.h"
#include <kwinconfig.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <kconfiggroup.h>
#include <kstandardaction.h>
2011-01-06 13:29:13 +00:00
#include <kwinglutils.h>
namespace KWin
{
2011-01-30 14:34:42 +00:00
KWIN_EFFECT(magnifier, MagnifierEffect)
const int FRAME_WIDTH = 5;
MagnifierEffect::MagnifierEffect()
2011-01-30 14:34:42 +00:00
: zoom(1)
, target_zoom(1)
, polling(false)
{
KActionCollection* actionCollection = new KActionCollection(this);
KAction* a;
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction(KStandardAction::ZoomIn, this, SLOT(zoomIn())));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Equal));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction(KStandardAction::ZoomOut, this, SLOT(zoomOut())));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Minus));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction(KStandardAction::ActualSize, this, SLOT(toggle())));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_0));
2011-01-30 14:34:42 +00:00
reconfigure(ReconfigureAll);
}
2011-01-30 14:34:42 +00:00
void MagnifierEffect::reconfigure(ReconfigureFlags)
{
KConfigGroup conf = EffectsHandler::effectConfig("Magnifier");
int width, height;
width = conf.readEntry("Width", 200);
height = conf.readEntry("Height", 200);
2011-01-30 14:34:42 +00:00
magnifier_size = QSize(width, height);
}
2011-01-30 14:34:42 +00:00
void MagnifierEffect::prePaintScreen(ScreenPrePaintData& data, int time)
{
if (zoom != target_zoom) {
double diff = time / animationTime(500.0);
if (target_zoom > zoom)
zoom = qMin(zoom * qMax(1 + diff, 1.2), target_zoom);
else
2011-01-30 14:34:42 +00:00
zoom = qMax(zoom * qMin(1 - diff, 0.8), target_zoom);
}
2011-01-30 14:34:42 +00:00
effects->prePaintScreen(data, time);
if (zoom != 1.0)
data.paint |= magnifierArea().adjusted(-FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH);
}
2011-01-30 14:34:42 +00:00
void MagnifierEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
ScreenPaintData data2 = data;
2011-01-30 14:34:42 +00:00
effects->paintScreen(mask, region, data); // paint normal screen
if (zoom != 1.0) {
// paint magnifier
QRect area = magnifierArea();
2011-01-30 14:34:42 +00:00
PaintClipper::push(area); // don't allow any painting outside of the area
mask |= PAINT_SCREEN_TRANSFORMED;
data2.xScale *= zoom;
data2.yScale *= zoom;
QPoint cursor = cursorPos();
// set the position so that the cursor is in the same position in the scaled view
2011-01-30 14:34:42 +00:00
data2.xTranslate = - int(cursor.x() * (zoom - 1));
data2.yTranslate = - int(cursor.y() * (zoom - 1));
effects->paintScreen(mask, region, data2);
PaintClipper::pop(area);
2011-01-06 13:29:13 +00:00
QVector<float> verts;
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
vbo->reset();
vbo->setColor(QColor(0, 0, 0));
// top frame
verts << area.right() + FRAME_WIDTH << area.top() - FRAME_WIDTH;
verts << area.left() - FRAME_WIDTH << area.top() - FRAME_WIDTH;
verts << area.left() - FRAME_WIDTH << area.top() - 1;
verts << area.left() - FRAME_WIDTH << area.top() - 1;
verts << area.right() + FRAME_WIDTH << area.top() - 1;
verts << area.right() + FRAME_WIDTH << area.top() - FRAME_WIDTH;
// left frame
verts << area.left() - 1 << area.top() - FRAME_WIDTH;
verts << area.left() - FRAME_WIDTH << area.top() - FRAME_WIDTH;
verts << area.left() - FRAME_WIDTH << area.bottom() + FRAME_WIDTH;
verts << area.left() - FRAME_WIDTH << area.bottom() + FRAME_WIDTH;
verts << area.left() - 1 << area.bottom() + FRAME_WIDTH;
verts << area.left() - 1 << area.top() - FRAME_WIDTH;
// right frame
verts << area.right() + FRAME_WIDTH << area.top() - FRAME_WIDTH;
verts << area.right() + 1 << area.top() - FRAME_WIDTH;
verts << area.right() + 1 << area.bottom() + FRAME_WIDTH;
verts << area.right() + 1 << area.bottom() + FRAME_WIDTH;
verts << area.right() + FRAME_WIDTH << area.bottom() + FRAME_WIDTH;
verts << area.right() + FRAME_WIDTH << area.top() - FRAME_WIDTH;
// bottom frame
verts << area.right() + FRAME_WIDTH << area.bottom() + 1;
verts << area.left() - FRAME_WIDTH << area.bottom() + 1;
verts << area.left() - FRAME_WIDTH << area.bottom() + FRAME_WIDTH;
verts << area.left() - FRAME_WIDTH << area.bottom() + FRAME_WIDTH;
verts << area.right() + FRAME_WIDTH << area.bottom() + FRAME_WIDTH;
verts << area.right() + FRAME_WIDTH << area.bottom() + 1;
2011-01-30 14:34:42 +00:00
vbo->setData(verts.size() / 2, 2, verts.constData(), NULL);
2011-01-06 13:29:13 +00:00
if (ShaderManager::instance()->isValid()) {
ShaderManager::instance()->pushShader(ShaderManager::ColorShader);
}
vbo->render(GL_TRIANGLES);
if (ShaderManager::instance()->isValid()) {
ShaderManager::instance()->popShader();
}
}
2011-01-30 14:34:42 +00:00
}
void MagnifierEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
if (zoom != target_zoom) {
QRect framedarea = magnifierArea().adjusted(-FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH);
effects->addRepaint(framedarea);
}
effects->postPaintScreen();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QRect MagnifierEffect::magnifierArea(QPoint pos) const
{
return QRect(pos.x() - magnifier_size.width() / 2, pos.y() - magnifier_size.height() / 2,
magnifier_size.width(), magnifier_size.height());
}
void MagnifierEffect::zoomIn()
2011-01-30 14:34:42 +00:00
{
target_zoom *= 1.2;
2011-01-30 14:34:42 +00:00
if (!polling) {
polling = true;
effects->startMousePolling();
}
2011-01-30 14:34:42 +00:00
effects->addRepaint(magnifierArea().adjusted(-FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH));
}
void MagnifierEffect::zoomOut()
2011-01-30 14:34:42 +00:00
{
target_zoom /= 1.2;
2011-01-30 14:34:42 +00:00
if (target_zoom < 1) {
target_zoom = 1;
2011-01-30 14:34:42 +00:00
if (polling) {
polling = false;
effects->stopMousePolling();
}
}
2011-01-30 14:34:42 +00:00
effects->addRepaint(magnifierArea().adjusted(-FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH));
}
void MagnifierEffect::toggle()
2011-01-30 14:34:42 +00:00
{
if (target_zoom == 1.0) {
target_zoom = 2;
2011-01-30 14:34:42 +00:00
if (!polling) {
polling = true;
effects->startMousePolling();
}
2011-01-30 14:34:42 +00:00
} else {
target_zoom = 1;
2011-01-30 14:34:42 +00:00
if (polling) {
polling = false;
effects->stopMousePolling();
}
}
2011-01-30 14:34:42 +00:00
effects->addRepaint(magnifierArea().adjusted(-FRAME_WIDTH, -FRAME_WIDTH, FRAME_WIDTH, FRAME_WIDTH));
}
2011-01-30 14:34:42 +00:00
void MagnifierEffect::mouseChanged(const QPoint& pos, const QPoint& old,
Qt::MouseButtons, Qt::MouseButtons, Qt::KeyboardModifiers, Qt::KeyboardModifiers)
{
if (pos != old && zoom != 1)
// need full repaint as we might lose some change events on fast mouse movements
// see Bug 187658
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
} // namespace
#include "magnifier.moc"