From 922efd33a77b0412691390a5e064f3fbabe4e983 Mon Sep 17 00:00:00 2001 From: Anna Udovichenko Date: Fri, 4 May 2012 21:35:52 +0300 Subject: [PATCH] implemented downloading pictures to library --- src/board/UBBoardPaletteManager.h | 2 +- src/board/UBFeaturesController.cpp | 21 ++++++++++- src/board/UBFeaturesController.h | 4 ++ src/gui/UBFeaturesWidget.cpp | 59 ++++++++++++++++++++++++++++++ src/gui/UBFeaturesWidget.h | 8 ++-- 5 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/board/UBBoardPaletteManager.h b/src/board/UBBoardPaletteManager.h index 86ef4355..8e9cef3e 100644 --- a/src/board/UBBoardPaletteManager.h +++ b/src/board/UBBoardPaletteManager.h @@ -44,7 +44,7 @@ class UBApplicationController; class UBDockTeacherGuideWidget; // Uncomment this to use old-styles lib paletter - #define USE_WEB_WIDGET +// #define USE_WEB_WIDGET class UBBoardPaletteManager : public QObject diff --git a/src/board/UBFeaturesController.cpp b/src/board/UBFeaturesController.cpp index 677b8333..b104c317 100644 --- a/src/board/UBFeaturesController.cpp +++ b/src/board/UBFeaturesController.cpp @@ -369,7 +369,7 @@ void UBFeaturesController::addItemAsBackground(const UBFeature &item) UBFeature UBFeaturesController::getDestinationForItem( const QUrl &url ) { - QString mimetype = UBFileSystemUtils::mimeTypeFromFileName( fileNameFromUrl(url) ); + QString mimetype = UBFileSystemUtils::mimeTypeFromFileName( url.toString() ); if ( mimetype.contains("audio") ) return audiosElement; @@ -387,6 +387,25 @@ UBFeature UBFeaturesController::getDestinationForItem( const QUrl &url ) return UBFeature(); } +UBFeature UBFeaturesController::addDownloadedFile( const QUrl &sourceUrl, const QByteArray &pData ) +{ + UBFeature dest = getDestinationForItem( sourceUrl ); + if ( dest == UBFeature() ) + return UBFeature(); + QString fileName = QFileInfo( sourceUrl.toString() ).fileName(); + QString filePath = dest.getFullPath().toLocalFile() + "/" + fileName; + + QFile file( filePath ); + if( file.open(QIODevice::WriteOnly )) + { + file.write(pData); + file.close(); + return UBFeature( dest.getFullVirtualPath(), thumbnailForFile( filePath ), + fileName, QUrl::fromLocalFile(filePath), FEATURE_ITEM ); + } + return UBFeature(); +} + UBFeature UBFeaturesController::moveItemToFolder( const QUrl &url, const UBFeature &destination ) { UBFeature newElement = copyItemToFolder( url, destination ); diff --git a/src/board/UBFeaturesController.h b/src/board/UBFeaturesController.h index 8569b499..0431eb7e 100644 --- a/src/board/UBFeaturesController.h +++ b/src/board/UBFeaturesController.h @@ -10,6 +10,7 @@ #include #include #include +#include //#include "UBDockPaletteWidget.h" @@ -75,6 +76,9 @@ public: const UBFeature& getCurrentElement()const { return currentElement; } void setCurrentElement( const UBFeature &elem ) { currentElement = elem; } const UBFeature & getTrashElement () const { return trashElement; } + + UBFeature addDownloadedFile( const QUrl &sourceUrl, const QByteArray &pData ); + UBFeature moveItemToFolder( const QUrl &url, const UBFeature &destination ); UBFeature copyItemToFolder( const QUrl &url, const UBFeature &destination ); void deleteItem( const QUrl &url ); diff --git a/src/gui/UBFeaturesWidget.cpp b/src/gui/UBFeaturesWidget.cpp index f2d686c3..c5d5cd76 100644 --- a/src/gui/UBFeaturesWidget.cpp +++ b/src/gui/UBFeaturesWidget.cpp @@ -126,6 +126,8 @@ UBFeaturesWidget::UBFeaturesWidget(QWidget *parent, const char *name):UBDockPale connect( thumbSlider, SIGNAL( sliderMoved(int) ), this, SLOT(thumbnailSizeChanged( int ) ) ); connect( UBApplication::boardController, SIGNAL( displayMetadata( QMap ) ), this, SLOT( onDisplayMetadata( QMap ) ) ); + connect( UBDownloadManager::downloadManager(), SIGNAL( addDownloadedFileToLibrary( bool, QUrl, QString, QByteArray ) ), + this, SLOT( onAddDownloadedFileToLibrary( bool, QUrl, QString,QByteArray ) ) ); } bool UBFeaturesWidget::eventFilter( QObject *target, QEvent *event ) @@ -317,6 +319,20 @@ void UBFeaturesWidget::onDisplayMetadata( QMap metadata ) mActionBar->setCurrentState( IN_PROPERTIES ); } +void UBFeaturesWidget::onAddDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentHeader, QByteArray pData) +{ + if ( pSuccess ) + { + UBFeature newFeature = controller->addDownloadedFile( sourceUrl, pData ); + if ( newFeature != UBFeature() ) + { + featuresModel->addItem( newFeature ); + QSortFilterProxyModel *model = dynamic_cast( featuresListView->model() ); + model->invalidate(); + } + } +} + void UBFeaturesWidget::switchToListView() { stackedWidget->setCurrentIndex(ID_LISTVIEW); @@ -557,7 +573,34 @@ UBFeatureProperties::UBFeatureProperties( QWidget *parent, const char *name ) : connect( mpAddPageButton, SIGNAL(clicked()), this, SLOT(onAddToPage()) ); connect( mpSetAsBackgroundButton, SIGNAL( clicked() ), this, SLOT( onSetAsBackground() ) ); + connect( mpAddToLibButton, SIGNAL( clicked() ), this, SLOT(onAddToLib() ) ); +} + +void UBFeatureProperties::resizeEvent( QResizeEvent *event ) +{ + Q_UNUSED(event); + adaptSize(); +} + +void UBFeatureProperties::showEvent (QShowEvent *event ) +{ + Q_UNUSED(event); + adaptSize(); +} +void UBFeatureProperties::adaptSize() +{ + if( NULL != mpOrigPixmap ) + { + if( width() < THUMBNAIL_WIDTH + 40 ) + { + mpThumbnail->setPixmap( mpOrigPixmap->scaledToWidth( width() - 40 ) ); + } + else + { + mpThumbnail->setPixmap( mpOrigPixmap->scaledToWidth( THUMBNAIL_WIDTH ) ); + } + } } void UBFeatureProperties::showElement( const UBFeature &elem ) @@ -629,6 +672,22 @@ void UBFeatureProperties::onAddToPage() featuresWidget->getFeaturesController()->addItemToPage( *mpElement ); } +void UBFeatureProperties::onAddToLib() +{ + if ( UBApplication::isFromWeb( mpElement->getFullPath().toString() ) ) + { + sDownloadFileDesc desc; + desc.isBackground = false; + desc.modal = false; + desc.name = QFileInfo( mpElement->getFullPath().toString()).fileName(); + qDebug() << desc.name; + desc.url = mpElement->getFullPath().toString(); + qDebug() << desc.url; + UBDownloadManager::downloadManager()->addFileToDownload(desc); + } +} + + void UBFeatureProperties::onSetAsBackground() { QWidget *w = parentWidget()->parentWidget(); diff --git a/src/gui/UBFeaturesWidget.h b/src/gui/UBFeaturesWidget.h index a76584b7..d9e77a4d 100644 --- a/src/gui/UBFeaturesWidget.h +++ b/src/gui/UBFeaturesWidget.h @@ -100,6 +100,7 @@ private slots: void removeFromFavorite( const QMimeData & ); void thumbnailSizeChanged( int ); void onDisplayMetadata( QMap ); + void onAddDownloadedFileToLibrary(bool, QUrl, QString, QByteArray); protected: bool eventFilter(QObject *target, QEvent *event); }; @@ -151,17 +152,18 @@ public: protected: - //void resizeEvent(QResizeEvent *event); - //void showEvent(QShowEvent *event); + void resizeEvent(QResizeEvent *event); + void showEvent(QShowEvent *event); private slots: void onAddToPage(); - //void onAddToLib(); + void onAddToLib(); void onSetAsBackground(); //void onBack(); private: void populateMetadata(); + void adaptSize(); QVBoxLayout* mpLayout; QHBoxLayout* mpButtonLayout;