瀏覽代碼

added AlbumModel and PictureModel files

runningwater 2 年之前
父節點
當前提交
6c1fd99ead
共有 5 個文件被更改,包括 221 次插入1 次删除
  1. 83 0
      gallery-core/AlbumModel.cpp
  2. 39 0
      gallery-core/AlbumModel.h
  3. 51 0
      gallery-core/PictureModel.cpp
  4. 43 0
      gallery-core/PictureModel.h
  5. 5 1
      gallery-core/gallery-core.pro

+ 83 - 0
gallery-core/AlbumModel.cpp

@@ -0,0 +1,83 @@
+#include "AlbumModel.h"
+using namespace std;
+
+AlbumModel::AlbumModel(QObject* parent)
+    : QAbstractListModel(parent),
+      mDb(DatabaseManager::instance()),
+      mAlbums(mDb.albumDao.albums()) {}
+
+QModelIndex AlbumModel::addAlbum(const Album& album) {
+  int rowIndex = rowCount();
+
+  beginInsertRows(QModelIndex(), rowIndex, rowIndex);
+  unique_ptr<Album> newAlbum(new Album(album));
+  mDb.albumDao.addAlbum(*newAlbum);
+  mAlbums->push_back(std::move(newAlbum));
+  endInsertRows();
+
+  return index(rowIndex, 0);
+}
+
+int AlbumModel::rowCount(const QModelIndex& parent) const {
+  return mAlbums->size();
+}
+
+QVariant AlbumModel::data(const QModelIndex& index, int role) const {
+  if (!isIndexValid(index)) {
+    return QVariant();
+  }
+  const Album& album = *mAlbums->at(index.row());
+
+  switch (role) {
+    case Roles::IdRole:
+      return album.id();
+    case Roles::NameRole:
+    case Qt::DisplayRole:
+      return album.name();
+    default:
+      return QVariant();
+  }
+}
+
+bool AlbumModel::setData(const QModelIndex& index,
+                         const QVariant& value,
+                         int role) {
+  if (!isIndexValid(index) || role != Roles::NameRole) {
+    return false;
+  }
+  Album& album = *mAlbums->at(index.row());
+  album.setName(value.toString());
+  mDb.albumDao.updateAlbum(album);
+  emit dataChanged(index, index);
+  return true;
+}
+
+bool AlbumModel::removeRows(int row, int count, const QModelIndex& parent) {
+  if (row < 0 || row >= rowCount() || count < 0 || (row + count) > rowCount()) {
+    return false;
+  }
+  beginRemoveRows(parent, row, row + count - 1);
+  int countLeft = count;
+  while (countLeft--) {
+    const Album& album = *mAlbums->at(row + countLeft);
+    mDb.albumDao.removeAlbum(album.id());
+  }
+  mAlbums->erase(mAlbums->begin() + row, mAlbums->begin() + row + count);
+  endRemoveRows();
+  return true;
+}
+
+QHash<int, QByteArray> AlbumModel::roleNames() const {
+  QHash<int, QByteArray> roles;
+  ;
+  roles[Roles::IdRole] = "id";
+  roles[Roles::NameRole] = "name";
+  return roles;
+}
+
+bool AlbumModel::isIndexValid(const QModelIndex& index) const {
+  if (index.row() < 0 || index.row() >= rowCount() || !index.isValid()) {
+    return false;
+  }
+  return true;
+}

+ 39 - 0
gallery-core/AlbumModel.h

