feat: load current folder pics and nav using pageup and pagedown

This commit is contained in:
Gary Wang 2019-11-16 15:53:24 +08:00
parent 568c50deff
commit 4b4494f116
4 changed files with 98 additions and 10 deletions

View File

@ -25,15 +25,10 @@ GraphicsView::GraphicsView(QWidget *parent)
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
}
void GraphicsView::showFromUrlList(const QList<QUrl> &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<QUrl> &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<QUrl> &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<QImage>(mimeData->imageData());
QPixmap pixmap = QPixmap::fromImage(img);

View File

@ -11,7 +11,7 @@ class GraphicsView : public QGraphicsView
public:
GraphicsView(QWidget *parent = nullptr);
void showFromUrlList(const QList<QUrl> &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();

View File

@ -15,6 +15,8 @@
#include <QScreen>
#include <QMenu>
#include <QShortcut>
#include <QDir>
#include <QCollator>
#ifdef _WIN32
#include <windows.h>
@ -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<QUrl> &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();

View File

@ -26,6 +26,10 @@ public:
void showUrls(const QList<QUrl> &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<QUrl> m_files;
int m_currentFileIndex = -1;
};
#endif // MAINWINDOW_H