Clean up NotebookTab

This commit is contained in:
Rasmus Karlsson 2017-08-13 16:07:00 +02:00
parent 773208ac6b
commit eb3645a723
2 changed files with 99 additions and 91 deletions

View file

@ -8,18 +8,18 @@
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
NotebookTab::NotebookTab(Notebook *notebook) NotebookTab::NotebookTab(Notebook *_notebook)
: QWidget(notebook) : QWidget(_notebook)
, colorScheme(notebook->colorScheme) , colorScheme(_notebook->colorScheme)
, _posAnimation(this, "pos") , positionChangedAnimation(this, "pos")
, _notebook(notebook) , notebook(_notebook)
{ {
this->calcSize(); this->calcSize();
this->setAcceptDrops(true); this->setAcceptDrops(true);
_posAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic)); this->positionChangedAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
this->_hideXConnection = SettingsManager::getInstance().hideTabX.valueChanged.connect( this->hideXConnection = SettingsManager::getInstance().hideTabX.valueChanged.connect(
boost::bind(&NotebookTab::hideTabXChanged, this, _1)); boost::bind(&NotebookTab::hideTabXChanged, this, _1));
this->setMouseTracking(true); this->setMouseTracking(true);
@ -27,85 +27,89 @@ NotebookTab::NotebookTab(Notebook *notebook)
NotebookTab::~NotebookTab() NotebookTab::~NotebookTab()
{ {
this->_hideXConnection.disconnect(); this->hideXConnection.disconnect();
} }
void NotebookTab::calcSize() void NotebookTab::calcSize()
{ {
if (SettingsManager::getInstance().hideTabX.get()) { if (SettingsManager::getInstance().hideTabX.get()) {
resize(fontMetrics().width(_title) + 8, 24); this->resize(fontMetrics().width(title) + 8, 24);
} else { } else {
resize(fontMetrics().width(_title) + 8 + 24, 24); this->resize(fontMetrics().width(title) + 8 + 24, 24);
} }
if (parent() != nullptr) { if (this->parent() != nullptr) {
((Notebook *)parent())->performLayout(true); (static_cast<Notebook *>(this->parent()))->performLayout(true);
} }
} }
const QString &NotebookTab::getTitle() const const QString &NotebookTab::getTitle() const
{ {
return _title; return title;
} }
void NotebookTab::setTitle(const QString &title) void NotebookTab::setTitle(const QString &newTitle)
{ {
_title = title; this->title = newTitle;
} }
bool NotebookTab::getSelected() bool NotebookTab::isSelected() const
{ {
return _selected; return this->selected;
} }
void NotebookTab::setSelected(bool value) void NotebookTab::setSelected(bool value)
{ {
_selected = value; this->selected = value;
update();
this->update();
} }
NotebookTab::HighlightStyle NotebookTab::getHighlightStyle() const NotebookTab::HighlightStyle NotebookTab::getHighlightStyle() const
{ {
return _highlightStyle; return this->highlightStyle;
} }
void NotebookTab::setHighlightStyle(HighlightStyle style) void NotebookTab::setHighlightStyle(HighlightStyle newHighlightStyle)
{ {
_highlightStyle = style; this->highlightStyle = newHighlightStyle;
update();
this->update();
} }
QRect NotebookTab::getDesiredRect() const QRect NotebookTab::getDesiredRect() const
{ {
return QRect(_posAnimationDesired, size()); return QRect(positionAnimationDesiredPoint, size());
} }
void NotebookTab::hideTabXChanged(bool) void NotebookTab::hideTabXChanged(bool)
{ {
calcSize(); this->calcSize();
update(); this->update();
} }
void NotebookTab::moveAnimated(QPoint pos, bool animated) void NotebookTab::moveAnimated(QPoint pos, bool animated)
{ {
_posAnimationDesired = pos; this->positionAnimationDesiredPoint = pos;
if ((window() != nullptr && !window()->isVisible()) || !animated || _posAnimated == false) { QWidget *w = this->window();
move(pos);
_posAnimated = true; if ((w != nullptr && !w->isVisible()) || !animated || !positionChangedAnimationRunning) {
this->move(pos);
this->positionChangedAnimationRunning = true;
return; return;
} }
if (_posAnimation.endValue() == pos) { if (this->positionChangedAnimation.endValue() == pos) {
return; return;
} }
_posAnimation.stop(); this->positionChangedAnimation.stop();
_posAnimation.setDuration(75); this->positionChangedAnimation.setDuration(75);
_posAnimation.setStartValue(this->pos()); this->positionChangedAnimation.setStartValue(this->pos());
_posAnimation.setEndValue(pos); this->positionChangedAnimation.setEndValue(pos);
_posAnimation.start(); this->positionChangedAnimation.start();
} }
void NotebookTab::paintEvent(QPaintEvent *) void NotebookTab::paintEvent(QPaintEvent *)
@ -114,16 +118,16 @@ void NotebookTab::paintEvent(QPaintEvent *)
QColor fg = QColor(0, 0, 0); QColor fg = QColor(0, 0, 0);
if (_selected) { if (this->selected) {
painter.fillRect(rect(), this->colorScheme.TabSelectedBackground); painter.fillRect(rect(), this->colorScheme.TabSelectedBackground);
fg = this->colorScheme.TabSelectedText; fg = this->colorScheme.TabSelectedText;
} else if (_mouseOver) { } else if (this->mouseOver) {
painter.fillRect(rect(), this->colorScheme.TabHoverBackground); painter.fillRect(rect(), this->colorScheme.TabHoverBackground);
fg = this->colorScheme.TabHoverText; fg = this->colorScheme.TabHoverText;
} else if (_highlightStyle == HighlightHighlighted) { } else if (this->highlightStyle == HighlightHighlighted) {
painter.fillRect(rect(), this->colorScheme.TabHighlightedBackground); painter.fillRect(rect(), this->colorScheme.TabHighlightedBackground);
fg = this->colorScheme.TabHighlightedText; fg = this->colorScheme.TabHighlightedText;
} else if (_highlightStyle == HighlightNewMessage) { } else if (this->highlightStyle == HighlightNewMessage) {
painter.fillRect(rect(), this->colorScheme.TabNewMessageBackground); painter.fillRect(rect(), this->colorScheme.TabNewMessageBackground);
fg = this->colorScheme.TabHighlightedText; fg = this->colorScheme.TabHighlightedText;
} else { } else {
@ -133,87 +137,91 @@ void NotebookTab::paintEvent(QPaintEvent *)
painter.setPen(fg); painter.setPen(fg);
QRect rect(0, 0, width() - (SettingsManager::getInstance().hideTabX.get() ? 0 : 16), height()); QRect rect(0, 0, this->width() - (SettingsManager::getInstance().hideTabX.get() ? 0 : 16),
this->height());
painter.drawText(rect, _title, QTextOption(Qt::AlignCenter)); painter.drawText(rect, title, QTextOption(Qt::AlignCenter));
if (!SettingsManager::getInstance().hideTabX.get() && (_mouseOver || _selected)) { if (!SettingsManager::getInstance().hideTabX.get() && (mouseOver || selected)) {
if (_mouseOverX) { QRect xRect = this->getXRect();
painter.fillRect(getXRect(), QColor(0, 0, 0, 64)); if (mouseOverX) {
painter.fillRect(xRect, QColor(0, 0, 0, 64));
if (_mouseDownX) { if (mouseDownX) {
painter.fillRect(getXRect(), QColor(0, 0, 0, 64)); painter.fillRect(xRect, QColor(0, 0, 0, 64));
} }
} }
painter.drawLine(getXRect().topLeft() + QPoint(4, 4), painter.drawLine(xRect.topLeft() + QPoint(4, 4), xRect.bottomRight() + QPoint(-4, -4));
getXRect().bottomRight() + QPoint(-4, -4)); painter.drawLine(xRect.topRight() + QPoint(-4, 4), xRect.bottomLeft() + QPoint(4, -4));
painter.drawLine(getXRect().topRight() + QPoint(-4, 4),
getXRect().bottomLeft() + QPoint(4, -4));
} }
} }
void NotebookTab::mousePressEvent(QMouseEvent *event) void NotebookTab::mousePressEvent(QMouseEvent *event)
{ {
_mouseDown = true; this->mouseDown = true;
_mouseDownX = getXRect().contains(event->pos()); this->mouseDownX = this->getXRect().contains(event->pos());
update(); this->update();
_notebook->select(page); this->notebook->select(page);
} }
void NotebookTab::mouseReleaseEvent(QMouseEvent *event) void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
{ {
_mouseDown = false; this->mouseDown = false;
if (!SettingsManager::getInstance().hideTabX.get() && _mouseDownX && if (!SettingsManager::getInstance().hideTabX.get() && this->mouseDownX &&
getXRect().contains(event->pos())) { this->getXRect().contains(event->pos())) {
_mouseDownX = false; this->mouseDownX = false;
_notebook->removePage(page); this->notebook->removePage(this->page);
} else { } else {
update(); this->update();
} }
} }
void NotebookTab::enterEvent(QEvent *) void NotebookTab::enterEvent(QEvent *)
{ {
_mouseOver = true; this->mouseOver = true;
update(); this->update();
} }
void NotebookTab::leaveEvent(QEvent *) void NotebookTab::leaveEvent(QEvent *)
{ {
_mouseOverX = _mouseOver = false; this->mouseOverX = false;
this->mouseOver = false;
update(); this->update();
} }
void NotebookTab::dragEnterEvent(QDragEnterEvent *) void NotebookTab::dragEnterEvent(QDragEnterEvent *)
{ {
_notebook->select(page); this->notebook->select(this->page);
} }
void NotebookTab::mouseMoveEvent(QMouseEvent *event) void NotebookTab::mouseMoveEvent(QMouseEvent *event)
{ {
bool overX = getXRect().contains(event->pos()); if (!SettingsManager::getInstance().hideTabX.get()) {
bool overX = this->getXRect().contains(event->pos());
if (overX != _mouseOverX) { if (overX != this->mouseOverX) {
_mouseOverX = overX && !SettingsManager::getInstance().hideTabX.get(); // Over X state has been changed (we either left or entered it;
this->mouseOverX = overX;
update(); this->update();
}
} }
if (_mouseDown && !getDesiredRect().contains(event->pos())) { if (this->mouseDown && !this->getDesiredRect().contains(event->pos())) {
QPoint relPoint = mapToParent(event->pos()); QPoint relPoint = this->mapToParent(event->pos());
int index; int index;
NotebookPage *clickedPage = _notebook->tabAt(relPoint, index); NotebookPage *clickedPage = notebook->tabAt(relPoint, index);
if (clickedPage != nullptr && clickedPage != page) { if (clickedPage != nullptr && clickedPage != this->page) {
_notebook->rearrangePage(clickedPage, index); this->notebook->rearrangePage(clickedPage, index);
} }
} }
} }
@ -222,7 +230,7 @@ void NotebookTab::load(const boost::property_tree::ptree &tree)
{ {
// Load tab title // Load tab title
try { try {
setTitle(QString::fromStdString(tree.get<std::string>("title"))); this->setTitle(QString::fromStdString(tree.get<std::string>("title")));
} catch (boost::property_tree::ptree_error) { } catch (boost::property_tree::ptree_error) {
} }
} }
@ -231,7 +239,7 @@ boost::property_tree::ptree NotebookTab::save()
{ {
boost::property_tree::ptree tree; boost::property_tree::ptree tree;
tree.put("title", getTitle().toStdString()); tree.put("title", this->getTitle().toStdString());
return tree; return tree;
} }

