2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/chatwidget.hpp"
|
|
|
|
#include "channelmanager.hpp"
|
|
|
|
#include "colorscheme.hpp"
|
|
|
|
#include "notebookpage.hpp"
|
|
|
|
#include "settingsmanager.hpp"
|
|
|
|
#include "widgets/textinputdialog.hpp"
|
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
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void ezShortcut(ChatWidget *w, const char *key, T t)
|
|
|
|
{
|
|
|
|
auto s = new QShortcut(QKeySequence(key), w);
|
|
|
|
s->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
QObject::connect(s, &QShortcut::activated, w, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
ChatWidget::ChatWidget(ChannelManager &_channelManager, NotebookPage *parent)
|
|
|
|
: BaseWidget(parent)
|
2017-06-13 21:13:58 +02:00
|
|
|
, channelManager(_channelManager)
|
|
|
|
, channel(_channelManager.getEmpty())
|
2017-06-11 11:36:42 +02:00
|
|
|
, vbox(this)
|
|
|
|
, header(this)
|
|
|
|
, view(this)
|
|
|
|
, input(this)
|
2016-12-29 17:31:07 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->vbox.setSpacing(0);
|
|
|
|
this->vbox.setMargin(1);
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->vbox.addWidget(&this->header);
|
|
|
|
this->vbox.addWidget(&this->view, 1);
|
|
|
|
this->vbox.addWidget(&this->input);
|
2017-06-10 23:53:39 +02:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
// Initialize chat widget-wide hotkeys
|
2017-06-10 23:53:39 +02:00
|
|
|
// CTRL+T: Create new split (Add page)
|
2017-06-11 11:36:42 +02:00
|
|
|
ezShortcut(this, "CTRL+T", &ChatWidget::doAddSplit);
|
2017-06-10 23:53:39 +02:00
|
|
|
|
|
|
|
// CTRL+W: Close Split
|
2017-06-11 11:36:42 +02:00
|
|
|
ezShortcut(this, "CTRL+W", &ChatWidget::doCloseSplit);
|
2017-06-10 23:53:39 +02:00
|
|
|
|
|
|
|
// CTRL+R: Change Channel
|
2017-06-11 11:36:42 +02:00
|
|
|
ezShortcut(this, "CTRL+R", &ChatWidget::doChangeChannel);
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatWidget::~ChatWidget()
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->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
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
return this->channel;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:45:40 +02:00
|
|
|
std::shared_ptr<Channel> &ChatWidget::getChannelRef()
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
return this->channel;
|
2017-05-27 17:45:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
const QString &ChatWidget::getChannelName() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
return this->channelName;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
void ChatWidget::setChannelName(const QString &_newChannelName)
|
2017-01-17 00:15:44 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
QString newChannelName = _newChannelName.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-06-11 11:36:42 +02:00
|
|
|
if (QString::compare(newChannelName, this->channelName, Qt::CaseInsensitive) == 0) {
|
|
|
|
this->channelName = newChannelName;
|
|
|
|
this->header.updateChannelText();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// remove current channel
|
2017-06-11 11:36:42 +02:00
|
|
|
if (!this->channelName.isEmpty()) {
|
2017-06-13 21:13:58 +02:00
|
|
|
this->channelManager.removeChannel(this->channelName);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->detachChannel();
|
2017-01-17 00:15:44 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// update members
|
2017-06-11 11:36:42 +02:00
|
|
|
this->channelName = newChannelName;
|
2017-02-02 23:51:02 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// update messages
|
2017-06-11 11:36:42 +02:00
|
|
|
this->messages.clear();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
if (newChannelName.isEmpty()) {
|
|
|
|
this->channel = nullptr;
|
2017-01-17 00:15:44 +01:00
|
|
|
} else {
|
2017-06-13 21:13:58 +02:00
|
|
|
this->setChannel(this->channelManager.addChannel(newChannelName));
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update header
|
2017-06-11 11:36:42 +02:00
|
|
|
this->header.updateChannelText();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// update view
|
2017-06-11 11:36:42 +02:00
|
|
|
this->view.layoutMessages();
|
|
|
|
this->view.update();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
void ChatWidget::setChannel(std::shared_ptr<Channel> _newChannel)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->channel = _newChannel;
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// on new message
|
2017-06-11 11:36:42 +02:00
|
|
|
this->messageAppendedConnection =
|
|
|
|
this->channel->messageAppended.connect([this](SharedMessage &message) {
|
|
|
|
SharedMessageRef deleted;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
auto messageRef = new MessageRef(message);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
if (this->messages.appendItem(SharedMessageRef(messageRef), deleted)) {
|
|
|
|
qreal value = std::max(0.0, this->view.getScrollBar().getDesiredValue() - 1);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->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-06-11 11:36:42 +02:00
|
|
|
this->messageRemovedConnection =
|
|
|
|
this->channel->messageRemovedFromStart.connect([](SharedMessage &) {
|
|
|
|
//
|
|
|
|
});
|
2017-02-02 23:51:02 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
auto snapshot = this->channel->getMessageSnapshot();
|
2017-02-02 23:51:02 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
for (int i = 0; i < snapshot.getLength(); 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-06-11 11:36:42 +02:00
|
|
|
this->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
|
2017-06-11 11:36:42 +02:00
|
|
|
this->messageAppendedConnection.disconnect();
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// on message removed
|
2017-06-11 11:36:42 +02:00
|
|
|
this->messageRemovedConnection.disconnect();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
LimitedQueueSnapshot<SharedMessageRef> ChatWidget::getMessagesSnapshot()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
return this->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-06-11 11:36:42 +02: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-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::layoutMessages()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
if (this->view.layoutMessages()) {
|
|
|
|
this->view.update();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:16:39 +02:00
|
|
|
void ChatWidget::updateGifEmotes()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->view.updateGifEmotes();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
void ChatWidget::giveFocus()
|
|
|
|
{
|
|
|
|
this->input.textInput.setFocus();
|
|
|
|
}
|
|
|
|
|
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-06-26 16:41:20 +02:00
|
|
|
painter.fillRect(this->rect(), this->colorScheme.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());
|
2017-06-29 14:13:00 +02:00
|
|
|
page->addChat(true);
|
2017-06-10 23:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doCloseSplit()
|
|
|
|
{
|
2017-06-11 12:03:47 +02:00
|
|
|
NotebookPage *page = static_cast<NotebookPage *>(this->parentWidget());
|
|
|
|
page->removeFromLayout(this);
|
2017-06-10 23:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doChangeChannel()
|
|
|
|
{
|
|
|
|
this->showChangeChannelPopup();
|
|
|
|
}
|
|
|
|
|
2017-06-11 09:11:55 +02:00
|
|
|
void ChatWidget::doPopup()
|
|
|
|
{
|
|
|
|
// TODO: Copy signals and stuff too
|
2017-06-26 16:41:20 +02:00
|
|
|
auto widget =
|
|
|
|
new ChatWidget(this->channelManager, static_cast<NotebookPage *>(this->parentWidget()));
|
2017-06-11 09:11:55 +02:00
|
|
|
widget->setChannelName(this->getChannelName());
|
|
|
|
widget->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doClearChat()
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
qDebug() << "[UNIMPLEMENTED] Clear chat";
|
2017-06-11 09:11:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doOpenChannel()
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
qDebug() << "[UNIMPLEMENTED] Open twitch.tv/" << this->getChannelName();
|
2017-06-11 09:11:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidget::doOpenPopupPlayer()
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
qDebug() << "[UNIMPLEMENTED] Open twitch.tv/" << this->getChannelName() << "/popout";
|
2017-06-11 09:11:55 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|