From d8fcc1a3edf4b3829b8b4f78944e04a2724023fd Mon Sep 17 00:00:00 2001 From: fourtf Date: Wed, 14 Nov 2018 17:26:08 +0100 Subject: [PATCH] small refactor --- src/messages/layouts/MessageLayoutElement.cpp | 16 +- src/messages/layouts/MessageLayoutElement.hpp | 10 +- src/singletons/WindowManager.cpp | 16 +- src/singletons/WindowManager.hpp | 17 ++- src/widgets/Window.cpp | 13 -- src/widgets/Window.hpp | 2 - src/widgets/helper/ChannelView.cpp | 140 +++++++++--------- src/widgets/helper/ChannelView.hpp | 9 +- src/widgets/splits/Split.cpp | 10 +- src/widgets/splits/Split.hpp | 1 - 10 files changed, 106 insertions(+), 128 deletions(-) diff --git a/src/messages/layouts/MessageLayoutElement.cpp b/src/messages/layouts/MessageLayoutElement.cpp index aaefca5ed..97915ae1c 100644 --- a/src/messages/layouts/MessageLayoutElement.cpp +++ b/src/messages/layouts/MessageLayoutElement.cpp @@ -174,9 +174,9 @@ TextLayoutElement::TextLayoutElement(MessageElement &_creator, QString &_text, const QSize &_size, QColor _color, FontStyle _style, float _scale) : MessageLayoutElement(_creator, _size) - , color(_color) - , style(_style) - , scale(_scale) + , color_(_color) + , style_(_style) + , scale_(_scale) { this->setText(_text); } @@ -212,9 +212,9 @@ void TextLayoutElement::paint(QPainter &painter) { auto app = getApp(); - painter.setPen(this->color); + painter.setPen(this->color_); - painter.setFont(app->fonts->getFont(this->style, this->scale)); + painter.setFont(app->fonts->getFont(this->style_, this->scale_)); painter.drawText( QRectF(this->getRect().x(), this->getRect().y(), 10000, 10000), @@ -234,7 +234,8 @@ int TextLayoutElement::getMouseOverIndex(const QPoint &abs) const auto app = getApp(); - QFontMetrics metrics = app->fonts->getFontMetrics(this->style, this->scale); + QFontMetrics metrics = + app->fonts->getFontMetrics(this->style_, this->scale_); int x = this->getRect().left(); @@ -264,7 +265,8 @@ int TextLayoutElement::getXFromIndex(int index) { auto app = getApp(); - QFontMetrics metrics = app->fonts->getFontMetrics(this->style, this->scale); + QFontMetrics metrics = + app->fonts->getFontMetrics(this->style_, this->scale_); if (index <= 0) { diff --git a/src/messages/layouts/MessageLayoutElement.hpp b/src/messages/layouts/MessageLayoutElement.hpp index 775328a46..d5e11a0a3 100644 --- a/src/messages/layouts/MessageLayoutElement.hpp +++ b/src/messages/layouts/MessageLayoutElement.hpp @@ -80,8 +80,8 @@ class TextLayoutElement : public MessageLayoutElement { public: TextLayoutElement(MessageElement &creator_, QString &text, - const QSize &size, QColor color, FontStyle style, - float scale); + const QSize &size, QColor color_, FontStyle style_, + float scale_); void listenToLinkChanges(); @@ -95,9 +95,9 @@ protected: int getXFromIndex(int index) override; private: - QColor color; - FontStyle style; - float scale; + QColor color_; + FontStyle style_; + float scale_; std::vector managedConnections_; }; diff --git a/src/singletons/WindowManager.cpp b/src/singletons/WindowManager.cpp index a77ed22a1..71277d556 100644 --- a/src/singletons/WindowManager.cpp +++ b/src/singletons/WindowManager.cpp @@ -127,8 +127,7 @@ void WindowManager::updateWordTypeMask() // bits flags.set(MEF::BitsAmount); - flags.set(settings->animateEmotes ? MEF::BitsAnimated - : MEF::BitsStatic); + flags.set(settings->animateEmotes ? MEF::BitsAnimated : MEF::BitsStatic); // badges flags.set(settings->showBadgesGlobalAuthority ? MEF::BadgeGlobalAuthority @@ -148,9 +147,9 @@ void WindowManager::updateWordTypeMask() flags.set(MEF::AlwaysShow); flags.set(MEF::Collapsed); flags.set(settings->boldUsernames ? MEF::BoldUsername - : MEF::NonBoldUsername); + : MEF::NonBoldUsername); flags.set(settings->lowercaseDomains ? MEF::LowercaseLink - : MEF::OriginalLink); + : MEF::OriginalLink); // update flags MessageElementFlags newFlags = static_cast(flags); @@ -165,7 +164,7 @@ void WindowManager::updateWordTypeMask() void WindowManager::layoutChannelViews(Channel *channel) { - this->layout.invoke(channel); + this->layoutRequested.invoke(channel); } void WindowManager::forceLayoutChannelViews() @@ -176,15 +175,12 @@ void WindowManager::forceLayoutChannelViews() void WindowManager::repaintVisibleChatWidgets(Channel *channel) { - if (this->mainWindow_ != nullptr) - { - this->mainWindow_->repaintVisibleChatWidgets(channel); - } + this->layoutRequested.invoke(channel); } void WindowManager::repaintGifEmotes() { - this->repaintGifs.invoke(); + this->gifRepaintRequested.invoke(); } // void WindowManager::updateAll() diff --git a/src/singletons/WindowManager.hpp b/src/singletons/WindowManager.hpp index 65ade477b..522729c7d 100644 --- a/src/singletons/WindowManager.hpp +++ b/src/singletons/WindowManager.hpp @@ -67,14 +67,6 @@ public: MessageElementFlags getWordFlags(); void updateWordTypeMask(); - pajlada::Signals::NoArgSignal repaintGifs; - - // This signal fires whenever views rendering a channel, or all views if the - // channel is a nullptr, need to redo their layout - pajlada::Signals::Signal layout; - - pajlada::Signals::NoArgSignal wordFlagsChanged; - // Sends an alert to the main window // It reads the `longAlert` setting to decide whether the alert will expire // or not @@ -85,6 +77,15 @@ public: // again void queueSave(); + /// Signals + pajlada::Signals::NoArgSignal gifRepaintRequested; + + // This signal fires whenever views rendering a channel, or all views if the + // channel is a nullptr, need to redo their layout + pajlada::Signals::Signal layoutRequested; + + pajlada::Signals::NoArgSignal wordFlagsChanged; + private: void encodeNodeRecusively(SplitContainer::Node *node, QJsonObject &obj); diff --git a/src/widgets/Window.cpp b/src/widgets/Window.cpp index e72c191c1..5e21e8d0d 100644 --- a/src/widgets/Window.cpp +++ b/src/widgets/Window.cpp @@ -68,19 +68,6 @@ SplitNotebook &Window::getNotebook() return *this->notebook_; } -void Window::repaintVisibleChatWidgets(Channel *channel) -{ - auto page = this->notebook_->getOrAddSelectedPage(); - - for (const auto &split : page->getSplits()) - { - if (channel == nullptr || channel == split->getChannel().get()) - { - split->layoutMessages(); - } - } -} - bool Window::event(QEvent *event) { switch (event->type()) diff --git a/src/widgets/Window.hpp b/src/widgets/Window.hpp index f9267a81f..e27bdfae9 100644 --- a/src/widgets/Window.hpp +++ b/src/widgets/Window.hpp @@ -25,8 +25,6 @@ public: WindowType getType(); SplitNotebook &getNotebook(); - void repaintVisibleChatWidgets(Channel *channel = nullptr); - pajlada::Signals::NoArgSignal closed; protected: diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index e8ed1edd1..215a2d96d 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -168,13 +168,16 @@ void ChannelView::initializeSignals() getSettings()->showLastMessageIndicator.connect( [this](auto, auto) { this->update(); }, this->connections_); - connections_.push_back( - getApp()->windows->repaintGifs.connect([&] { this->queueUpdate(); })); + connections_.push_back(getApp()->windows->gifRepaintRequested.connect( + [&] { this->queueUpdate(); })); connections_.push_back( - getApp()->windows->layout.connect([&](Channel *channel) { - if (channel == nullptr || this->channel_.get() == channel) + getApp()->windows->layoutRequested.connect([&](Channel *channel) { + if (this->isVisible() && + (channel == nullptr || this->channel_.get() == channel)) + { this->queueLayout(); + } })); connections_.push_back( @@ -402,13 +405,9 @@ void ChannelView::updateScrollbar( void ChannelView::clearMessages() { // Clear all stored messages in this chat widget - this->messages.clear(); + this->messages_.clear(); this->scrollBar_->clearHighlights(); - - // Layout chat widget messages, and force an update regardless if there are - // no messages this->queueLayout(); - this->queueUpdate(); } Scrollbar &ChannelView::getScrollBar() @@ -483,18 +482,21 @@ LimitedQueueSnapshot ChannelView::getMessagesSnapshot() { if (!this->paused() /*|| this->scrollBar_->isVisible()*/) { - this->snapshot_ = this->messages.getSnapshot(); + this->snapshot_ = this->messages_.getSnapshot(); } return this->snapshot_; } +ChannelPtr ChannelView::channel() +{ + return this->channel_; +} + void ChannelView::setChannel(ChannelPtr newChannel) { - if (this->channel_) - { - this->detachChannel(); - } + /// Clear connections from the last channel + this->channelConnections_.clear(); this->clearMessages(); @@ -539,7 +541,7 @@ void ChannelView::setChannel(ChannelPtr newChannel) this->lastMessageHasAlternateBackground_ = !this->lastMessageHasAlternateBackground_; - this->messages.pushBack(MessageLayoutPtr(messageRef), deleted); + this->messages_.pushBack(MessageLayoutPtr(messageRef), deleted); } this->channel_ = newChannel; @@ -580,7 +582,7 @@ void ChannelView::messageAppended(MessagePtr &message, this->lastMessageHasAlternateBackground_ = !this->lastMessageHasAlternateBackground_; - if (this->messages.pushBack(MessageLayoutPtr(messageRef), deleted)) + if (this->messages_.pushBack(MessageLayoutPtr(messageRef), deleted)) { // if (!this->isPaused()) { if (this->scrollBar_->isAtBottom()) @@ -635,7 +637,7 @@ void ChannelView::messageAddedAtStart(std::vector &messages) } /// Add the messages at the start - if (this->messages.pushFront(messageRefs).size() > 0) + if (this->messages_.pushFront(messageRefs).size() > 0) { if (this->scrollBar_->isAtBottom()) this->scrollBar_->scrollToBottom(); @@ -669,13 +671,13 @@ void ChannelView::messageRemoveFromStart(MessagePtr &message) void ChannelView::messageReplaced(size_t index, MessagePtr &replacement) { - if (index >= this->messages.getSnapshot().size() || index < 0) + if (index >= this->messages_.getSnapshot().size() || index < 0) { return; } MessageLayoutPtr newItem(new MessageLayout(replacement)); - auto snapshot = this->messages.getSnapshot(); + auto snapshot = this->messages_.getSnapshot(); if (index >= snapshot.size()) { log("Tried to replace out of bounds message. Index: {}. " @@ -693,15 +695,10 @@ void ChannelView::messageReplaced(size_t index, MessagePtr &replacement) this->scrollBar_->replaceHighlight(index, replacement->getScrollBarHighlight()); - this->messages.replaceItem(message, newItem); + this->messages_.replaceItem(message, newItem); this->queueLayout(); } -void ChannelView::detachChannel() -{ - this->channelConnections_.clear(); -} - void ChannelView::updateLastReadMessage() { auto _snapshot = this->getMessagesSnapshot(); @@ -1048,18 +1045,18 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) SelectionItem newEnd(messageIndex, wordEnd); // Selection changed in same message - if (messageIndex == this->dCSelection_.origMessageIndex) + if (messageIndex == this->doubleClickSelection_.origMessageIndex) { // Selecting to the left if (wordStart < this->selection_.start.charIndex && - !this->dCSelection_.selectingRight) + !this->doubleClickSelection_.selectingRight) { - this->dCSelection_.selectingLeft = true; + this->doubleClickSelection_.selectingLeft = true; // Ensure that the original word stays selected(Edge case) - if (wordStart > this->dCSelection_.originalEnd) + if (wordStart > this->doubleClickSelection_.originalEnd) { - this->setSelection(this->dCSelection_.origStartItem, - newEnd); + this->setSelection( + this->doubleClickSelection_.origStartItem, newEnd); } else { @@ -1068,14 +1065,14 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) // Selecting to the right } else if (wordEnd > this->selection_.end.charIndex && - !this->dCSelection_.selectingLeft) + !this->doubleClickSelection_.selectingLeft) { - this->dCSelection_.selectingRight = true; + this->doubleClickSelection_.selectingRight = true; // Ensure that the original word stays selected(Edge case) - if (wordEnd < this->dCSelection_.originalStart) + if (wordEnd < this->doubleClickSelection_.originalStart) { this->setSelection(newStart, - this->dCSelection_.origEndItem); + this->doubleClickSelection_.origEndItem); } else { @@ -1084,14 +1081,14 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) } // Swapping from selecting left to selecting right if (wordStart > this->selection_.start.charIndex && - !this->dCSelection_.selectingRight) + !this->doubleClickSelection_.selectingRight) { - if (wordStart > this->dCSelection_.originalEnd) + if (wordStart > this->doubleClickSelection_.originalEnd) { - this->dCSelection_.selectingLeft = false; - this->dCSelection_.selectingRight = true; - this->setSelection(this->dCSelection_.origStartItem, - newEnd); + this->doubleClickSelection_.selectingLeft = false; + this->doubleClickSelection_.selectingRight = true; + this->setSelection( + this->doubleClickSelection_.origStartItem, newEnd); } else { @@ -1100,14 +1097,14 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) // Swapping from selecting right to selecting left } else if (wordEnd < this->selection_.end.charIndex && - !this->dCSelection_.selectingLeft) + !this->doubleClickSelection_.selectingLeft) { - if (wordEnd < this->dCSelection_.originalStart) + if (wordEnd < this->doubleClickSelection_.originalStart) { - this->dCSelection_.selectingLeft = true; - this->dCSelection_.selectingRight = false; + this->doubleClickSelection_.selectingLeft = true; + this->doubleClickSelection_.selectingRight = false; this->setSelection(newStart, - this->dCSelection_.origEndItem); + this->doubleClickSelection_.origEndItem); } else { @@ -1122,38 +1119,40 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) if (messageIndex < this->selection_.start.messageIndex) { // Swapping from left to right selecting - if (!this->dCSelection_.selectingLeft) + if (!this->doubleClickSelection_.selectingLeft) { - this->dCSelection_.selectingLeft = true; - this->dCSelection_.selectingRight = false; + this->doubleClickSelection_.selectingLeft = true; + this->doubleClickSelection_.selectingRight = false; } if (wordStart < this->selection_.start.charIndex && - !this->dCSelection_.selectingRight) + !this->doubleClickSelection_.selectingRight) { - this->dCSelection_.selectingLeft = true; + this->doubleClickSelection_.selectingLeft = true; } - this->setSelection(newStart, this->dCSelection_.origEndItem); + this->setSelection(newStart, + this->doubleClickSelection_.origEndItem); // Message under the original } else if (messageIndex > this->selection_.end.messageIndex) { // Swapping from right to left selecting - if (!this->dCSelection_.selectingRight) + if (!this->doubleClickSelection_.selectingRight) { - this->dCSelection_.selectingLeft = false; - this->dCSelection_.selectingRight = true; + this->doubleClickSelection_.selectingLeft = false; + this->doubleClickSelection_.selectingRight = true; } if (wordEnd > this->selection_.end.charIndex && - !this->dCSelection_.selectingLeft) + !this->doubleClickSelection_.selectingLeft) { - this->dCSelection_.selectingRight = true; + this->doubleClickSelection_.selectingRight = true; } - this->setSelection(this->dCSelection_.origStartItem, newEnd); + this->setSelection(this->doubleClickSelection_.origStartItem, + newEnd); // Selection changed in non original message } else { - if (this->dCSelection_.selectingLeft) + if (this->doubleClickSelection_.selectingLeft) { this->setSelection(newStart, this->selection_.end); } @@ -1164,11 +1163,11 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) } } // Reset direction of selection - if (wordStart == this->dCSelection_.originalStart && - wordEnd == this->dCSelection_.originalEnd) + if (wordStart == this->doubleClickSelection_.originalStart && + wordEnd == this->doubleClickSelection_.originalEnd) { - this->dCSelection_.selectingLeft = - this->dCSelection_.selectingRight = false; + this->doubleClickSelection_.selectingLeft = + this->doubleClickSelection_.selectingRight = false; } } @@ -1274,8 +1273,8 @@ void ChannelView::mouseReleaseEvent(QMouseEvent *event) // check if mouse was pressed if (event->button() == Qt::LeftButton) { - this->dCSelection_.selectingLeft = this->dCSelection_.selectingRight = - false; + this->doubleClickSelection_.selectingLeft = + this->doubleClickSelection_.selectingRight = false; if (this->isDoubleClick_) { this->isDoubleClick_ = false; @@ -1560,6 +1559,7 @@ void ChannelView::mouseDoubleClickEvent(QMouseEvent *event) this->clickTimer_->start(); return; } + if (!this->isMouseDown_) { this->isDoubleClick_ = true; @@ -1574,11 +1574,11 @@ void ChannelView::mouseDoubleClickEvent(QMouseEvent *event) SelectionItem wordMin(messageIndex, wordStart); SelectionItem wordMax(messageIndex, wordEnd); - this->dCSelection_.originalStart = wordStart; - this->dCSelection_.originalEnd = wordEnd; - this->dCSelection_.origMessageIndex = messageIndex; - this->dCSelection_.origStartItem = wordMin; - this->dCSelection_.origEndItem = wordMax; + this->doubleClickSelection_.originalStart = wordStart; + this->doubleClickSelection_.originalEnd = wordEnd; + this->doubleClickSelection_.origMessageIndex = messageIndex; + this->doubleClickSelection_.origStartItem = wordMin; + this->doubleClickSelection_.origEndItem = wordMax; this->setSelection(wordMin, wordMax); } diff --git a/src/widgets/helper/ChannelView.hpp b/src/widgets/helper/ChannelView.hpp index 34b554336..48c253194 100644 --- a/src/widgets/helper/ChannelView.hpp +++ b/src/widgets/helper/ChannelView.hpp @@ -65,11 +65,14 @@ public: const boost::optional &getOverrideFlags() const; void updateLastReadMessage(); + /// Pausing bool paused() const; void pause(PauseReason reason, boost::optional msecs = boost::none); void unpause(PauseReason reason); + ChannelPtr channel(); void setChannel(ChannelPtr channel_); + LimitedQueueSnapshot getMessagesSnapshot(); void queueLayout(); @@ -118,8 +121,6 @@ private: void messageRemoveFromStart(MessagePtr &message); void messageReplaced(size_t index, MessagePtr &replacement); - void detachChannel(); - void performLayout(bool causedByScollbar = false); void layoutVisibleMessages( LimitedQueueSnapshot &messages); @@ -177,7 +178,7 @@ private: bool isMouseDown_ = false; bool isRightMouseDown_ = false; bool isDoubleClick_ = false; - DoubleClickSelection dCSelection_; + DoubleClickSelection doubleClickSelection_; QPointF lastPressPosition_; QPointF lastRightPressPosition_; QPointF lastDClickPosition_; @@ -186,7 +187,7 @@ private: Selection selection_; bool selecting_ = false; - LimitedQueue messages; + LimitedQueue messages_; pajlada::Signals::Connection messageAppendedConnection_; pajlada::Signals::Connection messageAddedAtStartConnection_; diff --git a/src/widgets/splits/Split.cpp b/src/widgets/splits/Split.cpp index add9f3745..fb8a3c66c 100644 --- a/src/widgets/splits/Split.cpp +++ b/src/widgets/splits/Split.cpp @@ -1,8 +1,8 @@ #include "widgets/splits/Split.hpp" -#include "controllers/accounts/AccountController.hpp" #include "common/Common.hpp" #include "common/NetworkRequest.hpp" +#include "controllers/accounts/AccountController.hpp" #include "debug/Log.hpp" #include "providers/twitch/EmoteValue.hpp" #include "providers/twitch/TwitchChannel.hpp" @@ -106,7 +106,7 @@ Split::Split(QWidget *parent) // CTRL+F: Search createShortcut(this, "CTRL+F", &Split::showSearch); - // F5: reload emotes + // F5: reload emotes createShortcut(this, "F5", &Split::reloadChannelAndSubscriberEmotes); // F12 @@ -320,11 +320,6 @@ void Split::showChangeChannelPopup(const char *dialogTitle, bool empty, this->selectChannelDialog_ = dialog; } -void Split::layoutMessages() -{ - this->view_->queueLayout(); -} - void Split::updateGifEmotes() { this->view_->queueUpdate(); @@ -659,7 +654,6 @@ void Split::reloadChannelAndSubscriberEmotes() twitchChannel->refreshChannelEmotes(); } - template static Iter select_randomly(Iter start, Iter end, RandomGenerator &g) { diff --git a/src/widgets/splits/Split.hpp b/src/widgets/splits/Split.hpp index a41be31e1..95db30155 100644 --- a/src/widgets/splits/Split.hpp +++ b/src/widgets/splits/Split.hpp @@ -62,7 +62,6 @@ public: std::function callback); void giveFocus(Qt::FocusReason reason); bool hasFocus() const; - void layoutMessages(); void updateGifEmotes(); void updateLastReadMessage(); void setIsTopRightSplit(bool value);