kexi

pixmapcollection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
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
20#include <tqpixmap.h>
21#include <tqlayout.h>
22#include <tqlabel.h>
23#include <tqstringlist.h>
24#include <tqtoolbutton.h>
25#include <tqdom.h>
26
27#include <tdeapplication.h>
28#include <kiconloader.h>
29#include <tdefiledialog.h>
30#include <kcombobox.h>
31#include <kicondialog.h>
32#include <klineedit.h>
33#include <kicontheme.h>
34#include <kpixmapio.h>
35#include <tdepopupmenu.h>
36#include <kdebug.h>
37#include <tdelocale.h>
38#include <tdemessagebox.h>
39
40#include "pixmapcollection.h"
41
43PixmapCollection::PixmapCollection(const TQString &collectionName, TQObject *parent, const char *name)
44 : TQObject(parent, name)
45{
46 m_name = collectionName;
47}
48
49TQString
50PixmapCollection::addPixmapPath(const KURL &url)
51{
52 TQString name = url.filename();
53 while(m_pixmaps.contains(name))
54 {
55 bool ok;
56 int num = name.right(1).toInt(&ok, 10);
57 if(ok)
58 name = name.left(name.length()-1) + TQString::number(num+1);
59 else
60 name += "2";
61 }
62
63 m_pixmaps.insert(name, qMakePair(url.path(), 0));
64 return name;
65}
66
67TQString
68PixmapCollection::addPixmapName(const TQString &icon, int size)
69{
70 TQString name = icon;
71 while(m_pixmaps.contains(name))
72 {
73 bool ok;
74 int num = name.right(1).toInt(&ok, 10);
75 if(ok)
76 name = name.left(name.length()-1) + TQString::number(num+1);
77 else
78 name += "2";
79 }
80
81 m_pixmaps.insert(name, qMakePair(icon, size));
82 return name;
83}
84
85void
86PixmapCollection::removePixmap(const TQString &name)
87{
88 m_pixmaps.remove(name);
89}
90
91TQPixmap
92PixmapCollection::getPixmap(const TQString &name)
93{
94 if(!m_pixmaps.contains(name))
95 {
96 kdDebug() << " The icon " << name << " you requested is not in the collection" << endl;
97 return TQPixmap();
98 }
99
100 if(m_pixmaps[name].second != 0)
101 {
102 return kapp->iconLoader()->loadIcon(m_pixmaps[name].first, TDEIcon::NoGroup, m_pixmaps[name].second);
103 }
104 else
105 return TQPixmap(m_pixmaps[name].first);
106}
107
108bool
109PixmapCollection::contains(const TQString &name)
110{
111 return m_pixmaps.contains(name);
112}
113
114void
115PixmapCollection::save(TQDomNode parentNode)
116{
117 if(m_pixmaps.isEmpty())
118 return;
119
120 TQDomDocument domDoc = parentNode.ownerDocument();
121 TQDomElement collection = domDoc.createElement("collection");
122 parentNode.appendChild(collection);
123
124 PixmapMap::ConstIterator it;
125 PixmapMap::ConstIterator endIt = m_pixmaps.constEnd();
126 for(it = m_pixmaps.constBegin(); it != endIt; ++it)
127 {
128 TQDomElement item = domDoc.createElement("pixmap");
129 collection.appendChild(item);
130 item.setAttribute("name", it.key());
131 if(it.data().second != 0)
132 item.setAttribute("size", TQString::number(it.data().second));
133
134 TQString text = it.data().first;
135 TQDomText textNode = domDoc.createTextNode(text);
136 item.appendChild(textNode);
137 }
138}
139
140void
141PixmapCollection::load(TQDomNode node)
142{
143 TQDomDocument domDoc = node.ownerDocument();
144 for(TQDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling())
145 {
146 TQDomElement el = n.toElement();
147 TQPair<TQString, int> pair = qMakePair(el.text(), el.attribute("size").toInt());
148 m_pixmaps[el.attribute("name")] = pair;
149 }
150}
151
153LoadIconDialog::LoadIconDialog(TQWidget *parent)
154: KDialogBase(parent, "loadicon_dialog", true, i18n("Load KDE Icon by Name"), Ok|Cancel, Ok, false)
155{
156 TQFrame *frame = makeMainWidget();
157 TQGridLayout *l = new TQGridLayout(frame, 2, 3, 0, 6);
158
159 // Name input
160 TQLabel *name = new TQLabel(i18n("&Name:"), frame);
161 l->addWidget(name, 0, 0);
162 name->setAlignment(TQt::AlignRight|TQt::AlignVCenter);
163 m_nameInput = new KLineEdit("kexi", frame);
164 l->addWidget(m_nameInput, 0, 1);
165 name->setBuddy(m_nameInput);
166
167 // Choose size
168 TQLabel *size = new TQLabel(i18n("&Size:"), frame);
169 l->addWidget(size, 1, 0);
170 size->setAlignment(TQt::AlignRight|TQt::AlignVCenter);
171
172 KComboBox *combo = new KComboBox(frame);
173 l->addWidget(combo, 1, 1);
174 size->setBuddy(combo);
175 TQStringList list;
176 list << i18n("Small") << i18n("Medium") << i18n("Large") << i18n("Huge");
177 combo->insertStringList(list);
178 combo->setCurrentItem(2);
179 connect(combo, TQ_SIGNAL(activated(int)), this, TQ_SLOT(changeIconSize(int)));
180
181
182 // Icon chooser button
183 m_button = new TDEIconButton(frame);
184 m_button->setIcon("kexi");
185 m_button->setIconSize(TDEIcon::SizeMedium);
186 l->addMultiCellWidget(m_button, 0, 1, 2, 2);
187 connect(m_button, TQ_SIGNAL(iconChanged(TQString)), this, TQ_SLOT(updateIconName(TQString)));
188 connect(m_nameInput, TQ_SIGNAL(textChanged(const TQString &)), this, TQ_SLOT(setIcon(const TQString &)));
189}
190
191void
192LoadIconDialog::updateIconName(TQString icon)
193{
194 m_nameInput->setText(icon);
195}
196
197void
198LoadIconDialog::setIcon(const TQString &icon)
199{
200 m_button->setIcon(icon);
201}
202
203void
204LoadIconDialog::changeIconSize(int index)
205{
206 int size = TDEIcon::SizeMedium;
207 switch(index)
208 {
209 case 0: size = TDEIcon::SizeSmall; break;
210 //case 1: size = TDEIcon::SizeSmallMedium; break;
211 case 1: size = TDEIcon::SizeMedium; break;
212 case 2: size = TDEIcon::SizeLarge; break;
213#if !defined(TQ_WS_WIN)
214 case 3: size = TDEIcon::SizeHuge; break;
215#endif
216 default:;
217 }
218
219 m_button->setIconSize(size);
220}
221
222int LoadIconDialog::iconSize()
223{
224 return m_button->iconSize();
225}
226
227TQString LoadIconDialog::iconName()
228{
229 return m_button->icon();
230}
231
234: KDialogBase(parent, "pixcollection_dialog", true,
235 i18n("Edit Pixmap Collection: %1").arg(collection->collectionName()), Close, Close, false)
236{
237 m_collection = collection;
238 TQFrame *frame = makeMainWidget();
239 TQHBoxLayout *l = new TQHBoxLayout(frame, 0, 6);
240 setInitialSize(TQSize(400, 200), true);
241
243 TQVBoxLayout *vlayout = new TQVBoxLayout(l, 3);
244 TQToolButton *newItemPath = new TQToolButton(frame);
245 newItemPath->setIconSet(BarIconSet("document-open"));
246 newItemPath->setTextLabel(i18n("&Add File"), true);
247 vlayout->addWidget(newItemPath);
248 m_buttons.insert(BNewItemPath, newItemPath);
249 connect(newItemPath, TQ_SIGNAL(clicked()), this, TQ_SLOT(newItemByPath()));
250
251 TQToolButton *newItemName = new TQToolButton(frame);
252 newItemName->setIconSet(BarIconSet("icons"));
253 newItemName->setTextLabel(i18n("&Add an Icon"), true);
254 vlayout->addWidget(newItemName);
255 m_buttons.insert(BNewItemName, newItemName);
256 connect(newItemName, TQ_SIGNAL(clicked()), this, TQ_SLOT(newItemByName()));
257
258 TQToolButton *delItem = new TQToolButton(frame);
259 delItem->setIconSet(BarIconSet("edit_remove"));
260 delItem->setTextLabel(i18n("&Remove Selected Item"), true);
261 vlayout->addWidget(delItem);
262 m_buttons.insert(BDelItem, delItem);
263 connect(delItem, TQ_SIGNAL(clicked()), this, TQ_SLOT(removeItem()));
264 vlayout->addStretch();
265
266 // Setup the iconView
267 m_iconView = new TDEIconView(frame, "pixcollection_iconView");
268 m_iconView->resize(100,100);
269 m_iconView->setArrangement(TQIconView::LeftToRight);
270 m_iconView->setAutoArrange(true);
271 m_iconView->setMode(TDEIconView::Select);
272 l->addWidget(m_iconView);
273 connect(m_iconView, TQ_SIGNAL(contextMenuRequested(TQIconViewItem*, const TQPoint&)), this, TQ_SLOT(displayMenu(TQIconViewItem*, const TQPoint&)));
274 connect(m_iconView, TQ_SIGNAL(itemRenamed(TQIconViewItem*, const TQString &)), this, TQ_SLOT(renameCollectionItem(TQIconViewItem*, const TQString&)));
275
276 PixmapMap::ConstIterator it;
277 PixmapMap::ConstIterator endIt = collection->m_pixmaps.end();
278 for(it = collection->m_pixmaps.constBegin(); it != endIt; ++it)
279 createIconViewItem(it.key());
280}
281
282void
283PixmapCollectionEditor::newItemByName()
284{
285 LoadIconDialog d(parentWidget());
286 if(d.exec()== TQDialog::Accepted)
287 {
288 if(d.iconName().isEmpty())
289 return;
290
291 TQString name = m_collection->addPixmapName(d.iconName(), d.iconSize());
292 createIconViewItem(name);
293 }
294}
295
296void
297PixmapCollectionEditor::newItemByPath()
298{
299 KURL url = KFileDialog::getImageOpenURL("::kexi", parentWidget());
300 if(url.isEmpty())
301 return;
302 TQString name = m_collection->addPixmapPath(url);
303 createIconViewItem(name);
304}
305
306void
307PixmapCollectionEditor::removeItem()
308{
309 TQIconViewItem *item = m_iconView->currentItem();
310 if( !item )
311 return;
312
313 int confirm = KMessageBox::questionYesNo(parentWidget(), TQString("<qt>")+
314 i18n("Do you want to remove item \"%1\" from collection \"%2\"?")
315 .arg(item->text()).arg(m_collection->collectionName()) + "</qt>");
316 if(confirm == KMessageBox::No)
317 return;
318
319 m_collection->removePixmap(item->text());
320 delete item;
321}
322
323void
324PixmapCollectionEditor::renameItem()
325{
326 if(m_iconView->currentItem())
327 m_iconView->currentItem()->rename();
328}
329
330void
331PixmapCollectionEditor::createIconViewItem(const TQString &name)
332{
333 PixmapIconViewItem *item = new PixmapIconViewItem(m_iconView, name, getPixmap(name));
334 item->setRenameEnabled(true);
335}
336
337TQPixmap
338PixmapCollectionEditor::getPixmap(const TQString &name)
339{
340 TQPixmap pixmap = m_collection->getPixmap(name);
341 if((pixmap.width() <= 48) && (pixmap.height() <= 48))
342 return pixmap;
343
344 KPixmapIO io;
345 TQImage image = io.convertToImage(pixmap);
346 pixmap = io.convertToPixmap(image.scale(48, 48, TQImage::ScaleMin));
347 return pixmap;
348}
349
350void
351PixmapCollectionEditor::renameCollectionItem(TQIconViewItem *it, const TQString &name)
352{
353 PixmapIconViewItem *item = static_cast<PixmapIconViewItem*>(it);
354 if(!m_collection->m_pixmaps.contains(item->name()))
355 return;
356
357 // We just rename the collection item
358 TQPair<TQString, int> pair = m_collection->m_pixmaps[item->name()];
359 m_collection->m_pixmaps.remove(item->name());
360 m_collection->m_pixmaps[name] = pair;
361 item->setName(name);
362}
363
364void
365PixmapCollectionEditor::displayMenu(TQIconViewItem *it, const TQPoint &p)
366{
367 if(!it) return;
368 TDEPopupMenu *menu = new TDEPopupMenu();
369 menu->insertItem(SmallIconSet("edit"), i18n("Rename Item"), this, TQ_SLOT(renameItem()));
370 menu->insertItem(SmallIconSet("remove"), i18n("Remove Item"), this, TQ_SLOT(removeItem()));
371 menu->exec(p);
372}
373
375PixmapCollectionChooser::PixmapCollectionChooser(PixmapCollection *collection, const TQString &selectedItem, TQWidget *parent)
376: KDialogBase(parent, "pixchoose_dialog", true, i18n("Select Pixmap From %1").arg(collection->collectionName()),
377 User1|Ok|Cancel, Ok, false, KGuiItem(i18n("Edit Collection...")))
378{
379 m_collection = collection;
380 setInitialSize(TQSize(400, 200), true);
381
382 m_iconView = new TDEIconView(this, "pixchooser_iconView");
383 setMainWidget(m_iconView);
384 m_iconView->setArrangement(TQIconView::LeftToRight);
385 m_iconView->setAutoArrange(true);
386 m_iconView->setMode(TDEIconView::Select);
387
388 PixmapMap::ConstIterator it;
389 PixmapMap::ConstIterator endIt = collection->m_pixmaps.constEnd();
390 for(it = collection->m_pixmaps.constBegin(); it != endIt; ++it)
391 new PixmapIconViewItem(m_iconView, it.key(), getPixmap(it.key()));
392
393 TQIconViewItem *item = m_iconView->findItem(selectedItem, TQt::ExactMatch);
394 if(item && !selectedItem.isEmpty())
395 m_iconView->setCurrentItem(item);
396}
397
398TQPixmap
399PixmapCollectionChooser::pixmap()
400{
401 if(! m_iconView->currentItem())
402 return TQPixmap();
403 TQString name = m_iconView->currentItem()->text();
404 return m_collection->getPixmap(name);
405}
406
407TQString
408PixmapCollectionChooser::pixmapName()
409{
410 return m_iconView->currentItem() ? m_iconView->currentItem()->text() : TQString("");
411}
412
413TQPixmap
414PixmapCollectionChooser::getPixmap(const TQString &name)
415{
416 TQPixmap pixmap = m_collection->getPixmap(name);
417 if((pixmap.width() <= 48) && (pixmap.height() <= 48))
418 return pixmap;
419
420 // We scale the pixmap down to 48x48 to fit in the iconView
421 KPixmapIO io;
422 TQImage image = io.convertToImage(pixmap);
423 pixmap = io.convertToPixmap(image.scale(48, 48, TQImage::ScaleMin));
424 return pixmap;
425}
426
427void
428PixmapCollectionChooser::slotUser1()
429{
430 PixmapCollectionEditor dialog(m_collection, parentWidget());
431 dialog.exec();
432
433 m_iconView->clear();
434 PixmapMap::ConstIterator it;
435 PixmapMap::ConstIterator endIt = m_collection->m_pixmaps.constEnd();
436 for(it = m_collection->m_pixmaps.constBegin(); it != endIt; ++it)
437 new PixmapIconViewItem(m_iconView, it.key(), getPixmap(it.key()));
438}
439
440#include "pixmapcollection.moc"
A simple dialog to choose a KDE icon.
PixmapCollectionEditor(PixmapCollection *collection, TQWidget *parent=0)
Pixmap Collection Editor Dialog.
A class that store pixmaps (by path or by name for KDE icons).
PixmapCollection(const TQString &collectionName, TQObject *parent=0, const char *name=0)
Pixmap Collection.