123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "picturemodel.h"
- #include <QDir>
- #include <QDebug>
- struct FSNode {
- FSNode(const QString& rname, const FSNode *pparent = nullptr)
- : name(rname),
- parent(pparent) { /**/ }
- const QString name;
- const FSNode *parent;
- };
- PictureModel::PictureModel(QObject *parent)
- : QAbstractListModel(parent)
- { /**/ }
- PictureModel::~PictureModel()
- {
- // TODO: Destroy model
- }
- void PictureModel::addModelNode(const FSNode* parentNode)
- {
- // TODO: Check for symlink recursion
- QDir parentDir(qualifyNode(parentNode));
- foreach(const QString ¤tDir, parentDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
- const FSNode *dir = new FSNode(currentDir, parentNode);
- addModelNode(dir);
- }
- foreach(const QString ¤tFile, parentDir.entryList(QDir::Files)) {
- if (!extensions.isEmpty()) {
- QString extension = currentFile.mid(currentFile.length() - 3);
- if (!extensions.contains(extension))
- continue;
- }
- const FSNode *file = new FSNode(currentFile, parentNode);
- files << file;
- }
- }
- void PictureModel::setModelRoot(const QString &root)
- {
- QDir currentDir(root);
- if (!currentDir.exists()) {
- qDebug() << "Being told to watch a non existent directory";
- }
- addModelNode(new FSNode(root));
- // foreach(FSNode *node, files) {
- // qDebug() << "Contains:" << qualifyNode(node);
- // }
- }
- int PictureModel::rowCount(const QModelIndex &parent) const
- {
- return files.length();
- }
- QString PictureModel::randomPicture() const
- {
- return qualifyNode(files.at(qrand()%files.size()));
- }
- QVariant PictureModel::data(const QModelIndex &index, int role) const
- {
- if (index.row() < 0 || index.row() >= files.length())
- return QVariant();
- const FSNode *node = files.at(index.row());
- return qualifyNode(node);
- }
- QString PictureModel::qualifyNode(const FSNode *node) const {
- QString qualifiedPath;
- while(node->parent != nullptr) {
- qualifiedPath = "/" + node->name + qualifiedPath;
- node = node->parent;
- }
- qualifiedPath = node->name + qualifiedPath;
- return qualifiedPath;
- }
- void PictureModel::addSupportedExtension(const QString &extension)
- {
- extensions << extension;
- }
- QHash<int, QByteArray> PictureModel::roleNames() const
- {
- QHash<int, QByteArray> roles;
- roles[PathRole] = "path";
- return roles;
- }
|