diff --git a/app/graphicsview.cpp b/app/graphicsview.cpp index 69c9721..3557f3e 100644 --- a/app/graphicsview.cpp +++ b/app/graphicsview.cpp @@ -23,14 +23,14 @@ GraphicsView::GraphicsView(QWidget *parent) setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setStyleSheet("background-color: rgba(0, 0, 0, 220);" "border-radius: 3px;"); - setAcceptDrops(true); + setAcceptDrops(false); setCheckerboardEnabled(false); connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged); connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged); } -void GraphicsView::showFileFromPath(const QString &filePath, bool doRequestGallery) +void GraphicsView::showFileFromPath(const QString &filePath) { emit navigatorViewRequired(false, transform()); @@ -48,17 +48,14 @@ void GraphicsView::showFileFromPath(const QString &filePath, bool doRequestGalle // So we cannot use imageFormat() and check if it returns QImage::Format_Invalid to detect if we support the file. // QImage::Format imageFormat = imageReader.imageFormat(); if (imageReader.format().isEmpty()) { - doRequestGallery = false; showText(tr("File is not a valid image")); } else if (imageReader.supportsAnimation() && imageReader.imageCount() > 1) { showAnimated(filePath); } else if (!imageReader.canRead()) { - doRequestGallery = false; showText(tr("Image data is invalid or currently unsupported")); } else { QPixmap && pixmap = QPixmap::fromImageReader(&imageReader); if (pixmap.isNull()) { - doRequestGallery = false; showText(tr("Image data is invalid or currently unsupported")); } else { pixmap.setDevicePixelRatio(devicePixelRatioF()); @@ -66,10 +63,6 @@ void GraphicsView::showFileFromPath(const QString &filePath, bool doRequestGalle } } } - - if (doRequestGallery) { - emit requestGallery(filePath); - } } void GraphicsView::showImage(const QPixmap &pixmap) @@ -318,55 +311,6 @@ void GraphicsView::resizeEvent(QResizeEvent *event) return QGraphicsView::resizeEvent(event); } -void GraphicsView::dragEnterEvent(QDragEnterEvent *event) -{ - if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) { - event->acceptProposedAction(); - } else { - event->ignore(); - } -// qDebug() << event->mimeData() << "Drag Enter Event" -// << event->mimeData()->hasUrls() << event->mimeData()->hasImage() -// << event->mimeData()->formats() << event->mimeData()->hasFormat("text/uri-list"); - - return QGraphicsView::dragEnterEvent(event); -} - -void GraphicsView::dragMoveEvent(QDragMoveEvent *event) -{ - Q_UNUSED(event) - // by default, QGraphicsView/Scene will ignore the action if there are no QGraphicsItem under cursor. - // We actually doesn't care and would like to keep the drag event as-is, so just do nothing here. -} - -void GraphicsView::dropEvent(QDropEvent *event) -{ - event->acceptProposedAction(); - - const QMimeData * mimeData = event->mimeData(); - - if (mimeData->hasUrls()) { - const QList &urls = mimeData->urls(); - if (urls.isEmpty()) { - showText(tr("File url list is empty")); - } else { - showFileFromPath(urls.first().toLocalFile(), true); - } - } else if (mimeData->hasImage()) { - QImage img = qvariant_cast(mimeData->imageData()); - QPixmap pixmap = QPixmap::fromImage(img); - if (pixmap.isNull()) { - showText(tr("Image data is invalid")); - } else { - showImage(pixmap); - } - } else if (mimeData->hasText()) { - showText(mimeData->text()); - } else { - showText(tr("Not supported mimedata: %1").arg(mimeData->formats().first())); - } -} - bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const { return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize()) diff --git a/app/graphicsview.h b/app/graphicsview.h index 833cd3d..694557c 100644 --- a/app/graphicsview.h +++ b/app/graphicsview.h @@ -15,7 +15,7 @@ class GraphicsView : public QGraphicsView public: GraphicsView(QWidget *parent = nullptr); - void showFileFromPath(const QString &filePath, bool requestGallery = false); + void showFileFromPath(const QString &filePath); void showImage(const QPixmap &pixmap); void showImage(const QImage &image); @@ -48,7 +48,6 @@ public: signals: void navigatorViewRequired(bool required, QTransform transform); void viewportRectChanged(); - void requestGallery(const QString &filePath); public slots: void toggleCheckerboard(bool invertCheckerboardColor = false); @@ -60,10 +59,6 @@ private: void wheelEvent(QWheelEvent *event) override; void resizeEvent(QResizeEvent *event) override; - void dragEnterEvent(QDragEnterEvent *event) override; - void dragMoveEvent(QDragMoveEvent *event) override; - void dropEvent(QDropEvent *event) override; - bool isThingSmallerThanWindowWith(const QTransform &transform) const; bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const; void setCheckerboardEnabled(bool enabled, bool invertColor = false); diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index 363d5b6..0a67eb5 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -56,6 +56,7 @@ MainWindow::MainWindow(QWidget *parent) this->setMinimumSize(350, 330); this->setWindowIcon(QIcon(":/icons/app-icon.svg")); this->setMouseTracking(true); + this->setAcceptDrops(true); m_pm->setAutoLoadFilterSuffixes(supportedImageFormats()); @@ -95,9 +96,6 @@ MainWindow::MainWindow(QWidget *parent) connect(m_graphicsView, &GraphicsView::viewportRectChanged, m_gv, &NavigatorView::updateMainViewportRegion); - connect(m_graphicsView, &GraphicsView::requestGallery, - this, &MainWindow::loadGalleryBySingleLocalFile); - m_closeButton = new ToolButton(true, m_graphicsView); m_closeButton->setIconSize(QSize(32, 32)); m_closeButton->setFixedSize(QSize(50, 50)); @@ -142,7 +140,8 @@ MainWindow::MainWindow(QWidget *parent) m_nextButton->setVisible(galleryFileCount > 1); }); - connect(m_pm, &PlaylistManager::currentIndexChanged, this, &MainWindow::galleryCurrent); + connect(m_pm->model(), &PlaylistModel::modelReset, this, std::bind(&MainWindow::galleryCurrent, this, false)); + connect(m_pm, &PlaylistManager::currentIndexChanged, this, std::bind(&MainWindow::galleryCurrent, this, true)); QShortcut * fullscreenShorucut = new QShortcut(QKeySequence(QKeySequence::FullScreen), this); connect(fullscreenShorucut, &QShortcut::activated, @@ -170,12 +169,8 @@ MainWindow::~MainWindow() void MainWindow::showUrls(const QList &urls) { if (!urls.isEmpty()) { - if (urls.count() == 1) { - m_graphicsView->showFileFromPath(urls.first().toLocalFile(), true); - } else { - m_graphicsView->showFileFromPath(urls.first().toLocalFile(), false); - m_pm->setPlaylist(urls); - } + m_graphicsView->showFileFromPath(urls.first().toLocalFile()); + m_pm->loadPlaylist(urls); } else { m_graphicsView->showText(tr("File url list is empty")); return; @@ -240,16 +235,12 @@ void MainWindow::clearGallery() m_pm->setPlaylist({}); } -void MainWindow::loadGalleryBySingleLocalFile(const QString &path) -{ - m_pm->loadPlaylist(QUrl::fromLocalFile(path)); -} - void MainWindow::galleryPrev() { QModelIndex index = m_pm->previousIndex(); if (index.isValid()) { m_pm->setCurrentIndex(index); + m_graphicsView->showFileFromPath(m_pm->localFileByIndex(index)); } } @@ -258,17 +249,18 @@ void MainWindow::galleryNext() QModelIndex index = m_pm->nextIndex(); if (index.isValid()) { m_pm->setCurrentIndex(index); + m_graphicsView->showFileFromPath(m_pm->localFileByIndex(index)); } } -// If playlist (or its index) get changed, use this method to "reload" the current file. -void MainWindow::galleryCurrent() +// Only use this to update minor information. Do NOT use this to load image +// because it might cause an image gets loaded multiple times. +void MainWindow::galleryCurrent(bool showLoadImageHintWhenEmpty) { QModelIndex index = m_pm->curIndex(); if (index.isValid()) { setWindowTitle(m_pm->urlByIndex(index).fileName()); - m_graphicsView->showFileFromPath(m_pm->localFileByIndex(index), false); - } else { + } else if (showLoadImageHintWhenEmpty && m_pm->totalCount() <= 0) { m_graphicsView->showText(QCoreApplication::translate("GraphicsScene", "Drag image here")); } } @@ -511,6 +503,50 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event) return FramelessWindow::contextMenuEvent(event); } +void MainWindow::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) { + event->acceptProposedAction(); + } else { + event->ignore(); + } + + return FramelessWindow::dragEnterEvent(event); +} + +void MainWindow::dragMoveEvent(QDragMoveEvent *event) +{ + Q_UNUSED(event) +} + +void MainWindow::dropEvent(QDropEvent *event) +{ + event->acceptProposedAction(); + + const QMimeData * mimeData = event->mimeData(); + + if (mimeData->hasUrls()) { + const QList &urls = mimeData->urls(); + if (urls.isEmpty()) { + m_graphicsView->showText(tr("File url list is empty")); + } else { + showUrls(urls); + } + } else if (mimeData->hasImage()) { + QImage img = qvariant_cast(mimeData->imageData()); + QPixmap pixmap = QPixmap::fromImage(img); + if (pixmap.isNull()) { + m_graphicsView->showText(tr("Image data is invalid")); + } else { + m_graphicsView->showImage(pixmap); + } + } else if (mimeData->hasText()) { + m_graphicsView->showText(mimeData->text()); + } else { + m_graphicsView->showText(tr("Not supported mimedata: %1").arg(mimeData->formats().first())); + } +} + void MainWindow::centerWindow() { this->setGeometry( @@ -703,11 +739,11 @@ void MainWindow::on_actionPaste_triggered() } if (!clipboardImage.isNull()) { + setWindowTitle(tr("Image From Clipboard")); m_graphicsView->showImage(clipboardImage); clearGallery(); } else if (clipboardFileUrl.isValid()) { - QString localFile(clipboardFileUrl.toLocalFile()); - m_graphicsView->showFileFromPath(localFile, true); + m_graphicsView->showFileFromPath(clipboardFileUrl.toLocalFile()); m_pm->loadPlaylist(clipboardFileUrl); } } @@ -729,7 +765,7 @@ void MainWindow::on_actionTrash_triggered() tr("Move to trash failed, it might caused by file permission issue, file system limitation, or platform limitation.")); } else { m_pm->removeAt(index); - galleryCurrent(); + galleryCurrent(true); } } } diff --git a/app/mainwindow.h b/app/mainwindow.h index 7a0c842..c905fe2 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -42,10 +42,9 @@ public: QUrl currentImageFileUrl() const; void clearGallery(); - void loadGalleryBySingleLocalFile(const QString &path); void galleryPrev(); void galleryNext(); - void galleryCurrent(); + void galleryCurrent(bool showLoadImageHintWhenEmpty); static QStringList supportedImageFormats(); @@ -60,6 +59,9 @@ protected slots: void wheelEvent(QWheelEvent *event) override; void resizeEvent(QResizeEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; + void dragEnterEvent(QDragEnterEvent *event) override; + void dragMoveEvent(QDragMoveEvent *event) override; + void dropEvent(QDropEvent *event) override; void centerWindow(); void closeWindow(); diff --git a/app/playlistmanager.h b/app/playlistmanager.h index 7c04f37..73c7549 100644 --- a/app/playlistmanager.h +++ b/app/playlistmanager.h @@ -80,6 +80,6 @@ signals: void totalCountChanged(int count); private: - int m_currentIndex; + int m_currentIndex = -1; PlaylistModel m_model; };