2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2015, Nick Lanham <nick@afternight.org>
|
2022-08-20 14:51:19 +02:00
|
|
|
* Copyright 2019-2022, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-12 00:07:05 +01:00
|
|
|
#include "config.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2024-04-23 17:15:42 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QDialog>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QItemSelectionModel>
|
|
|
|
#include <QList>
|
2022-08-20 14:51:19 +02:00
|
|
|
#include <QByteArray>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2022-08-20 14:51:19 +02:00
|
|
|
#include <QIODevice>
|
|
|
|
#include <QDataStream>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QKeySequence>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSettings>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/logging.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "core/iconloader.h"
|
2024-04-11 02:56:01 +02:00
|
|
|
#include "core/settings.h"
|
2023-03-18 01:01:28 +01:00
|
|
|
#include "settings/collectionsettingspage.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "collectionmodel.h"
|
|
|
|
#include "savedgroupingmanager.h"
|
|
|
|
#include "ui_savedgroupingmanager.h"
|
|
|
|
|
2022-08-20 14:51:19 +02:00
|
|
|
const char *SavedGroupingManager::kSavedGroupingsSettingsGroup = "SavedGroupings";
|
|
|
|
|
|
|
|
SavedGroupingManager::SavedGroupingManager(const QString &saved_groupings_settings_group, QWidget *parent)
|
2018-02-27 18:06:05 +01:00
|
|
|
: QDialog(parent),
|
|
|
|
ui_(new Ui_SavedGroupingManager),
|
2021-06-20 23:53:28 +02:00
|
|
|
model_(new QStandardItemModel(0, 4, this)),
|
2022-08-20 14:51:19 +02:00
|
|
|
saved_groupings_settings_group_(saved_groupings_settings_group) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
ui_->setupUi(this);
|
|
|
|
|
|
|
|
model_->setHorizontalHeaderItem(0, new QStandardItem(tr("Name")));
|
|
|
|
model_->setHorizontalHeaderItem(1, new QStandardItem(tr("First level")));
|
|
|
|
model_->setHorizontalHeaderItem(2, new QStandardItem(tr("Second Level")));
|
|
|
|
model_->setHorizontalHeaderItem(3, new QStandardItem(tr("Third Level")));
|
|
|
|
ui_->list->setModel(model_);
|
2024-04-09 23:20:26 +02:00
|
|
|
ui_->remove->setIcon(IconLoader::Load(QStringLiteral("edit-delete")));
|
2018-02-27 18:06:05 +01:00
|
|
|
ui_->remove->setEnabled(false);
|
|
|
|
|
|
|
|
ui_->remove->setShortcut(QKeySequence::Delete);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(ui_->list->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SavedGroupingManager::UpdateButtonState);
|
|
|
|
|
|
|
|
QObject::connect(ui_->remove, &QPushButton::clicked, this, &SavedGroupingManager::Remove);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SavedGroupingManager::~SavedGroupingManager() {
|
|
|
|
delete ui_;
|
2022-08-20 14:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SavedGroupingManager::GetSavedGroupingsSettingsGroup(const QString &settings_group) {
|
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
if (settings_group.isEmpty() || settings_group == QLatin1String(CollectionSettingsPage::kSettingsGroup)) {
|
|
|
|
return QLatin1String(kSavedGroupingsSettingsGroup);
|
2022-08-20 14:51:19 +02:00
|
|
|
}
|
|
|
|
else {
|
2024-04-11 02:56:01 +02:00
|
|
|
return QLatin1String(kSavedGroupingsSettingsGroup) + QLatin1Char('_') + settings_group;
|
2022-08-20 14:51:19 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
QString SavedGroupingManager::GroupByToString(const CollectionModel::GroupBy g) {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
switch (g) {
|
2023-02-18 14:09:27 +01:00
|
|
|
case CollectionModel::GroupBy::None:
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::GroupByCount:{
|
2018-02-27 18:06:05 +01:00
|
|
|
return tr("None");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::AlbumArtist:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("Album artist");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Artist:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Artist");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Album:{
|
2018-02-27 18:06:05 +01:00
|
|
|
return tr("Album");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::AlbumDisc:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("Album - Disc");
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::YearAlbum:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("Year - Album");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::YearAlbumDisc:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("Year - Album - Disc");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::OriginalYearAlbum:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Original year - Album");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::OriginalYearAlbumDisc:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Original year - Album - Disc");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Disc:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Disc");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Year:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Year");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::OriginalYear:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("Original year");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Genre:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Genre");
|
2019-08-05 19:17:31 +02:00
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Composer:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("Composer");
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Performer:{
|
2018-02-27 18:06:05 +01:00
|
|
|
return tr("Performer");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Grouping:{
|
2018-02-27 18:06:05 +01:00
|
|
|
return tr("Grouping");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::FileType:{
|
2019-08-05 19:17:31 +02:00
|
|
|
return tr("File type");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Format:{
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Format");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Samplerate:{
|
2018-06-28 01:15:32 +02:00
|
|
|
return tr("Sample rate");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Bitdepth:{
|
2018-06-28 01:15:32 +02:00
|
|
|
return tr("Bit depth");
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case CollectionModel::GroupBy::Bitrate:{
|
2019-03-30 22:03:33 +01:00
|
|
|
return tr("Bitrate");
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 22:09:24 +02:00
|
|
|
return tr("Unknown");
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SavedGroupingManager::UpdateModel() {
|
|
|
|
|
|
|
|
model_->setRowCount(0); // don't use clear, it deletes headers
|
2024-04-11 02:56:01 +02:00
|
|
|
Settings s;
|
2022-08-20 14:51:19 +02:00
|
|
|
s.beginGroup(saved_groupings_settings_group_);
|
2020-09-10 22:09:24 +02:00
|
|
|
int version = s.value("version").toInt();
|
|
|
|
if (version == 1) {
|
|
|
|
QStringList saved = s.childKeys();
|
|
|
|
for (int i = 0; i < saved.size(); ++i) {
|
2024-04-11 02:56:01 +02:00
|
|
|
if (saved.at(i) == QStringLiteral("version")) continue;
|
2020-09-10 22:09:24 +02:00
|
|
|
QByteArray bytes = s.value(saved.at(i)).toByteArray();
|
|
|
|
QDataStream ds(&bytes, QIODevice::ReadOnly);
|
|
|
|
CollectionModel::Grouping g;
|
|
|
|
ds >> g;
|
|
|
|
|
|
|
|
QList<QStandardItem*> list;
|
|
|
|
list << new QStandardItem(saved.at(i))
|
|
|
|
<< new QStandardItem(GroupByToString(g.first))
|
|
|
|
<< new QStandardItem(GroupByToString(g.second))
|
|
|
|
<< new QStandardItem(GroupByToString(g.third));
|
|
|
|
|
|
|
|
model_->appendRow(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QStringList saved = s.childKeys();
|
|
|
|
for (int i = 0; i < saved.size(); ++i) {
|
2024-04-11 02:56:01 +02:00
|
|
|
if (saved.at(i) == QStringLiteral("version")) continue;
|
2020-09-10 22:09:24 +02:00
|
|
|
s.remove(saved.at(i));
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2020-09-10 22:09:24 +02:00
|
|
|
s.endGroup();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedGroupingManager::Remove() {
|
|
|
|
|
|
|
|
if (ui_->list->selectionModel()->hasSelection()) {
|
2024-04-11 02:56:01 +02:00
|
|
|
Settings s;
|
2022-08-20 14:51:19 +02:00
|
|
|
s.beginGroup(saved_groupings_settings_group_);
|
2024-04-23 17:15:42 +02:00
|
|
|
const QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
|
|
|
|
for (const QModelIndex &idx : indexes) {
|
2021-01-26 16:48:04 +01:00
|
|
|
if (idx.isValid()) {
|
|
|
|
qLog(Debug) << "Remove saved grouping: " << model_->item(idx.row(), 0)->text();
|
|
|
|
s.remove(model_->item(idx.row(), 0)->text());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 22:09:24 +02:00
|
|
|
s.endGroup();
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
UpdateModel();
|
2022-08-20 14:51:19 +02:00
|
|
|
|
|
|
|
emit UpdateGroupByActions();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedGroupingManager::UpdateButtonState() {
|
|
|
|
|
|
|
|
if (ui_->list->selectionModel()->hasSelection()) {
|
|
|
|
const QModelIndex current = ui_->list->selectionModel()->currentIndex();
|
|
|
|
ui_->remove->setEnabled(current.isValid());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui_->remove->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|