Change the way ChatWidget stores its channels name

This commit is contained in:
Rasmus Karlsson 2017-07-09 17:49:02 +02:00
parent 1f7d0dcead
commit b5b07a3e63
3 changed files with 56 additions and 55 deletions

View file

@ -13,6 +13,8 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include <functional>
using namespace chatterino::messages; using namespace chatterino::messages;
namespace chatterino { namespace chatterino {
@ -30,6 +32,8 @@ inline void ezShortcut(ChatWidget *w, const char *key, T t)
} // namespace } // namespace
static int index = 0;
ChatWidget::ChatWidget(ChannelManager &_channelManager, NotebookPage *parent) ChatWidget::ChatWidget(ChannelManager &_channelManager, NotebookPage *parent)
: BaseWidget(parent) : BaseWidget(parent)
, channelManager(_channelManager) , channelManager(_channelManager)
@ -38,6 +42,7 @@ ChatWidget::ChatWidget(ChannelManager &_channelManager, NotebookPage *parent)
, header(this) , header(this)
, view(this) , view(this)
, input(this) , input(this)
, channelName("/chatWidgets/" + std::to_string(index++) + "/channelName")
{ {
this->vbox.setSpacing(0); this->vbox.setSpacing(0);
this->vbox.setMargin(1); this->vbox.setMargin(1);
@ -55,6 +60,11 @@ ChatWidget::ChatWidget(ChannelManager &_channelManager, NotebookPage *parent)
// CTRL+R: Change Channel // CTRL+R: Change Channel
ezShortcut(this, "CTRL+R", &ChatWidget::doChangeChannel); ezShortcut(this, "CTRL+R", &ChatWidget::doChangeChannel);
this->channelName.getValueChangedSignal().connect(
std::bind(&ChatWidget::channelNameUpdated, this, std::placeholders::_1));
this->channelNameUpdated(this->channelName.getValue());
} }
ChatWidget::~ChatWidget() ChatWidget::~ChatWidget()
@ -72,49 +82,6 @@ std::shared_ptr<Channel> &ChatWidget::getChannelRef()
return this->channel; return this->channel;
} }
const QString &ChatWidget::getChannelName() const
{
return this->channelName;
}
void ChatWidget::setChannelName(const QString &_newChannelName)
{
QString newChannelName = _newChannelName.trimmed();
// return if channel name is the same
if (QString::compare(newChannelName, this->channelName, Qt::CaseInsensitive) == 0) {
this->channelName = newChannelName;
this->header.updateChannelText();
return;
}
// remove current channel
if (!this->channelName.isEmpty()) {
this->channelManager.removeChannel(this->channelName);
this->detachChannel();
}
// update members
this->channelName = newChannelName;
// update messages
this->messages.clear();
if (newChannelName.isEmpty()) {
this->channel = nullptr;
} else {
this->setChannel(this->channelManager.addChannel(newChannelName));
}
// update header
this->header.updateChannelText();
// update view
this->layoutMessages(true);
}
void ChatWidget::setChannel(std::shared_ptr<Channel> _newChannel) void ChatWidget::setChannel(std::shared_ptr<Channel> _newChannel)
{ {
this->channel = _newChannel; this->channel = _newChannel;
@ -159,6 +126,31 @@ void ChatWidget::detachChannel()
this->messageRemovedConnection.disconnect(); this->messageRemovedConnection.disconnect();
} }
void ChatWidget::channelNameUpdated(const std::string &newChannelName)
{
// remove current channel
if (!this->channel->isEmpty()) {
this->channelManager.removeChannel(this->channel->getName());
this->detachChannel();
}
// update messages
this->messages.clear();
if (newChannelName.empty()) {
this->channel = nullptr;
} else {
this->setChannel(this->channelManager.addChannel(QString::fromStdString(newChannelName)));
}
// update header
this->header.updateChannelText();
// update view
this->layoutMessages(true);
}
LimitedQueueSnapshot<SharedMessageRef> ChatWidget::getMessagesSnapshot() LimitedQueueSnapshot<SharedMessageRef> ChatWidget::getMessagesSnapshot()
{ {
return this->messages.getSnapshot(); return this->messages.getSnapshot();
@ -169,10 +161,12 @@ void ChatWidget::showChangeChannelPopup()
// create new input dialog and execute it // create new input dialog and execute it
TextInputDialog dialog(this); TextInputDialog dialog(this);
dialog.setText(this->channelName); dialog.setText(QString::fromStdString(this->channelName));
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
setChannelName(dialog.getText()); QString newChannelName = dialog.getText().trimmed();
this->channelName = newChannelName.toStdString();
} }
} }
@ -205,7 +199,7 @@ void ChatWidget::load(const boost::property_tree::ptree &tree)
{ {
// load tab text // load tab text
try { try {
this->setChannelName(QString::fromStdString(tree.get<std::string>("channelName"))); this->channelName = tree.get<std::string>("channelName");
} catch (boost::property_tree::ptree_error) { } catch (boost::property_tree::ptree_error) {
} }
} }
@ -214,7 +208,7 @@ boost::property_tree::ptree ChatWidget::save()
{ {
boost::property_tree::ptree tree; boost::property_tree::ptree tree;
tree.put("channelName", this->getChannelName().toStdString()); tree.put("channelName", this->channelName.getValue());
return tree; return tree;
} }
@ -242,7 +236,7 @@ void ChatWidget::doPopup()
// TODO: Copy signals and stuff too // TODO: Copy signals and stuff too
auto widget = auto widget =
new ChatWidget(this->channelManager, static_cast<NotebookPage *>(this->parentWidget())); new ChatWidget(this->channelManager, static_cast<NotebookPage *>(this->parentWidget()));
widget->setChannelName(this->getChannelName()); widget->channelName = this->channelName;
widget->show(); widget->show();
} }
@ -257,12 +251,14 @@ void ChatWidget::doClearChat()
void ChatWidget::doOpenChannel() void ChatWidget::doOpenChannel()
{ {
qDebug() << "[UNIMPLEMENTED] Open twitch.tv/" << this->getChannelName(); qDebug() << "[UNIMPLEMENTED] Open twitch.tv/"
<< QString::fromStdString(this->channelName.getValue());
} }
void ChatWidget::doOpenPopupPlayer() void ChatWidget::doOpenPopupPlayer()
{ {
qDebug() << "[UNIMPLEMENTED] Open twitch.tv/" << this->getChannelName() << "/popout"; qDebug() << "[UNIMPLEMENTED] Open twitch.tv/"
<< QString::fromStdString(this->channelName.getValue()) << "/popout";
} }
} // namespace widgets } // namespace widgets

