Skip to content

Commit

Permalink
GUI: Sort tree items correctly
Browse files Browse the repository at this point in the history
- By name
- Directories first
  • Loading branch information
fdde authored and DrMcCoy committed Dec 27, 2017
1 parent 6979ef7 commit 97bcc79
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#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 @@ -262,7 +263,14 @@ void MainWindow::setTreeViewModel(const QString &path) {
_treeView->setModel(nullptr);

_treeModel = std::make_unique<ResourceTree>(this, path, _treeView);
_treeView->setModel(_treeModel.get());

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

proxy->sort(0);

_treeView->setModel(proxy);

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

Expand Down
29 changes: 29 additions & 0 deletions src/gui/proxymodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "verdigris/wobjectdefs.h"

#include "src/gui/proxymodel.h"
#include "src/gui/resourcetree.h"
#include "src/gui/resourcetreeitem.h"

#include <QString>

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();

bool ldir = qobject_cast<ResourceTree*>(sourceModel())->getItem(left)->isDir();
bool rdir = qobject_cast<ResourceTree*>(sourceModel())->getItem(right)->isDir();

if (ldir && rdir)
return !QString::compare(lname, rname, Qt::CaseInsensitive);

else if (ldir && !rdir)
return true;

return !QString::compare(lname, rname, Qt::CaseInsensitive);
}

} // End of namespace GUI
20 changes: 20 additions & 0 deletions src/gui/proxymodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PROXYMODEL_H
#define PROXYMODEL_H

#include <QSortFilterProxyModel>

#include "verdigris/wobjectimpl.h"

namespace GUI {

class ProxyModel : public QSortFilterProxyModel {
W_OBJECT(ProxyModel)
public:
using QSortFilterProxyModel::QSortFilterProxyModel;
protected:
virtual bool lessThan(const QModelIndex& left, const QModelIndex& right) const override;
};

} // End of namespace GUI

#endif // PROXYMODEL_H
4 changes: 3 additions & 1 deletion src/gui/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ src_gui_libgui_la_SOURCES += \
src/gui/panelpreviewsound.h \
src/gui/panelpreviewimage.h \
src/gui/panelpreviewtext.h \
src/gui/proxymodel.h \
src/gui/statusbar.h \
src/gui/config.h \
$(EMPTY)
Expand All @@ -45,6 +46,7 @@ src_gui_libgui_la_SOURCES += \
src/gui/panelpreviewsound.cpp \
src/gui/panelpreviewimage.cpp \
src/gui/panelpreviewtext.cpp \
src/gui/proxymodel.cpp \
src/gui/statusbar.cpp \
src/gui/config.cpp
src/gui/config.cpp \
$(EMPTY)

0 comments on commit 97bcc79

Please sign in to comment.