1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
//////////////////////////////////////////////////////////////////////////////
// oxygenblurhelper.cpp
// handle regions passed to kwin for blurring
// -------------------
//
// Copyright (c) 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
//
// Loosely inspired (and largely rewritten) from BeSpin style
// Copyright (C) 2007 Thomas Luebking <thomas.luebking@web.de>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////////
#include "oxygenblurhelper.h"
#include "oxygenstyleconfigdata.h"
#include <QEvent>
#include <QVector>
#include <QProgressBar>
#include <QPushButton>
namespace Oxygen
{
//___________________________________________________________
BlurHelper::BlurHelper( QObject* parent, StyleHelper& helper ):
QObject( parent ),
_helper( helper ),
_enabled( false )
{
#if OXYGEN_HAVE_X11
if( _helper.isX11() )
{
// create atom
_blurAtom = _helper.createAtom( QStringLiteral( "_KDE_NET_WM_BLUR_BEHIND_REGION" ) );
_opaqueAtom = _helper.createAtom( QStringLiteral( "_NET_WM_OPAQUE_REGION" ) );
} else
{
_blurAtom = 0;
_opaqueAtom = 0;
}
#endif
}
//___________________________________________________________
void BlurHelper::registerWidget( QWidget* widget )
{
// check if already registered
if( _widgets.contains( widget ) ) return;
// install event filter
addEventFilter( widget );
// add to widgets list
_widgets.insert( widget );
// cleanup on destruction
connect( widget, SIGNAL(destroyed(QObject*)), SLOT(widgetDestroyed(QObject*)) );
if( enabled() )
{
// schedule shadow area repaint
_pendingWidgets.insert( widget, widget );
update();
}
}
//___________________________________________________________
void BlurHelper::unregisterWidget( QWidget* widget )
{
// remove event filter
widget->removeEventFilter( this );
// remove from widgets
_widgets.remove( widget );
if( isTransparent( widget ) ) clear( widget );
}
//___________________________________________________________
bool BlurHelper::eventFilter( QObject* object, QEvent* event )
{
// do nothing if not enabled
if( !enabled() ) return false;
switch( event->type() )
{
case QEvent::Hide:
{
QWidget* widget( qobject_cast<QWidget*>( object ) );
if( widget && isOpaque( widget ) && isTransparent( widget->window() ) )
{
QWidget* window( widget->window() );
_pendingWidgets.insert( window, window );
update();
}
break;
}
case QEvent::Show:
case QEvent::Resize:
{
// cast to widget and check
QWidget* widget( qobject_cast<QWidget*>( object ) );
if( !widget ) break;
if( isTransparent( widget ) )
{
_pendingWidgets.insert( widget, widget );
update();
} else if( isOpaque( widget ) ) {
QWidget* window( widget->window() );
if( isTransparent( window ) )
{
_pendingWidgets.insert( window, window );
update();
}
}
break;
}
default: break;
}
// never eat events
return false;
}
//___________________________________________________________
QRegion BlurHelper::blurRegion( QWidget* widget ) const
{
if( !widget->isVisible() ) return QRegion();
// get main region
QRegion region;
if(
qobject_cast<const QDockWidget*>( widget ) ||
qobject_cast<const QMenu*>( widget ) ||
qobject_cast<const QToolBar*>( widget ) ||
widget->inherits( "QComboBoxPrivateContainer" ) )
{
region = _helper.roundedMask( widget->rect() );
} else region = widget->mask().isEmpty() ? widget->rect():widget->mask();
// trim blur region to remove unnecessary areas
trimBlurRegion( widget, widget, region );
return region;
}
//___________________________________________________________
void BlurHelper::trimBlurRegion( QWidget* parent, QWidget* widget, QRegion& region ) const
{
// loop over children
foreach( QObject* childObject, widget->children() )
{
QWidget* child( qobject_cast<QWidget*>( childObject ) );
if( !(child && child->isVisible()) ) continue;
if( isOpaque( child ) )
{
const QPoint offset( child->mapTo( parent, QPoint( 0, 0 ) ) );
if( child->mask().isEmpty() )
{
const QRect rect( child->rect().translated( offset ).adjusted( 1, 1, -1, -1 ) );
region -= rect;
} else region -= child->mask().translated( offset );
} else { trimBlurRegion( parent, child, region ); }
}
return;
}
//___________________________________________________________
void BlurHelper::update( QWidget* widget ) const
{
#if OXYGEN_HAVE_X11
if( !_helper.isX11() ) return;
/*
directly from bespin code. Supposibly prevent playing with some 'pseudo-widgets'
that have winId matching some other -random- window
*/
if( !(widget->testAttribute(Qt::WA_WState_Created) || widget->internalWinId() ))
{ return; }
const QRegion blurRegion( this->blurRegion( widget ) );
const QRegion opaqueRegion = QRegion(0, 0, widget->width(), widget->height()) - blurRegion;
if( blurRegion.isEmpty() ) {
clear( widget );
} else {
QVector<quint32> data;
foreach( const QRect& rect, blurRegion.rects() )
{ data << rect.x() << rect.y() << rect.width() << rect.height(); }
xcb_change_property( _helper.connection(), XCB_PROP_MODE_REPLACE, widget->winId(), _blurAtom, XCB_ATOM_CARDINAL, 32, data.size(), data.constData() );
data.clear();
foreach( const QRect& rect, opaqueRegion.rects() )
{ data << rect.x() << rect.y() << rect.width() << rect.height(); }
xcb_change_property( _helper.connection(), XCB_PROP_MODE_REPLACE, widget->winId(), _opaqueAtom, XCB_ATOM_CARDINAL, 32, data.size(), data.constData() );
xcb_flush( _helper.connection() );
}
// force update
if( widget->isVisible() )
{ widget->update(); }
#else
Q_UNUSED( widget )
#endif
}
//___________________________________________________________
void BlurHelper::clear( QWidget* widget ) const
{
#if OXYGEN_HAVE_X11
if( !_helper.isX11() ) return;
xcb_delete_property( _helper.connection(), widget->winId(), _blurAtom );
xcb_delete_property( _helper.connection(), widget->winId(), _opaqueAtom );
#else
Q_UNUSED( widget )
#endif
}
//___________________________________________________________
bool BlurHelper::isOpaque( const QWidget* widget ) const
{
return
(!widget->isWindow()) &&
( (widget->autoFillBackground() && widget->palette().color( widget->backgroundRole() ).alpha() == 0xff ) ||
widget->testAttribute(Qt::WA_OpaquePaintEvent) );
}
//___________________________________________________________
bool BlurHelper::isTransparent( const QWidget* widget ) const
{
return
widget->isWindow() &&
widget->testAttribute( Qt::WA_TranslucentBackground ) &&
// widgets using qgraphicsview
!( widget->graphicsProxyWidget() ||
widget->inherits( "Plasma::Dialog" ) ) &&
// flags and special widgets
( widget->testAttribute( Qt::WA_StyledBackground ) ||
qobject_cast<const QMenu*>( widget ) ||
qobject_cast<const QDockWidget*>( widget ) ||
qobject_cast<const QToolBar*>( widget ) ||
widget->windowType() == Qt::ToolTip ) &&
_helper.hasAlphaChannel( widget );
}
}
|