Simplified sorting logic?

This commit is contained in:
martinrotter 2017-05-05 10:06:53 +02:00
parent 6776d3809d
commit e5e05856cf
2 changed files with 24 additions and 4 deletions

View file

@ -52,6 +52,24 @@ MessagesView::~MessagesView() {
qDebug("Destroying MessagesView instance."); qDebug("Destroying MessagesView instance.");
} }
void MessagesView::sort(int column, Qt::SortOrder order, bool repopulate_data, bool change_header, bool emit_changed_from_header) {
if (change_header && !emit_changed_from_header) {
header()->blockSignals(true);
}
if (repopulate_data) {
m_sourceModel->sort(column, order);
}
else {
m_sourceModel->setSort(column, order);
}
if (change_header) {
header()->setSortIndicator(column, order);
header()->blockSignals(false);
}
}
void MessagesView::createConnections() { void MessagesView::createConnections() {
connect(this, &MessagesView::doubleClicked, this, &MessagesView::openSelectedSourceMessagesExternally); connect(this, &MessagesView::doubleClicked, this, &MessagesView::openSelectedSourceMessagesExternally);
@ -78,7 +96,7 @@ void MessagesView::reloadSelections(bool mark_current_index_read) {
const Qt::SortOrder ord = static_cast<Qt::SortOrder>(qApp->settings()->value(GROUP(GUI), SETTING(GUI::DefaultSortOrderMessages)).toInt()); const Qt::SortOrder ord = static_cast<Qt::SortOrder>(qApp->settings()->value(GROUP(GUI), SETTING(GUI::DefaultSortOrderMessages)).toInt());
// Reload the model now. // Reload the model now.
m_sourceModel->sort(col, ord); sort(col, ord, true, false, false);
// Now, we must find the same previously focused message. // Now, we must find the same previously focused message.
if (selected_message.m_id > 0) { if (selected_message.m_id > 0) {
@ -141,7 +159,7 @@ void MessagesView::setupAppearance() {
setItemDelegate(new StyledItemDelegateWithoutFocus(this)); setItemDelegate(new StyledItemDelegateWithoutFocus(this));
header()->setDefaultSectionSize(MESSAGES_VIEW_DEFAULT_COL); header()->setDefaultSectionSize(MESSAGES_VIEW_DEFAULT_COL);
header()->setStretchLastSection(false); header()->setStretchLastSection(false);
header()->setSortIndicatorShown(false); header()->setSortIndicatorShown(true);
} }
void MessagesView::keyPressEvent(QKeyEvent *event) { void MessagesView::keyPressEvent(QKeyEvent *event) {
@ -268,7 +286,7 @@ void MessagesView::loadItem(RootItem *item) {
const Qt::SortOrder ord = static_cast<Qt::SortOrder>(qApp->settings()->value(GROUP(GUI), SETTING(GUI::DefaultSortOrderMessages)).toInt()); const Qt::SortOrder ord = static_cast<Qt::SortOrder>(qApp->settings()->value(GROUP(GUI), SETTING(GUI::DefaultSortOrderMessages)).toInt());
scrollToTop(); scrollToTop();
m_sourceModel->setSort(col, ord); sort(col, ord, false, true, false);
m_sourceModel->loadMessages(item); m_sourceModel->loadMessages(item);
// Messages are loaded, make sure that previously // Messages are loaded, make sure that previously
@ -562,7 +580,7 @@ void MessagesView::onSortIndicatorChanged(int column, Qt::SortOrder order) {
qApp->settings()->sync(); qApp->settings()->sync();
// Repopulate the shit. // Repopulate the shit.
m_sourceModel->sort(column, order); sort(column, order, true, false, false);
emit currentMessageRemoved(); emit currentMessageRemoved();
qDebug("Current order by clause is '%s'.", qPrintable(m_sourceModel->orderByClause())); qDebug("Current order by clause is '%s'.", qPrintable(m_sourceModel->orderByClause()));

View file

@ -36,6 +36,8 @@ class MessagesView : public QTreeView {
explicit MessagesView(QWidget *parent = 0); explicit MessagesView(QWidget *parent = 0);
virtual ~MessagesView(); virtual ~MessagesView();
void sort(int column, Qt::SortOrder order, bool repopulate_data, bool change_header, bool emit_changed_from_header);
// Model accessors. // Model accessors.
inline MessagesProxyModel *model() const { inline MessagesProxyModel *model() const {
return m_proxyModel; return m_proxyModel;