small refactor

This commit is contained in:
fourtf 2018-11-14 17:26:08 +01:00
parent 0f9ef9d0aa
commit d8fcc1a3ed
10 changed files with 106 additions and 128 deletions

View file

@ -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)
{

View file

@ -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<pajlada::Signals::ScopedConnection> managedConnections_;
};

View file

@ -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<MessageElementFlags>(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()

View file

@ -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<Channel *> 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<Channel *> layoutRequested;
pajlada::Signals::NoArgSignal wordFlagsChanged;
private:
void encodeNodeRecusively(SplitContainer::Node *node, QJsonObject &obj);

View file

@ -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())

View file

@ -25,8 +25,6 @@ public:
WindowType getType();
SplitNotebook &getNotebook();
void repaintVisibleChatWidgets(Channel *channel = nullptr);
pajlada::Signals::NoArgSignal closed;
protected:

View file

@ -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<MessageLayoutPtr> 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<MessagePtr> &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);
}

View file

@ -65,11 +65,14 @@ public:
const boost::optional<MessageElementFlags> &getOverrideFlags() const;
void updateLastReadMessage();
/// Pausing
bool paused() const;
void pause(PauseReason reason, boost::optional<uint> msecs = boost::none);
void unpause(PauseReason reason);
ChannelPtr channel();
void setChannel(ChannelPtr channel_);
LimitedQueueSnapshot<MessageLayoutPtr> 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<MessageLayoutPtr> &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<MessageLayoutPtr> messages;
LimitedQueue<MessageLayoutPtr> messages_;
pajlada::Signals::Connection messageAppendedConnection_;
pajlada::Signals::Connection messageAddedAtStartConnection_;

View file

@ -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 <typename Iter, typename RandomGenerator>
static Iter select_randomly(Iter start, Iter end, RandomGenerator &g)
{

View file

@ -62,7 +62,6 @@ public:
std::function<void(bool)> callback);
void giveFocus(Qt::FocusReason reason);
bool hasFocus() const;
void layoutMessages();
void updateGifEmotes();
void updateLastReadMessage();
void setIsTopRightSplit(bool value);