View file

@ -46,8 +46,6 @@ public:
std::shared_ptr<Channel> getChannel() const; std::shared_ptr<Channel> getChannel() const;
std::shared_ptr<Channel> &getChannelRef(); std::shared_ptr<Channel> &getChannelRef();
const QString &getChannelName() const;
void setChannelName(const QString &name);
void showChangeChannelPopup(); void showChangeChannelPopup();
messages::LimitedQueueSnapshot<messages::SharedMessageRef> getMessagesSnapshot(); messages::LimitedQueueSnapshot<messages::SharedMessageRef> getMessagesSnapshot();
@ -56,6 +54,8 @@ public:
void giveFocus(); void giveFocus();
pajlada::Settings::Setting<std::string> channelName;
protected: protected:
virtual void paintEvent(QPaintEvent *) override; virtual void paintEvent(QPaintEvent *) override;
@ -65,10 +65,11 @@ private:
void setChannel(std::shared_ptr<Channel> newChannel); void setChannel(std::shared_ptr<Channel> newChannel);
void detachChannel(); void detachChannel();
void channelNameUpdated(const std::string &newChannelName);
messages::LimitedQueue<messages::SharedMessageRef> messages; messages::LimitedQueue<messages::SharedMessageRef> messages;
std::shared_ptr<Channel> channel; std::shared_ptr<Channel> channel;
QString channelName;
QVBoxLayout vbox; QVBoxLayout vbox;
ChatWidgetHeader header; ChatWidgetHeader header;

View file

@ -70,9 +70,13 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget)
void ChatWidgetHeader::updateChannelText() void ChatWidgetHeader::updateChannelText()
{ {
const QString &c = this->chatWidget->getChannelName(); const std::string channelName = this->chatWidget->channelName;
this->channelNameLabel.setText(c.isEmpty() ? "<no channel>" : c); if (channelName.empty()) {
this->channelNameLabel.setText("<no channel>");
} else {
this->channelNameLabel.setText(QString::fromStdString(channelName));
}
} }
void ChatWidgetHeader::paintEvent(QPaintEvent *) void ChatWidgetHeader::paintEvent(QPaintEvent *)