Skip to content

Commit

Permalink
GUI: Use smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
fdde authored and DrMcCoy committed Dec 29, 2017
1 parent 6fd5b26 commit a62665c
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 80 deletions.
64 changes: 27 additions & 37 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace GUI {

W_OBJECT_IMPL(MainWindow)

MainWindow::~MainWindow(){} // needed for ScopedPtr

MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, const char *path) :
QMainWindow(parent) {
_centralWidget = new QWidget(this);
Expand All @@ -72,10 +74,10 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co
_menuFile = new QMenu(_menuBar);
_menuHelp = new QMenu(_menuBar);

_panelPreviewEmpty = new PanelPreviewEmpty();
_panelPreviewImage = new PanelPreviewImage();
_panelPreviewSound = new PanelPreviewSound();
_panelResourceInfo = new PanelResourceInfo();
_panelPreviewEmpty.reset(new PanelPreviewEmpty());
_panelPreviewImage.reset(new PanelPreviewImage());
_panelPreviewSound.reset(new PanelPreviewSound());
_panelResourceInfo.reset(new PanelResourceInfo());

_treeView = new QTreeView(_splitterLeftRight);
// 1:8 ratio, 8 being the (res info + preview) wrapper widget
Expand Down Expand Up @@ -146,7 +148,7 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co
QFrame *resInfoFrame = new QFrame(wrapperWidget);
{
QHBoxLayout *hl = new QHBoxLayout(resInfoFrame);
hl->addWidget(_panelResourceInfo);
hl->addWidget(_panelResourceInfo.get());
}
resInfoFrame->setFrameShape(QFrame::StyledPanel);
resInfoFrame->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
Expand All @@ -156,7 +158,7 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co
{
QHBoxLayout *hl = new QHBoxLayout(_resPreviewFrame);
hl->setMargin(0);
hl->addWidget(_panelPreviewEmpty);
hl->addWidget(_panelPreviewEmpty.get());
}
_resPreviewFrame->setFrameShape(QFrame::StyledPanel);

Expand Down Expand Up @@ -186,7 +188,7 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co

_actionOpenDirectory->setText(tr("&Open directory"));
_actionClose->setText(tr("&Close"));
_actionQuit->setText(tr("MainWindow"));
_actionQuit->setText(tr("Quit"));
_actionAbout->setText(tr("About"));
_actionOpenFile->setText(tr("&Open file"));

Expand All @@ -200,42 +202,31 @@ 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::log, this, &MainWindow::slotLog);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::closeDirClicked, this, &MainWindow::slotCloseDir);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::saveClicked, this, &MainWindow::saveItem);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::exportTGAClicked, this, &MainWindow::exportTGA);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::exportBMUMP3Clicked, this, &MainWindow::exportBMUMP3);
QObject::connect(_panelResourceInfo, &PanelResourceInfo::exportWAVClicked, this, &MainWindow::exportWAV);
QObject::connect(_panelResourceInfo.get(), &PanelResourceInfo::log, this, &MainWindow::slotLog);
QObject::connect(_panelResourceInfo.get(), &PanelResourceInfo::closeDirClicked, this, &MainWindow::slotCloseDir);
QObject::connect(_panelResourceInfo.get(), &PanelResourceInfo::saveClicked, this, &MainWindow::saveItem);
QObject::connect(_panelResourceInfo.get(), &PanelResourceInfo::exportTGAClicked, this, &MainWindow::exportTGA);
QObject::connect(_panelResourceInfo.get(), &PanelResourceInfo::exportBMUMP3Clicked, this, &MainWindow::exportBMUMP3);
QObject::connect(_panelResourceInfo.get(), &PanelResourceInfo::exportWAVClicked, this, &MainWindow::exportWAV);
QObject::connect(_actionAbout, &QAction::triggered, this, &MainWindow::slotAbout);

_actionAbout->setShortcut(QKeySequence(tr("F1")));

// status bar
_status = new StatusBar(this->statusBar());
_status.reset(new StatusBar(this->statusBar()));
_status->setText("Idle...");

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

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

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

MainWindow::~MainWindow() {
if (_panelPreviewEmpty)
delete _panelPreviewEmpty;

if (_panelPreviewSound)
delete _panelPreviewSound;

if (_panelPreviewImage)
delete _panelPreviewImage;
}

void MainWindow::slotLog(const QString &text) {
_log->append(text);
}
Expand All @@ -258,7 +249,7 @@ void MainWindow::open(const QString &path) {
return;
}

_treeModel = new ResourceTree(this, _treeView);
_treeModel.reset(new ResourceTree(this, _treeView));

