154 lines
3.3 KiB
C++
154 lines
3.3 KiB
C++
#include "quam/archive.h"
|
|
|
|
#include "quam/pack.h"
|
|
#include "quam/wad2.h"
|
|
|
|
#include <QIcon>
|
|
|
|
namespace Arc {
|
|
|
|
Option<ArchiveType> getArchiveType(std::istream &st) noexcept {
|
|
try {
|
|
auto pos = st.tellg();
|
|
auto magic = readBytes<4>(st);
|
|
st.seekg(pos);
|
|
if(magic == std::array{'P', 'A', 'C', 'K'}) {return ArcPack;}
|
|
if(magic == std::array{'W', 'A', 'D', '2'}) {return ArcWad2;}
|
|
} catch(...) {
|
|
}
|
|
return None;
|
|
}
|
|
|
|
Dir readArchive(std::istream &st) {
|
|
switch(auto v = getArchiveType(st);
|
|
v.has_value() ? *v : throw FileFormatError("not an archive")) {
|
|
case ArcPack: return readPack(st);
|
|
case ArcWad2: return readWad2(st);
|
|
}
|
|
}
|
|
|
|
Dir::iterator Dir::findNode(std::string const &name) {
|
|
return std::find_if(begin(), end(), [&name](Node const &node) {
|
|
return node.name == name;
|
|
});
|
|
}
|
|
|
|
Node::Node(Dir &&f, std::string &&n, FileType t) :
|
|
super_type(std::move(f)),
|
|
name(std::move(n)),
|
|
type(t)
|
|
{
|
|
}
|
|
|
|
Node::Node(File &&f, std::string &&n, FileType t) :
|
|
super_type(std::move(f)),
|
|
name(std::move(n)),
|
|
type(t)
|
|
{
|
|
}
|
|
|
|
Model::Model(QObject *parent) :
|
|
QAbstractItemModel{parent},
|
|
m_dir{nullptr}
|
|
{
|
|
}
|
|
|
|
Model::~Model() {
|
|
}
|
|
|
|
QVariant Model::data(QModelIndex const &index, int role) const {
|
|
if(!index.isValid()) {
|
|
return QVariant{};
|
|
}
|
|
|
|
auto node = static_cast<Node const *>(index.internalPointer());
|
|
|
|
switch(role) {
|
|
case Qt::DecorationRole:
|
|
if(index.column() == ColumnName) {
|
|
auto icon =
|
|
std::holds_alternative<Dir>(*node) ? "folder"
|
|
: "text-x-generic";
|
|
return QVariant{QIcon::fromTheme(icon)};
|
|
}
|
|
break;
|
|
case Qt::DisplayRole:
|
|
switch(index.column()) {
|
|
case ColumnSize:
|
|
if(auto file = std::get_if<File>(node)) {
|
|
return QVariant{QString::number(file->size())};
|
|
}
|
|
break;
|
|
case ColumnType:
|
|
return QVariant{tr(enumToString(node->type))};
|
|
case ColumnName:
|
|
return QVariant{tr(node->name.data())};
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
return QVariant{};
|
|
}
|
|
|
|
Qt::ItemFlags Model::flags(QModelIndex const &index) const {
|
|
if(!index.isValid()) {
|
|
return Qt::NoItemFlags;
|
|
} else {
|
|
return Qt::ItemIsSelectable |
|
|
Qt::ItemIsDragEnabled |
|
|
Qt::ItemIsEnabled |
|
|
Qt::ItemNeverHasChildren;
|
|
}
|
|
}
|
|
|
|
QVariant Model::headerData(int section,
|
|
Qt::Orientation orientation,
|
|
int role) const {
|
|
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
switch(section) {
|
|
case ColumnSize: return QVariant{tr("Size")};
|
|
case ColumnType: return QVariant{tr("Type")};
|
|
case ColumnName: return QVariant{tr("Name")};
|
|
}
|
|
}
|
|
return QVariant{};
|
|
}
|
|
|
|
QModelIndex Model::index(int row, int col, QModelIndex const &parent) const {
|
|
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
|
|
return QModelIndex{};
|
|
} else {
|
|
// despite index data being const, this function does not take a const
|
|
// pointer, which is very annoying!
|
|
return createIndex(row, col, const_cast<Node *>(&m_dir->at(row)));
|
|
}
|
|
}
|
|
|
|
QModelIndex Model::parent(QModelIndex const &) const {
|
|
return QModelIndex{};
|
|
}
|
|
|
|
int Model::rowCount(QModelIndex const &) const {
|
|
return m_dir ? m_dir->size() : 0;
|
|
}
|
|
|
|
int Model::columnCount(QModelIndex const &) const {
|
|
return enumMax<Column>();
|
|
}
|
|
|
|
Dir *Model::dir() const {
|
|
return m_dir;
|
|
}
|
|
|
|
void Model::setDir(Dir *dir) {
|
|
if(dir != m_dir) {
|
|
m_dir = dir;
|
|
emit layoutChanged();
|
|
emit dirChanged(dir);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// EOF
|