From c5c2718dc0e0166367da5256febbe756395419e7 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 2 Jul 2017 13:10:06 +0200 Subject: [PATCH] Use addChat instead of re-implementing the same code (thanks hemirt) Some refactoring Updated settings library version --- lib/settings | 2 +- src/ircmanager.cpp | 40 +++---- src/ircmanager.hpp | 4 +- src/widgets/notebookpage.cpp | 215 ++++++++++++++++++----------------- src/widgets/notebookpage.hpp | 29 ++--- 5 files changed, 144 insertions(+), 146 deletions(-) diff --git a/lib/settings b/lib/settings index b0a6b529a..1282c351d 160000 --- a/lib/settings +++ b/lib/settings @@ -1 +1 @@ -Subproject commit b0a6b529a0702d69c085d76ef832639ca1c02dcd +Subproject commit 1282c351db528f9e2f166b2bb1c217c9d67a8bc8 diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 081f4b9bb..857c933d2 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -137,24 +137,24 @@ void IrcManager::refreshTwitchEmotes(const QString &username, const QString &oau "/emotes?oauth_token=" + oauthToken + "&client_id=" + oauthClient); if (true) { - util::urlFetchJSONTimeout(url, - [=](QJsonObject &root) { - // nextLink = - // root.value("_links").toObject().value("next").toString(); + util::urlFetchJSONTimeout( + url, + [=](QJsonObject &root) { + // nextLink = + // root.value("_links").toObject().value("next").toString(); - auto blocks = root.value("blocks").toArray(); + auto blocks = root.value("blocks").toArray(); - _twitchBlockedUsersMutex.lock(); - for (QJsonValue block : blocks) { - QJsonObject user = block.toObject().value("user").toObject(); - // display_name - _twitchBlockedUsers.insert( - user.value("name").toString().toLower(), true); - } - _twitchBlockedUsersMutex.unlock(); - qDebug() << "XD"; - }, - 3000, &this->_accessManager); + _twitchBlockedUsersMutex.lock(); + for (QJsonValue block : blocks) { + QJsonObject user = block.toObject().value("user").toObject(); + // display_name + _twitchBlockedUsers.insert(user.value("name").toString().toLower(), true); + } + _twitchBlockedUsersMutex.unlock(); + qDebug() << "XD"; + }, + 3000, &this->networkAccessManager); } } @@ -341,7 +341,7 @@ bool IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessag "&client_id=" + _account.getOAuthClient()); QNetworkRequest request(url); - auto reply = _accessManager.put(request, QByteArray()); + auto reply = this->networkAccessManager.put(request, QByteArray()); reply->waitForReadyRead(10000); if (reply->error() == QNetworkReply::NoError) { @@ -373,7 +373,7 @@ bool IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMes "&client_id=" + _account.getOAuthClient()); QNetworkRequest request(url); - auto reply = _accessManager.deleteResource(request); + auto reply = this->networkAccessManager.deleteResource(request); reply->waitForReadyRead(10000); if (reply->error() == QNetworkReply::NoError) { @@ -398,8 +398,4 @@ void IrcManager::removeIgnoredUser(QString const &username) } } -QNetworkAccessManager &IrcManager::getAccessManager() -{ - return _accessManager; -} } // namespace chatterino diff --git a/src/ircmanager.hpp b/src/ircmanager.hpp index c9200a188..a1f597fa0 100644 --- a/src/ircmanager.hpp +++ b/src/ircmanager.hpp @@ -41,8 +41,6 @@ public: bool tryRemoveIgnoredUser(QString const &username, QString &errorMessage); void removeIgnoredUser(QString const &username); - QNetworkAccessManager &getAccessManager(); - void sendMessage(const QString &channelName, const QString &message); void joinChannel(const QString &channelName); @@ -71,7 +69,7 @@ private: QMap _twitchBlockedUsers; QMutex _twitchBlockedUsersMutex; - QNetworkAccessManager _accessManager; + QNetworkAccessManager networkAccessManager; // methods Communi::IrcConnection *createConnection(bool doRead); diff --git a/src/widgets/notebookpage.cpp b/src/widgets/notebookpage.cpp index f34ee1ddf..7563487e4 100644 --- a/src/widgets/notebookpage.cpp +++ b/src/widgets/notebookpage.cpp @@ -13,6 +13,8 @@ #include #include +#include + namespace chatterino { namespace widgets { @@ -24,26 +26,90 @@ NotebookPage::NotebookPage(ChannelManager &_channelManager, Notebook *parent, No : BaseWidget(parent->colorScheme, parent) , channelManager(_channelManager) , tab(_tab) - , _parentbox(this) - , _chatWidgets() - , _preview(this) + , dropPreview(this) { - tab->page = this; + this->tab->page = this; - setHidden(true); - setAcceptDrops(true); + this->setLayout(&this->ui.parentLayout); - _parentbox.addSpacing(2); - _parentbox.addLayout(&_hbox); - _parentbox.setMargin(0); + this->setHidden(true); + this->setAcceptDrops(true); - _hbox.setSpacing(1); - _hbox.setMargin(0); + this->ui.parentLayout.addSpacing(2); + this->ui.parentLayout.addLayout(&this->ui.hbox); + this->ui.parentLayout.setMargin(0); + + this->ui.hbox.setSpacing(1); + this->ui.hbox.setMargin(0); +} + +std::pair NotebookPage::removeFromLayout(ChatWidget *widget) +{ + // remove reference to chat widget from chatWidgets vector + auto it = std::find(std::begin(this->chatWidgets), std::end(this->chatWidgets), widget); + if (it != std::end(this->chatWidgets)) { + this->chatWidgets.erase(it); + } + + // remove from box and return location + for (int i = 0; i < this->ui.hbox.count(); ++i) { + auto vbox = static_cast(this->ui.hbox.itemAt(i)); + + for (int j = 0; j < vbox->count(); ++j) { + if (vbox->itemAt(j)->widget() != widget) { + continue; + } + + widget->setParent(nullptr); + + bool isLastItem = vbox->count() == 0; + + if (isLastItem) { + this->ui.hbox.removeItem(vbox); + + delete vbox; + } + + return std::pair(i, isLastItem ? -1 : j); + } + } + + return std::pair(-1, -1); +} + +void NotebookPage::addToLayout(ChatWidget *widget, + std::pair position = std::pair(-1, -1)) +{ + this->chatWidgets.push_back(widget); + widget->giveFocus(); + + // add vbox at the end + if (position.first < 0 || position.first >= this->ui.hbox.count()) { + auto vbox = new QVBoxLayout(); + vbox->addWidget(widget); + + this->ui.hbox.addLayout(vbox, 1); + return; + } + + // insert vbox + if (position.second == -1) { + auto vbox = new QVBoxLayout(); + vbox->addWidget(widget); + + this->ui.hbox.insertLayout(position.first, vbox, 1); + return; + } + + // add to existing vbox + auto vbox = static_cast(this->ui.hbox.itemAt(position.first)); + + vbox->insertWidget(std::max(0, std::min(vbox->count(), position.second)), widget); } const std::vector &NotebookPage::getChatWidgets() const { - return _chatWidgets; + return this->chatWidgets; } NotebookTab *NotebookPage::getTab() const @@ -59,79 +125,12 @@ void NotebookPage::addChat(bool openChannelNameDialog) w->showChangeChannelPopup(); } - addToLayout(w, std::pair(-1, -1)); -} - -std::pair NotebookPage::removeFromLayout(ChatWidget *widget) -{ - // remove from chatWidgets vector - for (auto it = _chatWidgets.begin(); it != _chatWidgets.end(); ++it) { - if (*it == widget) { - _chatWidgets.erase(it); - - break; - } - } - - // remove from box and return location - for (int i = 0; i < _hbox.count(); ++i) { - auto vbox = static_cast(_hbox.itemAt(i)); - - for (int j = 0; j < vbox->count(); ++j) { - if (vbox->itemAt(j)->widget() != widget) { - continue; - } - - widget->setParent(nullptr); - - bool isLastItem = vbox->count() == 0; - - if (isLastItem) { - _hbox.removeItem(vbox); - - delete vbox; - } - - return std::pair(i, isLastItem ? -1 : j); - } - } - - return std::pair(-1, -1); -} - -void NotebookPage::addToLayout(ChatWidget *widget, - std::pair position = std::pair(-1, -1)) -{ - _chatWidgets.push_back(widget); - widget->giveFocus(); - - // add vbox at the end - if (position.first < 0 || position.first >= _hbox.count()) { - auto vbox = new QVBoxLayout(); - vbox->addWidget(widget); - - _hbox.addLayout(vbox, 1); - return; - } - - // insert vbox - if (position.second == -1) { - auto vbox = new QVBoxLayout(); - vbox->addWidget(widget); - - _hbox.insertLayout(position.first, vbox, 1); - return; - } - - // add to existing vbox - auto vbox = static_cast(_hbox.itemAt(position.first)); - - vbox->insertWidget(std::max(0, std::min(vbox->count(), position.second)), widget); + this->addToLayout(w, std::pair(-1, -1)); } void NotebookPage::enterEvent(QEvent *) { - if (_hbox.count() == 0) { + if (this->ui.hbox.count() == 0) { this->setCursor(QCursor(Qt::PointingHandCursor)); } else { this->setCursor(QCursor(Qt::ArrowCursor)); @@ -144,9 +143,9 @@ void NotebookPage::leaveEvent(QEvent *) void NotebookPage::mouseReleaseEvent(QMouseEvent *event) { - if (_hbox.count() == 0 && event->button() == Qt::LeftButton) { + if (this->ui.hbox.count() == 0 && event->button() == Qt::LeftButton) { // "Add Chat" was clicked - this->addToLayout(this->createChatWidget(), std::pair(-1, -1)); + this->addChat(true); this->setCursor(QCursor(Qt::ArrowCursor)); } @@ -161,24 +160,26 @@ void NotebookPage::dragEnterEvent(QDragEnterEvent *event) return; } - _dropRegions.clear(); + this->dropRegions.clear(); - if (_hbox.count() == 0) { - _dropRegions.push_back(DropRegion(rect(), std::pair(-1, -1))); + if (this->ui.hbox.count() == 0) { + this->dropRegions.push_back(DropRegion(rect(), std::pair(-1, -1))); } else { - for (int i = 0; i < _hbox.count() + 1; ++i) { - _dropRegions.push_back(DropRegion(QRect(((i * 4 - 1) * width() / _hbox.count()) / 4, 0, - width() / _hbox.count() / 2 + 1, height() + 1), - std::pair(i, -1))); + for (int i = 0; i < this->ui.hbox.count() + 1; ++i) { + this->dropRegions.push_back( + DropRegion(QRect(((i * 4 - 1) * width() / this->ui.hbox.count()) / 4, 0, + width() / this->ui.hbox.count() / 2 + 1, height() + 1), + std::pair(i, -1))); } - for (int i = 0; i < _hbox.count(); ++i) { - auto vbox = static_cast(_hbox.itemAt(i)); + for (int i = 0; i < this->ui.hbox.count(); ++i) { + auto vbox = static_cast(this->ui.hbox.itemAt(i)); for (int j = 0; j < vbox->count() + 1; ++j) { - _dropRegions.push_back(DropRegion( - QRect(i * width() / _hbox.count(), ((j * 2 - 1) * height() / vbox->count()) / 2, - width() / _hbox.count() + 1, height() / vbox->count() + 1), + this->dropRegions.push_back(DropRegion( + QRect(i * width() / this->ui.hbox.count(), + ((j * 2 - 1) * height() / vbox->count()) / 2, + width() / this->ui.hbox.count() + 1, height() / vbox->count() + 1), std::pair(i, j))); } @@ -197,13 +198,13 @@ void NotebookPage::dragMoveEvent(QDragMoveEvent *event) void NotebookPage::setPreviewRect(QPoint mousePos) { - for (DropRegion region : _dropRegions) { + for (DropRegion region : this->dropRegions) { if (region.rect.contains(mousePos)) { - _preview.setBounds(region.rect); + this->dropPreview.setBounds(region.rect); - if (!_preview.isVisible()) { - _preview.show(); - _preview.raise(); + if (!this->dropPreview.isVisible()) { + this->dropPreview.show(); + this->dropPreview.raise(); } dropPosition = region.position; @@ -212,12 +213,12 @@ void NotebookPage::setPreviewRect(QPoint mousePos) } } - _preview.hide(); + this->dropPreview.hide(); } void NotebookPage::dragLeaveEvent(QDragLeaveEvent *event) { - _preview.hide(); + this->dropPreview.hide(); } void NotebookPage::dropEvent(QDropEvent *event) @@ -230,14 +231,14 @@ void NotebookPage::dropEvent(QDropEvent *event) addToLayout(NotebookPage::draggingSplit, dropPosition); } - _preview.hide(); + this->dropPreview.hide(); } void NotebookPage::paintEvent(QPaintEvent *) { QPainter painter(this); - if (_hbox.count() == 0) { + if (this->ui.hbox.count() == 0) { painter.fillRect(rect(), this->colorScheme.ChatBackground); painter.fillRect(0, 0, width(), 2, this->colorScheme.TabSelectedBackground); @@ -262,7 +263,7 @@ static std::pair getWidgetPositionInLayout(QLayout *layout, const Chat std::pair NotebookPage::getChatPosition(const ChatWidget *chatWidget) { - auto layout = _hbox.layout(); + auto layout = this->ui.hbox.layout(); if (layout == nullptr) { return std::make_pair(-1, -1); @@ -335,12 +336,12 @@ boost::property_tree::ptree NotebookPage::save() { boost::property_tree::ptree tree; - auto layout = _hbox.layout(); + auto layout = this->ui.hbox.layout(); saveFromLayout(layout, tree); /* - for (const auto &chat : chatWidgets) { + for (const auto &chat : this->chatWidgets) { boost::property_tree::ptree child = chat->save(); // Set child position diff --git a/src/widgets/notebookpage.hpp b/src/widgets/notebookpage.hpp index bba5ec032..6f8410a3c 100644 --- a/src/widgets/notebookpage.hpp +++ b/src/widgets/notebookpage.hpp @@ -43,16 +43,16 @@ public: static std::pair dropPosition; protected: - void paintEvent(QPaintEvent *) override; + virtual void paintEvent(QPaintEvent *) override; - void enterEvent(QEvent *) override; - void leaveEvent(QEvent *) override; - void mouseReleaseEvent(QMouseEvent *event) override; + virtual void enterEvent(QEvent *) override; + virtual void leaveEvent(QEvent *) override; + virtual void mouseReleaseEvent(QMouseEvent *event) override; - void dragEnterEvent(QDragEnterEvent *event) override; - void dragMoveEvent(QDragMoveEvent *event) override; - void dragLeaveEvent(QDragLeaveEvent *event) override; - void dropEvent(QDropEvent *event) override; + virtual void dragEnterEvent(QDragEnterEvent *event) override; + virtual void dragMoveEvent(QDragMoveEvent *event) override; + virtual void dragLeaveEvent(QDragLeaveEvent *event) override; + virtual void dropEvent(QDropEvent *event) override; private: struct DropRegion { @@ -68,13 +68,16 @@ private: NotebookTab *tab; - QVBoxLayout _parentbox; - QHBoxLayout _hbox; + struct { + QVBoxLayout parentLayout; - std::vector _chatWidgets; - std::vector _dropRegions; + QHBoxLayout hbox; + } ui; - NotebookPageDropPreview _preview; + std::vector chatWidgets; + std::vector dropRegions; + + NotebookPageDropPreview dropPreview; void setPreviewRect(QPoint mousePos);