1 /* Mergable.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program 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  * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "MergeMenu.h"
22 #include "Utils/Library/MergeData.h"
23 #include "Utils/Set.h"
24 
25 #include <QMenu>
26 #include <QStringList>
27 #include <QMap>
28 
29 using Gui::MergeMenu;
30 
31 struct MergeMenu::Private
32 {
33 	QAction* action = nullptr;
34 	QMenu* parent = nullptr;
35 
36 	QMap<Id, QString> data;
37 	Id targetId;
38 
39 	explicit Private(QMenu* parent) :
40 		parent(parent),
41 		targetId(-1) {}
42 };
43 
44 MergeMenu::MergeMenu(QMenu* parent) :
45 	Gui::WidgetTemplate<QMenu>(parent)
46 {
47 	m = Pimpl::make<Private>(parent);
48 
49 	m->action = m->parent->addMenu(this);
50 	m->action->setVisible(false);
51 	m->action->setText(tr("Merge"));
52 }
53 
54 MergeMenu::~MergeMenu() = default;
55 
56 void MergeMenu::setData(const QMap<Id, QString>& data)
57 {
58 	this->clear();
59 	m->data = data;
60 
61 	if(data.size() < 2)
62 	{
63 		return;
64 	}
65 
66 	for(Id key : data.keys())
67 	{
68 		QString val = data[key];
69 
70 		auto* action = new QAction(this);
71 		action->setText(val);
72 		action->setData(key);
73 		this->addAction(action);
74 
75 		connect(action, &QAction::triggered, this, [=]() {
76 			m->targetId = key;
77 			emit sigMergeTriggered();
78 		});
79 	}
80 }
81 
82 QAction* MergeMenu::action() const
83 {
84 	return m->action;
85 }
86 
87 bool MergeMenu::isDataValid() const
88 {
89 	QList<QString> valueList = m->data.values();
90 	QStringList strings(valueList);
91 
92 	return (strings.size() > 1);
93 }
94 
95 Library::MergeData MergeMenu::mergedata() const
96 {
97 	Util::Set<Id> sourceIds;
98 	for(Id key : m->data.keys())
99 	{
100 		sourceIds << key;
101 	}
102 
103 	return Library::MergeData(sourceIds, m->targetId, -1);
104 }
105 
106 void MergeMenu::languageChanged()
107 {
108 	m->action->setText(tr("Merge"));
109 }
110