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

457 lines
13 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "widgets/helper/NotebookTab.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2018-06-26 15:33:51 +02:00
#include "common/Common.hpp"
2018-06-26 14:09:39 +02:00
#include "debug/Log.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
#include "singletons/Themes.hpp"
2018-06-26 14:09:39 +02:00
#include "util/Clamp.hpp"
#include "util/Helpers.hpp"
#include "widgets/Notebook.hpp"
2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/SettingsDialog.hpp"
#include "widgets/dialogs/TextInputDialog.hpp"
2016-12-29 17:31:07 +01:00
#include <QApplication>
#include <QDebug>
2018-03-30 16:25:49 +02:00
#include <QLinearGradient>
2018-06-01 14:57:18 +02:00
#include <QMimeData>
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 {
2017-01-18 21:30:23 +01:00
2018-06-01 14:57:18 +02:00
NotebookTab::NotebookTab(Notebook *notebook)
2018-06-11 21:57:17 +02:00
: RippleEffectButton(notebook)
2018-06-01 14:57:18 +02:00
, positionChangedAnimation_(this, "pos")
, notebook_(notebook)
, menu_(this)
2018-04-18 09:12:29 +02:00
{
auto app = getApp();
2018-04-18 09:12:29 +02:00
this->setAcceptDrops(true);
2018-06-01 14:57:18 +02:00
this->positionChangedAnimation_.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
2018-04-18 09:12:29 +02:00
2018-05-23 11:59:37 +02:00
app->settings->showTabCloseButton.connect(boost::bind(&NotebookTab::hideTabXChanged, this, _1),
2018-06-01 14:57:18 +02:00
this->managedConnections_);
2018-04-18 09:12:29 +02:00
this->setMouseTracking(true);
2018-06-01 14:57:18 +02:00
this->menu_.addAction("Rename", [this]() {
2018-04-18 09:12:29 +02:00
TextInputDialog d(this);
d.setWindowTitle("Change tab title (Leave empty for default behaviour)");
2018-06-01 16:01:49 +02:00
d.setText(this->getCustomTitle());
d.highlightText();
2018-04-18 09:12:29 +02:00
if (d.exec() == QDialog::Accepted) {
QString newTitle = d.getText();
2018-06-01 16:01:49 +02:00
this->setCustomTitle(newTitle);
2018-04-18 09:12:29 +02:00
}
});
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
2018-06-01 14:57:18 +02:00
this->menu_.addAction("Close", [=]() { this->notebook_->removePage(this->page); });
2018-04-18 09:12:29 +02:00
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) {
2018-06-26 17:06:17 +02:00
// Log("New value is {}", newValue); //
2018-05-23 04:22:17 +02:00
// });
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-06-11 21:57:17 +02:00
// this->setMouseEffectColor(QColor("#999"));
this->setMouseEffectColor(this->themeManager->tabs.regular.text);
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
void NotebookTab::updateSize()
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(
2018-06-01 14:57:18 +02:00
FontStyle::UiTabs, float(qreal(this->getScale()) * this->devicePixelRatioF()));
2018-04-18 09:12:29 +02:00
2018-05-23 04:22:17 +02:00
if (this->hasXButton()) {
2018-06-11 15:04:54 +02:00
width = (metrics.width(this->getTitle()) + int(32 * scale));
2018-04-18 09:12:29 +02:00
} else {
2018-06-11 15:04:54 +02:00
width = (metrics.width(this->getTitle()) + int(16 * scale));
2018-04-18 09:12:29 +02:00
}
2018-06-26 17:06:17 +02:00
width = clamp(width, this->height(), int(150 * scale));
2018-05-23 04:22:17 +02:00
if (this->width() != width) {
2018-06-01 14:50:35 +02:00
this->resize(width, int(NOTEBOOK_TAB_HEIGHT * scale));
2018-06-01 14:57:18 +02:00
this->notebook_->performLayout();
2018-05-23 04:22:17 +02:00
}
2018-04-18 09:12:29 +02:00
}
2018-06-01 16:01:49 +02:00
const QString &NotebookTab::getCustomTitle() const
2018-04-18 09:12:29 +02:00
{
2018-06-01 16:01:49 +02:00
return this->customTitle_;
2018-04-18 09:12:29 +02:00
}
2018-06-01 16:01:49 +02:00
void NotebookTab::setCustomTitle(const QString &newTitle)
2018-04-18 09:12:29 +02:00
{
2018-06-01 16:01:49 +02:00
if (this->customTitle_ != newTitle) {
this->customTitle_ = newTitle;
this->titleUpdated();
2018-04-18 09:12:29 +02:00
}
}
2018-06-01 16:01:49 +02:00
void NotebookTab::resetCustomTitle()
{
this->setCustomTitle(QString());
}
bool NotebookTab::hasCustomTitle() const
{
return !this->customTitle_.isEmpty();
}
void NotebookTab::setDefaultTitle(const QString &title)
{
if (this->defaultTitle_ != title) {
this->defaultTitle_ = title;
if (this->customTitle_.isEmpty()) {
this->titleUpdated();
}
}
}
const QString &NotebookTab::getDefaultTitle() const
{
return this->defaultTitle_;
}
const QString &NotebookTab::getTitle() const
{
return this->customTitle_.isEmpty() ? this->defaultTitle_ : this->customTitle_;
}
void NotebookTab::titleUpdated()
{
this->updateSize();
this->update();
}
2018-05-23 11:59:37 +02:00
bool NotebookTab::isSelected() const
2018-04-18 09:12:29 +02:00
{
2018-06-01 14:57:18 +02:00
return this->selected_;
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
void NotebookTab::setSelected(bool value)
2018-04-18 09:12:29 +02:00
{
2018-06-01 14:57:18 +02:00
this->selected_ = value;
2018-04-18 09:12:29 +02:00
2018-06-01 14:57:18 +02:00
this->highlightState_ = HighlightState::None;
2018-04-18 09:12:29 +02:00
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;
}
2018-06-01 14:57:18 +02:00
if (this->highlightState_ != HighlightState::Highlighted) {
this->highlightState_ = newHighlightStyle;
2018-04-18 09:12:29 +02:00
this->update();
}
}
2018-05-23 11:59:37 +02:00
QRect NotebookTab::getDesiredRect() const
2018-04-18 09:12:29 +02:00
{
2018-06-01 14:57:18 +02:00
return QRect(this->positionAnimationDesiredPoint_, size());
2018-04-18 09:12:29 +02:00
}
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
{
2018-06-01 14:57:18 +02:00
this->positionAnimationDesiredPoint_ = pos;
2018-04-18 09:12:29 +02:00
QWidget *w = this->window();
2018-06-01 14:57:18 +02:00
if ((w != nullptr && !w->isVisible()) || !animated || !this->positionChangedAnimationRunning_) {
2018-04-18 09:12:29 +02:00
this->move(pos);
2018-06-01 14:57:18 +02:00
this->positionChangedAnimationRunning_ = true;
2018-04-18 09:12:29 +02:00
return;
}
2018-06-01 14:57:18 +02:00
if (this->positionChangedAnimation_.endValue() == pos) {
2018-04-18 09:12:29 +02:00
return;
}
2018-06-01 14:57:18 +02:00
this->positionChangedAnimation_.stop();
this->positionChangedAnimation_.setDuration(75);
this->positionChangedAnimation_.setStartValue(this->pos());
this->positionChangedAnimation_.setEndValue(pos);
this->positionChangedAnimation_.start();
2018-04-18 09:12:29 +02:00
}
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();
2018-06-24 18:30:48 +02:00
painter.setFont(getApp()->fonts->getFont(
FontStyle::UiTabs, scale * 96.f / this->logicalDpiX() * this->devicePixelRatioF()));
QFontMetrics metrics = app->fonts->getFontMetrics(
FontStyle::UiTabs, scale * 96.f / this->logicalDpiX() * 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
ThemeManager::TabColors colors;
ThemeManager::TabColors regular = this->themeManager->tabs.regular;
2018-04-18 09:12:29 +02:00
2018-06-01 14:57:18 +02:00
if (this->selected_) {
colors = this->themeManager->tabs.selected;
2018-06-01 14:57:18 +02:00
} else if (this->highlightState_ == HighlightState::Highlighted) {
colors = this->themeManager->tabs.highlighted;
2018-06-01 14:57:18 +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();
2018-06-11 21:57:17 +02:00
QBrush tabBackground = /*this->mouseOver_ ? colors.backgrounds.hover
:*/ (windowFocused ? colors.backgrounds.regular
: colors.backgrounds.unfocused);
2018-04-18 09:12:29 +02:00
// painter.fillRect(rect(), this->mouseOver_ ? regular.backgrounds.hover
// : (windowFocused ? regular.backgrounds.regular
// :
// regular.backgrounds.unfocused));
2018-05-23 04:22:17 +02:00
// fill the tab background
auto bgRect = rect();
bgRect.setTop(bgRect.top() + 2);
painter.fillRect(bgRect, tabBackground);
2018-05-23 04:22:17 +02:00
// 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
2018-06-01 14:57:18 +02:00
painter.fillRect(QRectF(0, (this->selected_ ? 0.f : 1.f) * scale, this->width(),
(this->selected_ ? 2.f : 1.f) * scale),
this->mouseOver_
2018-05-23 04:22:17 +02:00
? 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
2018-06-01 14:57:18 +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
2018-06-01 14:57:18 +02:00
if (this->shouldDrawXButton()) {
textRect.setRight(textRect.right() - this->height() / 2);
}
2018-04-18 09:12:29 +02:00
2018-06-01 14:57:18 +02:00
int width = metrics.width(this->getTitle());
Qt::Alignment alignment = width > textRect.width() ? Qt::AlignLeft | Qt::AlignVCenter
: Qt::AlignHCenter | Qt::AlignVCenter;
2018-05-25 13:53:55 +02:00
2018-06-01 14:57:18 +02:00
QTextOption option(alignment);
option.setWrapMode(QTextOption::NoWrap);
painter.drawText(textRect, this->getTitle(), option);
2018-04-18 09:12:29 +02:00
// 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-06-01 14:57:18 +02:00
if (this->mouseOverX_) {
2018-04-18 09:12:29 +02:00
painter.fillRect(xRect, QColor(0, 0, 0, 64));
2018-06-01 14:57:18 +02:00
if (this->mouseDownX_) {
2018-04-18 09:12:29 +02:00
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
2018-06-01 14:57:18 +02:00
if (!this->selected_) {
2018-05-23 04:22:17 +02:00
painter.fillRect(0, this->height() - 1, this->width(), 1, app->themes->window.background);
2018-06-11 21:57:17 +02:00
this->fancyPaint(painter);
2018-05-23 04:22:17 +02:00
}
}
2018-05-23 11:59:37 +02:00
bool NotebookTab::hasXButton()
2018-05-23 04:22:17 +02:00
{
2018-06-01 14:57:18 +02:00
return getApp()->settings->showTabCloseButton && this->notebook_->getAllowUserTabManagement();
2018-05-23 04:22:17 +02:00
}
2018-05-23 11:59:37 +02:00
bool NotebookTab::shouldDrawXButton()
2018-05-23 04:22:17 +02:00
{
2018-06-01 14:57:18 +02:00
return this->hasXButton() && (this->mouseOver_ || this->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
{
2018-06-01 14:57:18 +02:00
this->mouseDown_ = true;
this->mouseDownX_ = this->getXRect().contains(event->pos());
2018-04-18 09:12:29 +02:00
this->update();
2018-06-01 14:57:18 +02:00
this->notebook_->select(page);
2018-04-18 09:12:29 +02:00
2018-06-01 14:57:18 +02:00
if (this->notebook_->getAllowUserTabManagement()) {
2018-04-18 09:12:29 +02:00
switch (event->button()) {
case Qt::RightButton: {
2018-06-01 14:57:18 +02:00
this->menu_.popup(event->globalPos());
2018-04-18 09:12:29 +02:00
} break;
2018-06-01 14:57:18 +02:00
default:;
2018-04-18 09:12:29 +02:00
}
}
}
2018-05-23 11:59:37 +02:00
void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
2018-04-18 09:12:29 +02:00
{
2018-06-01 14:57:18 +02:00
this->mouseDown_ = false;
2018-04-18 09:12:29 +02:00
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) {
2018-06-01 14:57:18 +02:00
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-06-01 14:57:18 +02:00
if (this->hasXButton() && this->mouseDownX_ && this->getXRect().contains(event->pos())) {
this->mouseDownX_ = false;
2018-04-18 09:12:29 +02:00
removeThisPage();
2018-04-18 09:12:29 +02:00
} else {
this->update();
}
}
}
2018-06-11 21:57:17 +02:00
void NotebookTab::enterEvent(QEvent *event)
2018-04-18 09:12:29 +02:00
{
2018-06-01 14:57:18 +02:00
this->mouseOver_ = true;
2018-04-18 09:12:29 +02:00
this->update();
2018-06-11 21:57:17 +02:00
RippleEffectButton::enterEvent(event);
2018-04-18 09:12:29 +02:00
}
2018-06-11 21:57:17 +02:00
void NotebookTab::leaveEvent(QEvent *event)
2018-04-18 09:12:29 +02:00
{
2018-06-01 14:57:18 +02:00
this->mouseOverX_ = false;
this->mouseOver_ = false;
2018-04-18 09:12:29 +02:00
this->update();
2018-06-11 21:57:17 +02:00
RippleEffectButton::leaveEvent(event);
2018-04-18 09:12:29 +02:00
}
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-06-01 14:57:18 +02:00
if (this->notebook_->getAllowUserTabManagement()) {
this->notebook_->select(this->page);
2018-04-18 18:55:49 +02:00
}
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();
2018-06-01 14:57:18 +02:00
if (app->settings->showTabCloseButton && this->notebook_->getAllowUserTabManagement()) //
2018-04-18 09:12:29 +02:00
{
bool overX = this->getXRect().contains(event->pos());
2018-06-01 14:57:18 +02:00
if (overX != this->mouseOverX_) {
2018-04-18 09:12:29 +02:00
// Over X state has been changed (we either left or entered it;
2018-06-01 14:57:18 +02:00
this->mouseOverX_ = overX;
2018-04-18 09:12:29 +02:00
this->update();
}
}
QPoint relPoint = this->mapToParent(event->pos());
2018-06-01 14:57:18 +02:00
if (this->mouseDown_ && !this->getDesiredRect().contains(relPoint) &&
this->notebook_->getAllowUserTabManagement()) //
2018-04-18 09:12:29 +02:00
{
int index;
2018-06-01 14:57:18 +02:00
QWidget *clickedPage = this->notebook_->tabAt(relPoint, index, this->width());
2018-04-18 09:12:29 +02:00
if (clickedPage != nullptr && clickedPage != this->page) {
2018-06-01 14:57:18 +02:00
this->notebook_->rearrangePage(this->page, index);
2018-04-18 09:12:29 +02:00
}
}
2018-06-11 21:57:17 +02:00
RippleEffectButton::mouseMoveEvent(event);
2018-04-18 09:12:29 +02:00
}
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 chatterino