add more spacing
[personal-kdebase.git] / workspace / kwin / effects / invert.cpp
blobc90ec4eba33f13fa34c17724af3b58c4211bb118
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
6 Copyright (C) 2008 Lucas Murray <lmurray@undefinedfire.com>
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 "invert.h"
24 #include <kwinglutils.h>
25 #include <kactioncollection.h>
26 #include <kaction.h>
27 #include <klocale.h>
28 #include <kdebug.h>
29 #include <kwinshadereffect.h>
30 #include <KStandardDirs>
32 namespace KWin
35 KWIN_EFFECT( invert, InvertEffect )
36 KWIN_EFFECT_SUPPORTED( invert, ShaderEffect::supported() )
38 InvertEffect::InvertEffect()
39 : m_inited( false ),
40 m_valid( true ),
41 m_shader( NULL ),
42 m_allWindows( false )
44 KActionCollection* actionCollection = new KActionCollection( this );
46 KAction* a = (KAction*)actionCollection->addAction( "Invert" );
47 a->setText( i18n( "Toggle Invert Effect" ));
48 a->setGlobalShortcut( KShortcut( Qt::CTRL + Qt::META + Qt::Key_I ));
49 connect(a, SIGNAL( triggered(bool) ), this, SLOT( toggle() ));
51 KAction* b = (KAction*)actionCollection->addAction( "InvertWindow" );
52 b->setText( i18n( "Toggle Invert Effect on Window" ));
53 b->setGlobalShortcut( KShortcut( Qt::CTRL + Qt::META + Qt::Key_U ));
54 connect(b, SIGNAL( triggered(bool) ), this, SLOT( toggleWindow() ));
57 InvertEffect::~InvertEffect()
59 delete m_shader;
62 bool InvertEffect::loadData()
64 m_inited = true;
66 QString fragmentshader = KGlobal::dirs()->findResource("data", "kwin/invert.frag");
67 QString vertexshader = KGlobal::dirs()->findResource("data", "kwin/invert.vert");
68 if(fragmentshader.isEmpty() || vertexshader.isEmpty())
70 kError(1212) << "Couldn't locate shader files" << endl;
71 return false;
74 m_shader = new GLShader(vertexshader, fragmentshader);
75 if( !m_shader->isValid() )
77 kError(1212) << "The shader failed to load!" << endl;
78 return false;
80 else
82 m_shader->bind();
83 m_shader->setUniform("winTexture", 0);
84 m_shader->unbind();
87 return true;
90 void InvertEffect::paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data )
92 // Load if we haven't already
93 if( m_valid && !m_inited )
94 m_valid = loadData();
96 bool useShader = m_valid && ( m_allWindows != m_windows.contains( w ));
97 if( useShader )
99 m_shader->bind();
101 int texw = w->width();
102 int texh = w->height();
103 if( !GLTexture::NPOTTextureSupported() )
105 kWarning( 1212 ) << "NPOT textures not supported, wasting some memory" ;
106 texw = nearestPowerOfTwo(texw);
107 texh = nearestPowerOfTwo(texh);
109 m_shader->setUniform("textureWidth", (float)texw);
110 m_shader->setUniform("textureHeight", (float)texh);
112 data.shader = m_shader;
115 effects->paintWindow( w, mask, region, data );
117 if( useShader )
118 m_shader->unbind();
121 void InvertEffect::windowClosed( EffectWindow* w )
123 m_windows.removeOne( w );
126 void InvertEffect::toggle()
128 m_allWindows = !m_allWindows;
129 effects->addRepaintFull();
132 void InvertEffect::toggleWindow()
134 if( !m_windows.contains( effects->activeWindow() ))
135 m_windows.append( effects->activeWindow() );
136 else
137 m_windows.removeOne( effects->activeWindow() );
138 effects->activeWindow()->addRepaintFull();
141 } // namespace
143 #include "invert.moc"