From 841ac28d269d485e26543467d0cda350487ec147 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Tue, 1 Oct 2019 10:37:14 +0800 Subject: [PATCH] support commandline arguments (passing file list only) --- graphicsscene.cpp | 2 +- graphicsview.cpp | 47 ++++++++++++++++++++++++++--------------------- graphicsview.h | 3 +++ main.cpp | 21 +++++++++++++++++++++ mainwindow.cpp | 21 +++++++++++++-------- mainwindow.h | 4 ++++ 6 files changed, 68 insertions(+), 30 deletions(-) diff --git a/graphicsscene.cpp b/graphicsscene.cpp index f64ac43..834527e 100644 --- a/graphicsscene.cpp +++ b/graphicsscene.cpp @@ -50,7 +50,7 @@ void GraphicsScene::showGif(const QString &filepath) this->clear(); QMovie * movie = new QMovie(filepath); QLabel * label = new QLabel; - label->setStyleSheet("background-color:white;"); + label->setStyleSheet("background-color:rgba(225,255,255,0);"); label->setMovie(movie); this->addWidget(label); movie->start(); diff --git a/graphicsview.cpp b/graphicsview.cpp index 93c38a2..e061651 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -21,6 +21,31 @@ GraphicsView::GraphicsView(QWidget *parent) setCheckerboardEnabled(false); } +void GraphicsView::showFromUrlList(const QList &urlList) +{ + if (urlList.isEmpty()) { + // yeah, it's possible. dragging QQ's original sticker will trigger this, for example. + showText("File url list is empty"); + return; + } + QUrl url(urlList.first()); + QString filePath(url.toLocalFile()); + + if (filePath.endsWith(".svg")) { + showSvg(filePath); + } else if (filePath.endsWith(".gif")) { + showGif(filePath); + } else { + QImageReader imageReader(filePath); + QImage::Format imageFormat = imageReader.imageFormat(); + if (imageFormat == QImage::Format_Invalid) { + showText("File is not a valid image"); + } else { + showImage(QPixmap::fromImageReader(&imageReader)); + } + } +} + void GraphicsView::showImage(const QPixmap &pixmap) { resetTransform(); @@ -147,27 +172,7 @@ void GraphicsView::dropEvent(QDropEvent *event) const QMimeData * mimeData = event->mimeData(); if (mimeData->hasUrls()) { - if (mimeData->urls().isEmpty()) { - // yeah, it's possible. dragging QQ's original sticker will trigger this, for example. - showText("File url list is empty"); - return; - } - QUrl url(mimeData->urls().first()); - QString filePath(url.toLocalFile()); - - if (filePath.endsWith(".svg")) { - showSvg(filePath); - } else if (filePath.endsWith(".gif")) { - showGif(filePath); - } else { - QImageReader imageReader(filePath); - QImage::Format imageFormat = imageReader.imageFormat(); - if (imageFormat == QImage::Format_Invalid) { - showText("File is not a valid image"); - } else { - showImage(QPixmap::fromImageReader(&imageReader)); - } - } + showFromUrlList(mimeData->urls()); } else if (mimeData->hasImage()) { QImage img = qvariant_cast(mimeData->imageData()); QPixmap pixmap = QPixmap::fromImage(img); diff --git a/graphicsview.h b/graphicsview.h index cf75612..6ec8151 100644 --- a/graphicsview.h +++ b/graphicsview.h @@ -2,6 +2,7 @@ #define GRAPHICSVIEW_H #include +#include class GraphicsScene; class GraphicsView : public QGraphicsView @@ -9,6 +10,8 @@ class GraphicsView : public QGraphicsView public: GraphicsView(QWidget *parent = nullptr); + void showFromUrlList(const QList &urlList); + void showImage(const QPixmap &pixmap); void showText(const QString &text); void showSvg(const QString &filepath); diff --git a/main.cpp b/main.cpp index 76bc6d9..aae8572 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,33 @@ #include "mainwindow.h" #include +#include +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); + // parse commandline arguments + QCommandLineParser parser; + parser.addPositionalArgument("File list", QCoreApplication::translate("main", "File list.")); + + parser.process(a); + + QStringList urlStrList = parser.positionalArguments(); + QList urlList; + for (const QString & str : urlStrList) { + QUrl url = QUrl::fromLocalFile(str); + if (url.isValid()) { + urlList.append(url); + } + } + MainWindow w; w.show(); + if (!urlList.isEmpty()) { + w.showUrls(urlList); + } + return a.exec(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index 0cd9590..e1b7597 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -38,11 +38,11 @@ MainWindow::MainWindow(QWidget *parent) : GraphicsScene * scene = new GraphicsScene(this); - GraphicsView * pictureView = new GraphicsView(this); - pictureView->setScene(scene); - this->setCentralWidget(pictureView); + m_graphicsView = new GraphicsView(this); + m_graphicsView->setScene(scene); + this->setCentralWidget(m_graphicsView); - m_closeButton = new QPushButton(pictureView); + m_closeButton = new QPushButton(m_graphicsView); m_closeButton->setFlat(true); m_closeButton->setFixedSize(50, 50); m_closeButton->setStyleSheet("QPushButton {" @@ -58,13 +58,13 @@ MainWindow::MainWindow(QWidget *parent) : m_bottomButtonGroup = new BottomButtonGroup(this); connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, - this, [ = ](){ pictureView->setTransform(QTransform()); }); + this, [ = ](){ m_graphicsView->setTransform(QTransform()); }); connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, - this, [ = ](){ pictureView->scale(1.25, 1.25); }); + this, [ = ](){ m_graphicsView->scale(1.25, 1.25); }); connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked, - this, [ = ](){ pictureView->scale(0.75, 0.75); }); + this, [ = ](){ m_graphicsView->scale(0.75, 0.75); }); connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked, - this, [ = ](){ pictureView->toggleCheckerboard(); }); + this, [ = ](){ m_graphicsView->toggleCheckerboard(); }); this->setGeometry( QStyle::alignedRect( @@ -81,6 +81,11 @@ MainWindow::~MainWindow() } +void MainWindow::showUrls(const QList &urls) +{ + m_graphicsView->showFromUrlList(urls); +} + void MainWindow::showEvent(QShowEvent *event) { updateWidgetsPosition(); diff --git a/mainwindow.h b/mainwindow.h index b2362fb..a6c39e7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,6 +6,7 @@ #include #include +class GraphicsView; class BottomButtonGroup; class MainWindow : public QMainWindow { @@ -15,6 +16,8 @@ public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow() override; + void showUrls(const QList &urls); + protected slots: void showEvent(QShowEvent *event) override; void mousePressEvent(QMouseEvent *event) override; @@ -34,6 +37,7 @@ private: QPropertyAnimation *m_floatUpAnimation; QParallelAnimationGroup *m_exitAnimationGroup; QPushButton *m_closeButton; + GraphicsView *m_graphicsView; BottomButtonGroup *m_bottomButtonGroup; bool m_clickedOnWindow = false; };