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-06-10 23:53:39 +02:00
|
|
|
#include "notebookpage.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
|
|
|
|
2017-02-02 23:51:02 +01:00
|
|
|
#include <QDebug>
|
2017-01-17 00:15:44 +01:00
|
|
|
#include <QFont>
|
|
|
|
#include <QFontDatabase>
|
|
|
|
#include <QPainter>
|
2017-06-10 23:53:39 +02:00
|
|
|
#include <QShortcut>
|
2017-01-17 00:15:44 +01:00
|
|
|
#include <QVBoxLayout>
|
2017-02-02 22:15:09 +01:00
|
|
|
#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-06-10 23:53:39 +02:00
|
|
|
|
|
|
|
// Initialize widget-wide hotkeys
|
|
|
|
// CTRL+T: Create new split (Add page)
|
|
|
|
{
|
|
|
|
auto s = new QShortcut(QKeySequence("CTRL+T"), this);
|
|
|
|
s->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
connect(s, &QShortcut::activated, this, &ChatWidget::doAddSplit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CTRL+W: Close Split
|
|
|
|
{
|
|
|
|
auto s = new QShortcut(QKeySequence("CTRL+W"), this);
|
|
|
|
s->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
connect(s, &QShortcut::activated, this, &ChatWidget::doCloseSplit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CTRL+R: Change Channel
|
|
|
|
{
|
|
|
|
auto s = new QShortcut(QKeySequence("CTRL+R"), this);
|
|
|
|
s->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
connect(s, &QShortcut::activated, this, &ChatWidget::doChangeChannel);
|
|
|
|
}
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatWidget::~ChatWidget()
|
|
|
|
{
|
2017-05-29 21:02:01 +02:00
|
|
|
detachChannel();
|
2016-12-29 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
std::shared_ptr<Channel> ChatWidget::getChannel() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _channel;
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:45:40 +02:00
|
|
|
std::shared_ptr<Channel> &ChatWidget::getChannelRef()
|
|
|
|
{
|
|
|
|
return _channel;
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
const QString &ChatWidget::getChannelName() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _channelName;
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::setChannelName(const QString &name)
|
2017-01-17 00:15:44 +01:00
|
|
|
{
|
2017-05-27 17:45:40 +02:00
|
|
|
QString channelName = name.trimmed();
|
2017-01-17 00:15:44 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// return if channel name is the same
|
2017-05-27 17:45:40 +02:00
|
|
|
if (QString::compare(channelName, _channelName, Qt::CaseInsensitive) == 0) {
|
|
|
|
_channelName = channelName;
|
2017-04-12 17:46:44 +02:00
|
|
|
_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-02-02 22:15:09 +01:00
|
|
|
|
2017-05-29 21:02:01 +02:00
|
|
|
detachChannel();
|
2017-01-17 00:15:44 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// update members
|
2017-05-27 17:45:40 +02:00
|
|
|
_channelName = channelName;
|
2017-02-02 23:51:02 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// update messages
|
|
|
|
_messages.clear();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-05-27 17:45:40 +02:00
|
|
|
printf("Set channel name xD %s\n", qPrintable(name));
|
|
|
|
|
|
|
|
if (channelName.isEmpty()) {
|
2017-06-10 22:48:28 +02:00
|
|
|
_channel = nullptr;
|
2017-01-17 00:15:44 +01:00
|
|
|
} else {
|
2017-05-27 17:45:40 +02:00
|
|
|
_channel = ChannelManager::getInstance().addChannel(channelName);
|
|
|
|
printf("Created channel FeelsGoodMan %p\n", _channel.get());
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
attachChannel(_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update header
|
|
|
|
_header.updateChannelText();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// update view
|
|
|
|
_view.layoutMessages();
|
|
|
|
_view.update();
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::attachChannel(SharedChannel channel)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
// on new message
|
2017-05-27 16:16:39 +02:00
|
|
|
_messageAppendedConnection = channel->messageAppended.connect([this](SharedMessage &message) {
|
|
|
|
SharedMessageRef deleted;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
auto messageRef = new MessageRef(message);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
if (_messages.appendItem(SharedMessageRef(messageRef), deleted)) {
|
|
|
|
qreal value = std::max(0.0, _view.getScrollbar()->getDesiredValue() - 1);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
_view.getScrollbar()->setDesiredValue(value, false);
|
|
|
|
}
|
|
|
|
});
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// on message removed
|
2017-05-27 16:16:39 +02:00
|
|
|
_messageRemovedConnection = _channel->messageRemovedFromStart.connect([](SharedMessage &) {
|
|
|
|
//
|
|
|
|
});
|
2017-02-02 23:51:02 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
auto snapshot = _channel.get()->getMessageSnapshot();
|
2017-02-02 23:51:02 +01:00
|
|
|
|
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-02-02 23:51:02 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
auto messageRef = new MessageRef(snapshot[i]);
|
2017-02-02 23:51:02 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-29 21:02:01 +02:00
|
|
|
void ChatWidget::detachChannel()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
LimitedQueueSnapshot<SharedMessageRef> ChatWidget::getMessagesSnapshot()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _messages.getSnapshot();
|
2017-01-17 00:15:44 +01:00
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::showChangeChannelPopup()
|
2017-01-17 00:15:44 +01:00
|
|
|
{
|
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-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::layoutMessages()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
if (_view.layoutMessages()) {
|
|
|
|
_view.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::updateGifEmotes()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
_view.updateGifEmotes();
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02: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
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::load(const boost::property_tree::ptree &tree)
|
2017-01-29 11:38:00 +01:00
|
|
|
{
|
2017-04-12 17:46:44 +02:00
|
|
|
// load tab text
|
2017-01-29 11:38:00 +01:00
|
|
|
try {
|
2017-05-27 16:16:39 +02:00
|
|
|
this->setChannelName(QString::fromStdString(tree.get<std::string>("channelName")));
|
2017-01-29 11:38:00 +01:00
|
|
|
} catch (boost::property_tree::ptree_error) {
|
|
|
|
}
|
2017-01-18 21:30:23 +01:00
|
|
|
}
|
2017-01-29 11:38:00 +01:00
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
boost::property_tree::ptree ChatWidget::save()
|
2017-01-29 11:38:00 +01:00
|
|
|
{
|
|
|
|
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-06-10 23:53:39 +02:00
|
|
|
/// Slots
|
|
|
|
//
|
|
|
|
void ChatWidget::doAddSplit()
|
|
|
|
{
|
|
|
|
NotebookPage *page = static_cast<NotebookPage *>(this->parentWidget());
|
|
|
|
page->addChat();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doCloseSplit()
|
|
|
|
{
|
|
|
|
qDebug() << "Close split for" << this->getChannelName();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doChangeChannel()
|
|
|
|
{
|
|
|
|
this->showChangeChannelPopup();
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|