Main Page   Compound List   File List   Compound Members   File Members  

titleWidget.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003 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 //  As a special exception, Will Stokes gives permission to 
00011 //  link this program with Qt non-commercial edition, and 
00012 //  distribute the resulting executable, without including the 
00013 //  source code for the Qt non-commercial edition in the 
00014 //  source distribution. 
00015 //==============================================
00016 
00017 //Systemwide includes
00018 #include <qwidget.h>
00019 #include <qmenubar.h>
00020 #include <qpopupmenu.h>
00021 #include <qlayout.h>
00022 #include <qlabel.h>
00023 #include <qfont.h>
00024 #include <qpixmap.h>
00025 #include <qimage.h>
00026 #include <qpushbutton.h>
00027 #include <qlineedit.h>
00028 #include <qapplication.h>
00029 #include <qfiledialog.h>
00030 #include <qdir.h>
00031 #include <qcursor.h>
00032 
00033 //Projectwide includes
00034 #include "window.h"
00035 #include "about.h"
00036 #include "titleWidget.h"
00037 #include "layoutWidget.h"
00038 #include "subalbumsWidget.h"
00039 #include "subalbumWidget.h"
00040 #include "loadDialog.h"
00041 #include "saveDialog.h"
00042 #include "questionDialog.h"
00043 #include "../backend/album.h"
00044 #include "../backend/photo.h"
00045 #include "../config.h"
00046 
00047 #include <iostream.h>
00048 
00049 //==============================================
00050 TitleWidget::TitleWidget(QWidget *parent, 
00051                          const char* name ) : QFrame(parent,name)
00052 {
00054   window = (Window*)parent;
00055 
00057   about = NULL;
00058   
00059   //create file and help menus
00060   menu = new QMenuBar( this, "menuBar" );
00061   file = new QPopupMenu( this, "fileMenu" );
00062   NEW_ALBUM = file->insertItem( "&New", this, SLOT(newAlbum()), CTRL+Key_N );
00063   OPEN_ALBUM = file->insertItem( "&Open", this, SLOT(loadAlbum()), CTRL+Key_O );
00064   SAVE_ALBUM = file->insertItem( "&Save", this, SLOT(saveAlbum()), CTRL+Key_S );
00065   SAVEAS_ALBUM = file->insertItem( "&Save As", this, SLOT(saveAsAlbum()), CTRL+SHIFT+Key_S );
00066   file->insertItem( "&Quit", qApp, SLOT(quit()), CTRL+Key_Q);
00067   help = new QPopupMenu( this, "helpMenu" );
00068   help->insertItem( "&About", this, SLOT(aboutProgram()) );
00069   menu->insertItem( "&File", file );
00070   menu->insertSeparator();
00071   menu->insertItem( "&Help", help );
00072   
00073   //create backend album object
00074   albm = new Album();
00075 
00076   //create labels and text entries
00077   albumName = new QLabel( this );
00078   albumName->setText( "Album Name:" );
00079   albumName->setFont( QFont( "Times", 12, QFont::Bold ) );
00080   albumNameVal = new QLineEdit( this );
00081   albumNameVal->setFont( QFont( "Times", 12, QFont::Bold ) );
00082   connect( albumNameVal, SIGNAL(textChanged( const QString&)),
00083            SLOT( updateName(const QString&)) );
00084   
00085   albumDescription = new QLabel( this );
00086   albumDescription->setText( "Description:" );
00087   albumDescription->setFont( QFont( "Times", 12, QFont::Bold ) );
00088   albumDescriptionVal = new QLineEdit( this );
00089   albumDescriptionVal->setFont( QFont( "Times", 12, QFont::Bold ) );
00090   connect( albumDescriptionVal, SIGNAL(textChanged( const QString&)),
00091            SLOT( updateDescription(const QString&)) );
00092 
00093   albumAuthor = new QLabel( this );
00094   albumAuthor->setText( "Author:" );
00095   albumAuthor->setFont( QFont( "Times", 12, QFont::Bold ) );
00096   albumAuthorVal = new QLineEdit( this );
00097   albumAuthorVal->setFont( QFont( "Times", 12, QFont::Bold ) );
00098   connect( albumAuthorVal, SIGNAL(textChanged( const QString&)),
00099            SLOT( updateAuthor(const QString&)) );
00100   
00101   //create set image button
00102   representativeImageText = new QLabel( this );
00103   representativeImageText->setText( "Thumbnail:" );
00104   representativeImageText->setFont( QFont( "Times", 12, QFont::Bold ) );
00105   setImageImage = new QPixmap( QString(IMAGE_PATH)+"set.png" );
00106   setImage = new QPushButton( this );
00107   setImage->setPixmap( *setImageImage );
00108   setImage->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );      
00109   setImage->setEnabled(false);
00110   connect( setImage, SIGNAL(clicked()), SLOT(setImageAction()) );
00111   
00112   //create representative image
00113   representativeImage = albm->getRepresentativeImage(SMALL);
00114   representativeLogo = new QLabel( this );
00115   representativeLogo->setPixmap( *representativeImage );
00116            
00117   //create album Shaper image
00118   albumShaperImage = new QPixmap( QString(IMAGE_PATH)+"albumShaper.png" );
00119   albumShaperLogo = new QLabel( this );
00120   albumShaperLogo->setPixmap( *albumShaperImage );
00121 
00122   //Create horizontal line
00123   line = new QFrame(this);
00124   line->setFrameShadow( QFrame::Plain );
00125   line->setLineWidth( 2 );
00126   line->setFrameShape( QFrame::HLine );
00127 
00128   //place all labels in grid layout
00129   grid = new QGridLayout( this, 5, 6, 0 );
00130   grid->addMultiCellWidget( menu, 0, 0, 0, 5 );
00131   grid->addWidget( albumName,                   1, 0, Qt::AlignLeft);
00132   grid->addWidget( albumNameVal,                1, 1);
00133   grid->addWidget( albumDescription,            2, 0, Qt::AlignLeft);
00134   grid->addWidget( albumDescriptionVal,         2, 1);
00135   grid->addWidget( albumAuthor,                 3, 0, Qt::AlignLeft);
00136   grid->addWidget( albumAuthorVal,              3, 1);
00137   grid->addWidget( representativeImageText,     1, 2, Qt::AlignLeft );
00138   grid->addWidget( setImage,                    2, 2, Qt::AlignLeft );
00139   grid->addMultiCellWidget( representativeLogo, 1, 3, 3, 3 );
00140   grid->addMultiCellWidget( line,               4, 4, 0, 5);
00141   grid->addMultiCellWidget( albumShaperLogo,     1, 3, 5, 5 );
00142 
00143   //Set the second column, the actual album name and description and author
00144   //to have a minimum width
00145   grid->addColSpacing(1, 300 );
00146 
00147   //set the blank colum to stretch more than others to take
00148   //up leftover room
00149   grid->setColStretch( 4, 1 );
00150 
00151   //set the background of the widget to be white
00152   setPaletteBackgroundColor( QColor(255, 255, 255) );
00153   
00155   busy = false;
00156 }
00157 //==============================================
00158 TitleWidget::~TitleWidget()
00159 {
00160   delete setImageImage;
00161   delete representativeImage;
00162   delete albumShaperImage;
00163 }
00164 //==============================================
00165 void TitleWidget::updateName( const QString& val )
00166 {
00167   albm->setName(val);
00168 }
00169 //==============================================
00170 void TitleWidget::updateDescription( const QString& val )
00171 {
00172   albm->setDescription(val);
00173 }
00174 //==============================================
00175 void TitleWidget::updateAuthor( const QString& val )
00176 {
00177   albm->setAuthor(val);
00178 }
00179 //==============================================
00180 void TitleWidget::setAlbum( Album* val)
00181 {
00182   //delete old album
00183   delete albm;
00184   albm = val;
00185 }
00186 //==============================================
00187 Album* TitleWidget::getAlbum()
00188 {
00189   return albm;
00190 }
00191 //==============================================
00192 //called when user clicks "set image" button
00193 void TitleWidget::setImageAction()
00194 {
00195   //---------------------------------------------------------    
00196   //determine if a subalbum is even selected
00197   SubalbumWidget* sw = window->getLayout()->getSubalbum();
00198   if(sw == NULL)
00199     return;
00200   //---------------------------------------------------------        
00201   //determine if a photo is selected
00202   Photo* selectedPhoto = sw->getSelectedPhoto();
00203   if(selectedPhoto == NULL)
00204     return;
00205   //---------------------------------------------------------     
00206   //set representative iamges
00207   QImage* img = selectedPhoto->getImage(IMAGE);
00208   albm->setRepresentativeImages( img );
00209   delete img;
00210   img = NULL;
00211   //---------------------------------------------------------    
00212   //update onscreen images
00213   representativeLogo->setPixmap( *(albm->getRepresentativeImage(SMALL)) );
00214   //---------------------------------------------------------    
00215 }
00216 //==============================================
00217 void TitleWidget::refresh()
00218 {
00219   albumNameVal->setText( albm->getName() );
00220   albumDescriptionVal->setText( albm->getDescription() );
00221   albumAuthorVal->setText( albm->getAuthor() );
00222   representativeLogo->setPixmap( *(albm->getRepresentativeImage(SMALL)) );
00223 }                 
00224 //==============================================
00225 void TitleWidget::newAlbum()
00226 {
00227   //ask if the user is sure, then proceed
00228   QuestionDialog sure( "New album?",
00229                        "Warning, this operation cannot be undone and all current work will be lost.",
00230                        "warning.png",
00231                        this );
00232   //if user say yes then delete subalbum and refresh
00233   if(sure.exec())  
00234   {
00235     //delete old album
00236     delete albm;
00237     
00238     //create new one
00239     albm = new Album();
00240   
00241     //refresh screen
00242     refresh();
00243     window->refresh();
00244   }
00245 }
00246 //==============================================
00247 void TitleWidget::loadAlbum()
00248 {
00249   //set busy flag and deactivate buttons
00250   setBusy(true);
00251   window->getLayout()->getSubalbums()->updateButtons(false);
00252   if(window->getLayout()->getSubalbum() != NULL)
00253     window->getLayout()->getSubalbum()->updateButtons(false);
00254   
00255   QString albumXML = QFileDialog::getOpenFileName(NULL,
00256                                                   "XML Files (*.xml)",
00257                                                   this,
00258                                                   "open file dialog",
00259                                                   "Choose an album to load" );
00260 
00261   //if null return
00262   if(albumXML.isNull())
00263   {
00264     //nolonger busy
00265     setBusy(false);
00266     window->getLayout()->getSubalbums()->updateButtons(true);
00267     if(window->getLayout()->getSubalbum() != NULL)
00268       window->getLayout()->getSubalbum()->updateButtons(true);
00269     return;
00270   }
00271     
00272   qApp->setOverrideCursor( QCursor(Qt::WaitCursor));
00273   
00274   //pop up load dialog
00275   LoadDialog* dialog = new LoadDialog();
00276   
00277   //get size and location of application window
00278   QRect appRec = qApp->mainWidget()->geometry();
00279 
00280   //center this dialoag within application window
00281   int x, y;
00282   if(400 < appRec.width())
00283   { x = appRec.x() + ((appRec.width() - 400)/2); }
00284   else
00285   { x = appRec.x(); }
00286   if(300 < appRec.height())
00287   { y = appRec.y() + ((appRec.height() - 300)/2); }
00288   else
00289   { y = appRec.y(); }
00290     
00291   dialog->setGeometry(x, y, 400, 300);
00292   dialog->show();
00293   dialog->repaint();
00294   
00295   //create a new album
00296   dialog->printMessage("Freeing old album");
00297   delete albm;
00298   albm = new Album();
00299   
00300   //attempt to load xml file
00301   albm->importFromDisk(dialog, albumXML);
00302 
00303   //refresh screen
00304   refresh();
00305   
00306   window->refresh();
00307   //close dialog
00308   delete dialog;
00309   
00310   //nolonger busy
00311   setBusy(false);
00312   window->getLayout()->getSubalbums()->updateButtons(true);
00313   if(window->getLayout()->getSubalbum() != NULL)
00314     window->getLayout()->getSubalbum()->updateButtons(true);
00315   qApp->restoreOverrideCursor();
00316 }
00317 //==============================================
00318 void TitleWidget::saveAlbum()
00319 {
00320   //set busy flag and disable buttons
00321   setBusy(true);
00322   window->getLayout()->getSubalbums()->updateButtons(false);
00323   if(window->getLayout()->getSubalbum() != NULL)
00324     window->getLayout()->getSubalbum()->updateButtons(false);
00325 
00326   //if album not previously saved then
00327   //run saveas dialog
00328   if(!getAlbum()->prevSave())
00329   {
00330     saveAsAlbum();
00331   }
00332   //else bring up save dialog and save
00333   else
00334   {
00335     qApp->setOverrideCursor( QCursor(Qt::WaitCursor));
00336     
00337     //pop up save dialog
00338     SaveDialog* dialog = new SaveDialog();
00339     
00340     //get size and location of application window
00341     QRect appRec = qApp->mainWidget()->geometry();
00342 
00343     //center this dialoag within application window
00344     int x, y;
00345     if(400 < appRec.width())
00346     { x = appRec.x() + ((appRec.width() - 400)/2); }
00347     else
00348     { x = appRec.x(); }
00349     if(300 < appRec.height())
00350     { y = appRec.y() + ((appRec.height() - 300)/2); }
00351     else
00352     { y = appRec.y(); }
00353     
00354     dialog->setGeometry(x, y, 400, 300);
00355     dialog->show();
00356     dialog->repaint();
00357   
00358     //sync current subalbum to ensure all photo descriptions saved
00359     if(  window->getLayout()->getSubalbum() != NULL )
00360       window->getLayout()->getSubalbum()->syncPhotos();
00361     
00362     getAlbum()->exportToDisk(dialog);
00363   
00364     //close dialog
00365     delete dialog;
00366     
00367     qApp->restoreOverrideCursor();
00368   }
00369   
00370   //nolonger busy
00371   setBusy(false);
00372   window->getLayout()->getSubalbums()->updateButtons(true);
00373   if(window->getLayout()->getSubalbum() != NULL)
00374     window->getLayout()->getSubalbum()->updateButtons(true);
00375 }
00376 //==============================================
00377 void TitleWidget::saveAsAlbum()
00378 {
00379   //get directory name in which album directory will be placed in
00380   QString dirName = QFileDialog::getExistingDirectory(NULL,
00381                                                       this,
00382                                                       "get existing directory",
00383                                                       "Choose Directory for Album Folder",
00384                                                       TRUE);
00385   //if null return
00386   if(dirName.isNull())
00387     return;
00388     
00389   //create qdir object for specified directory  
00390   QDir d(dirName);
00391   //check if readable
00392   if(!d.isReadable())
00393     return;
00394     
00395   //create directory for album in this one
00396   //used album name changing spaces to _'s
00397   QString albumName = getAlbum()->getName();
00398   
00399   //if album name is an invalid (blank or just spaces) return
00400   if(albumName.stripWhiteSpace() == "")
00401     return;
00402   
00403   albumName.replace( QChar(' '), "_" );
00404   albumName.replace( "<", "" );
00405   albumName.replace( ">", "" );
00406   albumName.replace( "&", "and" );
00407   albumName.replace( "\"", "" );
00408   albumName.replace( "\'", "" );
00409   albumName.replace( "?", "" );
00410   
00411   //if unable to create directory return
00412   if(!d.mkdir(albumName))
00413   {
00414     cout << "Error! Unable to create save folder\n";
00415     return;
00416   }
00417 
00418   qApp->setOverrideCursor( QCursor(Qt::WaitCursor));
00419   
00420   //get path of full directory now
00421   QString fullPath = d.absPath() + "/" + albumName;
00422   
00423   //pop up save dialog
00424   SaveDialog* dialog = new SaveDialog();
00425   dialog->setGeometry(200, 200, 400, 300);
00426   dialog->show();
00427   dialog->repaint();
00428   
00429   //save
00430   getAlbum()->exportToDisk(dialog, fullPath);
00431   
00432   //close dialog
00433   delete dialog;
00434   
00435   qApp->restoreOverrideCursor();
00436 }
00437 //==============================================
00438 void TitleWidget::aboutProgram()
00439 {
00440   if(about == NULL)
00441   {
00442     about = new About();
00443     connect( about, SIGNAL(aboutClosed()),
00444              this, SLOT(aboutClosed()));
00445     about->show();
00446   
00447     //get size and location of application window
00448     QRect appRec = qApp->mainWidget()->geometry();
00449     QRect aboutRec = about->geometry();
00450     
00451     //center this dialoag within application window
00452     int x, y;
00453     if(aboutRec.width() < appRec.width())
00454     { x = appRec.x() + ((appRec.width() - aboutRec.width())/2); }
00455     else
00456     { x = appRec.x(); }
00457     if(aboutRec.height() < appRec.height())
00458     { y = appRec.y() + ((appRec.height() - aboutRec.height())/2); }
00459     else
00460     { y = appRec.y(); }
00461     
00462     about->move( QPoint( x, y) );
00463     
00464     
00465   }
00466   else
00467   {
00468     about->raise();   
00469   }
00470 }
00471 //==============================================
00472 void TitleWidget::aboutClosed()
00473 {
00474   delete about;
00475   about = NULL;
00476 }
00477 //==============================================
00478 void TitleWidget::setSetButtonState(bool state)
00479 {
00480   setImage->setEnabled(state);
00481 }
00482 //==============================================
00483 bool TitleWidget::getBusy()
00484 {
00485   return busy;
00486 }
00487 //==============================================
00488 void TitleWidget::setBusy(bool val)
00489 {
00490   busy = val;
00491   
00492   //disable/enable file operations
00493   if(busy)
00494   {
00495     file->setItemEnabled(NEW_ALBUM, false);
00496     file->setItemEnabled(OPEN_ALBUM, false);
00497     file->setItemEnabled(SAVE_ALBUM, false);
00498     file->setItemEnabled(SAVEAS_ALBUM, false);
00499     cacheSetButtonState = setImage->isEnabled();
00500     setSetButtonState(false);
00501   }
00502   else
00503   {
00504     file->setItemEnabled(NEW_ALBUM, true);
00505     file->setItemEnabled(OPEN_ALBUM, true);
00506     file->setItemEnabled(SAVE_ALBUM, true);
00507     file->setItemEnabled(SAVEAS_ALBUM, true);
00508     setSetButtonState(cacheSetButtonState);
00509   }
00510 }
00511 //==============================================

Generated on Tue Jun 10 23:41:21 2003 for AlbumShaper by doxygen 1.3.1