2010-05-09 22:18:05 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-09 22:18:05 +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/>.
|
|
|
|
*/
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internetviewcontainer.h"
|
|
|
|
#include "internetmodel.h"
|
|
|
|
#include "internetservice.h"
|
|
|
|
#include "ui_internetviewcontainer.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "core/mergedproxymodel.h"
|
2011-11-05 20:09:02 +01:00
|
|
|
#include "globalsearch/globalsearch.h"
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2011-03-13 15:14:16 +01:00
|
|
|
#include <QMetaMethod>
|
2010-05-09 22:18:05 +02:00
|
|
|
#include <QTimeLine>
|
2011-03-13 15:14:16 +01:00
|
|
|
#include <QtDebug>
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
const int InternetViewContainer::kAnimationDuration = 500;
|
2010-06-09 17:38:00 +02:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
InternetViewContainer::InternetViewContainer(QWidget *parent)
|
2010-05-09 22:18:05 +02:00
|
|
|
: QWidget(parent),
|
2011-07-15 15:27:50 +02:00
|
|
|
ui_(new Ui_InternetViewContainer),
|
2012-02-12 14:41:50 +01:00
|
|
|
app_(NULL),
|
2010-05-09 22:18:05 +02:00
|
|
|
current_service_(NULL),
|
2010-06-09 17:38:00 +02:00
|
|
|
current_header_(NULL)
|
2010-05-09 22:18:05 +02:00
|
|
|
{
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->setupUi(this);
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->tree, SIGNAL(collapsed(QModelIndex)), SLOT(Collapsed(QModelIndex)));
|
|
|
|
connect(ui_->tree, SIGNAL(expanded(QModelIndex)), SLOT(Expanded(QModelIndex)));
|
2011-03-13 15:14:16 +01:00
|
|
|
connect(ui_->tree, SIGNAL(FocusOnFilterSignal(QKeyEvent*)), SLOT(FocusOnFilter(QKeyEvent*)));
|
2010-05-10 23:50:31 +02:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
InternetViewContainer::~InternetViewContainer() {
|
2010-05-10 23:50:31 +02:00
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
InternetView* InternetViewContainer::tree() const {
|
2010-05-10 23:50:31 +02:00
|
|
|
return ui_->tree;
|
2010-05-09 22:18:05 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
void InternetViewContainer::SetApplication(Application* app) {
|
|
|
|
app_ = app;
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
ui_->tree->setModel(app_->internet_model()->merged_model());
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->tree->selectionModel(),
|
2010-05-09 22:18:05 +02:00
|
|
|
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
|
|
|
|
SLOT(CurrentIndexChanged(QModelIndex)));
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::ServiceChanged(const QModelIndex& index) {
|
|
|
|
InternetService* service = index.data(InternetModel::Role_Service).value<InternetService*>();
|
2010-06-09 17:38:00 +02:00
|
|
|
if (!service || service == current_service_)
|
|
|
|
return;
|
|
|
|
current_service_ = service;
|
|
|
|
|
|
|
|
QWidget* header = service->HeaderWidget();
|
|
|
|
if (header && !headers_.contains(header)) {
|
|
|
|
header->setParent(ui_->header_container);
|
|
|
|
header->setMaximumHeight(0);
|
|
|
|
ui_->header_container->layout()->addWidget(header);
|
|
|
|
header->show();
|
|
|
|
|
|
|
|
HeaderData d;
|
|
|
|
d.visible_ = false;
|
|
|
|
d.animation_ = new QTimeLine(kAnimationDuration, this);
|
|
|
|
d.animation_->setFrameRange(0, header->sizeHint().height());
|
|
|
|
connect(d.animation_, SIGNAL(frameChanged(int)), SLOT(SetHeaderHeight(int)));
|
|
|
|
|
|
|
|
headers_.insert(header, d);
|
2010-05-09 23:16:54 +02:00
|
|
|
}
|
2010-06-09 17:38:00 +02:00
|
|
|
|
|
|
|
SetHeaderVisible(current_header_, false);
|
|
|
|
current_header_ = header;
|
|
|
|
SetHeaderVisible(current_header_, true);
|
2010-05-09 23:16:54 +02:00
|
|
|
}
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::CurrentIndexChanged(const QModelIndex& index) {
|
2010-05-09 23:16:54 +02:00
|
|
|
ServiceChanged(index);
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::Collapsed(const QModelIndex& index) {
|
2012-06-27 01:21:57 +02:00
|
|
|
if (app_->internet_model()->merged_model()->mapToSource(index).model() == app_->internet_model()
|
|
|
|
&& index.data(InternetModel::Role_Type) == InternetModel::Type_Service) {
|
2010-06-09 17:38:00 +02:00
|
|
|
SetHeaderVisible(current_header_, false);
|
2010-05-09 23:16:54 +02:00
|
|
|
current_service_ = NULL;
|
2010-06-09 17:38:00 +02:00
|
|
|
current_header_ = NULL;
|
2010-05-09 23:16:54 +02:00
|
|
|
}
|
|
|
|
}
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::Expanded(const QModelIndex& index) {
|
2010-05-09 23:16:54 +02:00
|
|
|
ServiceChanged(index);
|
2010-05-09 22:18:05 +02:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::SetHeaderVisible(QWidget* header, bool visible) {
|
2010-06-09 17:38:00 +02:00
|
|
|
if (!header)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HeaderData& d = headers_[header];
|
|
|
|
if (d.visible_ == visible)
|
2010-05-09 22:18:05 +02:00
|
|
|
return;
|
2010-06-09 17:38:00 +02:00
|
|
|
d.visible_ = visible;
|
2010-05-09 22:18:05 +02:00
|
|
|
|
2010-06-09 17:38:00 +02:00
|
|
|
d.animation_->setDirection(visible ? QTimeLine::Forward : QTimeLine::Backward);
|
|
|
|
d.animation_->start();
|
2010-05-09 22:18:05 +02:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::FocusOnFilter(QKeyEvent* event) {
|
2011-03-13 15:14:16 +01:00
|
|
|
// Beware: magic
|
|
|
|
|
|
|
|
if (current_header_) {
|
|
|
|
int slot = current_header_->metaObject()->indexOfSlot(
|
|
|
|
QMetaObject::normalizedSignature("FocusOnFilter(QKeyEvent*)"));
|
|
|
|
if (slot != -1) {
|
|
|
|
current_header_->metaObject()->method(slot).invoke(
|
|
|
|
current_header_, Q_ARG(QKeyEvent*, event));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetViewContainer::SetHeaderHeight(int height) {
|
2010-06-09 17:38:00 +02:00
|
|
|
QTimeLine* animation = qobject_cast<QTimeLine*>(sender());
|
|
|
|
QWidget* header = NULL;
|
|
|
|
foreach (QWidget* h, headers_.keys()) {
|
|
|
|
if (headers_[h].animation_ == animation) {
|
|
|
|
header = h;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header)
|
|
|
|
header->setMaximumHeight(height);
|
2010-05-09 22:18:05 +02:00
|
|
|
}
|
2012-03-11 18:57:15 +01:00
|
|
|
|
|
|
|
void InternetViewContainer::ScrollToIndex(const QModelIndex& index) {
|
|
|
|
tree()->scrollTo(index, QTreeView::PositionAtCenter);
|
|
|
|
tree()->setCurrentIndex(index);
|
|
|
|
tree()->expand(index);
|
|
|
|
}
|