This commit is contained in:
Martin Rotter 2024-05-22 09:59:58 +02:00
parent 24f74394e1
commit 66d70f2b7f
8 changed files with 51 additions and 15 deletions

View file

@ -142,21 +142,24 @@ MessagesModelCache* MessagesModel::cache() const {
return m_cache;
}
void MessagesModel::repopulate() {
void MessagesModel::repopulate(int additional_article_id) {
m_cache->clear();
setQuery(selectStatement(), m_db);
QString statemnt = selectStatement(additional_article_id);
setQuery(statemnt, m_db);
if (lastError().isValid()) {
qCriticalNN << LOGSEC_MESSAGEMODEL << "Error when setting new msg view query: '" << lastError().text() << "'.";
qCriticalNN << LOGSEC_MESSAGEMODEL << "Used SQL select statement: '" << selectStatement() << "'.";
qCriticalNN << LOGSEC_MESSAGEMODEL
<< "Error when setting new msg view query:" << QUOTE_W_SPACE_DOT(lastError().text());
qCriticalNN << LOGSEC_MESSAGEMODEL << "Used SQL select statement:" << QUOTE_W_SPACE_DOT(statemnt);
}
while (canFetchMore()) {
fetchMore();
}
qDebugNN << LOGSEC_MESSAGEMODEL << "Repopulated model, SQL statement is now:\n"
<< QUOTE_W_SPACE_DOT(selectStatement());
qDebugNN << LOGSEC_MESSAGEMODEL << "Repopulated model, SQL statement is now:\n" << QUOTE_W_SPACE_DOT(statemnt);
}
bool MessagesModel::setData(const QModelIndex& idx, const QVariant& value, int role) {

View file

@ -41,7 +41,7 @@ class MessagesModel : public QSqlQueryModel, public MessagesModelSqlLayer {
// Fetches ALL available data to the model.
// NOTE: This activates the SQL query and populates the model with new data.
void repopulate();
void repopulate(int additional_article_id = 0);
// Model implementation.
bool setData(const QModelIndex& idx, const QVariant& value, int role = Qt::EditRole);

View file

@ -96,12 +96,21 @@ bool MessagesModelSqlLayer::isColumnNumeric(int column_id) const {
return m_numericColumns.contains(column_id);
}
QString MessagesModelSqlLayer::selectStatement() const {
QString MessagesModelSqlLayer::selectStatement(int additional_article_id) const {
QString fltr;
if (additional_article_id <= 0) {
fltr = m_filter;
}
else {
fltr = QSL("(%1) OR Messages.id = %2").arg(m_filter, QString::number(additional_article_id));
}
return QL1S("SELECT ") + formatFields() + QL1C(' ') +
QL1S("FROM Messages LEFT JOIN Feeds ON Messages.feed = Feeds.custom_id AND Messages.account_id = "
"Feeds.account_id "
"WHERE ") +
m_filter + orderByClause() + QL1C(';');
fltr + orderByClause() + QL1C(';');
}
QString MessagesModelSqlLayer::orderByClause() const {

View file

@ -27,7 +27,7 @@ class MessagesModelSqlLayer {
protected:
QString orderByClause() const;
QString selectStatement() const;
QString selectStatement(int additional_article_id = -1) const;
QString formatFields() const;
bool isColumnNumeric(int column_id) const;

View file

@ -11,7 +11,8 @@
#include <QTimer>
MessagesProxyModel::MessagesProxyModel(MessagesModel* source_model, QObject* parent)
: QSortFilterProxyModel(parent), m_sourceModel(source_model), m_filter(MessageListFilter::NoFiltering) {
: QSortFilterProxyModel(parent), m_sourceModel(source_model), m_filter(MessageListFilter::NoFiltering),
m_additionalArticleId(0) {
setObjectName(QSL("MessagesProxyModel"));
initializeFilters();
@ -118,6 +119,11 @@ bool MessagesProxyModel::filterAcceptsMessage(int msg_row_index) const {
if (m_filter == MessageListFilter::NoFiltering) {
return true;
}
else if (m_additionalArticleId > 0 &&
m_sourceModel->data(msg_row_index, MSG_DB_ID_INDEX, Qt::ItemDataRole::EditRole).toInt() ==
m_additionalArticleId) {
return true;
}
for (MessageListFilter val : m_filterKeys) {
if (Globals::hasFlag(m_filter, val) && m_filters[val](msg_row_index)) {
@ -225,8 +231,17 @@ bool MessagesProxyModel::filterAcceptsRow(int source_row, const QModelIndex& sou
(m_sourceModel->cache()->containsData(source_row) || filterAcceptsMessage(source_row));
}
int MessagesProxyModel::additionalArticleId() const {
return m_additionalArticleId;
}
void MessagesProxyModel::setAdditionalArticleId(int newAdditionalArticleId) {
m_additionalArticleId = newAdditionalArticleId;
}
void MessagesProxyModel::setMessageListFilter(MessageListFilter filter) {
m_filter = filter;
m_additionalArticleId = 0;
}
QModelIndexList MessagesProxyModel::mapListFromSource(const QModelIndexList& indexes, bool deep) const {

View file

@ -52,6 +52,9 @@ class MessagesProxyModel : public QSortFilterProxyModel {
// Performs sort of items.
virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
int additionalArticleId() const;
void setAdditionalArticleId(int newAdditionalArticleId);
private:
void initializeFilters();
@ -68,6 +71,7 @@ class MessagesProxyModel : public QSortFilterProxyModel {
MessageListFilter m_filter;
QMap<MessageListFilter, std::function<bool(int)>> m_filters;
QList<MessageListFilter> m_filterKeys;
int m_additionalArticleId;
};
Q_DECLARE_METATYPE(MessagesProxyModel::MessageListFilter)

View file

@ -175,15 +175,17 @@ void MessagesView::sort(int column,
bool repopulate_data,
bool change_header,
bool emit_changed_from_header,
bool ignore_multicolumn_sorting) {
bool ignore_multicolumn_sorting,
int additional_article_id) {
if (change_header && !emit_changed_from_header) {
header()->blockSignals(true);
}
m_sourceModel->addSortState(column, order, ignore_multicolumn_sorting);
m_proxyModel->setAdditionalArticleId(additional_article_id);
if (repopulate_data) {
m_sourceModel->repopulate();
m_sourceModel->repopulate(additional_article_id);
}
if (change_header) {
@ -209,6 +211,8 @@ void MessagesView::keyboardSearch(const QString& search) {
}
void MessagesView::reloadSelections() {
bool force_load_currently_selected_article = true;
const QDateTime dt1 = QDateTime::currentDateTime();
QModelIndex current_index = selectionModel()->currentIndex();
const bool is_current_selected =
@ -221,7 +225,7 @@ void MessagesView::reloadSelections() {
bool do_not_mark_read_on_select = false;
// Reload the model now.
sort(col, ord, true, false, false, true);
sort(col, ord, true, false, false, true, force_load_currently_selected_article ? selected_message_id : 0);
// Now, we must find the same previously focused message.
if (selected_message_id > 0) {

View file

@ -120,7 +120,8 @@ class MessagesView : public BaseTreeView {
bool repopulate_data,
bool change_header,
bool emit_changed_from_header,
bool ignore_multicolumn_sorting);
bool ignore_multicolumn_sorting,
int additional_article_id = 0);
// Creates needed connections.
void createConnections();