mirror-chatterino2/src/widgets/helper/notebooktab.cpp

302 lines
8.2 KiB
C++
Raw Normal View History

2017-11-12 17:21:50 +01:00
#include "widgets/helper/notebooktab.hpp"
#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"
#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"
#include "widgets/textinputdialog.hpp"
2016-12-29 17:31:07 +01:00
#include <QApplication>
#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
NotebookTab::NotebookTab(Notebook *_notebook, const std::string &_uuid)
2017-08-13 16:10:53 +02:00
: BaseWidget(_notebook)
, uuid(_uuid)
, settingRoot(fS("/containers/{}/tab", this->uuid))
2017-08-13 16:07:00 +02:00
, positionChangedAnimation(this, "pos")
, notebook(_notebook)
, title(fS("{}/title", this->settingRoot), "")
, useDefaultBehaviour(fS("{}/useDefaultBehaviour", this->settingRoot), true)
, menu(this)
2016-12-29 17:31:07 +01:00
{
2017-01-22 12:46:35 +01:00
this->calcSize();
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(
boost::bind(&NotebookTab::hideTabXChanged, this, _1), this->managedConnections);
2017-01-26 05:26:21 +01:00
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);
}
}
});
QAction *enableHighlightsOnNewMessageAction =
new QAction("Enable highlights on new message", &this->menu);
enableHighlightsOnNewMessageAction->setCheckable(true);
this->menu.addAction("Close", [=]() { this->notebook->removePage(this->page); });
this->menu.addAction(enableHighlightsOnNewMessageAction);
connect(enableHighlightsOnNewMessageAction, &QAction::toggled, [this](bool newValue) {
debug::Log("New value is {}", newValue); //
});
}
2017-04-12 17:46:44 +02:00
void NotebookTab::calcSize()
2016-12-30 18:00:25 +01:00
{
2017-09-22 00:50:43 +02:00
float scale = getDpiMultiplier();
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) {
this->resize(static_cast<int>((fontMetrics().width(qTitle) + 16) * scale),
2017-09-22 00:50:43 +02:00
static_cast<int>(24 * scale));
} else {
this->resize(static_cast<int>((fontMetrics().width(qTitle) + 8 + 24) * scale),
2017-09-22 00:50:43 +02:00
static_cast<int>(24 * scale));
}
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
}
QString NotebookTab::getTitle() const
2017-01-22 12:46:35 +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
{
this->title = newTitle.toStdString();
this->calcSize();
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;
this->highlightState = HighlightState::None;
2017-08-13 16:07:00 +02:00
this->update();
2017-04-12 17:46:44 +02:00
}
void NotebookTab::setHighlightState(HighlightState newHighlightStyle)
2017-04-12 17:46:44 +02:00
{
if (this->isSelected()) {
return;
}
this->highlightState = newHighlightStyle;
2017-08-13 16:07:00 +02: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)
{
2017-08-13 16:07:00 +02:00
this->calcSize();
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;
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;
} else if (this->highlightState == HighlightState::Highlighted) {
2018-01-02 02:15:11 +01:00
colors = this->themeManager.tabs.highlighted;
} 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();
// fill the tab background
painter.fillRect(rect(),
2018-01-02 02:21:38 +01:00
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);
2018-01-02 02:15:11 +01:00
// set area for text
2017-09-22 00:50:43 +02:00
float scale = this->getDpiMultiplier();
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());
2018-01-02 02:15:11 +01:00
// draw text
painter.drawText(rect, this->getTitle(), QTextOption(Qt::AlignCenter));
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-08-13 16:07:00 +02:00
if (mouseDownX) {
painter.fillRect(xRect, QColor(0, 0, 0, 64));
}
}
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));
}
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
this->notebook->select(page);
switch (event->button()) {
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
if (event->button() == Qt::MiddleButton) {
if (this->rect().contains(event->pos())) {
this->notebook->removePage(this->page);
}
} else {
2017-12-31 22:58:35 +01:00
if (!singletons::SettingManager::getInstance().hideTabX && this->mouseDownX &&
this->getXRect().contains(event->pos())) {
this->mouseDownX = false;
this->notebook->removePage(this->page);
} else {
this->update();
}
}
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-04-12 17:46:44 +02:00
void NotebookTab::mouseMoveEvent(QMouseEvent *event)
{
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-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-08-13 16:07:00 +02:00
this->update();
}
}
2017-01-22 12:46:35 +01:00
2017-08-13 16:07:00 +02:00
if (this->mouseDown && !this->getDesiredRect().contains(event->pos())) {
QPoint relPoint = this->mapToParent(event->pos());
2017-01-22 12:46:35 +01:00
int index;
2017-11-12 17:21:50 +01:00
SplitContainer *clickedPage = notebook->tabAt(relPoint, index);
2017-01-22 12:46:35 +01:00
2017-08-13 16:07:00 +02:00
if (clickedPage != nullptr && clickedPage != this->page) {
this->notebook->rearrangePage(clickedPage, index);
2017-01-22 12:46:35 +01:00
}
}
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino