summaryrefslogtreecommitdiffstats
path: root/kexi/widget/pixmapcollection.cpp
blob: 19875521cb38136e9c88ec34199b1716a288dc3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqpixmap.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqstringlist.h>
#include <tqtoolbutton.h>
#include <tqdom.h>

#include <kapplication.h>
#include <kiconloader.h>
#include <kfiledialog.h>
#include <kcombobox.h>
#include <kicondialog.h>
#include <klineedit.h>
#include <kicontheme.h>
#include <kpixmapio.h>
#include <kpopupmenu.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "pixmapcollection.h"

/// Pixmap Collection
PixmapCollection::PixmapCollection(const TQString &collectionName, TQObject *parent, const char *name)
 : TQObject(parent, name)
{
	m_name = collectionName;
}

TQString
PixmapCollection::addPixmapPath(const KURL &url)
{
	TQString name = url.filename();
	while(m_pixmaps.contains(name))
	{
		bool ok;
		int num = name.right(1).toInt(&ok, 10);
		if(ok)
			name = name.left(name.length()-1) + TQString::number(num+1);
		else
			name += "2";
	}

	m_pixmaps.insert(name, tqMakePair(url.path(), 0));
	return name;
}

TQString
PixmapCollection::addPixmapName(const TQString &icon, int size)
{
	TQString name = icon;
	while(m_pixmaps.contains(name))
	{
		bool ok;
		int num = name.right(1).toInt(&ok, 10);
		if(ok)
			name = name.left(name.length()-1) + TQString::number(num+1);
		else
			name += "2";
	}

	m_pixmaps.insert(name, tqMakePair(icon, size));
	return name;
}

void
PixmapCollection::removePixmap(const TQString &name)
{
	m_pixmaps.remove(name);
}

TQPixmap
PixmapCollection::getPixmap(const TQString &name)
{
	if(!m_pixmaps.contains(name))
	{
		kdDebug() << " The icon " << name << " you requested is not in the collection" << endl;
		return TQPixmap();
	}

	if(m_pixmaps[name].second != 0)
	{
		return kapp->iconLoader()->loadIcon(m_pixmaps[name].first, KIcon::NoGroup, m_pixmaps[name].second);
	}
	else
		return TQPixmap(m_pixmaps[name].first);
}

bool
PixmapCollection::contains(const TQString &name)
{
	return m_pixmaps.contains(name);
}

void
PixmapCollection::save(TQDomNode parentNode)
{
	if(m_pixmaps.isEmpty())
		return;

	TQDomDocument domDoc = parentNode.ownerDocument();
	TQDomElement collection = domDoc.createElement("collection");
	parentNode.appendChild(collection);

	PixmapMap::ConstIterator it;
	PixmapMap::ConstIterator endIt = m_pixmaps.constEnd();
	for(it = m_pixmaps.constBegin(); it != endIt; ++it)
	{
		TQDomElement item = domDoc.createElement("pixmap");
		collection.appendChild(item);
		item.setAttribute("name", it.key());
		if(it.data().second != 0)
			item.setAttribute("size", TQString::number(it.data().second));

		TQString text = it.data().first;
		TQDomText textNode = domDoc.createTextNode(text);
		item.appendChild(textNode);
	}
}

void
PixmapCollection::load(TQDomNode node)
{
	TQDomDocument domDoc = node.ownerDocument();
	for(TQDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling())
	{
		TQDomElement el = n.toElement();
		TQPair<TQString, int> pair = tqMakePair(el.text(), el.attribute("size").toInt());
		m_pixmaps[el.attribute("name")] = pair;
	}
}

//// A dialog to load a KDE icon by its name
LoadIconDialog::LoadIconDialog(TQWidget *parent)
: KDialogBase(parent, "loadicon_dialog", true, i18n("Load KDE Icon by Name"), Ok|Cancel, Ok, false)
{
	TQFrame *frame = makeMainWidget();
	TQGridLayout *l = new TQGridLayout(frame, 2, 3, 0, 6);

	// Name input
	TQLabel *name = new TQLabel(i18n("&Name:"), frame);
	l->addWidget(name, 0, 0);
	name->setAlignment(TQt::AlignRight|TQt::AlignVCenter);
	m_nameInput = new KLineEdit("kexi", frame);
	l->addWidget(m_nameInput, 0, 1);
	name->setBuddy(m_nameInput);

	// Choose size
	TQLabel *size = new TQLabel(i18n("&Size:"), frame);
	l->addWidget(size, 1, 0);
	size->setAlignment(TQt::AlignRight|TQt::AlignVCenter);

	KComboBox *combo = new KComboBox(frame);
	l->addWidget(combo, 1, 1);
	size->setBuddy(combo);
	TQStringList list;
	list << i18n("Small") << i18n("Medium") << i18n("Large") << i18n("Huge");
	combo->insertStringList(list);
	combo->setCurrentItem(2);
	connect(combo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(changeIconSize(int)));


	// Icon chooser button
	m_button = new KIconButton(frame);
	m_button->setIcon("kexi");
	m_button->setIconSize(KIcon::SizeMedium);
	l->addMultiCellWidget(m_button, 0, 1, 2, 2);
	connect(m_button, TQT_SIGNAL(iconChanged(TQString)), this, TQT_SLOT(updateIconName(TQString)));
	connect(m_nameInput, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(setIcon(const TQString &)));
}

