1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-27 05:10:26 +00:00

implemented sorting in report views (Fixes #2540)

This commit is contained in:
Marc Zinnschlag 2015-07-03 10:45:08 +02:00
parent 2385938485
commit 382cdb8c60
2 changed files with 52 additions and 24 deletions

View file

@ -11,6 +11,7 @@
#include <QPainter> #include <QPainter>
#include <QContextMenuEvent> #include <QContextMenuEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QSortFilterProxyModel>
#include "../../model/tools/reportmodel.hpp" #include "../../model/tools/reportmodel.hpp"
@ -63,7 +64,7 @@ void CSVTools::ReportTable::contextMenuEvent (QContextMenuEvent *event)
for (QModelIndexList::const_iterator iter (selectedRows.begin()); for (QModelIndexList::const_iterator iter (selectedRows.begin());
iter!=selectedRows.end(); ++iter) iter!=selectedRows.end(); ++iter)
{ {
QString hint = mModel->data (mModel->index (iter->row(), 2)).toString(); QString hint = mProxyModel->data (mProxyModel->index (iter->row(), 2)).toString();
if (!hint.isEmpty() && hint[0]=='R') if (!hint.isEmpty() && hint[0]=='R')
{ {
@ -152,7 +153,10 @@ CSVTools::ReportTable::ReportTable (CSMDoc::Document& document,
setSelectionBehavior (QAbstractItemView::SelectRows); setSelectionBehavior (QAbstractItemView::SelectRows);
setSelectionMode (QAbstractItemView::ExtendedSelection); setSelectionMode (QAbstractItemView::ExtendedSelection);
setModel (mModel); mProxyModel = new QSortFilterProxyModel (this);
mProxyModel->setSourceModel (mModel);
setModel (mProxyModel);
setColumnHidden (2, true); setColumnHidden (2, true);
mIdTypeDelegate = CSVWorld::IdTypeDelegateFactory().makeDelegate (0, mIdTypeDelegate = CSVWorld::IdTypeDelegateFactory().makeDelegate (0,
@ -197,7 +201,7 @@ std::vector<CSMWorld::UniversalId> CSVTools::ReportTable::getDraggedRecords() co
for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end(); for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end();
++iter) ++iter)
{ {
ids.push_back (mModel->getUniversalId (iter->row())); ids.push_back (mModel->getUniversalId (mProxyModel->mapToSource (*iter).row()));
} }
return ids; return ids;
@ -245,13 +249,22 @@ std::vector<int> CSVTools::ReportTable::getReplaceIndices (bool selection) const
{ {
QModelIndexList selectedRows = selectionModel()->selectedRows(); QModelIndexList selectedRows = selectionModel()->selectedRows();
std::vector<int> rows;
for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end(); for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end();
++iter) ++iter)
{ {
QString hint = mModel->data (mModel->index (iter->row(), 2)).toString(); rows.push_back (mProxyModel->mapToSource (*iter).row());
}
std::sort (rows.begin(), rows.end());
for (std::vector<int>::const_iterator iter (rows.begin()); iter!=rows.end(); ++iter)
{
QString hint = mModel->data (mModel->index (*iter, 2)).toString();
if (!hint.isEmpty() && hint[0]=='R') if (!hint.isEmpty() && hint[0]=='R')
indices.push_back (iter->row()); indices.push_back (*iter);
} }
} }
else else
@ -279,18 +292,28 @@ void CSVTools::ReportTable::showSelection()
for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end(); for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end();
++iter) ++iter)
emit editRequest (mModel->getUniversalId (iter->row()), mModel->getHint (iter->row())); {
int row = mProxyModel->mapToSource (*iter).row();
emit editRequest (mModel->getUniversalId (row), mModel->getHint (row));
}
} }
void CSVTools::ReportTable::removeSelection() void CSVTools::ReportTable::removeSelection()
{ {
QModelIndexList selectedRows = selectionModel()->selectedRows(); QModelIndexList selectedRows = selectionModel()->selectedRows();
std::reverse (selectedRows.begin(), selectedRows.end()); std::vector<int> rows;
for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end(); for (QModelIndexList::iterator iter (selectedRows.begin()); iter!=selectedRows.end();
++iter) ++iter)
mModel->removeRows (iter->row(), 1); {
rows.push_back (mProxyModel->mapToSource (*iter).row());
}
std::sort (rows.begin(), rows.end());
for (std::vector<int>::const_reverse_iterator iter (rows.rbegin()); iter!=rows.rend(); ++iter)
mProxyModel->removeRows (*iter, 1);
selectionModel()->clear(); selectionModel()->clear();
} }

View file

@ -6,6 +6,7 @@
#include "../world/dragrecordtable.hpp" #include "../world/dragrecordtable.hpp"
class QAction; class QAction;
class QSortFilterProxyModel;
namespace CSMTools namespace CSMTools
{ {
@ -31,6 +32,7 @@ namespace CSVTools
Action_EditAndRemove Action_EditAndRemove
}; };
QSortFilterProxyModel *mProxyModel;
CSMTools::ReportModel *mModel; CSMTools::ReportModel *mModel;
CSVWorld::CommandDelegate *mIdTypeDelegate; CSVWorld::CommandDelegate *mIdTypeDelegate;
QAction *mShowAction; QAction *mShowAction;
@ -63,11 +65,14 @@ namespace CSVTools
void clear(); void clear();
// Return indices of rows that are suitable for replacement. /// Return indices of rows that are suitable for replacement.
// ///
// \param selection Only list selected rows. /// \param selection Only list selected rows.
///
/// \return rows in the original model
std::vector<int> getReplaceIndices (bool selection) const; std::vector<int> getReplaceIndices (bool selection) const;
/// \param index row in the original model
void flagAsReplaced (int index); void flagAsReplaced (int index);
private slots: private slots: