~ubuntu-branches/ubuntu/edgy/psi/edgy

« back to all changes in this revision

Viewing changes to libpsi/psiwidgets/iconaction.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * iconaction.cpp - the QAction subclass that uses Icons and supports animation
 
3
 * Copyright (C) 2003  Michail Pishchagin
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include "iconaction.h"
 
22
 
 
23
#include "iconwidget.h"
 
24
#include "iconset.h"
 
25
 
 
26
#include <qpopupmenu.h>
 
27
 
 
28
//----------------------------------------------------------------------------
 
29
// IconAction
 
30
//----------------------------------------------------------------------------
 
31
 
 
32
class IconAction::Private : public QObject
 
33
{
 
34
        Q_OBJECT
 
35
public:
 
36
        QPtrList<IconToolButton> buttons;
 
37
        Icon *icon;
 
38
        IconAction *action;
 
39
 
 
40
        Private(IconAction *act) {
 
41
                icon = 0;
 
42
                action = act;
 
43
        }
 
44
 
 
45
public slots:
 
46
        void menuClicked() {
 
47
                action->setOn( !action->isOn() );
 
48
                emit action->setOn( action->isOn() );
 
49
                emit action->activated(); // required for our lovely status change actions
 
50
        }
 
51
};
 
52
 
 
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)
 
55
{
 
56
        d = new Private(this);
 
57
 
 
58
        setIcon(icon);
 
59
}
 
60
 
 
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)
 
63
{
 
64
        d = new Private(this);
 
65
}
 
66
 
 
67
IconAction::~IconAction()
 
68
{
 
69
        // delete the buttons list before our own destruction
 
70
        d->buttons.setAutoDelete(true);
 
71
        d->buttons.clear();
 
72
 
 
73
        delete d;
 
74
}
 
75
 
 
76
void IconAction::setIcon(const Icon *i)
 
77
{
 
78
        if ( d->icon ) {
 
79
                disconnect(d->icon, 0, this, 0 );
 
80
                d->icon->stop();
 
81
                delete d->icon;
 
82
                d->icon = 0;
 
83
        }
 
84
 
 
85
        QIconSet is;
 
86
        if ( i ) {
 
87
                d->icon = new Icon(*i);
 
88
                connect(d->icon, SIGNAL(iconModified(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
 
89
                d->icon->activated(true);
 
90
 
 
91
                is = d->icon->iconSet();
 
92
        }
 
93
 
 
94
        QAction::setIconSet( is );
 
95
 
 
96
        for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
 
97
                IconToolButton *btn = it.current();
 
98
                btn->setIcon ( d->icon );
 
99
        }
 
100
}
 
101
 
 
102
void IconAction::setIcon(const QString &name)
 
103
{
 
104
        setIcon( IconsetFactory::iconPtr(name) );
 
105
}
 
106
 
 
107
const QString &IconAction::iconName() const
 
108
{
 
109
        if ( d->icon )
 
110
                return d->icon->name();
 
111
        return QString::null;
 
112
}
 
113
 
 
114
bool IconAction::addTo(QWidget *w)
 
115
{
 
116
        if ( w->inherits("QToolBar") ) {
 
117
                QCString bname = name() + QCString("_action_button");
 
118
                IconToolButton *btn = new IconToolButton ( w, bname );
 
119
                d->buttons.append ( btn );
 
120
 
 
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);
 
127
 
 
128
                connect(btn, SIGNAL(clicked()), this, SIGNAL(activated()));
 
129
                connect(btn, SIGNAL(toggled(bool)), this, SLOT(setOn(bool)));
 
130
 
 
131
                connect(btn, SIGNAL(destroyed()), SLOT(objectDestroyed()));
 
132
        }
 
133
        else if ( w->inherits("QPopupMenu") ) { // i have added this because of many mysterious segfaults
 
134
                if ( !isVisible() )
 
135
                        return true;
 
136
 
 
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()));
 
145
                }
 
146
                else
 
147
                        popup->connectItem(id, this, SIGNAL(activated()));
 
148
 
 
149
                if ( !accel().isEmpty() )
 
150
                        popup->changeItem(id, iconSet(), menuText() + '\t' + (QString)accel());
 
151
 
 
152
        }
 
153
        else
 
154
                return QAction::addTo(w);
 
155
 
 
156
        return true;
 
157
}
 
158
 
 
159
void IconAction::objectDestroyed()
 
160
{
 
161
        const QObject *obj = sender();
 
162
        d->buttons.removeRef((IconToolButton *)obj);
 
163
}
 
164
 
 
165
void IconAction::setOn(bool b)
 
166
{
 
167
        QAction::setOn(b);
 
168
        for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
 
169
                IconToolButton *btn = it.current();
 
170
                btn->setOn(b);
 
171
        }
 
172
}
 
173
 
 
174
void IconAction::toolButtonToggled(bool b)
 
175
{
 
176
        setOn(b);
 
177
}
 
178
 
 
179
void IconAction::setEnabled(bool e)
 
180
{
 
181
        QAction::setEnabled(e);
 
182
        for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
 
183
                IconToolButton *btn = it.current();
 
184
                btn->setEnabled (e);
 
185
        }
 
186
}
 
187
 
 
188
void IconAction::setText(const QString &t)
 
189
{
 
190
        QAction::setText(t);
 
191
        for ( QPtrListIterator<IconToolButton> it(d->buttons); it.current(); ++it ) {
 
192
                IconToolButton *btn = it.current();
 
193
                btn->setTextLabel(t);
 
194
        }
 
195
}
 
196
 
 
197
QPtrList<IconToolButton> IconAction::buttonList()
 
198
{
 
199
        return d->buttons;
 
200
}
 
201
 
 
202
void IconAction::iconUpdated(const QPixmap &)
 
203
{
 
204
        QAction::setIconSet( d->icon->iconSet() );
 
205
}
 
206
 
 
207
#include "iconaction.moc"