2010-03-24 00:11:46 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2010-06-25 00:45:30 +02:00
|
|
|
#include "librarydirectorymodel.h"
|
2010-05-09 02:10:26 +02:00
|
|
|
#include "librarymodel.h"
|
2009-12-24 20:16:07 +01:00
|
|
|
#include "libraryview.h"
|
|
|
|
#include "libraryitem.h"
|
2010-05-09 02:10:26 +02:00
|
|
|
#include "librarybackend.h"
|
2010-07-31 18:12:16 +02:00
|
|
|
#include "core/deletefiles.h"
|
|
|
|
#include "core/musicstorage.h"
|
2010-07-19 23:16:22 +02:00
|
|
|
#include "devices/devicemanager.h"
|
2010-07-24 20:31:05 +02:00
|
|
|
#include "devices/devicestatefiltermodel.h"
|
2010-05-19 17:45:29 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-06-25 00:45:30 +02:00
|
|
|
#include "ui/organisedialog.h"
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
#include <QPainter>
|
2010-03-21 00:59:39 +01:00
|
|
|
#include <QContextMenuEvent>
|
|
|
|
#include <QMenu>
|
2010-07-31 18:12:16 +02:00
|
|
|
#include <QMessageBox>
|
2010-03-21 00:59:39 +01:00
|
|
|
#include <QSortFilterProxyModel>
|
2010-04-01 02:12:25 +02:00
|
|
|
#include <QSettings>
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-04-01 02:12:25 +02:00
|
|
|
const char* LibraryView::kSettingsGroup = "LibraryView";
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
LibraryItemDelegate::LibraryItemDelegate(QObject *parent)
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const {
|
|
|
|
LibraryItem::Type type =
|
2010-05-09 02:10:26 +02:00
|
|
|
static_cast<LibraryItem::Type>(index.data(LibraryModel::Role_Type).toInt());
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case LibraryItem::Type_Divider: {
|
2010-04-08 22:30:50 +02:00
|
|
|
QString text(index.data().toString());
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
// Draw the background
|
|
|
|
//QStyledItemDelegate::paint(painter, opt, QModelIndex());
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
|
|
// Draw the text
|
|
|
|
QFont bold_font(opt.font);
|
|
|
|
bold_font.setBold(true);
|
|
|
|
|
|
|
|
QRect text_rect(opt.rect);
|
|
|
|
text_rect.setLeft(text_rect.left() + 30);
|
|
|
|
|
|
|
|
painter->setPen(opt.palette.color(QPalette::Text));
|
|
|
|
painter->setFont(bold_font);
|
|
|
|
painter->drawText(text_rect, text);
|
|
|
|
|
|
|
|
//Draw the line under the item
|
2010-02-21 23:28:19 +01:00
|
|
|
QPen line_pen(opt.palette.color(QPalette::Dark));
|
2009-12-24 20:16:07 +01:00
|
|
|
line_pen.setWidth(2);
|
|
|
|
|
|
|
|
painter->setPen(line_pen);
|
|
|
|
painter->drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight());
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
QStyledItemDelegate::paint(painter, opt, index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryView::LibraryView(QWidget* parent)
|
2010-05-17 01:44:33 +02:00
|
|
|
: AutoExpandingTreeView(parent),
|
2009-12-24 20:16:07 +01:00
|
|
|
library_(NULL),
|
|
|
|
total_song_count_(-1),
|
2010-03-21 00:59:39 +01:00
|
|
|
nomusic_(":nomusic.png"),
|
2010-05-28 14:52:22 +02:00
|
|
|
context_menu_(new QMenu(this)),
|
|
|
|
is_in_keyboard_search_(false)
|
2009-12-24 20:16:07 +01:00
|
|
|
{
|
|
|
|
setItemDelegate(new LibraryItemDelegate(this));
|
2010-01-17 22:22:56 +01:00
|
|
|
|
2010-05-19 17:45:29 +02:00
|
|
|
load_ = context_menu_->addAction(IconLoader::Load("media-playback-start"),
|
2010-05-15 19:45:04 +02:00
|
|
|
tr("Load"), this, SLOT(Load()));
|
2010-05-19 17:45:29 +02:00
|
|
|
add_to_playlist_ = context_menu_->addAction(IconLoader::Load("media-playback-start"),
|
2010-04-04 15:31:21 +02:00
|
|
|
tr("Add to playlist"), this, SLOT(AddToPlaylist()));
|
|
|
|
context_menu_->addSeparator();
|
2010-06-25 00:45:30 +02:00
|
|
|
organise_ = context_menu_->addAction(IconLoader::Load("edit-copy"),
|
|
|
|
tr("Organise files..."), this, SLOT(Organise()));
|
2010-07-19 23:16:22 +02:00
|
|
|
copy_to_device_ = context_menu_->addAction(IconLoader::Load("multimedia-player-ipod-mini-blue"),
|
|
|
|
tr("Copy to device..."), this, SLOT(CopyToDevice()));
|
2010-06-25 00:45:30 +02:00
|
|
|
delete_ = context_menu_->addAction(IconLoader::Load("edit-delete"),
|
|
|
|
tr("Delete from disk..."), this, SLOT(Delete()));
|
|
|
|
context_menu_->addSeparator();
|
2010-03-21 00:59:39 +01:00
|
|
|
show_in_various_ = context_menu_->addAction(
|
|
|
|
tr("Show in various artists"), this, SLOT(ShowInVarious()));
|
|
|
|
no_show_in_various_ = context_menu_->addAction(
|
|
|
|
tr("Don't show in various artists"), this, SLOT(NoShowInVarious()));
|
2010-04-01 02:12:25 +02:00
|
|
|
|
|
|
|
ReloadSettings();
|
|
|
|
}
|
|
|
|
|
2010-06-25 00:45:30 +02:00
|
|
|
LibraryView::~LibraryView() {
|
|
|
|
}
|
|
|
|
|
2010-04-01 02:12:25 +02:00
|
|
|
void LibraryView::ReloadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
2010-05-17 01:44:33 +02:00
|
|
|
SetAutoOpen(s.value("auto_open", true).toBool());
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2010-06-25 00:45:30 +02:00
|
|
|
void LibraryView::SetTaskManager(TaskManager *task_manager) {
|
2010-07-31 18:12:16 +02:00
|
|
|
task_manager_ = task_manager;
|
2010-06-25 00:45:30 +02:00
|
|
|
organise_dialog_.reset(new OrganiseDialog(task_manager));
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
void LibraryView::SetLibrary(LibraryModel *library) {
|
2009-12-24 20:16:07 +01:00
|
|
|
library_ = library;
|
|
|
|
}
|
|
|
|
|
2010-07-19 23:16:22 +02:00
|
|
|
void LibraryView::SetDeviceManager(DeviceManager *device_manager) {
|
|
|
|
devices_ = device_manager;
|
2010-07-24 20:31:05 +02:00
|
|
|
copy_to_device_->setDisabled(devices_->connected_devices_model()->rowCount() == 0);
|
|
|
|
connect(devices_->connected_devices_model(), SIGNAL(IsEmptyChanged(bool)),
|
|
|
|
copy_to_device_, SLOT(setDisabled(bool)));
|
2010-07-19 23:16:22 +02:00
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
void LibraryView::TotalSongCountUpdated(int count) {
|
|
|
|
bool old = total_song_count_;
|
|
|
|
total_song_count_ = count;
|
|
|
|
if (old != total_song_count_)
|
|
|
|
update();
|
|
|
|
|
|
|
|
if (total_song_count_ == 0)
|
|
|
|
setCursor(Qt::PointingHandCursor);
|
|
|
|
else
|
|
|
|
unsetCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::paintEvent(QPaintEvent* event) {
|
|
|
|
QTreeView::paintEvent(event);
|
|
|
|
QPainter p(viewport());
|
|
|
|
|
|
|
|
QRect rect(viewport()->rect());
|
|
|
|
if (total_song_count_ == 0) {
|
2010-01-08 17:21:22 +01:00
|
|
|
// Draw the confused clementine
|
2009-12-24 20:16:07 +01:00
|
|
|
QRect image_rect((rect.width() - nomusic_.width()) / 2, 50,
|
|
|
|
nomusic_.width(), nomusic_.height());
|
|
|
|
p.drawPixmap(image_rect, nomusic_);
|
|
|
|
|
|
|
|
// Draw the title text
|
|
|
|
QFont bold_font;
|
|
|
|
bold_font.setBold(true);
|
|
|
|
p.setFont(bold_font);
|
|
|
|
|
|
|
|
QFontMetrics metrics(bold_font);
|
|
|
|
|
|
|
|
QRect title_rect(0, image_rect.bottom() + 20, rect.width(), metrics.height());
|
2010-02-23 19:33:09 +01:00
|
|
|
p.drawText(title_rect, Qt::AlignHCenter, tr("Your library is empty!"));
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
// Draw the other text
|
|
|
|
p.setFont(QFont());
|
|
|
|
|
|
|
|
QRect text_rect(0, title_rect.bottom() + 5, rect.width(), metrics.height());
|
2010-02-23 19:33:09 +01:00
|
|
|
p.drawText(text_rect, Qt::AlignHCenter, tr("Click here to add some music"));
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::mouseReleaseEvent(QMouseEvent* e) {
|
|
|
|
QTreeView::mouseReleaseEvent(e);
|
|
|
|
|
|
|
|
if (total_song_count_ == 0) {
|
|
|
|
emit ShowConfigDialog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:59:39 +01:00
|
|
|
void LibraryView::contextMenuEvent(QContextMenuEvent *e) {
|
|
|
|
context_menu_index_ = indexAt(e->pos());
|
|
|
|
if (!context_menu_index_.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
context_menu_index_ = qobject_cast<QSortFilterProxyModel*>(model())
|
|
|
|
->mapToSource(context_menu_index_);
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
int type = library_->data(context_menu_index_, LibraryModel::Role_Type).toInt();
|
|
|
|
int container_type = library_->data(context_menu_index_, LibraryModel::Role_ContainerType).toInt();
|
|
|
|
bool enable_various = container_type == LibraryModel::GroupBy_Album;
|
2010-04-04 15:31:21 +02:00
|
|
|
bool enable_add = type == LibraryItem::Type_Container ||
|
|
|
|
type == LibraryItem::Type_Song;
|
2010-03-21 00:59:39 +01:00
|
|
|
|
2010-05-15 19:45:04 +02:00
|
|
|
load_->setEnabled(enable_add);
|
2010-04-04 15:31:21 +02:00
|
|
|
add_to_playlist_->setEnabled(enable_add);
|
2010-03-21 00:59:39 +01:00
|
|
|
show_in_various_->setEnabled(enable_various);
|
|
|
|
no_show_in_various_->setEnabled(enable_various);
|
|
|
|
|
|
|
|
context_menu_->popup(e->globalPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::ShowInVarious() {
|
|
|
|
ShowInVarious(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::NoShowInVarious() {
|
|
|
|
ShowInVarious(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::ShowInVarious(bool on) {
|
|
|
|
if (!context_menu_index_.isValid())
|
|
|
|
return;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
QString artist = library_->data(context_menu_index_, LibraryModel::Role_Artist).toString();
|
|
|
|
QString album = library_->data(context_menu_index_, LibraryModel::Role_Key).toString();
|
|
|
|
library_->backend()->ForceCompilation(artist, album, on);
|
2010-03-21 00:59:39 +01:00
|
|
|
}
|
2010-04-04 15:31:21 +02:00
|
|
|
|
2010-05-15 19:45:04 +02:00
|
|
|
void LibraryView::Load() {
|
|
|
|
if (!context_menu_index_.isValid())
|
|
|
|
return;
|
|
|
|
|
2010-06-09 17:49:05 +02:00
|
|
|
emit Load(selectedIndexes());
|
2010-05-15 19:45:04 +02:00
|
|
|
}
|
|
|
|
|
2010-04-04 15:31:21 +02:00
|
|
|
void LibraryView::AddToPlaylist() {
|
|
|
|
if (!context_menu_index_.isValid())
|
|
|
|
return;
|
|
|
|
|
2010-06-09 17:49:05 +02:00
|
|
|
emit AddToPlaylist(selectedIndexes());
|
2010-04-04 15:31:21 +02:00
|
|
|
}
|
2010-05-28 14:52:22 +02:00
|
|
|
|
|
|
|
void LibraryView::keyboardSearch(const QString &search) {
|
|
|
|
is_in_keyboard_search_ = true;
|
|
|
|
QTreeView::keyboardSearch(search);
|
|
|
|
is_in_keyboard_search_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::scrollTo(const QModelIndex &index, ScrollHint hint) {
|
|
|
|
if (is_in_keyboard_search_)
|
|
|
|
QTreeView::scrollTo(index, QAbstractItemView::PositionAtTop);
|
|
|
|
else
|
|
|
|
QTreeView::scrollTo(index, hint);
|
|
|
|
}
|
2010-06-25 00:45:30 +02:00
|
|
|
|
2010-07-30 00:16:12 +02:00
|
|
|
void LibraryView::GetSelectedFileInfo(
|
2010-07-31 18:12:16 +02:00
|
|
|
QStringList *filenames, quint64 *size, SongList* songs_out) const {
|
2010-06-25 00:45:30 +02:00
|
|
|
QModelIndexList selected_indexes =
|
|
|
|
qobject_cast<QSortFilterProxyModel*>(model())->mapSelectionToSource(
|
|
|
|
selectionModel()->selection()).indexes();
|
|
|
|
SongList songs = library_->GetChildSongs(selected_indexes);
|
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
if (size)
|
|
|
|
*size = 0;
|
|
|
|
|
2010-06-25 00:45:30 +02:00
|
|
|
foreach (const Song& song, songs) {
|
2010-07-31 18:12:16 +02:00
|
|
|
if (filenames)
|
|
|
|
*filenames << song.filename();
|
2010-06-25 00:45:30 +02:00
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
if (size && song.filesize() >= 0)
|
2010-07-30 00:16:12 +02:00
|
|
|
*size += song.filesize();
|
|
|
|
}
|
2010-07-31 18:12:16 +02:00
|
|
|
|
|
|
|
if (songs_out)
|
|
|
|
*songs_out = songs;
|
2010-07-19 23:16:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::Organise() {
|
2010-07-30 00:16:12 +02:00
|
|
|
QStringList filenames;
|
|
|
|
quint64 size = 0;
|
|
|
|
GetSelectedFileInfo(&filenames, &size);
|
|
|
|
|
2010-07-19 23:16:22 +02:00
|
|
|
organise_dialog_->SetDestinationModel(library_->directory_model());
|
2010-06-25 00:45:30 +02:00
|
|
|
organise_dialog_->SetCopy(false);
|
2010-07-30 00:16:12 +02:00
|
|
|
organise_dialog_->SetFilenames(filenames, size);
|
2010-06-25 00:45:30 +02:00
|
|
|
organise_dialog_->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryView::Delete() {
|
2010-07-31 18:12:16 +02:00
|
|
|
SongList songs;
|
|
|
|
GetSelectedFileInfo(NULL, NULL, &songs);
|
|
|
|
|
|
|
|
if (songs.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (QMessageBox::question(this, tr("Delete files"),
|
|
|
|
tr("These files will be deleted from disk, are you sure you want to continue?"),
|
|
|
|
QMessageBox::Yes, QMessageBox::Cancel) != QMessageBox::Yes)
|
|
|
|
return;
|
2010-06-25 00:45:30 +02:00
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
// We can cheat and always take the storage of the first directory, since
|
|
|
|
// they'll all be FilesystemMusicStorage in a library and deleting doesn't
|
|
|
|
// check the actual directory.
|
|
|
|
MusicStorage* storage = library_->directory_model()->index(0, 0).data(
|
|
|
|
MusicStorage::Role_Storage).value<MusicStorage*>();
|
|
|
|
DeleteFiles* delete_files = new DeleteFiles(task_manager_, storage);
|
|
|
|
delete_files->Start(songs);
|
2010-06-25 00:45:30 +02:00
|
|
|
}
|
2010-07-19 23:16:22 +02:00
|
|
|
|
|
|
|
void LibraryView::CopyToDevice() {
|
2010-07-30 00:16:12 +02:00
|
|
|
QStringList filenames;
|
|
|
|
quint64 size = 0;
|
|
|
|
GetSelectedFileInfo(&filenames, &size);
|
|
|
|
|
2010-07-25 11:52:29 +02:00
|
|
|
organise_dialog_->SetDestinationModel(devices_->connected_devices_model(), true);
|
2010-07-19 23:16:22 +02:00
|
|
|
organise_dialog_->SetCopy(true);
|
2010-07-30 00:16:12 +02:00
|
|
|
organise_dialog_->SetFilenames(filenames, size);
|
2010-07-19 23:16:22 +02:00
|
|
|
organise_dialog_->show();
|
|
|
|
}
|
2010-07-31 19:29:52 +02:00
|
|
|
|
|
|
|
void LibraryView::keyReleaseEvent(QKeyEvent* e) {
|
|
|
|
switch (e->key()) {
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
|
|
|
if (currentIndex().isValid())
|
|
|
|
emit doubleClicked(currentIndex());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeView::keyReleaseEvent(e);
|
|
|
|
}
|