File: stylehelper.h

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 (285 lines) | stat: -rw-r--r-- 14,397 bytes parent folder | download | duplicates (7)
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
//=============================================================================
//  MuseScore
//  Linux Music Score Editor
//  $Id:$
//
//  Copyright (C) 2010 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  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, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#ifndef __HELPER_H__
#define __HELPER_H__

#include "tileset.h"
#include "colorscheme.h"
#include "animationmodes.h"
#include "cache.h"

typedef BaseCache<QColor> ColorCache;
typedef BaseCache<TileSet> TileSetCache;
typedef BaseCache<QPixmap> PixmapCache;

typedef QMap<quint32, bool> ColorMap;

//---------------------------------------------------------
//   StyleHelper
//---------------------------------------------------------

class StyleHelper {
            mutable ColorCache m_midColorCache;
            mutable ColorCache m_backgroundColorCache;
            mutable ColorCache m_backgroundTopColorCache;
            mutable ColorCache m_backgroundBottomColorCache;
            mutable ColorCache m_decoColorCache;
            mutable ColorCache m_lightColorCache;
            mutable ColorCache m_darkColorCache;
            mutable ColorCache m_shadowColorCache;
            mutable ColorCache m_backgroundRadialColorCache;


            mutable TileSetCache m_cornerCache;
            mutable TileSetCache m_slabSunkenCache;
            mutable TileSetCache m_slabInvertedCache;
            mutable TileSetCache m_holeCache;
            mutable TileSetCache m_holeFlatCache;
            mutable TileSetCache m_slopeCache;
            mutable TileSetCache m_grooveCache;
            mutable TileSetCache m_slitCache;
            mutable TileSetCache m_dockFrameCache;
            mutable TileSetCache m_scrollHoleCache;
            mutable TileSetCache m_selectionCache;

            mutable Cache<QPixmap> m_dialSlabCache;
            mutable Cache<QPixmap> m_roundSlabCache;
            mutable Cache<TileSet> m_holeFocusedCache;
            mutable Cache<TileSet> m_slabCache;
            mutable PixmapCache m_backgroundCache;
            mutable PixmapCache m_progressBarCache;
            mutable PixmapCache m_windecoButtonCache;
            mutable PixmapCache m_windecoButtonGlowCache;
            mutable PixmapCache m_dotCache;

            qreal _bgcontrast;
            qreal _contrast;
            static const qreal _shadowGain;
            static const qreal _glowBias;
            static const qreal _slabThickness;

            StatefulBrush _viewHoverBrush;
            StatefulBrush _viewFocusBrush;
            StatefulBrush _viewNegativeTextBrush;

            mutable ColorMap m_highThreshold;
            mutable ColorMap m_lowThreshold;

            void drawHole(QPainter&, const QColor&, qreal shade, int r = 7) const;

      public:
            StyleHelper();
            StatefulBrush viewHoverBrush() const {
                  return _viewHoverBrush;
                  }
            StatefulBrush viewFocusBrush() const {
                  return _viewFocusBrush;
                  }

            TileSet* hole(const QColor& color, qreal shade, int size, bool outline) const;
            TileSet* holeFlat(const QColor&, qreal shade, int size = 7) const;

            void fillHole(QPainter&, const QRect&, int size = 7) const;

            //! generic hole
            void renderHole(QPainter* p, const QColor& color, const QRect& r, bool focus = false, bool hover = false,
                            TileSet::Tiles posFlags = TileSet::Ring, bool outline = false) const {
                  renderHole(p, color, r, focus, hover, -1, AnimationNone, posFlags, outline);
                  }
            //! generic hole (with animated glow)
            void renderHole(QPainter* p, const QColor&, const QRect& r,
                            bool focus, bool hover, qreal opacity, AnimationMode animationMode,
                            TileSet::Tiles posFlags = TileSet::Ring, bool outline = false) const;
            TileSet* holeFocused(const QColor&, const QColor& glowColor, qreal shade, int size = 7, bool outline = false) const;

            inline const QColor& calcMidColor(const QColor& color) const;
            const QWidget* checkAutoFillBackground( const QWidget* ) const;

            //! returns menu background color matching position in a top level widget of given height
            const QColor& backgroundColor(const QColor& color, int height, int y) const {
                  return backgroundColor(color, qMin(qreal(1.0), qreal(y) / qMin(300, 3 * height / 4)));
                  }
            const QColor& backgroundColor(const QColor&, qreal ratio) const;

            const QColor& backgroundColor(const QColor& color, const QWidget* w, const QPoint& point) const {
                  if (!(w && w->window()) || checkAutoFillBackground(w))
                        return color;
                  else
                        return backgroundColor(color, w->window()->height(), w->mapTo(w->window(), point).y());
                  }

            const QColor& backgroundRadialColor(const QColor& color) const;
            const QColor& backgroundTopColor(const QColor& color) const;
            const QColor& backgroundBottomColor(const QColor& color) const;

            bool lowThreshold(const QColor& color) const;
            bool highThreshold(const QColor& color) const;
            static QColor alphaColor(QColor color, qreal alpha);
            const QColor& calcLightColor(const QColor& color) const;
            const QColor& calcDarkColor(const QColor& color) const;
            void drawInverseShadow(QPainter&, const QColor&, int pad, int size, qreal fuzz) const;
            void drawInverseGlow(QPainter&, const QColor&, int pad, int size, int rsize) const;
            const QColor& calcShadowColor(const QColor& color) const;
            void renderMenuBackground(QPainter* p, const QRect& clipRect, const QWidget* widget, const QPalette& pal) const {
                  renderMenuBackground(p, clipRect, widget, pal.color(widget->window()->backgroundRole()));
                  }
            // render menu background
            void renderMenuBackground(QPainter*, const QRect&, const QWidget*, const QColor&) const;
            QPixmap verticalGradient(const QColor& color, int height, int offset = 0) const;
            QPixmap radialGradient(const QColor& color, int width, int height = 64) const;
            inline bool hasAlphaChannel(const QWidget*) const;
            bool compositingActive() const {
                  return false;      // return KWindowSystem::compositingActive();
                  }
            void renderWindowBackground(QPainter* p, const QRect& clipRect, const QWidget* widget,
                                        const QPalette& pal, int y_shift = -23, int gradientHeight = 64) const {
                  renderWindowBackground( p, clipRect, widget, pal.color( widget->window()->backgroundRole() ), y_shift, gradientHeight );
                  }
            /*!
            y_shift: shift the background gradient upwards, to fit with the windec
            gradientHeight: the height of the generated gradient.
            for different heights, the gradient is translated so that it is always at the same position from the bottom
            */
            void renderWindowBackground(QPainter* p, const QRect& clipRect, const QWidget* widget,
                                        const QWidget* window, const QPalette& pal, int y_shift = -23, int gradientHeight = 64) const {
                  renderWindowBackground( p, clipRect, widget, window, pal.color( window->backgroundRole() ), y_shift, gradientHeight );
                  }

            //! render window background using a given color as a reference
            void renderWindowBackground(QPainter* p, const QRect& clipRect, const QWidget* widget,
                                        const QColor& color, int y_shift = -23, int gradientHeight = 64) const {
                  renderWindowBackground( p, clipRect, widget, widget->window(), color, y_shift, gradientHeight );
                  }

            //! render window background using a given color as a reference
            void renderWindowBackground(QPainter* p, const QRect& clipRect, const QWidget* widget,
                                        const QWidget* window, const QColor& color, int y_shift = -23, int gradientHeight = 64) const;
            void renderDot(QPainter*, const QPoint&, const QColor&) const;
            void drawSeparator(QPainter* p, const QRect& r, const QColor& color, Qt::Orientation orientation) const;
            const QColor& decoColor(const QColor& background, const QColor& color) const;
            //! returns a region matching given rect, with rounded corners, based on the multipliers
            /*! setting any of the multipliers to zero will result in no corners shown on the corresponding side */
            QRegion roundedMask( const QRect&, int left = 1, int right = 1, int top = 1, int bottom = 1 ) const;

            //!@name slabs
            //@{

            void fillSlab(QPainter&, const QRect&, int size = 7) const;

            // progressbar
            QPixmap progressBarIndicator(const QPalette&, const QRect&) const;

