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

432 lines
12 KiB
C++
Raw Normal View History

#include "widgets/helper/notebooktab.hpp"
#include "application.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>
2018-03-30 16:25:49 +02:00
#include <QLinearGradient>
2017-01-15 16:38:30 +01:00
#include <QPainter>
#include <boost/bind.hpp>
2017-01-15 16:38:30 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2018-05-23 11:59:37 +02:00
NotebookTab::NotebookTab(Notebook *_notebook)
2018-04-18 09:12:29 +02:00
: BaseWidget(_notebook)
, positionChangedAnimation(this, "pos")
, notebook(_notebook)
, menu(this)
{
auto app = getApp();
2018-04-18 09:12:29 +02:00
this->setAcceptDrops(true);
this->positionChangedAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
2018-05-23 11:59:37 +02:00
app->settings->showTabCloseButton.connect(boost::bind(&NotebookTab::hideTabXChanged, this, _1),
this->managedConnections);
2018-04-18 09:12:29 +02:00
this->setMouseTracking(true);
this->menu.addAction("Rename", [this]() {
TextInputDialog d(this);
d.setWindowTitle("Change tab title (Leave empty for default behaviour)");
if (this->useDefaultTitle) {
d.setText("");
} else {
d.setText(this->getTitle());
d.highlightText();
}
if (d.exec() == QDialog::Accepted) {
QString newTitle = d.getText();
if (newTitle.isEmpty()) {
this->useDefaultTitle = true;
// fourtf: xD
// this->page->refreshTitle();
} else {
this->useDefaultTitle = false;
this->setTitle(newTitle);
}
}
});
2018-05-23 04:22:17 +02:00
// QAction *enableHighlightsOnNewMessageAction =
// new QAction("Enable highlights on new message", &this->menu);
// enableHighlightsOnNewMessageAction->setCheckable(true);
2018-04-18 09:12:29 +02:00
this->menu.addAction("Close", [=]() { this->notebook->removePage(this->page); });
2018-05-23 04:22:17 +02:00
// this->menu.addAction(enableHighlightsOnNewMessageAction);
2018-04-18 09:12:29 +02:00
2018-05-23 04:22:17 +02:00
// QObject::connect(enableHighlightsOnNewMessageAction, &QAction::toggled, [this](bool
// newValue) {
// debug::Log("New value is {}", newValue); //
// });
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
void NotebookTab::themeRefreshEvent()
2018-04-18 09:12:29 +02:00
{
this->update();
}
2018-05-23 11:59:37 +02:00
void NotebookTab::updateSize()
2018-04-18 09:12:29 +02:00
{
auto app = getApp();
2018-04-18 09:12:29 +02:00
float scale = getScale();
int width;
2018-05-25 13:53:55 +02:00
QFontMetrics metrics = getApp()->fonts->getFontMetrics(
FontStyle::UiTabs, this->getScale() * this->devicePixelRatioF());
2018-04-18 09:12:29 +02:00
2018-05-23 04:22:17 +02:00
if (this->hasXButton()) {
width = (int)((metrics.width(this->title) + 32) * scale);
2018-04-18 09:12:29 +02:00
} else {
2018-05-23 04:22:17 +02:00
width = (int)((metrics.width(this->title) + 16) * scale);
2018-04-18 09:12:29 +02:00
}
2018-05-23 04:22:17 +02:00
width = std::min((int)(150 * scale), width);
if (this->width() != width) {
this->resize(width, (int)(NOTEBOOK_TAB_HEIGHT * scale));
this->notebook->performLayout();
}
2018-04-18 09:12:29 +02:00
// if (this->parent() != nullptr) {
// (static_cast<Notebook2 *>(this->parent()))->performLayout(true);
// }
}
2018-05-23 11:59:37 +02:00
const QString &NotebookTab::getTitle() const
2018-04-18 09:12:29 +02:00
{
return this->title;
}
2018-05-23 11:59:37 +02:00
void NotebookTab::setTitle(const QString &newTitle)
2018-04-18 09:12:29 +02:00
{
if (this->title != newTitle) {
this->title = newTitle;
this->updateSize();
this->update();
}
}
2018-05-23 11:59:37 +02:00
bool NotebookTab::isSelected() const
2018-04-18 09:12:29 +02:00
{
return this->selected;
}
2018-05-23 11:59:37 +02:00
void NotebookTab::setSelected(bool value)
2018-04-18 09:12:29 +02:00
{
this->selected = value;
this->highlightState = HighlightState::None;
this->update();
}
2018-05-23 11:59:37 +02:00
void NotebookTab::setHighlightState(HighlightState newHighlightStyle)
2018-04-18 09:12:29 +02:00
{
if (this->isSelected()) {
return;
}
if (this->highlightState != HighlightState::Highlighted) {
this->highlightState = newHighlightStyle;
this->update();
}
}
2018-05-23 11:59:37 +02:00
QRect NotebookTab::getDesiredRect() const
2018-04-18 09:12:29 +02:00
{
return QRect(positionAnimationDesiredPoint, size());
}
2018-05-23 11:59:37 +02:00
void NotebookTab::hideTabXChanged(bool)
2018-04-18 09:12:29 +02:00
{
this->updateSize();
this->update();
}
2018-05-23 11:59:37 +02:00
void NotebookTab::moveAnimated(QPoint pos, bool animated)
2018-04-18 09:12:29 +02:00
{
this->positionAnimationDesiredPoint = pos;
QWidget *w = this->window();
if ((w != nullptr && !w->isVisible()) || !animated || !positionChangedAnimationRunning) {
this->move(pos);
this->positionChangedAnimationRunning = true;
return;
}
if (this->positionChangedAnimation.endValue() == pos) {
return;
}
this->positionChangedAnimation.stop();
this->positionChangedAnimation.setDuration(75);
this->positionChangedAnimation.setStartValue(this->pos());
this->positionChangedAnimation.setEndValue(pos);
this->positionChangedAnimation.start();
}
2018-05-23 11:59:37 +02:00
void NotebookTab::paintEvent(QPaintEvent *)
2018-04-18 09:12:29 +02:00
{
auto app = getApp();
2018-04-18 09:12:29 +02:00
QPainter painter(this);
float scale = this->getScale();
painter.setFont(getApp()->fonts->getFont(FontStyle::UiTabs, scale * this->devicePixelRatioF()));
2018-05-25 13:53:55 +02:00
QFontMetrics metrics =
app->fonts->getFontMetrics(FontStyle::UiTabs, scale * this->devicePixelRatioF());
2018-05-23 04:22:17 +02:00
2018-05-25 13:53:55 +02:00
int height = int(scale * NOTEBOOK_TAB_HEIGHT);
2018-04-18 09:12:29 +02:00
// int fullHeight = (int)(scale * 48);
// select the right tab colors
singletons::ThemeManager::TabColors colors;
singletons::ThemeManager::TabColors regular = this->themeManager->tabs.regular;
2018-04-18 09:12:29 +02:00
if (this->selected) {
colors = this->themeManager->tabs.selected;
2018-04-18 09:12:29 +02:00
} else if (this->highlightState == HighlightState::Highlighted) {
colors = this->themeManager->tabs.highlighted;
2018-04-18 09:12:29 +02:00
} else if (this->highlightState == HighlightState::NewMessage) {
colors = this->themeManager->tabs.newMessage;
2018-04-18 09:12:29 +02:00
} else {
colors = this->themeManager->tabs.regular;
2018-04-18 09:12:29 +02:00
}
bool windowFocused = this->window() == QApplication::activeWindow();
// || SettingsDialog::getHandle() == QApplication::activeWindow();
QBrush tabBackground = this->mouseOver ? colors.backgrounds.hover
: (windowFocused ? colors.backgrounds.regular
: colors.backgrounds.unfocused);
2018-05-23 04:22:17 +02:00
painter.fillRect(rect(), this->mouseOver ? regular.backgrounds.hover
: (windowFocused ? regular.backgrounds.regular
: regular.backgrounds.unfocused));
// fill the tab background
painter.fillRect(rect(), tabBackground);
// draw border
// painter.setPen(QPen("#fff"));
// QPainterPath path(QPointF(0, height));
// path.lineTo(0, 0);
// path.lineTo(this->width() - 1, 0);
// path.lineTo(this->width() - 1, this->height() - 1);
// path.lineTo(0, this->height() - 1);
// painter.drawPath(path);
// top line
painter.fillRect(QRectF(0, (this->selected ? 0.f : 1.f) * scale, this->width(),
(this->selected ? 2.f : 1.f) * scale),
this->mouseOver
? colors.line.hover
: (windowFocused ? colors.line.regular : colors.line.unfocused));
2018-04-18 09:12:29 +02:00
// set the pen color
painter.setPen(colors.text);
// set area for text
2018-05-25 13:53:55 +02:00
int rectW = (!app->settings->showTabCloseButton ? 0 : int(16 * scale));
2018-04-18 09:12:29 +02:00
QRect rect(0, 0, this->width() - rectW, height);
// draw text
if (true) { // legacy
// painter.drawText(rect, this->getTitle(), QTextOption(Qt::AlignCenter));
2018-05-25 13:53:55 +02:00
int offset = int(scale * 8);
QRect textRect(offset, this->selected ? 1 : 2, this->width() - offset - offset, height);
2018-05-23 04:22:17 +02:00
if (this->shouldDrawXButton()) {
textRect.setRight(textRect.right() - this->height() / 2);
}
2018-04-18 09:12:29 +02:00
2018-05-25 13:53:55 +02:00
int width = metrics.width(this->getTitle());
Qt::Alignment alignment = width > textRect.width() ? Qt::AlignLeft | Qt::AlignVCenter
: Qt::AlignHCenter | Qt::AlignVCenter;
QTextOption option(alignment);
2018-04-18 09:12:29 +02:00
option.setWrapMode(QTextOption::NoWrap);
painter.drawText(textRect, this->getTitle(), option);
} else {
// QTextOption option(Qt::AlignLeft | Qt::AlignVCenter);
// option.setWrapMode(QTextOption::NoWrap);
// int offset = (int)(scale * 16);
// QRect textRect(offset, 0, this->width() - offset - offset, height);
// painter.drawText(textRect, this->getTitle(), option);
}
// draw close x
2018-05-23 04:22:17 +02:00
if (this->shouldDrawXButton()) {
2018-04-18 09:12:29 +02:00
QRect xRect = this->getXRect();
if (!xRect.isNull()) {
2018-05-23 04:22:17 +02:00
painter.setBrush(QColor("#fff"));
2018-04-18 09:12:29 +02:00
if (mouseOverX) {
painter.fillRect(xRect, QColor(0, 0, 0, 64));
if (mouseDownX) {
painter.fillRect(xRect, QColor(0, 0, 0, 64));
}
}
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));
}
}
2018-05-23 04:22:17 +02:00
// draw line at bottom
if (!this->selected) {
painter.fillRect(0, this->height() - 1, this->width(), 1, app->themes->window.background);
}
}
2018-05-23 11:59:37 +02:00
bool NotebookTab::hasXButton()
2018-05-23 04:22:17 +02:00
{
return getApp()->settings->showTabCloseButton && this->notebook->getAllowUserTabManagement();
}
2018-05-23 11:59:37 +02:00
bool NotebookTab::shouldDrawXButton()
2018-05-23 04:22:17 +02:00
{
return this->hasXButton() && (mouseOver || selected);
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
void NotebookTab::mousePressEvent(QMouseEvent *event)
2018-04-18 09:12:29 +02:00
{
this->mouseDown = true;
this->mouseDownX = this->getXRect().contains(event->pos());
this->update();
this->notebook->select(page);
if (this->notebook->getAllowUserTabManagement()) {
switch (event->button()) {
case Qt::RightButton: {
this->menu.popup(event->globalPos());
} break;
}
}
}
2018-05-23 11:59:37 +02:00
void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
2018-04-18 09:12:29 +02:00
{
this->mouseDown = false;
auto removeThisPage = [this] {
auto reply = QMessageBox::question(this, "Remove this tab",
"Are you sure that you want to remove this tab?",
QMessageBox::Yes | QMessageBox::Cancel);
if (reply == QMessageBox::Yes) {
this->notebook->removePage(this->page);
}
};
2018-04-18 09:12:29 +02:00
if (event->button() == Qt::MiddleButton) {
if (this->rect().contains(event->pos())) {
removeThisPage();
2018-04-18 09:12:29 +02:00
}
} else {
2018-05-23 04:22:17 +02:00
if (this->hasXButton() && this->mouseDownX && this->getXRect().contains(event->pos())) {
2018-04-18 09:12:29 +02:00
this->mouseDownX = false;
removeThisPage();
2018-04-18 09:12:29 +02:00
} else {
this->update();
}
}
}
2018-05-23 11:59:37 +02:00
void NotebookTab::enterEvent(QEvent *)
2018-04-18 09:12:29 +02:00
{
this->mouseOver = true;
this->update();
}
2018-05-23 11:59:37 +02:00
void NotebookTab::leaveEvent(QEvent *)
2018-04-18 09:12:29 +02:00
{
this->mouseOverX = false;
this->mouseOver = false;
this->update();
}
2018-05-23 12:35:10 +02:00
void NotebookTab::dragEnterEvent(QDragEnterEvent *event)
2018-04-18 09:12:29 +02:00
{
2018-05-23 12:35:10 +02:00
if (!event->mimeData()->hasFormat("chatterino/split"))
return;
if (!SplitContainer::isDraggingSplit)
return;
2018-04-18 18:55:49 +02:00
if (this->notebook->getAllowUserTabManagement()) {
this->notebook->select(this->page);
}
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
void NotebookTab::mouseMoveEvent(QMouseEvent *event)
2018-04-18 09:12:29 +02:00
{
auto app = getApp();
if (app->settings->showTabCloseButton && this->notebook->getAllowUserTabManagement()) //
2018-04-18 09:12:29 +02:00
{
bool overX = this->getXRect().contains(event->pos());
if (overX != this->mouseOverX) {
// Over X state has been changed (we either left or entered it;
this->mouseOverX = overX;
this->update();
}
}
QPoint relPoint = this->mapToParent(event->pos());
if (this->mouseDown && !this->getDesiredRect().contains(relPoint) &&
this->notebook->getAllowUserTabManagement()) //
{
int index;
QWidget *clickedPage = notebook->tabAt(relPoint, index, this->width());
2018-05-23 04:22:17 +02:00
// assert(clickedPage);
2018-04-18 09:12:29 +02:00
if (clickedPage != nullptr && clickedPage != this->page) {
this->notebook->rearrangePage(this->page, index);
}
}
}
2018-05-23 11:59:37 +02:00
QRect NotebookTab::getXRect()
2018-04-18 09:12:29 +02:00
{
2018-05-23 04:22:17 +02:00
// if (!this->notebook->getAllowUserTabManagement()) {
// return QRect();
// }
2018-04-18 09:12:29 +02:00
float s = this->getScale();
2018-05-23 04:22:17 +02:00
return QRect(this->width() - static_cast<int>(20 * s), static_cast<int>(6 * s),
2018-04-18 09:12:29 +02:00
static_cast<int>(16 * s), static_cast<int>(16 * s));
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino