Skip to content

Commit

Permalink
GUI: Fix selection model and refactor resource tree/item
Browse files Browse the repository at this point in the history
  • Loading branch information
fdde authored and DrMcCoy committed Dec 27, 2017
1 parent 3c10c24 commit 3e45e1f
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 214 deletions.
29 changes: 19 additions & 10 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#include "src/gui/panelpreviewsound.h"
#include "src/gui/panelpreviewtext.h"
#include "src/gui/panelresourceinfo.h"
#include "src/gui/proxymodel.h"
#include "src/gui/resourcetree.h"
#include "src/gui/resourcetreeitem.h"
#include "src/images/dumptga.h"
Expand Down Expand Up @@ -224,6 +223,9 @@ MainWindow::MainWindow(QWidget *parent, const char *title, const QSize &size, co

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

_treeModel = nullptr;
_proxyModel = nullptr;

if (qpath.isEmpty())
_actionClose->setEnabled(false);
else
Expand Down Expand Up @@ -259,17 +261,22 @@ void MainWindow::setTreeViewModel(const QString &path) {
_status->pop();
} BOOST_SCOPE_EXIT_END

_treeModel.reset();
_treeView->setModel(nullptr);

_treeModel = std::make_unique<ResourceTree>(this, path, _treeView);
if (_treeModel)
delete _treeModel;

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

ProxyModel *proxy = new ProxyModel(this);
proxy->setSourceModel(_treeModel.get());
if (_proxyModel)
delete _proxyModel;

proxy->sort(0);
_proxyModel = new ProxyModel(this);
_proxyModel->setSourceModel(_treeModel);

_treeView->setModel(proxy);
_proxyModel->sort(0);

_treeView->setModel(_proxyModel);

_treeView->expandToDepth(0);
_treeView->show();
Expand Down Expand Up @@ -368,15 +375,17 @@ void MainWindow::showPreviewPanel() {
}

void MainWindow::resourceSelect(const QItemSelection &selected, const QItemSelection &UNUSED(deselected)) {
const QModelIndex index = selected.indexes().at(0);
_currentItem = _treeModel->getItem(index);
const QModelIndexList index = _proxyModel->mapSelectionToSource(selected).indexes();
_currentItem = _treeModel->itemFromIndex(index.at(0));

_panelResourceInfo->update(_currentItem);
showPreviewPanel();

_panelPreviewImage->setItem(_currentItem);
if (kPreviewMoreTypes)
_panelPreviewText->setItem(_currentItem);
_panelPreviewSound->setItem(_currentItem);

showPreviewPanel();
}

QString constructStatus(const QString &_action, const QString &name, const QString &destination) {
Expand Down
4 changes: 3 additions & 1 deletion src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include "src/common/readstream.h"
#include "src/common/writestream.h"
#include "src/gui/proxymodel.h"
#include "src/gui/statusbar.h"
#include "src/sound/sound.h"

Expand Down Expand Up @@ -91,7 +92,8 @@ private /*slots*/:

std::shared_ptr<StatusBar> _status;
const ResourceTreeItem *_currentItem;
std::unique_ptr<ResourceTree> _treeModel;
ResourceTree *_treeModel;
ProxyModel *_proxyModel;
QString _rootPath;

QWidget *_centralWidget;
Expand Down
25 changes: 16 additions & 9 deletions src/gui/proxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ namespace GUI {
W_OBJECT_IMPL(ProxyModel)

bool ProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
QString lname = sourceModel()->data(left).toString();
QString rname = sourceModel()->data(right).toString();
ResourceTree *model = qobject_cast<ResourceTree*>(sourceModel());

bool ldir = qobject_cast<ResourceTree*>(sourceModel())->getItem(left)->isDir();
bool rdir = qobject_cast<ResourceTree*>(sourceModel())->getItem(right)->isDir();
ResourceTreeItem *itemLeft = model->itemFromIndex(left);
ResourceTreeItem *itemRight = model->itemFromIndex(right);

if (ldir && rdir)
return !QString::compare(lname, rname, Qt::CaseInsensitive);
bool compare = QString::compare(itemLeft->getName(), itemRight->getName(), Qt::CaseInsensitive) < 0;

else if (ldir && !rdir)
return true;
bool leftDir = itemLeft->isDir();
bool rightDir = itemRight->isDir();

return !QString::compare(lname, rname, Qt::CaseInsensitive);
if (leftDir && rightDir)
return compare;

else if (leftDir && !rightDir)
return true;

else if (!leftDir && rightDir)
return false;

return compare;
}

} // End of namespace GUI

0 comments on commit 3e45e1f

Please sign in to comment.