summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--filesystemwidget.cpp12
-rw-r--r--filesystemwidget.h2
-rw-r--r--helper.cpp10
-rw-r--r--helper.h9
-rw-r--r--pictureviewer.cpp170
-rw-r--r--pictureviewer.h45
-rw-r--r--pictureviewerinfoitem.cpp48
-rw-r--r--pictureviewerinfoitem.h23
-rw-r--r--shemov.pro8
9 files changed, 317 insertions, 10 deletions
diff --git a/filesystemwidget.cpp b/filesystemwidget.cpp
index 8e27928..48516df 100644
--- a/filesystemwidget.cpp
+++ b/filesystemwidget.cpp
@@ -30,6 +30,7 @@
#include "helper.h"
#include "messagedialog.h"
#include "archiveeditdialog.h"
+#include "pictureviewer.h"
FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent) {
mModel = new QFileSystemModel;
@@ -62,6 +63,8 @@ FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent) {
mFileView->setSelectionMode(QAbstractItemView::ExtendedSelection);
mFileView->setEditTriggers(QAbstractItemView::NoEditTriggers);
+ mPicViewer = new PictureViewer;
+
QString title = QString(tr("%1 - Rename file")).arg(qApp->applicationName());
mRenameDialog = new MessageDialog(tr("Rename dummy to:"), title, this);
connect(mRenameDialog, SIGNAL(accepted()), this, SLOT(doRenameFile()));
@@ -153,13 +156,8 @@ void FilesystemWidget::fileViewActivated(const QModelIndex &idx){
programArgs = data.second;
}
if(mt.toLower().startsWith("image")){
- QPair<QString, QStringList> data = programData("pictureviewer", QString());
- if(data.first.isEmpty()){
- QMessageBox::critical(this, tr("Error"), tr("No picture viewer configured."));
- return;
- }
- program = data.first;
- programArgs = data.second;
+ mPicViewer->showPic(path);
+ return;
}
programArgs << path;
QProcess::startDetached(program, programArgs);
diff --git a/filesystemwidget.h b/filesystemwidget.h
index ccd5123..2fb1bc1 100644
--- a/filesystemwidget.h
+++ b/filesystemwidget.h
@@ -23,6 +23,7 @@ class QAction;
class QVariant;
class MessageDialog;
class ArchiveEditDialog;
+class PictureViewer;
class FilesystemWidget : public QWidget {
Q_OBJECT
@@ -80,6 +81,7 @@ class FilesystemWidget : public QWidget {
ArchiveEditDialog *mAEDialog;
qint64 mSize;
QAction *mRefreshAction;
+ PictureViewer *mPicViewer;
};
#endif
diff --git a/helper.cpp b/helper.cpp
index 1b8640b..d03c383 100644
--- a/helper.cpp
+++ b/helper.cpp
@@ -117,8 +117,16 @@ namespace Helper {
return retval;
}
- bool StringListContains::operator()(const QString s, const QString &part) const{
+ bool StringListContains::operator()(const QString s, const QString &part) const {
return s.contains(part);
}
+
+ bool SortFileInfoList::operator ()(const QFileInfo &lhs, const QFileInfo &rhs) const {
+ return lhs.fileName().toLower() < rhs.fileName().toLower();
+ }
+
+ bool FileInfoListContains::operator ()(const QFileInfo &info, const QString &file) const {
+ return info.fileName() == file;
+ }
}
diff --git a/helper.h b/helper.h
index 64dc3da..90a1ad6 100644
--- a/helper.h
+++ b/helper.h
@@ -11,6 +11,7 @@
#include <magic.h>
class QString;
+class QFileInfo;
namespace Helper {
const QString mimeType(const QString &path);
@@ -22,6 +23,14 @@ namespace Helper {
public:
bool operator()(const QString s, const QString &part) const;
};
+ class SortFileInfoList : public std::binary_function<QFileInfo, QFileInfo, bool> {
+ public:
+ bool operator()(const QFileInfo &lhs, const QFileInfo &rhs) const;
+ };
+ class FileInfoListContains : public std::binary_function<QFileInfo, QString, bool> {
+ public:
+ bool operator()(const QFileInfo &info, const QString &file) const;
+ };
}
#endif
diff --git a/pictureviewer.cpp b/pictureviewer.cpp
new file mode 100644
index 0000000..74a06c5
--- /dev/null
+++ b/pictureviewer.cpp
@@ -0,0 +1,170 @@
+/*
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version
+ 2 of the License, or (at your option) any later version.
+*/
+
+#include <QGraphicsScene>
+#include <QGraphicsPixmapItem>
+#include <QFileInfo>
+#include <QLinearGradient>
+#include <QDesktopWidget>
+#include <QApplication>
+#include <QDir>
+#include <QWheelEvent>
+
+#include <algorithm>
+
+#include "pictureviewer.h"
+#include "pictureviewerinfoitem.h"
+#include "helper.h"
+
+PictureViewer::PictureViewer(QWidget *parent) : QGraphicsView(parent), mCurrentPic(0), mInfoItem(0) {
+ mScene = new QGraphicsScene(this);
+ setScene(mScene);
+}
+
+void PictureViewer::showPic(const QString &path){
+ QFileInfo fi(path);
+ if(!fi.exists() || fi.isDir()){
+ return;
+ }
+ QPixmap img(path);
+ if(img.isNull()){
+ return;
+ }
+
+ if(mCurrentPic){
+ mScene->removeItem(mCurrentPic);
+ mScene->removeItem(mInfoItem);
+ delete mCurrentPic;
+ delete mInfoItem;
+ mCurrentPic = 0;
+ mInfoItem = 0;
+ }
+
+ setDir(path);
+ if(!isVisible()){
+ show();
+ }
+
+ QSize maxSize;
+ QDesktopWidget *desktopWidget = QApplication::desktop();
+ QSize desktopSize = desktopWidget->availableGeometry().size();
+ if((img.height() > desktopSize.height()) || (img.width() > desktopSize.height())){
+ maxSize = desktopSize;
+ }else{
+ maxSize = img.size();
+ }
+
+ if(img.width() > maxSize.width() || img.height() > maxSize.height()){
+ img = img.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+ }
+
+ resize(img.size() + QSize(20, 20));
+ setSceneRect(viewport()->rect());
+ QGraphicsPixmapItem *item = new QGraphicsPixmapItem(img);
+ mCurrentPic = item;
+ item->setPos(center(img));
+ mScene->addItem(item);
+
+ PictureviewerInfoItem *infoItem = new PictureviewerInfoItem(fi.fileName());
+ QRectF infoItemRect = infoItem->boundingRect();
+ int infoWidth = infoItemRect.size().width();
+ int viewportWidth = viewport()->size().width();
+ int center = (viewportWidth - infoWidth) / 2;
+ mInfoItem = infoItem;
+ QPoint infoPoint = QPoint(center, 10);
+ infoItem->setPos(infoPoint);
+ mScene->addItem(infoItem);
+}
+
+void PictureViewer::next(){
+ if(mCurrentEntry == mDirEntries.constEnd()){
+ return;
+ }
+ QString nextPath;
+ while(mCurrentEntry != mDirEntries.constEnd()){
+ ++mCurrentEntry;
+ if(mCurrentEntry == mDirEntries.constEnd()){
+ break;
+ }
+ QFileInfo fi = *mCurrentEntry;
+ if(isPic(fi.absoluteFilePath())){
+ nextPath = fi.absoluteFilePath();
+ break;
+ }
+ }
+ if(!nextPath.isEmpty()){
+ showPic(nextPath);
+ }
+}
+
+void PictureViewer::previous(){
+ if(mCurrentEntry == mDirEntries.constBegin()){
+ return;
+ }
+ QString nextPath;
+ while(mCurrentEntry != mDirEntries.constBegin()){
+ --mCurrentEntry;
+ QFileInfo fi = *mCurrentEntry;
+ if(isPic(fi.absoluteFilePath())){
+ nextPath = fi.absoluteFilePath();
+ break;
+ }
+ }
+ if(!nextPath.isEmpty()){
+ showPic(nextPath);
+ }
+}
+
+void PictureViewer::wheelEvent(QWheelEvent *event){
+ int steps = event->delta() / 8 / 15;
+ if(steps < 0){
+ next();
+ }else{
+ previous();
+ }
+}
+
+void PictureViewer::setGradient(){
+ QColor c1(255, 7, 15);
+ QColor c2(80, 55, 250);
+ QLinearGradient g(QPointF(0, 0), sceneRect().bottomRight());
+ g.setColorAt(0, c1);
+ g.setColorAt(1, c2);
+ setBackgroundBrush(QBrush(g));
+}
+
+void PictureViewer::setDir(const QString &path){
+ QFileInfo fi(path);
+ QString dir = fi.absolutePath();
+ if(dir == mCurrentDir || !fi.exists()){
+ return;
+ }
+ mCurrentDir = dir;
+ QDir currentDir = QDir(mCurrentDir);
+ mDirEntries = currentDir.entryInfoList(QDir::Files, QDir::Name);
+ std::sort(mDirEntries.begin(), mDirEntries.end(), Helper::SortFileInfoList());
+ mCurrentEntry = std::find_if(mDirEntries.constBegin(), mDirEntries.constEnd(), std::bind2nd(Helper::FileInfoListContains(), fi.fileName()));
+}
+
+bool PictureViewer::isPic(const QString &path){
+ QFileInfo fi(path);
+ if(!fi.exists() || !fi.isFile()){
+ return false;
+ }
+ QString mime = Helper::mimeType(path);
+ if(mime.toLower().startsWith("image")){
+ return true;
+ }
+ return false;
+}
+
+QPointF PictureViewer::center(const QPixmap &pic){
+ QSize viewportSize = viewport()->size();
+ float x = (viewportSize.width() - pic.width()) / 2;
+ float y = (viewportSize.height() - pic.height()) / 2;
+ return QPointF(x, y);
+}
diff --git a/pictureviewer.h b/pictureviewer.h
new file mode 100644
index 0000000..2791bbe
--- /dev/null
+++ b/pictureviewer.h
@@ -0,0 +1,45 @@
+/*
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version
+ 2 of the License, or (at your option) any later version.
+*/
+
+#ifndef PICTUREVIEWER_H
+#define PICTUREVIEWER_H
+
+#include <QGraphicsView>
+#include <QFileInfoList>
+
+class QGraphicsScene;
+class QGraphicsPixmapItem;
+class QWheelEvent;
+class PictureviewerInfoItem;
+
+class PictureViewer : public QGraphicsView {
+ Q_OBJECT
+ public:
+ PictureViewer(QWidget *parent = 0);
+
+ public slots:
+ void showPic(const QString &path);
+ void next();
+ void previous();
+
+ protected:
+ void wheelEvent(QWheelEvent *event);
+
+ private:
+ void setGradient();
+ void setDir(const QString &path);
+ bool isPic(const QString &path);
+ QPointF center(const QPixmap &pic);
+ QGraphicsScene *mScene;
+ QGraphicsPixmapItem *mCurrentPic;
+ PictureviewerInfoItem *mInfoItem;
+ QString mCurrentDir;
+ QFileInfoList mDirEntries;
+ QFileInfoList::const_iterator mCurrentEntry;
+};
+
+#endif
diff --git a/pictureviewerinfoitem.cpp b/pictureviewerinfoitem.cpp
new file mode 100644
index 0000000..e80e8c6
--- /dev/null
+++ b/pictureviewerinfoitem.cpp
@@ -0,0 +1,48 @@
+/*
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version
+ 2 of the License, or (at your option) any later version.
+*/
+
+#include <QApplication>
+#include <QFont>
+#include <QFontMetrics>
+#include <QPen>
+#include <QBrush>
+#include <QPainter>
+
+#include "pictureviewerinfoitem.h"
+
+PictureviewerInfoItem::PictureviewerInfoItem(const QString &fileName, QGraphicsItem *parent) : QGraphicsItem(parent), mFileName(fileName){
+ setZValue(1);
+}
+
+QRectF PictureviewerInfoItem::boundingRect() const {
+ QSize size = qApp->fontMetrics().size(Qt::TextSingleLine, mFileName);
+ size += QSize(2, 2);
+ QRectF retval;
+ retval.setWidth(size.width());
+ retval.setHeight(size.height());
+ return retval;
+}
+
+void PictureviewerInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
+ Q_UNUSED(option);
+ Q_UNUSED(widget);
+ painter->save();
+ painter->setRenderHint(QPainter::Antialiasing, false);
+ painter->setRenderHint(QPainter::TextAntialiasing, true);
+ QRectF boundRect = boundingRect();
+ QColor backgroundColor(Qt::white);
+ backgroundColor.setAlpha(70);
+ QBrush brush(backgroundColor);
+ painter->setPen(QPen(Qt::NoPen));
+ painter->setBrush(brush);
+ painter->drawRect(boundRect);
+ QPen pen(Qt:: black);
+ painter->setPen(pen);
+ QPoint start(1, qApp->fontMetrics().ascent() + 1);
+ painter->drawText(start, mFileName);
+ painter->restore();
+}
diff --git a/pictureviewerinfoitem.h b/pictureviewerinfoitem.h
new file mode 100644
index 0000000..25deec4
--- /dev/null
+++ b/pictureviewerinfoitem.h
@@ -0,0 +1,23 @@
+/*
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version
+ 2 of the License, or (at your option) any later version.
+*/
+
+#ifndef PICTUREVIEWERINFOITEM_H
+#define PICTUREVIEWERINFOITEM_H
+
+#include <QGraphicsItem>
+
+class PictureviewerInfoItem : public QGraphicsItem {
+ public:
+ PictureviewerInfoItem(const QString &fileName, QGraphicsItem *parent = 0);
+ QRectF boundingRect() const;
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
+
+ private:
+ const QString mFileName;
+};
+
+#endif
diff --git a/shemov.pro b/shemov.pro
index 98de832..5b2b761 100644
--- a/shemov.pro
+++ b/shemov.pro
@@ -42,7 +42,9 @@ SOURCES = main.cpp \
programconfigurator.cpp \
addmoviewizard.cpp \
listmodelsingleton.cpp \
- moviemodelsingleton.cpp
+ moviemodelsingleton.cpp \
+ pictureviewer.cpp \
+ pictureviewerinfoitem.cpp
HEADERS = listitem.h \
listmodel.h \
movieitem.h \
@@ -80,6 +82,8 @@ HEADERS = listitem.h \
programconfigurator.h \
addmoviewizard.h \
listmodelsingleton.h \
- moviemodelsingleton.h
+ moviemodelsingleton.h \
+ pictureviewer.h \
+ pictureviewerinfoitem.h
LIBS += -lmagic
RESOURCES = shemov.qrc