NOISSUE allow user to sort mod list by clicking on column headers

This commit is contained in:
Petr Mrázek 2016-08-05 00:10:33 +02:00
parent b76d4573cd
commit cf0694a0cb
4 changed files with 12 additions and 34 deletions

View File

@ -29,14 +29,16 @@ ModList::ModList(const QString &dir) : QAbstractListModel(), m_dir(dir)
QDir::NoSymLinks); QDir::NoSymLinks);
m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware); m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
m_watcher = new QFileSystemWatcher(this); m_watcher = new QFileSystemWatcher(this);
is_watching = false; connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(directoryChanged(QString)));
connect(m_watcher, SIGNAL(directoryChanged(QString)), this,
SLOT(directoryChanged(QString)));
} }
void ModList::startWatching() void ModList::startWatching()
{ {
if(is_watching)
return;
update(); update();
is_watching = m_watcher->addPath(m_dir.absolutePath()); is_watching = m_watcher->addPath(m_dir.absolutePath());
if (is_watching) if (is_watching)
{ {
@ -50,6 +52,9 @@ void ModList::startWatching()
void ModList::stopWatching() void ModList::stopWatching()
{ {
if(!is_watching)
return;
is_watching = !m_watcher->removePath(m_dir.absolutePath()); is_watching = !m_watcher->removePath(m_dir.absolutePath());
if (!is_watching) if (!is_watching)
{ {
@ -61,19 +66,6 @@ void ModList::stopWatching()
} }
} }
void ModList::internalSort(QList<Mod> &what)
{
auto predicate = [](const Mod &left, const Mod &right)
{
if (left.name() == right.name())
{
return left.mmc_id().localeAwareCompare(right.mmc_id()) < 0;
}
return left.name().localeAwareCompare(right.name()) < 0;
};
std::sort(what.begin(), what.end(), predicate);
}
bool ModList::update() bool ModList::update()
{ {
if (!isValid()) if (!isValid())
@ -93,7 +85,6 @@ bool ModList::update()
{ {
newMods.append(Mod(entry)); newMods.append(Mod(entry));
} }
internalSort(newMods);
orderedMods.append(newMods); orderedMods.append(newMods);
orderOrStateChanged = true; orderOrStateChanged = true;
} }
@ -363,13 +354,6 @@ bool ModList::dropMimeData(const QMimeData* data, Qt::DropAction action, int, in
added = true; added = true;
} }
} }
if(added)
{
// re-sort the list
beginResetModel();
internalSort(mods);
endResetModel();
}
if (was_watching) if (was_watching)
{ {
startWatching(); startWatching();

View File

@ -104,13 +104,6 @@ public:
return mods; return mods;
} }
private:
void internalSort(QList<Mod> & what);
struct OrderItem
{
QString id;
bool enabled = false;
};
private private
slots: slots:
void directoryChanged(QString path); void directoryChanged(QString path);
@ -120,8 +113,7 @@ signals:
protected: protected:
QFileSystemWatcher *m_watcher; QFileSystemWatcher *m_watcher;
bool is_watching; bool is_watching = false;
QDir m_dir; QDir m_dir;
QString m_list_id;
QList<Mod> mods; QList<Mod> mods;
}; };

View File

@ -47,10 +47,12 @@ ModFolderPage::ModFolderPage(BaseInstance *inst, std::shared_ptr<ModList> mods,
m_filterModel = new QSortFilterProxyModel(this); m_filterModel = new QSortFilterProxyModel(this);
m_filterModel->setDynamicSortFilter(true); m_filterModel->setDynamicSortFilter(true);
m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
m_filterModel->setSourceModel(m_mods.get()); m_filterModel->setSourceModel(m_mods.get());
m_filterModel->setFilterKeyColumn(-1); m_filterModel->setFilterKeyColumn(-1);
ui->modTreeView->setModel(m_filterModel); ui->modTreeView->setModel(m_filterModel);
ui->modTreeView->installEventFilter(this); ui->modTreeView->installEventFilter(this);
ui->modTreeView->sortByColumn(1, Qt::AscendingOrder);
auto smodel = ui->modTreeView->selectionModel(); auto smodel = ui->modTreeView->selectionModel();
connect(smodel, &QItemSelectionModel::currentChanged, this, &ModFolderPage::modCurrent); connect(smodel, &QItemSelectionModel::currentChanged, this, &ModFolderPage::modCurrent);
connect(ui->filterEdit, &QLineEdit::textChanged, this, &ModFolderPage::on_filterTextChanged ); connect(ui->filterEdit, &QLineEdit::textChanged, this, &ModFolderPage::on_filterTextChanged );

View File

@ -26,7 +26,7 @@ ModListView::ModListView ( QWidget* parent )
setAllColumnsShowFocus ( true ); setAllColumnsShowFocus ( true );
setExpandsOnDoubleClick ( false ); setExpandsOnDoubleClick ( false );
setRootIsDecorated ( false ); setRootIsDecorated ( false );
setSortingEnabled ( false ); setSortingEnabled ( true );
setAlternatingRowColors ( true ); setAlternatingRowColors ( true );
setSelectionMode ( QAbstractItemView::ExtendedSelection ); setSelectionMode ( QAbstractItemView::ExtendedSelection );
setHeaderHidden ( false ); setHeaderHidden ( false );