kiconselectaction.cpp
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License version 2 as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 **/ 00019 00020 #include "kiconselectaction.h" 00021 00022 #include <qpopupmenu.h> 00023 #include <kiconloader.h> 00024 #include <kdebug.h> 00025 #include <ktoolbar.h> 00026 #include <ktoolbarbutton.h> 00027 00028 class KIconSelectActionPrivate 00029 { 00030 public: 00031 KIconSelectActionPrivate() 00032 { 00033 m_menu = 0; 00034 } 00035 QStringList m_iconlst; 00036 QPopupMenu* m_menu; 00037 }; 00038 00039 KIconSelectAction::KIconSelectAction(const QString& text, int accel, QObject* parent, const char* name) 00040 : KSelectAction(text, accel, parent, name) 00041 { 00042 d = new KIconSelectActionPrivate; 00043 } 00044 00045 KIconSelectAction::~KIconSelectAction() 00046 { 00047 delete d; 00048 } 00049 00050 void KIconSelectAction::updateIcons() 00051 { 00052 if (d->m_menu) 00053 { 00054 QStringList lst = items(); 00055 for (uint id=0; id<lst.count(); ++id) 00056 d->m_menu->changeItem(id, SmallIconSet(d->m_iconlst[id]), lst[id]); 00057 } 00058 } 00059 00060 void KIconSelectAction::createPopupMenu() 00061 { 00062 if (!d->m_menu) 00063 { 00064 d->m_menu = popupMenu(); 00065 updateIcons(); 00066 } 00067 } 00068 00069 void KIconSelectAction::setItems(const QStringList& lst, const QStringList& iconlst) 00070 { 00071 KSelectAction::setItems(lst); 00072 d->m_iconlst = iconlst; 00073 updateIcons(); 00074 } 00075 00076 int KIconSelectAction::plug(QWidget* widget, int index) 00077 { 00078 int value(-1); 00079 if (widget->inherits("QPopupMenu")) 00080 { 00081 createPopupMenu(); 00082 value = KSelectAction::plug(widget, index); 00083 } 00084 else if (widget->inherits("KToolBar")) 00085 { 00086 KToolBar* bar = static_cast<KToolBar*>(widget); 00087 int id = KAction::getToolButtonID(); 00088 // To have a correct layout in the toolbar, a non 00089 // empty icon has to be used. Use "unknown" by default. 00090 QString iconName = (currentItem() != -1 ? d->m_iconlst[currentItem()] : "unknown"); 00091 00092 createPopupMenu(); 00093 bar->insertButton(iconName, id, true, plainText(), index); 00094 bar->getButton(id)->setPopup(d->m_menu, true); 00095 bar->setItemEnabled(id, isEnabled()); 00096 addContainer(bar, id); 00097 connect(bar, SIGNAL(destroyed()), SLOT(slotDestroyed())); 00098 00099 value = containerCount()-1; 00100 } 00101 return value; 00102 } 00103 00104 void KIconSelectAction::updateCurrentItem(int id) 00105 { 00106 QWidget* w = container(id); 00107 if (w->inherits("KToolBar")) 00108 static_cast<KToolBar*>(w)->getButton(itemId(id))->setIcon(d->m_iconlst[currentItem()]); 00109 else 00110 KSelectAction::updateCurrentItem(id); 00111 } 00112 00113 void KIconSelectAction::setCurrentItem(int index) 00114 { 00115 KSelectAction::setCurrentItem(index); 00116 } 00117 00118 #include "kiconselectaction.moc"