quake-tools/source/quam/archive.cc

154 lines
3.3 KiB
C++
Raw Normal View History

2019-10-03 12:17:16 -07:00
#include "quam/archive.h"
2019-10-04 10:37:34 -07:00
#include "quam/pack.h"
#include "quam/wad2.h"
#include <QIcon>
2019-10-03 12:17:16 -07:00
namespace Arc {
2019-10-04 10:37:34 -07:00
Option<ArchiveType> getArchiveType(std::istream &st) noexcept {
2019-10-04 10:37:34 -07:00
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;}
2019-10-04 10:37:34 -07:00
} catch(...) {
}
return None;
2019-10-04 10:37:34 -07:00
}
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);
2019-10-03 12:17:16 -07:00
}
}
Dir::iterator Dir::findNode(std::string const &name) {
return std::find_if(begin(), end(), [&name](Node const &node) {
2019-10-03 14:06:05 -07:00
return node.name == name;
});
}
Node::Node(Dir &&f, std::string &&n, FileType t) :
2019-10-03 14:06:05 -07:00
super_type(std::move(f)),
2019-10-03 12:17:16 -07:00
name(std::move(n)),
2019-10-03 14:06:05 -07:00
type(t)
2019-10-03 12:17:16 -07:00
{
}
Node::Node(File &&f, std::string &&n, FileType t) :
2019-10-03 14:06:05 -07:00
super_type(std::move(f)),
2019-10-03 12:17:16 -07:00
name(std::move(n)),
2019-10-03 14:06:05 -07:00
type(t)
{
}
Model::Model(QObject *parent) :
2019-10-03 14:06:05 -07:00
QAbstractItemModel{parent},
m_dir{nullptr}
2019-10-03 12:17:16 -07:00
{
}
Model::~Model() {
2019-10-03 14:06:05 -07:00
}
QVariant Model::data(QModelIndex const &index, int role) const {
2019-10-03 14:06:05 -07:00
if(!index.isValid()) {
return QVariant{};
}
auto node = static_cast<Node const *>(index.internalPointer());
2019-10-03 14:06:05 -07:00
switch(role) {
case Qt::DecorationRole:
if(index.column() == ColumnName) {
2019-10-03 14:06:05 -07:00
auto icon =
std::holds_alternative<Dir>(*node) ? "folder"
2019-10-03 14:06:05 -07:00
: "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)) {
2019-10-03 14:06:05 -07:00
return QVariant{QString::number(file->size())};
}
break;
case ColumnType:
return QVariant{tr(enumToString(node->type))};
case ColumnName:
2019-10-03 14:06:05 -07:00
return QVariant{tr(node->name.data())};
}
default:
break;
}
return QVariant{};
}
Qt::ItemFlags Model::flags(QModelIndex const &index) const {
2019-10-03 14:06:05 -07:00
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 {
2019-10-03 14:06:05 -07:00
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")};
2019-10-03 14:06:05 -07:00
}
}
return QVariant{};
}
QModelIndex Model::index(int row, int col, QModelIndex const &parent) const {
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
2019-10-03 14:06:05 -07:00
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)));
2019-10-03 14:06:05 -07:00
}
}
QModelIndex Model::parent(QModelIndex const &) const {
2019-10-03 14:06:05 -07:00
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);
}
2019-10-03 14:06:05 -07:00
}
}
2019-10-03 12:17:16 -07:00
// EOF