kivio

kivioaddstencilsetpanel.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2005 Peter Simonsson <psn@linux.se>,
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
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 #include "kivioaddstencilsetpanel.h"
00020 
00021 #include <qfile.h>
00022 #include <qdir.h>
00023 #include <qstringlist.h>
00024 #include <qiconview.h>
00025 #include <qgroupbox.h>
00026 #include <qpushbutton.h>
00027 #include <qtextbrowser.h>
00028 #include <qsplitter.h>
00029 
00030 #include <kstandarddirs.h>
00031 #include <kglobal.h>
00032 #include <klistview.h>
00033 
00034 #include "kivio_stencil_spawner_set.h"
00035 #include "kivio_stencil_spawner.h"
00036 #include "kivio_stencil_spawner_info.h"
00037 #include "kivio_stencil.h"
00038 #include "kivioglobal.h"
00039 
00040 namespace Kivio {
00041   AddStencilSetPanel::AddStencilSetPanel(QWidget *parent, const char *name)
00042     : KivioStencilSetWidget(parent, name)
00043   {
00044     int height = m_stencilsetGBox->height() / 2;
00045     QValueList<int> sizes;
00046     sizes << height << height;
00047     m_stencilSetSplitter->setSizes(sizes);
00048     updateList();
00049 
00050     connect(m_stencilSetLView, SIGNAL(selectionChanged(QListViewItem*)), this, SLOT(changeStencilSet(QListViewItem*)));
00051     connect(m_addToDocBtn, SIGNAL(clicked()), this, SLOT(addToDocument()));
00052 
00053     QListViewItem* tmp = m_stencilSetLView->firstChild();
00054 
00055     if(tmp) {
00056       tmp = tmp->firstChild();
00057 
00058       if(tmp) {
00059         m_stencilSetLView->setSelected(tmp, true);
00060         m_stencilSetLView->ensureItemVisible(tmp);
00061       }
00062     }
00063   }
00064   
00065   
00066   AddStencilSetPanel::~AddStencilSetPanel()
00067   {
00068   }
00069   
00070   void AddStencilSetPanel::updateList()
00071   {
00072     KStandardDirs *dirs = KGlobal::dirs();
00073     QStringList dirList = dirs->findDirs("data", "kivio/stencils");
00074     dirList.sort();
00075     m_stencilSetLView->clear();
00076     
00077     for(QStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it)
00078     {
00079       QString dir = (*it);
00080       loadCollections(dir);
00081     }
00082   }
00083 
00084   void AddStencilSetPanel::loadCollections(const QString& dir)
00085   {
00086     QDir rootDir( dir );
00087   
00088     rootDir.setFilter( QDir::Dirs );
00089     rootDir.setSorting( QDir::Name );
00090   
00091     const QFileInfoList *colList = rootDir.entryInfoList();
00092     QFileInfoListIterator colIt( *colList );
00093     QFileInfo *colFInfo;
00094     QString cId;
00095   
00096     while((colFInfo = colIt.current()))
00097     {
00098       if((colFInfo->fileName() != "..") && (colFInfo->fileName() != "."))
00099       {
00100         cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
00101         QListViewItem* li = m_stencilSetLView->firstChild();
00102         
00103         while(li) {
00104           if(li->text(1) == cId) {
00105             break;
00106           }
00107           
00108           li = li->nextSibling();
00109         }
00110         
00111         if(!li) {
00112           li = new KListViewItem(m_stencilSetLView,
00113             KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),
00114             KivioStencilSpawnerSet::readId(colFInfo->absFilePath()));
00115           li->setPixmap(0, loadIcon("icon", colFInfo->absFilePath()));
00116         }
00117         
00118         loadStencilSet(li, dir + "/" + colFInfo->fileName());
00119       }
00120       
00121       ++colIt;
00122     }
00123   }
00124   
00125   void AddStencilSetPanel::loadStencilSet(QListViewItem* li, const QString& dir)
00126   {
00127     QDir rootDir( dir );
00128   
00129     rootDir.setFilter( QDir::Dirs );
00130     rootDir.setSorting( QDir::Name );
00131   
00132     const QFileInfoList *setList = rootDir.entryInfoList();
00133     QFileInfoListIterator setIt( *setList );
00134     QFileInfo *setFInfo;
00135   
00136     while( (setFInfo = setIt.current()) )
00137     {
00138       if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
00139       {
00140         KListViewItem* tmp = new KListViewItem(li, KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),
00141           dir + "/" + setFInfo->fileName());
00142         tmp->setPixmap(0, loadIcon("icon", setFInfo->absFilePath()));
00143 
00144         if(m_currentDir == setFInfo->absFilePath()) {
00145           tmp->setSelected(true);
00146           m_stencilSetLView->ensureItemVisible(tmp);
00147         }
00148       }
00149 
00150       ++setIt;
00151     }
00152   }
00153 
00154   QPixmap AddStencilSetPanel::loadIcon(const QString& name, const QString& dir)
00155   {
00156     QString fs;
00157 
00158     if( QFile::exists(dir + "/" + name + ".xpm") ) {
00159       fs = dir + "/" + name + ".xpm";
00160     } else if( QFile::exists(dir + "/" + name + ".png") ) {
00161       fs = dir + "/" + name + ".png";
00162     } else {
00163       return QPixmap();
00164     }
00165 
00166     return QPixmap( fs );
00167   }
00168 
00169   void AddStencilSetPanel::changeStencilSet(QListViewItem* li)
00170   {
00171     if(!li || !li->parent()) {
00172       return;
00173     }
00174     
00175     m_stencilsetGBox->setTitle(li->text(0));
00176     m_addToDocBtn->setEnabled(true);
00177     KivioStencilSpawnerSet* spawnerSet = new KivioStencilSpawnerSet(li->text(0));
00178     // Load the stencil icons
00179     m_currentDir = li->text(1);
00180     spawnerSet->loadDir(m_currentDir);
00181     QStringList files = spawnerSet->files();
00182 
00183     // Remove the old icons
00184     m_stencilIView->clear();
00185     KivioStencilSpawner* spawner = 0;
00186     KivioStencil* stencil = 0;
00187     int count = 0;
00188 
00189     for(QStringList::Iterator it = files.begin(); it != files.end(); ++it)
00190     {
00191       spawner = spawnerSet->loadFile(m_currentDir + "/" + (*it));
00192 
00193       if(spawner) {
00194         stencil = spawner->newStencil();
00195 
00196         if(stencil) {
00197           (void) new QIconViewItem(m_stencilIView, spawner->info()->title(), Kivio::generatePixmapFromStencil(32, 32, stencil));
00198           delete stencil;
00199           count++;
00200         }
00201       }
00202 
00203       if(count == 10) { // Limit to max 10 stencils
00204         break;
00205       }
00206     }
00207 
00208     m_descriptionBrowser->setText(KivioStencilSpawnerSet::readDescription(m_currentDir));
00209 
00210     delete spawnerSet;
00211   }
00212 
00213   void AddStencilSetPanel::addToDocument()
00214   {
00215     if(m_currentDir.isEmpty()) {
00216       return;
00217     }
00218     
00219     emit addStencilSet(m_currentDir);
00220   }
00221 }
00222 
00223 #include "kivioaddstencilsetpanel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys