| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 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 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <qstyleoption.h> | 
|---|
| 43 | #include <qpainter.h> | 
|---|
| 44 | #include <qpixmapcache.h> | 
|---|
| 45 | #include <private/qmath_p.h> | 
|---|
| 46 | #include <private/qstyle_p.h> | 
|---|
| 47 | #include <qmath.h> | 
|---|
| 48 |  | 
|---|
| 49 | #if defined(Q_WS_WIN) | 
|---|
| 50 | #include "qt_windows.h" | 
|---|
| 51 | #elif defined(Q_WS_MAC) | 
|---|
| 52 | #include <private/qt_cocoa_helpers_mac_p.h> | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | #include "qstylehelper_p.h" | 
|---|
| 56 | #include <qstringbuilder.h> | 
|---|
| 57 |  | 
|---|
| 58 | QT_BEGIN_NAMESPACE | 
|---|
| 59 |  | 
|---|
| 60 | namespace QStyleHelper { | 
|---|
| 61 |  | 
|---|
| 62 | QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size) | 
|---|
| 63 | { | 
|---|
| 64 | const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(option); | 
|---|
| 65 | QString tmp = key % HexString<uint>(option->state) | 
|---|
| 66 | % HexString<uint>(option->direction) | 
|---|
| 67 | % HexString<uint>(complexOption ? uint(complexOption->activeSubControls) : 0u) | 
|---|
| 68 | % HexString<quint64>(option->palette.cacheKey()) | 
|---|
| 69 | % HexString<uint>(size.width()) | 
|---|
| 70 | % HexString<uint>(size.height()); | 
|---|
| 71 |  | 
|---|
| 72 | #ifndef QT_NO_SPINBOX | 
|---|
| 73 | if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) { | 
|---|
| 74 | tmp = tmp % HexString<uint>(spinBox->buttonSymbols) | 
|---|
| 75 | % HexString<uint>(spinBox->stepEnabled) | 
|---|
| 76 | % QLatin1Char(spinBox->frame ? '1' : '0'); ; | 
|---|
| 77 | } | 
|---|
| 78 | #endif // QT_NO_SPINBOX | 
|---|
| 79 | return tmp; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | qreal dpiScaled(qreal value) | 
|---|
| 83 | { | 
|---|
| 84 | static qreal scale = -1; | 
|---|
| 85 | if (scale < 0) { | 
|---|
| 86 | scale = 1.0; | 
|---|
| 87 | #if defined(Q_WS_WIN) | 
|---|
| 88 | { | 
|---|
| 89 | HDC hdcScreen = GetDC(0); | 
|---|
| 90 | int dpi = GetDeviceCaps(hdcScreen, LOGPIXELSX); | 
|---|
| 91 | ReleaseDC(0, hdcScreen); | 
|---|
| 92 | scale = dpi/96.0; | 
|---|
| 93 | } | 
|---|
| 94 | #elif defined(Q_WS_MAC) | 
|---|
| 95 | scale = qt_mac_get_scalefactor(); | 
|---|
| 96 | #endif | 
|---|
| 97 | } | 
|---|
| 98 | return value * scale; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | #ifndef QT_NO_DIAL | 
|---|
| 103 |  | 
|---|
| 104 | int calcBigLineSize(int radius) | 
|---|
| 105 | { | 
|---|
| 106 | int bigLineSize = radius / 6; | 
|---|
| 107 | if (bigLineSize < 4) | 
|---|
| 108 | bigLineSize = 4; | 
|---|
| 109 | if (bigLineSize > radius / 2) | 
|---|
| 110 | bigLineSize = radius / 2; | 
|---|
| 111 | return bigLineSize; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | static QPointF calcRadialPos(const QStyleOptionSlider *dial, qreal offset) | 
|---|
| 115 | { | 
|---|
| 116 | const int width = dial->rect.width(); | 
|---|
| 117 | const int height = dial->rect.height(); | 
|---|
| 118 | const int r = qMin(width, height) / 2; | 
|---|
| 119 | const int currentSliderPosition = dial->upsideDown ? dial->sliderPosition : (dial->maximum - dial->sliderPosition); | 
|---|
| 120 | qreal a = 0; | 
|---|
| 121 | if (dial->maximum == dial->minimum) | 
|---|
| 122 | a = Q_PI / 2; | 
|---|
| 123 | else if (dial->dialWrapping) | 
|---|
| 124 | a = Q_PI * 3 / 2 - (currentSliderPosition - dial->minimum) * 2 * Q_PI | 
|---|
| 125 | / (dial->maximum - dial->minimum); | 
|---|
| 126 | else | 
|---|
| 127 | a = (Q_PI * 8 - (currentSliderPosition - dial->minimum) * 10 * Q_PI | 
|---|
| 128 | / (dial->maximum - dial->minimum)) / 6; | 
|---|
| 129 | qreal xc = width / 2.0; | 
|---|
| 130 | qreal yc = height / 2.0; | 
|---|
| 131 | qreal len = r - QStyleHelper::calcBigLineSize(r) - 3; | 
|---|
| 132 | qreal back = offset * len; | 
|---|
| 133 | QPointF pos(QPointF(xc + back * qCos(a), yc - back * qSin(a))); | 
|---|
| 134 | return pos; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | qreal angle(const QPointF &p1, const QPointF &p2) | 
|---|
| 138 | { | 
|---|
| 139 | static const qreal rad_factor = 180 / Q_PI; | 
|---|
| 140 | qreal _angle = 0; | 
|---|
| 141 |  | 
|---|
| 142 | if (p1.x() == p2.x()) { | 
|---|
| 143 | if (p1.y() < p2.y()) | 
|---|
| 144 | _angle = 270; | 
|---|
| 145 | else | 
|---|
| 146 | _angle = 90; | 
|---|
| 147 | } else  { | 
|---|
| 148 | qreal x1, x2, y1, y2; | 
|---|
| 149 |  | 
|---|
| 150 | if (p1.x() <= p2.x()) { | 
|---|
| 151 | x1 = p1.x(); y1 = p1.y(); | 
|---|
| 152 | x2 = p2.x(); y2 = p2.y(); | 
|---|
| 153 | } else { | 
|---|
| 154 | x2 = p1.x(); y2 = p1.y(); | 
|---|
| 155 | x1 = p2.x(); y1 = p2.y(); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | qreal m = -(y2 - y1) / (x2 - x1); | 
|---|
| 159 | _angle = qAtan(m) *  rad_factor; | 
|---|
| 160 |  | 
|---|
| 161 | if (p1.x() < p2.x()) | 
|---|
| 162 | _angle = 180 - _angle; | 
|---|
| 163 | else | 
|---|
| 164 | _angle = -_angle; | 
|---|
| 165 | } | 
|---|
| 166 | return _angle; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | QPolygonF calcLines(const QStyleOptionSlider *dial) | 
|---|
| 170 | { | 
|---|
| 171 | QPolygonF poly; | 
|---|
| 172 | int width = dial->rect.width(); | 
|---|
| 173 | int height = dial->rect.height(); | 
|---|
| 174 | qreal r = qMin(width, height) / 2; | 
|---|
| 175 | int bigLineSize = calcBigLineSize(int(r)); | 
|---|
| 176 |  | 
|---|
| 177 | qreal xc = width / 2 + 0.5; | 
|---|
| 178 | qreal yc = height / 2 + 0.5; | 
|---|
| 179 | int ns = dial->tickInterval; | 
|---|
| 180 | int notches = (dial->maximum + ns - 1 - dial->minimum) / ns; | 
|---|
| 181 | if (notches <= 0) | 
|---|
| 182 | return poly; | 
|---|
| 183 | if (dial->maximum < dial->minimum || dial->maximum - dial->minimum > 1000) { | 
|---|
| 184 | int maximum = dial->minimum + 1000; | 
|---|
| 185 | notches = (maximum + ns - 1 - dial->minimum) / ns; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | poly.resize(2 + 2 * notches); | 
|---|
| 189 | int smallLineSize = bigLineSize / 2; | 
|---|
| 190 | for (int i = 0; i <= notches; ++i) { | 
|---|
| 191 | qreal angle = dial->dialWrapping ? Q_PI * 3 / 2 - i * 2 * Q_PI / notches | 
|---|
| 192 | : (Q_PI * 8 - i * 10 * Q_PI / notches) / 6; | 
|---|
| 193 | qreal s = qSin(angle); | 
|---|
| 194 | qreal c = qCos(angle); | 
|---|
| 195 | if (i == 0 || (((ns * i) % (dial->pageStep ? dial->pageStep : 1)) == 0)) { | 
|---|
| 196 | poly[2 * i] = QPointF(xc + (r - bigLineSize) * c, | 
|---|
| 197 | yc - (r - bigLineSize) * s); | 
|---|
| 198 | poly[2 * i + 1] = QPointF(xc + r * c, yc - r * s); | 
|---|
| 199 | } else { | 
|---|
| 200 | poly[2 * i] = QPointF(xc + (r - 1 - smallLineSize) * c, | 
|---|
| 201 | yc - (r - 1 - smallLineSize) * s); | 
|---|
| 202 | poly[2 * i + 1] = QPointF(xc + (r - 1) * c, yc -(r - 1) * s); | 
|---|
| 203 | } | 
|---|
| 204 | } | 
|---|
| 205 | return poly; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | // This will draw a nice and shiny QDial for us. We don't want | 
|---|
| 209 | // all the shinyness in QWindowsStyle, hence we place it here | 
|---|
| 210 |  | 
|---|
| 211 | void drawDial(const QStyleOptionSlider *option, QPainter *painter) | 
|---|
| 212 | { | 
|---|
| 213 | QPalette pal = option->palette; | 
|---|
| 214 | QColor buttonColor = pal.button().color(); | 
|---|
| 215 | const int width = option->rect.width(); | 
|---|
| 216 | const int height = option->rect.height(); | 
|---|
| 217 | const bool enabled = option->state & QStyle::State_Enabled; | 
|---|
| 218 | qreal r = qMin(width, height) / 2; | 
|---|
| 219 | r -= r/50; | 
|---|
| 220 | const qreal penSize = r/20.0; | 
|---|
| 221 |  | 
|---|
| 222 | painter->save(); | 
|---|
| 223 | painter->setRenderHint(QPainter::Antialiasing); | 
|---|
| 224 |  | 
|---|
| 225 | // Draw notches | 
|---|
| 226 | if (option->subControls & QStyle::SC_DialTickmarks) { | 
|---|
| 227 | painter->setPen(option->palette.dark().color().darker(120)); | 
|---|
| 228 | painter->drawLines(QStyleHelper::calcLines(option)); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | // Cache dial background | 
|---|
| 232 | BEGIN_STYLE_PIXMAPCACHE(QString::fromLatin1("qdial")); | 
|---|
| 233 | p->setRenderHint(QPainter::Antialiasing); | 
|---|
| 234 |  | 
|---|
| 235 | const qreal d_ = r / 6; | 
|---|
| 236 | const qreal dx = option->rect.x() + d_ + (width - 2 * r) / 2 + 1; | 
|---|
| 237 | const qreal dy = option->rect.y() + d_ + (height - 2 * r) / 2 + 1; | 
|---|
| 238 |  | 
|---|
| 239 | QRectF br = QRectF(dx + 0.5, dy + 0.5, | 
|---|
| 240 | int(r * 2 - 2 * d_ - 2), | 
|---|
| 241 | int(r * 2 - 2 * d_ - 2)); | 
|---|
| 242 | buttonColor.setHsv(buttonColor .hue(), | 
|---|
| 243 | qMin(140, buttonColor .saturation()), | 
|---|
| 244 | qMax(180, buttonColor.value())); | 
|---|
| 245 | QColor shadowColor(0, 0, 0, 20); | 
|---|
| 246 |  | 
|---|
| 247 | if (enabled) { | 
|---|
| 248 | // Drop shadow | 
|---|
| 249 | qreal shadowSize = qMax(1.0, penSize/2.0); | 
|---|
| 250 | QRectF shadowRect= br.adjusted(-2*shadowSize, -2*shadowSize, | 
|---|
| 251 | 2*shadowSize, 2*shadowSize); | 
|---|
| 252 | QRadialGradient shadowGradient(shadowRect.center().x(), | 
|---|
| 253 | shadowRect.center().y(), shadowRect.width()/2.0, | 
|---|
| 254 | shadowRect.center().x(), shadowRect.center().y()); | 
|---|
| 255 | shadowGradient.setColorAt(qreal(0.91), QColor(0, 0, 0, 40)); | 
|---|
| 256 | shadowGradient.setColorAt(qreal(1.0), Qt::transparent); | 
|---|
| 257 | p->setBrush(shadowGradient); | 
|---|
| 258 | p->setPen(Qt::NoPen); | 
|---|
| 259 | p->translate(shadowSize, shadowSize); | 
|---|
| 260 | p->drawEllipse(shadowRect); | 
|---|
| 261 | p->translate(-shadowSize, -shadowSize); | 
|---|
| 262 |  | 
|---|
| 263 | // Main gradient | 
|---|
| 264 | QRadialGradient gradient(br.center().x() - br.width()/3, dy, | 
|---|
| 265 | br.width()*1.3, br.center().x(), | 
|---|
| 266 | br.center().y() - br.height()/2); | 
|---|
| 267 | gradient.setColorAt(0, buttonColor.lighter(110)); | 
|---|
| 268 | gradient.setColorAt(qreal(0.5), buttonColor); | 
|---|
| 269 | gradient.setColorAt(qreal(0.501), buttonColor.darker(102)); | 
|---|
| 270 | gradient.setColorAt(1, buttonColor.darker(115)); | 
|---|
| 271 | p->setBrush(gradient); | 
|---|
| 272 | } else { | 
|---|
| 273 | p->setBrush(Qt::NoBrush); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | p->setPen(QPen(buttonColor.darker(280))); | 
|---|
| 277 | p->drawEllipse(br); | 
|---|
| 278 | p->setBrush(Qt::NoBrush); | 
|---|
| 279 | p->setPen(buttonColor.lighter(110)); | 
|---|
| 280 | p->drawEllipse(br.adjusted(1, 1, -1, -1)); | 
|---|
| 281 |  | 
|---|
| 282 | if (option->state & QStyle::State_HasFocus) { | 
|---|
| 283 | QColor highlight = pal.highlight().color(); | 
|---|
| 284 | highlight.setHsv(highlight.hue(), | 
|---|
| 285 | qMin(160, highlight.saturation()), | 
|---|
| 286 | qMax(230, highlight.value())); | 
|---|
| 287 | highlight.setAlpha(127); | 
|---|
| 288 | p->setPen(QPen(highlight, 2.0)); | 
|---|
| 289 | p->setBrush(Qt::NoBrush); | 
|---|
| 290 | p->drawEllipse(br.adjusted(-1, -1, 1, 1)); | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | END_STYLE_PIXMAPCACHE | 
|---|
| 294 |  | 
|---|
| 295 | QPointF dp = calcRadialPos(option, qreal(0.70)); | 
|---|
| 296 | buttonColor = buttonColor.lighter(104); | 
|---|
| 297 | buttonColor.setAlphaF(qreal(0.8)); | 
|---|
| 298 | const qreal ds = r/qreal(7.0); | 
|---|
| 299 | QRectF dialRect(dp.x() - ds, dp.y() - ds, 2*ds, 2*ds); | 
|---|
| 300 | QRadialGradient dialGradient(dialRect.center().x() + dialRect.width()/2, | 
|---|
| 301 | dialRect.center().y() + dialRect.width(), | 
|---|
| 302 | dialRect.width()*2, | 
|---|
| 303 | dialRect.center().x(), dialRect.center().y()); | 
|---|
| 304 | dialGradient.setColorAt(1, buttonColor.darker(140)); | 
|---|
| 305 | dialGradient.setColorAt(qreal(0.4), buttonColor.darker(120)); | 
|---|
| 306 | dialGradient.setColorAt(0, buttonColor.darker(110)); | 
|---|
| 307 | if (penSize > 3.0) { | 
|---|
| 308 | painter->setPen(QPen(QColor(0, 0, 0, 25), penSize)); | 
|---|
| 309 | painter->drawLine(calcRadialPos(option, qreal(0.90)), calcRadialPos(option, qreal(0.96))); | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | painter->setBrush(dialGradient); | 
|---|
| 313 | painter->setPen(QColor(255, 255, 255, 150)); | 
|---|
| 314 | painter->drawEllipse(dialRect.adjusted(-1, -1, 1, 1)); | 
|---|
| 315 | painter->setPen(QColor(0, 0, 0, 80)); | 
|---|
| 316 | painter->drawEllipse(dialRect); | 
|---|
| 317 | painter->restore(); | 
|---|
| 318 | } | 
|---|
| 319 | #endif //QT_NO_DIAL | 
|---|
| 320 |  | 
|---|
| 321 | void drawBorderPixmap(const QPixmap &pixmap, QPainter *painter, const QRect &rect, | 
|---|
| 322 | int left, int top, int right, | 
|---|
| 323 | int bottom) | 
|---|
| 324 | { | 
|---|
| 325 | QSize size = pixmap.size(); | 
|---|
| 326 | //painter->setRenderHint(QPainter::SmoothPixmapTransform); | 
|---|
| 327 |  | 
|---|
| 328 | //top | 
|---|
| 329 | if (top > 0) { | 
|---|
| 330 | painter->drawPixmap(QRect(rect.left() + left, rect.top(), rect.width() -right - left, top), pixmap, | 
|---|
| 331 | QRect(left, 0, size.width() -right - left, top)); | 
|---|
| 332 |  | 
|---|
| 333 | //top-left | 
|---|
| 334 | if(left > 0) | 
|---|
| 335 | painter->drawPixmap(QRect(rect.left(), rect.top(), left, top), pixmap, | 
|---|
| 336 | QRect(0, 0, left, top)); | 
|---|
| 337 |  | 
|---|
| 338 | //top-right | 
|---|
| 339 | if (right > 0) | 
|---|
| 340 | painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top(), right, top), pixmap, | 
|---|
| 341 | QRect(size.width() - right, 0, right, top)); | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 | //left | 
|---|
| 345 | if (left > 0) | 
|---|
| 346 | painter->drawPixmap(QRect(rect.left(), rect.top()+top, left, rect.height() - top - bottom), pixmap, | 
|---|
| 347 | QRect(0, top, left, size.height() - bottom - top)); | 
|---|
| 348 |  | 
|---|
| 349 | //center | 
|---|
| 350 | painter->drawPixmap(QRect(rect.left() + left, rect.top()+top, rect.width() -right - left, | 
|---|
| 351 | rect.height() - bottom - top), pixmap, | 
|---|
| 352 | QRect(left, top, size.width() -right -left, | 
|---|
| 353 | size.height() - bottom - top)); | 
|---|
| 354 | //right | 
|---|
| 355 | if (right > 0) | 
|---|
| 356 | painter->drawPixmap(QRect(rect.left() +rect.width() - right, rect.top()+top, right, rect.height() - top - bottom), pixmap, | 
|---|
| 357 | QRect(size.width() - right, top, right, size.height() - bottom - top)); | 
|---|
| 358 |  | 
|---|
| 359 | //bottom | 
|---|
| 360 | if (bottom > 0) { | 
|---|
| 361 | painter->drawPixmap(QRect(rect.left() +left, rect.top() + rect.height() - bottom, | 
|---|
| 362 | rect.width() - right - left, bottom), pixmap, | 
|---|
| 363 | QRect(left, size.height() - bottom, | 
|---|
| 364 | size.width() - right - left, bottom)); | 
|---|
| 365 | //bottom-left | 
|---|
| 366 | if (left > 0) | 
|---|
| 367 | painter->drawPixmap(QRect(rect.left(), rect.top() + rect.height() - bottom, left, bottom), pixmap, | 
|---|
| 368 | QRect(0, size.height() - bottom, left, bottom)); | 
|---|
| 369 |  | 
|---|
| 370 | //bottom-right | 
|---|
| 371 | if (right > 0) | 
|---|
| 372 | painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top() + rect.height() - bottom, right, bottom), pixmap, | 
|---|
| 373 | QRect(size.width() - right, size.height() - bottom, right, bottom)); | 
|---|
| 374 |  | 
|---|
| 375 | } | 
|---|
| 376 | } | 
|---|
| 377 | } | 
|---|
| 378 | QT_END_NAMESPACE | 
|---|