Compare commits

..

2 Commits

Author SHA1 Message Date
69fe7cf9ca add Project object and some metaobject crap 2019-09-30 01:30:32 -04:00
c6eb45589c make pak views use M/V code 2019-09-29 01:29:31 -04:00
10 changed files with 389 additions and 137 deletions

View File

@ -46,7 +46,10 @@ add_executable(
source/quam/main_window.h
source/quam/main_window.ui
source/quam/pak.cc
source/quam/pak.h)
source/quam/pak.h
source/quam/project.cc
source/quam/project.h
source/quam/project.ui)
make_qt_project(quam)

View File

@ -13,7 +13,7 @@
#include <ios>
#include <istream>
#include <iterator>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>

View File

@ -1,18 +1,23 @@
#include "common.h"
#include "quam/main_window.h"
#include "quam/pak.h"
#include "quam/project.h"
#include <QErrorMessage>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent},
Ui::MainWindow{},
m_errors{}
m_errors{new QErrorMessage{this}}
{
setupUi(this);
actionClose->setShortcut(QKeySequence{QKeySequence::Close});
actionOpen->setShortcut(QKeySequence{QKeySequence::Open});
actionQuit->setShortcut(QKeySequence{QKeySequence::Quit});
}
tableWidget->sortByColumn(Pak::ColumnId, Qt::AscendingOrder);
MainWindow::~MainWindow() {
}
void MainWindow::fileOpen() {
@ -29,11 +34,17 @@ void MainWindow::fileOpen() {
try {
auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st);
setTableToPakDir(*tableWidget, pak);
new Project{std::move(pak), m_errors, mdiArea};
} catch(std::exception const &exc) {
m_errors.showMessage(tr(exc.what()));
m_errors->showMessage(tr(exc.what()));
}
}
}
void MainWindow::fileClose() {
if(auto win = mdiArea->activeSubWindow()) {
mdiArea->removeSubWindow(win);
}
}
// EOF

View File

@ -1,23 +1,26 @@
#pragma once
#include "quam/pak.h"
#include "quam/ui_main_window.h"
#include <QErrorMessage>
#include <QFileDialog>
#include <QMainWindow>
#include <QWidget>
class QErrorMessage;
class MainWindow : public QMainWindow, private Ui::MainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
public slots:
void fileOpen();
void fileClose();
private:
QErrorMessage m_errors;
QErrorMessage *m_errors;
};
// EOF

View File

@ -11,80 +11,26 @@
</rect>
</property>
<property name="windowTitle">
<string>Quam!</string>
<string>QuAM!</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QTableWidget" name="tableWidget">
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
<widget class="QMdiArea" name="mdiArea">
<property name="viewMode">
<enum>QMdiArea::TabbedView</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="autoScroll">
<bool>false</bool>
</property>
<property name="tabKeyNavigation">
<property name="documentMode">
<bool>true</bool>
</property>
<property name="alternatingRowColors">
<property name="tabsClosable">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="sortingEnabled">
<property name="tabsMovable">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>ID</string>
</property>
<property name="toolTip">
<string>The ID of the file. This is the N-th file in the archive.</string>
</property>
</column>
<column>
<property name="text">
<string>Size</string>
</property>
<property name="toolTip">
<string>The size of the file in bytes.</string>
</property>
</column>
<column>
<property name="text">
<string>Name</string>
</property>
<property name="toolTip">
<string>The name of the file.</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
@ -101,6 +47,7 @@
<string>&amp;File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionClose"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
@ -109,7 +56,8 @@
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen">
<property name="icon">
<iconset theme="document-open"/>
<iconset theme="document-open">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Open</string>
@ -117,12 +65,22 @@
</action>
<action name="actionQuit">
<property name="icon">
<iconset theme="application-exit"/>
<iconset theme="application-exit">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Quit</string>
</property>
</action>
<action name="actionClose">
<property name="icon">
<iconset theme="document-close">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Close</string>
</property>
</action>
</widget>
<resources/>
<connections>
@ -158,8 +116,26 @@
</hint>
</hints>
</connection>
<connection>
<sender>actionClose</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>fileClose()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
<slot>selectCell(int,int)</slot>
<slot>fileClose()</slot>
</slots>
</ui>

View File

