Reworked PostCaptureModel
authorMohammed Sameer <msameer@foolab.org>
Sun, 25 Aug 2013 19:41:07 +0000 (22:41 +0300)
committerMohammed Sameer <msameer@foolab.org>
Sun, 25 Aug 2013 19:41:07 +0000 (22:41 +0300)
We don't really need to use 2 models. We can simply use QStandardItemModel
and sort the data before we insert them.

src/fileinfomodel.cpp [deleted file]
src/fileinfomodel.h [deleted file]
src/postcapturemodel.cpp
src/postcapturemodel.h
src/src.pro

diff --git a/src/fileinfomodel.cpp b/src/fileinfomodel.cpp
deleted file mode 100644 (file)
index 725e347..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/*!
- * This file is part of CameraPlus.
- *
- * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#include "fileinfomodel.h"
-
-FileInfoModel::FileInfoModel(QObject *parent) :
-  QAbstractListModel(parent) {
-
-}
-
-FileInfoModel::~FileInfoModel() {
-
-}
-
-int FileInfoModel::rowCount(const QModelIndex& parent) const {
-  if (parent.isValid()) {
-    return 0;
-  }
-
-  return m_info.size();
-}
-
-QVariant FileInfoModel::data(const QModelIndex& index, int role) const {
-  if (index.isValid() && index.row() < m_info.count() && role == Qt::DisplayRole) {
-    return m_info[index.row()];
-  }
-
-  return QVariant();
-}
-
-void FileInfoModel::setFiles(const QStringList& files) {
-  clear();
-
-  appendFiles(files);
-}
-
-void FileInfoModel::appendFile(const QString& file) {
-  appendFiles(QStringList() << file);
-}
-
-void FileInfoModel::appendFiles(const QStringList& files) {
-  if (files.isEmpty()) {
-    return;
-  }
-
-  beginInsertRows(QModelIndex(), m_info.size(), files.size() - 1);
-  m_info << files;
-  endInsertRows();
-}
-
-void FileInfoModel::removeFile(const QString& file) {
-  int index = m_info.indexOf(file);
-
-  if (index == -1) {
-    return;
-  }
-
-  beginRemoveRows(QModelIndex(), index, index);
-  m_info.removeAt(index);
-  endRemoveRows();
-}
-
-void FileInfoModel::clear() {
-  if (m_info.isEmpty()) {
-    return;
-  }
-
-  beginRemoveRows(QModelIndex(), 0, m_info.size());
-  m_info.clear();
-  endRemoveRows();
-}
diff --git a/src/fileinfomodel.h b/src/fileinfomodel.h
deleted file mode 100644 (file)
index 6649aa0..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-// -*- c++ -*-
-
-/*!
- * This file is part of CameraPlus.
- *
- * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#ifndef FILE_INFO_MODEL_H
-#define FILE_INFO_MODEL_H
-
-#include <QAbstractListModel>
-#include <QStringList>
-
-class FileInfoModel : public QAbstractListModel {
-  Q_OBJECT
-
-public:
-  FileInfoModel(QObject *parent = 0);
-  ~FileInfoModel();
-
-  int rowCount(const QModelIndex& parent = QModelIndex()) const;
-  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
-
-  void setFiles(const QStringList& files);
-
-  void appendFile(const QString& file);
-  void appendFiles(const QStringList& files);
-
-  void removeFile(const QString& file);
-
-private:
-  void clear();
-
-  QStringList m_info;
-};
-
-#endif /* FILE_INFO_MODEL_H */
index 037d67c..8c4c9e1 100644 (file)
  */
 
 #include "postcapturemodel.h"
-#include "fileinfomodel.h"
 #include <QDir>
 #include <QDateTime>
 #include <QUrl>
+#include <QStandardItem>
 
 static QHash<QString, QString> m_mime;
 
+class PostCaptureModelItem : public QStandardItem {
+public:
+  PostCaptureModelItem(const QString& path) :
+    m_info(QFileInfo(path)),
+    m_path(path) {
+
+  }
+
+  int type() const {
+    return UserType;
+  }
+
+  const QFileInfo *info() const {
+    return &m_info;
+  }
+
+  QString path() {
+    return m_path;
+  }
+
+  QVariant data (int role = Qt::UserRole + 1) const {
+    switch (role) {
+    case PostCaptureModel::UrlRole:
+      return QUrl::fromLocalFile(m_info.absoluteFilePath());
+
+    case PostCaptureModel::TitleRole:
+      return m_info.baseName();
+
+    case PostCaptureModel::MimeTypeRole:
+      if (m_mime.contains(m_info.completeSuffix())) {
+       return m_mime[m_info.completeSuffix()];
+      }
+
+      return QString();
+
+    case PostCaptureModel::CreatedRole:
+      return m_info.created().toString();
+
+    case PostCaptureModel::FileNameRole:
+      return m_info.fileName();
+
+    default:
+      return QVariant();
+    }
+  }
+
+private:
+  QFileInfo m_info;
+  QString m_path;
+};
+
+static bool lessThan(const QStandardItem *a1, const QStandardItem *a2) {
+  // we sort descending
+  return dynamic_cast<const PostCaptureModelItem *>(a1)->info()->created() >
+    dynamic_cast<const PostCaptureModelItem *>(a2)->info()->created();
+}
+
 PostCaptureModel::PostCaptureModel(QObject *parent) :
