2018-01-17 01:19:42 +01:00
|
|
|
#include "widgets/helper/notebooktab.hpp"
|
2017-12-22 14:44:31 +01:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "debug/log.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
2018-01-02 02:15:11 +01:00
|
|
|
#include "singletons/thememanager.hpp"
|
2017-12-22 14:44:31 +01:00
|
|
|
#include "util/helpers.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/notebook.hpp"
|
2018-01-02 02:15:11 +01:00
|
|
|
#include "widgets/settingsdialog.hpp"
|
2017-08-13 16:52:16 +02:00
|
|
|
#include "widgets/textinputdialog.hpp"
|
2016-12-29 17:31:07 +01:00
|
|
|
|
2017-08-17 19:15:03 +02:00
|
|
|
#include <QApplication>
|
2017-08-13 16:52:16 +02:00
|
|
|
#include <QDebug>
|
2017-01-15 16:38:30 +01:00
|
|
|
#include <QPainter>
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2017-12-28 18:15:27 +01:00
|
|
|
NotebookTab::NotebookTab(Notebook *_notebook, const std::string &_uuid)
|
2017-08-13 16:10:53 +02:00
|
|
|
: BaseWidget(_notebook)
|
2017-12-28 18:15:27 +01:00
|
|
|
, uuid(_uuid)
|
|
|
|
, settingRoot(fS("/containers/{}/tab", this->uuid))
|
2017-08-13 16:07:00 +02:00
|
|
|
, positionChangedAnimation(this, "pos")
|
|
|
|
, notebook(_notebook)
|
2017-12-22 14:44:31 +01:00
|
|
|
, title(fS("{}/title", this->settingRoot), "")
|
|
|
|
, useDefaultBehaviour(fS("{}/useDefaultBehaviour", this->settingRoot), true)
|
2017-08-13 16:52:16 +02:00
|
|
|
, menu(this)
|
2016-12-29 17:31:07 +01:00
|
|
|
{
|
2017-01-22 12:46:35 +01:00
|
|
|
this->setAcceptDrops(true);
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->positionChangedAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
|
2017-01-26 05:26:21 +01:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::SettingManager::getInstance().hideTabX.connect(
|
2017-12-17 03:37:46 +01:00
|
|
|
boost::bind(&NotebookTab::hideTabXChanged, this, _1), this->managedConnections);
|
2017-01-26 05:26:21 +01:00
|
|
|
|
2017-01-22 05:58:23 +01:00
|
|
|
this->setMouseTracking(true);
|
2017-08-13 16:52:16 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-22 14:44:31 +01:00
|
|
|
QAction *enableHighlightsOnNewMessageAction =
|
|
|
|
new QAction("Enable highlights on new message", &this->menu);
|
|
|
|
enableHighlightsOnNewMessageAction->setCheckable(true);
|
2017-08-17 19:15:03 +02:00
|
|
|
|
2017-12-22 14:44:31 +01:00
|
|
|
this->menu.addAction("Close", [=]() { this->notebook->removePage(this->page); });
|
|
|
|
|
|
|
|
this->menu.addAction(enableHighlightsOnNewMessageAction);
|
2017-08-13 16:52:16 +02:00
|
|
|
|
2018-01-25 20:49:49 +01:00
|
|
|
QObject::connect(enableHighlightsOnNewMessageAction, &QAction::toggled, [this](bool newValue) {
|
2017-12-22 14:44:31 +01:00
|
|
|
debug::Log("New value is {}", newValue); //
|
2017-08-13 16:52:16 +02:00
|
|
|
});
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 20:49:49 +01:00
|
|
|
void NotebookTab::themeRefreshEvent()
|
2016-12-30 18:00:25 +01:00
|
|
|
{
|
2018-01-25 20:49:49 +01:00
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotebookTab::updateSize()
|
|
|
|
{
|
|
|
|
float scale = getScale();
|
2017-12-22 14:44:31 +01:00
|
|
|
QString qTitle(qS(this->title));
|
2017-09-22 00:50:43 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
if (singletons::SettingManager::getInstance().hideTabX) {
|
2018-01-25 20:49:49 +01:00
|
|
|
this->resize((int)((fontMetrics().width(qTitle) + 16) * scale), (int)(24 * scale));
|
2017-01-22 05:58:23 +01:00
|
|
|
} else {
|
2018-01-25 20:49:49 +01:00
|
|
|
this->resize((int)((fontMetrics().width(qTitle) + 8 + 24) * scale), (int)(24 * scale));
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
2017-02-02 02:46:33 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if (this->parent() != nullptr) {
|
|
|
|
(static_cast<Notebook *>(this->parent()))->performLayout(true);
|
2017-02-02 02:46:33 +01:00
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 14:44:31 +01:00
|
|
|
QString NotebookTab::getTitle() const
|
2017-01-22 12:46:35 +01:00
|
|
|
{
|
2017-12-22 14:44:31 +01:00
|
|
|
return qS(this->title);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
void NotebookTab::setTitle(const QString &newTitle)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-22 14:44:31 +01:00
|
|
|
this->title = newTitle.toStdString();
|
2017-08-13 16:52:16 +02:00
|
|
|
|
2018-01-25 20:49:49 +01:00
|
|
|
this->updateSize();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
bool NotebookTab::isSelected() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
return this->selected;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotebookTab::setSelected(bool value)
|
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->selected = value;
|
|
|
|
|
2017-12-26 12:32:24 +01:00
|
|
|
this->highlightState = HighlightState::None;
|
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->update();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:05:14 +01:00
|
|
|
void NotebookTab::setHighlightState(HighlightState newHighlightStyle)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-26 12:32:24 +01:00
|
|
|
if (this->isSelected()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:17:26 +01:00
|
|
|
if (this->highlightState != HighlightState::Highlighted) {
|
|
|
|
this->highlightState = newHighlightStyle;
|
2017-08-13 16:07:00 +02:00
|
|
|
|
2018-01-17 17:17:26 +01:00
|
|
|
this->update();
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QRect NotebookTab::getDesiredRect() const
|
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
return QRect(positionAnimationDesiredPoint, size());
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotebookTab::hideTabXChanged(bool)
|
|
|
|
{
|
2018-01-25 20:49:49 +01:00
|
|
|
this->updateSize();
|
2017-08-13 16:07:00 +02:00
|
|
|
this->update();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotebookTab::moveAnimated(QPoint pos, bool animated)
|
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->positionAnimationDesiredPoint = pos;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
QWidget *w = this->window();
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if ((w != nullptr && !w->isVisible()) || !animated || !positionChangedAnimationRunning) {
|
|
|
|
this->move(pos);
|
|
|
|
|
|
|
|
this->positionChangedAnimationRunning = true;
|
2017-01-26 05:26:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if (this->positionChangedAnimation.endValue() == pos) {
|
2017-01-26 05:26:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->positionChangedAnimation.stop();
|
|
|
|
this->positionChangedAnimation.setDuration(75);
|
|
|
|
this->positionChangedAnimation.setStartValue(this->pos());
|
|
|
|
this->positionChangedAnimation.setEndValue(pos);
|
|
|
|
this->positionChangedAnimation.start();
|
2017-01-22 12:46:35 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::paintEvent(QPaintEvent *)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2018-01-02 02:15:11 +01:00
|
|
|
singletons::SettingManager &settingManager = singletons::SettingManager::getInstance();
|
2016-12-30 12:20:26 +01:00
|
|
|
QPainter painter(this);
|
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
// select the right tab colors
|
|
|
|
singletons::ThemeManager::TabColors colors;
|
2018-01-17 01:19:42 +01:00
|
|
|
singletons::ThemeManager::TabColors regular = this->themeManager.tabs.regular;
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if (this->selected) {
|
2018-01-02 02:15:11 +01:00
|
|
|
colors = this->themeManager.tabs.selected;
|
2017-12-26 12:05:14 +01:00
|
|
|
} else if (this->highlightState == HighlightState::Highlighted) {
|
2018-01-02 02:15:11 +01:00
|
|
|
colors = this->themeManager.tabs.highlighted;
|
2017-12-26 12:05:14 +01:00
|
|
|
} else if (this->highlightState == HighlightState::NewMessage) {
|
2018-01-02 02:15:11 +01:00
|
|
|
colors = this->themeManager.tabs.newMessage;
|
2017-01-11 18:52:09 +01:00
|
|
|
} else {
|
2018-01-02 02:15:11 +01:00
|
|
|
colors = this->themeManager.tabs.regular;
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
2016-12-30 18:00:25 +01:00
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
bool windowFocused = this->window() == QApplication::activeWindow();
|
|
|
|
// || SettingsDialog::getHandle() == QApplication::activeWindow();
|
|
|
|
|
2018-01-17 01:19:42 +01:00
|
|
|
painter.fillRect(rect(), this->mouseOver ? regular.backgrounds.hover
|
|
|
|
: (windowFocused ? regular.backgrounds.regular
|
|
|
|
: regular.backgrounds.unfocused));
|
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
// fill the tab background
|
2018-01-17 01:19:42 +01:00
|
|
|
painter.fillRect(rect(), this->mouseOver ? colors.backgrounds.hover
|
|
|
|
: (windowFocused ? colors.backgrounds.regular
|
|
|
|
: colors.backgrounds.unfocused));
|
2018-01-02 02:15:11 +01:00
|
|
|
|
|
|
|
// set the pen color
|
|
|
|
painter.setPen(colors.text);
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
// set area for text
|
2018-01-25 20:49:49 +01:00
|
|
|
float scale = this->getScale();
|
2018-01-02 02:15:11 +01:00
|
|
|
int rectW = (settingManager.hideTabX ? 0 : static_cast<int>(16) * scale);
|
2017-09-22 00:50:43 +02:00
|
|
|
QRect rect(0, 0, this->width() - rectW, this->height());
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
// draw text
|
2017-12-22 14:44:31 +01:00
|
|
|
painter.drawText(rect, this->getTitle(), QTextOption(Qt::AlignCenter));
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
// draw close x
|
|
|
|
if (!settingManager.hideTabX && (mouseOver || selected)) {
|
2017-08-13 16:07:00 +02:00
|
|
|
QRect xRect = this->getXRect();
|
|
|
|
if (mouseOverX) {
|
|
|
|
painter.fillRect(xRect, QColor(0, 0, 0, 64));
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if (mouseDownX) {
|
|
|
|
painter.fillRect(xRect, QColor(0, 0, 0, 64));
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:50:43 +02:00
|
|
|
int a = static_cast<int>(scale * 4);
|
|
|
|
|
|
|
|
painter.drawLine(xRect.topLeft() + QPoint(a, a), xRect.bottomRight() + QPoint(-a, -a));
|
|
|
|
painter.drawLine(xRect.topRight() + QPoint(-a, a), xRect.bottomLeft() + QPoint(a, -a));
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::mousePressEvent(QMouseEvent *event)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->mouseDown = true;
|
|
|
|
this->mouseDownX = this->getXRect().contains(event->pos());
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->update();
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2017-08-17 19:15:03 +02:00
|
|
|
this->notebook->select(page);
|
2017-08-13 16:52:16 +02:00
|
|
|
|
2017-08-17 19:15:03 +02:00
|
|
|
switch (event->button()) {
|
2017-08-13 16:52:16 +02:00
|
|
|
case Qt::RightButton: {
|
|
|
|
this->menu.popup(event->globalPos());
|
|
|
|
} break;
|
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->mouseDown = false;
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-08-17 19:15:03 +02:00
|
|
|
if (event->button() == Qt::MiddleButton) {
|
|
|
|
if (this->rect().contains(event->pos())) {
|
|
|
|
this->notebook->removePage(this->page);
|
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
} else {
|
2017-12-31 22:58:35 +01:00
|
|
|
if (!singletons::SettingManager::getInstance().hideTabX && this->mouseDownX &&
|
2017-08-17 19:15:03 +02:00
|
|
|
this->getXRect().contains(event->pos())) {
|
|
|
|
this->mouseDownX = false;
|
|
|
|
|
|
|
|
this->notebook->removePage(this->page);
|
|
|
|
} else {
|
|
|
|
this->update();
|
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::enterEvent(QEvent *)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->mouseOver = true;
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->update();
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::leaveEvent(QEvent *)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->mouseOverX = false;
|
|
|
|
this->mouseOver = false;
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->update();
|
2016-12-29 17:31:07 +01:00
|
|
|
}
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::dragEnterEvent(QDragEnterEvent *)
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2017-08-13 16:07:00 +02:00
|
|
|
this->notebook->select(this->page);
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void NotebookTab::mouseMoveEvent(QMouseEvent *event)
|
2017-01-22 05:58:23 +01:00
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
if (!singletons::SettingManager::getInstance().hideTabX) {
|
2017-08-13 16:07:00 +02:00
|
|
|
bool overX = this->getXRect().contains(event->pos());
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if (overX != this->mouseOverX) {
|
|
|
|
// Over X state has been changed (we either left or entered it;
|
|
|
|
this->mouseOverX = overX;
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
this->update();
|
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2018-01-22 21:31:45 +01:00
|
|
|
QPoint relPoint = this->mapToParent(event->pos());
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2018-01-22 21:31:45 +01:00
|
|
|
if (this->mouseDown && !this->getDesiredRect().contains(relPoint)) {
|
2017-01-22 12:46:35 +01:00
|
|
|
int index;
|
2018-01-22 21:31:45 +01:00
|
|
|
SplitContainer *clickedPage = notebook->tabAt(relPoint, index, this->width());
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-08-13 16:07:00 +02:00
|
|
|
if (clickedPage != nullptr && clickedPage != this->page) {
|
2018-01-22 21:31:45 +01:00
|
|
|
this->notebook->rearrangePage(this->page, index);
|
2017-01-22 12:46:35 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
2017-01-28 22:35:23 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|