AlbumShaper  1.0a3
mosaicOptionsDialog.cpp
Go to the documentation of this file.
1 //==============================================
2 // copyright : (C) 2003-2005 by Will Stokes
3 //==============================================
4 // This program is free software; you can redistribute it
5 // and/or modify it under the terms of the GNU General
6 // Public License as published by the Free Software
7 // Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //==============================================
10 
11 //Systemwide includes
12 #include <q3frame.h>
13 #include <qlabel.h>
14 #include <qradiobutton.h>
15 #include <qlineedit.h>
16 #include <qapplication.h>
17 #include <qdir.h>
18 #include <q3buttongroup.h>
19 #include <qcombobox.h>
20 #include <qpushbutton.h>
21 #include <qlayout.h>
22 #include <qlineedit.h>
23 #include <qspinbox.h>
24 #include <q3filedialog.h>
25 //Added by qt3to4:
26 #include <Q3GridLayout>
27 #include <QPixmap>
28 
29 //Projectwide includes
30 #include "mosaicOptionsDialog.h"
31 #include "../../clickableLabel.h"
32 #include "../../window.h"
33 #include "../../titleWidget.h"
34 #include "../../statusWidget.h"
35 #include "../../../config.h"
36 #include "../../../configuration/configuration.h"
37 #include "../../../backend/album.h"
38 #include "../../../backend/tools/imageTools.h"
39 #include "../../../backend/manipulations/mosaic.h"
40 
41 #define MAX_DEPTH 4
42 #define MAX_FILES 1000
43 
44 #include <iostream>
45 using namespace std;
46 
47 //==============================================
49 {
50  //--------------
51  //Tile size options:
52  Q3Frame* tileSizeOptions = new Q3Frame(this);
53 
54  QLabel* tileSizeLabel = new QLabel( tr("Tile size:"), this );
55  tileSizes = new QComboBox( tileSizeOptions );
56  tileSizes->insertItem( tr("Tiny") );
57  tileSizes->insertItem( tr("Small") );
58  tileSizes->insertItem( tr("Medium") );
59  tileSizes->insertItem( tr("Large") );
60  tileSizes->insertItem( tr("Huge") );
61  tileSizes->insertItem( tr("Custom") );
62 
63  tileSizePreview = new QLabel( "(? x ?)", tileSizeOptions );
64  tileWidth = new QSpinBox( 1, 500, 1, tileSizeOptions );
65  tileSizeX = new QLabel( "x", tileSizeOptions );
66  tileHeight = new QSpinBox( 1, 500, 1, tileSizeOptions );
67 
68  //set defaults
69  tileWidth->setValue( 40 );
70  tileHeight->setValue( 40 );
71 
72  //default to small
73  tileSizes->setCurrentItem( 1 );
75 
76  //update custom controls when selection changes in the future
77  connect( tileSizes, SIGNAL(activated(int)), this, SLOT(updateTileSizePreview()) );
78 
79  Q3GridLayout* tileSizeGrid = new Q3GridLayout( tileSizeOptions, 1, 6, 0 );
80  tileSizeGrid->addWidget( tileSizes, 1, 0 );
81  tileSizeGrid->addWidget( tileSizePreview, 1, 1 );
82  tileSizeGrid->addWidget( tileWidth, 1, 2 );
83  tileSizeGrid->addWidget( tileSizeX, 1, 3 );
84  tileSizeGrid->addWidget( tileHeight, 1, 4 );
85  tileSizeGrid->setColStretch( 5, 1 );
86  tileSizeGrid->setSpacing( WIDGET_SPACING );
87  //--------------
88  //Tile type options:
89  Q3Frame* tileTypeOptions = new Q3Frame(this);
90 
91  QLabel* tileTypeLabel = new QLabel( tr("Base tiles on:"), this );
92  //------------------------------
93  tileType_albumPhotos = new QRadioButton( tr("Album photos"), tileTypeOptions );
94  tileType_albumPhotos->setChecked(true);
95  //------------------------------
96  tileType_solidColors = new QRadioButton( tr("Solid colors"), tileTypeOptions );
97  //------------------------------
98  tileType_imagesFrom = new QRadioButton( tr("Images from:"), tileTypeOptions );
99 
100  locationVal = new QLineEdit( tileTypeOptions );
101 
102  Configuration* config = ((Window*)qApp->mainWidget())->getConfig();
103  QString path = config->getString( "loadSave", "addPhotoDir" );
104  QDir testPath(path);
105  if(!testPath.exists())
106  {
107  config->resetSetting( "loadSave", "addPhotoDir" );
108  path = config->getString( "loadSave", "addPhotoDir" );
109  }
110  locationVal->setText( path );
111  locationVal->setCursorPosition(0);
112 
113  browseButton = new ClickableLabel( tileTypeOptions );
114  browseButton->setPixmap( QPixmap(QString(IMAGE_PATH)+"buttonIcons/browse.png") );
115  connect( browseButton, SIGNAL(clicked()), SLOT(browse()) );
116 
117  //in the future enable/disable the images from line edit and browse button when
118  //the thrid option is selected/unselected. also force this to take place now as well
119  connect( tileType_imagesFrom, SIGNAL(stateChanged(int)),
120  this, SLOT(updateImagesFromOptions()) );
122  //------------------------------
123 
124  Q3ButtonGroup* typeGroup = new Q3ButtonGroup( tileTypeOptions );
125  typeGroup->hide();
126  typeGroup->insert( tileType_albumPhotos );
127  typeGroup->insert( tileType_solidColors );
128  typeGroup->insert( tileType_imagesFrom );
129 
130  Q3GridLayout* tileTypeGrid = new Q3GridLayout( tileTypeOptions, 3, 3, 0 );
131  tileTypeGrid->addMultiCellWidget( tileType_albumPhotos, 0,0, 0,2 );
132  tileTypeGrid->addMultiCellWidget( tileType_solidColors, 1,1, 0,2 );
133  tileTypeGrid->addWidget( tileType_imagesFrom, 2,0 );
134  tileTypeGrid->addWidget( locationVal, 2,1 );
135  tileTypeGrid->addWidget( browseButton, 2,2 );
136 
137  tileTypeGrid->setColSpacing(1, 300);
138  tileTypeGrid->setColStretch(1, 1);
139  tileTypeGrid->setSpacing( WIDGET_SPACING );
140  //--------------
141  //Dialog buttons:
142  Q3Frame* buttonsFrame = new Q3Frame( this, "dialogButtons" );
143 
144  QPushButton* applyButton = new QPushButton( tr("Apply"), buttonsFrame );
145  applyButton->setDefault(true);
146  applyButton->setFocus();
147  connect( applyButton, SIGNAL(clicked()), SLOT(accept()) );
148 
149  QPushButton* cancelButton = new QPushButton( tr("Cancel"), buttonsFrame );
150  connect( cancelButton, SIGNAL(clicked()), SLOT(reject()) );
151 
152  Q3GridLayout* buttonsGrid = new Q3GridLayout( buttonsFrame, 1, 2, 0 );
153  buttonsGrid->addWidget( applyButton, 0, 0 );
154  buttonsGrid->addWidget( cancelButton, 0, 1 );
155  buttonsGrid->setSpacing( WIDGET_SPACING );
156  //--------------
157  //Top level grid
158  Q3GridLayout* mainGrid = new Q3GridLayout( this, 5, 2, 0 );
159 
160  mainGrid->setRowStretch( 0, 1 );
161  mainGrid->addWidget( tileSizeLabel, 1,0, Qt::AlignRight | Qt::AlignVCenter );
162  mainGrid->addWidget( tileSizeOptions, 1,1 );
163  mainGrid->addWidget( tileTypeLabel, 2,0, Qt::AlignRight | Qt::AlignVCenter );
164  mainGrid->addWidget( tileTypeOptions, 2,1 );
165  mainGrid->setRowStretch( 3, 1 );
166  mainGrid->addMultiCellWidget( buttonsFrame, 4,4, 0,1, Qt::AlignHCenter );
167  mainGrid->setSpacing( WIDGET_SPACING );
168  mainGrid->setMargin( WIDGET_SPACING );
169  //--------------
170  //Window caption
171  setCaption( tr("Mosaic Options") );
172  //-------------------------------
173  //set window to not be resizeable
174  this->show();
175 // setFixedSize(size());
176  //-------------------------------
177 }
178 //==============================================
180 {
181  //construct a list of files based on the user selection
182  QStringList files = determineFilesList();
183 
184  //get selected tile size
185  QSize tileSize = determineTileSize();
186 
187  //return a populated mosaic options object
188  return new MosaicOptions( files, tileSize, ((Window*)qApp->mainWidget())->getStatus() );
189 }
190 //==============================================
192 {
193  if( tileSizes->currentItem() == 0 ) return QSize( 20, 20 );
194  else if( tileSizes->currentItem() == 1 ) return QSize( 40, 40 );
195  else if( tileSizes->currentItem() == 2 ) return QSize( 65, 65 );
196  else if( tileSizes->currentItem() == 3 ) return QSize( 100, 100 );
197  else if( tileSizes->currentItem() == 4 ) return QSize( 150, 150 );
198  else return QSize( tileWidth->value(), tileHeight->value() );
199 }
200 //==============================================
202 {
203  //Album photos
204  if( tileType_albumPhotos->isChecked() )
205  {
206  Album* albm = ((Window*)qApp->mainWidget())->getTitle()->getAlbum();
207  return albm->getThumbnailFilenames();
208  }
209 
210  //Solid colors - return empty list
211  else if ( tileType_solidColors->isChecked() ) { return QStringList(); }
212  //Images from...
213  else
214  {
215  QStringList files;
216  QString path = locationVal->text();
217  appendImagesInPath( files, path, 0 );
218  return files;
219  }
220 }
221 //==============================================
222 void MosaicOptionsDialog::appendImagesInPath(QStringList& files, QString path, int depth)
223 {
224 // cout << "appending files in " << path << "\n";
225  QDir tmpDir;
226  tmpDir.setPath( path );
227 
228  //add all iamges
229  tmpDir.setFilter( QDir::Files | QDir::Readable );
230  tmpDir.setNameFilter( "*.gif;*.jpg;*.jpeg;*.png;*.xpm;*.GIF;*.JPG;*.JPEG;*.PNG;*.XPM" );
231  QStringList images = tmpDir.entryList();
232  QStringList::iterator it;
233  QSize imageRes;
234  for(it = images.begin(); it != images.end(); it++ )
235  {
236  //check we can get a decent resolution out of the file
237  getImageSize( tmpDir.absFilePath( *it ), imageRes );
238  if( imageRes.width() <= 0 || imageRes.height() <= 0 ) continue;
239 
240 // cout << "appending " << *it << "\n";
241  files.append( tmpDir.absFilePath( *it ) );
242 
243  //break out if we have too many files
244  if( files.count() >= MAX_FILES ) break;
245  }
246 
247  //recurse on all directories (but not symbolic links) - but only go down three levels
248  if( depth < MAX_DEPTH && files.count() < MAX_FILES )
249  {
250  tmpDir.setFilter( QDir::Dirs | QDir::Readable | QDir::NoSymLinks );
251  tmpDir.setNameFilter( "*" );
252  QStringList directores = tmpDir.entryList();
253  for(it = directores.begin(); it != directores.end(); it++ )
254  {
255  QString dir = *it;
256  if( dir.compare( "." ) == 0 || dir.compare( ".." ) == 0 ) continue;
257 
258  appendImagesInPath( files, tmpDir.absFilePath( *it ), depth+1 );
259  }
260  }
261 }
262 //==============================================
264 {
265  //get selected tile size
266  QSize tileSize = determineTileSize();
267 
268  //show/hide custom controls
269  bool customSelected = tileSizes->currentItem() == tileSizes->count()-1;
270 
271  tileSizePreview->setShown( !customSelected );
272  tileWidth->setShown ( customSelected );
273  tileSizeX->setShown ( customSelected );
274  tileHeight->setShown( customSelected );
275 
276  //update tile size preview text
277  if( !customSelected )
278  {
279  tileSizePreview->setText( QString("(%1 x %2)").arg( tileSize.width() ).arg( tileSize.height() ) );
280  }
281 
282  //update preview image
283 }
284 //==============================================
286 {
287  bool enabled = tileType_imagesFrom->isChecked();
288  locationVal->setEnabled( enabled );
289  browseButton->setEnabled( enabled );
290 }
291 //==============================================
293 {
294  //get directory from user
295  QString dirName = Q3FileDialog::getExistingDirectory( locationVal->text(), this, NULL, tr("Images directory") );
296 
297  if(!dirName.isNull())
298  locationVal->setText( dirName );
299 }
300 //==============================================
301 
302 
303 
Top level widget, encapsulates the title widget, the layout widget, and the toolbar widget...
Definition: window.h:39
QStringList determineFilesList()
QString getString(QString group, QString key)
Fetch string setting.
void resetSetting(QString group, QString key)
Resets a setting to it&#39;s default value.
void setPixmap(const QPixmap &p)
MosaicOptionsDialog(QWidget *parent=0)
Constructs layout.
QRadioButton * tileType_imagesFrom
QString IMAGE_PATH
Definition: config.cpp:18
QStringList getThumbnailFilenames()
Returns a list of the most up to date thumbnail filesnames.
Definition: album.cpp:1426
A clickable label.
QRadioButton * tileType_solidColors
void appendImagesInPath(QStringList &files, QString path, int depth)
void setEnabled(bool val)
#define WIDGET_SPACING
Definition: config.h:31
QRadioButton * tileType_albumPhotos
MosaicOptions * getOptions()
returns a populate options object
bool getImageSize(const char *filename, QSize &size)
Get image dimensions.
Definition: imageTools.cpp:192
Configuration object manages all user-specific application settings.
Definition: configuration.h:24
An album contains Subalbums.
Definition: album.h:52
#define MAX_FILES
ClickableLabel * browseButton
#define MAX_DEPTH