2010-10-10 20:57:23 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-10-10 20:57:23 +02:00
|
|
|
|
|
|
|
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 "tagwidget.h"
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internet/lastfmservice.h"
|
|
|
|
#include "internet/internetmimedata.h"
|
|
|
|
#include "internet/internetmodel.h"
|
2011-01-10 23:26:13 +01:00
|
|
|
#include "playlist/playlistitemmimedata.h"
|
2010-12-04 17:19:30 +01:00
|
|
|
#include "smartplaylists/generator.h"
|
2011-01-10 23:26:13 +01:00
|
|
|
#include "smartplaylists/generatormimedata.h"
|
2010-12-04 17:19:30 +01:00
|
|
|
#include "smartplaylists/querygenerator.h"
|
2010-10-10 20:57:23 +02:00
|
|
|
#include "ui/flowlayout.h"
|
2010-12-04 17:19:30 +01:00
|
|
|
#include "ui/iconloader.h"
|
2010-10-10 20:57:23 +02:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPropertyAnimation>
|
|
|
|
|
|
|
|
const int TagWidgetTag::kIconSize = 16;
|
2010-10-10 21:25:53 +02:00
|
|
|
const int TagWidgetTag::kIconTextSpacing = 8;
|
|
|
|
const int TagWidgetTag::kHPadding = 6;
|
|
|
|
const int TagWidgetTag::kVPadding = 2;
|
2010-10-10 20:57:23 +02:00
|
|
|
|
|
|
|
TagWidgetTag::TagWidgetTag(const QIcon& icon, const QString& text, QWidget* parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
text_(text),
|
|
|
|
icon_(icon),
|
|
|
|
opacity_(0.0),
|
|
|
|
animation_(new QPropertyAnimation(this, "background_opacity", this))
|
|
|
|
{
|
|
|
|
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize TagWidgetTag::sizeHint() const {
|
|
|
|
QSize text = fontMetrics().size(0, text_);
|
|
|
|
QSize icon(kIconSize + kIconTextSpacing, kIconSize);
|
|
|
|
|
2010-10-10 21:25:53 +02:00
|
|
|
return QSize(kHPadding + icon.width() + text.width() + kHPadding,
|
|
|
|
kVPadding + qMax(text.height(), icon.height()) + kVPadding);
|
2010-10-10 20:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidgetTag::set_background_opacity(float opacity) {
|
|
|
|
opacity_ = opacity;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidgetTag::enterEvent(QEvent*) {
|
|
|
|
animation_->stop();
|
|
|
|
animation_->setEndValue(1.0);
|
|
|
|
animation_->setDuration(80);
|
|
|
|
animation_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidgetTag::leaveEvent(QEvent*) {
|
|
|
|
animation_->stop();
|
|
|
|
animation_->setEndValue(0.0);
|
|
|
|
animation_->setDuration(160);
|
|
|
|
animation_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidgetTag::paintEvent(QPaintEvent*) {
|
|
|
|
QPainter p(this);
|
|
|
|
|
|
|
|
const QRect tag_rect(rect());
|
2010-10-10 21:25:53 +02:00
|
|
|
const QRect icon_rect(tag_rect.topLeft() + QPoint(kHPadding, kVPadding),
|
2010-10-10 20:57:23 +02:00
|
|
|
QSize(kIconSize, kIconSize));
|
|
|
|
const QRect text_rect(icon_rect.topRight() + QPoint(kIconTextSpacing, 0),
|
2010-10-10 21:25:53 +02:00
|
|
|
QSize(tag_rect.width() - icon_rect.right() - kIconTextSpacing - kHPadding,
|
2010-10-10 20:57:23 +02:00
|
|
|
icon_rect.height()));
|
|
|
|
|
|
|
|
// Use the tag's opacity
|
|
|
|
p.setOpacity(0.3 + opacity_ * 0.7);
|
|
|
|
|
|
|
|
// Background
|
|
|
|
QColor background_color = palette().color(QPalette::Highlight);
|
|
|
|
background_color.setAlpha(128);
|
|
|
|
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
p.setPen(QPen(palette().dark(), 1.0));
|
|
|
|
p.setBrush(background_color);
|
|
|
|
p.drawRoundedRect(tag_rect, 5, 5);
|
|
|
|
|
|
|
|
// Icon
|
|
|
|
p.drawPixmap(icon_rect, icon_.pixmap(kIconSize));
|
|
|
|
|
|
|
|
// Text
|
|
|
|
p.setOpacity(1.0);
|
|
|
|
p.setPen(palette().color(QPalette::Text));
|
|
|
|
p.drawText(text_rect, text_);
|
|
|
|
}
|
|
|
|
|
2010-10-16 17:22:14 +02:00
|
|
|
void TagWidgetTag::mouseReleaseEvent(QMouseEvent*) {
|
|
|
|
emit Clicked();
|
|
|
|
}
|
|
|
|
|
2010-12-04 17:19:30 +01:00
|
|
|
void TagWidgetTag::contextMenuEvent(QContextMenuEvent*) {
|
|
|
|
emit Clicked();
|
|
|
|
}
|
|
|
|
|
2010-10-10 20:57:23 +02:00
|
|
|
|
2010-12-04 17:19:30 +01:00
|
|
|
TagWidget::TagWidget(Type type, QWidget* parent)
|
|
|
|
: QWidget(parent),
|
2011-11-11 22:35:25 +01:00
|
|
|
type_(type)
|
2010-10-10 20:57:23 +02:00
|
|
|
{
|
2010-10-10 21:25:53 +02:00
|
|
|
setLayout(new FlowLayout(4, 6, 4));
|
2010-10-10 20:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidget::AddTag(const QString& tag) {
|
|
|
|
if (tag.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
TagWidgetTag* widget = new TagWidgetTag(icon_, tag, this);
|
2010-10-16 17:22:14 +02:00
|
|
|
connect(widget, SIGNAL(Clicked()), SLOT(TagClicked()));
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
layout()->addWidget(widget);
|
|
|
|
tags_ << widget;
|
2010-10-10 20:57:23 +02:00
|
|
|
}
|
2010-10-16 17:22:14 +02:00
|
|
|
|
|
|
|
void TagWidget::TagClicked() {
|
|
|
|
TagWidgetTag* tag = qobject_cast<TagWidgetTag*>(sender());
|
|
|
|
if (!tag)
|
|
|
|
return;
|
|
|
|
|
2011-11-11 22:35:25 +01:00
|
|
|
emit DoGlobalSearch(tag->text());
|
2010-10-16 17:22:14 +02:00
|
|
|
}
|