mirror-chatterino2/widgets/notebooktab.cpp

251 lines
5.7 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-04-12 17:46:44 +02:00
#include "settingsmanager.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-04-14 17:47:28 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2016-12-30 12:20:26 +01:00
NotebookTab::NotebookTab(Notebook *notebook)
: QWidget(notebook)
2017-04-12 17:46:44 +02:00
, _posAnimation(this, "pos")
, _posAnimated(false)
, _posAnimationDesired()
, _notebook(notebook)
, _title("<no title>")
, _selected(false)
, _mouseOver(false)
, _mouseDown(false)
, _mouseOverX(false)
, _mouseDownX(false)
, _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-04-12 17:46:44 +02:00
_posAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
2017-01-26 05:26:21 +01:00
2017-04-12 17:46:44 +02:00
this->_hideXConnection = SettingsManager::getInstance().hideTabX.valueChanged.connect(
boost::bind(&NotebookTab::hideTabXChanged, this, _1));
2017-01-26 05:26:21 +01:00
this->setMouseTracking(true);
}
NotebookTab::~NotebookTab()
{
2017-04-12 17:46:44 +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()) {
resize(fontMetrics().width(_title) + 8, 24);
} else {
2017-04-12 17:46:44 +02:00
resize(fontMetrics().width(_title) + 8 + 24, 24);
}
2017-02-02 02:46:33 +01:00
2017-04-12 17:46:44 +02:00
if (parent() != nullptr) {
((Notebook *)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-04-12 17:46:44 +02:00
return _title;
}
void NotebookTab::setTitle(const QString &title)
{
_title = title;
}
2017-01-22 12:46:35 +01:00
2017-04-12 17:46:44 +02:00
bool NotebookTab::getSelected()
{
return _selected;
}
void NotebookTab::setSelected(bool value)
{
_selected = value;
update();
}
NotebookTab::HighlightStyle NotebookTab::getHighlightStyle() const
{
return _highlightStyle;
}
void NotebookTab::setHighlightStyle(HighlightStyle style)
{
_highlightStyle = style;
update();
}
QRect NotebookTab::getDesiredRect() const
{
return QRect(_posAnimationDesired, size());
}
void NotebookTab::hideTabXChanged(bool)
{
calcSize();
update();
}
void NotebookTab::moveAnimated(QPoint pos, bool animated)
{
_posAnimationDesired = pos;
if ((window() != NULL && !window()->isVisible()) || !animated || _posAnimated == false) {
2017-01-26 05:26:21 +01:00
move(pos);
2017-01-22 12:46:35 +01:00
2017-04-12 17:46:44 +02:00
_posAnimated = true;
2017-01-26 05:26:21 +01:00
return;
}
2017-04-12 17:46:44 +02:00
if (_posAnimation.endValue() == pos) {
2017-01-26 05:26:21 +01:00
return;
}
2017-01-22 12:46:35 +01:00
2017-04-12 17:46:44 +02:00
_posAnimation.stop();
_posAnimation.setDuration(75);
_posAnimation.setStartValue(this->pos());
_posAnimation.setEndValue(pos);
_posAnimation.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-02-02 01:23:26 +01:00
auto &colorScheme = ColorScheme::getInstance();
2017-01-01 02:30:42 +01:00
2017-04-12 17:46:44 +02:00
if (_selected) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabSelectedBackground);
fg = colorScheme.TabSelectedText;
2017-04-12 17:46:44 +02:00
} else if (_mouseOver) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabHoverBackground);
fg = colorScheme.TabHoverText;
2017-04-12 17:46:44 +02:00
} else if (_highlightStyle == HighlightHighlighted) {
2017-01-01 02:30:42 +01:00
painter.fillRect(rect(), colorScheme.TabHighlightedBackground);
fg = colorScheme.TabHighlightedText;
2017-04-12 17:46:44 +02:00
} else if (_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-04-12 17:46:44 +02:00
QRect rect(0, 0, width() - (SettingsManager::getInstance().hideTabX.get() ? 0 : 16), height());
2017-04-12 17:46:44 +02:00
painter.drawText(rect, _title, QTextOption(Qt::AlignCenter));
2017-04-12 17:46:44 +02:00
if (!SettingsManager::getInstance().hideTabX.get() && (_mouseOver || _selected)) {
if (_mouseOverX) {
painter.fillRect(getXRect(), QColor(0, 0, 0, 64));
2017-04-12 17:46:44 +02:00
if (_mouseDownX) {
painter.fillRect(getXRect(), QColor(0, 0, 0, 64));
}
}
2017-04-12 17:46:44 +02:00
painter.drawLine(getXRect().topLeft() + QPoint(4, 4),
getXRect().bottomRight() + QPoint(-4, -4));
painter.drawLine(getXRect().topRight() + QPoint(-4, 4),
getXRect().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-04-12 17:46:44 +02:00
_mouseDown = true;
_mouseDownX = getXRect().contains(event->pos());
2016-12-30 12:20:26 +01:00
2017-04-12 17:46:44 +02:00
update();
2017-01-01 02:30:42 +01:00
2017-04-12 17:46:44 +02:00
_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-04-12 17:46:44 +02:00
_mouseDown = false;
2016-12-30 12:20:26 +01:00
2017-04-12 17:46:44 +02:00
if (!SettingsManager::getInstance().hideTabX.get() && _mouseDownX &&
getXRect().contains(event->pos())) {
_mouseDownX = false;
2017-04-12 17:46:44 +02:00
_notebook->removePage(page);
} else {
2017-01-26 07:10:46 +01:00
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-04-12 17:46:44 +02:00
_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-04-12 17:46:44 +02:00
void NotebookTab::leaveEvent(QEvent *)
2016-12-30 12:20:26 +01:00
{
2017-04-12 17:46:44 +02:00
_mouseOverX = _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-04-12 17:46:44 +02:00
void NotebookTab::dragEnterEvent(QDragEnterEvent *)
2017-01-01 02:30:42 +01:00
{
2017-04-12 17:46:44 +02:00
_notebook->select(page);
2017-01-01 02:30:42 +01:00
}
2017-04-12 17:46:44 +02:00
void NotebookTab::mouseMoveEvent(QMouseEvent *event)
{
2017-04-12 17:46:44 +02:00
bool overX = getXRect().contains(event->pos());
2017-04-12 17:46:44 +02:00
if (overX != _mouseOverX) {
_mouseOverX = overX && !SettingsManager::getInstance().hideTabX.get();
2017-04-12 17:46:44 +02:00
update();
}
2017-01-22 12:46:35 +01:00
2017-04-12 17:46:44 +02:00
if (_mouseDown && !getDesiredRect().contains(event->pos())) {
QPoint relPoint = mapToParent(event->pos());
2017-01-22 12:46:35 +01:00
int index;
2017-04-12 17:46:44 +02:00
NotebookPage *page = _notebook->tabAt(relPoint, index);
2017-01-22 12:46:35 +01:00
2017-04-12 17:46:44 +02:00
if (page != nullptr && page != page) {
_notebook->rearrangePage(page, 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-04-12 17:46:44 +02:00
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-04-12 17:46:44 +02:00
tree.put("title", getTitle().toStdString());
return tree;
2017-01-18 21:30:23 +01:00
}
2017-04-14 17:47:28 +02:00
} // namespace widgets
} // namespace chatterino