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#if KDE_IS_VERSION(3,4,0)
64 m_stencilSetLView-> setShadeSortColumn(false);
65#endif
66 }
67
68
69 AddStencilSetPanel::~AddStencilSetPanel()
70 {
71 }
72
73 void AddStencilSetPanel::updateList()
74 {
75 TDEStandardDirs *dirs = TDEGlobal::dirs();
76 TQStringList dirList = dirs->findDirs("data", "kivio/stencils");
77 dirList.sort();
78 m_stencilSetLView->clear();
79
80 for(TQStringList::Iterator it = dirList.begin(); it != dirList.end(); ++it)
81 {
82 TQString dir = (*it);
83 loadCollections(dir);
84 }
85 }
86
87 void AddStencilSetPanel::loadCollections(const TQString& dir)
88 {
89 TQDir rootDir( dir );
90
91 rootDir.setFilter( TQDir::Dirs );
92 rootDir.setSorting( TQDir::Name );
93
94 const TQFileInfoList *colList = rootDir.entryInfoList();
95 TQFileInfoListIterator colIt( *colList );
96 TQFileInfo *colFInfo;
97 TQString cId;
98
99 while((colFInfo = colIt.current()))
100 {
101 if((colFInfo->fileName() != "..") && (colFInfo->fileName() != "."))
102 {
103 cId = KivioStencilSpawnerSet::readId(colFInfo->absFilePath());
104 TQListViewItem* li = m_stencilSetLView->firstChild();
105
106 while(li) {
107 if(li->text(1) == cId) {
108 break;
109 }
110
111 li = li->nextSibling();
112 }
113
114 if(!li) {
115 li = new TDEListViewItem(m_stencilSetLView,
116 KivioStencilSpawnerSet::readTitle(colFInfo->absFilePath()),
117 KivioStencilSpawnerSet::readId(colFInfo->absFilePath()));
118 li->setPixmap(0, loadIcon("icon", colFInfo->absFilePath()));
119 }
120
121 loadStencilSet(li, dir + "/" + colFInfo->fileName());
122 }
123
124 ++colIt;
125 }
126 }
127
128 void AddStencilSetPanel::loadStencilSet(TQListViewItem* li, const TQString& dir)
129 {
130 TQDir rootDir( dir );
131
132 rootDir.setFilter( TQDir::Dirs );
133 rootDir.setSorting( TQDir::Name );
134
135 const TQFileInfoList *setList = rootDir.entryInfoList();
136 TQFileInfoListIterator setIt( *setList );
137 TQFileInfo *setFInfo;
138
139 while( (setFInfo = setIt.current()) )
140 {
141 if( setFInfo->fileName() != ".." && setFInfo->fileName() != "." )
142 {
143 TDEListViewItem* tmp = new TDEListViewItem(li, KivioStencilSpawnerSet::readTitle(setFInfo->absFilePath()),
144 dir + "/" + setFInfo->fileName());
145 tmp->setPixmap(0, loadIcon("icon", setFInfo->absFilePath()));
146
147 if(m_currentDir == setFInfo->absFilePath()) {
148 tmp->setSelected(true);
149 m_stencilSetLView->ensureItemVisible(tmp);
150 }
151 }
152
153 ++setIt;
154 }
155 }
156
157 TQPixmap AddStencilSetPanel::loadIcon(const TQString& name, const TQString& dir)
158 {
159 TQString fs;
160
161 if( TQFile::exists(dir + "/" + name + ".xpm") ) {
162 fs = dir + "/" + name + ".xpm";
163 } else if( TQFile::exists(dir + "/" + name + ".png") ) {
164 fs = dir + "/" + name + ".png";
165 } else {
166 return TQPixmap();
167 }
168
169 return TQPixmap( fs );
170 }
171
172 void AddStencilSetPanel::changeStencilSet(TQListViewItem* li)
173 {
174 if(!li || !li->parent()) {
175 return;
176 }
177
178 m_stencilsetGBox->setTitle(li->text(0));
179 m_addToDocBtn->setEnabled(true);
180 KivioStencilSpawnerSet* spawnerSet = new KivioStencilSpawnerSet(li->text(0));
181 // Load the stencil icons
182 m_currentDir = li->text(1);
183 spawnerSet->loadDir(m_currentDir);
184 TQStringList files = spawnerSet->files();
185
186 // Remove the old icons
187 m_stencilIView->clear();
188 KivioStencilSpawner* spawner = 0;
189 KivioStencil* stencil = 0;
190 int count = 0;
191
192 for(TQStringList::Iterator it = files.begin(); it != files.end(); ++it)
193 {
194 spawner = spawnerSet->loadFile(m_currentDir + "/" + (*it));
195
196 if(spawner) {
197 stencil = spawner->newStencil();
198
199 if(stencil) {
200 (void) new TQIconViewItem(m_stencilIView, spawner->info()->title(), Kivio::generatePixmapFromStencil(32, 32, stencil));
201 delete stencil;
202 count++;
203 }
204 }
205
206 if(count == 10) { // Limit to max 10 stencils
207 break;
208 }
209 }
210
211 m_descriptionBrowser->setText(KivioStencilSpawnerSet::readDescription(m_currentDir));
212
213 delete spawnerSet;
214 }
215
216 void AddStencilSetPanel::addToDocument()
217 {
218 if(m_currentDir.isEmpty()) {
219 return;
220 }
221
222 emit addStencilSet(m_currentDir);
223 }
224}
225
226#include "kivioaddstencilsetpanel.moc"