Переглянути джерело

Get model notification working correctly

Change-Id: I66ec4b8e90835e3bcb8149384a334357e4698624
Donald Carr 9 роки тому
батько
коміт
a84cba8a95
4 змінених файлів з 37 додано та 7 видалено
  1. 8 1
      qml/main.qml
  2. 17 5
      src/main.cpp
  3. 6 1
      src/picturemodel.cpp
  4. 6 0
      src/picturemodel.h

+ 8 - 1
qml/main.qml

@@ -42,15 +42,22 @@ Window {
 
     Rectangle {
         visible: imageModel.rowCount() === 0
+
+        function checkModel() {
+            visible = (imageModel.rowCount() === 0)
+        }
+
         color: "red"
         width: childrenRect.width
         height: childrenRect.height
-
         anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter }
+
         Text {
             font.pointSize: 40
             text: "No images found/provided"
         }
+
+        Component.onCompleted: modelRelay.countChanged.connect(checkModel);
     }
 
     Component.onCompleted: showFullScreen()

+ 17 - 5
src/main.cpp

@@ -14,6 +14,13 @@
 #include <QTextStream>
 
 #include <QDebug>
+#include <QAbstractItemModel>
+
+class ModelRelay : public QObject {
+    Q_OBJECT
+signals:
+    void countChanged();
+};
 
 class FileReader : public QObject {
     Q_OBJECT
@@ -56,17 +63,22 @@ int main(int argc, char *argv[])
     QQmlApplicationEngine engine;
     QThread scanningThread;
     PictureModel *model = new PictureModel();
-    const QString &artPath = settings.value("artPath","/blackhole/media/art").toString();
+    QString artPath = settings.value("artPath","/blackhole/media/art").toString();
+    QStringList extensions = settings.value("extensions", QStringList() << "jpg" << "png").toStringList();
+    settings.setValue("artPath", artPath);
+    settings.setValue("extensions", extensions);
 
-    model->addSupportedExtension("jpg");
-    model->addSupportedExtension("png");
+    model->setSupportedExtensions(extensions);
     model->moveToThread(&scanningThread);
     scanningThread.start();
-
     QMetaObject::invokeMethod(model, "setModelRoot", Qt::QueuedConnection, Q_ARG(QString,artPath));
-    settings.setValue("artPath", artPath);
+
+    ModelRelay modelRelay;
+    QObject::connect(model, &PictureModel::countChanged, &modelRelay, &ModelRelay::countChanged, Qt::QueuedConnection);
 
     engine.rootContext()->setContextProperty("imageModel", model);
+    engine.rootContext()->setContextProperty("modelRelay", &modelRelay);
+
     engine.rootContext()->setContextProperty("fileReader", new FileReader(&app));
     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
 

+ 6 - 1
src/picturemodel.cpp

@@ -42,6 +42,7 @@ void PictureModel::addModelNode(const FSNode* parentNode)
         }
         const FSNode *file = new FSNode(currentFile, parentNode);
         files << file;
+        emit countChanged();
     }
 }
 
@@ -55,7 +56,11 @@ void PictureModel::setModelRoot(const QString &root)
 
 //    foreach(FSNode *node, files) {
 //        qDebug() << "Contains:" << qualifyNode(node);
-//    }
+    //    }
+}
+
+void PictureModel::setSupportedExtensions(QStringList extensions) {
+    this->extensions = extensions;
 }
 
 int PictureModel::rowCount(const QModelIndex &parent) const

+ 6 - 0
src/picturemodel.h

@@ -9,6 +9,7 @@ class FSNode;
 class PictureModel : public QAbstractListModel
 {
     Q_OBJECT
+    Q_PROPERTY (int count READ rowCount NOTIFY countChanged)
 public:
     enum PictureRoles {
         PathRole = Qt::UserRole + 1
@@ -23,10 +24,15 @@ public:
     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
 
     Q_INVOKABLE void setModelRoot(const QString &root);
+
+    void setSupportedExtensions(QStringList extensions);
     void addSupportedExtension(const QString &extension);
     void addModelNode(const FSNode *parent);
     QString qualifyNode(const FSNode *node) const;
 
+signals:
+    void countChanged();
+
 protected:
     QHash<int, QByteArray> roleNames() const;