make pak views use M/V code
This commit is contained in:
parent
89414dd5ec
commit
c6eb45589c
|
@ -13,7 +13,7 @@
|
|||
#include <ios>
|
||||
#include <istream>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
|
|
@ -2,17 +2,30 @@
|
|||
#include "quam/main_window.h"
|
||||
#include "quam/pak.h"
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QErrorMessage>
|
||||
#include <QFileDialog>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow{parent},
|
||||
Ui::MainWindow{},
|
||||
m_errors{}
|
||||
m_directory{},
|
||||
m_errors{new QErrorMessage{this}},
|
||||
m_model{nullptr},
|
||||
m_sorter{nullptr}
|
||||
{
|
||||
setupUi(this);
|
||||
|
||||
actionClose->setShortcut(QKeySequence{QKeySequence::Close});
|
||||
actionOpen->setShortcut(QKeySequence{QKeySequence::Open});
|
||||
actionQuit->setShortcut(QKeySequence{QKeySequence::Quit});
|
||||
|
||||
tableWidget->sortByColumn(Pak::ColumnId, Qt::AscendingOrder);
|
||||
tableView->sortByColumn(Pak::ColumnSize, Qt::AscendingOrder);
|
||||
tableView->resizeColumnsToContents();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
}
|
||||
|
||||
void MainWindow::fileOpen() {
|
||||
|
@ -27,13 +40,23 @@ void MainWindow::fileOpen() {
|
|||
|
||||
if(!fileName.isEmpty()) {
|
||||
try {
|
||||
auto st = openReadBin(fileName.toStdString());
|
||||
auto pak = readPak(st);
|
||||
setTableToPakDir(*tableWidget, pak);
|
||||
auto st = openReadBin(fileName.toStdString());
|
||||
m_directory = readPak(st);
|
||||
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) {
|
||||
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
|
||||
|
|
|
@ -1,23 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include "quam/pak.h"
|
||||
#include "quam/ui_main_window.h"
|
||||
|
||||
#include <QErrorMessage>
|
||||
#include <QFileDialog>
|
||||
#include <QMainWindow>
|
||||
#include <QWidget>
|
||||
|
||||
class QAbstractItemModel;
|
||||
class QErrorMessage;
|
||||
class QSortFilterProxyModel;
|
||||
|
||||
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;
|
||||
PakDir m_directory;
|
||||
QErrorMessage *m_errors;
|
||||
QAbstractItemModel *m_model;
|
||||
QSortFilterProxyModel *m_sorter;
|
||||
};
|
||||
|
||||
// EOF
|
||||
|
|
|
@ -11,80 +11,53 @@
|
|||
</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="QSplitter" name="splitter">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</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="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>
|
||||
<widget class="QTableView" name="tableView">
|
||||
<property name="verticalScrollBarPolicy">
|
||||
<enum>Qt::ScrollBarAlwaysOn</enum>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>The ID of the file. This is the N-th file in the archive.</string>
|
||||
<property name="horizontalScrollBarPolicy">
|
||||
<enum>Qt::ScrollBarAlwaysOff</enum>
|
||||
</property>
|
||||
</column>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Size</string>
|
||||
<property name="autoScroll">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>The size of the file in bytes.</string>
|
||||
<property name="tabKeyNavigation">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</column>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Name</string>
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>The name of the file.</string>
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::SingleSelection</enum>
|
||||
</property>
|
||||
</column>
|
||||
<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="verticalHeaderVisible">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="QPlainTextEdit" name="textEdit"/>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="textEdit"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
|
@ -101,6 +74,7 @@
|
|||
<string>&File</string>
|
||||
</property>
|
||||
<addaction name="actionOpen"/>
|
||||
<addaction name="actionClose"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actionQuit"/>
|
||||
</widget>
|
||||
|
@ -109,7 +83,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>&Open</string>
|
||||
|
@ -117,12 +92,21 @@
|
|||
</action>
|
||||
<action name="actionQuit">
|
||||
<property name="icon">
|
||||
<iconset theme="application-exit"/>
|
||||
<iconset theme="application-exit">
|
||||
<normaloff>.</normaloff>.</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>&Quit</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionClose">
|
||||
<property name="icon">
|
||||
<iconset theme="document-close"/>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>&Close</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections>
|
||||
|
@ -158,8 +142,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>
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "common.h"
|
||||
#include "quam/pak.h"
|
||||
|
||||
#include <QIcon>
|
||||
|
||||
struct PakHeader {
|
||||
quint32 dirOffset;
|
||||
quint32 dirNum;
|
||||
|
@ -33,7 +35,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 +45,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 +66,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 +98,103 @@ 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 |
|
||||
Qt::ItemIsDragEnabled |
|
||||
Qt::ItemIsEnabled |
|
||||
Qt::ItemNeverHasChildren;
|
||||
PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
|
||||
QAbstractItemModel{parent},
|
||||
m_root{root}
|
||||
{
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
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()));
|
||||
}
|
||||
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;
|
||||
PakDirModel::~PakDirModel() {
|
||||
}
|
||||
|
||||
QVariant PakDirModel::data(QModelIndex const &index, int role) const {
|
||||
if(!index.isValid()) {
|
||||
return QVariant{};
|
||||
}
|
||||
table.setSortingEnabled(sorted);
|
||||
table.resizeColumnsToContents();
|
||||
|
||||
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::ItemIsEnabled |
|
||||
Qt::ItemNeverHasChildren;
|
||||
}
|
||||
}
|
||||
|
||||
QVariant PakDirModel::headerData(int section,
|
||||
Qt::Orientation orientation,
|
||||
int role) const {
|
||||
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
||||
switch(section) {
|
||||
case Pak::ColumnSize: return QVariant{tr("Size")};
|
||||
case Pak::ColumnName: 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 Pak::ColumnMax;
|
||||
}
|
||||
|
||||
PakDirModelSorter::~PakDirModelSorter() {
|
||||
}
|
||||
|
||||
// EOF
|
||||
|
|
|
@ -1,35 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include <QTableWidget>
|
||||
#include <QAbstractItemModel>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
namespace Pak {
|
||||
enum PakColumn {
|
||||
ColumnId,
|
||||
ColumnSize,
|
||||
ColumnName,
|
||||
ColumnMax,
|
||||
};
|
||||
}
|
||||
|
||||
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 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
|
||||
|
|
Loading…
Reference in New Issue
Block a user