From a4533ae92cc5d847bcf811b28d2e3efd77a9a103 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 13 Aug 2017 16:52:16 +0200 Subject: [PATCH] Implement tab renaming/default tab name Fix #13 --- src/widgets/notebookpage.cpp | 26 +++++++++++++++ src/widgets/notebookpage.hpp | 2 ++ src/widgets/notebooktab.cpp | 62 ++++++++++++++++++++++++++++++++++-- src/widgets/notebooktab.hpp | 9 +++++- 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/widgets/notebookpage.cpp b/src/widgets/notebookpage.cpp index 981205c9b..edc73eb90 100644 --- a/src/widgets/notebookpage.cpp +++ b/src/widgets/notebookpage.cpp @@ -50,6 +50,8 @@ std::pair NotebookPage::removeFromLayout(ChatWidget *widget) auto it = std::find(std::begin(this->chatWidgets), std::end(this->chatWidgets), widget); if (it != std::end(this->chatWidgets)) { this->chatWidgets.erase(it); + + this->refreshTitle(); } // remove from box and return location @@ -82,6 +84,9 @@ void NotebookPage::addToLayout(ChatWidget *widget, std::pair position = std::pair(-1, -1)) { this->chatWidgets.push_back(widget); + + this->refreshTitle(); + widget->giveFocus(Qt::MouseFocusReason); // add vbox at the end @@ -404,6 +409,27 @@ ChatWidget *NotebookPage::createChatWidget() return new ChatWidget(this->channelManager, this); } +void NotebookPage::refreshTitle() +{ + if (!this->tab->useDefaultBehaviour) { + return; + } + + QString newTitle = ""; + bool first = true; + + for (const auto &chatWidget : this->chatWidgets) { + if (!first) { + newTitle += ", "; + } + newTitle += QString::fromStdString(chatWidget->channelName.getValue()); + + first = false; + } + + this->tab->setTitle(newTitle); +} + void NotebookPage::load(const boost::property_tree::ptree &tree) { try { diff --git a/src/widgets/notebookpage.hpp b/src/widgets/notebookpage.hpp index bc367eccd..64bdee644 100644 --- a/src/widgets/notebookpage.hpp +++ b/src/widgets/notebookpage.hpp @@ -98,6 +98,8 @@ private: ChatWidget *createChatWidget(); public: + void refreshTitle(); + void load(const boost::property_tree::ptree &tree); boost::property_tree::ptree save(); }; diff --git a/src/widgets/notebooktab.cpp b/src/widgets/notebooktab.cpp index af17173cb..79caf4ab9 100644 --- a/src/widgets/notebooktab.cpp +++ b/src/widgets/notebooktab.cpp @@ -2,7 +2,9 @@ #include "colorscheme.hpp" #include "settingsmanager.hpp" #include "widgets/notebook.hpp" +#include "widgets/textinputdialog.hpp" +#include #include namespace chatterino { @@ -12,6 +14,7 @@ NotebookTab::NotebookTab(Notebook *_notebook) : BaseWidget(_notebook) , positionChangedAnimation(this, "pos") , notebook(_notebook) + , menu(this) { this->calcSize(); this->setAcceptDrops(true); @@ -22,6 +25,37 @@ NotebookTab::NotebookTab(Notebook *_notebook) boost::bind(&NotebookTab::hideTabXChanged, this, _1)); this->setMouseTracking(true); + + this->menu.addAction("Rename", [this]() { + TextInputDialog d(this); + + d.setWindowTitle("Change tab title (Leave empty for default behaviour)"); + if (this->useDefaultBehaviour) { + d.setText(""); + } else { + d.setText(this->getTitle()); + } + + if (d.exec() == QDialog::Accepted) { + QString newTitle = d.getText(); + if (newTitle.isEmpty()) { + this->useDefaultBehaviour = true; + this->page->refreshTitle(); + } else { + this->useDefaultBehaviour = false; + this->setTitle(newTitle); + } + } + }); + + this->menu.addAction("Close", [=]() { + // this->notebook->removePage(this->page); // + qDebug() << "TODO: Implement"; // + }); + + this->menu.addAction("Enable highlights on new message", []() { + qDebug() << "TODO: Implement"; // + }); } NotebookTab::~NotebookTab() @@ -50,6 +84,8 @@ const QString &NotebookTab::getTitle() const void NotebookTab::setTitle(const QString &newTitle) { this->title = newTitle; + + this->calcSize(); } bool NotebookTab::isSelected() const @@ -163,7 +199,17 @@ void NotebookTab::mousePressEvent(QMouseEvent *event) this->update(); - this->notebook->select(page); + switch (event->button()) { + case Qt::LeftButton: { + this->notebook->select(page); + } break; + + case Qt::RightButton: { + this->notebook->select(page); + + this->menu.popup(event->globalPos()); + } break; + } } void NotebookTab::mouseReleaseEvent(QMouseEvent *event) @@ -229,7 +275,13 @@ void NotebookTab::load(const boost::property_tree::ptree &tree) { // Load tab title try { - this->setTitle(QString::fromStdString(tree.get("title"))); + QString newTitle = QString::fromStdString(tree.get("title")); + if (newTitle.isEmpty()) { + this->useDefaultBehaviour = true; + } else { + this->setTitle(newTitle); + this->useDefaultBehaviour = false; + } } catch (boost::property_tree::ptree_error) { } } @@ -238,7 +290,11 @@ boost::property_tree::ptree NotebookTab::save() { boost::property_tree::ptree tree; - tree.put("title", this->getTitle().toStdString()); + if (this->useDefaultBehaviour) { + tree.put("title", ""); + } else { + tree.put("title", this->getTitle().toStdString()); + } return tree; } diff --git a/src/widgets/notebooktab.hpp b/src/widgets/notebooktab.hpp index 36b06b49f..3ba17c636 100644 --- a/src/widgets/notebooktab.hpp +++ b/src/widgets/notebooktab.hpp @@ -2,6 +2,7 @@ #include "widgets/basewidget.hpp" +#include #include #include #include @@ -64,8 +65,12 @@ private: Notebook *notebook; - QString title = ""; + QString title; +public: + bool useDefaultBehaviour = true; + +private: bool selected = false; bool mouseOver = false; bool mouseDown = false; @@ -74,6 +79,8 @@ private: HighlightStyle highlightStyle = HighlightStyle::HighlightNone; + QMenu menu; + QRect getXRect() { return QRect(this->width() - 20, 4, 16, 16);