void
LoadIconDialog::updateIconName(TQString icon)
{
	m_nameInput->setText(icon);
}

void
LoadIconDialog::setIcon(const TQString &icon)
{
	m_button->setIcon(icon);
}

void
LoadIconDialog::changeIconSize(int index)
{
	int size = KIcon::SizeMedium;
	switch(index)
	{
		case 0: size = KIcon::SizeSmall; break;
		//case 1: size = KIcon::SizeSmallMedium; break;
		case 1: size = KIcon::SizeMedium; break;
		case 2: size = KIcon::SizeLarge; break;
#if !defined(TQ_WS_WIN) && KDE_IS_VERSION(3,1,9)
		case 3: size = KIcon::SizeHuge; break;
#endif
		default:;
	}

	m_button->setIconSize(size);
}

int LoadIconDialog::iconSize()
{
	return m_button->iconSize();
}

TQString LoadIconDialog::iconName()
{
	return m_button->icon();
}

/// Pixmap Collection Editor Dialog
PixmapCollectionEditor::PixmapCollectionEditor(PixmapCollection *collection, TQWidget *parent)
: KDialogBase(parent, "pixcollection_dialog", true,
	i18n("Edit Pixmap Collection: %1").tqarg(collection->collectionName()), Close, Close, false)
{
	m_collection = collection;
	TQFrame *frame = makeMainWidget();
	TQHBoxLayout *l = new TQHBoxLayout(frame, 0, 6);
	setInitialSize(TQSize(400, 200), true);

	//// Setup the icon toolbar /////////////////
	TQVBoxLayout *vtqlayout = new TQVBoxLayout(l, 3);
	TQToolButton *newItemPath = new TQToolButton(frame);
	newItemPath->setIconSet(BarIconSet("fileopen"));
	newItemPath->setTextLabel(i18n("&Add File"), true);
	vtqlayout->addWidget(newItemPath);
	m_buttons.insert(BNewItemPath, newItemPath);
	connect(newItemPath, TQT_SIGNAL(clicked()), this, TQT_SLOT(newItemByPath()));

	TQToolButton *newItemName = new TQToolButton(frame);
	newItemName->setIconSet(BarIconSet("icons"));
	newItemName->setTextLabel(i18n("&Add an Icon"), true);
	vtqlayout->addWidget(newItemName);
	m_buttons.insert(BNewItemName, newItemName);
	connect(newItemName, TQT_SIGNAL(clicked()), this, TQT_SLOT(newItemByName()));

	TQToolButton *delItem = new TQToolButton(frame);
	delItem->setIconSet(BarIconSet("edit_remove"));
	delItem->setTextLabel(i18n("&Remove Selected Item"), true);
	vtqlayout->addWidget(delItem);
	m_buttons.insert(BDelItem, delItem);
	connect(delItem, TQT_SIGNAL(clicked()), this, TQT_SLOT(removeItem()));
	vtqlayout->addStretch();

	// Setup the iconView
	m_iconView = new KIconView(frame, "pixcollection_iconView");
	m_iconView->resize(100,100);
	m_iconView->setArrangement(TQIconView::LeftToRight);
	m_iconView->setAutoArrange(true);
	m_iconView->setMode(KIconView::Select);
	l->addWidget(m_iconView);
	connect(m_iconView, TQT_SIGNAL(contextMenuRequested(TQIconViewItem*, const TQPoint&)), this, TQT_SLOT(displayMenu(TQIconViewItem*, const TQPoint&)));
	connect(m_iconView, TQT_SIGNAL(itemRenamed(TQIconViewItem*, const TQString &)), this, TQT_SLOT(renameCollectionItem(TQIconViewItem*, const TQString&)));

	PixmapMap::ConstIterator it;
	PixmapMap::ConstIterator endIt = collection->m_pixmaps.end();
	for(it = collection->m_pixmaps.constBegin(); it != endIt; ++it)
		createIconViewItem(it.key());
}

void
PixmapCollectionEditor::newItemByName()
{
	LoadIconDialog d(parentWidget());
	if(d.exec()== TQDialog::Accepted)
	{
		if(d.iconName().isEmpty())
			return;

		TQString name = m_collection->addPixmapName(d.iconName(), d.iconSize());
		createIconViewItem(name);
	}
}

void
PixmapCollectionEditor::newItemByPath()
{
	KURL url = KFileDialog::getImageOpenURL("::kexi", parentWidget());
	if(url.isEmpty())
		return;
	TQString name = m_collection->addPixmapPath(url);
	createIconViewItem(name);
}

