diff --git a/graphicsview.cpp b/graphicsview.cpp index 28f23b1..db76c5e 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -25,15 +25,10 @@ GraphicsView::GraphicsView(QWidget *parent) connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged); } -void GraphicsView::showFromUrlList(const QList &urlList) +void GraphicsView::showFileFromUrl(const QUrl &url, bool doRequestGallery) { emit navigatorViewRequired(false, 0); - if (urlList.isEmpty()) { - // yeah, it's possible. dragging QQ's original sticker will trigger this, for example. - showText(tr("File url list is empty")); - return; - } - QUrl url(urlList.first()); + QString filePath(url.toLocalFile()); if (filePath.endsWith(".svg")) { @@ -51,6 +46,10 @@ void GraphicsView::showFromUrlList(const QList &urlList) showImage(QPixmap::fromImageReader(&imageReader)); } } + + if (doRequestGallery) { + emit requestGallery(filePath); + } } void GraphicsView::showImage(const QPixmap &pixmap) @@ -228,7 +227,12 @@ void GraphicsView::dropEvent(QDropEvent *event) const QMimeData * mimeData = event->mimeData(); if (mimeData->hasUrls()) { - showFromUrlList(mimeData->urls()); + const QList &urls = mimeData->urls(); + if (urls.isEmpty()) { + showText(tr("File url list is empty")); + } else { + showFileFromUrl(urls.first()); + } } else if (mimeData->hasImage()) { QImage img = qvariant_cast(mimeData->imageData()); QPixmap pixmap = QPixmap::fromImage(img); diff --git a/graphicsview.h b/graphicsview.h index 6f5cc7f..bfb7713 100644 --- a/graphicsview.h +++ b/graphicsview.h @@ -11,7 +11,7 @@ class GraphicsView : public QGraphicsView public: GraphicsView(QWidget *parent = nullptr); - void showFromUrlList(const QList &urlList); + void showFileFromUrl(const QUrl &url, bool requestGallery = false); void showImage(const QPixmap &pixmap); void showText(const QString &text); @@ -34,6 +34,7 @@ public: signals: void navigatorViewRequired(bool required, qreal angle); void viewportRectChanged(); + void requestGallery(const QString &filePath); public slots: void toggleCheckerboard(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 360015c..e751fc0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #ifdef _WIN32 #include @@ -65,6 +67,9 @@ 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(m_graphicsView); m_closeButton->setIcon(QIcon(":/icons/window-close")); m_closeButton->setIconSize(QSize(50, 50)); @@ -106,6 +111,14 @@ MainWindow::MainWindow(QWidget *parent) : connect(quitAppShorucut, &QShortcut::activated, std::bind(&MainWindow::quitAppAction, this, false)); + QShortcut * prevPictureShorucut = new QShortcut(QKeySequence(Qt::Key_PageUp), this); + connect(prevPictureShorucut, &QShortcut::activated, + this, &MainWindow::galleryPrev); + + QShortcut * nextPictureShorucut = new QShortcut(QKeySequence(Qt::Key_PageDown), this); + connect(nextPictureShorucut, &QShortcut::activated, + this, &MainWindow::galleryNext); + centerWindow(); } @@ -116,7 +129,19 @@ MainWindow::~MainWindow() void MainWindow::showUrls(const QList &urls) { - m_graphicsView->showFromUrlList(urls); + if (!urls.isEmpty()) { + if (urls.count() == 1) { + m_graphicsView->showFileFromUrl(urls.first(), true); + } else { + m_graphicsView->showFileFromUrl(urls.first(), false); + m_files = urls; + m_currentFileIndex = 0; + } + } else { + m_graphicsView->showText(tr("File url list is empty")); + return; + } + m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio); } @@ -148,6 +173,57 @@ void MainWindow::adjustWindowSizeBySceneRect() } } +void MainWindow::loadGalleryBySingleLocalFile(const QString &path) +{ + QFileInfo info(path); + QDir dir(info.path()); + QString currentFileName = info.fileName(); + QStringList entryList = dir.entryList({"*.jpg", "*.jpeg", "*.png", "*.gif", "*.svg"}, + QDir::Files | QDir::NoSymLinks, QDir::NoSort); + + QCollator collator; + collator.setNumericMode(true); + + std::sort(entryList.begin(), entryList.end(), collator); + + m_currentFileIndex = -1; + m_files.clear(); + + for (int i = 0; i < entryList.count(); i++) { + const QString & oneEntry = entryList.at(i); + m_files.append(QUrl::fromLocalFile(dir.absoluteFilePath(oneEntry))); + if (oneEntry == currentFileName) { + m_currentFileIndex = i; + } + } + + qDebug() << m_files << m_currentFileIndex; +} + +void MainWindow::galleryPrev() +{ + int count = m_files.count(); + if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) { + return; + } + + m_currentFileIndex = m_currentFileIndex - 1 < 0 ? count - 1 : m_currentFileIndex - 1; + + m_graphicsView->showFileFromUrl(m_files.at(m_currentFileIndex), false); +} + +void MainWindow::galleryNext() +{ + int count = m_files.count(); + if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) { + return; + } + + m_currentFileIndex = m_currentFileIndex + 1 == count ? 0 : m_currentFileIndex + 1; + + m_graphicsView->showFileFromUrl(m_files.at(m_currentFileIndex), false); +} + void MainWindow::showEvent(QShowEvent *event) { updateWidgetsPosition(); diff --git a/mainwindow.h b/mainwindow.h index df182b9..b4ee4a9 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -26,6 +26,10 @@ public: void showUrls(const QList &urls); void adjustWindowSizeBySceneRect(); + void loadGalleryBySingleLocalFile(const QString &path); + void galleryPrev(); + void galleryNext(); + protected slots: void showEvent(QShowEvent *event) override; void enterEvent(QEvent *event) override; @@ -59,6 +63,9 @@ private: BottomButtonGroup *m_bottomButtonGroup; bool m_protectedMode = false; bool m_clickedOnWindow = false; + + QList m_files; + int m_currentFileIndex = -1; }; #endif // MAINWINDOW_H