Skip to content

Commit

Permalink
GUI: Use Common::FileTree like the original Phaethon did
Browse files Browse the repository at this point in the history
In addition:
- Use wrapper function for the status bar rather than a getter
- Rename setTreeViewModel -> open, finishTree -> openFinish
- Centre align sound percentage label
  • Loading branch information
fdde authored and DrMcCoy committed Dec 29, 2017
1 parent 321b0dc commit 4f61582
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 174 deletions.
90 changes: 45 additions & 45 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co
QObject::connect(_actionOpenFile, &QAction::triggered, this, &MainWindow::slotOpenFile);
QObject::connect(_actionClose, &QAction::triggered, this, &MainWindow::slotCloseDir);
QObject::connect(_actionQuit, &QAction::triggered, this, &MainWindow::slotQuit);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::loadModel, this, &MainWindow::setTreeViewModel);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::log, this, &MainWindow::slotLog);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::closeDirClicked, this, &MainWindow::slotCloseDir);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::saveClicked, this, &MainWindow::saveItem);
Expand All @@ -212,18 +211,18 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co
_actionAbout->setShortcut(QKeySequence(tr("F1")));

// status bar
_status = std::make_shared<StatusBar>(this->statusBar());
_status = new StatusBar(this->statusBar());
_status->setText("Idle...");

const QString qpath = QString::fromUtf8(path);

_treeModel = nullptr;
_proxyModel = nullptr;
_treeModel = new ResourceTree(this, _treeView);
_proxyModel = new ProxyModel(this);

if (qpath.isEmpty())
_actionClose->setEnabled(false);
else
setTreeViewModel(qpath);
open(qpath);
}

