Use addChat instead of re-implementing the same code (thanks hemirt)

Some refactoring

Updated settings library version
This commit is contained in:
Rasmus Karlsson 2017-07-02 13:10:06 +02:00
parent d2d65b9cb5
commit c5c2718dc0
5 changed files with 144 additions and 146 deletions

@ -1 +1 @@
Subproject commit b0a6b529a0702d69c085d76ef832639ca1c02dcd Subproject commit 1282c351db528f9e2f166b2bb1c217c9d67a8bc8

View file

@ -137,24 +137,24 @@ void IrcManager::refreshTwitchEmotes(const QString &username, const QString &oau
"/emotes?oauth_token=" + oauthToken + "&client_id=" + oauthClient); "/emotes?oauth_token=" + oauthToken + "&client_id=" + oauthClient);
if (true) { if (true) {
util::urlFetchJSONTimeout(url, util::urlFetchJSONTimeout(
[=](QJsonObject &root) { url,
// nextLink = [=](QJsonObject &root) {
// root.value("_links").toObject().value("next").toString(); // nextLink =
// root.value("_links").toObject().value("next").toString();
auto blocks = root.value("blocks").toArray(); auto blocks = root.value("blocks").toArray();
_twitchBlockedUsersMutex.lock(); _twitchBlockedUsersMutex.lock();
for (QJsonValue block : blocks) { for (QJsonValue block : blocks) {
QJsonObject user = block.toObject().value("user").toObject(); QJsonObject user = block.toObject().value("user").toObject();
// display_name // display_name
_twitchBlockedUsers.insert( _twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
user.value("name").toString().toLower(), true); }
} _twitchBlockedUsersMutex.unlock();
_twitchBlockedUsersMutex.unlock(); qDebug() << "XD";
qDebug() << "XD"; },
}, 3000, &this->networkAccessManager);
3000, &this->_accessManager);
} }
} }
@ -341,7 +341,7 @@ bool IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessag
"&client_id=" + _account.getOAuthClient()); "&client_id=" + _account.getOAuthClient());
QNetworkRequest request(url); QNetworkRequest request(url);
auto reply = _accessManager.put(request, QByteArray()); auto reply = this->networkAccessManager.put(request, QByteArray());
reply->waitForReadyRead(10000); reply->waitForReadyRead(10000);
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
@ -373,7 +373,7 @@ bool IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMes
"&client_id=" + _account.getOAuthClient()); "&client_id=" + _account.getOAuthClient());
QNetworkRequest request(url); QNetworkRequest request(url);
auto reply = _accessManager.deleteResource(request); auto reply = this->networkAccessManager.deleteResource(request);
reply->waitForReadyRead(10000); reply->waitForReadyRead(10000);
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
@ -398,8 +398,4 @@ void IrcManager::removeIgnoredUser(QString const &username)
} }
} }
QNetworkAccessManager &IrcManager::getAccessManager()
{
return _accessManager;
}
} // namespace chatterino } // namespace chatterino

View file

@ -41,8 +41,6 @@ public:
bool tryRemoveIgnoredUser(QString const &username, QString &errorMessage); bool tryRemoveIgnoredUser(QString const &username, QString &errorMessage);
void removeIgnoredUser(QString const &username); void removeIgnoredUser(QString const &username);
QNetworkAccessManager &getAccessManager();
void sendMessage(const QString &channelName, const QString &message); void sendMessage(const QString &channelName, const QString &message);
void joinChannel(const QString &channelName); void joinChannel(const QString &channelName);
@ -71,7 +69,7 @@ private:
QMap<QString, bool> _twitchBlockedUsers; QMap<QString, bool> _twitchBlockedUsers;
QMutex _twitchBlockedUsersMutex; QMutex _twitchBlockedUsersMutex;
QNetworkAccessManager _accessManager; QNetworkAccessManager networkAccessManager;
// methods // methods
Communi::IrcConnection *createConnection(bool doRead); Communi::IrcConnection *createConnection(bool doRead);

View file

@ -13,6 +13,8 @@
#include <QWidget> #include <QWidget>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <algorithm>
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
@ -24,26 +26,90 @@ NotebookPage::NotebookPage(ChannelManager &_channelManager, Notebook *parent, No
: BaseWidget(parent->colorScheme, parent) : BaseWidget(parent->colorScheme, parent)
, channelManager(_channelManager) , channelManager(_channelManager)
, tab(_tab) , tab(_tab)
, _parentbox(this) , dropPreview(this)
, _chatWidgets()
, _preview(this)
{ {
tab->page = this; this->tab->page = this;
setHidden(true); this->setLayout(&this->ui.parentLayout);
setAcceptDrops(true);
_parentbox.addSpacing(2); this->setHidden(true);
_parentbox.addLayout(&_hbox); this->setAcceptDrops(true);
_parentbox.setMargin(0);
_hbox.setSpacing(1); this->ui.parentLayout.addSpacing(2);
_hbox.setMargin(0); this->ui.parentLayout.addLayout(&this->ui.hbox);
this->ui.parentLayout.setMargin(0);
this->ui.hbox.setSpacing(1);
this->ui.hbox.setMargin(0);
}
std::pair<int, int> 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<QVBoxLayout *>(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<int, int>(i, isLastItem ? -1 : j);
}
}
return std::pair<int, int>(-1, -1);
}
void NotebookPage::addToLayout(ChatWidget *widget,
std::pair<int, int> position = std::pair<int, int>(-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<QVBoxLayout *>(this->ui.hbox.itemAt(position.first));
vbox->insertWidget(std::max(0, std::min(vbox->count(), position.second)), widget);
} }
const std::vector<ChatWidget *> &NotebookPage::getChatWidgets() const const std::vector<ChatWidget *> &NotebookPage::getChatWidgets() const
{ {
return _chatWidgets; return this->chatWidgets;
} }
NotebookTab *NotebookPage::getTab() const NotebookTab *NotebookPage::getTab() const
@ -59,79 +125,12 @@ void NotebookPage::addChat(bool openChannelNameDialog)
w->showChangeChannelPopup(); w->showChangeChannelPopup();
} }
addToLayout(w, std::pair<int, int>(-1, -1)); this->addToLayout(w, std::pair<int, int>(-1, -1));
}
std::pair<int, int> 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<QVBoxLayout *>(_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<int, int>(i, isLastItem ? -1 : j);
}
}
return std::pair<int, int>(-1, -1);
}
void NotebookPage::addToLayout(ChatWidget *widget,
std::pair<int, int> position = std::pair<int, int>(-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<QVBoxLayout *>(_hbox.itemAt(position.first));
vbox->insertWidget(std::max(0, std::min(vbox->count(), position.second)), widget);
} }
void NotebookPage::enterEvent(QEvent *) void NotebookPage::enterEvent(QEvent *)
{ {
if (_hbox.count() == 0) { if (this->ui.hbox.count() == 0) {
this->setCursor(QCursor(Qt::PointingHandCursor)); this->setCursor(QCursor(Qt::PointingHandCursor));
} else { } else {
this->setCursor(QCursor(Qt::ArrowCursor)); this->setCursor(QCursor(Qt::ArrowCursor));
@ -144,9 +143,9 @@ void NotebookPage::leaveEvent(QEvent *)
void NotebookPage::mouseReleaseEvent(QMouseEvent *event) 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 // "Add Chat" was clicked
this->addToLayout(this->createChatWidget(), std::pair<int, int>(-1, -1)); this->addChat(true);
this->setCursor(QCursor(Qt::ArrowCursor)); this->setCursor(QCursor(Qt::ArrowCursor));
} }
@ -161,24 +160,26 @@ void NotebookPage::dragEnterEvent(QDragEnterEvent *event)
return; return;
} }
_dropRegions.clear(); this->dropRegions.clear();
if (_hbox.count() == 0) { if (this->ui.hbox.count() == 0) {
_dropRegions.push_back(DropRegion(rect(), std::pair<int, int>(-1, -1))); this->dropRegions.push_back(DropRegion(rect(), std::pair<int, int>(-1, -1)));
} else { } else {
for (int i = 0; i < _hbox.count() + 1; ++i) { for (int i = 0; i < this->ui.hbox.count() + 1; ++i) {
_dropRegions.push_back(DropRegion(QRect(((i * 4 - 1) * width() / _hbox.count()) / 4, 0, this->dropRegions.push_back(
width() / _hbox.count() / 2 + 1, height() + 1), DropRegion(QRect(((i * 4 - 1) * width() / this->ui.hbox.count()) / 4, 0,
std::pair<int, int>(i, -1))); width() / this->ui.hbox.count() / 2 + 1, height() + 1),
std::pair<int, int>(i, -1)));
} }
for (int i = 0; i < _hbox.count(); ++i) { for (int i = 0; i < this->ui.hbox.count(); ++i) {
auto vbox = static_cast<QVBoxLayout *>(_hbox.itemAt(i)); auto vbox = static_cast<QVBoxLayout *>(this->ui.hbox.itemAt(i));
for (int j = 0; j < vbox->count() + 1; ++j) { for (int j = 0; j < vbox->count() + 1; ++j) {
_dropRegions.push_back(DropRegion( this->dropRegions.push_back(DropRegion(
QRect(i * width() / _hbox.count(), ((j * 2 - 1) * height() / vbox->count()) / 2, QRect(i * width() / this->ui.hbox.count(),
width() / _hbox.count() + 1, height() / vbox->count() + 1), ((j * 2 - 1) * height() / vbox->count()) / 2,
width() / this->ui.hbox.count() + 1, height() / vbox->count() + 1),
std::pair<int, int>(i, j))); std::pair<int, int>(i, j)));
} }
@ -197,13 +198,13 @@ void NotebookPage::dragMoveEvent(QDragMoveEvent *event)
void NotebookPage::setPreviewRect(QPoint mousePos) void NotebookPage::setPreviewRect(QPoint mousePos)
{ {
for (DropRegion region : _dropRegions) { for (DropRegion region : this->dropRegions) {
if (region.rect.contains(mousePos)) { if (region.rect.contains(mousePos)) {
_preview.setBounds(region.rect); this->dropPreview.setBounds(region.rect);
if (!_preview.isVisible()) { if (!this->dropPreview.isVisible()) {
_preview.show(); this->dropPreview.show();
_preview.raise(); this->dropPreview.raise();
} }
dropPosition = region.position; dropPosition = region.position;
@ -212,12 +213,12 @@ void NotebookPage::setPreviewRect(QPoint mousePos)
} }
} }
_preview.hide(); this->dropPreview.hide();
} }
void NotebookPage::dragLeaveEvent(QDragLeaveEvent *event) void NotebookPage::dragLeaveEvent(QDragLeaveEvent *event)
{ {
_preview.hide(); this->dropPreview.hide();
} }
void NotebookPage::dropEvent(QDropEvent *event) void NotebookPage::dropEvent(QDropEvent *event)
@ -230,14 +231,14 @@ void NotebookPage::dropEvent(QDropEvent *event)
addToLayout(NotebookPage::draggingSplit, dropPosition); addToLayout(NotebookPage::draggingSplit, dropPosition);
} }
_preview.hide(); this->dropPreview.hide();
} }
void NotebookPage::paintEvent(QPaintEvent *) void NotebookPage::paintEvent(QPaintEvent *)
{ {
QPainter painter(this); QPainter painter(this);
if (_hbox.count() == 0) { if (this->ui.hbox.count() == 0) {
painter.fillRect(rect(), this->colorScheme.ChatBackground); painter.fillRect(rect(), this->colorScheme.ChatBackground);
painter.fillRect(0, 0, width(), 2, this->colorScheme.TabSelectedBackground); painter.fillRect(0, 0, width(), 2, this->colorScheme.TabSelectedBackground);
@ -262,7 +263,7 @@ static std::pair<int, int> getWidgetPositionInLayout(QLayout *layout, const Chat
std::pair<int, int> NotebookPage::getChatPosition(const ChatWidget *chatWidget) std::pair<int, int> NotebookPage::getChatPosition(const ChatWidget *chatWidget)
{ {
auto layout = _hbox.layout(); auto layout = this->ui.hbox.layout();
if (layout == nullptr) { if (layout == nullptr) {
return std::make_pair(-1, -1); return std::make_pair(-1, -1);
@ -335,12 +336,12 @@ boost::property_tree::ptree NotebookPage::save()
{ {
boost::property_tree::ptree tree; boost::property_tree::ptree tree;
auto layout = _hbox.layout(); auto layout = this->ui.hbox.layout();
saveFromLayout(layout, tree); saveFromLayout(layout, tree);
/* /*
for (const auto &chat : chatWidgets) { for (const auto &chat : this->chatWidgets) {
boost::property_tree::ptree child = chat->save(); boost::property_tree::ptree child = chat->save();
// Set child position // Set child position

View file

@ -43,16 +43,16 @@ public:
static std::pair<int, int> dropPosition; static std::pair<int, int> dropPosition;
protected: protected:
void paintEvent(QPaintEvent *) override; virtual void paintEvent(QPaintEvent *) override;
void enterEvent(QEvent *) override; virtual void enterEvent(QEvent *) override;
void leaveEvent(QEvent *) override; virtual void leaveEvent(QEvent *) override;
void mouseReleaseEvent(QMouseEvent *event) override; virtual void mouseReleaseEvent(QMouseEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override; virtual void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override; virtual void dragMoveEvent(QDragMoveEvent *event) override;
void dragLeaveEvent(QDragLeaveEvent *event) override; virtual void dragLeaveEvent(QDragLeaveEvent *event) override;
void dropEvent(QDropEvent *event) override; virtual void dropEvent(QDropEvent *event) override;
private: private:
struct DropRegion { struct DropRegion {
@ -68,13 +68,16 @@ private:
NotebookTab *tab; NotebookTab *tab;
QVBoxLayout _parentbox; struct {
QHBoxLayout _hbox; QVBoxLayout parentLayout;
std::vector<ChatWidget *> _chatWidgets; QHBoxLayout hbox;
std::vector<DropRegion> _dropRegions; } ui;
NotebookPageDropPreview _preview; std::vector<ChatWidget *> chatWidgets;
std::vector<DropRegion> dropRegions;
NotebookPageDropPreview dropPreview;
void setPreviewRect(QPoint mousePos); void setPreviewRect(QPoint mousePos);