| 1 | /************************************************************************** |
| 2 | ** |
| 3 | ** This file is part of Qt Creator |
| 4 | ** |
| 5 | ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 6 | ** |
| 7 | ** Contact: Nokia Corporation ([email protected]) |
| 8 | ** |
| 9 | ** Commercial Usage |
| 10 | ** |
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in |
| 12 | ** accordance with the Qt Commercial License Agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and Nokia. |
| 15 | ** |
| 16 | ** GNU Lesser General Public License Usage |
| 17 | ** |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 2.1 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 24 | ** |
| 25 | ** If you are unsure which license is appropriate for your use, please |
| 26 | ** contact the sales department at http://qt.nokia.com/contact. |
| 27 | ** |
| 28 | **************************************************************************/ |
| 29 | |
| 30 | #include "stylehelper.h" |
| 31 | |
| 32 | #include <QApplication> |
| 33 | #include <QStyleOption> |
| 34 | #include <QWidget> |
| 35 | #include <QtCore/QObject> |
| 36 | #include <QtCore/QRect> |
| 37 | #include <QtGui/QPainter> |
| 38 | #include <QtGui/QPalette> |
| 39 | #include <QtGui/QPixmapCache> |
| 40 | |
| 41 | // Clamps float color values within (0, 255) |
| 42 | static int clamp(float x) |
| 43 | { |
| 44 | const int val = x > 255 ? 255 : static_cast<int>(x); |
| 45 | return val < 0 ? 0 : val; |
| 46 | } |
| 47 | |
| 48 | namespace Utils { |
| 49 | |
| 50 | qreal StyleHelper::sidebarFontSize() |
| 51 | { |
| 52 | #if defined(Q_WS_MAC) |
| 53 | return 10; |
| 54 | #else |
| 55 | return 7.5; |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | QColor StyleHelper::panelTextColor(bool lightColored) |
| 60 | { |
| 61 | if (!lightColored) |
| 62 | return Qt::white; |
| 63 | else |
| 64 | return Qt::black; |
| 65 | } |
| 66 | |
| 67 | // Invalid by default, setBaseColor needs to be called at least once |
| 68 | QColor StyleHelper::m_baseColor; |
| 69 | QColor StyleHelper::m_requestedBaseColor; |
| 70 | |
| 71 | QColor StyleHelper::baseColor(bool lightColored) |
| 72 | { |
| 73 | if (!lightColored) |
| 74 | return m_baseColor; |
| 75 | else |
| 76 | return m_baseColor.lighter(230); |
| 77 | } |
| 78 | |
| 79 | QColor StyleHelper::highlightColor(bool lightColored) |
| 80 | { |
| 81 | QColor result = baseColor(lightColored); |
| 82 | if (!lightColored) |
| 83 | result.setHsv(result.hue(), |
| 84 | clamp(result.saturation()), |
| 85 | clamp(result.value() * 1.16)); |
| 86 | else |
| 87 | result.setHsv(result.hue(), |
| 88 | clamp(result.saturation()), |
| 89 | clamp(result.value() * 1.06)); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | QColor StyleHelper::shadowColor(bool lightColored) |
| 94 | { |
| 95 | QColor result = baseColor(lightColored); |
| 96 | result.setHsv(result.hue(), |
| 97 | clamp(result.saturation() * 1.1), |
| 98 | clamp(result.value() * 0.70)); |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | QColor StyleHelper::borderColor(bool lightColored) |
| 103 | { |
| 104 | QColor result = baseColor(lightColored); |
| 105 | result.setHsv(result.hue(), |
| 106 | result.saturation(), |
| 107 | result.value() / 2); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | // We try to ensure that the actual color used are within |
| 112 | // reasonalbe bounds while generating the actual baseColor |
| 113 | // from the users request. |
| 114 | void StyleHelper::setBaseColor(const QColor& newcolor) |
| 115 | { |
| 116 | m_requestedBaseColor = newcolor; |
| 117 | |
| 118 | QColor color; |
| 119 | color.setHsv(newcolor.hue(), |
| 120 | newcolor.saturation() * 0.7, |
| 121 | 64 + newcolor.value() / 3); |
| 122 | |
| 123 | if (color.isValid() && color != m_baseColor) { |
| 124 | m_baseColor = color; |
| 125 | foreach (QWidget* w, QApplication::topLevelWidgets()) |
| 126 | w->update(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static void verticalGradientHelper(QPainter* p, const QRect& spanRect, const QRect& rect, bool lightColored) |
| 131 | { |
| 132 | QColor highlight = StyleHelper::highlightColor(lightColored); |
| 133 | QColor shadow = StyleHelper::shadowColor(lightColored); |
| 134 | QLinearGradient grad(spanRect.topRight(), spanRect.topLeft()); |
| 135 | grad.setColorAt(0, highlight.lighter(117)); |
| 136 | grad.setColorAt(1, shadow.darker(109)); |
| 137 | p->fillRect(rect, grad); |
| 138 | |
| 139 | QColor light(255, 255, 255, 80); |
| 140 | p->setPen(light); |
| 141 | p->drawLine(rect.topRight() - QPoint(1, 0), rect.bottomRight() - QPoint(1, 0)); |
| 142 | QColor dark(0, 0, 0, 90); |
| 143 | p->setPen(dark); |
| 144 | p->drawLine(rect.topLeft(), rect.bottomLeft()); |
| 145 | } |
| 146 | |
| 147 | void StyleHelper::verticalGradient(QPainter* painter, const QRect& spanRect, const QRect& clipRect, bool lightColored) |
| 148 | { |
| 149 | if (StyleHelper::usePixmapCache()) { |
| 150 | QString key; |
| 151 | QColor keyColor = baseColor(lightColored); |
| 152 | key.sprintf("mh_vertical %d %d %d %d %d", |
| 153 | spanRect.width(), spanRect.height(), clipRect.width(), |
| 154 | clipRect.height(), keyColor.rgb()); |
| 155 | ; |
| 156 | |
| 157 | QPixmap pixmap; |
| 158 | if (!QPixmapCache::find(key, pixmap)) { |
| 159 | pixmap = QPixmap(clipRect.size()); |
| 160 | QPainter p(&pixmap); |
| 161 | QRect rect(0, 0, clipRect.width(), clipRect.height()); |
| 162 | verticalGradientHelper(&p, spanRect, rect, lightColored); |
| 163 | p.end(); |
| 164 | QPixmapCache::insert(key, pixmap); |
| 165 | } |
| 166 | |
| 167 | painter->drawPixmap(clipRect.topLeft(), pixmap); |
| 168 | } else { |
| 169 | verticalGradientHelper(painter, spanRect, clipRect, lightColored); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Draws a cached pixmap with shadow |
| 174 | void StyleHelper::drawIconWithShadow(const QIcon& icon, const QRect& rect, |
| 175 | QPainter* p, QIcon::Mode iconMode, int radius, const QColor& color, const QPoint& offset) |
| 176 | { |
| 177 | QPixmap cache; |
| 178 | QString pixmapName = QString("icon %0 %1 %2").arg(icon.cacheKey()).arg(iconMode).arg(rect.height()); |
| 179 | |
| 180 | if (!QPixmapCache::find(pixmapName, cache)) { |
| 181 | QPixmap px = icon.pixmap(rect.size()); |
| 182 | cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)); |
| 183 | cache.fill(Qt::transparent); |
| 184 | |
| 185 | QPainter cachePainter(&cache); |
| 186 | if (iconMode == QIcon::Disabled) { |
| 187 | QImage im = px.toImage().convertToFormat(QImage::Format_ARGB32); |
| 188 | for (int y = 0; y < im.height(); ++y) { |
| 189 | QRgb* scanLine = (QRgb*)im.scanLine(y); |
| 190 | for (int x = 0; x < im.width(); ++x) { |
| 191 | QRgb pixel = *scanLine; |
| 192 | char intensity = qGray(pixel); |
| 193 | *scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel)); |
| 194 | ++scanLine; |
| 195 | } |
| 196 | } |
| 197 | px = QPixmap::fromImage(im); |
| 198 | } |
| 199 | |
| 200 | // Draw shadow |
| 201 | QImage tmp(px.size() + QSize(radius * 2, radius * 2 + 1), QImage::Format_ARGB32_Premultiplied); |
| 202 | tmp.fill(Qt::transparent); |
| 203 | |
| 204 | QPainter tmpPainter(&tmp); |
| 205 | tmpPainter.setCompositionMode(QPainter::CompositionMode_Source); |
| 206 | tmpPainter.drawPixmap(QPoint(radius, radius), px); |
| 207 | tmpPainter.end(); |
| 208 | |
| 209 | // blur the alpha channel |
| 210 | QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied); |
| 211 | blurred.fill(Qt::transparent); |
| 212 | QPainter blurPainter(&blurred); |
| 213 | qt_blurImage(&blurPainter, tmp, radius, false, true); |
| 214 | blurPainter.end(); |
| 215 | |
| 216 | tmp = blurred; |
| 217 | |
| 218 | // blacken the image... |
| 219 | tmpPainter.begin(&tmp); |
| 220 | tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
| 221 | tmpPainter.fillRect(tmp.rect(), color); |
| 222 | tmpPainter.end(); |
| 223 | |
| 224 | tmpPainter.begin(&tmp); |
| 225 | tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
| 226 | tmpPainter.fillRect(tmp.rect(), color); |
| 227 | tmpPainter.end(); |
| 228 | |
| 229 | // draw the blurred drop shadow... |
| 230 | cachePainter.drawImage(QRect(0, 0, cache.rect().width(), cache.rect().height()), tmp); |
| 231 | |
| 232 | // Draw the actual pixmap... |
| 233 | cachePainter.drawPixmap(QPoint(radius, radius) + offset, px); |
| 234 | QPixmapCache::insert(pixmapName, cache); |
| 235 | } |
| 236 | |
| 237 | QRect targetRect = cache.rect(); |
| 238 | targetRect.moveCenter(rect.center()); |
| 239 | p->drawPixmap(targetRect.topLeft() - offset, cache); |
| 240 | } |
| 241 | |
| 242 | } // namespace Utils |