make pak views use M/V code

This commit is contained in:
alison wright 2019-09-29 01:29:31 -04:00
parent 89414dd5ec
commit c6eb45589c
6 changed files with 266 additions and 134 deletions

View File

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

View File

@ -2,17 +2,30 @@
#include "quam/main_window.h" #include "quam/main_window.h"
#include "quam/pak.h" #include "quam/pak.h"
#include <QAbstractItemModel>
#include <QErrorMessage>
#include <QFileDialog>
#include <QSortFilterProxyModel>
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent}, QMainWindow{parent},
Ui::MainWindow{}, Ui::MainWindow{},
m_errors{} m_directory{},
m_errors{new QErrorMessage{this}},
m_model{nullptr},
m_sorter{nullptr}
{ {
setupUi(this); setupUi(this);
actionClose->setShortcut(QKeySequence{QKeySequence::Close});
actionOpen->setShortcut(QKeySequence{QKeySequence::Open}); actionOpen->setShortcut(QKeySequence{QKeySequence::Open});
actionQuit->setShortcut(QKeySequence{QKeySequence::Quit}); actionQuit->setShortcut(QKeySequence{QKeySequence::Quit});
tableWidget->sortByColumn(Pak::ColumnId, Qt::AscendingOrder); tableView->sortByColumn(Pak::ColumnSize, Qt::AscendingOrder);
tableView->resizeColumnsToContents();
}
MainWindow::~MainWindow() {
} }
void MainWindow::fileOpen() { void MainWindow::fileOpen() {
@ -28,12 +41,22 @@ void MainWindow::fileOpen() {
if(!fileName.isEmpty()) { if(!fileName.isEmpty()) {
try { try {
auto st = openReadBin(fileName.toStdString()); auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st); m_directory = readPak(st);
setTableToPakDir(*tableWidget, pak); m_model = new PakDirModel{&m_directory, this};
m_sorter = new PakDirModelSorter{m_model};
m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter);
} catch(std::exception const &exc) { } catch(std::exception const &exc) {
m_errors.showMessage(tr(exc.what())); m_errors->showMessage(tr(exc.what()));
} }
} }
} }
void MainWindow::fileClose() {
tableView->setModel(nullptr);
delete m_sorter;
delete m_model;
m_directory = PakDir{};
}
// EOF // EOF

View File

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

View File

@ -11,12 +11,16 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Quam!</string> <string>QuAM!</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_4">
<item> <item>
<widget class="QTableWidget" name="tableWidget"> <widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTableView" name="tableView">
<property name="verticalScrollBarPolicy"> <property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum> <enum>Qt::ScrollBarAlwaysOn</enum>
</property> </property>
@ -47,43 +51,12 @@
<attribute name="horizontalHeaderVisible"> <attribute name="horizontalHeaderVisible">
<bool>true</bool> <bool>true</bool>
</attribute> </attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible"> <attribute name="verticalHeaderVisible">
<bool>false</bool> <bool>false</bool>
</attribute> </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> </widget>
</item>
<item>
<widget class="QPlainTextEdit" name="textEdit"/> <widget class="QPlainTextEdit" name="textEdit"/>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -101,6 +74,7 @@
<string>&amp;File</string> <string>&amp;File</string>
</property> </property>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="actionClose"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionQuit"/> <addaction name="actionQuit"/>
</widget> </widget>
@ -109,7 +83,8 @@
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen"> <action name="actionOpen">
<property name="icon"> <property name="icon">
<iconset theme="document-open"/> <iconset theme="document-open">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Open</string> <string>&amp;Open</string>
@ -117,12 +92,21 @@
</action> </action>
<action name="actionQuit"> <action name="actionQuit">
<property name="icon"> <property name="icon">
<iconset theme="application-exit"/> <iconset theme="application-exit">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Quit</string> <string>&amp;Quit</string>
</property> </property>
</action> </action>
<action name="actionClose">
<property name="icon">
<iconset theme="document-close"/>
</property>
<property name="text">
<string>&amp;Close</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections> <connections>
@ -158,8 +142,26 @@
</hint> </hint>
</hints> </hints>
</connection> </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> </connections>
<slots> <slots>
<slot>fileOpen()</slot> <slot>fileOpen()</slot>
<slot>selectCell(int,int)</slot>
<slot>fileClose()</slot>
</slots> </slots>
</ui> </ui>