@@ -0,0 +1,39 @@
+#ifndef ALBUMMODEL_H
+#define ALBUMMODEL_H
+
+#include <QAbstractListModel>
+
+#include "Album.h"
+#include "DatabaseManager.h"
+#include "gallery-core_global.h"
+
+class GALLERYCORE_EXPORT AlbumModel : public QAbstractListModel {
+  Q_OBJECT
+ public:
+  enum Roles {
+    IdRole = Qt::UserRole + 1,
+    NameRole,
+  };
+  AlbumModel(QObject *parent = 0);
+
+  QModelIndex addAlbum(const Album &album);
+
+  // QAbstractItemModel interface
+ public:
+  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
+  QVariant data(const QModelIndex &index,
+                int role = Qt::DisplayRole) const override;
+  bool setData(const QModelIndex &index, const QVariant &value,
+               int role) override;
+  bool removeRows(int row, int count, const QModelIndex &parent) override;
+  QHash<int, QByteArray> roleNames() const override;
+
+ private:
+  bool isIndexValid(const QModelIndex &index) const;
+
+ private:
+  DatabaseManager &mDb;
+  std::unique_ptr<std::vector<std::unique_ptr<Album>>> mAlbums;
+};
+
+#endif  // ALBUMMODEL_H

+ 51 - 0
gallery-core/PictureModel.cpp

@@ -0,0 +1,51 @@
+#include "PictureModel.h"
+
+PictureModel::PictureModel(const AlbumModel &albumModel, QObject *parent)
+{
+
+}
+
+QModelIndex PictureModel::addPicture(const Picture &picture)
+{
+
+}
+
+int PictureModel::rowCount(const QModelIndex &parent) const
+{
+
+}
+
+QVariant PictureModel::data(const QModelIndex &index, int role) const
+{
+
+}
+
+bool PictureModel::removeRows(int row, int count, const QModelIndex &parent)
+{
+
+}
+
+void PictureModel::setAlbumId(int albumId)
+{
+
+}
+
+void PictureModel::clearAlbum()
+{
+
+}
+
+void PictureModel::deletePcituresForAlbum()
+{
+
+}
+
+void PictureModel::loadPictures(int albumId)
+{
+
+}
+
+bool PictureModel::isIndexValid(const QModelIndex &index) const
+{
+
+}

+ 43 - 0
gallery-core/PictureModel.h

@@ -0,0 +1,43 @@
+#ifndef PICTUREMODEL_H
+#define PICTUREMODEL_H
+
+#include <QAbstractListModel>
+#include "Picture.h"
+#include "gallery-core_global.h"
+
+class Album;
+class DatabaseManager;
+class AlbumModel;
+
+class GALLERYCORE_EXPORT PictureModel : public QAbstractListModel {
+  Q_OBJECT
+ public:
+  enum PictureRole {
+    FilePathRole = Qt::UserRole + 1,
+  };
+  explicit PictureModel(const AlbumModel& albumModel, QObject* parent = 0);
+
+  QModelIndex addPicture(const Picture& picture);
+
+  // QAbstractItemModel interface
+  int rowCount(const QModelIndex& parent) const override;
+  QVariant data(const QModelIndex& index, int role) const override;
+  bool removeRows(int row, int count, const QModelIndex& parent) override;
+
+  void setAlbumId(int albumId);
+  void clearAlbum();
+
+ public slots:
+  void deletePcituresForAlbum();
+
+ private:
+  void loadPictures(int albumId);
+  bool isIndexValid(const QModelIndex& index) const;
+
+ private:
+  DatabaseManager& mDb;
+  int mAlbumId;
+  std::unique_ptr<std::vector<std::unique_ptr<Picture>>> mPictures;
+};
+
+#endif  // PICTUREMODEL_H

+ 5 - 1
gallery-core/gallery-core.pro

@@ -12,15 +12,19 @@ CONFIG += c++17
 
 SOURCES += \
     AlbumDao.cpp \
+    AlbumModel.cpp \
     DatabaseManager.cpp \
     Picture.cpp \
     Album.cpp \
-    PictureDao.cpp
+    PictureDao.cpp \
+    PictureModel.cpp
 
 HEADERS += \
     AlbumDao.h \
+    AlbumModel.h \
     DatabaseManager.h \
     PictureDao.h \
+    PictureModel.h \
     gallery-core_global.h \
     Picture.h \
     Album.h