1 /*  smplayer, GUI front-end for mplayer.
2     Copyright (C) 2006-2021 Ricardo Villalba <ricardo@smplayer.info>
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 
19 #include "myactiongroup.h"
20 #include <QAction>
21 #include <QList>
22 #include <QWidget>
23 
MyActionGroupItem(QObject * parent,MyActionGroup * group,const char * name,int data,bool autoadd)24 MyActionGroupItem::MyActionGroupItem(QObject * parent, MyActionGroup *group,
25                                      const char * name,
26                                      int data, bool autoadd)
27 	: MyAction(parent, name, autoadd)
28 {
29 	setData(data);
30 	setCheckable(true);
31 	if (group) group->addAction(this);
32 }
33 
MyActionGroupItem(QObject * parent,MyActionGroup * group,const QString & text,const char * name,int data,bool autoadd)34 MyActionGroupItem::MyActionGroupItem(QObject * parent, MyActionGroup *group,
35                                      const QString & text,
36                                      const char * name,
37                                      int data, bool autoadd)
38 	: MyAction(parent, name, autoadd)
39 {
40 	setData(data);
41 	setText(text);
42 	setCheckable(true);
43 	if (group) group->addAction(this);
44 }
45 
46 
MyActionGroup(QObject * parent)47 MyActionGroup::MyActionGroup( QObject * parent ) : QActionGroup(parent)
48 {
49 	setExclusive(true);
50 	connect( this, SIGNAL(triggered(QAction *)),
51              this, SLOT(itemTriggered(QAction *)) );
52 }
53 
setChecked(int ID)54 void MyActionGroup::setChecked(int ID) {
55 	//qDebug("MyActionGroup::setChecked: ID: %d", ID);
56 
57 	QList <QAction *> l = actions();
58 	for (int n=0; n < l.count(); n++) {
59 		if ( (!l[n]->isSeparator()) && (l[n]->data().toInt() == ID) ) {
60 			l[n]->setChecked(true);
61 			return;
62 		}
63 	}
64 }
65 
checked()66 int MyActionGroup::checked() {
67 	QAction * a = checkedAction();
68 	if (a)
69 		return a->data().toInt();
70 	else
71 		return -1;
72 }
73 
uncheckAll()74 void MyActionGroup::uncheckAll() {
75 	QList <QAction *> l = actions();
76 	for (int n=0; n < l.count(); n++) {
77 		l[n]->setChecked(false);
78 	}
79 }
80 
setActionsEnabled(bool b)81 void MyActionGroup::setActionsEnabled(bool b) {
82 	QList <QAction *> l = actions();
83 	for (int n=0; n < l.count(); n++) {
84 		l[n]->setEnabled(b);
85 	}
86 }
87 
clear(bool remove)88 void MyActionGroup::clear(bool remove) {
89 	while (actions().count() > 0) {
90 		QAction * a = actions()[0];
91 		if (a) {
92 			removeAction(a);
93 			if (remove) a->deleteLater();
94 		}
95 	}
96 }
97 
itemTriggered(QAction * a)98 void MyActionGroup::itemTriggered(QAction *a) {
99 	qDebug("MyActionGroup::itemTriggered: '%s'", a->objectName().toUtf8().data());
100 	int value = a->data().toInt();
101 
102 	qDebug("MyActionGroup::itemTriggered: ID: %d", value);
103 
104 	emit activated(value);
105 }
106 
addTo(QWidget * w)107 void MyActionGroup::addTo(QWidget *w) {
108 	w->addActions( actions() );
109 }
110 
removeFrom(QWidget * w)111 void MyActionGroup::removeFrom(QWidget *w) {
112 	for (int n=0; n < actions().count(); n++) {
113 		w->removeAction( actions()[n] );
114 	}
115 }
116 
117 #include "moc_myactiongroup.cpp"
118