diff options
author | Arno <arno@disconnect.de> | 2016-11-27 06:52:50 +0100 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2016-11-27 06:52:50 +0100 |
commit | 9c89e2f83415c876f28f15ebff31c8325754a936 (patch) | |
tree | 81262fcd178af3071187cf89b415fc70cdb8d977 | |
parent | d1cf6c418855004a249d44fd383c889cbd1e7662 (diff) | |
download | ShemovCleaner-9c89e2f83415c876f28f15ebff31c8325754a936.tar.gz ShemovCleaner-9c89e2f83415c876f28f15ebff31c8325754a936.tar.bz2 ShemovCleaner-9c89e2f83415c876f28f15ebff31c8325754a936.zip |
Add picture viewer
Opens on doubleclick when it's image/*.
-rw-r--r-- | ShemovCleaner.pro | 6 | ||||
-rw-r--r-- | filewidget.cpp | 6 | ||||
-rw-r--r-- | globals.cpp | 10 | ||||
-rw-r--r-- | globals.h | 3 | ||||
-rw-r--r-- | viewer.cpp | 79 | ||||
-rw-r--r-- | viewer.h | 26 |
6 files changed, 127 insertions, 3 deletions
diff --git a/ShemovCleaner.pro b/ShemovCleaner.pro index 2647e72..f838cce 100644 --- a/ShemovCleaner.pro +++ b/ShemovCleaner.pro @@ -26,7 +26,8 @@ SOURCES += main.cpp\ filedisplay.cpp \ globals.cpp \ actorwidget.cpp \ - origindialog.cpp + origindialog.cpp \ + viewer.cpp HEADERS += \ filesorter.h \ @@ -40,7 +41,8 @@ HEADERS += \ filedisplay.h \ globals.h \ actorwidget.h \ - origindialog.h + origindialog.h \ + viewer.h DISTFILES += diff --git a/filewidget.cpp b/filewidget.cpp index 966ad95..f56b4ce 100644 --- a/filewidget.cpp +++ b/filewidget.cpp @@ -37,6 +37,7 @@ #include "origindialog.h" #include "helper.h" #include "globals.h" +#include "viewer.h" FileWidget::FileWidget(QWidget *parent) : QWidget(parent) { setupGui(); @@ -505,6 +506,11 @@ void FileWidget::properties(const QModelIndex &idx){ if(mime.startsWith("video")){ fileData(name.data(FullPathRole).toString(), name.data(MD5SumRole).toString()); } + if(mime.startsWith("image")){ + Viewer *v = Globals::instance()->viewer(); + v->setFile(idx.data(FullPathRole).toString()); + v->show(); + } } void FileWidget::cd(const QString &to){ diff --git a/globals.cpp b/globals.cpp index 2bdb175..53bd1f0 100644 --- a/globals.cpp +++ b/globals.cpp @@ -1,6 +1,7 @@ #include <QAction> #include "globals.h" +#include "viewer.h" Globals *Globals::mInstance = nullptr; @@ -19,4 +20,11 @@ QAction *Globals::action(int actionType){ return mActions.value(actionType); } -Globals::Globals(){} +Viewer *Globals::viewer(){ + if(!mViewer){ + mViewer = new Viewer; + } + return mViewer; +} + +Globals::Globals() : mViewer(nullptr) {} @@ -5,6 +5,7 @@ #include <QHash> class QAction; +class Viewer; class Globals : public QObject { Q_OBJECT @@ -13,6 +14,7 @@ class Globals : public QObject { static Globals *instance(); void addAction(QAction *a); QAction *action(int actionType); + Viewer *viewer(); private: Globals(); @@ -20,6 +22,7 @@ class Globals : public QObject { Globals &operator=(const Globals &other); static Globals *mInstance; QHash<int, QAction*> mActions; + Viewer *mViewer; }; #endif // GLOBALS_H diff --git a/viewer.cpp b/viewer.cpp new file mode 100644 index 0000000..e8d5f7f --- /dev/null +++ b/viewer.cpp @@ -0,0 +1,79 @@ +#include <QLabel> +#include <QHBoxLayout> +#include <QDesktopWidget> +#include <QApplication> +#include <QFileInfo> +#include <QMimeDatabase> +#include <QDir> +#include <QWheelEvent> + +#include "viewer.h" + +Viewer::Viewer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mCurIndex(-1) { + QDesktopWidget *dw = qApp->desktop(); + mLabel = new QLabel; + mLabel->setMaximumSize(dw->size() - QSize(20, 20)); + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(mLabel); + setLayout(mainLayout); +} + +void Viewer::setFile(const QString &file, bool allFiles){ + mCurIndex = -1; + QFileInfo fi(file); + QMimeDatabase db; + QMimeType mt = db.mimeTypeForFile(fi); + if(mt.name().startsWith("image")){ + mFiles << fi.absoluteFilePath(); + } + if(allFiles){ + foreach(auto curFi, fi.dir().entryInfoList()){ + if(!mFiles.contains(curFi.absoluteFilePath())){ + QMimeType mt = db.mimeTypeForFile(curFi); + if(mt.name().startsWith("image")){ + mFiles << curFi.absoluteFilePath(); + } + } + } + } + if(!mFiles.isEmpty()){ + mCurIndex = 0; + displayFile(mCurIndex); + } +} + +void Viewer::wheelEvent(QWheelEvent *event){ + QPoint numDeg = event->angleDelta() / 8; + if(numDeg.y() < 0){ //this is scrolling down -> next! + if(mCurIndex + 1 >= mFiles.count()){ + mCurIndex = 0; + }else{ + ++mCurIndex; + } + }else if(numDeg.y() > 0){ + if(mCurIndex - 1 < 0){ + mCurIndex = mFiles.count() - 1; + }else{ + --mCurIndex; + } + } + displayFile(mCurIndex); + event->accept(); +} + +void Viewer::displayFile(int index){ + if(index < 0){ + return; + } + QPixmap pm(mFiles.at(mCurIndex)); + if(pm.width() > mLabel->maximumWidth()){ + pm = pm.scaledToWidth(mLabel->maximumWidth()); + } + if(pm.height() > mLabel->maximumHeight()){ + pm = pm.scaledToHeight(mLabel->maximumHeight()); + } + mLabel->setPixmap(pm); + adjustSize(); + QString winTitle = QString(tr("%1 Viewer [%2]")).arg(qApp->applicationName()).arg(mFiles.at(mCurIndex)); + setWindowTitle(winTitle); +} diff --git a/viewer.h b/viewer.h new file mode 100644 index 0000000..6f9537e --- /dev/null +++ b/viewer.h @@ -0,0 +1,26 @@ +#ifndef VIEWER_H +#define VIEWER_H + +#include <QWidget> + +class QLabel; +class QWheelEvent; + +class Viewer : public QWidget { + public: + Viewer(QWidget *parent = 0, Qt::WindowFlags f = 0); + void setFile(const QString &file, bool allFiles = true); + + protected: + virtual void wheelEvent(QWheelEvent *event); + + private slots: + void displayFile(int index); + + private: + int mCurIndex; + QLabel *mLabel; + QStringList mFiles; +}; + +#endif // VIEWER_H |