diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 4a34529b..2d6f4ca5 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1884,7 +1884,14 @@ void UBBoardController::processMimeData(const QMimeData* pMimeData, const QPoint if (pMimeData->hasText()) { if("" != pMimeData->text()){ - mActiveScene->addText(pMimeData->text(), pPos); + // Sometimes, it is possible to have an URL as text. we check here if it is the case + QString qsTmp = pMimeData->text().remove(QRegExp("[\\0]")); + if(qsTmp.startsWith("http")){ + downloadURL(QUrl(qsTmp), pPos); + } + else{ + mActiveScene->addText(pMimeData->text(), pPos); + } } else{ #ifdef Q_WS_MACX diff --git a/src/board/UBBoardPaletteManager.cpp b/src/board/UBBoardPaletteManager.cpp index 4752491b..cc9fff7c 100644 --- a/src/board/UBBoardPaletteManager.cpp +++ b/src/board/UBBoardPaletteManager.cpp @@ -83,6 +83,7 @@ UBBoardPaletteManager::UBBoardPaletteManager(QWidget* container, UBBoardControll , mpTeacherBarWidget(NULL) , mpDesktopLibWidget(NULL) , mpDownloadWidget(NULL) + , mDownloadInProgress(false) { setupPalettes(); connectPalettes(); @@ -1009,16 +1010,18 @@ void UBBoardPaletteManager::refreshPalettes() void UBBoardPaletteManager::startDownloads() { - if(!mpDownloadWidget->isVisible()) + if(!mDownloadInProgress) { + mDownloadInProgress = true; mRightPalette->addTab(mpDownloadWidget); } } void UBBoardPaletteManager::stopDownloads() { - if(mpDownloadWidget->isVisible()) + if(mDownloadInProgress) { + mDownloadInProgress = false; mRightPalette->removeTab(mpDownloadWidget->name()); } } diff --git a/src/board/UBBoardPaletteManager.h b/src/board/UBBoardPaletteManager.h index bfab2f8b..9cbbb119 100644 --- a/src/board/UBBoardPaletteManager.h +++ b/src/board/UBBoardPaletteManager.h @@ -144,6 +144,8 @@ class UBBoardPaletteManager : public QObject // lib widget! UBLibWidget* mpDesktopLibWidget; + bool mDownloadInProgress; + private slots: void changeBackground(); diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index 4b867a33..b6eb0547 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -86,10 +86,7 @@ void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc) updateDownloadOrder(); UBApplication::mainWindow->showDownloadWidget(); } - else - { - UBApplication::boardController->paletteManager()->startDownloads(); - } + UBApplication::boardController->paletteManager()->startDownloads(); emit fileAddedToDownload(); } @@ -276,7 +273,7 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to // Verify if all downloads are finished if(mCrntDL.empty() && mPendingDL.empty()) { - emit allDownloadsFinished(); + finishDownloads(); } break; @@ -357,8 +354,7 @@ void UBDownloadManager::cancelDownloads() checkIfModalRemains(); - // Notify everyone that the downloads have been canceled. - emit cancelAllDownloads(); + finishDownloads(true); } void UBDownloadManager::onDownloadError(int id) @@ -380,6 +376,64 @@ void UBDownloadManager::onDownloadError(int id) } } +void UBDownloadManager::finishDownloads(bool cancel) +{ + UBApplication::boardController->paletteManager()->stopDownloads(); + if(cancel){ + emit cancelAllDownloads(); + } + else{ + emit allDownloadsFinished(); + } +} + +void UBDownloadManager::cancelDownload(int id) +{ + // Stop the download + mReplies[id]->abort(); + mReplies.remove(id); + + // Remove the canceled download from the download lists + bool bFound = false; + for(int i=0; i currentDownloads(); QVector pendingDownloads(); void cancelDownloads(); + void cancelDownload(int id); static void destroy(); @@ -88,14 +91,12 @@ private slots: void onDownloadError(int id); private: - UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager"); - ~UBDownloadManager(); - void init(); void updateDownloadOrder(); void updateFileCurrentSize(int id, qint64 received=-1, qint64 total=-1); void startFileDownload(sDownloadFileDesc desc); void checkIfModalRemains(); + void finishDownloads(bool cancel=false); /** The current downloads */ QVector mCrntDL; diff --git a/src/gui/UBDownloadWidget.cpp b/src/gui/UBDownloadWidget.cpp index 566e21a4..286b9f20 100644 --- a/src/gui/UBDownloadWidget.cpp +++ b/src/gui/UBDownloadWidget.cpp @@ -45,6 +45,11 @@ UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(pa mpTree = new QTreeWidget(this); mpTree->setRootIsDecorated(false); + mpTree->setColumnCount(2); + mpTree->header()->setStretchLastSection(false); + mpTree->header()->setResizeMode(eItemColumn_Desc, QHeaderView::Stretch); + mpTree->header()->setResizeMode(eItemColumn_Close, QHeaderView::Custom); + mpTree->resizeColumnToContents(eItemColumn_Close); mpTree->header()->close(); mpLayout->addWidget(mpTree, 1); @@ -59,6 +64,7 @@ UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(pa connect(UBDownloadManager::downloadManager(), SIGNAL(downloadUpdated(int,qint64,qint64)), this, SLOT(onDownloadUpdated(int,qint64,qint64))); connect(UBDownloadManager::downloadManager(), SIGNAL(downloadFinished(int)), this, SLOT(onDownloadFinished(int))); connect(mpCancelBttn, SIGNAL(clicked()), this, SLOT(onCancelClicked())); + connect(mpTree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(onItemClicked(QTreeWidgetItem*,int))); } /** @@ -111,13 +117,14 @@ void UBDownloadWidget::addCurrentDownloads() for(int i=0; isetText(0, actualDL.at(i).name); - mpItem->setData(0, Qt::UserRole, QVariant(actualDL.at(i).id)); + mpItem->setText(eItemColumn_Desc, actualDL.at(i).name); + mpItem->setData(eItemColumn_Desc, Qt::UserRole, QVariant(actualDL.at(i).id)); + mpItem->setIcon(eItemColumn_Close, QIcon(":images/close.svg")); mpTree->addTopLevelItem(mpItem); mpItem = new QTreeWidgetItem(mpTree); - mpItem->setData(0, Qt::UserRole, actualDL.at(i).currentSize); - mpItem->setData(0, Qt::UserRole + 1, actualDL.at(i).totalSize); - mpItem->setData(0, Qt::UserRole + 2, actualDL.at(i).id); + mpItem->setData(eItemColumn_Desc, Qt::UserRole, actualDL.at(i).currentSize); + mpItem->setData(eItemColumn_Desc, Qt::UserRole + 1, actualDL.at(i).totalSize); + mpItem->setData(eItemColumn_Desc, Qt::UserRole + 2, actualDL.at(i).id); mpTree->addTopLevelItem(mpItem); mpTree->setItemDelegateForRow(((i+1)*2)-1, &mProgressBarDelegate); } @@ -129,12 +136,11 @@ void UBDownloadWidget::addCurrentDownloads() void UBDownloadWidget::addPendingDownloads() { QVector pendingDL = UBDownloadManager::downloadManager()->pendingDownloads(); - qDebug() << "Pending downloads size: " << pendingDL.size(); for(int i=0; isetText(0, pendingDL.at(i).name); - mpItem->setData(0, Qt::UserRole, QVariant(pendingDL.at(i).id)); + mpItem->setText(eItemColumn_Desc, pendingDL.at(i).name); + mpItem->setData(eItemColumn_Desc, Qt::UserRole, QVariant(pendingDL.at(i).id)); mpTree->addTopLevelItem(mpItem); } } @@ -154,7 +160,7 @@ void UBDownloadWidget::onDownloadUpdated(int id, qint64 crnt, qint64 total) { for(int i=0; i< model->rowCount(); i++) { - QModelIndex currentIndex = model->index(i, 0); + QModelIndex currentIndex = model->index(i, eItemColumn_Desc); if(id == currentIndex.data(Qt::UserRole + 2)) { // We found the right item, now we update the progress bar @@ -186,6 +192,21 @@ void UBDownloadWidget::onCancelClicked() UBDownloadManager::downloadManager()->cancelDownloads(); } +/** + * \brief Handles the item click notification + * @param pItem as the item clicked + * @param col as the column containing the item clicked + */ +void UBDownloadWidget::onItemClicked(QTreeWidgetItem *pItem, int col) +{ + if( eItemColumn_Close == col + && "" != pItem->text(eItemColumn_Desc)){ + + // Stop the download of the clicked item and remove it from the list + UBDownloadManager::downloadManager()->cancelDownload(pItem->data(eItemColumn_Desc, Qt::UserRole).toInt()); + } +} + // --------------------------------------------------------------------------------------------- UBDownloadProgressDelegate::UBDownloadProgressDelegate(QObject *parent):QItemDelegate(parent) { @@ -200,5 +221,7 @@ void UBDownloadProgressDelegate::paint(QPainter *painter, const QStyleOptionView opt.maximum = index.data(Qt::UserRole + 1).toInt(); opt.progress = index.data(Qt::UserRole).toInt(); - QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0); + if(0 == index.column()){ + QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0); + } } diff --git a/src/gui/UBDownloadWidget.h b/src/gui/UBDownloadWidget.h index 0819da7c..e53df26d 100644 --- a/src/gui/UBDownloadWidget.h +++ b/src/gui/UBDownloadWidget.h @@ -25,6 +25,11 @@ #include "core/UBDownloadManager.h" +typedef enum{ + eItemColumn_Desc, + eItemColumn_Close +}eItemColumn; + class UBDownloadProgressDelegate : public QItemDelegate { Q_OBJECT @@ -47,6 +52,7 @@ private slots: void onDownloadUpdated(int id, qint64 crnt, qint64 total); void onDownloadFinished(int id); void onCancelClicked(); + void onItemClicked(QTreeWidgetItem* pItem, int col); private: void addCurrentDownloads(); diff --git a/src/gui/UBLibPathViewer.cpp b/src/gui/UBLibPathViewer.cpp index 2ebe046a..d63f95d2 100644 --- a/src/gui/UBLibPathViewer.cpp +++ b/src/gui/UBLibPathViewer.cpp @@ -457,39 +457,33 @@ void UBPathScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event) void UBPathScene::dropEvent(QGraphicsSceneDragDropEvent *event) { + bool bAccept = false; const QMimeData* pMimeData = event->mimeData(); - if(NULL != event->source() && 0 == QString::compare(event->source()->metaObject()->className(), "UBLibraryWidget")) - { + if(NULL != event->source() && 0 == QString::compare(event->source()->metaObject()->className(), "UBLibraryWidget")){ UBLibElement* pTargetElement = elementFromPos(event->scenePos()); - if(NULL != pTargetElement) - { - if(eUBLibElementType_Folder == pTargetElement->type()) - { + if(NULL != pTargetElement){ + if(eUBLibElementType_Folder == pTargetElement->type()){ // The drag comes from this application, we have now to get the list of UBLibElements* QList qlDroppedElems; foreach(QUrl url, pMimeData->urls()) qlDroppedElems << url.toString(); - if(!qlDroppedElems.empty()) - { + if(!qlDroppedElems.empty()){ // Send a signal with the target dir and the list of ublibelement* emit elementsDropped(qlDroppedElems, pTargetElement); } } } - event->accept(); + bAccept = true; } - else if(NULL != event->mimeData() && event->mimeData()->hasUrls()) - { + else if(NULL != event->mimeData() && event->mimeData()->hasUrls()){ QList urls = event->mimeData()->urls(); - foreach(QUrl eachUrl, urls) - { + foreach(QUrl eachUrl, urls){ QString sUrl = eachUrl.toString(); - if(!sUrl.startsWith("uniboardTool://") && !sUrl.startsWith("file://") && !sUrl.startsWith("/")) - { + if(!sUrl.startsWith("uniboardTool://") && !sUrl.startsWith("file://") && !sUrl.startsWith("/")){ // The dropped URL comes from the web qDebug() << "Dropped url: " << sUrl; @@ -508,9 +502,33 @@ void UBPathScene::dropEvent(QGraphicsSceneDragDropEvent *event) UBDownloadManager::downloadManager()->addFileToDownload(desc); } } + bAccept = true; } - else - { + else if(NULL != event->mimeData() && event->mimeData()->hasText()){ + // The user can only drop an Url in this location so if the text is not an Url, + // we discard it. + QString qsTxt = event->mimeData()->text().remove(QRegExp("[\\0]")); + if(qsTxt.startsWith("http")){ + // Show the download palette if it is hidden + UBApplication::boardController->paletteManager()->startDownloads(); + + // Add the dropped url to the download list + sDownloadFileDesc desc; + desc.currentSize = 0; + desc.id = 0; + desc.isBackground = false; + desc.modal = false; + desc.name = QFileInfo(qsTxt).fileName(); + desc.totalSize = 0; + desc.url = qsTxt; + UBDownloadManager::downloadManager()->addFileToDownload(desc); + bAccept = true; + } + } + if(bAccept){ + event->accept(); + } + else{ event->ignore(); } }