libKipi
imagecollectionselector.cpp
Go to the documentation of this file.
1/* ============================================================
2 *
3 * This file is a part of kipi-plugins project
4 * http://www.kipi-plugins.org
5 *
6 * Date : 2004-07-01
7 * Description : image collection selector
8 *
9 * Copyright (C) 2004-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
10 * Copyright (C) 2004-2005 by Renchi Raju <renchi.raju at kdemail.net>
11 * Copyright (C) 2004-2005 by Jesper K. Pedersen <blackie at kde.org>
12 * Copyright (C) 2004-2005 by Aurelien Gateau <aurelien dot gateau at free.fr>
13 *
14 * This program is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General
16 * Public License as published by the Free Software Foundation;
17 * either version 2, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * ============================================================ */
25
26// Qt includes.
27
28#include <qheader.h>
29#include <qlayout.h>
30#include <qpushbutton.h>
31#include <qlabel.h>
32#include <qvgroupbox.h>
33#include <qtimer.h>
34
35// KDE includes.
36
37#include <kbuttonbox.h>
38#include <kdialog.h>
39#include <klistview.h>
40#include <klocale.h>
41#include <kglobal.h>
42#include <kio/previewjob.h>
43
44// KIPI includes.
45
46#include "libkipi/interface.h"
47
48// Local includes.
49
51#include "imagecollectionselector.moc"
52
53namespace KIPI
54{
55
56class ImageCollectionItem : public QCheckListItem
57{
58public:
59
61 QListView * parent, ImageCollection collection)
62 : QCheckListItem( parent, collection.name(), QCheckListItem::CheckBox),
63 _imageCollection(collection), _selector(selector)
64 {}
65
66 ImageCollection imageCollection() const { return _imageCollection; }
67
68protected:
69
70 virtual void stateChange(bool val)
71 {
72 QCheckListItem::stateChange(val);
73 _selector->emitSelectionChanged();
74 }
75
76private:
77
78 ImageCollection _imageCollection;
79 ImageCollectionSelector* _selector;
80};
81
90
91ImageCollectionSelector::ImageCollectionSelector(QWidget* parent, Interface* interface, const char* name)
92 : QWidget(parent, name)
93{
94 d=new Private;
95 d->_interface=interface;
96 d->_itemToSelect = 0;
97
98 d->_list=new KListView(this);
99 d->_list->setResizeMode( QListView::LastColumn );
100 d->_list->addColumn("");
101 d->_list->header()->hide();
102
103 connect(d->_list, SIGNAL(selectionChanged(QListViewItem*)),
104 SLOT(slotSelectionChanged(QListViewItem*)));
105
106 QHBoxLayout* mainLayout=new QHBoxLayout(this, 0, KDialog::spacingHint());
107 mainLayout->addWidget(d->_list);
108
109 QVBoxLayout* rightLayout = new QVBoxLayout(mainLayout, 0);
110
111 KButtonBox* box=new KButtonBox(this, Vertical);
112 rightLayout->addWidget(box);
113 QPushButton* selectAll=box->addButton(i18n("Select All"));
114 QPushButton* invertSelection=box->addButton(i18n("Invert Selection"));
115 QPushButton* selectNone=box->addButton(i18n("Select None"));
116 box->layout();
117
118 connect(selectAll, SIGNAL(clicked()),
119 this, SLOT(slotSelectAll()) );
120 connect(invertSelection, SIGNAL(clicked()),
121 this, SLOT(slotInvertSelection()) );
122 connect(selectNone, SIGNAL(clicked()),
123 this, SLOT(slotSelectNone()) );
124
125 rightLayout->addItem(new QSpacerItem(10,20,QSizePolicy::Fixed,
126 QSizePolicy::Expanding));
127
128 QVGroupBox* rightBox = new QVGroupBox(this);
129 rightBox->setInsideMargin(KDialog::marginHint());
130 rightBox->setInsideSpacing(KDialog::spacingHint());
131 rightLayout->addWidget(rightBox);
132
134 {
135 d->_thumbLabel = new QLabel(rightBox);
136 d->_thumbLabel->setFixedSize(QSize(128,128));
137 d->_thumbLabel->setAlignment(AlignHCenter | AlignVCenter);
138 }
139 else
140 {
141 d->_thumbLabel = 0;
142 }
143 d->_textLabel = new QLabel(rightBox);
144
145 fillList();
146 QTimer::singleShot(0, this, SLOT(slotInitialShow()));
147}
148
149
154
155
156void ImageCollectionSelector::fillList()
157{
158 QValueList<ImageCollection> collections = d->_interface->allAlbums();
159 d->_list->clear();
161 bool currentWasInList = false;
162
163 /* note: the extensive use of blocksignals is to prevent bombarding
164 the plugin with too many selection changed signals. do not remove
165 them */
166
167 blockSignals(true);
168 for( QValueList<ImageCollection>::Iterator it = collections.begin() ;
169 it != collections.end() ; ++it )
170 {
171 ImageCollectionItem* item = new ImageCollectionItem( this, d->_list, *it);
172 if (!currentWasInList && *it == current)
173 {
174 item->setOn(true);
175 currentWasInList = true;
176 if (!d->_itemToSelect)
177 d->_itemToSelect = item;
178 }
179 }
180
181 if (!currentWasInList)
182 {
183 slotSelectAll();
184 d->_itemToSelect = d->_list->firstChild();
185 }
186 blockSignals(false);
187}
188
189void ImageCollectionSelector::emitSelectionChanged()
190{
191 emit selectionChanged();
192}
193
194QValueList<ImageCollection> ImageCollectionSelector::selectedImageCollections() const
195{
196 QValueList<ImageCollection> list;
197
198 QListViewItemIterator it( d->_list );
199
200 for (; it.current(); ++it)
201 {
202 ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
203
204 if (item->isOn())
205 {
206 list << item->imageCollection();
207 }
208 }
209
210 return list;
211}
212
213void ImageCollectionSelector::slotSelectAll()
214{
215 QListViewItemIterator it( d->_list );
216
217 /* note: the extensive use of blocksignals is to prevent bombarding
218 the plugin with too many selection changed signals. do not remove
219 them */
220 blockSignals(true);
221 for (; it.current(); ++it)
222 {
223 ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
224 item->setOn(true);
225 }
226 blockSignals(false);
227
228 emit selectionChanged();
229}
230
231
232void ImageCollectionSelector::slotInvertSelection()
233{
234 QListViewItemIterator it( d->_list );
235
236 /* note: the extensive use of blocksignals is to prevent bombarding
237 the plugin with too many selection changed signals. do not remove
238 them */
239 blockSignals(true);
240 for (; it.current(); ++it)
241 {
242 ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
243 item->setOn(!item->isOn());
244 }
245 blockSignals(false);
246
247 emit selectionChanged();
248}
249
250
251void ImageCollectionSelector::slotSelectNone()
252{
253 QListViewItemIterator it( d->_list );
254
255 /* note: the extensive use of blocksignals is to prevent bombarding
256 the plugin with too many selection changed signals. do not remove
257 them */
258 blockSignals(true);
259 for (; it.current(); ++it)
260 {
261 ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
262 item->setOn(false);
263 }
264 blockSignals(false);
265
266 emit selectionChanged();
267}
268
269void ImageCollectionSelector::slotSelectionChanged(QListViewItem* listItem)
270{
271 if (d->_thumbLabel)
272 d->_thumbLabel->clear();
273 d->_textLabel->clear();
274
275 if (!listItem)
276 return;
277
278 ImageCollectionItem* imcollItem = static_cast<ImageCollectionItem*>(listItem);
279
280 if (d->_thumbLabel)
281 {
282 KURL::List images(imcollItem->imageCollection().images());
283 if (!images.isEmpty())
284 {
285 KIO::PreviewJob* thumbJob = KIO::filePreview(images.first(), 128);
286 connect( thumbJob, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
287 SLOT(slotGotPreview(const KFileItem* , const QPixmap&)));
288 }
289 }
290
291 // Layout the ImageCollection information nicely
292
293 QString cellBeg("<tr><td><nobr><font size=-1><i>");
294 QString cellMid("</i></font></nobr></td><td><font size=-1>");
295 QString cellEnd("</font></td></tr>");
296
297 QString text("<table cellspacing=0 cellpadding=0>");
298
299 // number of images
300 text += cellBeg + i18n("Images:") +
301 cellMid + QString::number(imcollItem->imageCollection().images().count()) +
302 cellEnd;
303
304 // Optional features -------------------------------------------------------
305
306 // Album Comments
308 {
309 // Limit the comments string to 20 char...
310 QString comments = imcollItem->imageCollection().comment();
311 if (!comments.isEmpty())
312 {
313 comments.truncate(20);
314 comments.append("...");
315 }
316
317 text += cellBeg + i18n("Caption:") +
318 cellMid + comments +
319 cellEnd;
320 }
321
322 // Album Category
324 {
325 text += cellBeg + i18n("Category:") +
326 cellMid + imcollItem->imageCollection().category() +
327 cellEnd;
328 }
329
330 // Album Creation Date
332 {
333 QDate date(imcollItem->imageCollection().date());
334 text += cellBeg + i18n("Date:") +
335 cellMid + KGlobal::locale()->formatDate(date) +
336 cellEnd;
337 }
338
339 text += "</table>";
340
341 d->_textLabel->setText(text);
342
343 emit selectionChanged();
344}
345
346void ImageCollectionSelector::slotGotPreview(const KFileItem*, const QPixmap& pix)
347{
348 d->_thumbLabel->setPixmap(pix);
349}
350
351void ImageCollectionSelector::slotInitialShow()
352{
353 if (d->_itemToSelect)
354 {
355 d->_list->setSelected(d->_itemToSelect, true);
356 d->_list->ensureItemVisible(d->_itemToSelect);
357 d->_itemToSelect = 0;
358 }
359 emit selectionChanged();
360}
361
362} // KIPI
363
Definition imagecollectionselector.cpp:57
ImageCollection imageCollection() const
Definition imagecollectionselector.cpp:66
ImageCollectionItem(ImageCollectionSelector *selector, QListView *parent, ImageCollection collection)
Definition imagecollectionselector.cpp:60
virtual void stateChange(bool val)
Definition imagecollectionselector.cpp:70
Definition imagecollectionselector.h:49
ImageCollectionSelector(QWidget *parent, Interface *, const char *name=0)
Definition imagecollectionselector.cpp:91
friend class ImageCollectionItem
Definition imagecollectionselector.h:80
QValueList< ImageCollection > selectedImageCollections() const
Definition imagecollectionselector.cpp:194
~ImageCollectionSelector()
Definition imagecollectionselector.cpp:150
Definition imagecollection.h:53
Definition interface.h:64
virtual ImageCollection currentAlbum()=0
Definition interface.cpp:184
virtual QValueList< ImageCollection > allAlbums()=0
Definition interface.cpp:204
bool hasFeature(KIPI::Features feature)
Definition interface.cpp:129
Definition batchprogressdialog.cpp:70
@ AlbumsHaveCategory
Definition interface.h:56
@ AlbumsUseFirstImagePreview
Definition interface.h:58
@ AlbumsHaveComments
Definition interface.h:50
@ AlbumsHaveCreationDate
Definition interface.h:57
Definition imagecollectionselector.cpp:83
QLabel * _thumbLabel
Definition imagecollectionselector.cpp:86
Interface * _interface
Definition imagecollectionselector.cpp:84
QLabel * _textLabel
Definition imagecollectionselector.cpp:87
QListViewItem * _itemToSelect
Definition imagecollectionselector.cpp:88
KListView * _list
Definition imagecollectionselector.cpp:85