support commandline arguments (passing file list only)

This commit is contained in:
Gary Wang 2019-10-01 10:37:14 +08:00
parent ee1aa98e45
commit 841ac28d26
6 changed files with 68 additions and 30 deletions

View File

@ -50,7 +50,7 @@ void GraphicsScene::showGif(const QString &filepath)
this->clear(); this->clear();
QMovie * movie = new QMovie(filepath); QMovie * movie = new QMovie(filepath);
QLabel * label = new QLabel; QLabel * label = new QLabel;
label->setStyleSheet("background-color:white;"); label->setStyleSheet("background-color:rgba(225,255,255,0);");
label->setMovie(movie); label->setMovie(movie);
this->addWidget(label); this->addWidget(label);
movie->start(); movie->start();

View File

@ -21,6 +21,31 @@ GraphicsView::GraphicsView(QWidget *parent)
setCheckerboardEnabled(false); setCheckerboardEnabled(false);
} }
void GraphicsView::showFromUrlList(const QList<QUrl> &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) void GraphicsView::showImage(const QPixmap &pixmap)
{ {
resetTransform(); resetTransform();
@ -147,27 +172,7 @@ void GraphicsView::dropEvent(QDropEvent *event)
const QMimeData * mimeData = event->mimeData(); const QMimeData * mimeData = event->mimeData();
if (mimeData->hasUrls()) { if (mimeData->hasUrls()) {
if (mimeData->urls().isEmpty()) { showFromUrlList(mimeData->urls());
// 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));
}
}
} else if (mimeData->hasImage()) { } else if (mimeData->hasImage()) {
QImage img = qvariant_cast<QImage>(mimeData->imageData()); QImage img = qvariant_cast<QImage>(mimeData->imageData());
QPixmap pixmap = QPixmap::fromImage(img); QPixmap pixmap = QPixmap::fromImage(img);

View File

@ -2,6 +2,7 @@
#define GRAPHICSVIEW_H #define GRAPHICSVIEW_H
#include <QGraphicsView> #include <QGraphicsView>
#include <QUrl>
class GraphicsScene; class GraphicsScene;
class GraphicsView : public QGraphicsView class GraphicsView : public QGraphicsView
@ -9,6 +10,8 @@ class GraphicsView : public QGraphicsView
public: public:
GraphicsView(QWidget *parent = nullptr); GraphicsView(QWidget *parent = nullptr);
void showFromUrlList(const QList<QUrl> &urlList);
void showImage(const QPixmap &pixmap); void showImage(const QPixmap &pixmap);
void showText(const QString &text); void showText(const QString &text);
void showSvg(const QString &filepath); void showSvg(const QString &filepath);

View File

@ -1,12 +1,33 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser>
#include <QUrl>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, 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<QUrl> urlList;
for (const QString & str : urlStrList) {
QUrl url = QUrl::fromLocalFile(str);
if (url.isValid()) {
urlList.append(url);
}
}
MainWindow w; MainWindow w;
w.show(); w.show();
if (!urlList.isEmpty()) {
w.showUrls(urlList);
}
return a.exec(); return a.exec();
} }

View File

@ -38,11 +38,11 @@ MainWindow::MainWindow(QWidget *parent) :
GraphicsScene * scene = new GraphicsScene(this); GraphicsScene * scene = new GraphicsScene(this);
GraphicsView * pictureView = new GraphicsView(this); m_graphicsView = new GraphicsView(this);
pictureView->setScene(scene); m_graphicsView->setScene(scene);
this->setCentralWidget(pictureView); this->setCentralWidget(m_graphicsView);
m_closeButton = new QPushButton(pictureView); m_closeButton = new QPushButton(m_graphicsView);
m_closeButton->setFlat(true); m_closeButton->setFlat(true);
m_closeButton->setFixedSize(50, 50); m_closeButton->setFixedSize(50, 50);
m_closeButton->setStyleSheet("QPushButton {" m_closeButton->setStyleSheet("QPushButton {"
@ -58,13 +58,13 @@ MainWindow::MainWindow(QWidget *parent) :
m_bottomButtonGroup = new BottomButtonGroup(this); m_bottomButtonGroup = new BottomButtonGroup(this);
connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked,
this, [ = ](){ pictureView->setTransform(QTransform()); }); this, [ = ](){ m_graphicsView->setTransform(QTransform()); });
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, 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, 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, connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked,
this, [ = ](){ pictureView->toggleCheckerboard(); }); this, [ = ](){ m_graphicsView->toggleCheckerboard(); });
this->setGeometry( this->setGeometry(
QStyle::alignedRect( QStyle::alignedRect(
@ -81,6 +81,11 @@ MainWindow::~MainWindow()
} }
void MainWindow::showUrls(const QList<QUrl> &urls)
{
m_graphicsView->showFromUrlList(urls);
}
void MainWindow::showEvent(QShowEvent *event) void MainWindow::showEvent(QShowEvent *event)
{ {
updateWidgetsPosition(); updateWidgetsPosition();

View File

@ -6,6 +6,7 @@
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QPushButton> #include <QPushButton>
class GraphicsView;
class BottomButtonGroup; class BottomButtonGroup;
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
@ -15,6 +16,8 @@ public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override; ~MainWindow() override;
void showUrls(const QList<QUrl> &urls);
protected slots: protected slots:
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
@ -34,6 +37,7 @@ private:
QPropertyAnimation *m_floatUpAnimation; QPropertyAnimation *m_floatUpAnimation;
QParallelAnimationGroup *m_exitAnimationGroup; QParallelAnimationGroup *m_exitAnimationGroup;
QPushButton *m_closeButton; QPushButton *m_closeButton;
GraphicsView *m_graphicsView;
BottomButtonGroup *m_bottomButtonGroup; BottomButtonGroup *m_bottomButtonGroup;
bool m_clickedOnWindow = false; bool m_clickedOnWindow = false;
}; };