1 /*
2 * Strawberry Music Player
3 * This file was part of Clementine.
4 * Copyright 2010, David Sansome <me@davidsansome.com>
5 * Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
6 *
7 * Strawberry is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Strawberry is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include "config.h"
23
24 #include <functional>
25
26 #include <QDialog>
27 #include <QWidget>
28 #include <QComboBox>
29 #include <QDialogButtonBox>
30 #include <QPushButton>
31
32 #include "collectionmodel.h"
33 #include "groupbydialog.h"
34 #include "ui_groupbydialog.h"
35
36 #include <boost/multi_index/indexed_by.hpp>
37 #include <boost/multi_index/member.hpp>
38 #include <boost/multi_index/ordered_index.hpp>
39 #include <boost/multi_index/tag.hpp>
40 #include <boost/multi_index_container.hpp>
41 #include <boost/multi_index_container_fwd.hpp>
42 #include <boost/operators.hpp>
43
44 using boost::multi_index_container;
45 using boost::multi_index::indexed_by;
46 using boost::multi_index::ordered_unique;
47 using boost::multi_index::tag;
48 using boost::multi_index::member;
49
50 namespace {
51
52 struct Mapping {
Mapping__anon4d57daa80111::Mapping53 Mapping(CollectionModel::GroupBy g, int i) : group_by(g), combo_box_index(i) {}
54
55 CollectionModel::GroupBy group_by;
56 int combo_box_index;
57 };
58
59 struct tag_index {};
60 struct tag_group_by {};
61
62 } // namespace
63
64 class GroupByDialogPrivate {
65 private:
66 typedef multi_index_container<
67 Mapping,
68 indexed_by<
69 ordered_unique<tag<tag_index>, member<Mapping, int, &Mapping::combo_box_index> >,
70 ordered_unique<tag<tag_group_by>, member<Mapping, CollectionModel::GroupBy, &Mapping::group_by> > > > MappingContainer;
71
72 public:
73 MappingContainer mapping_;
74 };
75
GroupByDialog(QWidget * parent)76 GroupByDialog::GroupByDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_GroupByDialog), p_(new GroupByDialogPrivate) {
77
78 ui_->setupUi(this);
79 Reset();
80
81 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_None, 0));
82 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Artist, 1));
83 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_AlbumArtist, 2));
84 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Album, 3));
85 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_AlbumDisc, 4));
86 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Disc, 5));
87 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Format, 6));
88 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Genre, 7));
89 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Year, 8));
90 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_YearAlbum, 9));
91 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_YearAlbumDisc, 10));
92 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_OriginalYear, 11));
93 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_OriginalYearAlbum, 12));
94 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Composer, 13));
95 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Performer, 14));
96 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Grouping, 15));
97 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_FileType, 16));
98 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Samplerate, 17));
99 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Bitdepth, 18));
100 p_->mapping_.insert(Mapping(CollectionModel::GroupBy_Bitrate, 19));
101
102 QObject::connect(ui_->buttonbox->button(QDialogButtonBox::Reset), &QPushButton::clicked, this, &GroupByDialog::Reset);
103
104 resize(sizeHint());
105
106 }
107
108 GroupByDialog::~GroupByDialog() = default;
109
Reset()110 void GroupByDialog::Reset() {
111 ui_->combobox_first->setCurrentIndex(2); // Album Artist
112 ui_->combobox_second->setCurrentIndex(3); // Album
113 ui_->combobox_third->setCurrentIndex(0); // None
114 }
115
accept()116 void GroupByDialog::accept() {
117 emit Accepted(CollectionModel::Grouping(
118 p_->mapping_.get<tag_index>().find(ui_->combobox_first->currentIndex())->group_by,
119 p_->mapping_.get<tag_index>().find(ui_->combobox_second->currentIndex())->group_by,
120 p_->mapping_.get<tag_index>().find(ui_->combobox_third->currentIndex())->group_by)
121 );
122 QDialog::accept();
123 }
124
CollectionGroupingChanged(const CollectionModel::Grouping g)125 void GroupByDialog::CollectionGroupingChanged(const CollectionModel::Grouping g) {
126 ui_->combobox_first->setCurrentIndex(p_->mapping_.get<tag_group_by>().find(g[0])->combo_box_index);
127 ui_->combobox_second->setCurrentIndex(p_->mapping_.get<tag_group_by>().find(g[1])->combo_box_index);
128 ui_->combobox_third->setCurrentIndex(p_->mapping_.get<tag_group_by>().find(g[2])->combo_box_index);
129 }
130