kivio

kivioaddstencilsetpanel.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004-2005 Peter Simonsson <psn@linux.se>,
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "kivioaddstencilsetpanel.h"
20
21#include <tqfile.h>
22#include <tqdir.h>
23#include <tqstringlist.h>
24#include <tqiconview.h>
25#include <tqgroupbox.h>
26#include <tqpushbutton.h>
27#include <tqtextbrowser.h>
28#include <tqsplitter.h>
29
30#include <kstandarddirs.h>
31#include <tdeglobal.h>
32#include <tdelistview.h>
33
34#include "kivio_stencil_spawner_set.h"
35#include "kivio_stencil_spawner.h"
36#include "kivio_stencil_spawner_info.h"
37#include "kivio_stencil.h"
38#include "kivioglobal.h"
39
40namespace Kivio {
41 AddStencilSetPanel::AddStencilSetPanel(TQWidget *parent, const char *name)
42 : KivioStencilSetWidget(parent, name)
43 {
44 int height = m_stencilsetGBox->height() / 2;
45 TQValueList<int> sizes;
46 sizes << height << height;
47 m_stencilSetSplitter->setSizes(sizes);
48 updateList();
49
50 connect(m_stencilSetLView, TQ_SIGNAL(selectionChanged(TQListViewItem*)), this, TQ_SLOT(changeStencilSet(TQListViewItem*)));
51 connect(m_addToDocBtn, TQ_SIGNAL(clicked()), this, TQ_SLOT(addToDocument()));
52
53 TQListViewItem* tmp = m_stencilSetLView->firstChild();
54
55 if(tmp) {
56 tmp = tmp->firstChild();
57
58 if(tmp) {
59 m_stencilSetLView->setSelected(tmp, true);
60 m_stencilSetLView->ensureItemVisible(tmp);
61 }
62 }
63 m_stencilSetLView-> setShadeSortColumn(false);
64 }
65
66
67 AddStencilSetPanel::~AddStencilSetPanel()
68 {
69 }
70
71 void AddStencilSetPanel::updateList()
72 {
73 TDEStandardDirs *dirs = TDEGlobal::dirs();
74 TQStringList dirList = dirs->findDirs("data", "kivio/stencils");
75 dirList.sort();
76 m_stencilSetLView->clear();
77
78 for(TQStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it)
79 {
80 TQString dir = (*it);
81 loadCollections(dir);
82 }
83 }
84
85 void AddStencilSetPanel::loadCollections(const TQString& dir)
86 {
87 TQDir rootDir( dir );
88
89 rootDir.setFilter( TQDir::Dirs );
90 rootDir.setSorting( TQDir::Name );
91
92 const TQFileInfoList *colList = rootDir.entryInfoList();
93 TQFileInfoListIterator colIt( *colList );
94 TQFileInfo *colFInfo;
95 TQString cId;
96
97 while((colFInfo = colIt.current()))
98 {
99 if((colFInfo->fileName() != "..") && (colFInfo->fileName() != "."))
100 {
101 cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
102 TQListViewItem* li = m_stencilSetLView->firstChild();
103
104 while(li) {
105 if(li->text(1) == cId) {
106 break;
107 }
108
109 li = li->nextSibling();
110 }
111
112 if(!li) {
113 li = new TDEListViewItem(m_stencilSetLView,
114 KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),
115 KivioStencilSpawnerSet::readId(colFInfo->absFilePath()));
116 li->setPixmap(0, loadIcon("icon", colFInfo->absFilePath()));
117 }
118
119 loadStencilSet(li, dir + "/" + colFInfo->fileName());
120 }
121
122 ++colIt;
123 }
124 }
125
126 void AddStencilSetPanel::loadStencilSet(TQListViewItem* li, const TQString& dir)
127 {
128 TQDir rootDir( dir );
129
130 rootDir.setFilter( TQDir::Dirs );
131 rootDir.setSorting( TQDir::Name );
132
133 const TQFileInfoList *setList = rootDir.entryInfoList();
134 TQFileInfoListIterator setIt( *setList );
135 TQFileInfo *setFInfo;
136
137 while( (setFInfo = setIt.current()) )
138 {
139 if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
140 {
141 TDEListViewItem* tmp = new TDEListViewItem(li, KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),
142 dir + "/" + setFInfo->fileName());
143 tmp->setPixmap(0, loadIcon("icon", setFInfo->absFilePath()));
144
145 if(m_currentDir == setFInfo->absFilePath()) {
146 tmp->setSelected(true);
147 m_stencilSetLView->ensureItemVisible(tmp);
148 }
149 }
150
151 ++setIt;
152 }
153 }
154
155 TQPixmap AddStencilSetPanel::loadIcon(const TQString& name, const TQString& dir)
156 {
157 TQString fs;
158
159 if( TQFile::exists(dir + "/" + name + ".xpm") ) {
160 fs = dir + "/" + name + ".xpm";
161 } else if( TQFile::exists(dir + "/" + name + ".png") ) {
162 fs = dir + "/" + name + ".png";
163 } else {
164 return TQPixmap();
165 }
166
167 return TQPixmap( fs );
168 }
169
170 void AddStencilSetPanel::changeStencilSet(TQListViewItem* li)
171 {
172 if(!li || !li->parent()) {
173 return;
174 }
175
176 m_stencilsetGBox->setTitle(li->text(0));
177 m_addToDocBtn->setEnabled(true);
178 KivioStencilSpawnerSet* spawnerSet = new KivioStencilSpawnerSet(li->text(0));
179 // Load the stencil icons
180 m_currentDir = li->text(1);
181 spawnerSet->loadDir(m_currentDir);
182 TQStringList files = spawnerSet->files();
183
184 // Remove the old icons
185 m_stencilIView->clear();
186 KivioStencilSpawner* spawner = 0;
187 KivioStencil* stencil = 0;
188 int count = 0;
189
190 for(TQStringList::Iterator it = files.begin(); it != files.end(); ++it)
191 {
192 spawner = spawnerSet->loadFile(m_currentDir + "/" + (*it));
193
194 if(spawner) {
195 stencil = spawner->newStencil();
196
197 if(stencil) {
198 (void) new TQIconViewItem(m_stencilIView, spawner->info()->title(), Kivio::generatePixmapFromStencil(32, 32, stencil));
199 delete stencil;
200 count++;
201 }
202 }
203
204 if(count == 10) { // Limit to max 10 stencils
205 break;
206 }
207 }
208
209 m_descriptionBrowser->setText(KivioStencilSpawnerSet::readDescription(m_currentDir));
210
211 delete spawnerSet;
212 }
213
214 void AddStencilSetPanel::addToDocument()
215 {
216 if(m_currentDir.isEmpty()) {
217 return;
218 }
219
220 emit addStencilSet(m_currentDir);
221 }
222}
223
224#include "kivioaddstencilsetpanel.moc"