mirror-chatterino2/widgets/notebooktab.cpp

101 lines
2 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-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-18 04:33:30 +01:00
, notebook(notebook)
, text("<no title>")
, selected(false)
, mouseOver(false)
, mouseDown(false)
, highlightStyle(HighlightNone)
2016-12-29 17:31:07 +01:00
{
2016-12-30 18:00:25 +01:00
calcSize();
2017-01-01 02:30:42 +01:00
setAcceptDrops(true);
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::calcSize()
2016-12-30 18:00:25 +01:00
{
2017-01-18 04:33:30 +01:00
resize(fontMetrics().width(this->text) + 8, 24);
2016-12-30 12:20:26 +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-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-18 04:33:30 +01:00
painter.drawText(4, (height() + fontMetrics().height()) / 2, this->text);
2016-12-30 12:20:26 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookTab::mousePressEvent(QMouseEvent *)
2016-12-30 12:20:26 +01:00
{
2017-01-18 04:33:30 +01:00
this->mouseDown = true;
2016-12-30 12:20:26 +01:00
2016-12-30 18:00:25 +01:00
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 *)
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
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::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
{
2017-01-18 04:33:30 +01:00
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
NotebookTab::dragEnterEvent(QDragEnterEvent *event)
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
}
2017-01-18 21:30:23 +01:00
}
}