View File

@ -1,6 +1,8 @@
#include "common.h" #include "common.h"
#include "quam/pak.h" #include "quam/pak.h"
#include <QIcon>
struct PakHeader { struct PakHeader {
quint32 dirOffset; quint32 dirOffset;
quint32 dirNum; quint32 dirNum;
@ -33,7 +35,7 @@ static PakHeader readPakHeader(std::istream &st) {
return hdr; return hdr;
} }
static PakEntry readPakEntry(std::istream &st, quint32 id) { static PakEntry readPakEntry(std::istream &st) {
auto entName = readBytes<56>(st); auto entName = readBytes<56>(st);
auto entOffset = readLE<quint32>(st); auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st); auto entSize = readLE<quint32>(st);
@ -43,7 +45,6 @@ static PakEntry readPakEntry(std::istream &st, quint32 id) {
st.seekg(entOffset); st.seekg(entOffset);
PakFile file; PakFile file;
file.id = id;
file.resize(entSize); file.resize(entSize);
st.read(file.data(), entSize); st.read(file.data(), entSize);
@ -65,15 +66,29 @@ static PakEntry readPakEntry(std::istream &st, quint32 id) {
} }
void insertFile(PakDir &dir, std::string name, PakFile file) { void insertFile(PakDir &dir, std::string name, PakFile file) {
std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) { if(auto slash = name.find('/'); slash != std::string::npos) {
auto folder = name.substr(0, slash); next = name.substr(slash + 1);
auto next = name.substr(slash + 1); name = name.substr(0, slash);
dir[folder] = PakNode{PakDir{}}; }
insertFile(std::get<PakDir>(dir[folder]),
std::move(next), auto existingNode = std::find_if(dir.begin(), dir.end(),
std::move(file)); [&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 { } 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 +98,103 @@ PakDir readPak(std::istream &st) {
PakDir root; PakDir root;
for(quint32 id = 0; id < hdr.dirNum; id++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st, id); auto ent = readPakEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.file)); insertFile(root, std::move(ent.name), std::move(ent.file));
} }
return root; return root;
} }
void setTableToPakDir(QTableWidget &table, PakDir const &dir) { PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
constexpr auto Flags = Qt::ItemIsSelectable | 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());
switch(role) {
case Qt::DecorationRole:
if(index.column() == Pak::ColumnName) {
auto icon =
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(index.column()) {
case Pak::ColumnSize:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Pak::ColumnName:
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::ItemIsDragEnabled |
Qt::ItemIsEnabled | Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren; Qt::ItemNeverHasChildren;
}
}
auto sorted = table.isSortingEnabled(); QVariant PakDirModel::headerData(int section,
table.clearContents(); Qt::Orientation orientation,
table.setSortingEnabled(false); int role) const {
quint32 row{0}; if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
for(auto const &kv : dir) { switch(section) {
auto const &name = kv.first; case Pak::ColumnSize: return QVariant{tr("Size")};
auto const &node = kv.second; case Pak::ColumnName: return QVariant{tr("Name")};
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));
} }
table.setItem(row, Pak::ColumnId, item);
} }
{ return QVariant{};
auto item = new QTableWidgetItem; }
item->setFlags(Flags);
if(auto file = std::get_if<PakFile>(&node)) { QModelIndex PakDirModel::index(int row,
item->setText(QString::number(file->size())); 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); }
}
{ QModelIndex PakDirModel::parent(QModelIndex const &) const {
auto item = new QTableWidgetItem; return QModelIndex{};
auto icon = std::holds_alternative<PakDir>(node) ? "folder" : }
"text-x-generic";
item->setFlags(Flags); int PakDirModel::rowCount(QModelIndex const &) const {
item->setText(QString::fromStdString(name)); return m_root->size();
item->setIcon(QIcon::fromTheme(icon)); }
table.setItem(row, Pak::ColumnName, item);
} int PakDirModel::columnCount(QModelIndex const &) const {
++row; return Pak::ColumnMax;
} }
table.setSortingEnabled(sorted);
table.resizeColumnsToContents(); PakDirModelSorter::~PakDirModelSorter() {
} }
// EOF // EOF

View File

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