imagecollectionselector.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  * File   : imagecollectionselector.cpp
00003  * Authors: KIPI team developers (see AUTHORS files for details)
00004  *          
00005  * Date   : 2004-07
00006  * Description :
00007  *
00008  * Copyright 2004 by the KIPI team
00009  *
00010  * This program is free software; you can redistribute it
00011  * and/or modify it under the terms of the GNU Library General
00012  * Public License as published by the Free Software Foundation;
00013  * either version 2, or (at your option)
00014  * any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Library General Public License for more details.
00020  *
00021  * ============================================================ */
00022 
00023 // Qt includes.
00024 
00025 #include <qheader.h>
00026 #include <qlayout.h>
00027 #include <qpushbutton.h>
00028 #include <qlabel.h>
00029 #include <qvgroupbox.h>
00030 #include <qtimer.h>
00031 
00032 // KDE includes.
00033 
00034 #include <kbuttonbox.h>
00035 #include <kdialog.h>
00036 #include <klistview.h>
00037 #include <klocale.h>
00038 #include <kglobal.h>
00039 #include <kio/previewjob.h>
00040 
00041 // KIPI includes.
00042 
00043 #include "libkipi/interface.h"
00044 
00045 // Local includes.
00046 
00047 #include "imagecollectionselector.h"
00048 
00049 namespace KIPI 
00050 {
00051 
00052 class ImageCollectionItem : public QCheckListItem
00053 {
00054 public:
00055     ImageCollectionItem(ImageCollectionSelector* selector,
00056                         QListView * parent, ImageCollection collection)
00057     : QCheckListItem( parent, collection.name(), QCheckListItem::CheckBox),
00058       _imageCollection(collection), _selector(selector)
00059     {}
00060 
00061     ImageCollection imageCollection() const { return _imageCollection; }
00062 
00063 protected:
00064 
00065     virtual void stateChange(bool val)
00066     {
00067         QCheckListItem::stateChange(val);
00068         _selector->emitSelectionChanged();
00069     }
00070     
00071 private:
00072     
00073     ImageCollection          _imageCollection;
00074     ImageCollectionSelector* _selector;
00075 };
00076 
00077 struct ImageCollectionSelector::Private {
00078     Interface* _interface;
00079     KListView* _list;
00080     QLabel*    _thumbLabel;
00081     QLabel*    _textLabel;
00082     QListViewItem* _itemToSelect;
00083 };
00084 
00085 
00086 ImageCollectionSelector::ImageCollectionSelector(QWidget* parent, Interface* interface, const char* name)
00087                        : QWidget(parent, name)
00088 {
00089     d=new Private;
00090     d->_interface=interface;
00091     d->_itemToSelect = 0;
00092     
00093     d->_list=new KListView(this);
00094     d->_list->setResizeMode( QListView::LastColumn );
00095     d->_list->addColumn("");
00096     d->_list->header()->hide();
00097 
00098     connect(d->_list, SIGNAL(selectionChanged(QListViewItem*)),
00099             SLOT(slotSelectionChanged(QListViewItem*)));
00100     
00101     QHBoxLayout* mainLayout=new QHBoxLayout(this, 0, KDialog::spacingHint());
00102     mainLayout->addWidget(d->_list);
00103 
00104     QVBoxLayout* rightLayout = new QVBoxLayout(mainLayout, 0);
00105 
00106     KButtonBox* box=new KButtonBox(this, Vertical);
00107     rightLayout->addWidget(box);
00108     QPushButton* selectAll=box->addButton(i18n("Select All"));
00109     QPushButton* invertSelection=box->addButton(i18n("Invert Selection"));
00110     QPushButton* selectNone=box->addButton(i18n("Select None"));
00111     box->layout();
00112 
00113     connect(selectAll, SIGNAL(clicked()),
00114             this, SLOT(slotSelectAll()) );
00115     connect(invertSelection, SIGNAL(clicked()),
00116             this, SLOT(slotInvertSelection()) );
00117     connect(selectNone, SIGNAL(clicked()), 
00118             this, SLOT(slotSelectNone()) );
00119 
00120     rightLayout->addItem(new QSpacerItem(10,20,QSizePolicy::Fixed,
00121                                          QSizePolicy::Expanding));
00122     
00123     QVGroupBox* rightBox = new QVGroupBox(this);
00124     rightBox->setInsideMargin(KDialog::marginHint());
00125     rightBox->setInsideSpacing(KDialog::spacingHint());
00126     rightLayout->addWidget(rightBox);
00127 
00128     if (interface->hasFeature(AlbumsUseFirstImagePreview))
00129     {
00130         d->_thumbLabel = new QLabel(rightBox);
00131         d->_thumbLabel->setFixedSize(QSize(128,128));
00132         d->_thumbLabel->setAlignment(AlignHCenter | AlignVCenter);
00133     }
00134     else
00135     {
00136         d->_thumbLabel = 0;
00137     }
00138     d->_textLabel = new QLabel(rightBox);
00139     
00140     fillList();
00141     QTimer::singleShot(0, this, SLOT(slotInitialShow()));
00142 }
00143 
00144 
00145 ImageCollectionSelector::~ImageCollectionSelector() {
00146     delete d;
00147 }
00148 
00149 
00150 void ImageCollectionSelector::fillList() {
00151     QValueList<ImageCollection> collections = d->_interface->allAlbums();
00152     d->_list->clear();
00153     ImageCollection current = d->_interface->currentAlbum();
00154     bool currentWasInList = false;
00155 
00156     /* note: the extensive use of blocksignals is to prevent bombarding
00157        the plugin with too many selection changed signals. do not remove
00158        them */
00159     
00160     blockSignals(true);
00161     for( QValueList<ImageCollection>::Iterator it = collections.begin() ;
00162          it != collections.end() ; ++it )
00163     {
00164         ImageCollectionItem* item = new ImageCollectionItem( this, d->_list, *it);
00165         if (!currentWasInList && *it == current) {
00166             item->setOn(true);
00167             currentWasInList = true;
00168             if (!d->_itemToSelect)
00169                 d->_itemToSelect = item;
00170         }
00171     }
00172 
00173     if (!currentWasInList) {
00174         slotSelectAll();
00175         d->_itemToSelect = d->_list->firstChild();
00176     }
00177     blockSignals(false);
00178 }
00179 
00180 void ImageCollectionSelector::emitSelectionChanged()
00181 {
00182     emit selectionChanged();
00183 }
00184 
00185 QValueList<ImageCollection> ImageCollectionSelector::selectedImageCollections() const {
00186     QValueList<ImageCollection> list;
00187 
00188     QListViewItemIterator it( d->_list );
00189 
00190     for (; it.current(); ++it) {
00191         ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
00192 
00193         if (item->isOn()) {
00194             list << item->imageCollection();
00195         }
00196     }
00197 
00198     return list;
00199 }
00200 
00201 void ImageCollectionSelector::slotSelectAll() {
00202     QListViewItemIterator it( d->_list );
00203 
00204     /* note: the extensive use of blocksignals is to prevent bombarding
00205        the plugin with too many selection changed signals. do not remove
00206        them */
00207     blockSignals(true);
00208     for (; it.current(); ++it) {
00209         ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
00210         item->setOn(true);
00211     }
00212     blockSignals(false);
00213     
00214     emit selectionChanged();
00215 }
00216 
00217 
00218 void ImageCollectionSelector::slotInvertSelection() {
00219     QListViewItemIterator it( d->_list );
00220 
00221     /* note: the extensive use of blocksignals is to prevent bombarding
00222        the plugin with too many selection changed signals. do not remove
00223        them */
00224     blockSignals(true);
00225     for (; it.current(); ++it) {
00226         ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
00227         item->setOn(!item->isOn());
00228     }
00229     blockSignals(false);
00230 
00231     emit selectionChanged();
00232 }
00233 
00234 
00235 void ImageCollectionSelector::slotSelectNone() {
00236     QListViewItemIterator it( d->_list );
00237 
00238     /* note: the extensive use of blocksignals is to prevent bombarding
00239        the plugin with too many selection changed signals. do not remove
00240        them */
00241     blockSignals(true);
00242     for (; it.current(); ++it) {
00243         ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
00244         item->setOn(false);
00245     }
00246     blockSignals(false);
00247     
00248     emit selectionChanged();
00249 }
00250 
00251 void ImageCollectionSelector::slotSelectionChanged(QListViewItem* listItem)
00252 {
00253     if (d->_thumbLabel)
00254         d->_thumbLabel->clear();
00255     d->_textLabel->clear();
00256 
00257     if (!listItem)
00258         return;
00259 
00260     ImageCollectionItem* imcollItem =
00261         static_cast<ImageCollectionItem*>(listItem);
00262 
00263     if (d->_thumbLabel)
00264     {
00265         KURL::List images(imcollItem->imageCollection().images());
00266         if (!images.isEmpty())
00267         {
00268             KIO::PreviewJob* thumbJob = KIO::filePreview(images.first(), 128);
00269             connect( thumbJob, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
00270                      SLOT(slotGotPreview(const KFileItem* , const QPixmap&)));
00271         }
00272     }
00273     
00274     // Layout the ImageCollection information nicely
00275     
00276     QString cellBeg("<tr><td><nobr><font size=-1><i>");
00277     QString cellMid("</i></font></nobr></td><td><font size=-1>");
00278     QString cellEnd("</font></td></tr>");
00279 
00280     QString text("<table cellspacing=0 cellpadding=0>");
00281 
00282     // number of images 
00283     text += cellBeg + i18n("Images:") +
00284             cellMid + QString::number(imcollItem->imageCollection().images().count()) +
00285             cellEnd;
00286 
00287     // Optional features -------------------------------------------------------
00288     
00289     // Album Comments
00290     if (d->_interface->hasFeature(AlbumsHaveComments))
00291     {
00292         // Limit the comments string to 20 char...
00293         QString comments = imcollItem->imageCollection().comment();
00294         if (!comments.isEmpty())
00295         {
00296         comments.truncate(20);
00297         comments.append("...");
00298         }
00299         
00300         text += cellBeg + i18n("Comments:") +
00301                 cellMid + comments +
00302                 cellEnd;
00303     }
00304 
00305     // Album Category
00306     if (d->_interface->hasFeature(AlbumsHaveCategory))
00307     {
00308         text += cellBeg + i18n("Category:") +
00309                 cellMid + imcollItem->imageCollection().category() +
00310                 cellEnd;
00311     }
00312 
00313     // Album Creation Date
00314     if (d->_interface->hasFeature(AlbumsHaveCreationDate))
00315     {
00316         QDate date(imcollItem->imageCollection().date());
00317         text += cellBeg + i18n("Date:") +
00318                 cellMid + KGlobal::locale()->formatDate(date) +
00319                 cellEnd;
00320     }
00321 
00322     
00323     text += "</table>";
00324 
00325     d->_textLabel->setText(text);
00326     
00327     emit selectionChanged();
00328 }
00329 
00330 void ImageCollectionSelector::slotGotPreview(const KFileItem*, const QPixmap& pix)
00331 {
00332     d->_thumbLabel->setPixmap(pix);
00333 }
00334 
00335 void ImageCollectionSelector::slotInitialShow()
00336 {
00337     if (d->_itemToSelect)
00338     {
00339         d->_list->setSelected(d->_itemToSelect, true);
00340         d->_list->ensureItemVisible(d->_itemToSelect);
00341         d->_itemToSelect = 0;
00342     }
00343     emit selectionChanged();
00344 }
00345 
00346 } // KIPI
00347 
00348 #include "imagecollectionselector.moc"

Generated on Tue Sep 25 22:34:22 2007 for libKipi by  doxygen 1.5.3