~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/radio/radioviewcontainer.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Clementine.
2
 
   Copyright 2010, David Sansome <me@davidsansome.com>
3
 
 
4
 
   Clementine is free software: you can redistribute it and/or modify
5
 
   it under the terms of the GNU General Public License as published by
6
 
   the Free Software Foundation, either version 3 of the License, or
7
 
   (at your option) any later version.
8
 
 
9
 
   Clementine is distributed in the hope that it will be useful,
10
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
   GNU General Public License for more details.
13
 
 
14
 
   You should have received a copy of the GNU General Public License
15
 
   along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16
 
*/
17
 
 
18
 
#include "radioviewcontainer.h"
19
 
#include "radiomodel.h"
20
 
#include "radioservice.h"
21
 
#include "ui_radioviewcontainer.h"
22
 
#include "core/mergedproxymodel.h"
23
 
 
24
 
#include <QMetaMethod>
25
 
#include <QTimeLine>
26
 
#include <QtDebug>
27
 
 
28
 
const int RadioViewContainer::kAnimationDuration = 500;
29
 
 
30
 
RadioViewContainer::RadioViewContainer(QWidget *parent)
31
 
  : QWidget(parent),
32
 
    ui_(new Ui_RadioViewContainer),
33
 
    model_(NULL),
34
 
    current_service_(NULL),
35
 
    current_header_(NULL)
36
 
{
37
 
  ui_->setupUi(this);
38
 
 
39
 
  connect(ui_->tree, SIGNAL(collapsed(QModelIndex)), SLOT(Collapsed(QModelIndex)));
40
 
  connect(ui_->tree, SIGNAL(expanded(QModelIndex)), SLOT(Expanded(QModelIndex)));
41
 
  connect(ui_->tree, SIGNAL(FocusOnFilterSignal(QKeyEvent*)), SLOT(FocusOnFilter(QKeyEvent*)));
42
 
}
43
 
 
44
 
RadioViewContainer::~RadioViewContainer() {
45
 
  delete ui_;
46
 
}
47
 
 
48
 
RadioView* RadioViewContainer::tree() const {
49
 
  return ui_->tree;
50
 
}
51
 
 
52
 
void RadioViewContainer::SetModel(RadioModel* model) {
53
 
  model_ = model;
54
 
 
55
 
  ui_->tree->setModel(model->merged_model());
56
 
 
57
 
  connect(ui_->tree->selectionModel(),
58
 
          SIGNAL(currentChanged(QModelIndex,QModelIndex)),
59
 
          SLOT(CurrentIndexChanged(QModelIndex)));
60
 
}
61
 
 
62
 
void RadioViewContainer::ServiceChanged(const QModelIndex& index) {
63
 
  RadioService* service = index.data(RadioModel::Role_Service).value<RadioService*>();
64
 
  if (!service || service == current_service_)
65
 
    return;
66
 
  current_service_ = service;
67
 
 
68
 
  QWidget* header = service->HeaderWidget();
69
 
  if (header && !headers_.contains(header)) {
70
 
    header->setParent(ui_->header_container);
71
 
    header->setMaximumHeight(0);
72
 
    ui_->header_container->layout()->addWidget(header);
73
 
    header->show();
74
 
 
75
 
    HeaderData d;
76
 
    d.visible_ = false;
77
 
    d.animation_ = new QTimeLine(kAnimationDuration, this);
78
 
    d.animation_->setFrameRange(0, header->sizeHint().height());
79
 
    connect(d.animation_, SIGNAL(frameChanged(int)), SLOT(SetHeaderHeight(int)));
80
 
 
81
 
    headers_.insert(header, d);
82
 
  }
83
 
 
84
 
  SetHeaderVisible(current_header_, false);
85
 
  current_header_ = header;
86
 
  SetHeaderVisible(current_header_, true);
87
 
}
88
 
 
89
 
void RadioViewContainer::CurrentIndexChanged(const QModelIndex& index) {
90
 
  ServiceChanged(index);
91
 
}
92
 
 
93
 
void RadioViewContainer::Collapsed(const QModelIndex& index) {
94
 
  if (model_->merged_model()->mapToSource(index).model() == model_) {
95
 
    SetHeaderVisible(current_header_, false);
96
 
    current_service_ = NULL;
97
 
    current_header_ = NULL;
98
 
  }
99
 
}
100
 
 
101
 
void RadioViewContainer::Expanded(const QModelIndex& index) {
102
 
  ServiceChanged(index);
103
 
}
104
 
 
105
 
void RadioViewContainer::SetHeaderVisible(QWidget* header, bool visible) {
106
 
  if (!header)
107
 
    return;
108
 
 
109
 
  HeaderData& d = headers_[header];
110
 
  if (d.visible_ == visible)
111
 
    return;
112
 
  d.visible_ = visible;
113
 
 
114
 
  d.animation_->setDirection(visible ? QTimeLine::Forward : QTimeLine::Backward);
115
 
  d.animation_->start();
116
 
}
117
 
 
118
 
void RadioViewContainer::FocusOnFilter(QKeyEvent* event) {
119
 
  // Beware: magic
120
 
 
121
 
  if (current_header_) {
122
 
    int slot = current_header_->metaObject()->indexOfSlot(
123
 
          QMetaObject::normalizedSignature("FocusOnFilter(QKeyEvent*)"));
124
 
    if (slot != -1) {
125
 
      current_header_->metaObject()->method(slot).invoke(
126
 
            current_header_, Q_ARG(QKeyEvent*, event));
127
 
    }
128
 
  }
129
 
}
130
 
 
131
 
void RadioViewContainer::SetHeaderHeight(int height) {
132
 
  QTimeLine* animation = qobject_cast<QTimeLine*>(sender());
133
 
  QWidget* header = NULL;
134
 
  foreach (QWidget* h, headers_.keys()) {
135
 
    if (headers_[h].animation_ == animation) {
136
 
      header = h;
137
 
      break;
138
 
    }
139
 
  }
140
 
 
141
 
  if (header)
142
 
    header->setMaximumHeight(height);
143
 
}