2
* iconaction.cpp - the QAction subclass that uses Icons and supports animation
3
* Copyright (C) 2003 Michail Pishchagin
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include "iconaction.h"
23
#include "iconwidget.h"
26
#include <qpopupmenu.h>
28
//----------------------------------------------------------------------------
30
//----------------------------------------------------------------------------
32
class IconAction::Private : public QObject
36
QPtrList<IconToolButton> buttons;
40
Private(IconAction *act) {
47
action->setOn( !action->isOn() );
48
emit action->setOn( action->isOn() );
49
emit action->activated(); // required for our lovely status change actions
53
IconAction::IconAction(const QString &text, const QString &icon, const QString &menuText, QKeySequence accel, QObject *parent, const char *name, bool toggle)
54
: QAction(text, menuText, accel, parent, name, toggle)
56
d = new Private(this);
61
IconAction::IconAction(const QString &text, const QString &menuText, QKeySequence accel, QObject *parent, const char *name, bool toggle)
62
: QAction(text, menuText, accel, parent, name, toggle)
64
d = new Private(this);
67
IconAction::~IconAction()
69
// delete the buttons list before our own destruction
70
d->buttons.setAutoDelete(true);
76
void IconAction::setIcon(const Icon *i)
79
disconnect(d->icon, 0, this, 0 );
87
d->icon = new Icon(*i);
88
connect(d->icon, SIGNAL(iconModified(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
89
d->icon->activated(true);
91
is = d->icon->iconSet();
94
QAction::setIconSet( is );
96
for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
97
IconToolButton *btn = it.current();
98
btn->setIcon ( d->icon );
102
void IconAction::setIcon(const QString &name)
104
setIcon( IconsetFactory::iconPtr(name) );
107
const QString &IconAction::iconName() const
110
return d->icon->name();
111
return QString::null;
114
bool IconAction::addTo(QWidget *w)
116
if ( w->inherits("QToolBar") ) {
117
QCString bname = name() + QCString("_action_button");
118
IconToolButton *btn = new IconToolButton ( w, bname );
119
d->buttons.append ( btn );
121
btn->setToggleButton ( isToggleAction() );
122
btn->setOn( isOn() );
123
btn->setTextLabel ( text() );
124
btn->setIcon ( d->icon, false );
125
btn->setEnabled ( isEnabled() );
126
addingToolButton(btn);
128
connect(btn, SIGNAL(clicked()), this, SIGNAL(activated()));
129
connect(btn, SIGNAL(toggled(bool)), this, SLOT(setOn(bool)));
131
connect(btn, SIGNAL(destroyed()), SLOT(objectDestroyed()));
133
else if ( w->inherits("QPopupMenu") ) { // i have added this because of many mysterious segfaults
137
QPopupMenu *popup = (QPopupMenu *)w;
138
int id = popup->insertItem (iconSet(), menuText());
139
popup->setItemEnabled(id, isEnabled());
140
popup->setWhatsThis(id, whatsThis());
141
if ( isToggleAction() ) {
142
popup->setCheckable( true );
143
popup->setItemChecked(id, isOn());
144
popup->connectItem(id, d, SLOT(menuClicked()));
147
popup->connectItem(id, this, SIGNAL(activated()));
149
if ( !accel().isEmpty() )
150
popup->changeItem(id, iconSet(), menuText() + '\t' + (QString)accel());
154
return QAction::addTo(w);
159
void IconAction::objectDestroyed()
161
const QObject *obj = sender();
162
d->buttons.removeRef((IconToolButton *)obj);
165
void IconAction::setOn(bool b)
168
for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
169
IconToolButton *btn = it.current();
174
void IconAction::toolButtonToggled(bool b)
179
void IconAction::setEnabled(bool e)
181
QAction::setEnabled(e);
182
for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
183
IconToolButton *btn = it.current();
188
void IconAction::setText(const QString &t)
191
for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
192
IconToolButton *btn = it.current();
193
btn->setTextLabel(t);
197
QPtrList<IconToolButton> IconAction::buttonList()
202
void IconAction::iconUpdated(const QPixmap &)
204
QAction::setIconSet( d->icon->iconSet() );
207
#include "iconaction.moc"