File: frameshadow.cpp

package info (click to toggle)
musescore 2.0.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 202,532 kB
  • ctags: 58,769
  • sloc: cpp: 257,595; xml: 172,226; ansic: 139,931; python: 6,565; sh: 6,383; perl: 423; makefile: 290; awk: 142; pascal: 67; sed: 3
file content (504 lines) | stat: -rw-r--r-- 17,479 bytes parent folder | download | duplicates (2)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//////////////////////////////////////////////////////////////////////////////
// oxygenframeshadow.h
// handle sunken frames' shadows
// -------------------
//
// Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
//
// Largely inspired from skulpture widget style
// Copyright (c) 2007-2009 Christoph Feck <christoph@maxiom.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 "frameshadow.h"
#include "colorutils.h"

//---------------------------------------------------------
//   registerWidget
//---------------------------------------------------------

bool FrameShadowFactory::registerWidget( QWidget* widget, StyleHelper& helper ) {
      if (!widget)
            return false;
      if (isRegistered(widget))
            return false;

      // check whether widget is a frame, and has the proper shape
      bool accepted = false;
      bool flat = false;

      // cast to frame and check
      QFrame* frame(qobject_cast<QFrame*>(widget));
      if (!frame)
            return false;

      // also do not install on QSplitter
      /*
       due to Qt, splitters are set with a frame style that matches the condition below,
       though no shadow should be installed, obviously
       */
      if (qobject_cast<QSplitter*>(widget))
            return false;

      // further checks on frame shape, and parent
      if ( frame->frameStyle() == (QFrame::StyledPanel | QFrame::Sunken) )
            accepted = true;
      else if (widget->parent() && widget->parent()->inherits("QComboBoxPrivateContainer")) {
            accepted = true;
            flat = true;
            }

      if (!accepted)
            return false;

      // store in set
      _registeredWidgets.insert( widget );

      // catch object destruction
      connect(widget, SIGNAL(destroyed(QObject*)), SLOT(widgetDestroyed(QObject*)));

      // install shadow
      installShadows(widget, helper, flat);

      return true;
      }

//---------------------------------------------------------
//   unregisterWidget
//---------------------------------------------------------

void FrameShadowFactory::unregisterWidget( QWidget* widget ) {
      if (!isRegistered(widget))
            return;
      _registeredWidgets.remove(widget);
      removeShadows(widget);
      }

//---------------------------------------------------------
//   widgetDestroyed
//---------------------------------------------------------

void FrameShadowFactory::widgetDestroyed( QObject* o ) {
      _registeredWidgets.remove( o );
      }

//---------------------------------------------------------
//   installShadows
//---------------------------------------------------------

void FrameShadowFactory::installShadows(QWidget* widget, StyleHelper& helper, bool flat) {
      removeShadows(widget);

      widget->installEventFilter(this);
      if (!flat) {
            installShadow( widget, helper, Left );
            installShadow( widget, helper, Right );
            }

      installShadow( widget, helper, Top, flat );
      installShadow( widget, helper, Bottom, flat );
      }

//---------------------------------------------------------
//   removeShadows
//---------------------------------------------------------

void FrameShadowFactory::removeShadows( QWidget* widget ) {
      widget->removeEventFilter(this);

      const QList<QObject* > children = widget->children();
      foreach(QObject * child, children) {
            if (FrameShadowBase* shadow = qobject_cast<FrameShadowBase*>(child)) {
                  shadow->hide();
                  shadow->setParent(0);
                  shadow->deleteLater();
                  }
            }
      }

//---------------------------------------------------------
//   eventFilter
//---------------------------------------------------------

bool FrameShadowFactory::eventFilter( QObject* object, QEvent* event ) {
      switch ( event->type() ) {
                  // TODO: possibly implement ZOrderChange event, to make sure that
                  // the shadow is always painted on top
            case QEvent::ZOrderChange: {
                  raiseShadows( object );
                  break;
                  }
            case QEvent::Show:
                  updateShadowsGeometry( object );
                  update( object );
                  break;
            case QEvent::Resize:
                  updateShadowsGeometry( object );
                  break;
            default:
                  break;
            }
      return QObject::eventFilter( object, event );
      }

//---------------------------------------------------------
//   updateShadowsGeometry
//---------------------------------------------------------

void FrameShadowFactory::updateShadowsGeometry(QObject* object) const {
      const QList<QObject*> children = object->children();
      foreach(QObject * child, children) {
            if (FrameShadowBase* shadow = qobject_cast<FrameShadowBase*>(child)) {
                  shadow->updateGeometry();
                  }
            }
      }

//---------------------------------------------------------
//   raiseShadows
//---------------------------------------------------------

void FrameShadowFactory::raiseShadows(QObject* object) const {
      const QList<QObject*> children = object->children();
      foreach(QObject * child, children) {
            if (FrameShadowBase* shadow = qobject_cast<FrameShadowBase*>(child)) {
                  shadow->raise();
                  }
            }
      }

