This commit is contained in:
alison wright 2019-10-03 15:17:16 -04:00
parent 69fe7cf9ca
commit f42028a378
9 changed files with 107 additions and 215 deletions

View File

@ -41,6 +41,8 @@ find_package(
add_executable( add_executable(
quam WIN32 quam WIN32
source/common.h source/common.h
source/quam/archive.cc
source/quam/archive.h
source/quam/main.cc source/quam/main.cc
source/quam/main_window.cc source/quam/main_window.cc
source/quam/main_window.h source/quam/main_window.h

27
source/quam/archive.cc Normal file
View File

@ -0,0 +1,27 @@
#include "quam/archive.h"
#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 std::range_error("invalid file type");
}
}
ArcNode::ArcNode(ArcDir &&t, std::string &&n, Arc::FileType ty) :
super_type(std::move(t)),
name(std::move(n)),
type(ty)
{
}
ArcNode::ArcNode(ArcFile &&t, std::string &&n, Arc::FileType ty) :
super_type(std::move(t)),
name(std::move(n)),
type(ty)
{
}
// EOF

61
source/quam/archive.h Normal file
View File

@ -0,0 +1,61 @@
#pragma once
#include "common.h"
#include <QObject>
#include <QByteArray>
namespace Arc {
Q_NAMESPACE
enum Column {
ColumnSize,
ColumnName,
ColumnMax,
};
Q_ENUM_NS(Column)
enum FileType {
FileNormal,
FileLabel,
FilePalette = 64,
FileTexture,
FilePicture,
FileSound,
FileMipTexture,
};
Q_ENUM_NS(FileType)
Arc::FileType getFileType(int n);
}
struct ArcNode;
struct ArcDir : public std::vector<ArcNode> {
using std::vector<ArcNode>::vector;
};
Q_DECLARE_METATYPE(ArcDir)
struct ArcFile : public QByteArray {
using QByteArray::QByteArray;
};
Q_DECLARE_METATYPE(ArcFile)
struct ArcNode : public std::variant<ArcDir, ArcFile> {
using super_type = std::variant<ArcDir, ArcFile>;
ArcNode() = default;
ArcNode(ArcDir &&t, std::string &&n, Arc::FileType ty = Arc::FileNormal);
ArcNode(ArcFile &&t, std::string &&n, Arc::FileType ty = Arc::FileNormal);
ArcNode(ArcNode const &) = default;
ArcNode(ArcNode &&) = default;
std::string name;
Arc::FileType type;
};
Q_DECLARE_METATYPE(ArcNode)
// EOF

View File

@ -1,4 +1,3 @@
#include "common.h"
#include "quam/main_window.h" #include "quam/main_window.h"
#include "quam/project.h" #include "quam/project.h"
@ -34,7 +33,8 @@ void MainWindow::fileOpen() {
try { try {
auto st = openReadBin(fileName.toStdString()); auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st); auto pak = readPak(st);
new Project{std::move(pak), m_errors, mdiArea}; Q_UNUSED(pak);
//new Project{std::move(pak), m_errors, mdiArea};
} catch(std::exception const &exc) { } catch(std::exception const &exc) {
m_errors->showMessage(tr(exc.what())); m_errors->showMessage(tr(exc.what()));
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "common.h"
#include "quam/pak.h" #include "quam/pak.h"
#include "quam/ui_main_window.h" #include "quam/ui_main_window.h"

View File

@ -1,4 +1,3 @@
#include "common.h"
#include "quam/pak.h" #include "quam/pak.h"
#include <QIcon> #include <QIcon>
@ -11,7 +10,7 @@ struct PakHeader {
struct PakEntry { struct PakEntry {
std::string name; std::string name;
PakFile file; ArcFile file;
}; };
static constexpr quint32 sizeOfPakEntry = 64; static constexpr quint32 sizeOfPakEntry = 64;
@ -45,15 +44,14 @@ static PakEntry readPakEntry(std::istream &st) {
st.seekg(entOffset); st.seekg(entOffset);
PakFile file; ArcFile file;
file.resize(entSize); file.resize(entSize);
st.read(file.data(), entSize); st.read(file.data(), entSize);
st.seekg(pos); st.seekg(pos);
auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
std::string name; std::string name;
auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
std::copy(entName.cbegin(), zero, std::back_inserter(name)); std::copy(entName.cbegin(), zero, std::back_inserter(name));
if(name.front() == '/') { if(name.front() == '/') {
@ -66,7 +64,7 @@ static PakEntry readPakEntry(std::istream &st) {
return ent; return ent;
} }
void insertFile(PakDir &dir, std::string name, PakFile file) { void insertFile(ArcDir &dir, std::string name, ArcFile file) {
std::optional<std::string> next; std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) { if(auto slash = name.find('/'); slash != std::string::npos) {
@ -75,7 +73,7 @@ void insertFile(PakDir &dir, std::string name, PakFile file) {
} }
auto existingNode = std::find_if(dir.begin(), dir.end(), auto existingNode = std::find_if(dir.begin(), dir.end(),
[&name](PakNode const &node) { [&name](ArcNode const &node) {
return node.name == name; return node.name == name;
}); });
@ -83,8 +81,8 @@ void insertFile(PakDir &dir, std::string name, PakFile file) {
auto ref = auto ref =
existingNode != dir.end() existingNode != dir.end()
? *existingNode ? *existingNode
: dir.emplace_back(PakDir{}, std::move(name)); : dir.emplace_back(ArcDir{}, std::move(name));
insertFile(std::get<PakDir>(ref), *std::move(next), std::move(file)); insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file));
} else { } else {
if(existingNode != dir.end()) { if(existingNode != dir.end()) {
throw std::runtime_error("duplicate file"); throw std::runtime_error("duplicate file");
@ -93,11 +91,11 @@ void insertFile(PakDir &dir, std::string name, PakFile file) {
} }
} }
PakDir readPak(std::istream &st) { ArcDir readPak(std::istream &st) {
auto hdr = readPakHeader(st); auto hdr = readPakHeader(st);
st.seekg(hdr.dirOffset); st.seekg(hdr.dirOffset);
PakDir root; ArcDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st); auto ent = readPakEntry(st);
@ -107,105 +105,4 @@ PakDir readPak(std::istream &st) {
return root; return root;
} }
PakDirRoot::PakDirRoot(PakDir &&root, QObject *parent) :
QObject{parent},
PakDir{std::move(root)}
{
}
PakDirRoot::~PakDirRoot() {
}
PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
QAbstractItemModel{parent},
m_root{root}
{
}
PakDirModel::~PakDirModel() {
}
QVariant PakDirModel::data(QModelIndex const &index, int role) const {
if(!index.isValid()) {
return QVariant{};
}
auto node = static_cast<PakNode *>(index.internalPointer());
auto col = Pak::Column(index.column());
switch(role) {
case Qt::DecorationRole:
if(col == Pak::Column::Name) {
auto icon =
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(col) {
case Pak::Column::Size:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Pak::Column::Name:
return QVariant{tr(node->name.data())};
}
default:
break;
}
return QVariant{};
}
Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const {
if(!index.isValid()) {
return Qt::NoItemFlags;
} else {
return Qt::ItemIsSelectable |
Qt::ItemIsDragEnabled |
Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren;
}
}
QVariant PakDirModel::headerData(int section,
Qt::Orientation orientation,
int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(Pak::Column(section)) {
case Pak::Column::Size: return QVariant{tr("Size")};
case Pak::Column::Name: return QVariant{tr("Name")};
}
}
return QVariant{};
}
QModelIndex PakDirModel::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<PakNode *>(&m_root->at(row)));
}
}
QModelIndex PakDirModel::parent(QModelIndex const &) const {
return QModelIndex{};
}
int PakDirModel::rowCount(QModelIndex const &) const {
return m_root->size();
}
int PakDirModel::columnCount(QModelIndex const &) const {
return QMetaEnum::fromType<Pak::Column>().keyCount();
}
PakDirModelSorter::~PakDirModelSorter() {
}
// EOF // EOF

View File

@ -1,83 +1,8 @@
#pragma once #pragma once
#include <QAbstractItemModel> #include "common.h"
#include <QSortFilterProxyModel> #include "quam/archive.h"
namespace Pak { ArcDir readPak(std::istream &st);
Q_NAMESPACE
enum class Column {
Size,
Name,
};
Q_ENUM_NS(Column)
}
struct PakNode;
struct PakDir : public std::vector<PakNode> {
using std::vector<PakNode>::vector;
};
Q_DECLARE_METATYPE(PakDir)
struct PakFile : public QByteArray {
using QByteArray::QByteArray;
};
Q_DECLARE_METATYPE(PakFile)
struct PakNode : public std::variant<PakDir, PakFile> {
PakNode() = default;
template<typename T>
constexpr PakNode(T &&t, std::string &&n) :
variant(std::move(t)),
name(std::move(n)) {
}
PakNode(PakNode const &) = default;
PakNode(PakNode &&) = default;
std::string name;
};
Q_DECLARE_METATYPE(PakNode)
class PakDirRoot : public QObject, public PakDir {
Q_OBJECT
public:
explicit PakDirRoot(PakDir &&root, QObject *parent);
virtual ~PakDirRoot();
};
class PakDirModel : public QAbstractItemModel {
Q_OBJECT
public:
explicit PakDirModel(PakDir const *root, QObject *parent);
virtual ~PakDirModel();
QVariant data(QModelIndex const &index, int role) const override;
Qt::ItemFlags flags(QModelIndex const &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role)
const override;
QModelIndex index(int row, int col, QModelIndex const &parent)
const override;
QModelIndex parent(QModelIndex const &index) const override;
int rowCount(QModelIndex const &parent) const override;
int columnCount(QModelIndex const &parent) const override;
private:
PakDir const *const m_root;
};
class PakDirModelSorter : public QSortFilterProxyModel {
Q_OBJECT
public:
using QSortFilterProxyModel::QSortFilterProxyModel;
virtual ~PakDirModelSorter();
};
PakDir readPak(std::istream &st);
// EOF // EOF

View File

@ -1,4 +1,3 @@
#include "common.h"
#include "quam/project.h" #include "quam/project.h"
#include <QAbstractItemModel> #include <QAbstractItemModel>
@ -18,24 +17,7 @@ Project::Project(QErrorMessage *errors, QMdiArea *parent) :
showMaximized(); showMaximized();
} }
Project::Project(PakDir &&dir, QErrorMessage *errors, QMdiArea *parent) :
Project{errors, parent}
{
auto root = new PakDirRoot{std::move(dir), this};
auto model = new PakDirModel{root, this};
setupModel(model);
}
Project::~Project() { Project::~Project() {
} }
void Project::setupModel(QAbstractItemModel *model) {
m_model = model;
m_sorter = new QSortFilterProxyModel{this};
m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter);
tableView->sortByColumn(int(Pak::Column::Name), Qt::AscendingOrder);
tableView->resizeColumnsToContents();
}
// EOF // EOF

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "common.h"
#include "quam/pak.h" #include "quam/pak.h"
#include "quam/ui_project.h" #include "quam/ui_project.h"
@ -15,14 +16,10 @@ class Project : public QMdiSubWindow, private Ui::Project {
Q_OBJECT Q_OBJECT
public: public:
explicit Project(PakDir &&dir, QErrorMessage *errors, QMdiArea *parent); explicit Project(QErrorMessage *errors, QMdiArea *parent);
virtual ~Project(); virtual ~Project();
private: private:
explicit Project(QErrorMessage *errors, QMdiArea *parent);
void setupModel(QAbstractItemModel *model);
QErrorMessage *m_errors; QErrorMessage *m_errors;
QAbstractItemModel *m_model; QAbstractItemModel *m_model;
QSortFilterProxyModel *m_sorter; QSortFilterProxyModel *m_sorter;