View file

@ -32,8 +32,8 @@ public:
NotebookPage *page; NotebookPage *page;
const QString &getTitle() const; const QString &getTitle() const;
void setTitle(const QString &title); void setTitle(const QString &newTitle);
bool getSelected(); bool isSelected() const;
void setSelected(bool value); void setSelected(bool value);
HighlightStyle getHighlightStyle() const; HighlightStyle getHighlightStyle() const;
@ -57,23 +57,23 @@ protected:
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
private: private:
boost::signals2::connection _hideXConnection; boost::signals2::connection hideXConnection;
QPropertyAnimation _posAnimation; QPropertyAnimation positionChangedAnimation;
bool _posAnimated = false; bool positionChangedAnimationRunning = false;
QPoint _posAnimationDesired; QPoint positionAnimationDesiredPoint;
Notebook *_notebook; Notebook *notebook;
QString _title = "<no title>"; QString title = "<no title>";
bool _selected = false; bool selected = false;
bool _mouseOver = false; bool mouseOver = false;
bool _mouseDown = false; bool mouseDown = false;
bool _mouseOverX = false; bool mouseOverX = false;
bool _mouseDownX = false; bool mouseDownX = false;
HighlightStyle _highlightStyle = HighlightStyle::HighlightNone; HighlightStyle highlightStyle = HighlightStyle::HighlightNone;
QRect getXRect() QRect getXRect()
{ {