mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Use addChat instead of re-implementing the same code (thanks hemirt)
Some refactoring Updated settings library version
This commit is contained in:
parent
d2d65b9cb5
commit
c5c2718dc0
|
@ -1 +1 @@
|
|||
Subproject commit b0a6b529a0702d69c085d76ef832639ca1c02dcd
|
||||
Subproject commit 1282c351db528f9e2f166b2bb1c217c9d67a8bc8
|
|
@ -137,7 +137,8 @@ void IrcManager::refreshTwitchEmotes(const QString &username, const QString &oau
|
|||
"/emotes?oauth_token=" + oauthToken + "&client_id=" + oauthClient);
|
||||
|
||||
if (true) {
|
||||
util::urlFetchJSONTimeout(url,
|
||||
util::urlFetchJSONTimeout(
|
||||
url,
|
||||
[=](QJsonObject &root) {
|
||||
// nextLink =
|
||||
// root.value("_links").toObject().value("next").toString();
|
||||
|
@ -148,13 +149,12 @@ void IrcManager::refreshTwitchEmotes(const QString &username, const QString &oau
|
|||
for (QJsonValue block : blocks) {
|
||||
QJsonObject user = block.toObject().value("user").toObject();
|
||||
// display_name
|
||||
_twitchBlockedUsers.insert(
|
||||
user.value("name").toString().toLower(), true);
|
||||
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
||||
}
|
||||
_twitchBlockedUsersMutex.unlock();
|
||||
qDebug() << "XD";
|
||||
},
|
||||
3000, &this->_accessManager);
|
||||
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
|
||||
|
|
|
@ -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<QString, bool> _twitchBlockedUsers;
|
||||
QMutex _twitchBlockedUsersMutex;
|
||||
|
||||
QNetworkAccessManager _accessManager;
|
||||
QNetworkAccessManager networkAccessManager;
|
||||
|
||||
// methods
|
||||
Communi::IrcConnection *createConnection(bool doRead);
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
#include <QWidget>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
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<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
|
||||
{
|
||||
return _chatWidgets;
|
||||
return this->chatWidgets;
|
||||
}
|
||||
|
||||
NotebookTab *NotebookPage::getTab() const
|
||||
|
@ -59,79 +125,12 @@ void NotebookPage::addChat(bool openChannelNameDialog)
|
|||
w->showChangeChannelPopup();
|
||||
}
|
||||
|
||||
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);
|
||||
this->addToLayout(w, std::pair<int, int>(-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<int, int>(-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<int, int>(-1, -1)));
|
||||
if (this->ui.hbox.count() == 0) {
|
||||
this->dropRegions.push_back(DropRegion(rect(), std::pair<int, int>(-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),
|
||||
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<int, int>(i, -1)));
|
||||
}
|
||||
|
||||
for (int i = 0; i < _hbox.count(); ++i) {
|
||||
auto vbox = static_cast<QVBoxLayout *>(_hbox.itemAt(i));
|
||||
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() + 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<int, int>(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<int, int> getWidgetPositionInLayout(QLayout *layout, const Chat
|
|||
|
||||
std::pair<int, int> 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
|
||||
|
|
|
@ -43,16 +43,16 @@ public:
|
|||
static std::pair<int, int> 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<ChatWidget *> _chatWidgets;
|
||||
std::vector<DropRegion> _dropRegions;
|
||||
QHBoxLayout hbox;
|
||||
} ui;
|
||||
|
||||
NotebookPageDropPreview _preview;
|
||||
std::vector<ChatWidget *> chatWidgets;
|
||||
std::vector<DropRegion> dropRegions;
|
||||
|
||||
NotebookPageDropPreview dropPreview;
|
||||
|
||||
void setPreviewRect(QPoint mousePos);
|
||||
|
||||
|
|
Loading…
Reference in a new issue