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

794 lines
21 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"
#include "controllers/hotkeys/HotkeyCategory.hpp"
#include "controllers/hotkeys/HotkeyController.hpp"
#include "singletons/Fonts.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.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/splits/SplitContainer.hpp"
2016-12-29 17:31:07 +01:00
#include <QApplication>
#include <QDebug>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
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/bind.hpp>
2017-01-15 16:38:30 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace {
qreal deviceDpi(QWidget *widget)
{
#ifdef Q_OS_WIN
return widget->devicePixelRatioF();
#else
return 1.0;
#endif
}
// Translates the given rectangle by an amount in the direction to appear like the tab is selected.
// For example, if location is Top, the rectangle will be translated in the negative Y direction,
// or "up" on the screen, by amount.
void translateRectForLocation(QRect &rect, NotebookTabLocation location,
int amount)
{
switch (location)
{
case NotebookTabLocation::Top:
rect.translate(0, -amount);
break;
case NotebookTabLocation::Left:
rect.translate(-amount, 0);
break;
case NotebookTabLocation::Right:
rect.translate(amount, 0);
break;
case NotebookTabLocation::Bottom:
rect.translate(0, amount);
break;
}
}
} // namespace
2017-01-18 21:30:23 +01:00
2018-06-01 14:57:18 +02:00
NotebookTab::NotebookTab(Notebook *notebook)
2018-08-08 15:35:54 +02:00
: Button(notebook)
2018-06-01 14:57:18 +02:00
, positionChangedAnimation_(this, "pos")
, notebook_(notebook)
, menu_(this)
2018-04-18 09:12:29 +02:00
{
this->setAcceptDrops(true);
2018-08-06 21:17:03 +02:00
this->positionChangedAnimation_.setEasingCurve(
QEasingCurve(QEasingCurve::InCubic));
2018-04-18 09:12:29 +02:00
getSettings()->showTabCloseButton.connectSimple(
boost::bind(&NotebookTab::hideTabXChanged, this),
2018-08-06 21:17:03 +02:00
this->managedConnections_);
getSettings()->showTabLive.connect(
[this](auto, auto) {
this->update();
},
this->managedConnections_);
2018-04-18 09:12:29 +02:00
this->setMouseTracking(true);
this->menu_.addAction("Rename Tab", [this]() {
this->showRenameDialog();
});
2018-04-18 09:12:29 +02:00
// XXX: this doesn't update after changing hotkeys
this->menu_.addAction(
"Close Tab",
[=]() {
this->notebook_->removePage(this->page);
},
getApp()->hotkeys->getDisplaySequence(HotkeyCategory::Window,
"removeTab"));
2018-04-18 09:12:29 +02:00
this->menu_.addAction(
"Popup Tab",
[=]() {
if (auto container = dynamic_cast<SplitContainer *>(this->page))
{
container->popup();
}
},
getApp()->hotkeys->getDisplaySequence(HotkeyCategory::Window, "popup",
{{"window"}}));
highlightNewMessagesAction_ =
new QAction("Mark Tab as Unread on New Messages", &this->menu_);
highlightNewMessagesAction_->setCheckable(true);
highlightNewMessagesAction_->setChecked(highlightEnabled_);
QObject::connect(highlightNewMessagesAction_, &QAction::triggered,
[this](bool checked) {
this->highlightEnabled_ = checked;
});
this->menu_.addAction(highlightNewMessagesAction_);
this->menu_.addSeparator();
this->notebook_->addNotebookActionsToMenu(&this->menu_);
2018-04-18 09:12:29 +02:00
}
2018-10-22 19:57:02 +02:00
void NotebookTab::showRenameDialog()
{
auto dialog = new QDialog(this);
2018-10-22 19:57:02 +02:00
auto vbox = new QVBoxLayout;
2018-10-22 19:57:02 +02:00
auto lineEdit = new QLineEdit;
lineEdit->setText(this->getCustomTitle());
lineEdit->setPlaceholderText(this->getDefaultTitle());
lineEdit->selectAll();
vbox->addWidget(new QLabel("Name:"));
vbox->addWidget(lineEdit);
vbox->addStretch(1);
auto buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
vbox->addWidget(buttonBox);
dialog->setLayout(vbox);
QObject::connect(buttonBox, &QDialogButtonBox::accepted, [dialog] {
dialog->accept();
dialog->close();
});
QObject::connect(buttonBox, &QDialogButtonBox::rejected, [dialog] {
dialog->reject();
dialog->close();
});
dialog->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
dialog->setMinimumSize(dialog->minimumSizeHint().width() + 50,
dialog->minimumSizeHint().height() + 10);
dialog->setWindowFlags(
(dialog->windowFlags() & ~(Qt::WindowContextHelpButtonHint)) |
Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
dialog->setWindowTitle("Rename Tab");
if (dialog->exec() == QDialog::Accepted)
2018-10-22 19:57:02 +02:00
{
QString newTitle = lineEdit->text();
2018-10-22 19:57:02 +02:00
this->setCustomTitle(newTitle);
}
}
2018-07-06 17:11:37 +02:00
void NotebookTab::themeChangedEvent()
2018-04-18 09:12:29 +02:00
{
this->update();
2018-06-11 21:57:17 +02:00
// this->setMouseEffectColor(QColor("#999"));
2018-07-06 17:11:37 +02:00
this->setMouseEffectColor(this->theme->tabs.regular.text);
2018-04-18 09:12:29 +02:00
}
void NotebookTab::growWidth(int width)
{
if (this->growWidth_ != width)
{
this->growWidth_ = width;
this->updateSize();
}
else
{
this->growWidth_ = width;
}
}
int NotebookTab::normalTabWidth()
2018-04-18 09:12:29 +02:00
{
2018-11-21 21:37:41 +01:00
float scale = this->scale();
2018-04-18 09:12:29 +02:00
int width;
2018-05-25 13:53:55 +02:00
QFontMetrics metrics = getApp()->fonts->getFontMetrics(
FontStyle::UiTabs, float(qreal(this->scale()) * deviceDpi(this)));
2018-04-18 09:12:29 +02:00
2018-10-21 13:43:02 +02:00
if (this->hasXButton())
{
width = (metrics.horizontalAdvance(this->getTitle()) + int(32 * scale));
2018-10-21 13:43:02 +02:00
}
else
{
width = (metrics.horizontalAdvance(this->getTitle()) + int(16 * scale));
2018-04-18 09:12:29 +02:00
}
2018-11-25 21:14:42 +01:00
if (this->height() > 150 * scale)
{
width = this->height();
}
else
{
width = clamp(width, this->height(), int(150 * scale));
}
return width;
}
void NotebookTab::updateSize()
{
float scale = this->scale();
int width = this->normalTabWidth();
auto height = int(NOTEBOOK_TAB_HEIGHT * scale);
2018-05-23 04:22:17 +02:00
if (width < this->growWidth_)
{
width = this->growWidth_;
}
2018-10-21 13:43:02 +02:00
if (this->width() != width || this->height() != height)
{
this->resize(width, height);
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-10-21 13:43:02 +02:00
if (this->customTitle_ != newTitle)
{
2018-06-01 16:01:49 +02:00
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)
{
2018-10-21 13:43:02 +02:00
if (this->defaultTitle_ != title)
{
2018-06-01 16:01:49 +02:00
this->defaultTitle_ = title;
2018-10-21 13:43:02 +02:00
if (this->customTitle_.isEmpty())
{
2018-06-01 16:01:49 +02:00
this->titleUpdated();
}
}
}
const QString &NotebookTab::getDefaultTitle() const
{
return this->defaultTitle_;
}
const QString &NotebookTab::getTitle() const
{
2018-08-06 21:17:03 +02:00
return this->customTitle_.isEmpty() ? this->defaultTitle_
: this->customTitle_;
2018-06-01 16:01:49 +02:00
}
void NotebookTab::titleUpdated()
{
// Queue up save because: Tab title changed
getApp()->windows->queueSave();
this->notebook_->performLayout();
2018-06-01 16:01:49 +02:00
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-10-21 12:13:09 +02:00
void NotebookTab::setInLastRow(bool value)
{
2018-10-21 13:43:02 +02:00
if (this->isInLastRow_ != value)
{
2018-10-21 12:13:09 +02:00
this->isInLastRow_ = value;
this->update();
}
}
void NotebookTab::setTabLocation(NotebookTabLocation location)
{
if (this->tabLocation_ != location)
{
this->tabLocation_ = location;
this->update();
}
}
2018-10-13 14:20:06 +02:00
void NotebookTab::setLive(bool isLive)
{
2018-10-21 13:43:02 +02:00
if (this->isLive_ != isLive)
{
2018-10-13 14:20:06 +02:00
this->isLive_ = isLive;
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() || (!this->highlightEnabled_ &&
newHighlightStyle == HighlightState::NewMessage))
2018-10-21 13:43:02 +02:00
{
2018-04-18 09:12:29 +02:00
return;
}
2018-10-21 13:43:02 +02:00
if (this->highlightState_ != HighlightState::Highlighted)
{
2018-06-01 14:57:18 +02:00
this->highlightState_ = newHighlightStyle;
2018-04-18 09:12:29 +02:00
this->update();
}
}
void NotebookTab::setHighlightsEnabled(const bool &newVal)
{
this->highlightNewMessagesAction_->setChecked(newVal);
this->highlightEnabled_ = newVal;
}
bool NotebookTab::hasHighlightsEnabled() const
{
return this->highlightEnabled_;
}
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
}
void NotebookTab::hideTabXChanged()
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-08-06 21:17:03 +02:00
if ((w != nullptr && !w->isVisible()) || !animated ||
2018-10-21 13:43:02 +02:00
!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-10-21 13:43:02 +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);
2018-11-21 21:37:41 +01:00
float scale = this->scale();
2018-04-18 09:12:29 +02:00
auto div = std::max<float>(0.01f, this->logicalDpiX() * deviceDpi(this));
painter.setFont(
getApp()->fonts->getFont(FontStyle::UiTabs, scale * 96.f / div));
QFontMetrics metrics =
app->fonts->getFontMetrics(FontStyle::UiTabs, scale * 96.f / div);
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
// select the right tab colors
2018-06-28 20:03:04 +02:00
Theme::TabColors colors;
2018-07-06 17:11:37 +02:00
Theme::TabColors regular = this->theme->tabs.regular;
2018-04-18 09:12:29 +02:00
2018-10-21 13:43:02 +02:00
if (this->selected_)
2018-07-06 17:11:37 +02:00
colors = this->theme->tabs.selected;
2018-10-21 13:43:02 +02:00
else if (this->highlightState_ == HighlightState::Highlighted)
2018-07-06 17:11:37 +02:00
colors = this->theme->tabs.highlighted;
2018-10-21 13:43:02 +02:00
else if (this->highlightState_ == HighlightState::NewMessage)
2018-07-06 17:11:37 +02:00
colors = this->theme->tabs.newMessage;
2018-10-21 13:43:02 +02:00
else
2018-07-06 17:11:37 +02:00
colors = this->theme->tabs.regular;
2018-04-18 09:12:29 +02:00
bool windowFocused = this->window() == QApplication::activeWindow();
2018-06-11 21:57:17 +02:00
QBrush tabBackground = /*this->mouseOver_ ? colors.backgrounds.hover
:*/
2018-08-06 21:17:03 +02:00
(windowFocused ? colors.backgrounds.regular
: colors.backgrounds.unfocused);
2018-04-18 09:12:29 +02:00
auto selectionOffset = ceil((this->selected_ ? 0.f : 1.f) * scale);
2018-05-23 04:22:17 +02:00
// fill the tab background
auto bgRect = this->rect();
switch (this->tabLocation_)
{
case NotebookTabLocation::Top:
bgRect.setTop(selectionOffset);
break;
case NotebookTabLocation::Left:
bgRect.setLeft(selectionOffset);
break;
case NotebookTabLocation::Right:
bgRect.setRight(bgRect.width() - selectionOffset);
break;
case NotebookTabLocation::Bottom:
bgRect.setBottom(bgRect.height() - selectionOffset);
break;
}
painter.fillRect(bgRect, tabBackground);
2018-05-23 04:22:17 +02:00
// draw color indicator line
auto lineThickness = ceil((this->selected_ ? 2.f : 1.f) * scale);
auto lineColor = this->mouseOver_ ? colors.line.hover
: (windowFocused ? colors.line.regular
: colors.line.unfocused);
QRect lineRect;
switch (this->tabLocation_)
{
case NotebookTabLocation::Top:
lineRect =
QRect(bgRect.left(), bgRect.y(), bgRect.width(), lineThickness);
break;
case NotebookTabLocation::Left:
lineRect =
QRect(bgRect.x(), bgRect.top(), lineThickness, bgRect.height());
break;
case NotebookTabLocation::Right:
lineRect = QRect(bgRect.right() - lineThickness, bgRect.top(),
lineThickness, bgRect.height());
break;
case NotebookTabLocation::Bottom:
lineRect = QRect(bgRect.left(), bgRect.bottom() - lineThickness,
bgRect.width(), lineThickness);
break;
}
painter.fillRect(lineRect, lineColor);
2018-04-18 09:12:29 +02:00
2018-10-13 14:20:06 +02:00
// draw live indicator
2018-10-31 19:45:51 +01:00
if (this->isLive_ && getSettings()->showTabLive)
2018-10-21 13:43:02 +02:00
{
2018-10-13 14:20:06 +02:00
painter.setPen(QColor(Qt::GlobalColor::red));
2018-10-21 12:13:09 +02:00
painter.setRenderHint(QPainter::Antialiasing);
2018-10-13 14:20:06 +02:00
QBrush b;
b.setColor(QColor(Qt::GlobalColor::red));
b.setStyle(Qt::SolidPattern);
painter.setBrush(b);
2018-10-21 12:13:09 +02:00
auto x = this->width() - (7 * scale);
auto y = 4 * scale;
2018-10-21 12:13:09 +02:00
auto diameter = 4 * scale;
QRect liveIndicatorRect(x, y, diameter, diameter);
translateRectForLocation(liveIndicatorRect, this->tabLocation_,
this->selected_ ? 0 : -1);
painter.drawEllipse(liveIndicatorRect);
2018-10-13 14:20:06 +02:00
}
2018-04-18 09:12:29 +02:00
// set the pen color
painter.setPen(colors.text);
// set area for text
int rectW = (!getSettings()->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, 0, this->width() - offset - offset, height);
translateRectForLocation(textRect, this->tabLocation_,
this->selected_ ? -1 : -2);
2018-05-23 04:22:17 +02:00
2018-10-21 13:43:02 +02:00
if (this->shouldDrawXButton())
{
2018-06-01 14:57:18 +02:00
textRect.setRight(textRect.right() - this->height() / 2);
}
2018-04-18 09:12:29 +02:00
int width = metrics.horizontalAdvance(this->getTitle());
2018-08-06 21:17:03 +02:00
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-10-21 13:43:02 +02:00
if (this->shouldDrawXButton())
{
2018-10-31 19:45:51 +01:00
painter.setRenderHint(QPainter::Antialiasing, false);
2018-04-18 09:12:29 +02:00
QRect xRect = this->getXRect();
2018-10-21 13:43:02 +02:00
if (!xRect.isNull())
{
2018-05-23 04:22:17 +02:00
painter.setBrush(QColor("#fff"));
2018-10-21 13:43:02 +02:00
if (this->mouseOverX_)
{
2018-04-18 09:12:29 +02:00
painter.fillRect(xRect, QColor(0, 0, 0, 64));
2018-10-21 13:43:02 +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);
2018-08-06 21:17:03 +02:00
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-04-18 09:12:29 +02:00
}
}
2018-05-23 04:22:17 +02:00
2018-10-22 19:57:02 +02:00
// draw mouse over effect
if (!this->selected_)
{
this->fancyPaint(painter);
}
// draw line at border
2018-10-21 13:43:02 +02:00
if (!this->selected_ && this->isInLastRow_)
{
QRect borderRect;
switch (this->tabLocation_)
{
case NotebookTabLocation::Top:
borderRect = QRect(0, this->height() - 1, this->width(), 1);
break;
case NotebookTabLocation::Left:
borderRect = QRect(this->width() - 1, 0, 1, this->height());
break;
case NotebookTabLocation::Right:
borderRect = QRect(0, 0, 1, this->height());
break;
case NotebookTabLocation::Bottom:
borderRect = QRect(0, 0, this->width(), 1);
break;
}
painter.fillRect(borderRect, app->themes->window.background);
2018-10-21 12:13:09 +02:00
}
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
{
return getSettings()->showTabCloseButton &&
2018-08-06 21:17:03 +02:00
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-10-21 13:43:02 +02:00
if (event->button() == Qt::LeftButton)
{
this->mouseDown_ = true;
this->mouseDownX_ = this->getXRect().contains(event->pos());
2018-04-18 09:12:29 +02:00
this->notebook_->select(page);
}
2018-04-18 09:12:29 +02:00
2018-09-29 22:35:33 +02:00
this->update();
2018-10-21 13:43:02 +02:00
if (this->notebook_->getAllowUserTabManagement())
{
switch (event->button())
{
2019-09-26 00:51:05 +02:00
case Qt::RightButton: {
2020-08-13 20:59:57 +02:00
this->menu_.popup(event->globalPos() + QPoint(0, 8));
2018-10-21 13:43:02 +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] {
2018-08-06 21:17:03 +02:00
auto reply = QMessageBox::question(
this, "Remove this tab",
"Are you sure that you want to remove this tab?",
QMessageBox::Yes | QMessageBox::Cancel);
2018-10-21 13:43:02 +02:00
if (reply == QMessageBox::Yes)
{
2018-06-01 14:57:18 +02:00
this->notebook_->removePage(this->page);
}
};
if (event->button() == Qt::MiddleButton &&
this->notebook_->getAllowUserTabManagement())
2018-10-21 13:43:02 +02:00
{
if (this->rect().contains(event->pos()))
{
removeThisPage();
2018-04-18 09:12:29 +02:00
}
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-06 21:17:03 +02:00
if (this->hasXButton() && this->mouseDownX_ &&
2018-10-21 13:43:02 +02:00
this->getXRect().contains(event->pos()))
{
2018-06-01 14:57:18 +02:00
this->mouseDownX_ = false;
2018-04-18 09:12:29 +02:00
removeThisPage();
2018-10-21 13:43:02 +02:00
}
else
{
2018-04-18 09:12:29 +02:00
this->update();
}
}
}
2018-10-22 19:57:02 +02:00
void NotebookTab::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton &&
this->notebook_->getAllowUserTabManagement())
{
this->showRenameDialog();
}
}
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
2018-08-08 15:35:54 +02:00
Button::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
2018-08-08 15:35:54 +02:00
Button::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-10-21 13:43:02 +02:00
if (!event->mimeData()->hasFormat("chatterino/split"))
return;
2018-05-23 12:35:10 +02:00
2018-10-21 13:43:02 +02:00
if (!SplitContainer::isDraggingSplit)
return;
2018-05-23 12:35:10 +02:00
2018-10-21 13:43:02 +02:00
if (this->notebook_->getAllowUserTabManagement())
{
2018-06-01 14:57:18 +02:00
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
{
if (getSettings()->showTabCloseButton &&
this->notebook_->getAllowUserTabManagement())
2018-04-18 09:12:29 +02:00
{
bool overX = this->getXRect().contains(event->pos());
2018-10-21 13:43:02 +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-08-06 21:17:03 +02:00
QWidget *clickedPage =
this->notebook_->tabAt(relPoint, index, this->width());
2018-04-18 09:12:29 +02:00
2018-10-21 13:43:02 +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
2018-08-08 15:35:54 +02:00
Button::mouseMoveEvent(event);
2018-04-18 09:12:29 +02:00
}
void NotebookTab::wheelEvent(QWheelEvent *event)
{
const auto defaultMouseDelta = 120;
const auto verticalDelta = event->angleDelta().y();
const auto selectTab = [this](int delta) {
2019-08-11 21:08:18 +02:00
delta > 0 ? this->notebook_->selectPreviousTab()
: this->notebook_->selectNextTab();
};
// If it's true
// Then the user uses the trackpad or perhaps the most accurate mouse
// Which has small delta.
if (std::abs(verticalDelta) < defaultMouseDelta)
2018-10-21 13:43:02 +02:00
{
this->mouseWheelDelta_ += verticalDelta;
if (std::abs(this->mouseWheelDelta_) >= defaultMouseDelta)
{
selectTab(this->mouseWheelDelta_);
this->mouseWheelDelta_ = 0;
}
2018-10-21 13:43:02 +02:00
}
else
{
selectTab(verticalDelta);
}
}
2018-05-23 11:59:37 +02:00
QRect NotebookTab::getXRect()
2018-04-18 09:12:29 +02:00
{
QRect rect = this->rect();
2018-11-21 21:37:41 +01:00
float s = this->scale();
int size = static_cast<int>(16 * s);
int centerAdjustment =
this->tabLocation_ ==
(NotebookTabLocation::Top ||
this->tabLocation_ == NotebookTabLocation::Bottom)
? (size / 3) // slightly off true center
: (size / 2); // true center
QRect xRect(rect.right() - static_cast<int>(20 * s),
rect.center().y() - centerAdjustment, size, size);
if (this->selected_)
{
translateRectForLocation(xRect, this->tabLocation_, 1);
}
return xRect;
2018-04-18 09:12:29 +02:00
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino