summaryrefslogtreecommitdiffstats
path: root/kivio/kiviopart/kiviostencilsetaction.cpp
blob: b12d157f9496f9179bd3cd4d22506219faec5e7f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* This file is part of the KDE project
   Copyright (C) 2003 Peter Simonsson <psn@linux.se>,
   theKompany.com & Dave Marotti

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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 "kiviostencilsetaction.h"

#include <kpopupmenu.h>
#include <kstandarddirs.h>
#include <kapplication.h>
#include <kdebug.h>
#include <ktoolbar.h>
#include <ktoolbarbutton.h>
#include <kiconloader.h>
#include <klocale.h>

#include <tqfile.h>
#include <tqdir.h>
#include <tqpixmap.h>
#include <tqwhatsthis.h>
#include <tqmenubar.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>

#include "kivio_stencil_spawner_set.h"

static const char * default_plug_xpm[] = {
"16 16 5 1",
" 	c None",
".	c #000000",
"+	c #C4C7FF",
"@	c #5961FF",
"#	c #FFFFFF",
"                ",
"                ",
" ...            ",
" .++..          ",
" .+@++..        ",
" .+@@@++..      ",
" .+@@@@@++..    ",
" .+@@@@@@@++..  ",
" .+@@@@@@@@@++. ",
" .+@@@@@@@@@@@.#",
" .+@@@@@@@@@@@.#",
" .+@@@@@@@@@@@.#",
" .+@@@@@@@@@@@.#",
" .+@@@@@@@@@@@.#",
" ..............#",
"  ##############"};

KivioStencilSetAction::KivioStencilSetAction(const TQString &text, const TQString &pix,
  KActionCollection *tqparent, const char *name)
  : KAction(text, pix, 0, tqparent, name)
{
  setShortcutConfigurable( false );

  m_pathList.setAutoDelete(true);

  m_popup = new KPopupMenu(0L,"KivioStencilSetAction::popup");
  updateMenu();
}

KivioStencilSetAction::~KivioStencilSetAction()
{
  clearCollectionMenuList();
  delete m_popup;
  m_popup = 0;
}

KPopupMenu* KivioStencilSetAction::popupMenu() const
{
  return m_popup;
}

void KivioStencilSetAction::popup( const TQPoint& global )
{
  popupMenu()->popup(global);
}

int KivioStencilSetAction::plug( TQWidget* widget, int index)
{
  // This function is copied from KActionMenu::plug
  if (kapp && !kapp->authorizeKAction(name()))
    return -1;
  kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
  if ( widget->inherits(TQPOPUPMENU_OBJECT_NAME_STRING) )
  {
    TQPopupMenu* menu = static_cast<TQPopupMenu*>( widget );
    int id;

    if ( hasIconSet() )
      id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
    else
      id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), KIcon::Small),
        text(), popupMenu(), -1, index );

    if ( !isEnabled() )
      menu->setItemEnabled( id, false );

    addContainer( menu, id );
    connect( menu, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDestroyed() ) );

    return containerCount() - 1;
  }
  else if ( widget->inherits( "KToolBar" ) )
  {
    KToolBar *bar = static_cast<KToolBar *>( widget );

    int id_ = KAction::getToolButtonID();

    if ( icon().isEmpty() && !iconSet().isNull() )
      bar->insertButton( iconSet().pixmap(), id_, TQT_SIGNAL( clicked() ), this,
                          TQT_SLOT( slotActivated() ), isEnabled(), plainText(),
                          index );
    else
    {
      KInstance *instance;

      if ( m_parentCollection )
        instance = m_parentCollection->instance();
      else
        instance = KGlobal::instance();

      bar->insertButton( icon(), id_, TQT_SIGNAL( clicked() ), this,
                          TQT_SLOT( slotActivated() ), isEnabled(), plainText(),
                          index, instance );
    }

    addContainer( bar, id_ );

    if (!whatsThis().isEmpty())
      TQWhatsThis::add( bar->getButton(id_), whatsThis() );

    connect( bar, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDestroyed() ) );

    bar->getButton(id_)->setPopup(popupMenu(), true );

    return containerCount() - 1;
  }
  else if ( widget->inherits( TQMENUBAR_OBJECT_NAME_STRING ) )
  {
    TQMenuBar *bar = static_cast<TQMenuBar *>( widget );

    int id;

    id = bar->insertItem( text(), popupMenu(), -1, index );

    if ( !isEnabled() )
      bar->setItemEnabled( id, false );

    addContainer( bar, id );
    connect( bar, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDestroyed() ) );

    return containerCount() - 1;
  }

  return -1;
}

