kivio

kiviostencilsetaction.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Peter Simonsson <psn@linux.se>,
00003    theKompany.com & Dave Marotti
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 as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kiviostencilsetaction.h"
00022 
00023 #include <kpopupmenu.h>
00024 #include <kstandarddirs.h>
00025 #include <kapplication.h>
00026 #include <kdebug.h>
00027 #include <ktoolbar.h>
00028 #include <ktoolbarbutton.h>
00029 #include <kiconloader.h>
00030 #include <klocale.h>
00031 
00032 #include <qfile.h>
00033 #include <qdir.h>
00034 #include <qpixmap.h>
00035 #include <qwhatsthis.h>
00036 #include <qmenubar.h>
00037 #include <qstringlist.h>
00038 #include <qvaluelist.h>
00039 
00040 #include "kivio_stencil_spawner_set.h"
00041 
00042 static const char * default_plug_xpm[] = {
00043 "16 16 5 1",
00044 "   c None",
00045 ".  c #000000",
00046 "+  c #C4C7FF",
00047 "@  c #5961FF",
00048 "#  c #FFFFFF",
00049 "                ",
00050 "                ",
00051 " ...            ",
00052 " .++..          ",
00053 " .+@++..        ",
00054 " .+@@@++..      ",
00055 " .+@@@@@++..    ",
00056 " .+@@@@@@@++..  ",
00057 " .+@@@@@@@@@++. ",
00058 " .+@@@@@@@@@@@.#",
00059 " .+@@@@@@@@@@@.#",
00060 " .+@@@@@@@@@@@.#",
00061 " .+@@@@@@@@@@@.#",
00062 " .+@@@@@@@@@@@.#",
00063 " ..............#",
00064 "  ##############"};
00065 
00066 KivioStencilSetAction::KivioStencilSetAction(const QString &text, const QString &pix,
00067   KActionCollection *parent, const char *name)
00068   : KAction(text, pix, 0, parent, name)
00069 {
00070   setShortcutConfigurable( false );
00071 
00072   m_pathList.setAutoDelete(true);
00073 
00074   m_popup = new KPopupMenu(0L,"KivioStencilSetAction::popup");
00075   updateMenu();
00076 }
00077 
00078 KivioStencilSetAction::~KivioStencilSetAction()
00079 {
00080   clearCollectionMenuList();
00081   delete m_popup;
00082   m_popup = 0;
00083 }
00084 
00085 KPopupMenu* KivioStencilSetAction::popupMenu() const
00086 {
00087   return m_popup;
00088 }
00089 
00090 void KivioStencilSetAction::popup( const QPoint& global )
00091 {
00092   popupMenu()->popup(global);
00093 }
00094 
00095 int KivioStencilSetAction::plug( QWidget* widget, int index)
00096 {
00097   // This function is copied from KActionMenu::plug
00098   if (kapp && !kapp->authorizeKAction(name()))
00099     return -1;
00100   kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
00101   if ( widget->inherits("QPopupMenu") )
00102   {
00103     QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
00104     int id;
00105 
00106     if ( hasIconSet() )
00107       id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
00108     else
00109       id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), KIcon::Small),
00110         text(), popupMenu(), -1, index );
00111 
00112     if ( !isEnabled() )
00113       menu->setItemEnabled( id, false );
00114 
00115     addContainer( menu, id );
00116     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00117 
00118     return containerCount() - 1;
00119   }
00120   else if ( widget->inherits( "KToolBar" ) )
00121   {
00122     KToolBar *bar = static_cast<KToolBar *>( widget );
00123 
00124     int id_ = KAction::getToolButtonID();
00125 
00126     if ( icon().isEmpty() && !iconSet().isNull() )
00127       bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
00128                           SLOT( slotActivated() ), isEnabled(), plainText(),
00129                           index );
00130     else
00131     {
00132       KInstance *instance;
00133 
00134       if ( m_parentCollection )
00135         instance = m_parentCollection->instance();
00136       else
00137         instance = KGlobal::instance();
00138 
00139       bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
00140                           SLOT( slotActivated() ), isEnabled(), plainText(),
00141                           index, instance );
00142     }
00143 
00144     addContainer( bar, id_ );
00145 
00146     if (!whatsThis().isEmpty())
00147       QWhatsThis::add( bar->getButton(id_), whatsThis() );
00148 
00149     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00150 
00151     bar->getButton(id_)->setPopup(popupMenu(), true );
00152 
00153     return containerCount() - 1;
00154   }
00155   else if ( widget->inherits( "QMenuBar" ) )
00156   {
00157     QMenuBar *bar = static_cast<QMenuBar *>( widget );
00158 
00159     int id;
00160 
00161     id = bar->insertItem( text(), popupMenu(), -1, index );
00162 
00163     if ( !isEnabled() )
00164       bar->setItemEnabled( id, false );
00165 
00166     addContainer( bar, id );
00167     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00168 
00169     return containerCount() - 1;
00170   }
00171 
00172   return -1;
00173 }
00174 
00175 void KivioStencilSetAction::updateMenu()
00176 {
00177   m_ssId = 0;
00178   m_popup->clear();
00179 
00180   m_popup->insertItem(i18n("Show Stencil Set Chooser"), this, SIGNAL(showDialog()));
00181   m_popup->insertSeparator();
00182 
00183   m_pathList.clear();
00184   m_collectionIdList.clear();
00185   clearCollectionMenuList();
00186 
00187   KStandardDirs *dirs = KGlobal::dirs();
00188   QStringList dirList = dirs->findDirs("data", "kivio/stencils");
00189   dirList.sort();
00190 
00191   for( QStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it )
00192   {
00193     QString dir = (*it);
00194     loadCollections(dir);
00195   }
00196 }
00197 
00198 void KivioStencilSetAction::loadCollections( const QString& rootDirStr )
00199 {
00200   QDir rootDir( rootDirStr );
00201 
00202   rootDir.setFilter( QDir::Dirs );
00203   rootDir.setSorting( QDir::Name );
00204 
00205   const QFileInfoList *colList = rootDir.entryInfoList();
00206   QFileInfoListIterator colIt( *colList );
00207   QFileInfo *colFInfo;
00208   QString cId;
00209 
00210   while((colFInfo = colIt.current()))
00211   {
00212     if(colFInfo->fileName() != ".." && colFInfo->fileName() != ".")
00213     {
00214       cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
00215       KPopupMenu* ch;
00216       int index = m_collectionIdList.findIndex(cId);
00217       
00218       if(index < 0) {
00219         ch = new KPopupMenu;
00220         connect(ch,SIGNAL(activated(int)),SLOT(slotActivated(int)));
00221         m_popup->insertItem(QIconSet(dirtPixmap(colFInfo->absFilePath())),
00222           KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),ch);
00223         m_collectionIdList.append(cId);
00224         m_collectionMenuList.append(ch);
00225       } else {
00226         ch = m_collectionMenuList[index];
00227       }
00228     
00229       loadSet( ch, rootDirStr + "/" + colFInfo->fileName() );
00230     }
00231     
00232     ++colIt;
00233   }
00234 }
00235 
00236 void KivioStencilSetAction::loadSet( KPopupMenu* menu, const QString& rootDirStr )
00237 {
00238   QDir rootDir( rootDirStr );
00239 
00240   rootDir.setFilter( QDir::Dirs );
00241   rootDir.setSorting( QDir::Name );
00242 
00243   const QFileInfoList *setList = rootDir.entryInfoList();
00244   QFileInfoListIterator setIt( *setList );
00245   QFileInfo *setFInfo;
00246 
00247   while( (setFInfo = setIt.current()) )
00248   {
00249     if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
00250     {
00251       menu->insertItem(QIconSet(dirtPixmap(setFInfo->absFilePath())),
00252         KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),m_ssId);
00253       m_pathList.insert( m_ssId, new QString(rootDirStr + "/" + setFInfo->fileName()) );
00254       m_ssId++;
00255     }
00256     ++setIt;
00257   }
00258 }
00259 
00260 QPixmap KivioStencilSetAction::dirtPixmap( const QString& dir )
00261 {
00262     QString fs;
00263 
00264     if( QFile::exists(dir + "/icon.xpm") ) {
00265       fs = dir + "/icon.xpm";
00266     } else if( QFile::exists(dir + "/icon.png") ) {
00267       fs = dir + "/icon.png";
00268     } else {
00269       return QPixmap( (const char **)default_plug_xpm );
00270     }
00271 
00272     QFile file( fs );
00273     QFileInfo finfo( file );
00274     return QPixmap( finfo.absFilePath() );
00275 }
00276 
00277 void KivioStencilSetAction::slotActivated(int id)
00278 {
00279   if (id < 0 || !m_pathList.at((uint)id))
00280     return;
00281 
00282   QString path = *m_pathList.at((uint)id);
00283 
00284   emit activated(path);
00285 }
00286 
00287 void KivioStencilSetAction::clearCollectionMenuList()
00288 {
00289   QValueList<KPopupMenu*>::Iterator it;
00290   
00291   for(it = m_collectionMenuList.begin(); it != m_collectionMenuList.end(); ++it) {
00292     KPopupMenu* m = (*it);
00293     delete m;
00294   }
00295   
00296   m_collectionMenuList.clear();
00297 }
00298 
00299 #include "kiviostencilsetaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys