2010-10-07 23:06:26 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "collapsibleinfoheader.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
2010-10-11 21:49:12 +02:00
|
|
|
#include <QPropertyAnimation>
|
2010-10-07 23:06:26 +02:00
|
|
|
#include <QStyleOption>
|
|
|
|
|
|
|
|
const int CollapsibleInfoHeader::kHeight = 20;
|
2010-10-09 14:39:49 +02:00
|
|
|
const int CollapsibleInfoHeader::kIconSize = 16;
|
2010-10-07 23:06:26 +02:00
|
|
|
|
|
|
|
CollapsibleInfoHeader::CollapsibleInfoHeader(QWidget* parent)
|
|
|
|
: QWidget(parent),
|
2010-10-11 21:49:12 +02:00
|
|
|
expanded_(false),
|
|
|
|
hovering_(false),
|
|
|
|
animation_(new QPropertyAnimation(this, "opacity", this)),
|
|
|
|
opacity_(0.0)
|
2010-10-07 23:06:26 +02:00
|
|
|
{
|
|
|
|
setMinimumHeight(kHeight);
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
setCursor(QCursor(Qt::PointingHandCursor));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollapsibleInfoHeader::SetTitle(const QString& title) {
|
|
|
|
title_ = title;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:39:49 +02:00
|
|
|
void CollapsibleInfoHeader::SetIcon(const QIcon& icon) {
|
|
|
|
icon_ = icon;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2010-10-07 23:06:26 +02:00
|
|
|
void CollapsibleInfoHeader::SetExpanded(bool expanded) {
|
|
|
|
expanded_ = expanded;
|
|
|
|
|
|
|
|
emit ExpandedToggled(expanded);
|
|
|
|
if (expanded)
|
|
|
|
emit Expanded();
|
|
|
|
else
|
|
|
|
emit Collapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollapsibleInfoHeader::enterEvent(QEvent*) {
|
|
|
|
hovering_ = true;
|
2010-10-11 21:49:12 +02:00
|
|
|
if (!expanded_) {
|
|
|
|
animation_->stop();
|
|
|
|
animation_->setEndValue(1.0);
|
|
|
|
animation_->setDuration(80);
|
|
|
|
animation_->start();
|
|
|
|
}
|
2010-10-07 23:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollapsibleInfoHeader::leaveEvent(QEvent*) {
|
|
|
|
hovering_ = false;
|
2010-10-11 21:49:12 +02:00
|
|
|
if (!expanded_) {
|
|
|
|
animation_->stop();
|
|
|
|
animation_->setEndValue(0.0);
|
|
|
|
animation_->setDuration(160);
|
|
|
|
animation_->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollapsibleInfoHeader::set_opacity(float opacity) {
|
|
|
|
opacity_ = opacity;
|
2010-10-07 23:06:26 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollapsibleInfoHeader::paintEvent(QPaintEvent* e) {
|
|
|
|
QPainter p(this);
|
|
|
|
|
2010-10-11 21:49:12 +02:00
|
|
|
QColor active_text_color(palette().color(QPalette::Active, QPalette::HighlightedText));
|
|
|
|
QColor inactive_text_color(palette().color(QPalette::Active, QPalette::Text));
|
|
|
|
QColor text_color;
|
|
|
|
if (expanded_) {
|
|
|
|
text_color = active_text_color;
|
|
|
|
} else {
|
|
|
|
p.setOpacity(0.4 + opacity_ * 0.6);
|
|
|
|
text_color = QColor(
|
|
|
|
active_text_color.red() * opacity_ + inactive_text_color.red() * (1.0 - opacity_),
|
|
|
|
active_text_color.green() * opacity_ + inactive_text_color.green() * (1.0 - opacity_),
|
|
|
|
active_text_color.blue() * opacity_ + inactive_text_color.blue() * (1.0 - opacity_));
|
|
|
|
}
|
|
|
|
|
2010-10-07 23:06:26 +02:00
|
|
|
QRect indicator_rect(0, 0, height(), height());
|
2010-10-09 14:39:49 +02:00
|
|
|
QRect icon_rect(height() + 2, (kHeight - kIconSize) / 2, kIconSize, kIconSize);
|
2010-10-07 23:06:26 +02:00
|
|
|
QRect text_rect(rect());
|
2010-10-09 14:39:49 +02:00
|
|
|
text_rect.setLeft(icon_rect.right() + 4);
|
2010-10-07 23:06:26 +02:00
|
|
|
|
|
|
|
// Draw the background
|
2010-10-11 20:15:09 +02:00
|
|
|
QColor highlight(palette().color(QPalette::Active, QPalette::Highlight));
|
|
|
|
const QColor bg_color_1(highlight.lighter(120));
|
|
|
|
const QColor bg_color_2(highlight.darker(120));
|
2010-10-07 23:06:26 +02:00
|
|
|
const QColor bg_border(palette().color(QPalette::Dark));
|
|
|
|
QLinearGradient bg_brush(rect().topLeft(), rect().bottomLeft());
|
|
|
|
bg_brush.setColorAt(0.0, bg_color_1);
|
|
|
|
bg_brush.setColorAt(0.5, bg_color_1);
|
|
|
|
bg_brush.setColorAt(0.5, bg_color_2);
|
|
|
|
bg_brush.setColorAt(1.0, bg_color_2);
|
|
|
|
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.fillRect(rect(), bg_brush);
|
|
|
|
|
|
|
|
p.setPen(bg_border);
|
|
|
|
p.drawLine(rect().topLeft(), rect().topRight());
|
|
|
|
p.drawLine(rect().bottomLeft(), rect().bottomRight());
|
|
|
|
|
|
|
|
// Draw the expand/collapse indicator
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.initFrom(this);
|
|
|
|
opt.rect = indicator_rect;
|
|
|
|
opt.state |= QStyle::State_Children;
|
|
|
|
if (expanded_)
|
|
|
|
opt.state |= QStyle::State_Open;
|
|
|
|
if (hovering_)
|
|
|
|
opt.state |= QStyle::State_Active;
|
|
|
|
|
|
|
|
// Have to use the application's style here because using the widget's style
|
|
|
|
// will trigger QStyleSheetStyle's recursion guard (I don't know why).
|
|
|
|
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, &p, this);
|
|
|
|
|
2010-10-09 14:39:49 +02:00
|
|
|
// Draw the icon
|
|
|
|
p.drawPixmap(icon_rect, icon_.pixmap(kIconSize));
|
|
|
|
|
2010-10-07 23:06:26 +02:00
|
|
|
// Draw the title text
|
|
|
|
QFont bold_font(font());
|
|
|
|
bold_font.setBold(true);
|
|
|
|
p.setFont(bold_font);
|
|
|
|
|
2010-10-11 21:49:12 +02:00
|
|
|
p.setPen(text_color);
|
2010-10-07 23:06:26 +02:00
|
|
|
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, title_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollapsibleInfoHeader::mouseReleaseEvent(QMouseEvent* e) {
|
|
|
|
SetExpanded(!expanded_);
|
|
|
|
}
|