void KivioStencilSetAction::updateMenu()
{
  m_ssId = 0;
  m_popup->clear();

  m_popup->insertItem(i18n("Show Stencil Set Chooser"), this, TQT_SIGNAL(showDialog()));
  m_popup->insertSeparator();

  m_pathList.clear();
  m_collectionIdList.clear();
  clearCollectionMenuList();

  KStandardDirs *dirs = KGlobal::dirs();
  TQStringList dirList = dirs->findDirs("data", "kivio/stencils");
  dirList.sort();

  for( TQStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it )
  {
    TQString dir = (*it);
    loadCollections(dir);
  }
}

void KivioStencilSetAction::loadCollections( const TQString& rootDirStr )
{
  TQDir rootDir( rootDirStr );

  rootDir.setFilter( TQDir::Dirs );
  rootDir.setSorting( TQDir::Name );

  const TQFileInfoList *colList = rootDir.entryInfoList();
  TQFileInfoListIterator colIt( *colList );
  TQFileInfo *colFInfo;
  TQString cId;

  while((colFInfo = colIt.current()))
  {
    if(colFInfo->fileName() != ".." && colFInfo->fileName() != ".")
    {
      cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
      KPopupMenu* ch;
      int index = m_collectionIdList.tqfindIndex(cId);
      
      if(index < 0) {
        ch = new KPopupMenu;
        connect(ch,TQT_SIGNAL(activated(int)),TQT_SLOT(slotActivated(int)));
        m_popup->insertItem(TQIconSet(dirtPixmap(colFInfo->absFilePath())),
          KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),ch);
        m_collectionIdList.append(cId);
        m_collectionMenuList.append(ch);
      } else {
        ch = m_collectionMenuList[index];
      }
    
      loadSet( ch, rootDirStr + "/" + colFInfo->fileName() );
    }
    
    ++colIt;
  }
}

void KivioStencilSetAction::loadSet( KPopupMenu* menu, const TQString& rootDirStr )
{
  TQDir rootDir( rootDirStr );

  rootDir.setFilter( TQDir::Dirs );
  rootDir.setSorting( TQDir::Name );

  const TQFileInfoList *setList = rootDir.entryInfoList();
  TQFileInfoListIterator setIt( *setList );
  TQFileInfo *setFInfo;

  while( (setFInfo = setIt.current()) )
  {
    if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
    {
      menu->insertItem(TQIconSet(dirtPixmap(setFInfo->absFilePath())),
        KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),m_ssId);
      m_pathList.insert( m_ssId, new TQString(rootDirStr + "/" + setFInfo->fileName()) );
      m_ssId++;
    }
    ++setIt;
  }
}

TQPixmap KivioStencilSetAction::dirtPixmap( const TQString& dir )
{
    TQString fs;

    if( TQFile::exists(dir + "/icon.xpm") ) {
      fs = dir + "/icon.xpm";
    } else if( TQFile::exists(dir + "/icon.png") ) {
      fs = dir + "/icon.png";
    } else {
      return TQPixmap( (const char **)default_plug_xpm );
    }

    TQFile file( fs );
    TQFileInfo finfo( file );
    return TQPixmap( finfo.absFilePath() );
}

void KivioStencilSetAction::slotActivated(int id)
{
  if (id < 0 || !m_pathList.at((uint)id))
    return;

  TQString path = *m_pathList.at((uint)id);

  emit activated(path);
}

void KivioStencilSetAction::clearCollectionMenuList()
{
  TQValueList<KPopupMenu*>::Iterator it;
  
  for(it = m_collectionMenuList.begin(); it != m_collectionMenuList.end(); ++it) {
    KPopupMenu* m = (*it);
    delete m;
  }
  
  m_collectionMenuList.clear();
}

#include "kiviostencilsetaction.moc"