From 50bb3f5fd77b94824ded06f421f7859a50ae408e Mon Sep 17 00:00:00 2001 From: blue Date: Mon, 22 Mar 2021 21:04:26 +0300 Subject: [PATCH] started progress bars, changed gcc standard to 17 --- CMakeLists.txt | 2 +- ui/models/messagefeed.cpp | 2 +- ui/squawk.cpp | 2 + ui/utils/messagedelegate.cpp | 91 ++++++++++++++++++++++++++++++++++-- ui/utils/messagedelegate.h | 6 +++ ui/widgets/conversation.h | 1 - 6 files changed, 96 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f02df03..7418a7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.4) project(squawk) set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index 55451fb..01b1a9d 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -302,7 +302,7 @@ void Models::MessageFeed::downloadAttachment(const QString& messageId) QModelIndex ind = modelIndexById(messageId); if (ind.isValid()) { std::pair progressPair = downloads.insert(std::make_pair(messageId, 0)); - if (!progressPair.second) { //Only to take action if we weren't already downloading it + if (progressPair.second) { //Only to take action if we weren't already downloading it Shared::Message* msg = static_cast(ind.internalPointer()); emit dataChanged(ind, ind); emit fileLocalPathRequest(messageId, msg->getOutOfBandUrl()); diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 55bfe63..6b8416f 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -431,6 +431,8 @@ void Squawk::fileError(const QString& messageId, const QString& error) } } + +//TODO! Need to make it look like a standard message change event! void Squawk::fileLocalPathResponse(const QString& messageId, const QString& path) { std::map>::const_iterator itr = requestedFiles.find(messageId); diff --git a/ui/utils/messagedelegate.cpp b/ui/utils/messagedelegate.cpp index 038b0af..ff5b1fb 100644 --- a/ui/utils/messagedelegate.cpp +++ b/ui/utils/messagedelegate.cpp @@ -36,12 +36,17 @@ bodyMetrics(bodyFont), nickMetrics(nickFont), dateMetrics(dateFont), buttonHeight(0), +barHeight(0), buttons(new std::map()), +bars(new std::map()), idsToKeep(new std::set()), clearingWidgets(false) { QPushButton btn; buttonHeight = btn.sizeHint().height(); + + QProgressBar bar; + barHeight = bar.sizeHint().height(); } MessageDelegate::~MessageDelegate() @@ -50,8 +55,13 @@ MessageDelegate::~MessageDelegate() delete pair.second; } + for (const std::pair& pair: *bars){ + delete pair.second; + } + delete idsToKeep; delete buttons; + delete bars; } void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const @@ -110,9 +120,11 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio painter->save(); switch (data.attach.state) { case Models::none: - break; + clearHelperWidget(data); //i can't imagine the situation where it's gonna be needed + break; //but it's a possible performance problem case Models::uploading: case Models::downloading: + paintBar(getBar(data), painter, data.sentByMe, opt); break; case Models::remote: case Models::local: @@ -159,6 +171,7 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel break; case Models::uploading: case Models::downloading: + messageSize.rheight() += barHeight; break; case Models::remote: case Models::local: @@ -225,6 +238,18 @@ void MessageDelegate::paintButton(QPushButton* btn, QPainter* painter, bool sent option.rect.adjust(0, buttonHeight, 0, 0); } +void MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const +{ + QPoint start = option.rect.topLeft(); + + QWidget* vp = static_cast(painter->device()); + bar->setParent(vp); + bar->move(start); + bar->resize(option.rect.width(), barHeight); + bar->show(); + + option.rect.adjust(0, barHeight, 0, 0); +} QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const { @@ -240,6 +265,12 @@ QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const delete itr->second; buttons->erase(itr); } + } else { + std::map::const_iterator barItr = bars->find(data.id); + if (barItr != bars->end()) { + delete barItr->second; + bars->erase(barItr); + } } if (result == 0) { @@ -259,6 +290,30 @@ QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const return result; } +QProgressBar * MessageDelegate::getBar(const Models::FeedItem& data) const +{ + std::map::const_iterator barItr = bars->find(data.id); + QProgressBar* result = 0; + if (barItr != bars->end()) { + result = barItr->second; + } else { + std::map::const_iterator itr = buttons->find(data.id); + if (itr != buttons->end()) { + delete itr->second; + buttons->erase(itr); + } + } + + if (result == 0) { + result = new QProgressBar(); + bars->insert(std::make_pair(data.id, result)); + } + + result->setValue(data.attach.progress); + + return result; +} + void MessageDelegate::beginClearWidgets() { @@ -269,17 +324,27 @@ void MessageDelegate::beginClearWidgets() void MessageDelegate::endClearWidgets() { if (clearingWidgets) { - std::set toRemove; - for (const std::pair& pair: *buttons){ + std::set toRemoveButtons; + std::set toRemoveBars; + for (const std::pair& pair: *buttons) { if (idsToKeep->find(pair.first) == idsToKeep->end()) { delete pair.second; - toRemove.insert(pair.first); + toRemoveButtons.insert(pair.first); + } + } + for (const std::pair& pair: *bars) { + if (idsToKeep->find(pair.first) == idsToKeep->end()) { + delete pair.second; + toRemoveBars.insert(pair.first); } } - for (const QString& key : toRemove) { + for (const QString& key : toRemoveButtons) { buttons->erase(key); } + for (const QString& key : toRemoveBars) { + bars->erase(key); + } idsToKeep->clear(); clearingWidgets = false; @@ -292,6 +357,22 @@ void MessageDelegate::onButtonPushed() const emit buttonPushed(btn->messageId, btn->download); } +void MessageDelegate::clearHelperWidget(const Models::FeedItem& data) const +{ + std::map::const_iterator itr = buttons->find(data.id); + if (itr != buttons->end()) { + delete itr->second; + buttons->erase(itr); + } else { + std::map::const_iterator barItr = bars->find(data.id); + if (barItr != bars->end()) { + delete barItr->second; + bars->erase(barItr); + } + } +} + + // void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const // { // diff --git a/ui/utils/messagedelegate.h b/ui/utils/messagedelegate.h index 69ffb84..42c8ed5 100644 --- a/ui/utils/messagedelegate.h +++ b/ui/utils/messagedelegate.h @@ -27,6 +27,7 @@ #include #include #include +#include #include "shared/icons.h" @@ -55,7 +56,10 @@ signals: protected: void paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const; + void paintBar(QProgressBar* bar, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const; QPushButton* getButton(const Models::FeedItem& data) const; + QProgressBar* getBar(const Models::FeedItem& data) const; + void clearHelperWidget(const Models::FeedItem& data) const; protected slots: void onButtonPushed() const; @@ -75,8 +79,10 @@ private: QFontMetrics dateMetrics; int buttonHeight; + int barHeight; std::map* buttons; + std::map* bars; std::set* idsToKeep; bool clearingWidgets; }; diff --git a/ui/widgets/conversation.h b/ui/widgets/conversation.h index ac1ffcd..7d10aff 100644 --- a/ui/widgets/conversation.h +++ b/ui/widgets/conversation.h @@ -99,7 +99,6 @@ protected slots: void onEnterPressed(); void onAttach(); void onFileSelected(); - void onScrollResize(); void onBadgeClose(); void onClearButton(); void onTextEditDocSizeChanged(const QSizeF& size);