quake-tools/source/quam/archive.cc
2019-10-04 13:37:34 -04:00

150 lines
3.5 KiB
C++

#include "quam/archive.h"
#include "quam/pack.h"
#include "quam/wad2.h"
#include <QIcon>
#include <QMetaEnum>
Arc::FileType Arc::getFileType(int n) {
if(int t = QMetaEnum::fromType<Arc::FileType>().value(n); n != -1) {
return Arc::FileType(t);
} else {
throw EnumError("invalid file type");
}
}
Arc::ArchiveType Arc::getArchiveType(std::istream &st) noexcept {
Arc::ArchiveType ret = Arc::ArcNone;
try {
auto pos = st.tellg();
auto magic = readBytes<4>(st);
if(magic == std::array{'P', 'A', 'C', 'K'}) {
ret = Arc::ArcPack;
} else if(magic == std::array{'W', 'A', 'D', '2'}) {
ret = Arc::ArcWad2;
}
st.seekg(pos);
} catch(...) {
}
return ret;
}
ArcDir Arc::readArchive(std::istream &st) {
switch(Arc::getArchiveType(st)) {
case Arc::ArcPack: return readPack(st);
case Arc::ArcWad2: return readWad2(st);
case Arc::ArcNone: throw FileFormatError("not an archive");
}
}
ArcDir::iterator ArcDir::findNode(std::string const &name) {
return std::find_if(begin(), end(), [&name](ArcNode const &node) {
return node.name == name;
});
}
ArcNode::ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(f)),
name(std::move(n)),
type(t)
{
}
ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(f)),
name(std::move(n)),
type(t)
{
}
ArcModel::ArcModel(ArcDir const *root, QObject *parent) :
QAbstractItemModel{parent},
m_root{root}
{
}
ArcModel::~ArcModel() {
}
QVariant ArcModel::data(QModelIndex const &index, int role) const {
if(!index.isValid()) {
return QVariant{};
}
auto node = static_cast<ArcNode *>(index.internalPointer());
switch(role) {
case Qt::DecorationRole:
if(index.column() == Arc::ColumnName) {
auto icon =
std::holds_alternative<ArcDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(index.column()) {
case Arc::ColumnSize:
if(auto file = std::get_if<ArcFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Arc::ColumnName:
return QVariant{tr(node->name.data())};
}
default:
break;
}
return QVariant{};
}
Qt::ItemFlags ArcModel::flags(QModelIndex const &index) const {
if(!index.isValid()) {
return Qt::NoItemFlags;
} else {
return Qt::ItemIsSelectable |
Qt::ItemIsDragEnabled |
Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren;
}
}
QVariant ArcModel::headerData(int section,
Qt::Orientation orientation,
int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case Arc::ColumnSize: return QVariant{tr("Size")};
case Arc::ColumnName: return QVariant{tr("Name")};
}
}
return QVariant{};
}
QModelIndex ArcModel::index(int row,
int col,
QModelIndex const &parent) const {
if(!hasIndex(row, col, parent) || row > m_root->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<ArcNode *>(&m_root->at(row)));
}
}
QModelIndex ArcModel::parent(QModelIndex const &) const {
return QModelIndex{};
}
int ArcModel::rowCount(QModelIndex const &) const {
return m_root->size();
}
int ArcModel::columnCount(QModelIndex const &) const {
return Arc::ColumnMax;
}
// EOF