@ -1,6 +1,9 @@
#include "common.h"
#include "quam/pak.h"
#include <QIcon>
#include <QMetaEnum>
struct PakHeader {
quint32 dirOffset;
quint32 dirNum;
@ -33,7 +36,7 @@ static PakHeader readPakHeader(std::istream &st) {
return hdr;
}
static PakEntry readPakEntry(std::istream &st, quint32 id) {
static PakEntry readPakEntry(std::istream &st) {
auto entName = readBytes<56>(st);
auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st);
@ -43,7 +46,6 @@ static PakEntry readPakEntry(std::istream &st, quint32 id) {
st.seekg(entOffset);
PakFile file;
file.id = id;
file.resize(entSize);
st.read(file.data(), entSize);
@ -65,15 +67,29 @@ static PakEntry readPakEntry(std::istream &st, quint32 id) {
}
void insertFile(PakDir &dir, std::string name, PakFile file) {
std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) {
auto folder = name.substr(0, slash);
auto next = name.substr(slash + 1);
dir[folder] = PakNode{PakDir{}};
insertFile(std::get<PakDir>(dir[folder]),
std::move(next),
std::move(file));
next = name.substr(slash + 1);
name = name.substr(0, slash);
}
auto existingNode = std::find_if(dir.begin(), dir.end(),
[&name](PakNode const &node) {
return node.name == name;
});
if(next) {
auto ref =
existingNode != dir.end()
? *existingNode
: dir.emplace_back(PakDir{}, std::move(name));
insertFile(std::get<PakDir>(ref), *std::move(next), std::move(file));
} else {
dir[name] = PakNode{std::move(file)};
if(existingNode != dir.end()) {
throw std::runtime_error("duplicate file");
}
dir.emplace_back(std::move(file), std::move(name));
}
}
@ -83,57 +99,113 @@ PakDir readPak(std::istream &st) {
PakDir root;
for(quint32 id = 0; id < hdr.dirNum; id++) {
auto ent = readPakEntry(st, id);
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.file));
}
return root;
}
void setTableToPakDir(QTableWidget &table, PakDir const &dir) {
constexpr auto Flags = Qt::ItemIsSelectable |
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;
}
}
auto sorted = table.isSortingEnabled();
table.clearContents();
table.setSortingEnabled(false);
quint32 row{0};
for(auto const &kv : dir) {
auto const &name = kv.first;
auto const &node = kv.second;
table.setRowCount(row + 1);
{
auto item = new QTableWidgetItem;
item->setFlags(Flags);
if(auto file = std::get_if<PakFile>(&node)) {
item->setText(QString::number(file->id));
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")};
}
table.setItem(row, Pak::ColumnId, item);
}
{
auto item = new QTableWidgetItem;
item->setFlags(Flags);
if(auto file = std::get_if<PakFile>(&node)) {
item->setText(QString::number(file->size()));
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)));
}
table.setItem(row, Pak::ColumnSize, item);
}
{
auto item = new QTableWidgetItem;
auto icon = std::holds_alternative<PakDir>(node) ? "folder" :
"text-x-generic";
item->setFlags(Flags);
item->setText(QString::fromStdString(name));
item->setIcon(QIcon::fromTheme(icon));
table.setItem(row, Pak::ColumnName, item);
}
++row;
}
table.setSortingEnabled(sorted);
table.resizeColumnsToContents();
}
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

View File

@ -1,35 +1,83 @@
#pragma once
#include <QTableWidget>
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
namespace Pak {
enum PakColumn {
ColumnId,
ColumnSize,
ColumnName,
Q_NAMESPACE
enum class Column {
Size,
Name,
};
Q_ENUM_NS(Column)
}
struct PakNode;
struct PakDir : public std::map<std::string, PakNode> {
using std::map<std::string, PakNode>::map;
struct PakDir : public std::vector<PakNode> {
using std::vector<PakNode>::vector;
};
Q_DECLARE_METATYPE(PakDir)
struct PakFile : public QByteArray {
using QByteArray::QByteArray;
quint32 id{0};
};
Q_DECLARE_METATYPE(PakFile)
struct PakNode : public std::variant<PakDir, PakFile> {
using std::variant<PakDir, PakFile>::variant;
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);
void setTableToPakDir(QTableWidget &table, PakDir const &dir);
// EOF

41
source/quam/project.cc Normal file
View File

@ -0,0 +1,41 @@
#include "common.h"
#include "quam/project.h"
#include <QAbstractItemModel>
#include <QErrorMessage>
#include <QMdiArea>
#include <QSortFilterProxyModel>
Project::Project(QErrorMessage *errors, QMdiArea *parent) :
QMdiSubWindow{parent},
Ui::Project{},
m_errors{errors}
{
auto widget = new QWidget(this);
setupUi(widget);
setWidget(widget);
setAttribute(Qt::WA_DeleteOnClose);
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() {
}
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

31
source/quam/project.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include "quam/pak.h"
#include "quam/ui_project.h"
#include <QMainWindow>
#include <QMdiSubWindow>
#include <QWidget>
class QAbstractItemModel;
class QErrorMessage;
class QSortFilterProxyModel;
class Project : public QMdiSubWindow, private Ui::Project {
Q_OBJECT
public:
explicit Project(PakDir &&dir, QErrorMessage *errors, QMdiArea *parent);
virtual ~Project();
private:
explicit Project(QErrorMessage *errors, QMdiArea *parent);
void setupModel(QAbstractItemModel *model);
QErrorMessage *m_errors;
QAbstractItemModel *m_model;
QSortFilterProxyModel *m_sorter;
};
// EOF

67
source/quam/project.ui Normal file
View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Project</class>
<widget class="QWidget" name="Project">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Project View</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTableView" name="tableView">
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="autoScroll">
<bool>false</bool>
</property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
<widget class="QPlainTextEdit" name="textEdit"/>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>