-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracksmenu.cpp
95 lines (80 loc) · 2.58 KB
/
tracksmenu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* This is part of KokoVP
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tracksmenu.h"
#include <QActionGroup>
TracksMenu::TracksMenu(const QString &title, QWidget *parent) : QMenu{title, parent} {
trackAG = new QActionGroup(this);
trackAG->setExclusive(true);
connect(trackAG, &QActionGroup::triggered, this, &TracksMenu::handleAction);
noAction = new QAction(tr("No"));
noAction->setData(-1);
noAction->setCheckable(true);
noAction->setChecked(true);
trackAG->addAction(noAction);
addAction(noAction);
rRule = defRule;
}
void TracksMenu::clearTracks()
{
QList<QAction*> acts = trackAG->actions();
for (auto &a: acts)
{
if (a==noAction)
continue;
delete a;
}
trackAG->blockSignals(true);
noAction->trigger();
trackAG->blockSignals(false);
}
void TracksMenu::addTrack(const PlayerController::Track &t)
{
QString rRule_res = rRule(t);
QAction *trackAction = new QAction();
// & is used for mnemonics in QAction, replace to display actual ampersand
trackAction->setText((rRule_res.isNull() ? defRule(t) : rRule_res).replace("&", "&&"));
trackAction->setData(t.id);
trackAction->setCheckable(true);
trackAG->addAction(trackAction);
addAction(trackAction);
if (t.id==lastId)
trackAction->trigger();
}
void TracksMenu::setCurrentId(int id)
{
lastId = id;
for (auto &a : trackAG->actions())
{
if (a->data().toInt()==id)
return a->trigger();
}
}
void TracksMenu::handleAction(QAction *a)
{
int newId = a->data().toInt();
if (newId!=lastId)
{
lastId=newId;
qDebug() << "trackSwitched" << newId;
emit trackSwitched(newId);
}
}
QString TracksMenu::defRule(const PlayerController::Track &t)
{
if (!t.title.isEmpty())
return t.title;
if (!t.lang.isEmpty())
return t.lang;
return QString::number(t.id);
}