kwin/effects/snaphelper/snaphelper.cpp

237 lines
8.8 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2009 Lucas Murray <lmurray@undefinedfire.com>
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 "snaphelper.h"
#include <kwinglutils.h>
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
#include <kwinxrenderutils.h>
2013-01-29 07:28:30 +00:00
#include <xcb/render.h>
#endif
#include <QPainter>
namespace KWin
{
SnapHelperEffect::SnapHelperEffect()
2011-01-30 14:34:42 +00:00
: m_active(false)
, m_window(NULL)
{
m_timeline.setCurveShape(QTimeLine::LinearCurve);
2011-01-30 14:34:42 +00:00
reconfigure(ReconfigureAll);
connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowStartUserMovedResized(KWin::EffectWindow*)), this, SLOT(slotWindowStartUserMovedResized(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowFinishUserMovedResized(KWin::EffectWindow*)), this, SLOT(slotWindowFinishUserMovedResized(KWin::EffectWindow*)));
2013-05-11 12:09:15 +00:00
connect(effects, SIGNAL(windowGeometryShapeChanged(KWin::EffectWindow*,QRect)), this, SLOT(slotWindowResized(KWin::EffectWindow*,QRect)));
2011-01-30 14:34:42 +00:00
}
SnapHelperEffect::~SnapHelperEffect()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void SnapHelperEffect::reconfigure(ReconfigureFlags)
{
m_timeline.setDuration(animationTime(250));
}
2011-01-30 14:34:42 +00:00
void SnapHelperEffect::prePaintScreen(ScreenPrePaintData &data, int time)
{
double oldValue = m_timeline.currentValue();
2011-01-30 14:34:42 +00:00
if (m_active)
m_timeline.setCurrentTime(m_timeline.currentTime() + time);
else
m_timeline.setCurrentTime(m_timeline.currentTime() - time);
if (oldValue != m_timeline.currentValue())
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
effects->prePaintScreen(data, time);
}
void SnapHelperEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
effects->postPaintScreen();
if (m_timeline.currentValue() != 0.0) {
2011-01-30 14:34:42 +00:00
// Display the guide
if (effects->isOpenGLCompositing()) {
2010-12-11 14:56:21 +00:00
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
vbo->reset();
vbo->setUseColor(true);
ShaderBinder binder(ShaderManager::ColorShader);
2011-01-30 14:34:42 +00:00
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2010-12-08 18:25:15 +00:00
QColor color;
color.setRedF(0.5);
color.setGreenF(0.5);
color.setBlueF(0.5);
color.setAlphaF(m_timeline.currentValue() * 0.5);
2010-12-11 14:56:21 +00:00
vbo->setColor(color);
2011-01-30 14:34:42 +00:00
glLineWidth(4.0);
2010-12-08 18:25:15 +00:00
QVector<float> verts;
2011-01-30 14:34:42 +00:00
verts.reserve(effects->numScreens() * 24);
for (int i = 0; i < effects->numScreens(); ++i) {
2011-01-30 14:34:42 +00:00
const QRect& rect = effects->clientArea(ScreenArea, i, 0);
2010-12-08 18:25:15 +00:00
int midX = rect.x() + rect.width() / 2;
int midY = rect.y() + rect.height() / 2 ;
int halfWidth = m_window->width() / 2;
int halfHeight = m_window->height() / 2;
// Center lines
verts << rect.x() + rect.width() / 2 << rect.y();
verts << rect.x() + rect.width() / 2 << rect.y() + rect.height();
verts << rect.x() << rect.y() + rect.height() / 2;
verts << rect.x() + rect.width() << rect.y() + rect.height() / 2;
// Window outline
// The +/- 2 is to prevent line overlap
verts << midX - halfWidth + 2 << midY - halfHeight;
verts << midX + halfWidth + 2 << midY - halfHeight;
verts << midX + halfWidth << midY - halfHeight + 2;
verts << midX + halfWidth << midY + halfHeight + 2;
verts << midX + halfWidth - 2 << midY + halfHeight;
verts << midX - halfWidth - 2 << midY + halfHeight;
verts << midX - halfWidth << midY + halfHeight - 2;
verts << midX - halfWidth << midY - halfHeight - 2;
}
2011-01-30 14:34:42 +00:00
vbo->setData(verts.count() / 2, 2, verts.data(), NULL);
2010-12-11 14:56:21 +00:00
vbo->render(GL_LINES);
2011-01-30 14:34:42 +00:00
glDisable(GL_BLEND);
glLineWidth(1.0);
}
if ( effects->compositingType() == XRenderCompositing ) {
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
for (int i = 0; i < effects->numScreens(); ++i) {
const QRect& rect = effects->clientArea( ScreenArea, i, 0 );
int midX = rect.x() + rect.width() / 2;
int midY = rect.y() + rect.height() / 2 ;
int halfWidth = m_window->width() / 2;
int halfHeight = m_window->height() / 2;
2013-01-29 07:28:30 +00:00
xcb_rectangle_t rects[6];
// Center lines
rects[0].x = rect.x() + rect.width() / 2 - 2;
rects[0].y = rect.y();
rects[0].width = 4;
rects[0].height = rect.height();
rects[1].x = rect.x();
rects[1].y = rect.y() + rect.height() / 2 - 2;
rects[1].width = rect.width();
rects[1].height = 4;
// Window outline
// The +/- 4 is to prevent line overlap
rects[2].x = midX - halfWidth + 4;
rects[2].y = midY - halfHeight;
rects[2].width = 2*halfWidth - 4;
rects[2].height = 4;
rects[3].x = midX + halfWidth - 4;
rects[3].y = midY - halfHeight + 4;
rects[3].width = 4;
rects[3].height = 2*halfHeight - 4;
rects[4].x = midX - halfWidth;
rects[4].y = midY + halfHeight - 4;
rects[4].width = 2*halfWidth - 4;
rects[4].height = 4;
rects[5].x = midX - halfWidth;
rects[5].y = midY - halfHeight;
rects[5].width = 4;
rects[5].height = 2*halfHeight - 4;
xcb_render_fill_rectangles(xcbConnection(), XCB_RENDER_PICT_OP_OVER, effects->xrenderBufferPicture(),
preMultiply(QColor(128, 128, 128, m_timeline.currentValue()*128)), 6, rects);
}
#endif
}
if (effects->compositingType() == QPainterCompositing) {
QPainter *painter = effects->scenePainter();
painter->save();
QColor color;
color.setRedF(0.5);
color.setGreenF(0.5);
color.setBlueF(0.5);
color.setAlphaF(m_timeline.currentValue() * 0.5);
QPen pen(color);
pen.setWidth(4);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
for (int i = 0; i < effects->numScreens(); ++i) {
const QRect &rect = effects->clientArea(ScreenArea, i, 0);
// Center lines
painter->drawLine(rect.center().x(), rect.y(), rect.center().x(), rect.y() + rect.height());
painter->drawLine(rect.x(), rect.center().y(), rect.x() + rect.width(), rect.center().y());
// window outline
QRect windowRect(rect.center(), m_window->geometry().size());
painter->drawRect(windowRect.translated(-windowRect.width()/2, -windowRect.height()/2));
}
painter->restore();
}
} else if (m_window && !m_active) {
2011-01-30 14:34:42 +00:00
if (m_window->isDeleted())
m_window->unrefWindow();
m_window = NULL;
}
2011-01-30 14:34:42 +00:00
}
void SnapHelperEffect::slotWindowClosed(EffectWindow* w)
2011-01-30 14:34:42 +00:00
{
if (m_window == w) {
m_window->refWindow();
m_active = false;
}
2011-01-30 14:34:42 +00:00
}
void SnapHelperEffect::slotWindowStartUserMovedResized(EffectWindow *w)
2011-01-30 14:34:42 +00:00
{
if (w->isMovable()) {
m_active = true;
m_window = w;
effects->addRepaintFull();
}
}
void SnapHelperEffect::slotWindowFinishUserMovedResized(EffectWindow *w)
{
Q_UNUSED(w)
if (m_active) {
m_active = false;
effects->addRepaintFull();
}
2011-01-30 14:34:42 +00:00
}
void SnapHelperEffect::slotWindowResized(KWin::EffectWindow *w, const QRect &oldRect)
{
if (w == m_window) {
QRect r(oldRect);
for (int i = 0; i < effects->numScreens(); ++i) {
r.moveCenter(effects->clientArea( ScreenArea, i, 0 ).center());
effects->addRepaint(r);
}
}
}
bool SnapHelperEffect::isActive() const
{
return m_active || m_timeline.currentValue() != 0.0;
}
} // namespace