mirror-chatterino2/widgets/notebooktab.cpp

208 lines
5 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "widgets/notebooktab.h"
2017-01-01 02:30:42 +01:00
#include "colorscheme.h"
2017-01-23 16:38:06 +01:00
#include "settings.h"
2017-01-18 21:30:23 +01:00
#include "widgets/notebook.h"
2016-12-29 17:31:07 +01:00
2017-01-15 16:38:30 +01:00
#include <QPainter>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
2016-12-30 12:20:26 +01:00
NotebookTab::NotebookTab(Notebook *notebook)
: QWidget(notebook)
2017-01-26 05:26:21 +01:00
, posAnimation(this, "pos")
, posAnimated(false)
, posAnimationDesired()
2017-01-18 04:33:30 +01:00
, notebook(notebook)
, text("<no title>")
, selected(false)
, mouseOver(false)
, mouseDown(false)
, mouseOverX(false)
, mouseDownX(false)
2017-01-18 04:33:30 +01:00
, highlightStyle(HighlightNone)
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-01-26 05:26:21 +01:00
posAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
/* XXX(pajlada): Fix this
2017-01-23 16:38:06 +01:00
QObject::connect(&Settings::getInstance().getHideTabX(),
&BoolSetting::valueChanged, this,
2017-01-22 12:46:35 +01:00
&NotebookTab::hideTabXChanged);
*/
2017-01-26 05:26:21 +01:00
// Settings::getInstance().hideTabX.valueChanged.connect(
// boost::bind(&NotebookTab::hideTabXChanged, this));
this->setMouseTracking(true);
}
NotebookTab::~NotebookTab()
{
/* XXX(pajlada): Fix this
2017-01-23 16:38:06 +01:00
QObject::disconnect(&Settings::getInstance().getHideTabX(),
&BoolSetting::valueChanged, this,
2017-01-22 12:46:35 +01:00
&NotebookTab::hideTabXChanged);
*/
2017-01-01 02:30:42 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::calcSize()
2016-12-30 18:00:25 +01:00
{
2017-01-23 16:38:06 +01:00
if (Settings::getInstance().hideTabX.get()) {
this->resize(this->fontMetrics().width(this->text) + 8, 24);
} else {
this->resize(this->fontMetrics().width(this->text) + 8 + 24, 24);
}
2016-12-30 12:20:26 +01:00
}
2017-01-22 12:46:35 +01:00
void
2017-01-26 05:26:21 +01:00
NotebookTab::moveAnimated(QPoint pos, bool animated)
2017-01-22 12:46:35 +01:00
{
2017-01-26 05:26:21 +01:00
// move(pos);
2017-01-22 12:46:35 +01:00
2017-01-26 05:26:21 +01:00
posAnimationDesired = pos;
2017-01-22 12:46:35 +01:00
2017-01-26 05:26:21 +01:00
if (!animated || posAnimated == false) {
move(pos);
2017-01-22 12:46:35 +01:00
2017-01-26 05:26:21 +01:00
posAnimated = true;
return;
}
if (this->posAnimation.endValue() == pos) {
return;
}
2017-01-22 12:46:35 +01:00
2017-01-26 05:26:21 +01:00
this->posAnimation.stop();
this->posAnimation.setDuration(75);
this->posAnimation.setStartValue(this->pos());
this->posAnimation.setEndValue(pos);
this->posAnimation.start();
2017-01-22 12:46:35 +01:00
}
2017-01-11 18:52:09 +01: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-01-24 20:15:12 +01:00
auto colorScheme = ColorScheme::getInstance();
2017-01-01 02:30:42 +01:00
2017-01-18 04:33:30 +01:00
if (this->selected) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabSelectedBackground);
fg = colorScheme.TabSelectedText;
2017-01-18 04:33:30 +01:00
} else if (this->mouseOver) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabHoverBackground);
fg = colorScheme.TabHoverText;
2017-01-18 04:33:30 +01:00
} else if (this->highlightStyle == HighlightHighlighted) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabHighlightedBackground);
fg = colorScheme.TabHighlightedText;
2017-01-18 04:33:30 +01:00
} else if (this->highlightStyle == HighlightNewMessage) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabNewMessageBackground);
fg = colorScheme.TabHighlightedText;
2017-01-11 18:52:09 +01:00
} else {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabBackground);
fg = colorScheme.TabText;
}
2016-12-30 18:00:25 +01:00
painter.setPen(fg);
2017-01-23 16:38:06 +01:00
QRect rect(0, 0,
width() - (Settings::getInstance().hideTabX.get() ? 0 : 16),
height());
painter.drawText(rect, this->text, QTextOption(Qt::AlignCenter));
2017-01-23 16:38:06 +01:00
if (!Settings::getInstance().hideTabX.get() &&
2017-01-22 12:46:35 +01:00
(this->mouseOver || this->selected)) {
if (this->mouseOverX) {
painter.fillRect(this->getXRect(), QColor(0, 0, 0, 64));
2017-01-22 12:46:35 +01:00
if (this->mouseDownX) {
painter.fillRect(this->getXRect(), QColor(0, 0, 0, 64));
}
}
2017-01-22 12:46:35 +01:00
painter.drawLine(this->getXRect().topLeft() + QPoint(4, 4),
this->getXRect().bottomRight() + QPoint(-4, -4));
painter.drawLine(this->getXRect().topRight() + QPoint(-4, 4),
this->getXRect().bottomLeft() + QPoint(4, -4));
}
2016-12-30 12:20:26 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::mousePressEvent(QMouseEvent *event)
2016-12-30 12:20:26 +01:00
{
2017-01-18 04:33:30 +01:00
this->mouseDown = true;
this->mouseDownX = this->getXRect().contains(event->pos());
2016-12-30 12:20:26 +01:00
2017-01-26 07:10:46 +01:00
this->update();
2017-01-01 02:30:42 +01:00
2017-01-18 04:33:30 +01:00
this->notebook->select(page);
2016-12-30 12:20:26 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::mouseReleaseEvent(QMouseEvent *event)
2016-12-30 12:20:26 +01:00
{
2017-01-18 04:33:30 +01:00
this->mouseDown = false;
2016-12-30 12:20:26 +01:00
2017-01-23 16:38:06 +01:00
if (this->mouseDownX && this->getXRect().contains(event->pos())) {
this->mouseDownX = false;
this->notebook->removePage(this->page);
} else {
2017-01-26 07:10:46 +01:00
update();
}
2016-12-30 12:20:26 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::enterEvent(QEvent *)
2016-12-30 12:20:26 +01:00
{
2017-01-18 04:33:30 +01:00
this->mouseOver = true;
2016-12-30 12:20:26 +01:00
2017-01-26 07:10:46 +01:00
update();
2016-12-30 12:20:26 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::leaveEvent(QEvent *)
2016-12-30 12:20:26 +01:00
{
this->mouseOverX = this->mouseOver = false;
2016-12-30 12:20:26 +01:00
2017-01-26 07:10:46 +01:00
update();
2016-12-29 17:31:07 +01:00
}
2017-01-01 02:30:42 +01:00
2017-01-11 18:52:09 +01:00
void
2017-01-23 16:38:06 +01:00
NotebookTab::dragEnterEvent(QDragEnterEvent *)
2017-01-01 02:30:42 +01:00
{
2017-01-18 04:33:30 +01:00
this->notebook->select(page);
2017-01-01 02:30:42 +01:00
}
void
NotebookTab::mouseMoveEvent(QMouseEvent *event)
{
bool overX = this->getXRect().contains(event->pos());
if (overX != this->mouseOverX) {
this->mouseOverX = overX;
2017-01-26 07:10:46 +01:00
this->update();
}
2017-01-22 12:46:35 +01:00
2017-01-26 05:26:21 +01:00
if (this->mouseDown && !this->getDesiredRect().contains(event->pos())) {
2017-01-22 12:46:35 +01:00
QPoint relPoint = this->mapToParent(event->pos());
int index;
NotebookPage *page = notebook->tabAt(relPoint, index);
if (page != nullptr && page != this->page) {
notebook->rearrangePage(this->page, index);
}
}
}
2017-01-18 21:30:23 +01:00
}
}