MainWindow::~MainWindow() {
Expand All @@ -241,21 +240,40 @@ void MainWindow::slotLog(const QString &text) {
_log->append(text);
}

// called when the populate thread finishes
void MainWindow::finishTree() {
if (_proxyModel)
delete _proxyModel;
void MainWindow::open(const QString &path) {
if (_rootPath == path)
return;

_proxyModel = new ProxyModel(this);
_proxyModel->setSourceModel(_treeModel);
_rootPath = path;

// popped in openFinish
_status->push("Populating resource tree...");

try {
_files.readPath(Common::UString(path.toStdString()), -1);
} catch (Common::Exception &e) {
_status->pop();

Common::printException(e, "WARNING: ");
return;
}

// enters populate thread in here
_treeModel->populate(_files.getRoot());

_log->append(tr("Set root: %1").arg(path));

_actionClose->setEnabled(true);
}

// called when the populate thread finishes
void MainWindow::openFinish() {
_proxyModel->setSourceModel(_treeModel);
_proxyModel->sort(0);

_treeView->setModel(_proxyModel);

_treeView->expandToDepth(0);
_treeView->show();

_treeView->resizeColumnToContents(0);

QObject::connect(_treeView->selectionModel(), &QItemSelectionModel::selectionChanged,
Expand All @@ -264,38 +282,18 @@ void MainWindow::finishTree() {
_status->pop();
}

void MainWindow::setTreeViewModel(const QString &path) {
if (_rootPath == path)
return;
_rootPath = path;

// popped in finishTree
_status->push("Populating resource tree...");

_treeView->setModel(nullptr);

if (_treeModel)
delete _treeModel;

_treeModel = new ResourceTree(this, path, _treeView);

_log->append(tr("Set root: %1").arg(path));

_actionClose->setEnabled(true);
}

void MainWindow::slotOpenDir() {
QString dir = QFileDialog::getExistingDirectory(this,
tr("Open directory"), QString(QStandardPaths::HomeLocation), QFileDialog::ShowDirsOnly);
if (!dir.isEmpty())
setTreeViewModel(dir);
open(dir);
}

void MainWindow::slotOpenFile() {
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Aurora game resource file"), QString(QStandardPaths::HomeLocation), tr("Aurora game resource (*.*)"));
if (!fileName.isEmpty())
setTreeViewModel(fileName);
open(fileName);
}

void MainWindow::slotCloseDir() {
Expand Down Expand Up @@ -384,7 +382,7 @@ void MainWindow::saveItem() {
try {
QScopedPointer<Common::SeekableReadStream> res(_currentItem->getResourceData());

Common::WriteFile file(Common::UString(fileName.toStdString().c_str()));
Common::WriteFile file(fileName.toStdString());

file.writeStream(*res);
file.flush();
Expand Down Expand Up @@ -415,7 +413,7 @@ void MainWindow::exportTGA() {
try {
QScopedPointer<Images::Decoder> image(_currentItem->getImage());

image->dumpTGA(Common::UString(fileName.toStdString().c_str()));
image->dumpTGA(fileName.toStdString());

} catch (Common::Exception &e) {
Common::printException(e, "WARNING: ");
Expand All @@ -432,8 +430,6 @@ void MainWindow::exportBMUMP3Impl(Common::SeekableReadStream &bmu, Common::Write
mp3.writeStream(bmu);
}

#define USTR(x) (Common::UString((x).toStdString()))

void MainWindow::exportBMUMP3() {
if (!_currentItem)
return;
Expand All @@ -442,7 +438,7 @@ void MainWindow::exportBMUMP3() {

const QString title = "Save MP3 file";
const QString mask = "MP3 file (*.mp3)|*.mp3";
const char *defCstr = TypeMan.setFileType(USTR(_currentItem->getName()), Aurora::kFileTypeMP3).c_str();
const char *defCstr = TypeMan.setFileType(_currentItem->getName().toStdString(), Aurora::kFileTypeMP3).c_str();
const QString def = QString::fromUtf8(defCstr);

QString fileName = QFileDialog::getSaveFileName(this,
Expand All @@ -460,7 +456,7 @@ void MainWindow::exportBMUMP3() {
try {
Common::ScopedPtr<Common::SeekableReadStream> res(_currentItem->getResourceData());

Common::WriteFile file(USTR(fileName));
Common::WriteFile file(fileName.toStdString());

exportBMUMP3Impl(*res, file);
file.flush();
Expand Down Expand Up @@ -554,7 +550,7 @@ void MainWindow::exportWAV() {

const QString title = "Save PCM WAV file";
const QString mask = "WAV file (*.wav)|*.wav";
const char *defCstr = TypeMan.setFileType(USTR(_currentItem->getName()), Aurora::kFileTypeWAV).c_str();
const char *defCstr = TypeMan.setFileType(_currentItem->getName().toStdString(), Aurora::kFileTypeWAV).c_str();
const QString def = QString::fromUtf8(defCstr);

QString fileName = QFileDialog::getSaveFileName(this,
Expand All @@ -572,7 +568,7 @@ void MainWindow::exportWAV() {
try {
Common::ScopedPtr<Sound::AudioStream> sound(_currentItem->getAudioStream());

Common::WriteFile file(USTR(fileName));
Common::WriteFile file(fileName.toStdString());

exportWAVImpl(sound.get(), file);
file.flush();
Expand All @@ -590,8 +586,12 @@ void MainWindow::slotAbout() {
QMessageBox::about(this, "About Phaethon", msg);
}

std::shared_ptr<StatusBar> MainWindow::status() {
return _status;
void MainWindow::statusPush(const QString &text) {
_status->push(text);
}

void MainWindow::statusPop() {
_status->pop();
}

} // namespace GUI
12 changes: 8 additions & 4 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "verdigris/wobjectdefs.h"

#include "src/common/filetree.h"
#include "src/common/readstream.h"
#include "src/common/writestream.h"
#include "src/gui/proxymodel.h"
Expand All @@ -64,11 +65,12 @@ class MainWindow : public QMainWindow {
MainWindow(QWidget *parent, const char *title, const QSize &size, const char *path);
~MainWindow();

std::shared_ptr<StatusBar> status();
void finishTree();
void statusPush(const QString &text);
void statusPop();
void openFinish();

private /*slots*/:
void setTreeViewModel(const QString &path);
void open(const QString &path);
void slotOpenDir();
void slotOpenFile();
void slotCloseDir();
Expand All @@ -90,7 +92,9 @@ private /*slots*/:
void exportBMUMP3Impl(Common::SeekableReadStream &bmu, Common::WriteStream &mp3);
void exportWAVImpl(Sound::AudioStream *sound, Common::WriteStream &wav);

std::shared_ptr<StatusBar> _status;
Common::FileTree _files;

StatusBar *_status;
const ResourceTreeItem *_currentItem;
ResourceTree *_treeModel;
ProxyModel *_proxyModel;
Expand Down
1 change: 1 addition & 0 deletions src/gui/panelpreviewsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ PanelPreviewSound::PanelPreviewSound(QWidget *parent)
_labelVolume = new QLabel(this);
_labelPosition = new QLabel(this);
_labelPercent = new QLabel(this);
_labelPercent->setAlignment(Qt::AlignCenter);
_labelDuration = new QLabel(this);

_buttonPlay = new QPushButton(tr("Play"), this);
Expand Down
75 changes: 28 additions & 47 deletions src/gui/resourcetree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,32 @@ namespace GUI {

W_OBJECT_IMPL(ResourceTree)

ResourceTree::ResourceTree(MainWindow *mainWindow, const QString &path, QObject *parent)
ResourceTree::ResourceTree(MainWindow *mainWindow, QObject *parent)
: QAbstractItemModel(parent)
, _mainWindow(mainWindow)
, _iconProvider(new QFileIconProvider()) {
_root = new ResourceTreeItem();
_root->setData("Filename");
}

_root = new ResourceTreeItem("Filename", nullptr);

ResourceTreeItem *top = _root;

// If the root path is a directory, add a top level item
auto info = QFileInfo(path);
if (info.isDir()) {
top = new ResourceTreeItem(info.canonicalFilePath(), _root);
_root->appendChild(top);
}
void ResourceTree::populate(const Common::FileTree::Entry &rootEntry) {
ResourceTreeItem *treeRoot = new ResourceTreeItem(rootEntry);
_root->addChild(treeRoot);

QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
connect(watcher, &QFutureWatcher<void>::finished, _mainWindow, &MainWindow::openFinish);
QFuture<void> future = QtConcurrent::run(this, &ResourceTree::populate, rootEntry, treeRoot);
watcher->setFuture(future);
}

connect(watcher, &QFutureWatcher<void>::finished, _mainWindow, &MainWindow::finishTree);
void ResourceTree::populate(const Common::FileTree::Entry &entry, ResourceTreeItem *parent) {
for (std::list<Common::FileTree::Entry>::const_iterator childIter = entry.children.begin();
childIter != entry.children.end(); ++childIter) {

QFuture<void> future = QtConcurrent::run(this, &ResourceTree::populate, path, top);
watcher->setFuture(future);
ResourceTreeItem *child = new ResourceTreeItem(*childIter);
parent->addChild(child);
populate(*childIter, child);
}
}

ResourceTree::~ResourceTree() {
Expand All @@ -79,27 +83,6 @@ ResourceTree::~ResourceTree() {
delete _root;
}

void ResourceTree::populate(const QString& path, ResourceTreeItem *parent) {
QFileInfo info(path);
if (info.isDir()) {
QDir dir(path);
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);

QFileInfoList list = dir.entryInfoList();

for (const auto &item : list) {
ResourceTreeItem *child = new ResourceTreeItem(item.canonicalFilePath(), parent);
parent->appendChild(child);

if (item.isDir())
populate(item.canonicalFilePath(), child);
}
}
else {
parent->appendChild(new ResourceTreeItem(path, parent));
}
}

ResourceTreeItem *ResourceTree::itemFromIndex(const QModelIndex &index) const
{
if (!index.isValid())
Expand Down Expand Up @@ -194,9 +177,9 @@ void ResourceTree::fetchMore(const QModelIndex &index) {
if (archive.addedMembers)
return;

_mainWindow->status()->push(tr("Loading archive") + item->getName() + "...");
_mainWindow->statusPush(tr("Loading archive") + item->getName() + "...");
BOOST_SCOPE_EXIT((&_mainWindow)) {
_mainWindow->status()->pop();
_mainWindow->statusPop();
} BOOST_SCOPE_EXIT_END

// Load the archive, if necessary
Expand Down Expand Up @@ -228,17 +211,13 @@ bool ResourceTree::hasChildren(const QModelIndex &index) const {
return itemFromIndex(index)->hasChildren();
}



void ResourceTree::insertItemsFromArchive(Archive &archive, const QModelIndex &parentIndex) {
QList<ResourceTreeItem*> items;

ResourceTreeItem *parentItem = itemFromIndex(parentIndex);

auto resources = archive.data->getResources();
for (auto r = resources.begin(); r != resources.end(); ++r)
{
items << new ResourceTreeItem(archive.data, *r, parentItem);
items << new ResourceTreeItem(archive.data, *r);
}

insertItems(0, items, parentIndex);
Expand All @@ -249,8 +228,10 @@ void ResourceTree::insertItems(int position, QList<ResourceTreeItem*> &items, co

beginInsertRows(parent, position, position + items.count() - 1);

for (const auto &child : items)
parentItem->appendChild(child);
for (const auto &item : items)
{
parentItem->addChild(item);
}

endInsertRows();
}
Expand Down Expand Up @@ -330,17 +311,17 @@ void ResourceTree::loadKEYDataFiles(Aurora::KEYFile &key) {
for (uint i = 0; i < dataFiles.size(); i++) {
try {

_mainWindow->status()->push(tr("Loading data file") + QString::fromUtf8(dataFiles[i].c_str()) + "...");
_mainWindow->statusPush(tr("Loading data file") + QString::fromUtf8(dataFiles[i].c_str()) + "...");

Aurora::KEYDataFile *dataFile = getKEYDataFile(QString::fromUtf8(dataFiles[i].c_str()));
key.addDataFile(i, dataFile);

_mainWindow->status()->pop();
_mainWindow->statusPop();

} catch (Common::Exception &e) {
e.add("Failed to load KEY data file \"%s\"", dataFiles[i].c_str());

_mainWindow->status()->pop();
_mainWindow->statusPop();
Common::printException(e, "WARNING: ");
}
}
Expand Down

0 comments on commit 4f61582

Please sign in to comment.