subalbumsWidget.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it
00005 //  and/or modify it under the terms of the GNU General
00006 //  Public License as published by the Free Software
00007 //  Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //==============================================
00010 
00011 //Systemwide includes
00012 #include <qwidget.h>
00013 #include <qlayout.h>
00014 #include <qlabel.h>
00015 #include <qfont.h>
00016 #include <qpixmap.h>
00017 #include <qapplication.h>
00018 #include <qtoolbutton.h>
00019 #include <qtooltip.h>
00020 
00021 //Projectwide includes
00022 #include "subalbumsWidget.h"
00023 #include "subalbumPreviewWidget.h"
00024 #include "subalbumsIconView.h"
00025 #include "layoutWidget.h"
00026 #include "window.h"
00027 #include "titleWidget.h"
00028 #include "dialogs/questionDialog.h"
00029 #include "../config.h"
00030 #include "../backend/album.h"
00031 #include "../backend/subalbum.h"
00032 #include "../backend/tools/guiTools.h"
00033 #include "../configuration/configuration.h"
00034 
00035 //==============================================
00036 SubalbumsWidget::SubalbumsWidget(QWidget *parent,
00037                                  const char* name ) :
00038                                  QWidget(parent,name)
00039 {
00040   //set layout pointer
00041   layout = (LayoutWidget*)parent;
00042 
00043   //create "Collections:" header
00044   QLabel* collectionsHeader = new QLabel( this );
00045   collectionsHeader->setText( tr("Collections:") );
00046   QFont labelFont = collectionsHeader->font();
00047   labelFont.setWeight(QFont::Bold);
00048   collectionsHeader->setFont( labelFont );
00049   //--------------------------------------
00050   //create collections list
00051   collections = new SubalbumsIconView( this );
00052 
00053   //only one item can be selected at a time
00054   collections->setSelectionMode( QIconView::Single ) ;
00055 
00056   //single column of items
00057   collections->setGridX(1);
00058 
00059   //text is on right of icons
00060   collections->setItemTextPos( QIconView::Right );
00061 
00062   //disable frame
00063   collections->setFrameShape ( QFrame::NoFrame );
00064 
00065   collections->setMaxItemWidth(500);
00066   collections->setPaletteBackgroundColor( QColor(193, 210, 238) );
00067   collections->setDragAutoScroll(true);
00068   collections->setAcceptDrops(true);
00069   collections->setVScrollBarMode( QScrollView::Auto );
00070   collections->setHScrollBarMode( QScrollView::Auto );
00071   //--------------------------------------
00072   //no selection by default
00073   currentSelection = NULL;
00074   //--------------------------------------
00075   //connect drop event on iconview to reorder slot
00076   connect( collections, SIGNAL(itemHasMoved()), SLOT(reorder()) );
00077 
00078   //handle selection attempts
00079   connect( collections, SIGNAL(selectionChanged(QIconViewItem*)),
00080            this,      SLOT(handleSelectionAttempt(QIconViewItem*)));
00081   //--------------------------------------
00082   //create create/delete buttons
00083   QFont buttonFont( qApp->font() );
00084   buttonFont.setBold(true);
00085   buttonFont.setPointSize( 11 );
00086 
00087   createButton = new QToolButton( this );
00088   createButton->setTextLabel(tr("Create"));
00089   createButton->setIconSet( QPixmap(QString(IMAGE_PATH)+"buttonIcons/create.png") );
00090   createButton->setTextPosition(QToolButton::Right);
00091   createButton->setFont( buttonFont );
00092   createButton->setUsesTextLabel( true );
00093   createButton->setEnabled(true);
00094   createButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00095   QToolTip::add( createButton, tr("Create a collection and append to subalbum list") );
00096   connect( createButton, SIGNAL(clicked()), SLOT(createAction()) );
00097   
00098   deleteButton = new QToolButton( this );
00099   deleteButton->setTextLabel(tr("Delete"));
00100   deleteButton->setIconSet( QPixmap(QString(IMAGE_PATH)+"buttonIcons/delete.png") );
00101   deleteButton->setTextPosition(QToolButton::Right);
00102   deleteButton->setFont( buttonFont );
00103   deleteButton->setUsesTextLabel( true );
00104   deleteButton->setEnabled(false);
00105   deleteButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00106   QToolTip::add( deleteButton, tr("Delete selected collection and all its contents") );
00107   connect( deleteButton, SIGNAL(clicked()), SLOT(deleteAction()) );
00108   //--------------------------------------
00109   //place label, listbox, and buttons in grid
00110   QGridLayout* grid = new QGridLayout( this, 3, 2, 0 );
00111   grid->addMultiCellWidget( collectionsHeader, 0, 0, 0, 1, Qt::AlignHCenter );
00112   grid->addMultiCellWidget( collections, 1, 1, 0, 1 );
00113   grid->addWidget( createButton, 2, 0, Qt::AlignHCenter);
00114   grid->addWidget( deleteButton, 2, 1, Qt::AlignHCenter);
00115 
00116   //allow collections listing to grow
00117   grid->setRowStretch( 1, 1 );
00118 
00119   //set the background of the widget to be white
00120   setPaletteBackgroundColor( QColor(193, 210, 238) );
00121 }
00122 //==============================================
00123 void SubalbumsWidget::createAction()
00124 {
00125   //create new collection object
00126   Album* albm = layout->getWindow()->getTitle()->getAlbum();
00127   Subalbum* newCollection = new Subalbum( albm, albm->getNumSubalbums()+1 );
00128   albm->appendSubalbum( newCollection );
00129 
00130   //create collection icon and auto select it
00131   SubalbumPreviewWidget* newCollectionIcon = new SubalbumPreviewWidget( collections, newCollection );
00132   newCollectionIcon->setDropEnabled(true);
00133   selectCollection( newCollectionIcon );
00134   
00135   //update enabled state of delete collection button
00136   deleteButton->setEnabled( collections->count() > 1 );
00137 }
00138 //==============================================
00139 void SubalbumsWidget::deleteAction()
00140 {
00141   //if an item is selected it remove it
00142   if(collections->currentItem() != NULL)
00143   {
00144     //if user has chosen to not receive destructive action warnings, or agrees to the action, then
00145     //delete subalbum and refresh view
00146     bool proceed = !((Window*)qApp->mainWidget())->getConfig()->getBool( "alerts", "showDestructiveAlerts" );
00147     if(!proceed)
00148     {
00149       QuestionDialog sure( tr("Delete collection?"),
00150                            tr("Once deleted a collection and it's contents cannot be brought back unless a saved copy of the album exists."),
00151                            "alertIcons/warning.png",
00152                            this );
00153       proceed = sure.exec();
00154     }
00155     if(proceed)
00156     {
00157       //get handle on currently selected collection
00158       QIconViewItem* oldSelection = collections->currentItem();
00159 
00160       //get handle on the next automatically selected collection
00161       //auto select the new collection. If there is no next 
00162       //collection, select the previous collection (again if present)
00163       QIconViewItem* newSelection = oldSelection->nextItem();
00164       if(newSelection == NULL) newSelection = oldSelection->prevItem();
00165 
00166       //auto select a remaining collection if one exists
00167       //we select before removing and deleting the old collection so that
00168       //the collection information above smoothly transitions
00169       selectCollection( newSelection );
00170 
00171       //remove the collection from the album
00172       Subalbum* s = ((SubalbumPreviewWidget*) oldSelection)->getSubalbum();
00173       layout->getWindow()->getTitle()->getAlbum()->removeSubalbum( s );
00174 
00175       //free the collection icon
00176       delete oldSelection;
00177       oldSelection = NULL;
00178 
00179       //rearrange the items in the grid, making 
00180       //sure  new selection is visible
00181       collections->arrangeItemsInGrid();
00182       if(newSelection != NULL) collections->ensureItemVisible( newSelection );
00183 
00184       //update enabled state of delete collection button
00185       deleteButton->setEnabled( collections->count() > 1 );
00186 
00187       //notifty title widget that the album's photo count has possible changed
00188       layout->getWindow()->getTitle()->updateMenus();
00189     }
00190   }
00191 }
00192 //==============================================
00193 void SubalbumsWidget::refreshSelectedCollectionName()
00194 {
00195   if( currentSelection != NULL)
00196     currentSelection->setText( ((SubalbumPreviewWidget*)currentSelection)->getSubalbum()->getName() );
00197 }
00198 //==============================================
00199 void SubalbumsWidget::updatedSelectedCollectionImage( QPixmap* val)
00200 {
00201   if( currentSelection != NULL) 
00202     currentSelection->setPixmap( *val );
00203 }
00204 //==============================================
00205 void SubalbumsWidget::reorder()
00206 {
00207   //so item has been moved, reorder linked list of items as necessary
00208   collections->sort( true );
00209   collections->arrangeItemsInGrid();
00210 
00211   //sync lists
00212   Album* albm = layout->getWindow()->getTitle()->getAlbum();
00213   albm->syncSubalbumList((SubalbumPreviewWidget*)collections->firstItem());
00214 }
00215 //==============================================
00216 void SubalbumsWidget::updateButtons(bool enable)
00217 {
00218   if(enable)
00219   {
00220     createButton->setEnabled( true );    
00221     deleteButton->setEnabled( buttonsState );
00222   }
00223   else
00224   {
00225     buttonsState = createButton->isEnabled();
00226     createButton->setEnabled( false );
00227     deleteButton->setEnabled( false );
00228   }
00229 }
00230 //==============================================
00231 LayoutWidget* SubalbumsWidget::getParent() { return layout; }
00232 QIconViewItem* SubalbumsWidget::getCurrentSelection() { return currentSelection; }
00233 //==============================================
00234 Subalbum* SubalbumsWidget::getSelectedSubalbum()
00235 { 
00236   return ((SubalbumPreviewWidget*) currentSelection )->getSubalbum(); 
00237 }
00238 //==============================================
00239 void SubalbumsWidget::refreshCollectionsList()
00240 {
00241   //delete all previous entries
00242   QIconViewItem* current = collections->firstItem();
00243   while(current != NULL)
00244   {
00245     QIconViewItem* next = current->nextItem();
00246     delete current;
00247     current = next;
00248   }
00249   
00250   //for some reason scrollbar does not disappear automatically.
00251   //Calling clear fixes this.
00252   collections->clear();
00253   
00254   //reset cached selection handle
00255   currentSelection = NULL;
00256   
00257   //insert all collections
00258   Subalbum* curCollection = layout->getWindow()->getTitle()->getAlbum()->getFirstSubalbum();
00259   while( curCollection != NULL)
00260   {
00261     SubalbumPreviewWidget* item = new SubalbumPreviewWidget( collections, curCollection );
00262     item->setDropEnabled(true);
00263     curCollection = curCollection->getNext();
00264   }
00265   
00266   //refresh iconview
00267   collections->arrangeItemsInGrid();
00268   
00269   //auto select first item
00270   selectFirstCollection();
00271 }
00272 //==============================================
00273 void SubalbumsWidget::handleSelectionAttempt( QIconViewItem* item )
00274 {
00275   //select collections only when program is not busy.
00276   if( !layout->getWindow()->getTitle()->getBusy() )
00277     selectCollection( item );
00278 }
00279 //==============================================
00280 void SubalbumsWidget::selectFirstCollection()
00281 {
00282   selectCollection( collections->firstItem() );
00283 }
00284 //==============================================
00285 void SubalbumsWidget::selectCollection( QIconViewItem* item )
00286 {
00287   //no necessary action when selecting the currently selection collection
00288   if(currentSelection == item) return;
00289 
00290   //select item
00291   if( item != NULL ) collections->setSelected( item, true);
00292   
00293   //cachce selection
00294   currentSelection = item;
00295   
00296   //emit signal that a different collection has been selected
00297   if(currentSelection == NULL )
00298     emit collectionSelected( NULL );
00299   else
00300     emit collectionSelected( ((SubalbumPreviewWidget*)currentSelection)->getSubalbum() );
00301 }
00302 //==============================================
00303 /*
00304  void SubalbumsWidget::setSelectedSubalbum( Subalbum* selection )
00305 {
00306   QIconViewItem* current = collections->firstItem();
00307   while(current != NULL)
00308   {
00309     //found indicated collection
00310     if( ((SubalbumPreviewWidget*)current)->getSubalbum() == selection )
00311     {
00312       current->setSelected( true );
00313       break;
00314     }
00315 
00316     //move to next item
00317     current = current->nextItem();
00318   }
00319 }*/
00320 //==============================================
00321 

Generated on Thu Jan 3 10:52:46 2008 for AlbumShaper by  doxygen 1.5.4