mirror-chatterino2/widgets/chatwidget.cpp

203 lines
4.2 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "widgets/chatwidget.h"
2017-04-12 17:46:44 +02:00
#include "channelmanager.h"
2017-01-01 02:30:42 +01:00
#include "colorscheme.h"
2017-04-12 17:46:44 +02:00
#include "settingsmanager.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-04-14 17:52:22 +02:00
using namespace chatterino::messages;
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2016-12-29 17:31:07 +01:00
ChatWidget::ChatWidget(QWidget *parent)
2017-01-11 18:52:09 +01:00
: QWidget(parent)
2017-04-12 17:46:44 +02:00
, _messages()
, _channel(ChannelManager::getInstance().getEmpty())
, _channelName(QString())
, _vbox(this)
, _header(this)
, _view(this)
, _input(this)
2016-12-29 17:31:07 +01:00
{
2017-04-12 17:46:44 +02:00
this->_vbox.setSpacing(0);
this->_vbox.setMargin(1);
2017-01-01 02:30:42 +01:00
2017-04-12 17:46:44 +02:00
this->_vbox.addWidget(&_header);
this->_vbox.addWidget(&_view, 1);
this->_vbox.addWidget(&_input);
2017-01-01 02:30:42 +01:00
}
ChatWidget::~ChatWidget()
{
2016-12-29 17:31:07 +01:00
}
2017-04-12 17:46:44 +02:00
std::shared_ptr<Channel>
ChatWidget::getChannel() const
{
return _channel;
}
const QString &
ChatWidget::getChannelName() const
{
return _channelName;
}
2017-01-17 00:15:44 +01:00
void
ChatWidget::setChannelName(const QString &name)
{
QString channel = name.trimmed();
2017-04-12 17:46:44 +02:00
// return if channel name is the same
if (QString::compare(channel, _channelName, Qt::CaseInsensitive) == 0) {
_channelName = channel;
_header.updateChannelText();
2017-01-17 00:15:44 +01:00
return;
}
2017-04-12 17:46:44 +02:00
// remove current channel
if (!_channelName.isEmpty()) {
ChannelManager::getInstance().removeChannel(_channelName);
2017-04-12 17:46:44 +02:00
detachChannel(_channel);
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
// update members
_channelName = channel;
2017-04-12 17:46:44 +02:00
// update messages
_messages.clear();
2017-01-17 00:15:44 +01:00
if (channel.isEmpty()) {
2017-04-12 17:46:44 +02:00
_channel = NULL;
2017-01-17 00:15:44 +01:00
} else {
2017-04-12 17:46:44 +02:00
_channel = ChannelManager::getInstance().addChannel(channel);
attachChannel(_channel);
}
// update header
_header.updateChannelText();
2017-04-12 17:46:44 +02:00
// update view
_view.layoutMessages();
_view.update();
}
void
ChatWidget::attachChannel(SharedChannel channel)
{
// on new message
_messageAppendedConnection =
channel->messageAppended.connect([this](SharedMessage &message) {
SharedMessageRef deleted;
2017-04-12 17:46:44 +02:00
auto messageRef = new MessageRef(message);
2017-04-12 17:46:44 +02:00
if (_messages.appendItem(SharedMessageRef(messageRef), deleted)) {
qreal value =
std::max(0.0, _view.getScrollbar()->getDesiredValue() - 1);
2017-04-12 17:46:44 +02:00
_view.getScrollbar()->setDesiredValue(value, false);
}
});
2017-04-12 17:46:44 +02:00
// on message removed
_messageRemovedConnection =
_channel->messageRemovedFromStart.connect([this](SharedMessage &) {});
2017-04-12 17:46:44 +02:00
auto snapshot = _channel.get()->getMessageSnapshot();
2017-04-14 17:47:28 +02:00
for (int i = 0; i < snapshot.getSize(); i++) {
2017-04-12 17:46:44 +02:00
SharedMessageRef deleted;
2017-04-12 17:46:44 +02:00
auto messageRef = new MessageRef(snapshot[i]);
2017-04-12 17:46:44 +02:00
_messages.appendItem(SharedMessageRef(messageRef), deleted);
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
}
void
ChatWidget::detachChannel(std::shared_ptr<Channel> channel)
{
// on message added
_messageAppendedConnection.disconnect();
2017-01-26 04:26:40 +01:00
2017-04-12 17:46:44 +02:00
// on message removed
_messageRemovedConnection.disconnect();
}
LimitedQueueSnapshot<SharedMessageRef>
ChatWidget::getMessagesSnapshot()
{
return _messages.getSnapshot();
2017-01-17 00:15:44 +01:00
}
void
ChatWidget::showChangeChannelPopup()
{
2017-04-12 17:46:44 +02:00
// create new input dialog and execute it
2017-01-17 00:15:44 +01:00
TextInputDialog dialog(this);
2017-04-12 17:46:44 +02:00
dialog.setText(_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-04-12 17:46:44 +02:00
void
ChatWidget::layoutMessages()
{
if (_view.layoutMessages()) {
_view.update();
}
}
void
ChatWidget::updateGifEmotes()
{
_view.updateGifEmotes();
}
2017-01-11 18:52:09 +01:00
void
ChatWidget::paintEvent(QPaintEvent *)
2016-12-29 17:31:07 +01:00
{
2017-04-12 17:46:44 +02:00
// color the background of the chat
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)
{
2017-04-12 17:46:44 +02:00
// load tab text
2017-01-29 11:38:00 +01:00
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
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino