• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

digikam

albumselectdialog.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of digiKam project
00004  * http://www.digikam.org
00005  *
00006  * Date        : 2005-06-16
00007  * Description : a dialog to select a target album to download
00008  *               pictures from camera
00009  *
00010  * Copyright (C) 2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
00011  * Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
00012  *
00013  * This program is free software; you can redistribute it
00014  * and/or modify it under the terms of the GNU General
00015  * Public License as published by the Free Software Foundation;
00016  * either version 2, or (at your option)
00017  * any later version.
00018  *
00019  * This program is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * ============================================================ */
00025 
00026 // Qt includes.
00027 
00028 #include <QLabel>
00029 #include <QFrame>
00030 #include <QLayout>
00031 #include <QCursor>
00032 #include <QGridLayout>
00033 #include <QPixmap>
00034 #include <QTreeWidget>
00035 #include <QTreeWidgetItemIterator>
00036 
00037 // KDE includes.
00038 
00039 #include <kdebug.h>
00040 #include <kmenu.h>
00041 #include <klocale.h>
00042 #include <kapplication.h>
00043 #include <kaction.h>
00044 #include <kinputdialog.h>
00045 #include <kmessagebox.h>
00046 #include <kstandarddirs.h>
00047 
00048 // Local includes.
00049 
00050 #include "treefolderitem.h"
00051 #include "album.h"
00052 #include "albummanager.h"
00053 #include "albumthumbnailloader.h"
00054 #include "collectionmanager.h"
00055 #include "searchtextbar.h"
00056 #include "albumselectdialog.h"
00057 #include "albumselectdialog.moc"
00058 
00059 namespace Digikam
00060 {
00061 
00062 class AlbumSelectDialogPrivate
00063 {
00064 
00065 public:
00066 
00067     AlbumSelectDialogPrivate()
00068     {
00069         allowRootSelection = false;
00070         folderView         = 0;
00071         searchBar          = 0;
00072     }
00073 
00074     bool           allowRootSelection;
00075 
00076     QString        newAlbumString;
00077 
00078     QTreeWidget   *folderView;
00079 
00080     SearchTextBar *searchBar;
00081 };
00082 
00083 AlbumSelectDialog::AlbumSelectDialog(QWidget* parent, PAlbum* albumToSelect,
00084                                      const QString& header,
00085                                      const QString& newAlbumString,
00086                                      bool allowRootSelection)
00087                  : KDialog(parent)
00088 {
00089     d = new AlbumSelectDialogPrivate;
00090     d->allowRootSelection = allowRootSelection;
00091     d->newAlbumString     = newAlbumString;
00092 
00093     setCaption(i18n("Select Album"));
00094     setButtons(Help|User1|Ok|Cancel);
00095     setButtonText(User1, i18n("&New Album"));
00096     setButtonIcon(User1, KIcon("albumfolder-new"));
00097     setDefaultButton(Ok);
00098     setHelp("targetalbumdialog.anchor", "digikam");
00099     enableButtonOk(false);
00100 
00101     // -------------------------------------------------------------
00102 
00103     QWidget *page = new QWidget(this);
00104     setMainWidget(page);
00105 
00106     QGridLayout* grid       = new QGridLayout(page);
00107     QLabel *logo            = new QLabel(page);
00108     logo->setPixmap(QPixmap(KStandardDirs::locate("data", "digikam/data/logo-digikam.png"))
00109                             .scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation));
00110 
00111     QLabel *message = new QLabel(page);
00112     message->setWordWrap(true);
00113     if (!header.isEmpty())
00114         message->setText(header);
00115 
00116     d->folderView = new QTreeWidget(page);
00117     d->folderView->setHeaderLabels(QStringList() << i18n("My Albums"));
00118     d->folderView->setContextMenuPolicy(Qt::CustomContextMenu);
00119 
00120     d->searchBar = new SearchTextBar(page, "AlbumSelectDialogSearchBar");
00121 
00122     grid->addWidget(logo,          0, 0, 1, 1);
00123     grid->addWidget(message,       1, 0, 1, 1);
00124     grid->addWidget(d->folderView, 0, 1, 3, 1);
00125     grid->addWidget(d->searchBar,  3, 1, 1, 1);
00126     grid->setColumnStretch(1, 10);
00127     grid->setRowStretch(2, 10);
00128     grid->setMargin(0);
00129     grid->setSpacing(KDialog::spacingHint());
00130 
00131     // -------------------------------------------------------------
00132 
00133     populateTreeView(albumToSelect);
00134 
00135     // -------------------------------------------------------------
00136 
00137     connect(AlbumManager::instance(), SIGNAL(signalAlbumAdded(Album*)),
00138             this, SLOT(slotAlbumAdded(Album*)));
00139 
00140     connect(AlbumManager::instance(), SIGNAL(signalAlbumDeleted(Album*)),
00141             this, SLOT(slotAlbumDeleted(Album*)));
00142 
00143     connect(AlbumManager::instance(), SIGNAL(signalAlbumsCleared()),
00144             this, SLOT(slotAlbumsCleared()));
00145 
00146     connect(d->folderView, SIGNAL(itemSelectionChanged()),
00147             this, SLOT(slotSelectionChanged()));
00148 
00149     connect(d->folderView, SIGNAL(customContextMenuRequested(const QPoint&)),
00150             this, SLOT(slotContextMenu()));
00151 
00152     connect(this, SIGNAL(user1Clicked()),
00153             this, SLOT(slotUser1()));
00154 
00155     connect(d->searchBar, SIGNAL(textChanged(const QString&)),
00156             this, SLOT(slotSearchTextChanged(const QString&)));
00157 
00158     // -------------------------------------------------------------
00159 
00160     resize(500, 500);
00161     slotSelectionChanged();
00162 }
00163 
00164 AlbumSelectDialog::~AlbumSelectDialog()
00165 {
00166     delete d;
00167 }
00168 
00169 void AlbumSelectDialog::populateTreeView(PAlbum *albumToSelect)
00170 {
00171     AlbumList aList = AlbumManager::instance()->allPAlbums();
00172 
00173     for (AlbumList::const_iterator it = aList.begin(); it != aList.end(); ++it)
00174     {
00175         Album *album        = *it;
00176         TreeAlbumItem *item = 0;
00177 
00178         if (album->isRoot())
00179         {
00180             item = new TreeAlbumItem(d->folderView, album);
00181             item->setExpanded(true);
00182         }
00183         else
00184         {
00185             TreeAlbumItem* pitem = (TreeAlbumItem*)(album->parent()->extraData(d->folderView));
00186             if (!pitem)
00187             {
00188                 kWarning(50003) << "Failed to find parent for Album " << album->title() << endl;
00189                 continue;
00190             }
00191 
00192             item = new TreeAlbumItem(pitem, album);
00193         }
00194 
00195         if (item)
00196         {
00197             PAlbum* palbum = dynamic_cast<PAlbum*>(album);
00198             if (palbum)
00199                 item->setIcon(0, AlbumThumbnailLoader::instance()->getStandardAlbumIcon(palbum));
00200 
00201             if (album == albumToSelect)
00202             {
00203                 item->setExpanded(true);
00204                 d->folderView->setCurrentItem(item);
00205                 d->folderView->scrollToItem(item);
00206             }
00207         }
00208     }
00209 }
00210 
00211 void AlbumSelectDialog::slotAlbumAdded(Album* album)
00212 {
00213     if (!album || album->type() != Album::PHYSICAL)
00214         return;
00215 
00216     TreeAlbumItem* parentItem = (TreeAlbumItem*)(album->parent()->extraData(d->folderView));
00217 
00218     if (!parentItem)
00219     {
00220         kWarning(50003) << "Failed to find parent for Album "
00221                         << album->title() << endl;
00222         return;
00223     }
00224 
00225     TreeAlbumItem* item = new TreeAlbumItem(parentItem, album);
00226     PAlbum* palbum      = dynamic_cast<PAlbum*>(album);
00227     if (palbum)
00228         item->setIcon(0, AlbumThumbnailLoader::instance()->getStandardAlbumIcon(palbum));
00229 }
00230 
00231 void AlbumSelectDialog::slotAlbumDeleted(Album* album)
00232 {
00233     if (!album || album->type() != Album::PHYSICAL)
00234         return;
00235 
00236     TreeAlbumItem *item = (TreeAlbumItem*)(album->extraData(d->folderView));
00237     if (item)
00238         delete item;
00239 }
00240 
00241 void AlbumSelectDialog::slotAlbumsCleared()
00242 {
00243     for(QTreeWidgetItemIterator it(d->folderView); *it; ++it)
00244     {
00245         Album *album = static_cast<TreeAlbumItem*>(*it)->album();
00246         if (album)
00247             album->removeExtraData(d->folderView);
00248     }
00249     d->folderView->clear();
00250 }
00251 
00252 void AlbumSelectDialog::slotSelectionChanged()
00253 {
00254     QTreeWidgetItem* selItem = d->folderView->currentItem();
00255 
00256     if ((!selItem || (selItem == d->folderView->topLevelItem(0))) &&
00257         !d->allowRootSelection)
00258     {
00259         enableButtonOk(false);
00260         return;
00261     }
00262 
00263     enableButtonOk(true);
00264 }
00265 
00266 void AlbumSelectDialog::slotContextMenu()
00267 {
00268     KMenu popmenu(d->folderView);
00269 
00270     KAction *action = new KAction(KIcon("albumfolder-new"), i18n("Create New Album"), this);
00271     connect(action, SIGNAL(triggered(bool) ),
00272             this, SLOT(slotUser1()));
00273 
00274     popmenu.addAction(action);
00275     popmenu.exec(QCursor::pos());
00276 }
00277 
00278 void AlbumSelectDialog::slotUser1()
00279 {
00280     QTreeWidgetItem* item = d->folderView->currentItem();
00281     if (!item)
00282         item = d->folderView->topLevelItem(0);
00283 
00284     if (!item)
00285         return;
00286 
00287     TreeAlbumItem* viewItem = dynamic_cast<TreeAlbumItem*>(item);
00288     if (!viewItem)
00289         return;
00290 
00291     PAlbum* album = dynamic_cast<PAlbum*>(viewItem->album());
00292     if (!album)
00293         return;
00294 
00295     bool ok;
00296     QString newAlbumName = KInputDialog::getText(i18n("New Album Name"),
00297                                                  i18n("Creating new album in '%1'\n"
00298                                                       "Enter album name:",
00299                                                       album->prettyUrl()),
00300                                                  d->newAlbumString, &ok, this);
00301     if (!ok)
00302         return;
00303 
00304     PAlbum *newAlbum;
00305     QString errMsg;
00306     if (album->isRoot())
00307     {
00308         // if we create an album under root, need to supply the album root path.
00309         // TODO: Let user choose an album root
00310         newAlbum = AlbumManager::instance()->createPAlbum(CollectionManager::instance()->oneAlbumRootPath(),
00311                                                           newAlbumName, QString(), QDate::currentDate(), QString(), errMsg);
00312     }
00313     else
00314     {
00315         newAlbum = AlbumManager::instance()->createPAlbum(album, newAlbumName, QString(),
00316                                                           QDate::currentDate(), QString(), errMsg);
00317     }
00318 
00319     if (!newAlbum)
00320     {
00321         KMessageBox::error(this, errMsg);
00322         return;
00323     }
00324 
00325     TreeAlbumItem* newItem = (TreeAlbumItem*)newAlbum->extraData(d->folderView);
00326     if (newItem)
00327     {
00328         d->folderView->scrollToItem(newItem);
00329         newItem->setSelected(true);
00330     }
00331 }
00332 
00333 PAlbum* AlbumSelectDialog::selectAlbum(QWidget* parent,
00334                                        PAlbum* albumToSelect,
00335                                        const QString& header,
00336                                        const QString& newAlbumString,
00337                                        bool allowRootSelection )
00338 {
00339     AlbumSelectDialog dlg(parent, albumToSelect,
00340                           header, newAlbumString,
00341                           allowRootSelection);
00342 
00343     if (dlg.exec() != KDialog::Accepted)
00344         return 0;
00345 
00346     TreeAlbumItem* item = (TreeAlbumItem*) dlg.d->folderView->currentItem();
00347     if ((!item || (item == dlg.d->folderView->topLevelItem(0))) &&
00348         !allowRootSelection)
00349     {
00350         return 0;
00351     }
00352 
00353     return (dynamic_cast<PAlbum*>(item->album()));
00354 }
00355 
00356 void AlbumSelectDialog::slotSearchTextChanged(const QString& filter)
00357 {
00358     QString search = filter.toLower();
00359 
00360     bool atleastOneMatch = false;
00361 
00362     AlbumList pList = AlbumManager::instance()->allPAlbums();
00363     for (AlbumList::iterator it = pList.begin(); it != pList.end(); ++it)
00364     {
00365         PAlbum* palbum  = (PAlbum*)(*it);
00366 
00367         // don't touch the root Album
00368         if (palbum->isRoot())
00369             continue;
00370 
00371         bool match = palbum->title().toLower().contains(search);
00372         if (!match)
00373         {
00374             // check if any of the parents match the search
00375             Album* parent = palbum->parent();
00376             while (parent && !parent->isRoot())
00377             {
00378                 if (parent->title().toLower().contains(search))
00379                 {
00380                     match = true;
00381                     break;
00382                 }
00383 
00384                 parent = parent->parent();
00385             }
00386         }
00387 
00388         if (!match)
00389         {
00390             // check if any of the children match the search
00391             AlbumIterator it(palbum);
00392             while (it.current())
00393             {
00394                 if ((*it)->title().toLower().contains(search))
00395                 {
00396                     match = true;
00397                     break;
00398                 }
00399                 ++it;
00400             }
00401         }
00402 
00403         TreeAlbumCheckListItem* viewItem = (TreeAlbumCheckListItem*) palbum->extraData(d->folderView);
00404 
00405         if (match)
00406         {
00407             atleastOneMatch = true;
00408 
00409             if (viewItem)
00410                 viewItem->setHidden(false);
00411         }
00412         else
00413         {
00414             if (viewItem)
00415             {
00416                 viewItem->setHidden(true);
00417             }
00418         }
00419     }
00420 
00421     d->searchBar->slotSearchResult(atleastOneMatch);
00422 }
00423 
00424 }  // namespace Digikam

digikam

Skip menu "digikam"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • digikam
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal