diff --git a/src/channel.hpp b/src/channel.hpp index 5adacd148..d07aaef2e 100644 --- a/src/channel.hpp +++ b/src/channel.hpp @@ -20,8 +20,6 @@ class Message; class ChannelManager; -typedef std::shared_ptr SharedChannel; - class Channel { public: diff --git a/src/channelmanager.cpp b/src/channelmanager.cpp index be9c12ad9..35c0d08db 100644 --- a/src/channelmanager.cpp +++ b/src/channelmanager.cpp @@ -14,26 +14,26 @@ ChannelManager::ChannelManager() { } -SharedChannel ChannelManager::getWhispers() +std::shared_ptr ChannelManager::getWhispers() { return _whispers; } -SharedChannel ChannelManager::getMentions() +std::shared_ptr ChannelManager::getMentions() { return _mentions; } -SharedChannel ChannelManager::getEmpty() +std::shared_ptr ChannelManager::getEmpty() { return _empty; } -const std::vector ChannelManager::getItems() +const std::vector> ChannelManager::getItems() { QMutexLocker locker(&_channelsMutex); - std::vector items; + std::vector> items; for (auto &item : _channels.values()) { items.push_back(std::get<0>(item)); @@ -42,7 +42,7 @@ const std::vector ChannelManager::getItems() return items; } -SharedChannel ChannelManager::addChannel(const QString &channel) +std::shared_ptr ChannelManager::addChannel(const QString &channel) { QMutexLocker locker(&_channelsMutex); @@ -55,7 +55,7 @@ SharedChannel ChannelManager::addChannel(const QString &channel) auto it = _channels.find(channelName); if (it == _channels.end()) { - auto channel = SharedChannel(new Channel(channelName)); + auto channel = std::shared_ptr(new Channel(channelName)); _channels.insert(channelName, std::make_tuple(channel, 1)); IrcManager::getInstance().joinChannel(channelName); diff --git a/src/channelmanager.hpp b/src/channelmanager.hpp index 462a02551..289a2b987 100644 --- a/src/channelmanager.hpp +++ b/src/channelmanager.hpp @@ -12,14 +12,14 @@ public: return instance; } - SharedChannel getWhispers(); - SharedChannel getMentions(); - SharedChannel getEmpty(); + std::shared_ptr getWhispers(); + std::shared_ptr getMentions(); + std::shared_ptr getEmpty(); - const std::vector getItems(); + const std::vector> getItems(); - SharedChannel addChannel(const QString &channel); - SharedChannel getChannel(const QString &channel); + std::shared_ptr addChannel(const QString &channel); + std::shared_ptr getChannel(const QString &channel); void removeChannel(const QString &channel); private: @@ -27,12 +27,12 @@ private: ChannelManager(); - QMap> _channels; + QMap, int>> _channels; QMutex _channelsMutex; - SharedChannel _whispers; - SharedChannel _mentions; - SharedChannel _empty; + std::shared_ptr _whispers; + std::shared_ptr _mentions; + std::shared_ptr _empty; }; } // namespace chatterino diff --git a/src/widgets/accountpopup.cpp b/src/widgets/accountpopup.cpp index 5dfffb28f..3c12f536b 100644 --- a/src/widgets/accountpopup.cpp +++ b/src/widgets/accountpopup.cpp @@ -7,7 +7,7 @@ namespace chatterino { namespace widgets { -AccountPopupWidget::AccountPopupWidget(SharedChannel &channel) +AccountPopupWidget::AccountPopupWidget(std::shared_ptr &channel) : QWidget(nullptr) , _ui(new Ui::AccountPopup) , _channel(channel) diff --git a/src/widgets/chatwidget.cpp b/src/widgets/chatwidget.cpp index d7f6b8f76..be977db79 100644 --- a/src/widgets/chatwidget.cpp +++ b/src/widgets/chatwidget.cpp @@ -18,153 +18,152 @@ using namespace chatterino::messages; namespace chatterino { namespace widgets { +namespace { + +template +inline void ezShortcut(ChatWidget *w, const char *key, T t) +{ + auto s = new QShortcut(QKeySequence(key), w); + s->setContext(Qt::WidgetWithChildrenShortcut); + QObject::connect(s, &QShortcut::activated, w, t); +} + +} // namespace + ChatWidget::ChatWidget(QWidget *parent) : QWidget(parent) - , _messages() - , _channel(ChannelManager::getInstance().getEmpty()) - , _channelName(QString()) - , _vbox(this) - , _header(this) - , _view(this) - , _input(this) + , channel(ChannelManager::getInstance().getEmpty()) + , vbox(this) + , header(this) + , view(this) + , input(this) { - this->_vbox.setSpacing(0); - this->_vbox.setMargin(1); + this->vbox.setSpacing(0); + this->vbox.setMargin(1); - this->_vbox.addWidget(&_header); - this->_vbox.addWidget(&_view, 1); - this->_vbox.addWidget(&_input); + this->vbox.addWidget(&this->header); + this->vbox.addWidget(&this->view, 1); + this->vbox.addWidget(&this->input); - // Initialize widget-wide hotkeys + // Initialize chat widget-wide hotkeys // CTRL+T: Create new split (Add page) - { - auto s = new QShortcut(QKeySequence("CTRL+T"), this); - s->setContext(Qt::WidgetWithChildrenShortcut); - connect(s, &QShortcut::activated, this, &ChatWidget::doAddSplit); - } + ezShortcut(this, "CTRL+T", &ChatWidget::doAddSplit); // CTRL+W: Close Split - { - auto s = new QShortcut(QKeySequence("CTRL+W"), this); - s->setContext(Qt::WidgetWithChildrenShortcut); - connect(s, &QShortcut::activated, this, &ChatWidget::doCloseSplit); - } + ezShortcut(this, "CTRL+W", &ChatWidget::doCloseSplit); // CTRL+R: Change Channel - { - auto s = new QShortcut(QKeySequence("CTRL+R"), this); - s->setContext(Qt::WidgetWithChildrenShortcut); - connect(s, &QShortcut::activated, this, &ChatWidget::doChangeChannel); - } + ezShortcut(this, "CTRL+R", &ChatWidget::doChangeChannel); } ChatWidget::~ChatWidget() { - detachChannel(); + this->detachChannel(); } std::shared_ptr ChatWidget::getChannel() const { - return _channel; + return this->channel; } std::shared_ptr &ChatWidget::getChannelRef() { - return _channel; + return this->channel; } const QString &ChatWidget::getChannelName() const { - return _channelName; + return this->channelName; } -void ChatWidget::setChannelName(const QString &name) +void ChatWidget::setChannelName(const QString &_newChannelName) { - QString channelName = name.trimmed(); + QString newChannelName = _newChannelName.trimmed(); // return if channel name is the same - if (QString::compare(channelName, _channelName, Qt::CaseInsensitive) == 0) { - _channelName = channelName; - _header.updateChannelText(); + if (QString::compare(newChannelName, this->channelName, Qt::CaseInsensitive) == 0) { + this->channelName = newChannelName; + this->header.updateChannelText(); return; } // remove current channel - if (!_channelName.isEmpty()) { - ChannelManager::getInstance().removeChannel(_channelName); + if (!this->channelName.isEmpty()) { + ChannelManager::getInstance().removeChannel(this->channelName); - detachChannel(); + this->detachChannel(); } // update members - _channelName = channelName; + this->channelName = newChannelName; // update messages - _messages.clear(); + this->messages.clear(); - printf("Set channel name xD %s\n", qPrintable(name)); + printf("Set channel name xD %s\n", qPrintable(_newChannelName)); - if (channelName.isEmpty()) { - _channel = nullptr; + if (newChannelName.isEmpty()) { + this->channel = nullptr; } else { - _channel = ChannelManager::getInstance().addChannel(channelName); - printf("Created channel FeelsGoodMan %p\n", _channel.get()); - - attachChannel(_channel); + this->setChannel(ChannelManager::getInstance().addChannel(newChannelName)); } // update header - _header.updateChannelText(); + this->header.updateChannelText(); // update view - _view.layoutMessages(); - _view.update(); + this->view.layoutMessages(); + this->view.update(); } -void ChatWidget::attachChannel(SharedChannel channel) +void ChatWidget::setChannel(std::shared_ptr _newChannel) { + this->channel = _newChannel; + // on new message - _messageAppendedConnection = channel->messageAppended.connect([this](SharedMessage &message) { - SharedMessageRef deleted; + this->messageAppendedConnection = + this->channel->messageAppended.connect([this](SharedMessage &message) { + SharedMessageRef deleted; - auto messageRef = new MessageRef(message); + auto messageRef = new MessageRef(message); - if (_messages.appendItem(SharedMessageRef(messageRef), deleted)) { - qreal value = std::max(0.0, _view.getScrollbar()->getDesiredValue() - 1); + if (this->messages.appendItem(SharedMessageRef(messageRef), deleted)) { + qreal value = std::max(0.0, this->view.getScrollBar().getDesiredValue() - 1); - _view.getScrollbar()->setDesiredValue(value, false); - } - }); + this->view.getScrollBar().setDesiredValue(value, false); + } + }); // on message removed - _messageRemovedConnection = _channel->messageRemovedFromStart.connect([](SharedMessage &) { - // - }); + this->messageRemovedConnection = + this->channel->messageRemovedFromStart.connect([](SharedMessage &) { + // + }); - auto snapshot = _channel.get()->getMessageSnapshot(); + auto snapshot = this->channel->getMessageSnapshot(); for (int i = 0; i < snapshot.getSize(); i++) { SharedMessageRef deleted; auto messageRef = new MessageRef(snapshot[i]); - _messages.appendItem(SharedMessageRef(messageRef), deleted); + this->messages.appendItem(SharedMessageRef(messageRef), deleted); } } void ChatWidget::detachChannel() { // on message added - _messageAppendedConnection.disconnect(); + this->messageAppendedConnection.disconnect(); // on message removed - _messageRemovedConnection.disconnect(); + this->messageRemovedConnection.disconnect(); } LimitedQueueSnapshot ChatWidget::getMessagesSnapshot() { - return _messages.getSnapshot(); + return this->messages.getSnapshot(); } void ChatWidget::showChangeChannelPopup() @@ -172,7 +171,7 @@ void ChatWidget::showChangeChannelPopup() // create new input dialog and execute it TextInputDialog dialog(this); - dialog.setText(_channelName); + dialog.setText(this->channelName); if (dialog.exec() == QDialog::Accepted) { setChannelName(dialog.getText()); @@ -181,14 +180,14 @@ void ChatWidget::showChangeChannelPopup() void ChatWidget::layoutMessages() { - if (_view.layoutMessages()) { - _view.update(); + if (this->view.layoutMessages()) { + this->view.update(); } } void ChatWidget::updateGifEmotes() { - _view.updateGifEmotes(); + this->view.updateGifEmotes(); } void ChatWidget::paintEvent(QPaintEvent *) @@ -227,7 +226,7 @@ void ChatWidget::doAddSplit() void ChatWidget::doCloseSplit() { - qDebug() << "Close split for" << this->getChannelName(); + qDebug() << "[UNIMPLEMENTED] Close split for" << this->getChannelName(); } void ChatWidget::doChangeChannel() @@ -245,17 +244,17 @@ void ChatWidget::doPopup() void ChatWidget::doClearChat() { - qDebug() << "[UNIMPLEMENTED]: Clear chat"; + qDebug() << "[UNIMPLEMENTED] Clear chat"; } void ChatWidget::doOpenChannel() { - qDebug() << "[UNIMPLEMENTED]: Open twitch.tv/" << this->getChannelName(); + qDebug() << "[UNIMPLEMENTED] Open twitch.tv/" << this->getChannelName(); } void ChatWidget::doOpenPopupPlayer() { - qDebug() << "[UNIMPLEMENTED]: Open twitch.tv/" << this->getChannelName() << "/popout"; + qDebug() << "[UNIMPLEMENTED] Open twitch.tv/" << this->getChannelName() << "/popout"; } } // namespace widgets diff --git a/src/widgets/chatwidget.hpp b/src/widgets/chatwidget.hpp index 9d4a0b349..b25c63c99 100644 --- a/src/widgets/chatwidget.hpp +++ b/src/widgets/chatwidget.hpp @@ -37,8 +37,8 @@ public: ChatWidget(QWidget *parent = nullptr); ~ChatWidget(); - SharedChannel getChannel() const; - SharedChannel &getChannelRef(); + std::shared_ptr getChannel() const; + std::shared_ptr &getChannelRef(); const QString &getChannelName() const; void setChannelName(const QString &name); @@ -51,22 +51,21 @@ protected: void paintEvent(QPaintEvent *) override; private: - void attachChannel(std::shared_ptr _channel); + void setChannel(std::shared_ptr newChannel); void detachChannel(); - messages::LimitedQueue _messages; + messages::LimitedQueue messages; - SharedChannel _channel; - QString _channelName; + std::shared_ptr channel; + QString channelName; - QFont _font; - QVBoxLayout _vbox; - ChatWidgetHeader _header; - ChatWidgetView _view; - ChatWidgetInput _input; + QVBoxLayout vbox; + ChatWidgetHeader header; + ChatWidgetView view; + ChatWidgetInput input; - boost::signals2::connection _messageAppendedConnection; - boost::signals2::connection _messageRemovedConnection; + boost::signals2::connection messageAppendedConnection; + boost::signals2::connection messageRemovedConnection; public: void load(const boost::property_tree::ptree &tree); diff --git a/src/widgets/chatwidgetinput.cpp b/src/widgets/chatwidgetinput.cpp index 1a13c463e..4e0cc734b 100644 --- a/src/widgets/chatwidgetinput.cpp +++ b/src/widgets/chatwidgetinput.cpp @@ -11,44 +11,38 @@ namespace chatterino { namespace widgets { -ChatWidgetInput::ChatWidgetInput(ChatWidget *widget) - : _chatWidget(widget) - , _hbox() - , _vbox() - , _editContainer() - , _edit() - , _textLengthLabel() - , _emotesLabel(0) +ChatWidgetInput::ChatWidgetInput(ChatWidget *_chatWidget) + : QWidget(_chatWidget) + , chatWidget(_chatWidget) { - setLayout(&_hbox); - setMaximumHeight(150); - _hbox.setMargin(4); + this->setMaximumHeight(150); - _hbox.addLayout(&_editContainer); - _hbox.addLayout(&_vbox); + this->setLayout(&this->hbox); - _editContainer.addWidget(&_edit); - _editContainer.setMargin(4); + this->hbox.setMargin(4); - _vbox.addWidget(&_textLengthLabel); - _vbox.addStretch(1); - _vbox.addWidget(&_emotesLabel); + this->hbox.addLayout(&this->editContainer); + this->hbox.addLayout(&this->vbox); - _textLengthLabel.setText("100"); - _textLengthLabel.setAlignment(Qt::AlignRight); - _emotesLabel.getLabel().setTextFormat(Qt::RichText); - _emotesLabel.getLabel().setText( + this->editContainer.addWidget(&this->textInput); + this->editContainer.setMargin(4); + + this->vbox.addWidget(&this->textLengthLabel); + this->vbox.addStretch(1); + this->vbox.addWidget(&this->emotesLabel); + + this->textLengthLabel.setText("100"); + this->textLengthLabel.setAlignment(Qt::AlignRight); + + this->emotesLabel.getLabel().setTextFormat(Qt::RichText); + this->emotesLabel.getLabel().setText( ""); - QObject::connect(&_edit, &ResizingTextEdit::textChanged, this, - &ChatWidgetInput::editTextChanged); + connect(&textInput, &ResizingTextEdit::textChanged, this, &ChatWidgetInput::editTextChanged); - // QObject::connect(&edit, &ResizingTextEdit::keyPressEvent, this, - // &ChatWidgetInput::editKeyPressed); - - refreshTheme(); - setMessageLengthVisisble(SettingsManager::getInstance().showMessageLength.get()); + this->refreshTheme(); + this->setMessageLengthVisisble(SettingsManager::getInstance().showMessageLength.get()); QStringList list; list.append("asd"); @@ -56,20 +50,20 @@ ChatWidgetInput::ChatWidgetInput(ChatWidget *widget) list.append("asdg"); list.append("asdh"); - QCompleter *completer = new QCompleter(list, &_edit); + QCompleter *completer = new QCompleter(list, &this->textInput); - completer->setWidget(&_edit); + completer->setWidget(&textInput); - _edit.keyPressed.connect([this /*, completer*/](QKeyEvent *event) { + this->textInput.keyPressed.connect([this /*, completer*/](QKeyEvent *event) { if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { - auto c = _chatWidget->getChannel(); + auto c = this->chatWidget->getChannel(); if (c == nullptr) { return; } - c->sendMessage(_edit.toPlainText()); + c->sendMessage(textInput.toPlainText()); event->accept(); - _edit.setText(QString()); + textInput.setText(QString()); } // else { // completer->setCompletionPrefix("asdf"); @@ -101,9 +95,9 @@ void ChatWidgetInput::refreshTheme() palette.setColor(QPalette::Foreground, ColorScheme::getInstance().Text); - _textLengthLabel.setPalette(palette); + this->textLengthLabel.setPalette(palette); - _edit.setStyleSheet(ColorScheme::getInstance().InputStyleSheet); + this->textInput.setStyleSheet(ColorScheme::getInstance().InputStyleSheet); } void ChatWidgetInput::editTextChanged() @@ -124,17 +118,17 @@ void ChatWidgetInput::paintEvent(QPaintEvent *) { QPainter painter(this); - painter.fillRect(rect(), ColorScheme::getInstance().ChatInputBackground); + painter.fillRect(this->rect(), ColorScheme::getInstance().ChatInputBackground); painter.setPen(ColorScheme::getInstance().ChatInputBorder); - painter.drawRect(0, 0, width() - 1, height() - 1); + painter.drawRect(0, 0, this->width() - 1, this->height() - 1); } void ChatWidgetInput::resizeEvent(QResizeEvent *) { - if (height() == maximumHeight()) { - _edit.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); + if (this->height() == this->maximumHeight()) { + this->textInput.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); } else { - _edit.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + this->textInput.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } } diff --git a/src/widgets/chatwidgetinput.hpp b/src/widgets/chatwidgetinput.hpp index beb208dee..8a02591ea 100644 --- a/src/widgets/chatwidgetinput.hpp +++ b/src/widgets/chatwidgetinput.hpp @@ -21,29 +21,28 @@ class ChatWidgetInput : public QWidget Q_OBJECT public: - ChatWidgetInput(ChatWidget *parent); + ChatWidgetInput(ChatWidget *_chatWidget); ~ChatWidgetInput(); protected: - void paintEvent(QPaintEvent *); - - void resizeEvent(QResizeEvent *); + virtual void paintEvent(QPaintEvent *) override; + virtual void resizeEvent(QResizeEvent *) override; private: - ChatWidget *_chatWidget; + ChatWidget *chatWidget; - QHBoxLayout _hbox; - QVBoxLayout _vbox; - QHBoxLayout _editContainer; - ResizingTextEdit _edit; - QLabel _textLengthLabel; - ChatWidgetHeaderButton _emotesLabel; + QHBoxLayout hbox; + QVBoxLayout vbox; + QHBoxLayout editContainer; + ResizingTextEdit textInput; + QLabel textLengthLabel; + ChatWidgetHeaderButton emotesLabel; private slots: void refreshTheme(); void setMessageLengthVisisble(bool value) { - _textLengthLabel.setHidden(!value); + textLengthLabel.setHidden(!value); } void editTextChanged(); // void editKeyPressed(QKeyEvent *event); diff --git a/src/widgets/chatwidgetview.cpp b/src/widgets/chatwidgetview.cpp index 8ec13db8d..9fa8bfcd3 100644 --- a/src/widgets/chatwidgetview.cpp +++ b/src/widgets/chatwidgetview.cpp @@ -19,23 +19,19 @@ namespace chatterino { namespace widgets { -ChatWidgetView::ChatWidgetView(ChatWidget *parent) - : QWidget(parent) - , _chatWidget(parent) - , _scrollbar(this) - , _userPopupWidget(_chatWidget->getChannelRef()) - , _onlyUpdateEmotes(false) - , _mouseDown(false) - , _lastPressPosition() +ChatWidgetView::ChatWidgetView(ChatWidget *_chatWidget) + : QWidget(_chatWidget) + , chatWidget(_chatWidget) + , scrollBar(this) + , userPopupWidget(_chatWidget->getChannelRef()) { - setAttribute(Qt::WA_OpaquePaintEvent); - _scrollbar.setSmallChange(5); - setMouseTracking(true); + this->setAttribute(Qt::WA_OpaquePaintEvent); + this->setMouseTracking(true); QObject::connect(&SettingsManager::getInstance(), &SettingsManager::wordTypeMaskChanged, this, &ChatWidgetView::wordTypeMaskChanged); - _scrollbar.getCurrentValueChanged().connect([this] { + this->scrollBar.getCurrentValueChanged().connect([this] { // Whenever the scrollbar value has been changed, re-render the ChatWidgetView this->update(); }); @@ -49,28 +45,28 @@ ChatWidgetView::~ChatWidgetView() bool ChatWidgetView::layoutMessages() { - auto messages = _chatWidget->getMessagesSnapshot(); + auto messages = this->chatWidget->getMessagesSnapshot(); if (messages.getSize() == 0) { - _scrollbar.setVisible(false); - + this->scrollBar.setVisible(false); return false; } - bool showScrollbar = false, redraw = false; + bool showScrollbar = false; + bool redraw = false; // Bool indicating whether or not we were showing all messages // True if one of the following statements are true: // The scrollbar was not visible // The scrollbar was visible and at the bottom - this->showingLatestMessages = this->_scrollbar.isAtBottom() || !this->_scrollbar.isVisible(); + this->showingLatestMessages = this->scrollBar.isAtBottom() || !this->scrollBar.isVisible(); - int start = _scrollbar.getCurrentValue(); - int layoutWidth = _scrollbar.isVisible() ? width() - _scrollbar.width() : width(); + int start = this->scrollBar.getCurrentValue(); + int layoutWidth = this->scrollBar.isVisible() ? width() - this->scrollBar.width() : width(); // layout the visible messages in the view if (messages.getSize() > start) { - int y = -(messages[start]->getHeight() * (fmod(_scrollbar.getCurrentValue(), 1))); + int y = -(messages[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1))); for (int i = start; i < messages.getSize(); ++i) { auto message = messages[i]; @@ -96,21 +92,22 @@ bool ChatWidgetView::layoutMessages() h -= message->getHeight(); if (h < 0) { - _scrollbar.setLargeChange((messages.getSize() - i) + (qreal)h / message->getHeight()); - _scrollbar.setDesiredValue(_scrollbar.getDesiredValue()); + this->scrollBar.setLargeChange((messages.getSize() - i) + + (qreal)h / message->getHeight()); + this->scrollBar.setDesiredValue(this->scrollBar.getDesiredValue()); showScrollbar = true; break; } } - _scrollbar.setVisible(showScrollbar); + this->scrollBar.setVisible(showScrollbar); if (!showScrollbar) { - _scrollbar.setDesiredValue(0); + this->scrollBar.setDesiredValue(0); } - _scrollbar.setMaximum(messages.getSize()); + this->scrollBar.setMaximum(messages.getSize()); if (this->showingLatestMessages && showScrollbar) { // If we were showing the latest messages and the scrollbar now wants to be rendered, scroll @@ -118,7 +115,7 @@ bool ChatWidgetView::layoutMessages() // TODO: Do we want to check if the user is currently moving the scrollbar? // Perhaps also if the user scrolled with the scrollwheel in this ChatWidget in the last 0.2 // seconds or something - this->_scrollbar.scrollToBottom(); + this->scrollBar.scrollToBottom(); } return redraw; @@ -126,26 +123,26 @@ bool ChatWidgetView::layoutMessages() void ChatWidgetView::updateGifEmotes() { - _onlyUpdateEmotes = true; + this->onlyUpdateEmotes = true; this->update(); } -ScrollBar *ChatWidgetView::getScrollbar() +ScrollBar &ChatWidgetView::getScrollBar() { - return &_scrollbar; + return this->scrollBar; } void ChatWidgetView::resizeEvent(QResizeEvent *) { - _scrollbar.resize(_scrollbar.width(), height()); - _scrollbar.move(width() - _scrollbar.width(), 0); + this->scrollBar.resize(this->scrollBar.width(), height()); + this->scrollBar.move(width() - this->scrollBar.width(), 0); layoutMessages(); this->update(); } -void ChatWidgetView::paintEvent(QPaintEvent *event) +void ChatWidgetView::paintEvent(QPaintEvent * /*event*/) { QPainter _painter(this); @@ -154,10 +151,10 @@ void ChatWidgetView::paintEvent(QPaintEvent *event) ColorScheme &scheme = ColorScheme::getInstance(); // only update gif emotes - if (_onlyUpdateEmotes) { - _onlyUpdateEmotes = false; + if (this->onlyUpdateEmotes) { + this->onlyUpdateEmotes = false; - for (GifEmoteData &item : _gifEmotes) { + for (const GifEmoteData &item : this->gifEmotes) { _painter.fillRect(item.rect, scheme.ChatBackground); _painter.drawPixmap(item.rect, *item.image->getPixmap()); @@ -167,7 +164,7 @@ void ChatWidgetView::paintEvent(QPaintEvent *event) } // update all messages - _gifEmotes.clear(); + this->gifEmotes.clear(); _painter.fillRect(rect(), scheme.ChatBackground); @@ -204,15 +201,15 @@ void ChatWidgetView::paintEvent(QPaintEvent *event) painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/ - auto messages = _chatWidget->getMessagesSnapshot(); + auto messages = this->chatWidget->getMessagesSnapshot(); - int start = _scrollbar.getCurrentValue(); + int start = this->scrollBar.getCurrentValue(); if (start >= messages.getSize()) { return; } - int y = -(messages[start].get()->getHeight() * (fmod(_scrollbar.getCurrentValue(), 1))); + int y = -(messages[start].get()->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1))); for (int i = start; i < messages.getSize(); ++i) { messages::MessageRef *messageRef = messages[i].get(); @@ -276,7 +273,7 @@ void ChatWidgetView::paintEvent(QPaintEvent *event) data.rect = rect; - _gifEmotes.push_back(data); + this->gifEmotes.push_back(data); } } } @@ -292,7 +289,7 @@ void ChatWidgetView::paintEvent(QPaintEvent *event) } } - for (GifEmoteData &item : _gifEmotes) { + for (GifEmoteData &item : this->gifEmotes) { _painter.fillRect(item.rect, scheme.ChatBackground); _painter.drawPixmap(item.rect, *item.image->getPixmap()); @@ -301,11 +298,11 @@ void ChatWidgetView::paintEvent(QPaintEvent *event) void ChatWidgetView::wheelEvent(QWheelEvent *event) { - if (_scrollbar.isVisible()) { + if (this->scrollBar.isVisible()) { auto mouseMultiplier = SettingsManager::getInstance().mouseScrollMultiplier.get(); - _scrollbar.setDesiredValue( - _scrollbar.getDesiredValue() - event->delta() / 10.0 * mouseMultiplier, true); + this->scrollBar.setDesiredValue( + this->scrollBar.getDesiredValue() - event->delta() / 10.0 * mouseMultiplier, true); } } @@ -337,21 +334,21 @@ void ChatWidgetView::mouseMoveEvent(QMouseEvent *event) void ChatWidgetView::mousePressEvent(QMouseEvent *event) { - _mouseDown = true; - _lastPressPosition = event->screenPos(); + this->isMouseDown = true; + this->lastPressPosition = event->screenPos(); } void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event) { - if (!_mouseDown) { + if (!this->isMouseDown) { // We didn't grab the mouse press, so we shouldn't be handling the mouse // release return; } - _mouseDown = false; + this->isMouseDown = false; - float distance = util::distanceBetweenPoints(_lastPressPosition, event->screenPos()); + float distance = util::distanceBetweenPoints(this->lastPressPosition, event->screenPos()); qDebug() << "Distance: " << distance; @@ -370,7 +367,7 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event) if (!tryGetMessageAt(event->pos(), message, relativePos)) { // No message at clicked position - _userPopupWidget.hide(); + this->userPopupWidget.hide(); return; } @@ -385,10 +382,10 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event) switch (link.getType()) { case messages::Link::UserInfo: auto user = message->getMessage()->getUserName(); - _userPopupWidget.setName(user); - _userPopupWidget.move(event->screenPos().toPoint()); - _userPopupWidget.show(); - _userPopupWidget.setFocus(); + this->userPopupWidget.setName(user); + this->userPopupWidget.move(event->screenPos().toPoint()); + this->userPopupWidget.show(); + this->userPopupWidget.setFocus(); qDebug() << "Clicked " << user << "s message"; break; @@ -398,15 +395,15 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event) bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr &_message, QPoint &relativePos) { - auto messages = _chatWidget->getMessagesSnapshot(); + auto messages = this->chatWidget->getMessagesSnapshot(); - int start = _scrollbar.getCurrentValue(); + int start = this->scrollBar.getCurrentValue(); if (start >= messages.getSize()) { return false; } - int y = -(messages[start]->getHeight() * (fmod(_scrollbar.getCurrentValue(), 1))); + int y = -(messages[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1))); for (int i = start; i < messages.getSize(); ++i) { auto message = messages[i]; diff --git a/src/widgets/chatwidgetview.hpp b/src/widgets/chatwidgetview.hpp index 61cd4b907..48f1ac978 100644 --- a/src/widgets/chatwidgetview.hpp +++ b/src/widgets/chatwidgetview.hpp @@ -14,24 +14,25 @@ namespace chatterino { namespace widgets { + class ChatWidget; class ChatWidgetView : public QWidget { public: - explicit ChatWidgetView(ChatWidget *parent); + explicit ChatWidgetView(ChatWidget *_chatWidget); ~ChatWidgetView(); bool layoutMessages(); void updateGifEmotes(); - ScrollBar *getScrollbar(); + ScrollBar &getScrollBar(); protected: - void resizeEvent(QResizeEvent *) override; + virtual void resizeEvent(QResizeEvent *) override; - void paintEvent(QPaintEvent *) override; - void wheelEvent(QWheelEvent *event) override; + virtual void paintEvent(QPaintEvent *) override; + virtual void wheelEvent(QWheelEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override; @@ -46,22 +47,22 @@ private: QRect rect; }; - std::vector _gifEmotes; + std::vector gifEmotes; - ChatWidget *_chatWidget; + ChatWidget *chatWidget; - ScrollBar _scrollbar; + ScrollBar scrollBar; // This variable can be used to decide whether or not we should render the "Show latest // messages" button bool showingLatestMessages = true; - AccountPopupWidget _userPopupWidget; - bool _onlyUpdateEmotes; + AccountPopupWidget userPopupWidget; + bool onlyUpdateEmotes = false; // Mouse event variables - bool _mouseDown; - QPointF _lastPressPosition; + bool isMouseDown = false; + QPointF lastPressPosition; private slots: void wordTypeMaskChanged() diff --git a/src/widgets/scrollbar.cpp b/src/widgets/scrollbar.cpp index 0049f5be7..3601e0589 100644 --- a/src/widgets/scrollbar.cpp +++ b/src/widgets/scrollbar.cpp @@ -12,22 +12,8 @@ namespace widgets { ScrollBar::ScrollBar(QWidget *widget) : QWidget(widget) - , _mutex() , _currentValueAnimation(this, "currentValue") , _highlights(nullptr) - , _mouseOverIndex(-1) - , _mouseDownIndex(-1) - , _lastMousePosition() - , _buttonHeight(16) - , _trackHeight(100) - , _thumbRect() - , _maximum() - , _minimum() - , _largeChange() - , _smallChange() - , _desiredValue() - , _currentValue() - , _currentValueChanged() { resize(16, 100); diff --git a/src/widgets/scrollbar.hpp b/src/widgets/scrollbar.hpp index 539b86a6e..aba821ab6 100644 --- a/src/widgets/scrollbar.hpp +++ b/src/widgets/scrollbar.hpp @@ -58,21 +58,21 @@ private: void mouseReleaseEvent(QMouseEvent *event); void leaveEvent(QEvent *); - int _mouseOverIndex; - int _mouseDownIndex; + int _mouseOverIndex = -1; + int _mouseDownIndex = -1; QPoint _lastMousePosition; - int _buttonHeight; - int _trackHeight; + int _buttonHeight = 16; + int _trackHeight = 100; QRect _thumbRect; - qreal _maximum; - qreal _minimum; - qreal _largeChange; - qreal _smallChange; - qreal _desiredValue; - qreal _currentValue; + qreal _maximum = 0; + qreal _minimum = 0; + qreal _largeChange = 0; + qreal _smallChange = 5; + qreal _desiredValue = 0; + qreal _currentValue = 0; boost::signals2::signal _currentValueChanged;