add Project object and some metaobject crap

This commit is contained in:
alison wright 2019-09-30 01:30:32 -04:00
parent c6eb45589c
commit 69fe7cf9ca
9 changed files with 199 additions and 79 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

@ -1,28 +1,20 @@
#include "common.h"
#include "quam/main_window.h"
#include "quam/pak.h"
#include "quam/project.h"
#include <QAbstractItemModel>
#include <QErrorMessage>
#include <QFileDialog>
#include <QSortFilterProxyModel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent},
Ui::MainWindow{},
m_directory{},
m_errors{new QErrorMessage{this}},
m_model{nullptr},
m_sorter{nullptr}
m_errors{new QErrorMessage{this}}
{
setupUi(this);
actionClose->setShortcut(QKeySequence{QKeySequence::Close});
actionOpen->setShortcut(QKeySequence{QKeySequence::Open});
actionQuit->setShortcut(QKeySequence{QKeySequence::Quit});
tableView->sortByColumn(Pak::ColumnSize, Qt::AscendingOrder);
tableView->resizeColumnsToContents();
}
MainWindow::~MainWindow() {
@ -40,12 +32,9 @@ void MainWindow::fileOpen() {
if(!fileName.isEmpty()) {
try {
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);
auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st);
new Project{std::move(pak), m_errors, mdiArea};
} catch(std::exception const &exc) {
m_errors->showMessage(tr(exc.what()));
}
@ -53,10 +42,9 @@ void MainWindow::fileOpen() {
}
void MainWindow::fileClose() {
tableView->setModel(nullptr);
delete m_sorter;
delete m_model;
m_directory = PakDir{};
if(auto win = mdiArea->activeSubWindow()) {
mdiArea->removeSubWindow(win);
}
}
// EOF

View File

@ -6,9 +6,7 @@
#include <QMainWindow>
#include <QWidget>
class QAbstractItemModel;
class QErrorMessage;
class QSortFilterProxyModel;
class MainWindow : public QMainWindow, private Ui::MainWindow {
Q_OBJECT
@ -22,10 +20,7 @@ public slots:
void fileClose();
private:
PakDir m_directory;
QErrorMessage *m_errors;
QAbstractItemModel *m_model;
QSortFilterProxyModel *m_sorter;
QErrorMessage *m_errors;
};
// EOF

View File

@ -16,46 +16,19 @@
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QMdiArea" name="mdiArea">
<property name="viewMode">
<enum>QMdiArea::TabbedView</enum>
</property>
<property name="documentMode">
<bool>true</bool>
</property>
<property name="tabsClosable">
<bool>true</bool>
</property>
<property name="tabsMovable">
<bool>true</bool>
</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="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
<widget class="QPlainTextEdit" name="textEdit"/>
</widget>
</item>
</layout>
@ -101,7 +74,8 @@
</action>
<action name="actionClose">
<property name="icon">
<iconset theme="document-close"/>
<iconset theme="document-close">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Close</string>

View File

@ -2,6 +2,7 @@
#include "quam/pak.h"
#include <QIcon>
#include <QMetaEnum>
struct PakHeader {
quint32 dirOffset;
@ -106,6 +107,15 @@ PakDir readPak(std::istream &st) {
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}
@ -121,10 +131,11 @@ QVariant PakDirModel::data(QModelIndex const &index, int role) const {
}
auto node = static_cast<PakNode *>(index.internalPointer());
auto col = Pak::Column(index.column());
switch(role) {
case Qt::DecorationRole:
if(index.column() == Pak::ColumnName) {
if(col == Pak::Column::Name) {
auto icon =
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
@ -132,13 +143,13 @@ QVariant PakDirModel::data(QModelIndex const &index, int role) const {
}
break;
case Qt::DisplayRole:
switch(index.column()) {
case Pak::ColumnSize:
switch(col) {
case Pak::Column::Size:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Pak::ColumnName:
case Pak::Column::Name:
return QVariant{tr(node->name.data())};
}
default:
@ -162,9 +173,9 @@ 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")};
switch(Pak::Column(section)) {
case Pak::Column::Size: return QVariant{tr("Size")};
case Pak::Column::Name: return QVariant{tr("Name")};
}
}
return QVariant{};
@ -191,7 +202,7 @@ int PakDirModel::rowCount(QModelIndex const &) const {
}
int PakDirModel::columnCount(QModelIndex const &) const {
return Pak::ColumnMax;
return QMetaEnum::fromType<Pak::Column>().keyCount();
}
PakDirModelSorter::~PakDirModelSorter() {

View File

@ -4,11 +4,13 @@
#include <QSortFilterProxyModel>
namespace Pak {
enum PakColumn {
ColumnSize,
ColumnName,
ColumnMax,
Q_NAMESPACE
enum class Column {
Size,
Name,
};
Q_ENUM_NS(Column)
}
struct PakNode;
@ -39,6 +41,14 @@ struct PakNode : public std::variant<PakDir, PakFile> {
};
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

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>