            QPixmap dialSlab(const QColor&, qreal shade, int size = 7) const;
            QPixmap dialSlabFocused(const QColor&, const QColor&, qreal shade, int size = 7) const;
            QPixmap roundSlab(const QColor&, qreal shade, int size = 7) const;
            QPixmap roundSlabFocused(const QColor&, const QColor& glowColor, qreal shade, int size = 7) const;

            TileSet* slabFocused(const QColor&, const QColor& glowColor, qreal shade, int size = 7) const;
            TileSet* slabSunken(const QColor&, qreal shade, int size = 7) const;
            TileSet* slabInverted(const QColor&, qreal shade, int size = 7) const;
            TileSet* slab(const QColor&, qreal shade, int size = 7) const;

            //! draw frame that mimics some sort of shadows around a panel
            /*! it is used for menus, detached dock panels and toolbar, as well as window decoration when compositing is disabled */
            void drawFloatFrame(
                  QPainter* p, const QRect r, const QColor& color,
                  bool drawUglyShadow = true, bool isActive = false,
                  const QColor& frameColor = QColor(),
                  TileSet::Tiles tiles = TileSet::Ring) const;
            TileSet* scrollHole(const QColor&, Qt::Orientation orientation, bool smallShadow = false) const;
            //! round corners (used for Menus, combobox drop-down, detached toolbars and dockwidgets
            TileSet* roundCorner(const QColor&, int size = 5) const;

            //! groupbox background
            TileSet* slope(const QColor&, qreal shade, int size = 7) const;
            TileSet* selection( const QColor&, int height, bool custom) const;
            //! focus rect for flat toolbuttons
            TileSet* slitFocused(const QColor&) const;
            void drawSlab(QPainter&, const QColor&, qreal shade) const;
            void drawShadow(QPainter&, const QColor&, int size) const;
            void drawOuterGlow(QPainter&, const QColor&, int size) const;
            void drawRoundSlab( QPainter&, const QColor&, qreal ) const;

            //! returns menu background color matching position in a given menu widget
            const QColor& menuBackgroundColor(const QColor& color, const QWidget* w, const QPoint& point) const {
                  if ( !( w && w->window() ) || checkAutoFillBackground( w ) )
                        return color;
                  else
                        return menuBackgroundColor( color, w->window()->height(), w->mapTo( w->window(), point ).y() );
                  }
            //! returns menu background color matching position in a menu widget of given height
            const QColor& menuBackgroundColor(const QColor& color, int height, int y) const {
                  return backgroundColor( color, qMin(qreal(1.0), qreal(y) / qMin(200, 3 * height / 4) ) );
                  }
            //! scrollbar groove
            TileSet* groove(const QColor&, qreal shade, int size = 7) const;
            QPalette mergePalettes( const QPalette&, qreal ratio ) const;
            TileSet* dockFrame(const QColor&, int size);
            QPixmap windecoButton(const QColor& color, bool pressed, int size = 21) const;
            //! negative text brush (used for close button hover)
            const StatefulBrush& viewNegativeTextBrush( void ) const {
                  return _viewNegativeTextBrush;
                  }
            void invalidateCaches();
            void setMaxCacheSize(int value);
            void reloadConfig();
      };

//---------------------------------------------------------
//   calcMidColor
//---------------------------------------------------------

const QColor& StyleHelper::calcMidColor(const QColor& color) const {
      const quint64 key(color.rgba());
      QColor* out = m_midColorCache.object(key);
      if (!out) {
            out = new QColor(ColorScheme::shade(color, ColorScheme::MidShade, _contrast - 1.0));
            m_midColorCache.insert(key, out);
            }
      return *out;
      }

//---------------------------------------------------------
//   hasAlphaChannel
//---------------------------------------------------------

bool StyleHelper::hasAlphaChannel( const QWidget* widget ) const {
#if 0
//#ifdef Q_OS_LINUX
      if (compositingActive()) {
            if (widget)
                  return widget->x11Info().depth() == 32;
            else
                  return QX11Info().appDepth() == 32;
            }
      else
            return false;
#else
      Q_UNUSED(widget)
      return compositingActive();
#endif
      }


#endif