// enters populate thread in here
_treeModel->populate(_files.getRoot());
Expand All @@ -270,10 +261,10 @@ void MainWindow::open(const QString &path) {

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

_treeView->setModel(_proxyModel);
_treeView->setModel(_proxyModel.get());
_treeView->expandToDepth(0);
_treeView->show();
_treeView->resizeColumnToContents(0);
Expand All @@ -299,14 +290,13 @@ void MainWindow::slotOpenFile() {
}

void MainWindow::slotCloseDir() {
showPreviewPanel(_panelPreviewEmpty);
showPreviewPanel(_panelPreviewEmpty.get());

_panelResourceInfo->setButtonsForClosedDir();

_treeView->setModel(nullptr);

delete _treeModel;
_treeModel = nullptr;
_treeModel.reset(nullptr);

_log->append(tr("Closed directory: %1").arg(_rootPath));

Expand All @@ -329,7 +319,7 @@ void MainWindow::showPreviewPanel(QFrame *panel) {
if (old != panel) {
_resPreviewFrame->layout()->removeWidget(old);
old->setParent(0);
if (old == _panelPreviewSound)
if (old == _panelPreviewSound.get())
_panelPreviewSound->stop();
_resPreviewFrame->layout()->addWidget(panel);
}
Expand All @@ -339,15 +329,15 @@ void MainWindow::showPreviewPanel(QFrame *panel) {
void MainWindow::showPreviewPanel() {
switch (_currentItem->getResourceType()) {
case Aurora::kResourceImage:
showPreviewPanel(_panelPreviewImage);
showPreviewPanel(_panelPreviewImage.get());
break;

case Aurora::kResourceSound:
showPreviewPanel(_panelPreviewSound);
showPreviewPanel(_panelPreviewSound.get());
break;

default:
showPreviewPanel(_panelPreviewEmpty);
showPreviewPanel(_panelPreviewEmpty.get());
break;
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <memory>

#include <QAction>
#include <QFileSystemModel>
#include <QGraphicsView>
Expand Down Expand Up @@ -94,10 +92,10 @@ private /*slots*/:

Common::FileTree _files;

StatusBar *_status;
const ResourceTreeItem *_currentItem;
ResourceTree *_treeModel;
ProxyModel *_proxyModel;
Common::ScopedPtr<StatusBar> _status;
ResourceTreeItem *_currentItem;
Common::ScopedPtr<ResourceTree> _treeModel;
Common::ScopedPtr<ProxyModel> _proxyModel;
QString _rootPath;

QWidget *_centralWidget;
Expand All @@ -124,10 +122,10 @@ private /*slots*/:
QMenu *_menuHelp;

// resource preview
PanelPreviewEmpty *_panelPreviewEmpty;
PanelPreviewImage *_panelPreviewImage;
PanelPreviewSound *_panelPreviewSound;
PanelResourceInfo *_panelResourceInfo;
Common::ScopedPtr<PanelPreviewEmpty> _panelPreviewEmpty;
Common::ScopedPtr<PanelPreviewImage> _panelPreviewImage;
Common::ScopedPtr<PanelPreviewSound> _panelPreviewSound;
Common::ScopedPtr<PanelResourceInfo> _panelResourceInfo;
};

} // namespace GUI
Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelpreviewempty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace GUI {

W_OBJECT_IMPL(PanelPreviewEmpty)

PanelPreviewEmpty::PanelPreviewEmpty(QWidget *parent) : QFrame(parent) {
PanelPreviewEmpty::PanelPreviewEmpty() : QFrame(0) {
QHBoxLayout *layoutTop = new QHBoxLayout(this);
layoutTop->setContentsMargins(0, 0, 0, 0);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelpreviewempty.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PanelPreviewEmpty : public QFrame {
W_OBJECT(PanelPreviewEmpty)

public:
PanelPreviewEmpty(QWidget *parent = 0);
PanelPreviewEmpty();
};

} // End of namespace GUI
Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelpreviewimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace GUI {

W_OBJECT_IMPL(PanelPreviewImage)

PanelPreviewImage::PanelPreviewImage(QWidget *parent) : QFrame(parent) {
PanelPreviewImage::PanelPreviewImage() : QFrame(0) {
_zoomFactor = 1.0f;

_mode = Qt::SmoothTransformation;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelpreviewimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PanelPreviewImage : public QFrame {
void updateButtons();

public:
PanelPreviewImage(QWidget *parent = 0);
PanelPreviewImage();

void setItem(const GUI::ResourceTreeItem *item);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelpreviewsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace GUI {

W_OBJECT_IMPL(PanelPreviewSound)

PanelPreviewSound::PanelPreviewSound(QWidget *parent) : QFrame(parent) {
PanelPreviewSound::PanelPreviewSound() : QFrame(0) {
QGridLayout *layoutTop = new QGridLayout(this);
QHBoxLayout *layoutLabels = new QHBoxLayout();
QHBoxLayout *layoutButtons = new QHBoxLayout();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelpreviewsound.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PanelPreviewSound : public QFrame {
void update();

public:
PanelPreviewSound(QWidget *parent = 0);
PanelPreviewSound();

void setItem(const ResourceTreeItem *item);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelresourceinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace GUI {

W_OBJECT_IMPL(PanelResourceInfo)

PanelResourceInfo::PanelResourceInfo(QWidget *parent) : QFrame(parent) {
PanelResourceInfo::PanelResourceInfo() : QFrame(0) {
QVBoxLayout *layoutTop = new QVBoxLayout(this);
QVBoxLayout *layoutLabels = new QVBoxLayout();
QHBoxLayout *layoutButtons = new QHBoxLayout();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/panelresourceinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PanelResourceInfo : public QFrame {
QLabel *_labelResType;

public:
PanelResourceInfo(QWidget *parent = 0);
PanelResourceInfo();

void showExportButtons(const GUI::ResourceTreeItem *item);
void showExportButtons(bool enableRaw, bool showMP3, bool showWAV, bool showTGA);
Expand Down
17 changes: 8 additions & 9 deletions src/gui/resourcetree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ namespace GUI {
W_OBJECT_IMPL(ResourceTree)

ResourceTree::ResourceTree(MainWindow *mainWindow, QObject *parent) : QAbstractItemModel(parent),
_mainWindow(mainWindow), _iconProvider(new QFileIconProvider()) {
_root = new ResourceTreeItem("Filename");
_mainWindow(mainWindow) {
_root.reset(new ResourceTreeItem("Filename"));
_iconProvider.reset(new QFileIconProvider());
}

void ResourceTree::populate(const Common::FileTree::Entry &rootEntry) {
Expand All @@ -76,13 +77,11 @@ void ResourceTree::populate(const Common::FileTree::Entry &entry, ResourceTreeIt
ResourceTree::~ResourceTree() {
_archives.clear();
_keyDataFiles.clear();
delete _iconProvider;
delete _root;
}

ResourceTreeItem *ResourceTree::itemFromIndex(const QModelIndex &index) const {
if (!index.isValid())
return _root;
return _root.get();

return static_cast<ResourceTreeItem*>(index.internalPointer());
}
Expand All @@ -98,7 +97,7 @@ QModelIndex ResourceTree::index(int row, int col, const QModelIndex &parent) con

QModelIndex ResourceTree::parent(const QModelIndex &index) const {
ResourceTreeItem *parent = itemFromIndex(index)->getParent();
if (parent == _root)
if (parent == _root.get())
return QModelIndex();

return createIndex(parent->row(), 0, parent);
Expand Down Expand Up @@ -206,16 +205,16 @@ void ResourceTree::insertItemsFromArchive(Archive &archive, const QModelIndex &p
auto resources = archive.data->getResources();
for (auto r = resources.begin(); r != resources.end(); ++r)
{
items << new ResourceTreeItem(archive.data, *r);
items.push_back(new ResourceTreeItem(archive.data, *r));
}

insertItems(0, items, parentIndex);
}

void ResourceTree::insertItems(int position, QList<ResourceTreeItem*> &items, const QModelIndex &parent) {
void ResourceTree::insertItems(size_t position, QList<ResourceTreeItem*> &items, const QModelIndex &parent) {
ResourceTreeItem *parentItem = itemFromIndex(parent);

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

for (const auto &item : items)
{
Expand Down
6 changes: 3 additions & 3 deletions src/gui/resourcetree.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class ResourceTree : public QAbstractItemModel {
W_OBJECT(ResourceTree)

private:
ResourceTreeItem *_root;
Common::ScopedPtr<ResourceTreeItem> _root;
MainWindow *_mainWindow;

QFileIconProvider *_iconProvider;
Common::ScopedPtr<QFileIconProvider> _iconProvider;

typedef Common::PtrMap<QString, Aurora::Archive> ArchiveMap;
typedef Common::PtrMap<QString, Aurora::KEYDataFile> KEYDataFileMap;
Expand All @@ -87,7 +87,7 @@ class ResourceTree : public QAbstractItemModel {

/** Called in fetchMore. */
void insertItemsFromArchive(Archive &archive, const QModelIndex &parentIndex);
void insertItems(int position, QList<ResourceTreeItem*> &items, const QModelIndex &parentIndex);
void insertItems(size_t position, QList<ResourceTreeItem*> &items, const QModelIndex &parentIndex);

Aurora::Archive *getArchive(const QString &path);
Aurora::KEYDataFile *getKEYDataFile(const QString &file);
Expand Down
Loading

0 comments on commit a62665c

Please sign in to comment.