From bfcaace42e3f65b1dd9cd14f1f84247650842b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Wed, 17 Nov 2021 18:57:52 +0100 Subject: [PATCH] fixed a lot of issues regarding thumbnails --- src/board/UBBoardController.cpp | 29 +++++- src/core/UBApplicationController.cpp | 11 +- src/core/UBPersistenceManager.cpp | 2 +- src/document/UBDocumentContainer.cpp | 58 +++++------ src/document/UBDocumentContainer.h | 27 ++--- src/document/UBDocumentController.cpp | 49 +++++++-- src/document/UBDocumentController.h | 5 +- src/gui/UBDocumentNavigator.cpp | 143 ++++++++++++++++++++------ src/gui/UBDocumentNavigator.h | 6 +- src/gui/UBDocumentThumbnailWidget.cpp | 43 ++++++++ src/gui/UBDocumentThumbnailWidget.h | 3 + src/gui/UBThumbnailWidget.cpp | 12 ++- src/gui/UBThumbnailWidget.h | 20 +++- 13 files changed, 308 insertions(+), 100 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 8ba62e58..6ab9bd9a 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -521,6 +521,10 @@ void UBBoardController::addScene() persistCurrentScene(false,true); UBDocumentContainer::addPage(mActiveSceneIndex + 1); + if (UBApplication::documentController->selectedDocument() == selectedDocument()) + { + UBApplication::documentController->insertThumbPage(mActiveSceneIndex+1); + } selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); @@ -584,7 +588,11 @@ void UBBoardController::duplicateScene(int nIndex) scIndexes << nIndex; duplicatePages(scIndexes); insertThumbPage(nIndex); - emit documentThumbnailsUpdated(this); + if (UBApplication::documentController->selectedDocument() == selectedDocument()) + { + UBApplication::documentController->insertThumbPage(nIndex); + } + //emit documentThumbnailsUpdated(this); emit addThumbnailRequired(this, nIndex + 1); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); @@ -779,6 +787,10 @@ void UBBoardController::deleteScene(int nIndex) QList scIndexes; scIndexes << nIndex; deletePages(scIndexes); + if (UBApplication::documentController->selectedDocument() == selectedDocument()) + { + UBApplication::documentController->deleteThumbPage(nIndex); + } selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); UBMetadataDcSubsetAdaptor::persist(selectedDocument()); @@ -1618,7 +1630,12 @@ void UBBoardController::moveSceneToIndex(int source, int target) { persistCurrentScene(false,true); - UBDocumentContainer::movePageToIndex(source, target); + UBPersistenceManager::persistenceManager()->moveSceneToIndex(selectedDocument(), source, target); + UBDocumentContainer::moveThumbPage(source, target); + if (UBApplication::documentController->selectedDocument() == selectedDocument()) + { + UBApplication::documentController->moveThumbPage(source, target); + } selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); UBPersistenceManager::persistenceManager()->persistDocumentMetadata(selectedDocument()); @@ -1877,7 +1894,7 @@ void UBBoardController::documentSceneChanged(UBDocumentProxy* pDocumentProxy, in if(selectedDocument() == pDocumentProxy) { setActiveDocumentScene(mActiveSceneIndex); - updatePage(pIndex); + updateThumbPage(pIndex); } } @@ -2021,7 +2038,11 @@ void UBBoardController::persistCurrentScene(bool isAnAutomaticBackup, bool force && (mActiveScene->isModified())) { UBPersistenceManager::persistenceManager()->persistDocumentScene(selectedDocument(), mActiveScene, mActiveSceneIndex, isAnAutomaticBackup); - updatePage(mActiveSceneIndex); + updateThumbPage(mActiveSceneIndex); + if (UBApplication::documentController->selectedDocument() == selectedDocument()) + { + UBApplication::documentController->updateThumbPage(mActiveSceneIndex); + } } } diff --git a/src/core/UBApplicationController.cpp b/src/core/UBApplicationController.cpp index 480afb65..27f558c5 100644 --- a/src/core/UBApplicationController.cpp +++ b/src/core/UBApplicationController.cpp @@ -351,11 +351,11 @@ void UBApplicationController::showBoard() if (mMainMode == Document) { - int selectedSceneIndex = UBApplication::documentController->getSelectedItemIndex(); - if (selectedSceneIndex != -1) - { - UBApplication::boardController->setActiveDocumentScene(UBApplication::documentController->selectedDocument(), selectedSceneIndex); - } +// int selectedSceneIndex = UBApplication::documentController->getSelectedItemIndex(); +// if (selectedSceneIndex != -1) +// { +// UBApplication::boardController->setActiveDocumentScene(UBApplication::documentController->selectedDocument(), selectedSceneIndex); +// } } mMainMode = Board; @@ -435,6 +435,7 @@ void UBApplicationController::showDocument() { if (UBApplication::boardController->activeScene()->isModified()) UBApplication::boardController->persistCurrentScene(); + UBApplication::boardController->hide(); } diff --git a/src/core/UBPersistenceManager.cpp b/src/core/UBPersistenceManager.cpp index ae4c2180..2aec77db 100644 --- a/src/core/UBPersistenceManager.cpp +++ b/src/core/UBPersistenceManager.cpp @@ -827,7 +827,6 @@ UBGraphicsScene* UBPersistenceManager::createDocumentSceneAt(UBDocumentProxy* pr for(int i = count - 1; i >= index; i--) { - UBApplication::showMessage(tr("renaming pages (%1/%2)").arg(i).arg(count)); renamePage(proxy, i , i + 1); } @@ -977,6 +976,7 @@ UBDocumentProxy* UBPersistenceManager::persistDocumentMetadata(UBDocumentProxy* void UBPersistenceManager::renamePage(UBDocumentProxy* pDocumentProxy, const int sourceIndex, const int targetIndex) { + UBApplication::showMessage(tr("renaming pages (%1/%2)").arg(sourceIndex).arg(pDocumentProxy->pageCount())); QFile svg(pDocumentProxy->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.svg", sourceIndex)); svg.rename(pDocumentProxy->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.svg", targetIndex)); diff --git a/src/document/UBDocumentContainer.cpp b/src/document/UBDocumentContainer.cpp index 445b1304..c1164ce3 100644 --- a/src/document/UBDocumentContainer.cpp +++ b/src/document/UBDocumentContainer.cpp @@ -65,16 +65,15 @@ void UBDocumentContainer::duplicatePages(QList& pageIndexes) } } -bool UBDocumentContainer::movePageToIndex(int source, int target) +void UBDocumentContainer::moveThumbPage(int source, int target) { - //on document view - UBPersistenceManager::persistenceManager()->moveSceneToIndex(mCurrentDocument, source, target); - deleteThumbPage(source); - insertThumbPage(target); - emit documentThumbnailsUpdated(this); - //on board thumbnails view + mDocumentThumbs.move(source, target); + + //on board thumbnails view (UBDocumentNavigator) + emit documentPageMoved(source, target); + + //on board thumbnails view (UBoardThumbnailsView) emit moveThumbnailRequired(source, target); - return true; } void UBDocumentContainer::deletePages(QList& pageIndexes) @@ -84,26 +83,20 @@ void UBDocumentContainer::deletePages(QList& pageIndexes) foreach(int index, pageIndexes) { deleteThumbPage(index - offset); - emit removeThumbnailRequired(index - offset); offset++; - } - emit documentThumbnailsUpdated(this); } void UBDocumentContainer::addPage(int index) { UBPersistenceManager::persistenceManager()->createDocumentSceneAt(mCurrentDocument, index); insertThumbPage(index); - - emit documentThumbnailsUpdated(this); - emit addThumbnailRequired(this, index); } void UBDocumentContainer::addPixmapAt(std::shared_ptr pix, int index) { - documentThumbs().insert(index, pix); + mDocumentThumbs.insert(index, pix); } @@ -120,14 +113,15 @@ void UBDocumentContainer::initThumbPage() insertThumbPage(i); } -void UBDocumentContainer::updatePage(int index) -{ - updateThumbPage(index); -} - void UBDocumentContainer::deleteThumbPage(int index) { mDocumentThumbs.removeAt(index); + + //on board thumbnails view (UBDocumentNavigator) + emit documentPageRemoved(index); + + //on board thumbnails view (UBoardThumbnailsView) + emit removeThumbnailRequired(index); } void UBDocumentContainer::updateThumbPage(int index) @@ -136,30 +130,26 @@ void UBDocumentContainer::updateThumbPage(int index) { QPixmap pixmap = UBThumbnailAdaptor::get(mCurrentDocument, index); mDocumentThumbs[index] = std::make_shared(pixmap); - emit documentPageUpdated(index); //refresh specific thumbnail in board - } - else - { - qDebug() << "error [updateThumbPage] : index > mDocumentThumbs' size."; + + emit documentPageUpdated(index); } } void UBDocumentContainer::insertThumbPage(int index) { QPixmap newPixmap = UBThumbnailAdaptor::get(mCurrentDocument, index); - if (index < documentThumbs().size()) - { - *documentThumbs().at(index) = newPixmap; - } - else - { - documentThumbs().insert(index, std::make_shared(newPixmap)); - } + mDocumentThumbs.insert(index, std::make_shared(newPixmap)); + + emit documentPageInserted(index); + emit addThumbnailRequired(this, index); } void UBDocumentContainer::insertExistingThumbPage(int index, std::shared_ptr thumbnailPixmap) { - documentThumbs().insert(index, thumbnailPixmap); + mDocumentThumbs.insert(index, thumbnailPixmap); + + emit documentPageInserted(index); + emit addThumbnailRequired(this, index); } void UBDocumentContainer::reloadThumbnails() diff --git a/src/document/UBDocumentContainer.h b/src/document/UBDocumentContainer.h index abf7ab1f..aa220895 100644 --- a/src/document/UBDocumentContainer.h +++ b/src/document/UBDocumentContainer.h @@ -61,39 +61,40 @@ class UBDocumentContainer : public QObject static int sceneIndexFromPage(int sceneIndex); void duplicatePages(QList& pageIndexes); - bool movePageToIndex(int source, int target); void deletePages(QList& pageIndexes); - void clearThumbPage(); - void initThumbPage(); + + void addPage(int index); void addPixmapAt(std::shared_ptr pix, int index); - void updatePage(int index); - void addEmptyThumbPage(); + virtual void reloadThumbnails(); - void insertThumbPage(int index); + void clearThumbPage(); + void initThumbPage(); void insertExistingThumbPage(int index, std::shared_ptr thumbnailPixmap); + void insertThumbPage(int index); + void addEmptyThumbPage(); + void deleteThumbPage(int index); + void updateThumbPage(int index); + void moveThumbPage(int source, int target); private: UBDocumentProxy* mCurrentDocument; QList> mDocumentThumbs; - - protected: - void deleteThumbPage(int index); - void updateThumbPage(int index); - signals: void documentSet(UBDocumentProxy* document); + void documentPageInserted(int index); void documentPageUpdated(int index); + void documentPageRemoved(int index); + void documentPageMoved(int from, int to); + void documentThumbnailsUpdated(UBDocumentContainer* source); void initThumbnailsRequired(UBDocumentContainer* source); void addThumbnailRequired(UBDocumentContainer* source, int index); void removeThumbnailRequired(int index); void moveThumbnailRequired(int from, int to); void updateThumbnailsRequired(); - - void documentThumbnailsUpdated(UBDocumentContainer* source); }; diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index 6166f806..4038656c 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -1555,9 +1555,14 @@ void UBDocumentTreeView::dropEvent(QDropEvent *event) Q_ASSERT(QFileInfo(thumbTmp).exists()); Q_ASSERT(QFileInfo(thumbTo).exists()); + auto pix = std::make_shared(thumbTmp); UBDocumentController *ctrl = UBApplication::documentController; ctrl->addPixmapAt(pix, toIndex); + if (UBApplication::boardController->selectedDocument() == targetDocProxy) + { + UBApplication::boardController->insertThumbPage(toIndex); + } } QApplication::restoreOverrideCursor(); @@ -1800,6 +1805,10 @@ UBDocumentController::UBDocumentController(UBMainWindow* mainWindow) setupToolbar(); connect(this, SIGNAL(exportDone()), mMainWindow, SLOT(onExportDone())); connect(this, SIGNAL(documentThumbnailsUpdated(UBDocumentContainer*)), this, SLOT(refreshDocumentThumbnailsView(UBDocumentContainer*))); + connect(this, SIGNAL(documentPageInserted(int)), this, SLOT(insertThumbnail(int))); + connect(this, SIGNAL(documentPageUpdated(int)), this, SLOT(updateThumbnail(int))); + connect(this, SIGNAL(documentPageRemoved(int)), this, SLOT(removeThumbnail(int))); + connect(this, SIGNAL(documentPageMoved(int, int)), this, SLOT(moveThumbnail(int, int))); connect(this, SIGNAL(reorderDocumentsRequested()), this, SLOT(reorderDocuments())); } @@ -3048,23 +3057,45 @@ bool UBDocumentController::addFileToDocument(UBDocumentProxy* document) void UBDocumentController::moveSceneToIndex(UBDocumentProxy* proxy, int source, int target) { - if (UBDocumentContainer::movePageToIndex(source, target)) - { - proxy->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(proxy); + UBPersistenceManager::persistenceManager()->moveSceneToIndex(proxy, source, target); - mDocumentUI->thumbnailWidget->hightlightItem(target); + proxy->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); + UBMetadataDcSubsetAdaptor::persist(proxy); - mBoardController->setActiveDocumentScene(target); - mBoardController->reloadThumbnails(); + UBDocumentContainer::moveThumbPage(source, target); + if (UBApplication::boardController->selectedDocument() == selectedDocument()) + { + UBApplication::boardController->moveThumbPage(source, target); } + mDocumentUI->thumbnailWidget->hightlightItem(target); + + //mBoardController->setActiveDocumentScene(target); +} + +void UBDocumentController::insertThumbnail(int index, const QPixmap& pix) +{ + QGraphicsPixmapItem *newThumbnail = new UBSceneThumbnailPixmap(pix, selectedDocument(), index); // deleted by the tree widget + + mDocumentUI->thumbnailWidget->insertThumbnail(index, newThumbnail); +} + +void UBDocumentController::updateThumbnail(int index) +{ + auto pix = UBApplication::boardController->pageAt(index); + + mDocumentUI->thumbnailWidget->updateThumbnailPixmap(index, *pix); } -void UBDocumentController::updateThumbnailPixmap(int index, const QPixmap& newThumbnail) + +void UBDocumentController::removeThumbnail(int index) { - mDocumentUI->thumbnailWidget->updateThumbnailPixmap(index, newThumbnail); + mDocumentUI->thumbnailWidget->removeThumbnail(index); } +void UBDocumentController::moveThumbnail(int from, int to) +{ + mDocumentUI->thumbnailWidget->moveThumbnail(from, to); +} void UBDocumentController::thumbnailViewResized() { diff --git a/src/document/UBDocumentController.h b/src/document/UBDocumentController.h index 7fbd555a..3b634175 100644 --- a/src/document/UBDocumentController.h +++ b/src/document/UBDocumentController.h @@ -501,7 +501,10 @@ class UBDocumentController : public UBDocumentContainer void collapseAll(); void expandAll(); - void updateThumbnailPixmap(int index, const QPixmap& newThumbnail); + void updateThumbnail(int index); + void removeThumbnail(int index); + void moveThumbnail(int from, int to); + void insertThumbnail(int index, const QPixmap& pix); protected: virtual void setupViews(); diff --git a/src/gui/UBDocumentNavigator.cpp b/src/gui/UBDocumentNavigator.cpp index 4f0adba3..60ecc1f4 100644 --- a/src/gui/UBDocumentNavigator.cpp +++ b/src/gui/UBDocumentNavigator.cpp @@ -83,7 +83,10 @@ UBDocumentNavigator::UBDocumentNavigator(QWidget *parent, const char *name):QGra setFrameShadow(QFrame::Plain); connect(UBApplication::boardController, SIGNAL(documentThumbnailsUpdated(UBDocumentContainer*)), this, SLOT(generateThumbnails(UBDocumentContainer*))); - connect(UBApplication::boardController, SIGNAL(documentPageUpdated(int)), this, SLOT(updateSpecificThumbnail(int))); + connect(UBApplication::boardController, SIGNAL(documentPageInserted(int)), this, SLOT(insertThumbnail(int))); + connect(UBApplication::boardController, SIGNAL(documentPageUpdated(int)), this, SLOT(updateThumbnail(int))); + connect(UBApplication::boardController, SIGNAL(documentPageRemoved(int)), this, SLOT(removeThumbnail(int))); + connect(UBApplication::boardController, SIGNAL(documentPageMoved(int, int)), this, SLOT(moveThumbnail(int, int))); connect(UBApplication::boardController, SIGNAL(pageSelectionChanged(int)), this, SLOT(onScrollToSelectedPage(int))); connect(&mLongPressTimer, SIGNAL(timeout()), this, SLOT(longPressTimeout()), Qt::UniqueConnection); @@ -140,6 +143,7 @@ void UBDocumentNavigator::generateThumbnails(UBDocumentContainer* source) { if (UBApplication::documentController->selectedDocument() == source->selectedDocument()) { + if (UBApplication::documentController->pageAt(i)) { found = true; @@ -154,27 +158,31 @@ void UBDocumentNavigator::generateThumbnails(UBDocumentContainer* source) //claudio This is a very bad hack and shows a architectural problem // source->selectedDocument()->pageCount() != source->pageCount() if(i>=source->pageCount() || !source->pageAt(i)) + { source->insertThumbPage(i); - } - - auto pix = source->pageAt(i); - Q_ASSERT(!pix->isNull()); - int pageIndex = UBDocumentContainer::pageFromSceneIndex(i); + } + else + { + auto pix = source->pageAt(i); + Q_ASSERT(!pix->isNull()); + int pageIndex = UBDocumentContainer::pageFromSceneIndex(i); - UBSceneThumbnailNavigPixmap* pixmapItem = new UBSceneThumbnailNavigPixmap(*pix, source->selectedDocument(), i); + UBSceneThumbnailNavigPixmap* pixmapItem = new UBSceneThumbnailNavigPixmap(*pix, source->selectedDocument(), i); - QString label = tr("Page %0").arg(pageIndex); - UBThumbnailTextItem *labelItem = new UBThumbnailTextItem(label); + QString label = tr("Page %0").arg(pageIndex); + UBThumbnailTextItem *labelItem = new UBThumbnailTextItem(label); - UBImgTextThumbnailElement thumbWithText(pixmapItem, labelItem); - thumbWithText.setBorder(border()); - mThumbsWithLabels.append(thumbWithText); + UBImgTextThumbnailElement thumbWithText(pixmapItem, labelItem); + thumbWithText.setBorder(border()); + mThumbsWithLabels.append(thumbWithText); - if (lastClickedIndex == i) - mLastClickedThumbnail = pixmapItem; + if (lastClickedIndex == i) + mLastClickedThumbnail = pixmapItem; - mScene->addItem(pixmapItem); - mScene->addItem(labelItem); + mScene->addItem(pixmapItem); + mScene->addItem(labelItem); + } + } } if (selectedIndex >= 0 && selectedIndex < mThumbsWithLabels.count()) @@ -210,18 +218,20 @@ void UBDocumentNavigator::onScrollToSelectedPage(int index) * \brief Refresh the given thumbnail * @param iPage as the given page related thumbnail */ -void UBDocumentNavigator::updateSpecificThumbnail(int iPage) +void UBDocumentNavigator::updateThumbnail(int index) { - auto pix = UBApplication::boardController->pageAt(iPage); - UBSceneThumbnailNavigPixmap* newItem = new UBSceneThumbnailNavigPixmap(*pix, UBApplication::boardController->selectedDocument(), iPage); + auto pix = UBApplication::boardController->pageAt(index); + UBSceneThumbnailNavigPixmap* newItem = new UBSceneThumbnailNavigPixmap(*pix, UBApplication::boardController->selectedDocument(), index); // Get the old thumbnail - UBSceneThumbnailNavigPixmap* oldItem = mThumbsWithLabels.at(iPage).getThumbnail(); + UBSceneThumbnailNavigPixmap* oldItem = mThumbsWithLabels.at(index).getThumbnail(); if(oldItem) { + if (oldItem->isSelected()) + mScene->removeItem(oldItem->selectionItem()); mScene->removeItem(oldItem); mScene->addItem(newItem); - mThumbsWithLabels[iPage].setThumbnail(newItem); + mThumbsWithLabels[index].setThumbnail(newItem); if (mLastClickedThumbnail == oldItem) mLastClickedThumbnail = newItem; if (mSelectedThumbnail == oldItem) @@ -233,15 +243,59 @@ void UBDocumentNavigator::updateSpecificThumbnail(int iPage) ensureVisible(0, 0, 10, 10); refreshScene(); +} + +void UBDocumentNavigator::removeThumbnail(int index) +{ + //remove selectionItem + if (mThumbsWithLabels.at(index).getThumbnail()->isSelected()) + mScene->removeItem(mThumbsWithLabels.at(index).getThumbnail()->selectionItem()); + + mScene->removeItem(mThumbsWithLabels.at(index).getCaption()); + mScene->removeItem(mThumbsWithLabels.at(index).getThumbnail()); - if (UBApplication::documentController) + mThumbsWithLabels.removeAt(index); + + //update thumbs page number accordingly + for (int i=0; i < mThumbsWithLabels.length(); i++) { - if (UBApplication::documentController->selectedDocument() == UBApplication::boardController->selectedDocument()) - { - //update the pixmap in document mode - UBApplication::documentController->updateThumbnailPixmap(iPage, *pix); - } + mThumbsWithLabels.at(i).getCaption()->setText(tr("Page %0").arg(i+1)); + } + + refreshScene(); +} + +void UBDocumentNavigator::insertThumbnail(int index) +{ + auto pix = UBApplication::boardController->pageAt(index); + UBSceneThumbnailNavigPixmap* pixmapItem = new UBSceneThumbnailNavigPixmap(*pix, UBApplication::boardController->selectedDocument(), index); + + QString label = tr("Page %0").arg(index+1); + UBThumbnailTextItem *labelItem = new UBThumbnailTextItem(label); + labelItem->setWidth(mThumbnailWidth); + + UBImgTextThumbnailElement thumbWithText(pixmapItem, labelItem); + thumbWithText.setBorder(border()); + + mThumbsWithLabels.insert(index, thumbWithText); + + if (mLastClickedThumbnail) + { + if (mLastClickedThumbnail->sceneIndex() == index) + mLastClickedThumbnail = pixmapItem; } + + //sceneIndex is 0-based while pageNumber is 1-based + for (int i=0; i < mThumbsWithLabels.size(); i++) + { + mThumbsWithLabels.at(i).getThumbnail()->setSceneIndex(i); + mThumbsWithLabels.at(i).getCaption()->setPageNumber(i+1); + } + + mScene->addItem(pixmapItem); + mScene->addItem(labelItem); + + refreshScene(); } /** @@ -319,7 +373,7 @@ void UBDocumentNavigator::resizeEvent(QResizeEvent *event) mThumbnailWidth = (width() > mThumbnailMinWidth) ? width() - 2*border() : mThumbnailMinWidth; if(mSelectedThumbnail) - ensureVisible(mSelectedThumbnail); + ensureVisible(mSelectedThumbnail); // Refresh the scene refreshScene(); @@ -351,6 +405,17 @@ void UBDocumentNavigator::mousePressEvent(QMouseEvent *event) } } +void UBDocumentNavigator::clearSelection() +{ + for (auto item : mScene->items()) + { + if (item->type() == QGraphicsRectItem::Type && item != mDropBar) + { + mScene->removeItem(item); + } + } +} + UBSceneThumbnailNavigPixmap* UBDocumentNavigator::clickedThumbnail(const QPoint pos) const { UBSceneThumbnailNavigPixmap* clickedThumbnail = NULL; @@ -600,8 +665,12 @@ void UBDocumentNavigator::dropEvent(QDropEvent *event) if (mDropSource && mDropTarget) { - if (mDropSource->sceneIndex() != mDropTarget->sceneIndex()) - UBApplication::boardController->moveSceneToIndex(mDropSource->sceneIndex(), mDropTarget->sceneIndex()); + int sourceIndex = mDropSource->sceneIndex(); + int targetIndex = mDropTarget->sceneIndex(); + if (sourceIndex != targetIndex) + { + UBApplication::boardController->moveSceneToIndex(sourceIndex, targetIndex); + } } mDropSource = NULL; @@ -611,3 +680,17 @@ void UBDocumentNavigator::dropEvent(QDropEvent *event) mDropBar->setRect(QRectF()); mDropBar->hide(); } + +void UBDocumentNavigator::moveThumbnail(int from, int to) +{ + mThumbsWithLabels.move(from, to); + + //sceneIndex is 0-based while pageNumber is 1-based + for (int i=0; i < mThumbsWithLabels.size(); i++) + { + mThumbsWithLabels.at(i).getThumbnail()->setSceneIndex(i); + mThumbsWithLabels.at(i).getCaption()->setPageNumber(i+1); + } + + refreshScene(); +} diff --git a/src/gui/UBDocumentNavigator.h b/src/gui/UBDocumentNavigator.h index ffe5a447..1a252a6e 100644 --- a/src/gui/UBDocumentNavigator.h +++ b/src/gui/UBDocumentNavigator.h @@ -53,12 +53,16 @@ public: int nbColumns(); void setThumbnailMinWidth(int width); int thumbnailMinWidth(); + void clearSelection(); UBSceneThumbnailNavigPixmap* clickedThumbnail(const QPoint pos) const; public slots: void onScrollToSelectedPage(int index);// { if (mCrntItem) centerOn(mCrntItem); } void generateThumbnails(UBDocumentContainer* source); - void updateSpecificThumbnail(int iPage); + void insertThumbnail(int index); + void updateThumbnail(int index); + void removeThumbnail(int index); + void moveThumbnail(int from, int to); void longPressTimeout(); void mousePressAndHoldEvent(); diff --git a/src/gui/UBDocumentThumbnailWidget.cpp b/src/gui/UBDocumentThumbnailWidget.cpp index 4f37beb5..45dbc52d 100644 --- a/src/gui/UBDocumentThumbnailWidget.cpp +++ b/src/gui/UBDocumentThumbnailWidget.cpp @@ -309,6 +309,49 @@ void UBDocumentThumbnailWidget::updateThumbnailPixmap(int index, const QPixmap& } } +void UBDocumentThumbnailWidget::removeThumbnail(int sceneIndex) +{ + if (sceneIndex >= 0 && sceneIndex < mGraphicItems.length()) + { + QGraphicsItem* thumbnailItem = mGraphicItems.at(sceneIndex); + QGraphicsItem* textItem = mLabelsItems.at(sceneIndex); + UBSceneThumbnailPixmap *thumbnail = dynamic_cast(thumbnailItem); + if (thumbnail) + { + mGraphicItems.removeAt(sceneIndex); + if (thumbnailItem) + scene()->removeItem(thumbnailItem); + + mLabelsItems.removeAt(sceneIndex); + if (textItem) + scene()->removeItem(textItem); + } + refreshScene(); + } +} + +void UBDocumentThumbnailWidget::insertThumbnail(int index, QGraphicsPixmapItem* newThumbnail) +{ + if (!mGraphicItems.contains(newThumbnail)) //sometimes, refreshDocumentThumbnailsView is called before + { + auto thumbnailTextItem = new UBThumbnailTextItem(index); + mGraphicItems.insert(index, newThumbnail); + mLabelsItems.insert(index, thumbnailTextItem); + } + + refreshScene(); +} + +void UBDocumentThumbnailWidget::moveThumbnail(int from, int to) +{ + UBSceneThumbnailPixmap *thumbnail = dynamic_cast(mGraphicItems.at(from)); + if (thumbnail) + { + mGraphicItems.move(from, to); + } + refreshScene(); +} + void UBDocumentThumbnailWidget::hightlightItem(int index) { if (0 <= index && index < mLabelsItems.length()) diff --git a/src/gui/UBDocumentThumbnailWidget.h b/src/gui/UBDocumentThumbnailWidget.h index a7e36da3..5ac9e620 100644 --- a/src/gui/UBDocumentThumbnailWidget.h +++ b/src/gui/UBDocumentThumbnailWidget.h @@ -49,6 +49,9 @@ class UBDocumentThumbnailWidget: public UBThumbnailWidget public slots: void updateThumbnailPixmap(int index, const QPixmap& newThumbnail); + void removeThumbnail(int index); + void moveThumbnail(int from, int to); + void insertThumbnail(int index, QGraphicsPixmapItem *newThumbnail); virtual void setGraphicsItems(const QList& pGraphicsItems, const QList& pItemPaths, const QStringList pLabels = QStringList(), const QString& pMimeType = QString("")); signals: diff --git a/src/gui/UBThumbnailWidget.cpp b/src/gui/UBThumbnailWidget.cpp index b31bdd1e..ba3694d2 100644 --- a/src/gui/UBThumbnailWidget.cpp +++ b/src/gui/UBThumbnailWidget.cpp @@ -136,6 +136,12 @@ void UBThumbnailWidget::setGraphicsItems(const QList& pGraphicsI mLastSelectedThumbnail = 0; } +void UBThumbnailWidget::insertThumbnailToScene(QGraphicsPixmapItem* newThumbnail, UBThumbnailTextItem* thumbnailTextItem) +{ + mThumbnailsScene.addItem(newThumbnail); + mThumbnailsScene.addItem(thumbnailTextItem); +} + void UBThumbnailWidget::refreshScene() { @@ -156,6 +162,10 @@ void UBThumbnailWidget::refreshScene() { QGraphicsItem* item = mGraphicItems.at(i); + UBSceneThumbnailPixmap *thumbnail = dynamic_cast(item); + if (thumbnail) + thumbnail->setSceneIndex(i); + qreal scaleWidth = mThumbnailWidth / item->boundingRect().width(); qreal scaleHeight = thumbnailHeight / item->boundingRect().height(); @@ -193,7 +203,7 @@ void UBThumbnailWidget::refreshScene() if (mLabelsItems.size() > i) { QFontMetrics fm(mLabelsItems.at(i)->font(), this); - QString elidedText = fm.elidedText(mLabels.at(i), Qt::ElideRight, mThumbnailWidth); + QString elidedText = fm.elidedText(mLabelsItems.at(i)->toPlainText(), Qt::ElideRight, mThumbnailWidth); mLabelsItems.at(i)->setPlainText(elidedText); mLabelsItems.at(i)->setWidth(fm.width(elidedText) + 2 * mLabelsItems.at(i)->document()->documentMargin()); diff --git a/src/gui/UBThumbnailWidget.h b/src/gui/UBThumbnailWidget.h index c286f00d..527b9cc2 100644 --- a/src/gui/UBThumbnailWidget.h +++ b/src/gui/UBThumbnailWidget.h @@ -81,6 +81,7 @@ class UBThumbnailWidget : public QGraphicsView void setThumbnailWidth(qreal pThumbnailWidth); void setSpacing(qreal pSpacing); virtual void setGraphicsItems(const QList& pGraphicsItems, const QList& pItemPaths, const QStringList pLabels = QStringList(), const QString& pMimeType = QString("")); + void insertThumbnailToScene(QGraphicsPixmapItem* newThumbnail, UBThumbnailTextItem* thumbnailTextItem); void refreshScene(); void sceneSelectionChanged(); @@ -184,7 +185,6 @@ class UBThumbnail item->sceneBoundingRect().height() + 10); mSelectionItem->show(); - } else { @@ -199,6 +199,7 @@ class UBThumbnail void setRow(int row) { mRow = row; } UBThumbnailTextItem *label(){return mLabel;} void setLabel(UBThumbnailTextItem *label){mLabel = label;} + QGraphicsRectItem* selectionItem(){ return mSelectionItem; } protected: QGraphicsRectItem *mSelectionItem; @@ -255,6 +256,18 @@ class UBThumbnailTextItem : public QGraphicsTextItem } } + void setPageNumber(int i) + { + mUnelidedText = tr("Page %0").arg(i); + computeText(); + } + + void setText(const QString& text) + { + mUnelidedText = text; + computeText(); + } + void computeText() { QFontMetricsF fm(font()); @@ -339,6 +352,11 @@ class UBSceneThumbnailPixmap : public UBThumbnailPixmap return mSceneIndex; } + void setSceneIndex(int i) + { + mSceneIndex = i; + } + void highlight() { //NOOP