quake-tools/source/quam/archive.h

135 lines
2.6 KiB
C
Raw Normal View History

2019-10-03 12:17:16 -07:00
#pragma once
#include "common.h"
2019-10-04 10:37:34 -07:00
#include <QAbstractItemModel>
2019-10-03 12:17:16 -07:00
#include <QByteArray>
2019-10-09 16:06:14 -07:00
#include <QMetaObject>
2019-10-04 10:37:34 -07:00
#include <QObject>
2019-10-09 16:06:14 -07:00
#include <QValidator>
2019-10-04 10:37:34 -07:00
2019-10-03 12:17:16 -07:00
namespace Arc {
Q_NAMESPACE
2019-10-08 20:43:35 -07:00
enum class Column {
Size,
Type,
Name,
2019-10-03 12:17:16 -07:00
};
Q_ENUM_NS(Column)
enum class FileType {
Label,
MipTexture,
Normal,
Palette,
Picture,
Sound,
Texture,
2019-10-03 12:17:16 -07:00
};
Q_ENUM_NS(FileType)
2019-10-08 20:43:35 -07:00
enum class ArcType {
Pack,
Wad2,
2019-10-04 10:37:34 -07:00
};
2019-10-08 20:43:35 -07:00
Q_ENUM_NS(ArcType)
2019-10-04 10:37:34 -07:00
2019-10-09 16:06:14 -07:00
struct Node;
2019-10-03 12:17:16 -07:00
struct Dir : public std::vector<Node> {
using std::vector<Node>::vector;
2019-10-03 12:17:16 -07:00
2019-10-08 20:43:35 -07:00
Node *findNode(std::string const &name);
2019-10-09 16:06:14 -07:00
static Dir readArchive(std::istream &st, ArcType type);
};
2019-10-03 14:06:05 -07:00
struct File : public QByteArray {
using QByteArray::QByteArray;
};
2019-10-03 12:17:16 -07:00
2019-10-08 20:43:35 -07:00
struct Node : public Variant<Dir, File> {
using superType = Variant<Dir, File>;
2019-10-03 12:17:16 -07:00
Node() = default;
2019-10-03 12:17:16 -07:00
Node(Dir &&f, std::string &&n, FileType t = FileType::Normal);
Node(File &&f, std::string &&n, FileType t = FileType::Normal);
2019-10-03 12:17:16 -07:00
Node(Node const &) = default;
Node(Node &&) = default;
2019-10-03 12:17:16 -07:00
std::string name;
FileType type;
};
class Model : public QAbstractItemModel {
Q_OBJECT
public:
explicit Model(QObject *parent);
~Model();
2019-10-03 12:17:16 -07:00
QVariant data(QModelIndex const &index, int role) const override;
Qt::ItemFlags flags(QModelIndex const &index) const override;
2019-10-08 20:43:35 -07:00
QVariant headerData(int section, Qt::Orientation ori, int role)
const override;
QModelIndex index(int row,
2019-10-08 20:43:35 -07:00
int col,
QModelIndex const &parent = QModelIndex())
const override;
QModelIndex parent(QModelIndex const &index = QModelIndex())
const override;
int rowCount(QModelIndex const &index = QModelIndex()) const override;
int columnCount(QModelIndex const &index = QModelIndex()) const override;
2019-10-03 12:17:16 -07:00
Dir *dir() const;
2019-10-04 10:37:34 -07:00
public slots:
void setDir(Dir *dir);
2019-10-09 16:06:14 -07:00
void setDirToIndex(QModelIndex const &ind);
2019-10-04 10:37:34 -07:00
signals:
2019-10-09 16:06:14 -07:00
void dirChanged(Dir *from, Dir *to);
2019-10-04 10:37:34 -07:00
private:
Dir *m_dir;
};
2019-10-09 16:06:14 -07:00
struct ArcInfo {
ArcInfo(ArcType arcType) noexcept;
ArcType type;
std::size_t pathMaxChars;
QMetaObject validatorType;
};
class Arc : QObject {
Q_OBJECT
public:
explicit Arc(std::istream &st, QObject *parent = nullptr);
~Arc();
ArcInfo info;
Dir root;
QValidator *validator;
};
Option<ArcType> getArchiveType(std::istream &st) noexcept;
}
Q_DECLARE_METATYPE(Arc::Dir)
Q_DECLARE_METATYPE(Arc::File)
Q_DECLARE_METATYPE(Arc::Node)
2019-10-04 10:37:34 -07:00
2019-10-08 20:43:35 -07:00
static inline QDebug operator<<(QDebug debug, Arc::Node const &t) {
debug << "Arc::Node(" << t.name << ", ";
std::visit([&](auto &&arg) {debug << arg;},
static_cast<Variant<Arc::Dir, Arc::File>>(t));
debug << ")";
return debug;
}
2019-10-03 12:17:16 -07:00
// EOF