qtmobility/examples/flickrdemo/flickrdemo.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    48 #include <qgeopositioninfo.h>
    48 #include <qgeopositioninfo.h>
    49 #include <qgeosatelliteinfo.h>
    49 #include <qgeosatelliteinfo.h>
    50 #include <qnetworkconfigmanager.h>
    50 #include <qnetworkconfigmanager.h>
    51 #include <qnetworksession.h>
    51 #include <qnetworksession.h>
    52 
    52 
       
    53 #include <QAction>
       
    54 #include <QApplication>
       
    55 #include <QDialogButtonBox>
       
    56 #include <QDir>
       
    57 #include <QFile>
       
    58 #include <QLabel>
       
    59 #include <QMenuBar>
       
    60 #include <QMessageBox>
       
    61 #include <QNetworkAccessManager>
       
    62 #include <QNetworkRequest>
       
    63 #include <QProgressDialog>
       
    64 #include <QPushButton>
       
    65 #include <QTimer>
       
    66 #include <QVBoxLayout>
       
    67 #include <QWidget>
       
    68 
    53 // static constant intialization
    69 // static constant intialization
    54 
    70 
    55 const QSize FlickrDemo::gridSize = QSize(52, 52);
    71 const QSize FlickrDemo::gridSize = QSize(52, 52);
    56 const QSize FlickrDemo::thumbnailSize = QSize(50, 50);
    72 const QSize FlickrDemo::thumbnailSize = QSize(50, 50);
    57 const QSize FlickrDemo::imageSize = QSize(150, 150);
    73 const QSize FlickrDemo::imageSize = QSize(150, 150);
    58 const QString FlickrDemo::apikey = QString("e36784df8a03fea04c22ed93318b291c");
    74 const QString FlickrDemo::apikey = QString("e36784df8a03fea04c22ed93318b291c");
    59 #ifdef Q_OS_SYMBIAN
       
    60 const QString FlickrDemo::savePath = "c:\\Data\\Images\\"; // In S60 Download images to Gallery
       
    61 #else
       
    62 const QString FlickrDemo::savePath = QDir::tempPath();
       
    63 #endif
       
    64 
    75 
    65 FlickrDemo::FlickrDemo(QWidget* parent) :
    76 FlickrDemo::FlickrDemo(QWidget* parent) :
    66         QMainWindow(parent),
    77         QMainWindow(parent),
    67         m_logfileInUse(false),
    78         m_logfileInUse(false),
    68         m_session(0),
    79         m_session(0),
    69         m_file(0),
    80         m_pictureListReply(0),
    70         m_httpGetId(-1),
    81         m_thumbnailReply(0),
    71         m_httpThumbnailGetId(-1),
    82         m_pictureReply(0),
    72         m_pages(0),
    83         m_pages(0),
    73         m_page(1),
    84         m_page(1),
    74         m_satellitesInView(0),
    85         m_satellitesInView(0),
    75         m_satellitesUsed(0),
    86         m_satellitesUsed(0),
    76         m_latitude(-1000),
    87         m_latitude(-1000),
    77         m_longitude(-1000),
    88         m_longitude(-1000),
    78         m_downloadPictureList(true),
    89         m_downloadPictureList(true),
    79         m_downloadingThumbnails(false)
    90         m_shuttingDown(false)
    80 {
    91 {
    81     resize(252, 344);
    92     resize(252, 344);
    82 
    93 
    83     locationLabel = new QLabel(tr("Lat: Long:"));
    94     locationLabel = new QLabel(tr("Lat: Long:"));
    84     satellitesLabel = new QLabel(tr("Using 0 of 0 satellites"));
    95     satellitesLabel = new QLabel(tr("Using 0 of 0 satellites"));
   130                 this, SLOT(satellitesInViewUpdated(const QList<QGeoSatelliteInfo>&)));
   141                 this, SLOT(satellitesInViewUpdated(const QList<QGeoSatelliteInfo>&)));
   131         connect(m_satellite, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)),
   142         connect(m_satellite, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)),
   132                 this, SLOT(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)));
   143                 this, SLOT(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)));
   133     }
   144     }
   134 
   145 
   135     // QHttp
   146     m_nam = new QNetworkAccessManager(this);
   136     m_http = new QHttp(this);
       
   137     connect(m_http, SIGNAL(requestFinished(int, bool)),
       
   138             this, SLOT(httpRequestFinished(int, bool)));
       
   139     connect(m_http, SIGNAL(dataReadProgress(int, int)),
       
   140             this, SLOT(updateDataReadProgress(int, int)));
       
   141     connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)),
       
   142             this, SLOT(readResponseHeader(const QHttpResponseHeader&)));
       
   143 
   147 
   144     QTimer::singleShot(0, this, SLOT(delayedInit()));
   148     QTimer::singleShot(0, this, SLOT(delayedInit()));
   145 }
   149 }
   146 
   150 
   147 FlickrDemo::~FlickrDemo()
   151 FlickrDemo::~FlickrDemo()
   148 {
   152 {
       
   153     m_shuttingDown = true;
       
   154 
   149     m_location->stopUpdates();
   155     m_location->stopUpdates();
   150     if (m_satellite)
   156     if (m_satellite)
   151         m_satellite->stopUpdates();
   157         m_satellite->stopUpdates();
   152     m_http->abort();
   158 
       
   159     if (m_pictureListReply) {
       
   160         m_pictureListReply->abort();
       
   161         delete m_pictureListReply;
       
   162     }
       
   163     if (m_thumbnailReply) {
       
   164         m_thumbnailReply->abort();
       
   165         delete m_thumbnailReply;
       
   166     }
       
   167     if (m_pictureReply) {
       
   168         m_pictureReply->abort();
       
   169         delete m_pictureReply;
       
   170     }
       
   171 
   153     if (m_session)
   172     if (m_session)
   154         m_session->close();
   173         m_session->close();
   155 }
   174 }
   156 
   175 
   157 void FlickrDemo::delayedInit()
   176 void FlickrDemo::delayedInit()
   159     if (m_logfileInUse) {
   178     if (m_logfileInUse) {
   160         QMessageBox::information(this, tr("Flickr Demo"),
   179         QMessageBox::information(this, tr("Flickr Demo"),
   161                                  tr("No GPS support detected, using GPS data from a sample log file instead."));
   180                                  tr("No GPS support detected, using GPS data from a sample log file instead."));
   162     }
   181     }
   163 
   182 
   164     QNetworkConfigurationManager manager;
   183     QTM_PREPEND_NAMESPACE(QNetworkConfigurationManager) manager;
   165     const bool canStartIAP = (manager.capabilities()
   184     const bool canStartIAP = (manager.capabilities()
   166                               & QNetworkConfigurationManager::CanStartAndStopInterfaces);
   185                               & QTM_PREPEND_NAMESPACE(QNetworkConfigurationManager)::CanStartAndStopInterfaces);
   167     QNetworkConfiguration cfg = manager.defaultConfiguration();
   186     QTM_PREPEND_NAMESPACE(QNetworkConfiguration) cfg = manager.defaultConfiguration();
   168     if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
   187     if (!cfg.isValid() || (!canStartIAP && cfg.state() != QTM_PREPEND_NAMESPACE(QNetworkConfiguration)::Active)) {
   169         QMessageBox::information(this, tr("Flickr Demo"), tr("Available Access Points not found."));
   188         QMessageBox::information(this, tr("Flickr Demo"), tr("Available Access Points not found."));
   170         return;
   189         return;
   171     }
   190     }
   172 
   191 
   173     m_session = new QNetworkSession(cfg, this);
   192     m_session = new QNetworkSession(cfg, this);
   280     urlstring.append("&page=");
   299     urlstring.append("&page=");
   281     urlstring.append(QString::number(m_page));
   300     urlstring.append(QString::number(m_page));
   282 
   301 
   283     QUrl url(urlstring);
   302     QUrl url(urlstring);
   284 
   303 
   285     m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
   304     QNetworkRequest req(url);
   286     m_httpRequestAborted = false;
   305     m_pictureListReply = m_nam->get(req);
   287 
   306     connect(m_pictureListReply,
   288     m_httpGetId = m_http->get(urlstring);
   307             SIGNAL(downloadProgress(qint64, qint64)),
       
   308             this,
       
   309             SLOT(pictureListDownloadProgress(qint64, qint64)));
       
   310     connect(m_pictureListReply,
       
   311             SIGNAL(finished()),
       
   312             this,
       
   313             SLOT(pictureListFinished()));
       
   314     connect(m_pictureListReply,
       
   315             SIGNAL(error(QNetworkReply::NetworkError)),
       
   316             this,
       
   317             SLOT(pictureListError(QNetworkReply::NetworkError)));
   289 
   318 
   290     m_progressDialog->setWindowTitle(tr("FlickrDemo"));
   319     m_progressDialog->setWindowTitle(tr("FlickrDemo"));
   291     m_progressDialog->setLabelText(tr("Downloading\nPicture List."));
   320     m_progressDialog->setLabelText(tr("Downloading\nPicture List."));
   292     m_progressDialog->setMaximum(10);
   321     m_progressDialog->setMaximum(10);
   293     m_progressDialog->setValue(0);
   322     m_progressDialog->setValue(0);
   364     }
   393     }
   365     QString pictureUrl = item->data(Qt::UserRole).toString();
   394     QString pictureUrl = item->data(Qt::UserRole).toString();
   366     pictureUrl.append("_m.jpg");
   395     pictureUrl.append("_m.jpg");
   367 
   396 
   368     QUrl url(pictureUrl);
   397     QUrl url(pictureUrl);
   369     QFileInfo fileInfo(url.path());
   398 
   370     QString fileName = fileInfo.fileName();
   399     QNetworkRequest req(url);
   371     if (fileName.isEmpty()) {
   400     m_pictureReply = m_nam->get(req);
   372         fileName = "test.jpg";
   401     connect(m_pictureReply,
   373     }
   402             SIGNAL(downloadProgress(qint64, qint64)),
   374 
   403             this,
   375     m_filePath = savePath;
   404             SLOT(pictureDownloadProgress(qint64, qint64)));
   376     m_filePath.append(fileName);
   405     connect(m_pictureReply,
   377 
   406             SIGNAL(finished()),
   378     if (QFile::exists(m_filePath)) {
   407             this,
   379         if (QMessageBox::question(this,
   408             SLOT(pictureFinished()));
   380                                   tr("Flickr Demo"),
   409     connect(m_pictureReply,
   381                                   tr("File %1 is already downloaded."
   410             SIGNAL(error(QNetworkReply::NetworkError)),
   382                                      "Overwrite?").arg(fileName),
   411             this,
   383                                   QMessageBox::Yes | QMessageBox::No,
   412             SLOT(pictureError(QNetworkReply::NetworkError)));
   384                                   QMessageBox::No)
       
   385                 == QMessageBox::No) {
       
   386             displayImage();
       
   387             return;
       
   388         }
       
   389         QFile::remove(m_filePath);
       
   390     }
       
   391 
       
   392     m_file = new QFile(m_filePath);
       
   393     if (!m_file->open(QIODevice::WriteOnly)) {
       
   394         QMessageBox::information(this, tr("Flickr Demo"),
       
   395                                  tr("Unable to save the file %1: %2.").arg(m_filePath).arg(m_file->errorString()));
       
   396         delete m_file;
       
   397         m_file = 0;
       
   398         return;
       
   399     }
       
   400 
       
   401     m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
       
   402 
       
   403     m_httpRequestAborted = false;
       
   404     QByteArray encodedUrl = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
       
   405     if (encodedUrl.isEmpty()) {
       
   406         encodedUrl = "/";
       
   407     }
       
   408     m_httpGetId = m_http->get(encodedUrl, m_file);
       
   409 
   413 
   410     m_progressDialog->setWindowTitle(tr("Flickr Demo"));
   414     m_progressDialog->setWindowTitle(tr("Flickr Demo"));
   411     m_progressDialog->setLabelText(tr("Downloading:\n%1").arg(fileName));
   415     m_progressDialog->setLabelText(tr("Downloading:\n%1").arg(pictureUrl));
   412     m_progressDialog->setMaximum(10);
   416     m_progressDialog->setMaximum(10);
   413     m_progressDialog->setValue(0);
   417     m_progressDialog->setValue(0);
   414     m_progressDialog->show();
   418     m_progressDialog->show();
   415 
   419 
   416     downloadButton->setEnabled(false);
   420     downloadButton->setEnabled(false);
   417 }
   421 }
   418 
   422 
   419 void FlickrDemo::cancelDownload()
   423 void FlickrDemo::cancelDownload()
   420 {
   424 {
   421     m_httpRequestAborted = true;
   425     if (m_pictureListReply) {
   422     m_downloadingThumbnails = false;
   426         m_pictureListReply->abort();
   423     m_http->abort();
   427         delete m_pictureListReply;
       
   428         m_pictureListReply = 0;
       
   429     }
       
   430 
       
   431     if (m_pictureReply) {
       
   432         m_pictureReply->abort();
       
   433         delete m_pictureReply;
       
   434         m_pictureReply = 0;
       
   435     }
       
   436 
   424     downloadButton->setEnabled(true);
   437     downloadButton->setEnabled(true);
   425 }
   438 }
   426 
   439 
   427 void FlickrDemo::httpRequestFinished(int requestId, bool error)
   440 void FlickrDemo::pictureListDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
   428 {
   441 {
   429     if (m_downloadingThumbnails && m_httpThumbnailGetId == requestId) {
   442     m_progressDialog->setMaximum(bytesTotal);
   430         if (!error) {
   443     m_progressDialog->setValue(bytesReceived);
   431             QByteArray picture = m_http->readAll();
   444 }
   432             if (!picture.isNull() && picture.size() > 0) {
   445 
   433                 QListWidgetItem* item = listWidget->item(m_nameCounter);
   446 void FlickrDemo::pictureListFinished()
   434                 QImage image;
   447 {
   435                 if (image.loadFromData(picture, "jpg")) {
   448     m_progressDialog->hide();
   436                     item->setIcon(QPixmap::fromImage(image.scaled(thumbnailSize,
   449     if (parsePictureList(QString::fromUtf8(m_pictureListReply->readAll()))) {
   437                                                      Qt::KeepAspectRatio, Qt::SmoothTransformation)));
   450         m_downloadPictureList = false;
   438                     listWidget->update();
   451         downloadButton->setText(tr("Download Selected Picture"));
   439                 }
   452         m_downloadAct->setText(tr("Download Selected Picture"));
   440             }
   453     }
       
   454 
       
   455     downloadButton->setEnabled(true);
       
   456 
       
   457     QTimer::singleShot(0, this, SLOT(clearPictureListRequest()));
       
   458 }
       
   459 
       
   460 void FlickrDemo::pictureListError(QNetworkReply::NetworkError code)
       
   461 {
       
   462     if (m_shuttingDown)
       
   463         return;
       
   464 
       
   465     m_progressDialog->hide();
       
   466     QMessageBox::information(this,
       
   467                              tr("Flickr Demo"),
       
   468                              tr("Error downloading picture list: %1.").arg(m_pictureListReply->errorString()));
       
   469 
       
   470     QTimer::singleShot(0, this, SLOT(clearPictureListRequest()));
       
   471 }
       
   472 
       
   473 void FlickrDemo::clearPictureListRequest()
       
   474 {
       
   475     delete m_pictureListReply;
       
   476     m_pictureListReply = 0;
       
   477 }
       
   478 
       
   479 void FlickrDemo::thumbnailFinished()
       
   480 {
       
   481     QByteArray picture = m_thumbnailReply->readAll();
       
   482     if (!picture.isNull() && picture.size() > 0) {
       
   483         QListWidgetItem* item = listWidget->item(m_nameCounter);
       
   484         QImage image;
       
   485         if (image.loadFromData(picture, "jpg")) {
       
   486             item->setIcon(QPixmap::fromImage(
       
   487                               image.scaled(thumbnailSize,
       
   488                                            Qt::KeepAspectRatio,
       
   489                                            Qt::SmoothTransformation)));
       
   490             listWidget->update();
   441         }
   491         }
   442         downloadNextThumbnail();
   492     }
       
   493     downloadNextThumbnail();
       
   494 }
       
   495 
       
   496 void FlickrDemo::thumbnailError(QNetworkReply::NetworkError code)
       
   497 {
       
   498     if (m_shuttingDown)
   443         return;
   499         return;
   444     }
   500 
   445 
   501     QMessageBox::information(this,
   446     if (requestId != m_httpGetId) {
   502                              tr("Flickr Demo"),
       
   503                              tr("Error downloading thumbnails: %1.").arg(m_thumbnailReply->errorString()));
       
   504 
       
   505     QTimer::singleShot(0, this, SLOT(clearThumbnailRequest()));
       
   506 }
       
   507 
       
   508 void FlickrDemo::clearThumbnailRequest()
       
   509 {
       
   510     delete m_thumbnailReply;
       
   511     m_thumbnailReply = 0;
       
   512 }
       
   513 
       
   514 void FlickrDemo::pictureDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
       
   515 {
       
   516     m_progressDialog->setMaximum(bytesTotal);
       
   517     m_progressDialog->setValue(bytesReceived);
       
   518 }
       
   519 
       
   520 void FlickrDemo::pictureFinished()
       
   521 {
       
   522     m_progressDialog->hide();
       
   523     downloadButton->setEnabled(true);
       
   524 
       
   525     QByteArray picture = m_pictureReply->readAll();
       
   526 
       
   527     if (picture.isNull() || picture.size() <= 0)
   447         return;
   528         return;
   448     }
   529 
       
   530     QImage image;
       
   531     if (!image.loadFromData(picture, "jpg"))
       
   532         return;
       
   533 
       
   534     QPixmap pixmap = QPixmap::fromImage(
       
   535                          image.scaled(imageSize,
       
   536                                       Qt::KeepAspectRatio,
       
   537                                       Qt::SmoothTransformation));
       
   538 
       
   539     displayImage(pixmap);
       
   540 
       
   541     QTimer::singleShot(0, this, SLOT(clearPictureRequest()));
       
   542 }
       
   543 
       
   544 void FlickrDemo::pictureError(QNetworkReply::NetworkError code)
       
   545 {
       
   546     if (m_shuttingDown)
       
   547         return;
   449 
   548 
   450     m_progressDialog->hide();
   549     m_progressDialog->hide();
   451 
   550     QMessageBox::information(this,
   452     if (m_httpRequestAborted) {
   551                              tr("Flickr Demo"),
   453         if (m_file) {
   552                              tr("Error downloading picture: %1.").arg(m_pictureReply->errorString()));
   454             m_file->close();
       
   455             m_file->remove();
       
   456             delete m_file;
       
   457             m_file = 0;
       
   458         }
       
   459 
       
   460         return;
       
   461     }
       
   462 
       
   463     if (!m_downloadPictureList && m_file) {
       
   464         m_file->close();
       
   465     }
       
   466 
       
   467     if (error) {
       
   468         if (!m_downloadPictureList && m_file) {
       
   469             m_file->remove();
       
   470         }
       
   471         QMessageBox::information(this,
       
   472                                  tr("Flickr Demo"),
       
   473                                  tr("Download failed: %1.").arg(m_http->errorString()));
       
   474     }
       
   475 
       
   476     if (m_downloadPictureList) {
       
   477         if (parsePictureList(QString::fromUtf8(m_http->readAll()))) {
       
   478             m_downloadPictureList = false;
       
   479             downloadButton->setText(tr("Download Selected Picture"));
       
   480             m_downloadAct->setText(tr("Download Selected Picture"));
       
   481         }
       
   482     } else {
       
   483         displayImage();
       
   484     }
       
   485 
   553 
   486     downloadButton->setEnabled(true);
   554     downloadButton->setEnabled(true);
   487 }
   555 
   488 
   556     QTimer::singleShot(0, this, SLOT(clearPictureRequest()));
   489 void FlickrDemo::displayImage()
   557 }
   490 {
   558 
   491     PictureDialog dialog(m_filePath, listWidget->currentItem()->text(), this);
   559 void FlickrDemo::clearPictureRequest()
       
   560 {
       
   561     delete m_pictureReply;
       
   562     m_pictureReply = 0;
       
   563 }
       
   564 
       
   565 void FlickrDemo::displayImage(const QPixmap &pixmap)
       
   566 {
       
   567     PictureDialog dialog(pixmap, listWidget->currentItem()->text(), this);
   492 #if defined(Q_OS_SYMBIAN) || defined (Q_OS_WINCE)
   568 #if defined(Q_OS_SYMBIAN) || defined (Q_OS_WINCE)
   493     dialog.showMaximized();
   569     dialog.showMaximized();
   494 #endif
   570 #endif
   495     if (!dialog.exec()) {
   571     dialog.exec();
   496         if (m_file && m_file->exists()) {
       
   497             m_file->remove();
       
   498         }
       
   499     }
       
   500     if(m_file)
       
   501         delete m_file;
       
   502     m_file = 0;
       
   503 }
       
   504 
       
   505 void FlickrDemo::readResponseHeader(const QHttpResponseHeader& responseHeader)
       
   506 {
       
   507     switch (responseHeader.statusCode()) {
       
   508         case 200: // Ok
       
   509         case 301: // Moved Permanently
       
   510         case 302: // Found
       
   511         case 303: // See Other
       
   512         case 307: // Temporary Redirect
       
   513             // these are not error conditions
       
   514             break;
       
   515         default:
       
   516             QMessageBox::information(this,
       
   517                                      tr("Flickr Demo"),
       
   518                                      tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
       
   519             m_downloadingThumbnails = false;
       
   520             m_httpRequestAborted = true;
       
   521             m_progressDialog->hide();
       
   522             m_http->abort();
       
   523     }
       
   524 }
       
   525 
       
   526 void FlickrDemo::updateDataReadProgress(int bytesRead, int totalBytes)
       
   527 {
       
   528     if (m_httpRequestAborted) {
       
   529         return;
       
   530     }
       
   531 
       
   532     if (!m_downloadingThumbnails) {
       
   533         m_progressDialog->setMaximum(totalBytes);
       
   534         m_progressDialog->setValue(bytesRead);
       
   535     }
       
   536 }
   572 }
   537 
   573 
   538 void FlickrDemo::downloadNextThumbnail()
   574 void FlickrDemo::downloadNextThumbnail()
   539 {
   575 {
   540     m_nameCounter++;
   576     m_nameCounter++;
   541     if (m_nameCounter < m_names.count()) {
   577     if (m_nameCounter < m_names.count()) {
   542         QString pictureUrl = m_names[m_nameCounter];
   578         QString pictureUrl = m_names[m_nameCounter];
   543         pictureUrl.append("_s.jpg");
   579         pictureUrl.append("_s.jpg");
   544         QUrl url(pictureUrl);
   580         QUrl url(pictureUrl);
   545         m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
   581 
   546         m_downloadingThumbnails = true;
   582         QNetworkRequest req(url);
   547         m_httpThumbnailGetId = m_http->get(pictureUrl);
   583         m_thumbnailReply = m_nam->get(req);
       
   584         connect(m_thumbnailReply,
       
   585                 SIGNAL(finished()),
       
   586                 this,
       
   587                 SLOT(thumbnailFinished()));
       
   588         connect(m_thumbnailReply,
       
   589                 SIGNAL(error(QNetworkReply::NetworkError)),
       
   590                 this,
       
   591                 SLOT(thumbnailError(QNetworkReply::NetworkError)));
   548     } else {
   592     } else {
   549         m_downloadingThumbnails = false;
   593         QTimer::singleShot(0, this, SLOT(clearThumbnailRequest()));
   550     }
   594     }
   551 }
   595 }
   552 
   596 
   553 // static constant intialization
   597 PictureDialog::PictureDialog(const QPixmap& pixmap, const QString& pictureName, QWidget* parent) :
   554 
       
   555 const QSize PictureDialog::imageSize = QSize(150, 150);
       
   556 
       
   557 PictureDialog::PictureDialog(const QString& filePath, const QString& pictureName, QWidget* parent) :
       
   558         QDialog(parent)
   598         QDialog(parent)
   559 {
   599 {
   560     resize(252, 361);
   600     resize(252, 361);
   561     QVBoxLayout *verticalLayout = new QVBoxLayout();
   601     QVBoxLayout *verticalLayout = new QVBoxLayout();
   562     verticalLayout->setSpacing(6);
   602     verticalLayout->setSpacing(6);
   563     verticalLayout->setContentsMargins(11, 11, 11, 11);
   603     verticalLayout->setContentsMargins(11, 11, 11, 11);
   564 
   604 
   565     label = new QLabel();
   605     label = new QLabel();
   566     QString fileName = QFileInfo(filePath).fileName();
   606     label->setText(tr("Downloaded:\n%1").arg(pictureName));
   567     label->setText(tr("Downloaded:\n%1\n%2").arg(pictureName).arg(fileName));
       
   568 
   607 
   569     QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
   608     QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
   570     sizePolicy.setHorizontalStretch(0);
   609     sizePolicy.setHorizontalStretch(0);
   571     sizePolicy.setVerticalStretch(0);
   610     sizePolicy.setVerticalStretch(0);
   572     sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
   611     sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
   573     label->setSizePolicy(sizePolicy);
   612     label->setSizePolicy(sizePolicy);
   574 
   613 
   575     verticalLayout->addWidget(label);
   614     verticalLayout->addWidget(label);
   576 
   615 
   577     imageLabel = new QLabel();
   616     imageLabel = new QLabel();
   578     QImage image;
   617     imageLabel->setPixmap(pixmap);
   579     image.load(filePath);
       
   580     imageLabel->setPixmap(QPixmap::fromImage(image.scaled(imageSize, Qt::KeepAspectRatio,
       
   581                           Qt::SmoothTransformation)));
       
   582 
   618 
   583     verticalLayout->addWidget(imageLabel);
   619     verticalLayout->addWidget(imageLabel);
   584 
   620 
   585     keepButton = new QPushButton(tr("Keep"));
       
   586     keepButton->setDefault(true);
       
   587     discardButton = new QPushButton(tr("Discard"));
       
   588 
       
   589     buttonBox = new QDialogButtonBox();
   621     buttonBox = new QDialogButtonBox();
   590     buttonBox->addButton(keepButton, QDialogButtonBox::AcceptRole);
   622     buttonBox->setStandardButtons(QDialogButtonBox::Close);
   591     buttonBox->addButton(discardButton, QDialogButtonBox::DestructiveRole);
   623     connect(buttonBox, SIGNAL(rejected()), this, SLOT(accept()));
   592     connect(buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(clicked(QAbstractButton *)));
       
   593 
   624 
   594     verticalLayout->addWidget(buttonBox);
   625     verticalLayout->addWidget(buttonBox);
   595 
   626 
   596     setLayout(verticalLayout);
   627     setLayout(verticalLayout);
   597 
   628 
   598     setWindowTitle(tr("Flickr Demo"));
   629     setWindowTitle(tr("Flickr Demo"));
   599 }
   630 }
   600 
       
   601 void PictureDialog::clicked(QAbstractButton* button)
       
   602 {
       
   603     if (button == keepButton) {
       
   604         accept();
       
   605     } else if (button == discardButton) {
       
   606         reject();
       
   607     }
       
   608 }