mosaicOptionsDialog.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 <qframe.h>
00013 #include <qlabel.h>
00014 #include <qradiobutton.h>
00015 #include <qlineedit.h>
00016 #include <qapplication.h>
00017 #include <qdir.h>
00018 #include <qbuttongroup.h>
00019 #include <qcombobox.h>
00020 #include <qpushbutton.h>
00021 #include <qlayout.h>
00022 #include <qlineedit.h>
00023 #include <qspinbox.h>
00024 #include <qfiledialog.h>
00025 
00026 //Projectwide includes
00027 #include "mosaicOptionsDialog.h"
00028 #include "../../clickableLabel.h"
00029 #include "../../window.h"
00030 #include "../../titleWidget.h"
00031 #include "../../statusWidget.h"
00032 #include "../../../config.h"
00033 #include "../../../configuration/configuration.h"
00034 #include "../../../backend/album.h"
00035 #include "../../../backend/tools/imageTools.h"
00036 #include "../../../backend/manipulations/mosaic.h"
00037 
00038 #define MAX_DEPTH 4
00039 #define MAX_FILES 1000
00040 
00041 #include <iostream>
00042 using namespace std;
00043 
00044 //==============================================
00045 MosaicOptionsDialog::MosaicOptionsDialog( QWidget *parent ) : QDialog(parent,NULL,true)
00046 {
00047   //--------------
00048   //Tile size options:
00049   QFrame* tileSizeOptions = new QFrame(this);
00050   
00051   QLabel* tileSizeLabel = new QLabel( tr("Tile size:"), this );
00052   tileSizes = new QComboBox( tileSizeOptions );
00053   tileSizes->insertItem( tr("Tiny") );
00054   tileSizes->insertItem( tr("Small") );
00055   tileSizes->insertItem( tr("Medium") );
00056   tileSizes->insertItem( tr("Large") );
00057   tileSizes->insertItem( tr("Huge") );
00058   tileSizes->insertItem( tr("Custom") );
00059   
00060   tileSizePreview = new QLabel( "(? x ?)", tileSizeOptions );
00061   tileWidth  = new QSpinBox( 1, 500, 1, tileSizeOptions );
00062   tileSizeX  = new QLabel( "x", tileSizeOptions );
00063   tileHeight = new QSpinBox( 1, 500, 1, tileSizeOptions );
00064   
00065   //set defaults
00066   tileWidth->setValue( 40 );
00067   tileHeight->setValue( 40 );
00068   
00069   //default to small
00070   tileSizes->setCurrentItem( 1 );
00071   updateTileSizePreview();
00072   
00073   //update custom controls when selection changes in the future
00074   connect( tileSizes, SIGNAL(activated(int)), this, SLOT(updateTileSizePreview()) );  
00075   
00076   QGridLayout* tileSizeGrid = new QGridLayout( tileSizeOptions, 1, 6, 0 );
00077   tileSizeGrid->addWidget( tileSizes,  1, 0 );
00078   tileSizeGrid->addWidget( tileSizePreview,  1, 1 );
00079   tileSizeGrid->addWidget( tileWidth,        1, 2 );
00080   tileSizeGrid->addWidget( tileSizeX,        1, 3 );
00081   tileSizeGrid->addWidget( tileHeight,       1, 4 );
00082   tileSizeGrid->setColStretch( 5, 1 );
00083   tileSizeGrid->setSpacing( WIDGET_SPACING );
00084   //--------------
00085   //Tile type options:
00086   QFrame* tileTypeOptions = new QFrame(this);
00087   
00088   QLabel* tileTypeLabel = new QLabel( tr("Base tiles on:"), this );
00089   //------------------------------
00090   tileType_albumPhotos = new QRadioButton( tr("Album photos"), tileTypeOptions );  
00091   tileType_albumPhotos->setChecked(true);
00092   //------------------------------  
00093   tileType_solidColors = new QRadioButton( tr("Solid colors"), tileTypeOptions );  
00094   //------------------------------  
00095   tileType_imagesFrom = new QRadioButton( tr("Images from:"), tileTypeOptions );  
00096   
00097   locationVal = new QLineEdit( tileTypeOptions );
00098   
00099   Configuration* config = ((Window*)qApp->mainWidget())->getConfig();
00100   QString path = config->getString( "loadSave", "addPhotoDir" );
00101   QDir testPath(path);
00102   if(!testPath.exists())
00103   {
00104     config->resetSetting( "loadSave", "addPhotoDir" );
00105     path = config->getString( "loadSave", "addPhotoDir" );
00106   }
00107   locationVal->setText( path );
00108   locationVal->setCursorPosition(0);
00109   
00110   browseButton = new ClickableLabel( tileTypeOptions );
00111   browseButton->setPixmap( QPixmap(QString(IMAGE_PATH)+"buttonIcons/browse.png") );
00112   connect( browseButton, SIGNAL(clicked()), SLOT(browse()) );  
00113   
00114   //in the future enable/disable the images from line edit and browse button when
00115   //the thrid option is selected/unselected. also force this to take place now as well
00116   connect( tileType_imagesFrom, SIGNAL(stateChanged(int)),
00117            this, SLOT(updateImagesFromOptions()) );
00118   updateImagesFromOptions();
00119   //------------------------------    
00120   
00121   QButtonGroup* typeGroup = new QButtonGroup( tileTypeOptions );
00122   typeGroup->hide();
00123   typeGroup->insert( tileType_albumPhotos );
00124   typeGroup->insert( tileType_solidColors );
00125   typeGroup->insert( tileType_imagesFrom );
00126  
00127   QGridLayout* tileTypeGrid = new QGridLayout( tileTypeOptions, 3, 3, 0 );
00128   tileTypeGrid->addMultiCellWidget( tileType_albumPhotos,   0,0, 0,2 );
00129   tileTypeGrid->addMultiCellWidget( tileType_solidColors,   1,1, 0,2 );
00130   tileTypeGrid->addWidget(          tileType_imagesFrom,   2,0 );
00131   tileTypeGrid->addWidget(          locationVal,  2,1 );
00132   tileTypeGrid->addWidget(          browseButton, 2,2 );
00133   
00134   tileTypeGrid->setColSpacing(1, 300);
00135   tileTypeGrid->setColStretch(1, 1);
00136   tileTypeGrid->setSpacing( WIDGET_SPACING );
00137   //--------------
00138   //Dialog buttons:  
00139   QFrame* buttonsFrame =   new QFrame( this, "dialogButtons" );
00140                                 
00141   QPushButton* applyButton = new QPushButton( tr("Apply"), buttonsFrame );
00142   applyButton->setDefault(true);
00143   applyButton->setFocus();
00144   connect( applyButton, SIGNAL(clicked()), SLOT(accept()) );
00145 
00146   QPushButton* cancelButton = new QPushButton( tr("Cancel"), buttonsFrame );
00147   connect( cancelButton, SIGNAL(clicked()), SLOT(reject()) );
00148    
00149   QGridLayout* buttonsGrid = new QGridLayout( buttonsFrame, 1, 2, 0 );
00150   buttonsGrid->addWidget( applyButton,  0, 0 );
00151   buttonsGrid->addWidget( cancelButton, 0, 1 );
00152   buttonsGrid->setSpacing( WIDGET_SPACING );
00153   //--------------
00154   //Top level grid  
00155   QGridLayout* mainGrid = new QGridLayout( this, 5, 2, 0 );
00156 
00157   mainGrid->setRowStretch( 0, 1 );
00158   mainGrid->addWidget( tileSizeLabel,            1,0, Qt::AlignRight | Qt::AlignVCenter );
00159   mainGrid->addWidget( tileSizeOptions,          1,1 );
00160   mainGrid->addWidget( tileTypeLabel,            2,0, Qt::AlignRight | Qt::AlignVCenter );
00161   mainGrid->addWidget( tileTypeOptions,          2,1 );
00162   mainGrid->setRowStretch( 3, 1 );
00163   mainGrid->addMultiCellWidget( buttonsFrame,    4,4, 0,1, Qt::AlignHCenter );
00164   mainGrid->setSpacing( WIDGET_SPACING );
00165   mainGrid->setMargin( WIDGET_SPACING );  
00166   //--------------
00167   //Window caption
00168   setCaption( tr("Mosaic Options") );
00169   //-------------------------------
00170   //set window to not be resizeable
00171   this->show();
00172 //  setFixedSize(size());
00173   //-------------------------------
00174 }
00175 //==============================================
00176 MosaicOptions* MosaicOptionsDialog::getOptions()
00177 {
00178   //construct a list of files based on the user selection
00179   QStringList files = determineFilesList();
00180   
00181   //get selected tile size
00182   QSize tileSize = determineTileSize();
00183   
00184   //return a populated mosaic options object
00185   return new MosaicOptions( files, tileSize, ((Window*)qApp->mainWidget())->getStatus() );
00186 }
00187 //==============================================
00188 QSize MosaicOptionsDialog::determineTileSize()
00189 {
00190   if( tileSizes->currentItem() == 0 )      return QSize(  20,  20 );
00191   else if( tileSizes->currentItem() == 1 ) return QSize(  40,  40 );
00192   else if( tileSizes->currentItem() == 2 ) return QSize(  65,  65 );
00193   else if( tileSizes->currentItem() == 3 ) return QSize( 100, 100 );
00194   else if( tileSizes->currentItem() == 4 ) return QSize( 150, 150 );
00195   else                                     return QSize( tileWidth->value(), tileHeight->value() );
00196 }
00197 //==============================================
00198 QStringList MosaicOptionsDialog::determineFilesList()
00199 {
00200   //Album photos
00201   if(  tileType_albumPhotos->isChecked() )
00202   {
00203     Album* albm = ((Window*)qApp->mainWidget())->getTitle()->getAlbum();
00204     return albm->getThumbnailFilenames();
00205   }
00206   
00207   //Solid colors - return empty list
00208   else if ( tileType_solidColors->isChecked() ) { return QStringList(); }
00209   //Images from...
00210   else
00211   {
00212      QStringList files;
00213      QString path = locationVal->text();
00214      appendImagesInPath( files, path, 0 );
00215      return files;
00216   }
00217 }
00218 //==============================================
00219 void MosaicOptionsDialog::appendImagesInPath(QStringList& files, QString path, int depth)
00220 {
00221 //  cout << "appending files in " << path << "\n";
00222   QDir tmpDir;
00223   tmpDir.setPath( path );
00224 
00225   //add all iamges  
00226   tmpDir.setFilter( QDir::Files | QDir::Readable );
00227   tmpDir.setNameFilter( "*.gif;*.jpg;*.jpeg;*.png;*.xpm;*.GIF;*.JPG;*.JPEG;*.PNG;*.XPM" );  
00228   QStringList images = tmpDir.entryList();
00229   QStringList::iterator it;
00230   QSize imageRes;
00231   for(it = images.begin(); it != images.end(); it++ ) 
00232   {
00233     //check we can get a decent resolution out of the file
00234     getImageSize( tmpDir.absFilePath( *it ), imageRes );
00235     if( imageRes.width() <= 0 || imageRes.height() <= 0 ) continue;
00236     
00237 //    cout << "appending " << *it << "\n";
00238     files.append( tmpDir.absFilePath( *it ) ); 
00239     
00240     //break out if we have too many files    
00241     if( files.count() >= MAX_FILES ) break;
00242   }    
00243   
00244   //recurse on all directories (but not symbolic links) - but only go down three levels
00245   if( depth < MAX_DEPTH && files.count() < MAX_FILES )
00246   {
00247     tmpDir.setFilter( QDir::Dirs | QDir::Readable | QDir::NoSymLinks );
00248     tmpDir.setNameFilter( "*" );
00249     QStringList directores = tmpDir.entryList();
00250     for(it = directores.begin(); it != directores.end(); it++ ) 
00251     {  
00252       QString dir = *it;
00253       if( dir.compare( "." ) == 0 || dir.compare( ".." ) == 0 ) continue;
00254       
00255       appendImagesInPath( files, tmpDir.absFilePath( *it ), depth+1 ); 
00256     }
00257   }
00258 }
00259 //==============================================
00260 void MosaicOptionsDialog::updateTileSizePreview()
00261 {
00262   //get selected tile size
00263   QSize tileSize = determineTileSize();
00264 
00265   //show/hide custom controls
00266   bool customSelected = tileSizes->currentItem() == tileSizes->count()-1;
00267   
00268   tileSizePreview->setShown( !customSelected );
00269   tileWidth->setShown ( customSelected );
00270   tileSizeX->setShown ( customSelected );
00271   tileHeight->setShown( customSelected );
00272   
00273   //update tile size preview text
00274   if( !customSelected )
00275   {
00276     tileSizePreview->setText( QString("(%1 x %2)").arg( tileSize.width() ).arg( tileSize.height() ) );
00277   }
00278   
00279   //update preview image
00280 }
00281 //==============================================
00282 void MosaicOptionsDialog::updateImagesFromOptions()
00283 {
00284   bool enabled = tileType_imagesFrom->isChecked();
00285   locationVal->setEnabled( enabled );
00286   browseButton->setEnabled( enabled );
00287 }
00288 //==============================================
00289 void MosaicOptionsDialog::browse()
00290 {
00291   //get directory from user
00292   QString dirName = QFileDialog::getExistingDirectory( locationVal->text(), this, NULL, tr("Images directory") );
00293   
00294   if(!dirName.isNull())
00295     locationVal->setText( dirName );
00296 }
00297 //==============================================
00298 
00299 
00300 

Generated on Wed Jan 24 05:38:28 2007 for AlbumShaper by  doxygen 1.5.1