//---------------------------------------------------------
//   update
//---------------------------------------------------------

void FrameShadowFactory::update( QObject* object ) const {
      const QList<QObject* > children = object->children();
      foreach( QObject * child, children ) {
            if (FrameShadowBase* shadow = qobject_cast<FrameShadowBase*>(child)) {
                  shadow->update();
                  }
            }
      }

//---------------------------------------------------------
//   updateState
//---------------------------------------------------------

void FrameShadowFactory::updateState(const QWidget* widget, bool focus, bool hover, qreal opacity, AnimationMode mode) const {
      const QList<QObject*> children = widget->children();
      foreach (QObject * child, children) {
            if (FrameShadowBase* shadow = qobject_cast<FrameShadowBase*>(child)) {
                  shadow->updateState( focus, hover, opacity, mode );
                  }
            }
      }

//---------------------------------------------------------
//   installShadow
//---------------------------------------------------------

void FrameShadowFactory::installShadow( QWidget* widget, StyleHelper& helper, ShadowArea area, bool flat ) const {
      FrameShadowBase* shadow(0);
      if (flat)
            shadow = new FlatFrameShadow(area, helper);
      else
            shadow = new SunkenFrameShadow(area, helper);
      shadow->setParent(widget);
      shadow->updateGeometry();
      shadow->show();
      }

//---------------------------------------------------------
//   init
//---------------------------------------------------------

void FrameShadowBase::init() {
      setAttribute(Qt::WA_OpaquePaintEvent, false);

      setFocusPolicy(Qt::NoFocus);
      setAttribute(Qt::WA_TransparentForMouseEvents, true);
      setContextMenuPolicy(Qt::NoContextMenu);

      // grab viewport widget
      QWidget* viewport( FrameShadowBase::viewport() );
      // set cursor from viewport
      if (viewport)
            setCursor(viewport->cursor());
      }

//---------------------------------------------------------
//   viewport
//---------------------------------------------------------

QWidget* FrameShadowBase::viewport() const {
      if (!parentWidget())
            return 0;

      if (QAbstractScrollArea* widget = qobject_cast<QAbstractScrollArea*>(parentWidget()))
            return widget->viewport();
      else
            return 0;
      }

//---------------------------------------------------------
//   event
//---------------------------------------------------------

bool FrameShadowBase::event(QEvent* e) {
      // paintEvents are handled separately
      if (e->type() == QEvent::Paint)
            return QWidget::event(e);

      QWidget* viewport(FrameShadowBase::viewport());

      switch (e->type()) {
            case QEvent::DragEnter:
            case QEvent::DragMove:
            case QEvent::DragLeave:
            case QEvent::Drop:
                  if ( viewport ) {
                        setAcceptDrops(viewport->acceptDrops());
                        return viewport->QObject::event(e);
                        }
                  break;

            case QEvent::Enter:
                  if ( viewport ) {
                        setCursor(viewport->cursor());
                        setAcceptDrops(viewport->acceptDrops());
                        }
                  break;

            case QEvent::ContextMenu:
                  if ( viewport ) {
                        QContextMenuEvent* me = static_cast<QContextMenuEvent*>(e);
                        QContextMenuEvent* ne = new QContextMenuEvent(me->reason(), parentWidget()->mapFromGlobal(me->globalPos()), me->globalPos());
                        QApplication::sendEvent(viewport, ne);
                        e->accept();
                        return true;
                        }
                  break;

            case QEvent::MouseButtonPress:
                  releaseMouse();
            case QEvent::MouseMove:
            case QEvent::MouseButtonRelease:
                  if ( viewport ) {
                        QMouseEvent* me = static_cast<QMouseEvent*>(e);
                        QMouseEvent* ne = new QMouseEvent(e->type(), parentWidget()->mapFromGlobal(me->globalPos()), me->globalPos(), me->button(), me->buttons(), me->modifiers());
                        QApplication::sendEvent(viewport, ne);
                        e->accept();
                        return true;
                        }
                  break;

            default:
                  break;
            }
      e->ignore();
      return false;
      }

//---------------------------------------------------------
//   updateGeometry
//---------------------------------------------------------

void SunkenFrameShadow::updateGeometry() {
      QWidget* widget = parentWidget();
      if (!widget)
            return;

      QRect cr = widget->contentsRect();
      switch (shadowArea()) {
            case Top:
                  cr.setHeight( SHADOW_SIZE_TOP );
                  cr.adjust( -1, -1, 1, 0 );
                  break;
            case Left:
                  cr.setWidth(SHADOW_SIZE_LEFT);
                  cr.adjust(-1, SHADOW_SIZE_TOP, 0, -SHADOW_SIZE_BOTTOM);
                  break;
            case Bottom:
                  cr.setTop(cr.bottom() - SHADOW_SIZE_BOTTOM + 1);
                  cr.adjust( -1, 0, 1, 1 );
                  break;
            case Right:
                  cr.setLeft(cr.right() - SHADOW_SIZE_RIGHT + 1);
                  cr.adjust(0, SHADOW_SIZE_TOP, 1, -SHADOW_SIZE_BOTTOM);
                  break;
            case UnknownArea:
            default:
                  return;
            }
      setGeometry(cr);
      }

//---------------------------------------------------------
//   updateState
//---------------------------------------------------------

void SunkenFrameShadow::updateState( bool focus, bool hover, qreal opacity, AnimationMode mode ) {
      bool changed = false;
      if (_focus != focus) {
            _focus = focus;
            changed |= true;
            }
      if (_hover != hover) {
            _hover = hover;
            changed |= !_focus;
            }
      if (_mode != mode) {
            _mode = mode;
            changed |=
                  (_mode == AnimationNone) ||
                  (_mode == AnimationFocus) ||
                  (_mode == AnimationHover && !_focus );
            }

      if (_opacity != opacity) {
            _opacity = opacity;
            changed |= (_mode != AnimationNone );
            }
      if (changed) {
            if (QWidget* viewport = this->viewport()) {
                  // need to disable viewport updates to avoid some redundant painting
                  // besides it fixes one visual glitch (from Qt) in QTableViews
                  viewport->setUpdatesEnabled( false );
                  update() ;
                  viewport->setUpdatesEnabled( true );
                  }
            else
                  update();
            }
      }

//---------------------------------------------------------
//   paintEvent
//---------------------------------------------------------

void SunkenFrameShadow::paintEvent(QPaintEvent* event ) {
      // this fixes shadows in frames that change frameStyle() after polish()
      if (QFrame* frame = qobject_cast<QFrame*>(parentWidget())) {
            if (frame->frameStyle() != (QFrame::StyledPanel | QFrame::Sunken))
                  return;
            }

      QWidget* parent = parentWidget();
      QRect r = parent->contentsRect();
      r.translate(mapFromParent(QPoint(0, 0)));

      QColor base( palette().color(QPalette::Window) );
      TileSet::Tiles tiles;
      switch ( shadowArea() ) {
            case Top: {
                  tiles = TileSet::Left | TileSet::Top | TileSet::Right;
                  r.adjust( -2, -2, 2, -1 );
                  break;
                  }

            case Bottom: {
                  tiles = TileSet::Left | TileSet::Bottom | TileSet::Right;
                  r.adjust( -2, 1, 2, 2 );
                  break;
                  }

            case Left: {
                  tiles = TileSet::Left;
                  r.adjust( -2, -3, -1, 3 );
                  break;
                  }

            case Right: {
                  tiles = TileSet::Right;
                  r.adjust( -1, -3, 2, 3 );
                  break;
                  }

            default:
                  return;
            }

      QPainter painter(this);
      painter.setClipRegion( event->region() );
      _helper.renderHole( &painter, palette().color( QPalette::Window ), r, _focus, _hover, _opacity, _mode, tiles, true );
      }

//---------------------------------------------------------
//   updateGeometry
//---------------------------------------------------------

void FlatFrameShadow::updateGeometry() {
      QWidget* widget = parentWidget();
      if (!widget)
            return;

      QRect cr = widget->contentsRect();
      switch (shadowArea()) {
            case Top:
                  cr.setHeight( SHADOW_SIZE_TOP - 3 );
                  break;

            case Bottom:
                  cr.setTop(cr.bottom() - SHADOW_SIZE_BOTTOM + 4);
                  break;

            case UnknownArea:
            default:
                  return;
            }
      setGeometry(cr);
      }

//---------------------------------------------------------
//   paintEvent
//---------------------------------------------------------

void FlatFrameShadow::paintEvent(QPaintEvent* event ) {
      // this fixes shadows in frames that change frameStyle() after polish()
      if (QFrame* frame = qobject_cast<QFrame*>(parentWidget())) {
            if (frame->frameStyle() != (QFrame::NoFrame))
                  return;
            }
      QWidget* parent = parentWidget();
      QImage pm(size(),  QImage::Format_ARGB32_Premultiplied);

            {
            pm.fill(Qt::transparent);
            QPainter p(&pm);
            p.setClipRegion(event->region());
            p.setRenderHints(QPainter::Antialiasing);
            p.translate(-geometry().topLeft());
            p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
            p.setPen(Qt::NoPen);
            _helper.renderMenuBackground(&p, geometry(), parent, parent->palette());

            // mask
            p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
            p.setBrush(Qt::black);
            p.drawRoundedRect(QRectF(parent->contentsRect()), 2.5, 2.5);
            }

      QPainter p(this);
      p.setClipRegion(event->region());
      p.fillRect(rect(), Qt::transparent);
      p.drawPixmap(QPoint(0, 0), QPixmap::fromImage(pm));
      return;
      }