mirror-chatterino2/widgets/notebooktab.cpp

200 lines
4.8 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-22 12:46:35 +01:00
// , posAnimation(this, "pos")
// , posAnimated(false)
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
/* 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);
*/
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
NotebookTab::moveAnimated(QPoint pos)
{
move(pos);
// if (posAnimated == false) {
// move(pos);
// posAnimated = true;
// return;
// }
// if (this->posAnimation.endValue() == pos) {
// return;
// }
// this->posAnimation.stop();
// this->posAnimation.setDuration(50);
// this->posAnimation.setStartValue(this->pos());
// this->posAnimation.setEndValue(pos);
// this->posAnimation.start();
}
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-06 23:28:48 +01:00
auto colorScheme = ColorScheme::instance();
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
this->repaint();
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 {
repaint();
}
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
2016-12-30 18:00:25 +01:00
repaint();
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
2016-12-30 18:00:25 +01:00
repaint();
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;
this->repaint();
}
2017-01-22 12:46:35 +01:00
if (this->mouseDown && !this->rect().contains(event->pos())) {
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
}
}