From bdfe6b909f11e1b90ce437632d457a089170c8d0 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Fri, 25 May 2012 23:34:56 +0100 Subject: [PATCH] Draw a slider arrow on the moodbar. --- src/moodbar/moodbarproxystyle.cpp | 109 ++++++++++++++++++++++++++---- src/moodbar/moodbarproxystyle.h | 10 ++- 2 files changed, 106 insertions(+), 13 deletions(-) diff --git a/src/moodbar/moodbarproxystyle.cpp b/src/moodbar/moodbarproxystyle.cpp index 9bab682a8..86fa27271 100644 --- a/src/moodbar/moodbarproxystyle.cpp +++ b/src/moodbar/moodbarproxystyle.cpp @@ -26,6 +26,10 @@ #include const int MoodbarProxyStyle::kNumHues = 12; +const int MoodbarProxyStyle::kMarginSize = 3; +const int MoodbarProxyStyle::kBorderSize = 1; +const int MoodbarProxyStyle::kArrowWidth = 17; +const int MoodbarProxyStyle::kArrowHeight = 13; MoodbarProxyStyle::MoodbarProxyStyle(QSlider* slider) : QProxyStyle(slider->style()), @@ -130,7 +134,7 @@ void MoodbarProxyStyle::Render( if (fade_source_.isNull()) { // Draw the normal slider into the fade source pixmap. fade_source_ = QPixmap(option->rect.size()); - fade_source_.fill(option->palette.color(QPalette::Background)); + fade_source_.fill(option->palette.color(QPalette::Active, QPalette::Background)); QPainter p(&fade_source_); QStyleOptionSlider opt_copy(*option); @@ -163,6 +167,7 @@ void MoodbarProxyStyle::Render( case MoodbarOn: EnsureMoodbarRendered(); painter->drawPixmap(option->rect, moodbar_pixmap_); + DrawArrow(option, painter); break; } } @@ -175,7 +180,7 @@ void MoodbarProxyStyle::EnsureMoodbarRendered() { } if (moodbar_pixmap_dirty_) { - moodbar_pixmap_ = MoodbarPixmap(moodbar_colors_, slider_->size()); + moodbar_pixmap_ = MoodbarPixmap(moodbar_colors_, slider_->size(), slider_->palette()); moodbar_pixmap_dirty_ = false; } } @@ -192,7 +197,7 @@ MoodbarProxyStyle::ColorList MoodbarProxyStyle::MoodbarColors( case Style_Happy: properties = StyleProperties(samples / 360 * 2, 0, 359, 150, 250); break; case Style_SystemDefault: default: { - const QColor highlight_color(palette.color(QPalette::Highlight)); + const QColor highlight_color(palette.color(QPalette::Active, QPalette::Highlight)); properties.threshold_ = samples / 360 * 3; properties.range_start_ = (highlight_color.hsvHue() - 20 + 360) % 360; @@ -251,16 +256,26 @@ MoodbarProxyStyle::ColorList MoodbarProxyStyle::MoodbarColors( return colors; } -QPixmap MoodbarProxyStyle::MoodbarPixmap(const ColorList& colors, const QSize& size) { +QPixmap MoodbarProxyStyle::MoodbarPixmap(const ColorList& colors, const QSize& size, + const QPalette& palette) { + QRect rect(QPoint(0, 0), size); + QRect border_rect(rect); + border_rect.adjust(kMarginSize, kMarginSize, -kMarginSize, -kMarginSize); + + QRect inner_rect(border_rect); + inner_rect.adjust(kBorderSize, kBorderSize, -kBorderSize, -kBorderSize); + + const QSize inner_size(inner_rect.size()); + // Sample the colors and map them to screen pixels. ColorList screen_colors; - for (int x=0; xrect.adjusted(kMarginSize, kMarginSize, -kMarginSize, kMarginSize); + + case SC_SliderHandle: { + const QStyleOptionSlider* slider_opt = + qstyleoption_cast(opt); + + const int x = + (slider_opt->sliderValue - slider_opt->minimum) * (opt->rect.width() - kArrowWidth) / + (slider_opt->maximum - slider_opt->minimum); + + return QRect(QPoint(opt->rect.left() + x, opt->rect.top()), + QSize(kArrowWidth, kArrowHeight)); + } + + default: + break; + } + } + + return QProxyStyle::subControlRect(cc, opt, sc, widget); +} + +void MoodbarProxyStyle::DrawArrow(const QStyleOptionSlider* option, + QPainter* painter) const { + // Get the dimensions of the arrow + const QRect rect = subControlRect(CC_Slider, option, SC_SliderHandle, slider_); + + // Make a polygon + QPolygon poly; + poly << rect.topLeft() + << rect.topRight() + << QPoint(rect.center().x(), rect.bottom()); + + // Draw it + painter->save(); + painter->setRenderHint(QPainter::Antialiasing); + painter->translate(0.5, 0.5); + painter->setPen(slider_->palette().color(QPalette::Active, QPalette::Highlight)); + painter->setBrush(slider_->palette().brush(QPalette::Active, QPalette::Base)); + painter->drawPolygon(poly); + painter->restore(); +} diff --git a/src/moodbar/moodbarproxystyle.h b/src/moodbar/moodbarproxystyle.h index 10fa59005..c8585da0a 100644 --- a/src/moodbar/moodbarproxystyle.h +++ b/src/moodbar/moodbarproxystyle.h @@ -40,6 +40,8 @@ public: // QProxyStyle void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const; + QRect subControlRect(ComplexControl cc, const QStyleOptionComplex* opt, + SubControl sc, const QWidget* widget) const; // QObject bool eventFilter(QObject* object, QEvent* event); @@ -53,6 +55,10 @@ public slots: private: static const int kNumHues; + static const int kMarginSize; + static const int kBorderSize; + static const int kArrowWidth; + static const int kArrowHeight; enum State { MoodbarOn, @@ -82,10 +88,12 @@ private: void Render(ComplexControl control, const QStyleOptionSlider* option, QPainter* painter, const QWidget* widget); void EnsureMoodbarRendered(); + void DrawArrow(const QStyleOptionSlider* option, QPainter* painter) const; static ColorList MoodbarColors(const QByteArray& data, MoodbarStyle style, const QPalette& palette); - static QPixmap MoodbarPixmap(const ColorList& colors, const QSize& size); + static QPixmap MoodbarPixmap(const ColorList& colors, const QSize& size, + const QPalette& palette); private slots: void FaderValueChanged(qreal value);