-  QSortFilterProxyModel(parent),
-  m_model(new FileInfoModel(this)) {
+  QStandardItemModel(parent) {
 
   QHash<int, QByteArray> roles;
   roles.insert(UrlRole, "url");
@@ -37,11 +93,6 @@ PostCaptureModel::PostCaptureModel(QObject *parent) :
   roles.insert(CreatedRole, "created");
   roles.insert(FileNameRole, "fileName");
 
-  setSourceModel(m_model);
-
-  setDynamicSortFilter(true);
-  setSortRole(CreatedRole);
-
   setRoleNames(roles);
 
   if (m_mime.isEmpty()) {
@@ -76,102 +127,46 @@ void PostCaptureModel::setVideoPath(const QString& path) {
   }
 }
 
-QStringList PostCaptureModel::loadDir(QDir& dir) {
-  QStringList files;
+void PostCaptureModel::loadDir(const QDir& dir, QList<QStandardItem *>& out) {
   QStringList entries(dir.entryList(QDir::Files | QDir::NoDotAndDotDot));
 
   foreach (const QString& entry, entries) {
-    files << dir.absoluteFilePath(entry);
+    out << new PostCaptureModelItem(dir.absoluteFilePath(entry));
   }
-
-  return files;
 }
 
 void PostCaptureModel::reload() {
-  QStringList files;
+  QList<QStandardItem *> files;
 
   QDir images(m_imagePath);
   QDir videos(m_videoPath);
 
-  if (images.canonicalPath() == videos.canonicalPath()) {
-    files += loadDir(images);
-  }
-  else {
-    files += loadDir(images);
-    files += loadDir(videos);
-  }
+  loadDir(images, files);
 
-  m_data.clear();
-
-  m_model->setFiles(files);
-
-  sort(0, Qt::DescendingOrder);
-}
-
-void PostCaptureModel::clear() {
-  if (m_model->rowCount(QModelIndex()) == 0) {
-    return;
+  if (images.canonicalPath() != videos.canonicalPath()) {
+    loadDir(videos, files);
   }
 
-  m_data.clear();
+  qSort(files.begin(), files.end(), lessThan);
 
-  m_model->setFiles(QStringList());
+  invisibleRootItem()->appendRows(files);
 }
 
-bool PostCaptureModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
-  return info(sourceModel()->data(left, Qt::DisplayRole).toString()).created() <
-    info(sourceModel()->data(right, Qt::DisplayRole).toString()).created();
+void PostCaptureModel::clear() {
+  QStandardItemModel::clear();
 }
 
-QVariant PostCaptureModel::data(const QModelIndex& index, int role) const {
-  if (!index.isValid() || index.row() < 0) {
-    return QVariant();
-  }
-
-  QFileInfo inf = info(QSortFilterProxyModel::data(index, Qt::DisplayRole).toString());
-  switch (role) {
-  case UrlRole:
-    return QUrl::fromLocalFile(inf.absoluteFilePath());
+void PostCaptureModel::remove(const QUrl& file) {
+  QString path(file.toLocalFile());
 
-  case TitleRole:
-    return inf.baseName();
+  int count = invisibleRootItem()->rowCount();
 
-  case MimeTypeRole:
-    if (m_mime.contains(inf.completeSuffix())) {
-      return m_mime[inf.completeSuffix()];
+  for (int x = 0; x < count; x++) {
+    if (dynamic_cast<PostCaptureModelItem *>(invisibleRootItem()->child(x))->path() == path) {
+      invisibleRootItem()->removeRow(x);
+      return;
     }
-
-    return QString();
-
-  case CreatedRole:
-    return inf.created().toString();
-
-  case FileNameRole:
-    return inf.fileName();
-
-  default:
-    break;
-  }
-
-  return QVariant();
-}
-
-QFileInfo PostCaptureModel::info(const QString& path) const {
-  if (m_data.contains(path)) {
-    return m_data[path];
   }
-
-  QFileInfo inf(path);
-  m_data.insert(path, inf);
-
-  return inf;
-}
-
-void PostCaptureModel::remove(const QUrl& file) {
-  QString path(file.toLocalFile());
-
-  m_data.remove(path);
-  m_model->removeFile(path);
 }
 
 #if defined(QT5)
index 2370214..0d0adf1 100644 (file)
 #ifndef POST_CAPTURE_MODEL_H
 #define POST_CAPTURE_MODEL_H
 
-#include <QSortFilterProxyModel>
-#include <QStringList>
-#include <QFileInfo>
+#include <QStandardItemModel>
 
-class FileInfoModel;
 class QDir;
 
-class PostCaptureModel : public QSortFilterProxyModel {
+class PostCaptureModel : public QStandardItemModel {
   Q_OBJECT
 
   Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged);
   Q_PROPERTY(QString videoPath READ videoPath WRITE setVideoPath NOTIFY videoPathChanged);
 
+public:
+  PostCaptureModel(QObject *parent = 0);
+  ~PostCaptureModel();
+
   typedef enum {
     UrlRole = Qt::UserRole + 1,
     TitleRole = Qt::UserRole + 2,
@@ -44,12 +45,6 @@ class PostCaptureModel : public QSortFilterProxyModel {
     FileNameRole = Qt::UserRole + 5,
   } Roles;
 
-public:
-  PostCaptureModel(QObject *parent = 0);
-  ~PostCaptureModel();
-
-  virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
-
   QString imagePath() const;
   void setImagePath(const QString& path);
 
@@ -65,20 +60,11 @@ signals:
   void imagePathChanged();
   void videoPathChanged();
 
-protected:
-  bool lessThan(const QModelIndex& left, const QModelIndex& right) const;
-
 private:
-  QFileInfo info(const QString& path) const;
-
   QString m_imagePath;
   QString m_videoPath;
 
-  FileInfoModel *m_model;
-
-  QStringList loadDir(QDir& dir);
-
-  mutable QHash<QString, QFileInfo> m_data;
+  void loadDir(const QDir& dir, QList<QStandardItem *>& out);
 
 #if defined(QT5)
   QHash<int, QByteArray> roleNames() const;
index 3ff8d8c..9238f70 100644 (file)
@@ -22,15 +22,13 @@ SOURCES += main.cpp \
            trackerstore.cpp focusrectangle.cpp sharehelper.cpp \
            deletehelper.cpp galleryhelper.cpp postcapturemodel.cpp \
            gridlines.cpp platformsettings.cpp dbusservice.cpp \
-           mountprotector.cpp devicesettings.cpp dirmodel.cpp \
-           fileinfomodel.cpp
+           mountprotector.cpp devicesettings.cpp
 
 HEADERS += settings.h filenaming.h cameraresources.h \
            trackerstore.h focusrectangle.h sharehelper.h \
            deletehelper.h galleryhelper.h postcapturemodel.h \
            gridlines.h platformsettings.h dbusservice.h \
-           mountprotector.h devicesettings.h dirmodel.h \
-           fileinfomodel.h
+           mountprotector.h devicesettings.h
 
 RESOURCES += ../qml/qml.qrc