Prep for multicolumn sql sort.

This commit is contained in:
martinrotter 2017-05-09 09:47:42 +02:00
parent e5e05856cf
commit 78872dfa21
5 changed files with 54 additions and 14 deletions

View File

@ -28,12 +28,15 @@
MessagesModel::MessagesModel(QObject *parent) MessagesModel::MessagesModel(QObject *parent)
: QSqlRelationalTableModel(parent, qApp->database()->connection(QSL("MessagesModel"), DatabaseFactory::FromSettings)), : QSqlRelationalTableModel(parent, qApp->database()->connection(QSL("MessagesModel"), DatabaseFactory::FromSettings)),
m_fieldNames(QHash<int,QString>()), m_sortColumn(QList<int>()), m_sortOrder(QList<Qt::SortOrder>()),
m_messageHighlighter(NoHighlighting), m_customDateFormat(QString()) { m_messageHighlighter(NoHighlighting), m_customDateFormat(QString()) {
setupFonts(); setupFonts();
setupIcons(); setupIcons();
setupHeaderData(); setupHeaderData();
updateDateFormat(); updateDateFormat();
//m_fieldNames[0] =
// Set desired table and edit strategy. // Set desired table and edit strategy.
// NOTE: Changes to the database are actually NOT submitted // NOTE: Changes to the database are actually NOT submitted
// via model, but via DIRECT SQL calls are used to do persistent messages. // via model, but via DIRECT SQL calls are used to do persistent messages.
@ -48,8 +51,23 @@ MessagesModel::~MessagesModel() {
qDebug("Destroying MessagesModel instance."); qDebug("Destroying MessagesModel instance.");
} }
QString MessagesModel::selectStatement() const {
//return QSqlRelationalTableModel::selectStatement();
return QL1S("SELECT Messages.id, is_read, is_deleted, is_important, Feeds.title, Messages.title, Messages.url, author, Messages.date_created, contents, is_pdeleted, enclosures, Messages.account_id, Messages.custom_id, custom_hash, Messages.feed "
"FROM Messages LEFT JOIN Feeds ON Messages.feed = Feeds.custom_id WHERE ") +
filter() + " " + orderByClause();
}
QString MessagesModel::orderByClause() const { QString MessagesModel::orderByClause() const {
auto aaa = record().fieldName(4);
return QSqlRelationalTableModel::orderByClause(); return QSqlRelationalTableModel::orderByClause();
QString clause(QSL("ORDER BY "));
} }
void MessagesModel::setupIcons() { void MessagesModel::setupIcons() {
@ -66,6 +84,25 @@ void MessagesModel::fetchAllData() {
} }
} }
void MessagesModel::addSortState(int column, Qt::SortOrder order) {
int existing = m_sortColumn.indexOf(column);
if (existing >= 0) {
m_sortColumn.removeAt(existing);
m_sortOrder.removeAt(existing);
}
if (m_sortColumn.size() > MAX_MULTICOLUMN_SORT_STATES) {
// We support only limited number of sort states
// due to DB performance.
m_sortColumn.removeAt(0);
m_sortOrder.removeAt(0);
}
m_sortColumn.append(column);
m_sortOrder.append(order);
}
void MessagesModel::setupFonts() { void MessagesModel::setupFonts() {
m_normalFont = Application::font("MessagesView"); m_normalFont = Application::font("MessagesView");
m_boldFont = m_normalFont; m_boldFont = m_normalFont;
@ -188,14 +225,6 @@ Qt::ItemFlags MessagesModel::flags(const QModelIndex &index) const {
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemNeverHasChildren; return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemNeverHasChildren;
} }
QString MessagesModel::selectStatement() const {
//return QSqlRelationalTableModel::selectStatement();
return QL1S("SELECT Messages.id, is_read, is_deleted, is_important, Feeds.title, Messages.title, Messages.url, author, Messages.date_created, contents, is_pdeleted, enclosures, Messages.account_id, Messages.custom_id, custom_hash, Messages.feed "
"FROM Messages LEFT JOIN Feeds ON Messages.feed = Feeds.custom_id WHERE ") +
filter() + " " + orderByClause();
}
QVariant MessagesModel::data(int row, int column, int role) const { QVariant MessagesModel::data(int row, int column, int role) const {
return data(index(row, column), role); return data(index(row, column), role);
} }

View File

@ -59,10 +59,11 @@ class MessagesModel : public QSqlRelationalTableModel {
RootItem::Importance messageImportance(int row_index) const; RootItem::Importance messageImportance(int row_index) const;
RootItem *loadedItem() const; RootItem *loadedItem() const;
void updateDateFormat(); void updateDateFormat();
void reloadWholeLayout(); void reloadWholeLayout();
void addSortState(int column, Qt::SortOrder order);
// CORE messages manipulators. // CORE messages manipulators.
// NOTE: These are used to change properties of one message. // NOTE: These are used to change properties of one message.
// NOTE: Model is NOT reset after one of these methods are applied // NOTE: Model is NOT reset after one of these methods are applied
@ -80,9 +81,6 @@ class MessagesModel : public QSqlRelationalTableModel {
bool setBatchMessagesRead(const QModelIndexList &messages, RootItem::ReadStatus read); bool setBatchMessagesRead(const QModelIndexList &messages, RootItem::ReadStatus read);
bool setBatchMessagesRestored(const QModelIndexList &messages); bool setBatchMessagesRestored(const QModelIndexList &messages);
// Fetches ALL available data to the model.
void fetchAllData();
// Filters messages // Filters messages
void highlightMessages(MessageHighlighter highlight); void highlightMessages(MessageHighlighter highlight);
@ -104,6 +102,16 @@ class MessagesModel : public QSqlRelationalTableModel {
void setupFonts(); void setupFonts();
void setupIcons(); void setupIcons();
// Fetches ALL available data to the model.
void fetchAllData();
// NOTE: These two lists contain data for multicolumn sorting.
// They are always same length. Most important sort column/order
// are located in the end of these lists.
QHash<int,QString> m_fieldNames;
QList<int> m_sortColumn;
QList<Qt::SortOrder> m_sortOrder;
MessageHighlighter m_messageHighlighter; MessageHighlighter m_messageHighlighter;
QString m_customDateFormat; QString m_customDateFormat;

View File

@ -26,6 +26,7 @@
#define ARGUMENTS_LIST_SEPARATOR "\n" #define ARGUMENTS_LIST_SEPARATOR "\n"
#define MAX_MULTICOLUMN_SORT_STATES 3
#define ENCLOSURES_OUTER_SEPARATOR '#' #define ENCLOSURES_OUTER_SEPARATOR '#'
#define ECNLOSURES_INNER_SEPARATOR '&' #define ECNLOSURES_INNER_SEPARATOR '&'
#define URI_SCHEME_FEED_SHORT "feed:" #define URI_SCHEME_FEED_SHORT "feed:"

View File

@ -57,6 +57,8 @@ void MessagesView::sort(int column, Qt::SortOrder order, bool repopulate_data, b
header()->blockSignals(true); header()->blockSignals(true);
} }
m_sourceModel->addSortState(column, order);
if (repopulate_data) { if (repopulate_data) {
m_sourceModel->sort(column, order); m_sourceModel->sort(column, order);
} }

View File

@ -36,8 +36,6 @@ 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;
@ -101,6 +99,8 @@ class MessagesView : public QTreeView {
void currentMessageRemoved(); void currentMessageRemoved();
private: private:
void sort(int column, Qt::SortOrder order, bool repopulate_data, bool change_header, bool emit_changed_from_header);
// Creates needed connections. // Creates needed connections.
void createConnections(); void createConnections();