1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-06 09:07:19 +02:00

Viewer archive loading

This commit is contained in:
Daniel Evans 2014-02-10 15:34:09 +00:00
parent 0aff0a9a0d
commit 70dba94de4
10 changed files with 160 additions and 7 deletions

View File

@ -49,10 +49,10 @@ public:
LoaderIMGFile &getAssetInfo(const std::string& assetname);
/// Get the information of an asset by its index
LoaderIMGFile &getAssetInfoByIndex(size_t index);
const LoaderIMGFile &getAssetInfoByIndex(size_t index) const;
/// Returns the number of asset files in the archive
uint32_t getAssetCount();
uint32_t getAssetCount() const;
private:
Versions m_version; ///< Version of this IMG archive

View File

@ -110,13 +110,13 @@ bool LoaderIMG::saveAsset(const std::string& assetname, const std::string& filen
}
/// Get the information of an asset by its index
LoaderIMGFile &LoaderIMG::getAssetInfoByIndex(size_t index)
const LoaderIMGFile &LoaderIMG::getAssetInfoByIndex(size_t index) const
{
return m_assets[index];
}
uint32_t LoaderIMG::getAssetCount()
uint32_t LoaderIMG::getAssetCount() const
{
return m_assetCount;
}

View File

@ -0,0 +1,17 @@
#include "ArchiveContentsWidget.hpp"
ArchiveContentsWidget::ArchiveContentsWidget(QWidget* parent, Qt::WindowFlags flags)
: QDockWidget(parent, flags), model(nullptr)
{
setWindowTitle("Archive");
table = new QTableView();
setWidget(table);
}
void ArchiveContentsWidget::setArchive(const LoaderIMG& archive)
{
auto m = table->model();
model = new IMGTableModel(archive);
table->setModel(model);
delete m;
}

View File

@ -0,0 +1,21 @@
#pragma once
#ifndef _ARCHIVECONTENTSWIDGET_HPP_
#define _ARCHIVECONTENTSWIDGET_HPP_
#include <QDockWidget>
#include <QTableView>
#include "IMGTableModel.hpp"
class ArchiveContentsWidget : public QDockWidget
{
Q_OBJECT
IMGTableModel* model;
QTableView* table;
public:
ArchiveContentsWidget(QWidget* parent = 0, Qt::WindowFlags flags = 0);
void setArchive(const LoaderIMG& archive);
};
#endif

View File

@ -6,7 +6,9 @@ find_package(Qt5Core REQUIRED)
add_executable(rwviewer
main.cpp
ViewerWindow.cpp
ViewerWidget.cpp)
ViewerWidget.cpp
IMGTableModel.cpp
ArchiveContentsWidget.cpp)
include_directories(../rwengine/include /usr/include/bullet)

View File

@ -0,0 +1,41 @@
#include "IMGTableModel.hpp"
QVariant IMGTableModel::data(const QModelIndex& index, int role) const
{
if(role == Qt::DisplayRole) {
if(index.row() < archive.getAssetCount()) {
auto& f = archive.getAssetInfoByIndex(index.row());
if(index.column() == 0) {
return QString(f.name);
}
if(index.column() == 1) {
return f.size;
}
}
}
return QVariant::Invalid;
}
QVariant IMGTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole && orientation == Qt::Horizontal) {
if(section == 0) {
return "Name";
}
if(section == 1) {
return "Size";
}
}
return QVariant::Invalid;
}
int IMGTableModel::rowCount(const QModelIndex& parent) const
{
return archive.getAssetCount();
}
int IMGTableModel::columnCount(const QModelIndex& parent) const
{
return 2;
}

View File

@ -0,0 +1,28 @@
#pragma once
#ifndef _IMGTABLEMODEL_HPP_
#define _IMGTABLEMODEL_HPP_
#include <QAbstractItemModel>
#include <loaders/LoaderIMG.hpp>
class IMGTableModel : public QAbstractTableModel
{
Q_OBJECT
LoaderIMG archive;
public:
IMGTableModel(const LoaderIMG& archive, QObject* parent = 0)
: QAbstractTableModel(parent), archive(archive)
{}
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
};
#endif

View File

@ -1,8 +1,42 @@
#include "ViewerWindow.hpp"
#include "ViewerWidget.hpp"
#include "ArchiveContentsWidget.hpp"
#include <QMenuBar>
#include <QFileDialog>
ViewerWindow::ViewerWindow(QWidget* parent, Qt::WindowFlags flags): QMainWindow(parent, flags)
{
setMinimumSize(640, 480);
viewer = new ViewerWidget();
this->setCentralWidget(viewer);
archivewidget = new ArchiveContentsWidget;
this->addDockWidget(Qt::LeftDockWidgetArea, archivewidget);
QMenuBar* mb = this->menuBar();
QMenu* file = mb->addMenu("&File");
file->addAction("Open &Archive", this, SLOT(openArchive()));
}
void ViewerWindow::openArchive(const QString& name)
{
QString rname = name;
QString lower = name.toLower();
if(lower.endsWith(".img")) {
rname = rname.left(rname.size()-4);
}
LoaderIMG ld;
ld.load(rname.toStdString());
archivewidget->setArchive(ld);
}
void ViewerWindow::openArchive()
{
QFileDialog dialog(this, "Open Archive", QDir::homePath(), "IMG Archives (*.img)");
if(dialog.exec()) {
openArchive(dialog.selectedFiles().at(0));
}
}

View File

@ -3,12 +3,22 @@
#define _VIEWERWINDOW_HPP_
#include <QMainWindow>
class ArchiveContentsWidget;
class ViewerWidget;
class ViewerWindow : public QMainWindow
{
Q_OBJECT
ViewerWidget* viewer;
ArchiveContentsWidget* archivewidget;
public:
ViewerWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0);
void openArchive(const QString& name);
public slots:
void openArchive();
};
#endif

View File

@ -12,7 +12,7 @@ BOOST_AUTO_TEST_CASE(test_open_archive)
BOOST_CHECK( loader.getAssetCount() > 0 );
LoaderIMGFile& f = loader.getAssetInfoByIndex(0);
auto& f = loader.getAssetInfoByIndex(0);
// A few assumptions..
@ -20,7 +20,7 @@ BOOST_AUTO_TEST_CASE(test_open_archive)
BOOST_CHECK_EQUAL( f.offset, 0);
BOOST_CHECK_EQUAL( f.size, 33);
LoaderIMGFile& f2 = loader.getAssetInfo("radar00.txd");
auto& f2 = loader.getAssetInfo("radar00.txd");
BOOST_CHECK_EQUAL( f2.name, f.name );
BOOST_CHECK_EQUAL( f2.offset, f.offset );