Update counts of all/unread msgs with tags.

This commit is contained in:
Martin Rotter 2020-10-08 19:37:52 +02:00
parent 1e1b3f5255
commit 44e467b9c7
8 changed files with 91 additions and 5 deletions

View File

@ -8,6 +8,8 @@
#include <QRandomGenerator>
ColorToolButton::ColorToolButton(QWidget* parent) : QToolButton(parent), m_color(Qt::GlobalColor::black) {
setToolTip(tr("Click me to change color!"));
connect(this, &ColorToolButton::clicked, this, [this]() {
auto new_color = QColorDialog::getColor(m_color, parentWidget(), tr("Select new color"),
QColorDialog::ColorDialogOption::DontUseNativeDialog |

View File

@ -49,7 +49,7 @@ MessagePreviewer::MessagePreviewer(QWidget* parent)
m_txtMessage = new MessageBrowser(this);
#endif
m_toolBar->setOrientation(Qt::Vertical);
m_toolBar->setOrientation(Qt::Orientation::Vertical);
m_layout->setContentsMargins(3, 3, 3, 3);
m_layout->addWidget(m_txtMessage, 0, 1, 1, 1);
m_layout->addWidget(m_toolBar, 0, 0, -1, 1);

View File

@ -396,6 +396,49 @@ int DatabaseQueries::getMessageCountsForFeed(const QSqlDatabase& db, const QStri
}
}
int DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db, Label* label, int account_id, bool only_total_counts, bool* ok) {
QSqlQuery q(db);
q.setForwardOnly(true);
if (only_total_counts) {
q.prepare("SELECT COUNT(*) FROM Messages "
"INNER JOIN LabelsInMessages "
"ON "
" Messages.is_pdeleted = 0 AND Messages.is_deleted = 0 AND "
" LabelsInMessages.account_id = :account_id AND LabelsInMessages.account_id = Messages.account_id AND "
" LabelsInMessages.label = :label AND LabelsInMessages.message = Messages.custom_id;");
}
else {
q.prepare("SELECT COUNT(*) FROM Messages "
"INNER JOIN LabelsInMessages "
"ON "
" Messages.is_pdeleted = 0 AND Messages.is_deleted = 0 AND Messages.is_read = 0 AND "
" LabelsInMessages.account_id = :account_id AND LabelsInMessages.account_id = Messages.account_id AND "
" LabelsInMessages.label = :label AND LabelsInMessages.message = Messages.custom_id;");
}
q.bindValue(QSL(":account_id"), account_id);
q.bindValue(QSL(":label"), label->customId());
if (q.exec() && q.next()) {
if (ok != nullptr) {
*ok = true;
}
return q.value(0).toInt();
}
else {
auto aa = q.lastError().text();
if (ok != nullptr) {
*ok = false;
}
return 0;
}
}
int DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool only_total_counts, bool* ok) {
QSqlQuery q(db);

View File

@ -52,6 +52,8 @@ class DatabaseQueries {
bool only_total_counts, bool* ok = nullptr);
static int getMessageCountsForFeed(const QSqlDatabase& db, const QString& feed_custom_id, int account_id,
bool only_total_counts, bool* ok = nullptr);
static int getMessageCountsForLabel(const QSqlDatabase& db, Label* label, int account_id,
bool only_total_counts, bool* ok = nullptr);
static int getImportantMessageCounts(const QSqlDatabase& db, int account_id,
bool only_total_counts, bool* ok = nullptr);
static int getMessageCountsForBin(const QSqlDatabase& db, int account_id, bool including_total_counts, bool* ok = nullptr);

View File

@ -29,6 +29,14 @@ void Label::setColor(const QColor& color) {
m_color = color;
}
int Label::countOfUnreadMessages() const {
return m_unreadCount;
}
int Label::countOfAllMessages() const {
return m_totalCount;
}
bool Label::canBeEdited() const {
return true;
}
@ -62,6 +70,17 @@ bool Label::deleteViaGui() {
}
}
void Label::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
int account_id = getParentServiceRoot()->accountId();
if (including_total_count) {
setCountOfAllMessages(DatabaseQueries::getMessageCountsForLabel(database, this, account_id, true));
}
setCountOfUnreadMessages(DatabaseQueries::getMessageCountsForLabel(database, this, account_id, false));
}
QIcon Label::generateIcon(const QColor& color) {
QPixmap pxm(64, 64);
@ -70,8 +89,16 @@ QIcon Label::generateIcon(const QColor& color) {
QPainter paint(&pxm);
QPainterPath path;
path.addRoundedRect(QRectF(pxm.rect()), 10, 10);
path.addRoundedRect(QRectF(pxm.rect()), 16, 16);
paint.fillPath(path, color);
return pxm;
}
void Label::setCountOfAllMessages(int totalCount) {
m_totalCount = totalCount;
}
void Label::setCountOfUnreadMessages(int unreadCount) {
m_unreadCount = unreadCount;
}

View File

@ -17,14 +17,22 @@ class Label : public RootItem {
QColor color() const;
void setColor(const QColor& color);
void setCountOfAllMessages(int totalCount);
void setCountOfUnreadMessages(int unreadCount);
virtual int countOfAllMessages() const;
virtual int countOfUnreadMessages() const;
virtual bool canBeEdited() const;
virtual bool editViaGui();
virtual bool canBeDeleted() const;
virtual bool deleteViaGui();
virtual void updateCounts(bool including_total_count);
static QIcon generateIcon(const QColor& color);
private:
QColor m_color;
int m_totalCount{};
int m_unreadCount{};
};
#endif // LABEL_H

View File

@ -201,13 +201,13 @@ bool RootItem::performDragDropChange(RootItem* target_item) {
int RootItem::countOfUnreadMessages() const {
return boolinq::from(m_childItems).sum([](RootItem* it) {
return it->kind() == RootItem::Kind::Important ? 0 : it->countOfUnreadMessages();
return (it->kind() == RootItem::Kind::Important || it->kind() == RootItem::Kind::Labels) ? 0 : it->countOfUnreadMessages();
});
}
int RootItem::countOfAllMessages() const {
return boolinq::from(m_childItems).sum([](RootItem* it) {
return it->kind() == RootItem::Kind::Important ? 0 : it->countOfAllMessages();
return (it->kind() == RootItem::Kind::Important || it->kind() == RootItem::Kind::Labels) ? 0 : it->countOfAllMessages();
});
}

View File

@ -142,7 +142,11 @@ void StandardServiceRoot::loadFromDatabase() {
// As the last item, add recycle bin, which is needed.
appendChild(recycleBin());
appendChild(importantNode());
appendChild(new LabelsNode(DatabaseQueries::getLabels(database, accountId()), this));
auto* labelss = new LabelsNode(DatabaseQueries::getLabels(database, accountId()), this);
appendChild(labelss);
requestItemExpand({ labelss }, true);
updateCounts(true);
}