void
PixmapCollectionEditor::removeItem()
{
	TQIconViewItem *item = m_iconView->currentItem();
	if( !item )
	  return;

	int confirm = KMessageBox::questionYesNo(parentWidget(), TQString("<qt>")+
		i18n("Do you want to remove item \"%1\" from collection \"%2\"?")
		.tqarg(item->text()).tqarg(m_collection->collectionName()) + "</qt>");
	if(confirm == KMessageBox::No)
		return;

	m_collection->removePixmap(item->text());
	delete item;
}

void
PixmapCollectionEditor::renameItem()
{
        if(m_iconView->currentItem())
                m_iconView->currentItem()->rename();
}

void
PixmapCollectionEditor::createIconViewItem(const TQString &name)
{
	PixmapIconViewItem *item = new PixmapIconViewItem(m_iconView, name, getPixmap(name));
	item->setRenameEnabled(true);
}

TQPixmap
PixmapCollectionEditor::getPixmap(const TQString &name)
{
	TQPixmap pixmap = m_collection->getPixmap(name);
	if((pixmap.width() <= 48) && (pixmap.height() <= 48))
		return pixmap;

	KPixmapIO io;
	TQImage image = io.convertToImage(pixmap);
	pixmap = io.convertToPixmap(image.scale(48, 48, TQ_ScaleMin));
	return pixmap;
}

void
PixmapCollectionEditor::renameCollectionItem(TQIconViewItem *it, const TQString &name)
{
	PixmapIconViewItem *item = static_cast<PixmapIconViewItem*>(it);
	if(!m_collection->m_pixmaps.contains(item->name()))
		return;

	// We just rename the collection item
	TQPair<TQString, int> pair = m_collection->m_pixmaps[item->name()];
	m_collection->m_pixmaps.remove(item->name());
	m_collection->m_pixmaps[name] = pair;
	item->setName(name);
}

void
PixmapCollectionEditor::displayMenu(TQIconViewItem *it, const TQPoint &p)
{
	if(!it) return;
	KPopupMenu *menu = new KPopupMenu();
	menu->insertItem(SmallIconSet("edit"), i18n("Rename Item"), this, TQT_SLOT(renameItem()));
	menu->insertItem(SmallIconSet("remove"), i18n("Remove Item"), this, TQT_SLOT(removeItem()));
	menu->exec(p);
}

//// A Dialog to choose a pixmap from the PixmapCollection
PixmapCollectionChooser::PixmapCollectionChooser(PixmapCollection *collection, const TQString &selectedItem, TQWidget *parent)
: KDialogBase(parent, "pixchoose_dialog", true, i18n("Select Pixmap From %1").tqarg(collection->collectionName()),
   User1|Ok|Cancel, Ok, false, KGuiItem(i18n("Edit Collection...")))
{
	m_collection = collection;
	setInitialSize(TQSize(400, 200), true);

	m_iconView = new KIconView(this, "pixchooser_iconView");
	setMainWidget(m_iconView);
	m_iconView->setArrangement(TQIconView::LeftToRight);
	m_iconView->setAutoArrange(true);
	m_iconView->setMode(KIconView::Select);

	PixmapMap::ConstIterator it;
	PixmapMap::ConstIterator endIt = collection->m_pixmaps.constEnd();
	for(it = collection->m_pixmaps.constBegin(); it != endIt; ++it)
		new PixmapIconViewItem(m_iconView, it.key(), getPixmap(it.key()));

	TQIconViewItem *item = m_iconView->findItem(selectedItem, TQt::ExactMatch);
	if(item && !selectedItem.isEmpty())
		m_iconView->setCurrentItem(item);
}

TQPixmap
PixmapCollectionChooser::pixmap()
{
        if(! m_iconView->currentItem())
                return TQPixmap();
	TQString name = m_iconView->currentItem()->text();
	return m_collection->getPixmap(name);
}

TQString
PixmapCollectionChooser::pixmapName()
{
	return m_iconView->currentItem() ? m_iconView->currentItem()->text() : TQString("");
}

TQPixmap
PixmapCollectionChooser::getPixmap(const TQString &name)
{
	TQPixmap pixmap = m_collection->getPixmap(name);
	if((pixmap.width() <= 48) && (pixmap.height() <= 48))
		return pixmap;

	// We scale the pixmap down to 48x48 to fit in the iconView
	KPixmapIO io;
	TQImage image = io.convertToImage(pixmap);
	pixmap = io.convertToPixmap(image.scale(48, 48, TQ_ScaleMin));
	return pixmap;
}

void
PixmapCollectionChooser::slotUser1()
{
	PixmapCollectionEditor dialog(m_collection, parentWidget());
	dialog.exec();

	m_iconView->clear();
	PixmapMap::ConstIterator it;
	PixmapMap::ConstIterator endIt = m_collection->m_pixmaps.constEnd();
	for(it = m_collection->m_pixmaps.constBegin(); it != endIt; ++it)
		 new PixmapIconViewItem(m_iconView, it.key(), getPixmap(it.key()));
}

#include "pixmapcollection.moc"