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 #if KDE_IS_VERSION(3,4,0)
00064     m_stencilSetLView-> setShadeSortColumn(false);
00065 #endif
00066   }
00067   
00068   
00069   AddStencilSetPanel::~AddStencilSetPanel()
00070   {
00071   }
00072   
00073   void AddStencilSetPanel::updateList()
00074   {
00075     KStandardDirs *dirs = KGlobal::dirs();
00076     QStringList dirList = dirs->findDirs("data", "kivio/stencils");
00077     dirList.sort();
00078     m_stencilSetLView->clear();
00079     
00080     for(QStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it)
00081     {
00082       QString dir = (*it);
00083       loadCollections(dir);
00084     }
00085   }
00086 
00087   void AddStencilSetPanel::loadCollections(const QString& dir)
00088   {
00089     QDir rootDir( dir );
00090   
00091     rootDir.setFilter( QDir::Dirs );
00092     rootDir.setSorting( QDir::Name );
00093   
00094     const QFileInfoList *colList = rootDir.entryInfoList();
00095     QFileInfoListIterator colIt( *colList );
00096     QFileInfo *colFInfo;
00097     QString cId;
00098   
00099     while((colFInfo = colIt.current()))
00100     {
00101       if((colFInfo->fileName() != "..") && (colFInfo->fileName() != "."))
00102       {
00103         cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
00104         QListViewItem* li = m_stencilSetLView->firstChild();
00105         
00106         while(li) {
00107           if(li->text(1) == cId) {
00108             break;
00109           }
00110           
00111           li = li->nextSibling();
00112         }
00113         
00114         if(!li) {
00115           li = new KListViewItem(m_stencilSetLView,
00116             KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),
00117             KivioStencilSpawnerSet::readId(colFInfo->absFilePath()));
00118           li->setPixmap(0, loadIcon("icon", colFInfo->absFilePath()));
00119         }
00120         
00121         loadStencilSet(li, dir + "/" + colFInfo->fileName());
00122       }
00123       
00124       ++colIt;
00125     }
00126   }
00127   
00128   void AddStencilSetPanel::loadStencilSet(QListViewItem* li, const QString& dir)
00129   {
00130     QDir rootDir( dir );
00131   
00132     rootDir.setFilter( QDir::Dirs );
00133     rootDir.setSorting( QDir::Name );
00134   
00135     const QFileInfoList *setList = rootDir.entryInfoList();
00136     QFileInfoListIterator setIt( *setList );
00137     QFileInfo *setFInfo;
00138   
00139     while( (setFInfo = setIt.current()) )
00140     {
00141       if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
00142       {
00143         KListViewItem* tmp = new KListViewItem(li, KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),
00144           dir + "/" + setFInfo->fileName());
00145         tmp->setPixmap(0, loadIcon("icon", setFInfo->absFilePath()));
00146 
00147         if(m_currentDir == setFInfo->absFilePath()) {
00148           tmp->setSelected(true);
00149           m_stencilSetLView->ensureItemVisible(tmp);
00150         }
00151       }
00152 
00153       ++setIt;
00154     }
00155   }
00156 
00157   QPixmap AddStencilSetPanel::loadIcon(const QString& name, const QString& dir)
00158   {
00159     QString fs;
00160 
00161     if( QFile::exists(dir + "/" + name + ".xpm") ) {
00162       fs = dir + "/" + name + ".xpm";
00163     } else if( QFile::exists(dir + "/" + name + ".png") ) {
00164       fs = dir + "/" + name + ".png";
00165     } else {
00166       return QPixmap();
00167     }
00168 
00169     return QPixmap( fs );
00170   }
00171 
00172   void AddStencilSetPanel::changeStencilSet(QListViewItem* li)
00173   {
00174     if(!li || !li->parent()) {
00175       return;
00176     }
00177     
00178     m_stencilsetGBox->setTitle(li->text(0));
00179     m_addToDocBtn->setEnabled(true);
00180     KivioStencilSpawnerSet* spawnerSet = new KivioStencilSpawnerSet(li->text(0));
00181     // Load the stencil icons
00182     m_currentDir = li->text(1);
00183     spawnerSet->loadDir(m_currentDir);
00184     QStringList files = spawnerSet->files();
00185 
00186     // Remove the old icons
00187     m_stencilIView->clear();
00188     KivioStencilSpawner* spawner = 0;
00189     KivioStencil* stencil = 0;
00190     int count = 0;
00191 
00192     for(QStringList::Iterator it = files.begin(); it != files.end(); ++it)
00193     {
00194       spawner = spawnerSet->loadFile(m_currentDir + "/" + (*it));
00195 
00196       if(spawner) {
00197         stencil = spawner->newStencil();
00198 
00199         if(stencil) {
00200           (void) new QIconViewItem(m_stencilIView, spawner->info()->title(), Kivio::generatePixmapFromStencil(32, 32, stencil));
00201           delete stencil;
00202           count++;
00203         }
00204       }
00205 
00206       if(count == 10) { // Limit to max 10 stencils
00207         break;
00208       }
00209     }
00210 
00211     m_descriptionBrowser->setText(KivioStencilSpawnerSet::readDescription(m_currentDir));
00212 
00213     delete spawnerSet;
00214   }
00215 
00216   void AddStencilSetPanel::addToDocument()
00217   {
00218     if(m_currentDir.isEmpty()) {
00219       return;
00220     }
00221     
00222     emit addStencilSet(m_currentDir);
00223   }
00224 }
00225 
00226 #include "kivioaddstencilsetpanel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys