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)
: 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()) {
setupFonts();
setupIcons();
setupHeaderData();
updateDateFormat();
//m_fieldNames[0] =
// Set desired table and edit strategy.
// NOTE: Changes to the database are actually NOT submitted
// via model, but via DIRECT SQL calls are used to do persistent messages.
@ -48,8 +51,23 @@ MessagesModel::~MessagesModel() {
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 {
auto aaa = record().fieldName(4);
return QSqlRelationalTableModel::orderByClause();
QString clause(QSL("ORDER BY "));
}
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() {
m_normalFont = Application::font("MessagesView");
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;
}
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 {
return data(index(row, column), role);
}

View File

@ -59,10 +59,11 @@ class MessagesModel : public QSqlRelationalTableModel {
RootItem::Importance messageImportance(int row_index) const;
RootItem *loadedItem() const;
void updateDateFormat();
void reloadWholeLayout();
void addSortState(int column, Qt::SortOrder order);
// CORE messages manipulators.
// NOTE: These are used to change properties of one message.
// 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 setBatchMessagesRestored(const QModelIndexList &messages);
// Fetches ALL available data to the model.
void fetchAllData();
// Filters messages
void highlightMessages(MessageHighlighter highlight);
@ -104,6 +102,16 @@ class MessagesModel : public QSqlRelationalTableModel {
void setupFonts();
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;
QString m_customDateFormat;

View File

@ -26,6 +26,7 @@
#define ARGUMENTS_LIST_SEPARATOR "\n"
#define MAX_MULTICOLUMN_SORT_STATES 3
#define ENCLOSURES_OUTER_SEPARATOR '#'
#define ECNLOSURES_INNER_SEPARATOR '&'
#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);
}
m_sourceModel->addSortState(column, order);
if (repopulate_data) {
m_sourceModel->sort(column, order);
}

View File

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