kiviostencilsetaction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Peter Simonsson <psn@linux.se>,
3 theKompany.com & Dave Marotti
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kiviostencilsetaction.h"
22
23#include <tdepopupmenu.h>
24#include <kstandarddirs.h>
25#include <tdeapplication.h>
26#include <kdebug.h>
27#include <tdetoolbar.h>
28#include <tdetoolbarbutton.h>
29#include <kiconloader.h>
30#include <tdelocale.h>
31
32#include <tqfile.h>
33#include <tqdir.h>
34#include <tqpixmap.h>
35#include <tqwhatsthis.h>
36#include <tqmenubar.h>
37#include <tqstringlist.h>
38#include <tqvaluelist.h>
39
40#include "kivio_stencil_spawner_set.h"
41
42static const char * default_plug_xpm[] = {
43"16 16 5 1",
44" c None",
45". c #000000",
46"+ c #C4C7FF",
47"@ c #5961FF",
48"# c #FFFFFF",
49" ",
50" ",
51" ... ",
52" .++.. ",
53" .+@++.. ",
54" .+@@@++.. ",
55" .+@@@@@++.. ",
56" .+@@@@@@@++.. ",
57" .+@@@@@@@@@++. ",
58" .+@@@@@@@@@@@.#",
59" .+@@@@@@@@@@@.#",
60" .+@@@@@@@@@@@.#",
61" .+@@@@@@@@@@@.#",
62" .+@@@@@@@@@@@.#",
63" ..............#",
64" ##############"};
65
66KivioStencilSetAction::KivioStencilSetAction(const TQString &text, const TQString &pix,
67 TDEActionCollection *parent, const char *name)
68 : TDEAction(text, pix, 0, parent, name)
69{
70 setShortcutConfigurable( false );
71
72 m_pathList.setAutoDelete(true);
73
74 m_popup = new TDEPopupMenu(0L,"KivioStencilSetAction::popup");
75 updateMenu();
76}
77
78KivioStencilSetAction::~KivioStencilSetAction()
79{
80 clearCollectionMenuList();
81 delete m_popup;
82 m_popup = 0;
83}
84
85TDEPopupMenu* KivioStencilSetAction::popupMenu() const
86{
87 return m_popup;
88}
89
90void KivioStencilSetAction::popup( const TQPoint& global )
91{
92 popupMenu()->popup(global);
93}
94
95int KivioStencilSetAction::plug( TQWidget* widget, int index)
96{
97 // This function is copied from TDEActionMenu::plug
98 if (kapp && !kapp->authorizeTDEAction(name()))
99 return -1;
100 kdDebug(129) << "TDEAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
101 if ( widget->inherits("TQPopupMenu") )
102 {
103 TQPopupMenu* menu = static_cast<TQPopupMenu*>( widget );
104 int id;
105
106 if ( hasIconSet() )
107 id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
108 else
109 id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), TDEIcon::Small),
110 text(), popupMenu(), -1, index );
111
112 if ( !isEnabled() )
113 menu->setItemEnabled( id, false );
114
115 addContainer( menu, id );
116 connect( menu, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
117
118 return containerCount() - 1;
119 }
120 else if ( widget->inherits( "TDEToolBar" ) )
121 {
122 TDEToolBar *bar = static_cast<TDEToolBar *>( widget );
123
124 int id_ = TDEAction::getToolButtonID();
125
126 if ( icon().isEmpty() && !iconSet().isNull() )
127 bar->insertButton( iconSet().pixmap(), id_, TQ_SIGNAL( clicked() ), this,
128 TQ_SLOT( slotActivated() ), isEnabled(), plainText(),
129 index );
130 else
131 {
132 TDEInstance *instance;
133
134 if ( m_parentCollection )
135 instance = m_parentCollection->instance();
136 else
137 instance = TDEGlobal::instance();
138
139 bar->insertButton( icon(), id_, TQ_SIGNAL( clicked() ), this,
140 TQ_SLOT( slotActivated() ), isEnabled(), plainText(),
141 index, instance );
142 }
143
144 addContainer( bar, id_ );
145
146 if (!whatsThis().isEmpty())
147 TQWhatsThis::add( bar->getButton(id_), whatsThis() );
148
149 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
150
151 bar->getButton(id_)->setPopup(popupMenu(), true );
152
153 return containerCount() - 1;
154 }
155 else if ( widget->inherits( "TQMenuBar" ) )
156 {
157 TQMenuBar *bar = static_cast<TQMenuBar *>( widget );
158
159 int id;
160
161 id = bar->insertItem( text(), popupMenu(), -1, index );
162
163 if ( !isEnabled() )
164 bar->setItemEnabled( id, false );
165
166 addContainer( bar, id );
167 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
168
169 return containerCount() - 1;
170 }
171
172 return -1;
173}
174
175void KivioStencilSetAction::updateMenu()
176{
177 m_ssId = 0;
178 m_popup->clear();
179
180 m_popup->insertItem(i18n("Show Stencil Set Chooser"), this, TQ_SIGNAL(showDialog()));
181 m_popup->insertSeparator();
182
183 m_pathList.clear();
184 m_collectionIdList.clear();
185 clearCollectionMenuList();
186
187 TDEStandardDirs *dirs = TDEGlobal::dirs();
188 TQStringList dirList = dirs->findDirs("data", "kivio/stencils");
189 dirList.sort();
190
191 for( TQStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it )
192 {
193 TQString dir = (*it);
194 loadCollections(dir);
195 }
196}
197
198void KivioStencilSetAction::loadCollections( const TQString& rootDirStr )
199{
200 TQDir rootDir( rootDirStr );
201
202 rootDir.setFilter( TQDir::Dirs );
203 rootDir.setSorting( TQDir::Name );
204
205 const TQFileInfoList *colList = rootDir.entryInfoList();
206 TQFileInfoListIterator colIt( *colList );
207 TQFileInfo *colFInfo;
208 TQString cId;
209
210 while((colFInfo = colIt.current()))
211 {
212 if(colFInfo->fileName() != ".." && colFInfo->fileName() != ".")
213 {
214 cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
215 TDEPopupMenu* ch;
216 int index = m_collectionIdList.findIndex(cId);
217
218 if(index < 0) {
219 ch = new TDEPopupMenu;
220 connect(ch,TQ_SIGNAL(activated(int)),TQ_SLOT(slotActivated(int)));
221 m_popup->insertItem(TQIconSet(dirtPixmap(colFInfo->absFilePath())),
222 KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),ch);
223 m_collectionIdList.append(cId);
224 m_collectionMenuList.append(ch);
225 } else {
226 ch = m_collectionMenuList[index];
227 }
228
229 loadSet( ch, rootDirStr + "/" + colFInfo->fileName() );
230 }
231
232 ++colIt;
233 }
234}
235
236void KivioStencilSetAction::loadSet( TDEPopupMenu* menu, const TQString& rootDirStr )
237{
238 TQDir rootDir( rootDirStr );
239
240 rootDir.setFilter( TQDir::Dirs );
241 rootDir.setSorting( TQDir::Name );
242
243 const TQFileInfoList *setList = rootDir.entryInfoList();
244 TQFileInfoListIterator setIt( *setList );
245 TQFileInfo *setFInfo;
246
247 while( (setFInfo = setIt.current()) )
248 {
249 if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
250 {
251 menu->insertItem(TQIconSet(dirtPixmap(setFInfo->absFilePath())),
252 KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),m_ssId);
253 m_pathList.insert( m_ssId, new TQString(rootDirStr + "/" + setFInfo->fileName()) );
254 m_ssId++;
255 }
256 ++setIt;
257 }
258}
259
260TQPixmap KivioStencilSetAction::dirtPixmap( const TQString& dir )
261{
262 TQString fs;
263
264 if( TQFile::exists(dir + "/icon.xpm") ) {
265 fs = dir + "/icon.xpm";
266 } else if( TQFile::exists(dir + "/icon.png") ) {
267 fs = dir + "/icon.png";
268 } else {
269 return TQPixmap( (const char **)default_plug_xpm );
270 }
271
272 TQFile file( fs );
273 TQFileInfo finfo( file );
274 return TQPixmap( finfo.absFilePath() );
275}
276
277void KivioStencilSetAction::slotActivated(int id)
278{
279 if (id < 0 || !m_pathList.at((uint)id))
280 return;
281
282 TQString path = *m_pathList.at((uint)id);
283
284 emit activated(path);
285}
286
287void KivioStencilSetAction::clearCollectionMenuList()
288{
289 TQValueList<TDEPopupMenu*>::Iterator it;
290
291 for(it = m_collectionMenuList.begin(); it != m_collectionMenuList.end(); ++it) {
292 TDEPopupMenu* m = (*it);
293 delete m;
294 }
295
296 m_collectionMenuList.clear();
297}
298
299#include "kiviostencilsetaction.moc"