mirror-chatterino2/src/widgets/notebooktab.cpp

249 lines
6.2 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "widgets/notebooktab.hpp"
#include "colorscheme.hpp"
#include "settingsmanager.hpp"
#include "widgets/notebook.hpp"
2016-12-29 17:31:07 +01:00
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)
: QWidget(_notebook)
, colorScheme(_notebook->colorScheme)
, positionChangedAnimation(this, "pos")
, notebook(_notebook)
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-08-13 16:07:00 +02:00
this->hideXConnection = SettingsManager::getInstance().hideTabX.valueChanged.connect(
2017-04-12 17:46:44 +02:00
boost::bind(&NotebookTab::hideTabXChanged, this, _1));
2017-01-26 05:26:21 +01:00
this->setMouseTracking(true);
}
NotebookTab::~NotebookTab()
{
2017-08-13 16:07:00 +02:00
this->hideXConnection.disconnect();
2017-01-01 02:30:42 +01:00
}
2017-04-12 17:46:44 +02:00
void NotebookTab::calcSize()
2016-12-30 18:00:25 +01:00
{
2017-04-12 17:46:44 +02:00
if (SettingsManager::getInstance().hideTabX.get()) {
2017-08-13 16:07:00 +02:00
this->resize(fontMetrics().width(title) + 8, 24);
} else {
2017-08-13 16:07:00 +02:00
this->resize(fontMetrics().width(title) + 8 + 24, 24);
}
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;
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) {
painter.fillRect(rect(), this->colorScheme.TabSelectedBackground);
fg = this->colorScheme.TabSelectedText;
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-08-13 16:07:00 +02:00
QRect rect(0, 0, this->width() - (SettingsManager::getInstance().hideTabX.get() ? 0 : 16),
this->height());
2017-08-13 16:07:00 +02:00
painter.drawText(rect, title, QTextOption(Qt::AlignCenter));
2017-08-13 16:07:00 +02:00
if (!SettingsManager::getInstance().hideTabX.get() && (mouseOver || selected)) {
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-08-13 16:07:00 +02:00
painter.drawLine(xRect.topLeft() + QPoint(4, 4), xRect.bottomRight() + QPoint(-4, -4));
painter.drawLine(xRect.topRight() + QPoint(-4, 4), xRect.bottomLeft() + QPoint(4, -4));
}
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
2017-08-13 16:07:00 +02:00
this->notebook->select(page);
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
2017-08-13 16:07:00 +02:00
if (!SettingsManager::getInstance().hideTabX.get() && this->mouseDownX &&
this->getXRect().contains(event->pos())) {
this->mouseDownX = false;
2017-08-13 16:07:00 +02:00
this->notebook->removePage(this->page);
} else {
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::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-08-13 16:07:00 +02:00
if (!SettingsManager::getInstance().hideTabX.get()) {
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-08-13 16:07:00 +02:00
NotebookPage *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 {
2017-08-13 16:07:00 +02:00
this->setTitle(QString::fromStdString(tree.get<std::string>("title")));
} 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;
2017-08-13 16:07:00 +02:00
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