mirror-chatterino2/widgets/chatwidget.cpp

146 lines
3.3 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "widgets/chatwidget.h"
2017-01-17 00:15:44 +01:00
#include "channels.h"
2017-01-01 02:30:42 +01:00
#include "colorscheme.h"
2017-01-23 16:38:06 +01:00
#include "settings.h"
2017-01-18 21:30:23 +01:00
#include "widgets/textinputdialog.h"
2017-01-17 00:15:44 +01:00
#include <QDebug>
2017-01-17 00:15:44 +01:00
#include <QFont>
#include <QFontDatabase>
#include <QPainter>
#include <QVBoxLayout>
#include <boost/signals2.hpp>
2016-12-29 17:31:07 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
2016-12-29 17:31:07 +01:00
ChatWidget::ChatWidget(QWidget *parent)
2017-01-11 18:52:09 +01:00
: QWidget(parent)
, messages()
2017-01-30 19:14:25 +01:00
, channel(Channels::getEmpty())
2017-01-18 04:33:30 +01:00
, channelName(QString())
, vbox(this)
, header(this)
, view(this)
2017-01-29 13:23:22 +01:00
, input(this)
2016-12-29 17:31:07 +01:00
{
2017-01-18 04:33:30 +01:00
this->vbox.setSpacing(0);
this->vbox.setMargin(1);
2017-01-01 02:30:42 +01:00
2017-01-18 04:33:30 +01:00
this->vbox.addWidget(&header);
2017-01-21 05:14:27 +01:00
this->vbox.addWidget(&view, 1);
2017-01-18 04:33:30 +01:00
this->vbox.addWidget(&input);
2017-01-01 02:30:42 +01:00
}
ChatWidget::~ChatWidget()
{
2016-12-29 17:31:07 +01:00
}
2017-01-17 00:15:44 +01:00
void
ChatWidget::setChannelName(const QString &name)
{
QString channel = name.trimmed();
2017-01-18 04:33:30 +01:00
if (QString::compare(channel, this->channelName, Qt::CaseInsensitive) ==
0) {
this->channelName = channel;
this->header.updateChannelText();
2017-01-17 00:15:44 +01:00
return;
}
2017-01-18 04:33:30 +01:00
if (!this->channelName.isEmpty()) {
Channels::removeChannel(this->channelName);
this->messageAppendedConnection.disconnect();
this->messageRemovedConnection.disconnect();
2017-01-17 00:15:44 +01:00
}
this->channelName = channel;
this->header.updateChannelText();
this->view.layoutMessages();
messages.clear();
2017-01-17 00:15:44 +01:00
if (channel.isEmpty()) {
2017-01-18 04:33:30 +01:00
this->channel = NULL;
2017-01-17 00:15:44 +01:00
} else {
2017-01-18 04:33:30 +01:00
this->channel = Channels::addChannel(channel);
this->messageAppendedConnection =
this->channel.get()->messageAppended.connect([this](
std::shared_ptr<messages::Message> &message) {
std::shared_ptr<messages::MessageRef> deleted;
auto messageRef = new messages::MessageRef(message);
this->messages.appendItem(
std::shared_ptr<messages::MessageRef>(messageRef), deleted);
});
this->messageRemovedConnection =
this->channel.get()->messageRemovedFromStart.connect(
[this](std::shared_ptr<messages::Message> &) {});
auto snapshot = this->channel.get()->getMessageSnapshot();
for (int i = 0; i < snapshot.getLength(); i++) {
std::shared_ptr<messages::MessageRef> deleted;
auto messageRef = new messages::MessageRef(snapshot[i]);
this->messages.appendItem(
std::shared_ptr<messages::MessageRef>(messageRef), deleted);
}
2017-01-17 00:15:44 +01:00
}
2017-01-26 04:26:40 +01:00
this->view.layoutMessages();
2017-01-26 07:10:46 +01:00
this->view.update();
2017-01-17 00:15:44 +01:00
}
void
ChatWidget::showChangeChannelPopup()
{
TextInputDialog dialog(this);
2017-01-18 04:33:30 +01:00
dialog.setText(this->channelName);
2017-01-17 00:15:44 +01:00
if (dialog.exec() == QDialog::Accepted) {
2017-01-18 04:33:30 +01:00
setChannelName(dialog.getText());
2017-01-17 00:15:44 +01:00
}
}
2017-01-11 18:52:09 +01:00
void
ChatWidget::paintEvent(QPaintEvent *)
2016-12-29 17:31:07 +01:00
{
2017-01-11 18:52:09 +01:00
QPainter painter(this);
2016-12-29 17:31:07 +01:00
2017-01-24 20:15:12 +01:00
painter.fillRect(this->rect(), ColorScheme::getInstance().ChatBackground);
2016-12-29 17:31:07 +01:00
}
2017-01-29 11:38:00 +01:00
void
ChatWidget::load(const boost::property_tree::ptree &tree)
{
// Load tab text
try {
this->setChannelName(
QString::fromStdString(tree.get<std::string>("channelName")));
} catch (boost::property_tree::ptree_error) {
}
2017-01-18 21:30:23 +01:00
}
2017-01-29 11:38:00 +01:00
boost::property_tree::ptree
ChatWidget::save()
{
boost::property_tree::ptree tree;
tree.put("channelName", this->getChannelName().toStdString());
return tree;
2017-01-18 21:30:23 +01:00
}
2017-01-29 11:38:00 +01:00
} // namespace widgets
} // namespace chatterino