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

315 lines
8.2 KiB
C++
Raw Normal View History

2017-11-12 17:21:50 +01:00
#include "widgets/helper/notebooktab.hpp"
2017-06-11 09:31:45 +02:00
#include "colorscheme.hpp"
#include "settingsmanager.hpp"
#include "widgets/notebook.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
2017-08-13 16:07:00 +02:00
NotebookTab::NotebookTab(Notebook *_notebook)
2017-08-13 16:10:53 +02:00
: BaseWidget(_notebook)
2017-08-13 16:07:00 +02:00
, positionChangedAnimation(this, "pos")
, notebook(_notebook)
, 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
SettingsManager::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);
}
}
});
this->menu.addAction("Close", [=]() {
this->notebook->removePage(this->page);
qDebug() << "lmoa";
});
this->menu.addAction("Enable highlights on new message", []() {
qDebug() << "TODO: Implement"; //
});
}
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();
if (SettingsManager::getInstance().hideTabX) {
2017-09-22 00:50:43 +02:00
this->resize(static_cast<int>((fontMetrics().width(title) + 16) * scale),
static_cast<int>(24 * scale));
} else {
2017-09-22 00:50:43 +02:00
this->resize(static_cast<int>((fontMetrics().width(title) + 8 + 24) * scale),
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
}
2017-04-12 17:46:44 +02:00
const QString &NotebookTab::getTitle() const
2017-01-22 12:46:35 +01:00
{
2017-08-13 16:07:00 +02:00
return 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-08-13 16:07:00 +02:00
this->title = newTitle;
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->update();
2017-04-12 17:46:44 +02:00
}
NotebookTab::HighlightStyle NotebookTab::getHighlightStyle() const
{
2017-08-13 16:07:00 +02:00
return this->highlightStyle;
2017-04-12 17:46:44 +02:00
}
2017-08-13 16:07:00 +02:00
void NotebookTab::setHighlightStyle(HighlightStyle newHighlightStyle)
2017-04-12 17:46:44 +02:00
{
2017-08-13 16:07:00 +02:00
this->highlightStyle = newHighlightStyle;
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
{
QPainter painter(this);
2017-01-01 02:30:42 +01:00
QColor fg = QColor(0, 0, 0);
2017-08-13 16:07:00 +02:00
if (this->selected) {
if (this->window() == QApplication::activeWindow()) {
painter.fillRect(rect(), this->colorScheme.TabSelectedBackground);
fg = this->colorScheme.TabSelectedText;
} else {
painter.fillRect(rect(), this->colorScheme.TabSelectedUnfocusedBackground);
fg = this->colorScheme.TabSelectedUnfocusedText;
}
2017-08-13 16:07:00 +02:00
} else if (this->mouseOver) {
painter.fillRect(rect(), this->colorScheme.TabHoverBackground);
fg = this->colorScheme.TabHoverText;
2017-08-13 16:07:00 +02:00
} else if (this->highlightStyle == HighlightHighlighted) {
painter.fillRect(rect(), this->colorScheme.TabHighlightedBackground);
fg = this->colorScheme.TabHighlightedText;
2017-08-13 16:07:00 +02:00
} else if (this->highlightStyle == HighlightNewMessage) {
painter.fillRect(rect(), this->colorScheme.TabNewMessageBackground);
fg = this->colorScheme.TabHighlightedText;
2017-01-11 18:52:09 +01:00
} else {
painter.fillRect(rect(), this->colorScheme.TabBackground);
fg = this->colorScheme.TabText;
2017-01-01 02:30:42 +01:00
}
2016-12-30 18:00:25 +01:00
painter.setPen(fg);
2017-09-22 00:50:43 +02:00
float scale = this->getDpiMultiplier();
int rectW = (SettingsManager::getInstance().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-08-13 16:07:00 +02:00
painter.drawText(rect, title, QTextOption(Qt::AlignCenter));
if (!SettingsManager::getInstance().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 {
if (!SettingsManager::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)
{
if (!SettingsManager::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-12 17:46:44 +02:00
void NotebookTab::load(const boost::property_tree::ptree &tree)
{
// Load tab title
try {
QString newTitle = QString::fromStdString(tree.get<std::string>("title"));
if (newTitle.isEmpty()) {
this->useDefaultBehaviour = true;
} else {
this->setTitle(newTitle);
this->useDefaultBehaviour = false;
}
} catch (boost::property_tree::ptree_error) {
}
2017-01-18 21:30:23 +01:00
}
2017-04-12 17:46:44 +02:00
boost::property_tree::ptree NotebookTab::save()
{
boost::property_tree::ptree tree;
if (this->useDefaultBehaviour) {
tree.put("title", "");
} else {
tree.put("title", this->getTitle().toStdString());
}
return tree;
2017-01-18 21:30:23 +01:00
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino