Forráskód Böngészése

Get picture viewer to functional state

Change-Id: Ied79828bdef054e02de65b08b31ec3927ba9c13b
Donald Carr 9 éve
commit
36d2816bac
8 módosított fájl, 300 hozzáadás és 0 törlés
  1. 73 0
      .gitignore
  2. 18 0
      artriculate.pro
  3. 13 0
      deployment.pri
  4. 32 0
      main.cpp
  5. 34 0
      main.qml
  6. 83 0
      picturemodel.cpp
  7. 42 0
      picturemodel.h
  8. 5 0
      qml.qrc

+ 73 - 0
.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 18 - 0
artriculate.pro

@@ -0,0 +1,18 @@
+TEMPLATE = app
+
+QT += qml quick
+CONFIG += c++11
+
+SOURCES += main.cpp \
+    picturemodel.cpp
+
+RESOURCES += qml.qrc
+
+# Additional import path used to resolve QML modules in Qt Creator's code model
+QML_IMPORT_PATH =
+
+# Default rules for deployment.
+include(deployment.pri)
+
+HEADERS += \
+    picturemodel.h

+ 13 - 0
deployment.pri

@@ -0,0 +1,13 @@
+unix:!android {
+    isEmpty(target.path) {
+        qnx {
+            target.path = /tmp/$${TARGET}/bin
+        } else {
+            target.path = /opt/$${TARGET}/bin
+        }
+        export(target.path)
+    }
+    INSTALLS += target
+}
+
+export(INSTALLS)

+ 32 - 0
main.cpp

@@ -0,0 +1,32 @@
+#include <QGuiApplication>
+#include <QQmlApplicationEngine>
+#include <QQmlContext>
+#include <QThread>
+
+#include <picturemodel.h>
+
+class PictureThreadWrapper : public QObject {
+public:
+    PictureThreadWrapper(QObject *parent = 0) : QObject (parent) {
+        PictureModel::instance()->addSupportedExtension("jpg");
+        PictureModel::instance()->setModelRoot("/blackhole/media/art");
+    }
+};
+
+int main(int argc, char *argv[])
+{
+    QGuiApplication app(argc, argv);
+
+    QQmlApplicationEngine engine;
+
+    QThread scanningThread;
+    PictureThreadWrapper *wrapper = new PictureThreadWrapper();
+    wrapper->moveToThread(&scanningThread);
+    scanningThread.start();
+
+    engine.rootContext()->setContextProperty("imageModel", PictureModel::instance());
+
+    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
+
+    return app.exec();
+}

+ 34 - 0
main.qml

@@ -0,0 +1,34 @@
+import QtQuick 2.5
+import QtQuick.Window 2.2
+
+Window {
+    id: root
+    visible: true
+    width: 1024
+    height: 768
+
+    ListView {
+        id: view
+        clip: true
+        snapMode: ListView.SnapToItem
+        orientation: ListView.Horizontal
+        anchors.fill: parent
+        delegate: Rectangle {
+            color: "black"
+            width: view.width
+            height: view.height
+            Image {
+                id: artwork
+                property int padding: 0
+                width: parent.width - padding
+                height: parent.height - padding
+                fillMode: Image.PreserveAspectFit
+                anchors.centerIn: parent
+                source: "file://" + modelData
+            }
+        }
+        onWidthChanged: {
+            view.model = imageModel
+        }
+    }
+}

+ 83 - 0
picturemodel.cpp

@@ -0,0 +1,83 @@
+#include "picturemodel.h"
+
+#include <ftw.h>
+#include <stdio.h>
+
+#include <QDir>
+
+#include <QDebug>
+
+PictureModel* PictureModel::model = 0;
+
+int handleDirNode(const char *fpath, const struct stat *sb, int type, struct FTW *ftwbuf) {
+    if (type == FTW_F) {
+      PictureModel::instance()->addPath(fpath);
+    }
+    return 0;
+}
+
+PictureModel::PictureModel(QObject *parent)
+{ /**/ }
+
+PictureModel *PictureModel::instance()
+{
+    if (!model) {
+        model = new PictureModel();
+    }
+    return model;
+}
+
+bool PictureModel::setModelRoot(const QString &root)
+{
+    qDebug() << "Flattening" << root;
+
+    QDir currentDir(root);
+    if (!currentDir.exists()) {
+        qDebug() << "Being told to watch a non existent directory";
+        return false;
+    }
+
+//    QString dirName = currentDir.dirName();
+//    qDebug() << dirName;
+//    currentDir.cdUp();
+//    QDir::setCurrent(currentDir.path());
+
+    nftw(root.toLatin1().data(), handleDirNode, 1000, FTW_PHYS);
+    return true;
+}
+
+int PictureModel::rowCount(const QModelIndex &parent) const
+{
+    return paths.length();
+}
+
+QVariant PictureModel::data(const QModelIndex &index, int role) const
+{
+    if (index.row() < 0 || index.row() >= paths.length())
+        return QVariant();
+
+    return paths.at(index.row());
+}
+
+bool PictureModel::addPath(const QString &path)
+{
+    if (!extensions.isEmpty()) {
+        QString extension = path.mid(path.length() - 3);
+        if (!extensions.contains(extension))
+            return false;
+    }
+    paths << path;
+    return true;
+}
+
+void PictureModel::addSupportedExtension(const QString &extension)
+{
+    extensions << extension;
+}
+
+QHash<int, QByteArray> PictureModel::roleNames() const
+{
+    QHash<int, QByteArray> roles;
+    roles[PathRole] = "path";
+    return roles;
+}

+ 42 - 0
picturemodel.h

@@ -0,0 +1,42 @@
+#ifndef PICTUREMODEL_H
+#define PICTUREMODEL_H
+
+#include <QAbstractListModel>
+
+class Node {
+public:
+  Node(const QString &name) : m_name(name) {}
+  // TODO: symlink considerations
+  // parentNode is clearly always a directory
+private:
+  Node *m_parentNode;
+  QString m_name;
+};
+
+class PictureModel : public QAbstractListModel
+{
+    Q_OBJECT
+public:
+    enum PictureRoles {
+        PathRole = Qt::UserRole + 1
+    };
+
+    static PictureModel* instance();
+    bool setModelRoot(const QString &root);
+    int rowCount(const QModelIndex & parent = QModelIndex()) const;
+
+    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
+
+    bool addPath(const QString &path);
+    void addSupportedExtension(const QString &extension);
+protected:
+    QHash<int, QByteArray> roleNames() const;
+private:
+    static PictureModel* model;
+    PictureModel(QObject *parent = 0);
+    QList<Node> nodes;
+    QStringList paths;
+    QStringList extensions;
+};
+
+#endif // PICTUREMODEL_H

+ 5 - 0
qml.qrc

@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/">
+        <file>main.qml</file>
+    </qresource>
+</RCC>