summaryrefslogtreecommitdiffstats
path: root/kdeprint/management/kiconselectaction.cpp
blob: eb7d1a82ab9d735f5bd4e5724a06599deb0fb8e8 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "kiconselectaction.h"

#include <tqpopupmenu.h>
#include <kiconloader.h>
#include <kdebug.h>
#include <ktoolbar.h>
#include <ktoolbarbutton.h>

class KIconSelectActionPrivate
{
public:
	KIconSelectActionPrivate()
	{
		m_menu = 0;
	}
	TQStringList	m_iconlst;
	TQPopupMenu*	m_menu;
};

KIconSelectAction::KIconSelectAction(const TQString& text, int accel, TQObject* parent, const char* name)
: KSelectAction(text, accel, parent, name)
{
	d = new KIconSelectActionPrivate;
}

KIconSelectAction::~KIconSelectAction()
{
	delete d;
}

void KIconSelectAction::updateIcons()
{
	if (d->m_menu)
	{
		TQStringList	lst = items();
		for (uint id=0; id<lst.count(); ++id)
			d->m_menu->changeItem(id, SmallIconSet(d->m_iconlst[id]), lst[id]);
	}
}

void KIconSelectAction::createPopupMenu()
{
	if (!d->m_menu)
	{
		d->m_menu = popupMenu();
		updateIcons();
	}
}

void KIconSelectAction::setItems(const TQStringList& lst, const TQStringList& iconlst)
{
	KSelectAction::setItems(lst);
	d->m_iconlst = iconlst;
	updateIcons();
}

int KIconSelectAction::plug(TQWidget* widget, int index)
{
	int	value(-1);
	if (widget->inherits("QPopupMenu"))
	{
		createPopupMenu();
		value = KSelectAction::plug(widget, index);
	}
	else if (widget->inherits("KToolBar"))
	{
		KToolBar* bar = static_cast<KToolBar*>(widget);
		int id = KAction::getToolButtonID();
		// To have a correct layout in the toolbar, a non
		// empty icon has to be used. Use "unknown" by default.
		TQString	iconName = (currentItem() != -1 ? d->m_iconlst[currentItem()] : "unknown");

		createPopupMenu();
		bar->insertButton(iconName, id, true, plainText(), index);
		bar->getButton(id)->setPopup(d->m_menu, true);
		bar->setItemEnabled(id, isEnabled());
		addContainer(bar, id);
		connect(bar, TQT_SIGNAL(destroyed()), TQT_SLOT(slotDestroyed()));

		value = containerCount()-1;
	}
	return value;
}

void KIconSelectAction::updateCurrentItem(int id)
{
	TQWidget*	w = container(id);
	if (w->inherits("KToolBar"))
		static_cast<KToolBar*>(w)->getButton(itemId(id))->setIcon(d->m_iconlst[currentItem()]);
	else
		KSelectAction::updateCurrentItem(id);
}

void KIconSelectAction::setCurrentItem(int index)
{
	KSelectAction::setCurrentItem(index);
}

#include "kiconselectaction.moc"