applied code style to notebooktab

This commit is contained in:
fourtf 2018-06-01 14:57:18 +02:00
parent 449d410ce0
commit 9e36af26fa
2 changed files with 96 additions and 105 deletions

View file

@ -13,30 +13,31 @@
#include <QApplication> #include <QApplication>
#include <QDebug> #include <QDebug>
#include <QLinearGradient> #include <QLinearGradient>
#include <QMimeData>
#include <QPainter> #include <QPainter>
#include <boost/bind.hpp> #include <boost/bind.hpp>
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
NotebookTab::NotebookTab(Notebook *_notebook) NotebookTab::NotebookTab(Notebook *notebook)
: BaseWidget(_notebook) : BaseWidget(notebook)
, positionChangedAnimation(this, "pos") , positionChangedAnimation_(this, "pos")
, notebook(_notebook) , notebook_(notebook)
, menu(this) , menu_(this)
{ {
auto app = getApp(); auto app = getApp();
this->setAcceptDrops(true); this->setAcceptDrops(true);
this->positionChangedAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic)); this->positionChangedAnimation_.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
app->settings->showTabCloseButton.connect(boost::bind(&NotebookTab::hideTabXChanged, this, _1), app->settings->showTabCloseButton.connect(boost::bind(&NotebookTab::hideTabXChanged, this, _1),
this->managedConnections); this->managedConnections_);
this->setMouseTracking(true); this->setMouseTracking(true);
this->menu.addAction("Rename", [this]() { this->menu_.addAction("Rename", [this]() {
TextInputDialog d(this); TextInputDialog d(this);
d.setWindowTitle("Change tab title (Leave empty for default behaviour)"); d.setWindowTitle("Change tab title (Leave empty for default behaviour)");
@ -65,7 +66,7 @@ NotebookTab::NotebookTab(Notebook *_notebook)
// new QAction("Enable highlights on new message", &this->menu); // new QAction("Enable highlights on new message", &this->menu);
// enableHighlightsOnNewMessageAction->setCheckable(true); // enableHighlightsOnNewMessageAction->setCheckable(true);
this->menu.addAction("Close", [=]() { this->notebook->removePage(this->page); }); this->menu_.addAction("Close", [=]() { this->notebook_->removePage(this->page); });
// this->menu.addAction(enableHighlightsOnNewMessageAction); // this->menu.addAction(enableHighlightsOnNewMessageAction);
@ -86,31 +87,31 @@ void NotebookTab::updateSize()
int width; int width;
QFontMetrics metrics = getApp()->fonts->getFontMetrics( QFontMetrics metrics = getApp()->fonts->getFontMetrics(
FontStyle::UiTabs, float(this->getScale() * this->devicePixelRatioF())); FontStyle::UiTabs, float(qreal(this->getScale()) * this->devicePixelRatioF()));
if (this->hasXButton()) { if (this->hasXButton()) {
width = int((metrics.width(this->title) + 32) * scale); width = int((metrics.width(this->title_) + 32) * scale);
} else { } else {
width = int((metrics.width(this->title) + 16) * scale); width = int((metrics.width(this->title_) + 16) * scale);
} }
width = std::max<int>(this->height(), std::min(int(150 * scale), width)); width = std::max<int>(this->height(), std::min(int(150 * scale), width));
if (this->width() != width) { if (this->width() != width) {
this->resize(width, int(NOTEBOOK_TAB_HEIGHT * scale)); this->resize(width, int(NOTEBOOK_TAB_HEIGHT * scale));
this->notebook->performLayout(); this->notebook_->performLayout();
} }
} }
const QString &NotebookTab::getTitle() const const QString &NotebookTab::getTitle() const
{ {
return this->title; return this->title_;
} }
void NotebookTab::setTitle(const QString &newTitle) void NotebookTab::setTitle(const QString &newTitle)
{ {
if (this->title != newTitle) { if (this->title_ != newTitle) {
this->title = newTitle; this->title_ = newTitle;
this->updateSize(); this->updateSize();
this->update(); this->update();
} }
@ -118,14 +119,14 @@ void NotebookTab::setTitle(const QString &newTitle)
bool NotebookTab::isSelected() const bool NotebookTab::isSelected() const
{ {
return this->selected; return this->selected_;
} }
void NotebookTab::setSelected(bool value) void NotebookTab::setSelected(bool value)
{ {
this->selected = value; this->selected_ = value;
this->highlightState = HighlightState::None; this->highlightState_ = HighlightState::None;
this->update(); this->update();
} }
@ -136,8 +137,8 @@ void NotebookTab::setHighlightState(HighlightState newHighlightStyle)
return; return;
} }
if (this->highlightState != HighlightState::Highlighted) { if (this->highlightState_ != HighlightState::Highlighted) {
this->highlightState = newHighlightStyle; this->highlightState_ = newHighlightStyle;
this->update(); this->update();
} }
@ -145,7 +146,7 @@ void NotebookTab::setHighlightState(HighlightState newHighlightStyle)
QRect NotebookTab::getDesiredRect() const QRect NotebookTab::getDesiredRect() const
{ {
return QRect(positionAnimationDesiredPoint, size()); return QRect(this->positionAnimationDesiredPoint_, size());
} }
void NotebookTab::hideTabXChanged(bool) void NotebookTab::hideTabXChanged(bool)
@ -156,26 +157,26 @@ void NotebookTab::hideTabXChanged(bool)
void NotebookTab::moveAnimated(QPoint pos, bool animated) void NotebookTab::moveAnimated(QPoint pos, bool animated)
{ {
this->positionAnimationDesiredPoint = pos; this->positionAnimationDesiredPoint_ = pos;
QWidget *w = this->window(); QWidget *w = this->window();
if ((w != nullptr && !w->isVisible()) || !animated || !positionChangedAnimationRunning) { if ((w != nullptr && !w->isVisible()) || !animated || !this->positionChangedAnimationRunning_) {
this->move(pos); this->move(pos);
this->positionChangedAnimationRunning = true; this->positionChangedAnimationRunning_ = true;
return; return;
} }
if (this->positionChangedAnimation.endValue() == pos) { if (this->positionChangedAnimation_.endValue() == pos) {
return; return;
} }
this->positionChangedAnimation.stop(); this->positionChangedAnimation_.stop();
this->positionChangedAnimation.setDuration(75); this->positionChangedAnimation_.setDuration(75);
this->positionChangedAnimation.setStartValue(this->pos()); this->positionChangedAnimation_.setStartValue(this->pos());
this->positionChangedAnimation.setEndValue(pos); this->positionChangedAnimation_.setEndValue(pos);
this->positionChangedAnimation.start(); this->positionChangedAnimation_.start();
} }
void NotebookTab::paintEvent(QPaintEvent *) void NotebookTab::paintEvent(QPaintEvent *)
@ -195,11 +196,11 @@ void NotebookTab::paintEvent(QPaintEvent *)
singletons::ThemeManager::TabColors colors; singletons::ThemeManager::TabColors colors;
singletons::ThemeManager::TabColors regular = this->themeManager->tabs.regular; singletons::ThemeManager::TabColors regular = this->themeManager->tabs.regular;
if (this->selected) { if (this->selected_) {
colors = this->themeManager->tabs.selected; colors = this->themeManager->tabs.selected;
} else if (this->highlightState == HighlightState::Highlighted) { } else if (this->highlightState_ == HighlightState::Highlighted) {
colors = this->themeManager->tabs.highlighted; colors = this->themeManager->tabs.highlighted;
} else if (this->highlightState == HighlightState::NewMessage) { } else if (this->highlightState_ == HighlightState::NewMessage) {
colors = this->themeManager->tabs.newMessage; colors = this->themeManager->tabs.newMessage;
} else { } else {
colors = this->themeManager->tabs.regular; colors = this->themeManager->tabs.regular;
@ -208,13 +209,13 @@ void NotebookTab::paintEvent(QPaintEvent *)
bool windowFocused = this->window() == QApplication::activeWindow(); bool windowFocused = this->window() == QApplication::activeWindow();
// || SettingsDialog::getHandle() == QApplication::activeWindow(); // || SettingsDialog::getHandle() == QApplication::activeWindow();
QBrush tabBackground = this->mouseOver ? colors.backgrounds.hover QBrush tabBackground = this->mouseOver_ ? colors.backgrounds.hover
: (windowFocused ? colors.backgrounds.regular : (windowFocused ? colors.backgrounds.regular
: colors.backgrounds.unfocused); : colors.backgrounds.unfocused);
painter.fillRect(rect(), this->mouseOver ? regular.backgrounds.hover painter.fillRect(rect(), this->mouseOver_ ? regular.backgrounds.hover
: (windowFocused ? regular.backgrounds.regular : (windowFocused ? regular.backgrounds.regular
: regular.backgrounds.unfocused)); : regular.backgrounds.unfocused));
// fill the tab background // fill the tab background
painter.fillRect(rect(), tabBackground); painter.fillRect(rect(), tabBackground);
@ -229,9 +230,9 @@ void NotebookTab::paintEvent(QPaintEvent *)
// painter.drawPath(path); // painter.drawPath(path);
// top line // top line
painter.fillRect(QRectF(0, (this->selected ? 0.f : 1.f) * scale, this->width(), painter.fillRect(QRectF(0, (this->selected_ ? 0.f : 1.f) * scale, this->width(),
(this->selected ? 2.f : 1.f) * scale), (this->selected_ ? 2.f : 1.f) * scale),
this->mouseOver this->mouseOver_
? colors.line.hover ? colors.line.hover
: (windowFocused ? colors.line.regular : colors.line.unfocused)); : (windowFocused ? colors.line.regular : colors.line.unfocused));
@ -243,40 +244,31 @@ void NotebookTab::paintEvent(QPaintEvent *)
QRect rect(0, 0, this->width() - rectW, height); QRect rect(0, 0, this->width() - rectW, height);
// draw text // draw text
if (true) { // legacy int offset = int(scale * 8);
// painter.drawText(rect, this->getTitle(), QTextOption(Qt::AlignCenter)); QRect textRect(offset, this->selected_ ? 1 : 2, this->width() - offset - offset, height);
int offset = int(scale * 8);
QRect textRect(offset, this->selected ? 1 : 2, this->width() - offset - offset, height);
if (this->shouldDrawXButton()) { if (this->shouldDrawXButton()) {
textRect.setRight(textRect.right() - this->height() / 2); textRect.setRight(textRect.right() - this->height() / 2);
}
int width = metrics.width(this->getTitle());
Qt::Alignment alignment = width > textRect.width() ? Qt::AlignLeft | Qt::AlignVCenter
: Qt::AlignHCenter | Qt::AlignVCenter;
QTextOption option(alignment);
option.setWrapMode(QTextOption::NoWrap);
painter.drawText(textRect, this->getTitle(), option);
} else {
// QTextOption option(Qt::AlignLeft | Qt::AlignVCenter);
// option.setWrapMode(QTextOption::NoWrap);
// int offset = (int)(scale * 16);
// QRect textRect(offset, 0, this->width() - offset - offset, height);
// painter.drawText(textRect, this->getTitle(), option);
} }
int width = metrics.width(this->getTitle());
Qt::Alignment alignment = width > textRect.width() ? Qt::AlignLeft | Qt::AlignVCenter
: Qt::AlignHCenter | Qt::AlignVCenter;
QTextOption option(alignment);
option.setWrapMode(QTextOption::NoWrap);
painter.drawText(textRect, this->getTitle(), option);
// draw close x // draw close x
if (this->shouldDrawXButton()) { if (this->shouldDrawXButton()) {
QRect xRect = this->getXRect(); QRect xRect = this->getXRect();
if (!xRect.isNull()) { if (!xRect.isNull()) {
painter.setBrush(QColor("#fff")); painter.setBrush(QColor("#fff"));
if (mouseOverX) { if (this->mouseOverX_) {
painter.fillRect(xRect, QColor(0, 0, 0, 64)); painter.fillRect(xRect, QColor(0, 0, 0, 64));
if (mouseDownX) { if (this->mouseDownX_) {
painter.fillRect(xRect, QColor(0, 0, 0, 64)); painter.fillRect(xRect, QColor(0, 0, 0, 64));
} }
} }
@ -289,42 +281,43 @@ void NotebookTab::paintEvent(QPaintEvent *)
} }
// draw line at bottom // draw line at bottom
if (!this->selected) { if (!this->selected_) {
painter.fillRect(0, this->height() - 1, this->width(), 1, app->themes->window.background); painter.fillRect(0, this->height() - 1, this->width(), 1, app->themes->window.background);
} }
} }
bool NotebookTab::hasXButton() bool NotebookTab::hasXButton()
{ {
return getApp()->settings->showTabCloseButton && this->notebook->getAllowUserTabManagement(); return getApp()->settings->showTabCloseButton && this->notebook_->getAllowUserTabManagement();
} }
bool NotebookTab::shouldDrawXButton() bool NotebookTab::shouldDrawXButton()
{ {
return this->hasXButton() && (mouseOver || selected); return this->hasXButton() && (this->mouseOver_ || this->selected_);
} }
void NotebookTab::mousePressEvent(QMouseEvent *event) void NotebookTab::mousePressEvent(QMouseEvent *event)
{ {
this->mouseDown = true; this->mouseDown_ = true;
this->mouseDownX = this->getXRect().contains(event->pos()); this->mouseDownX_ = this->getXRect().contains(event->pos());
this->update(); this->update();
this->notebook->select(page); this->notebook_->select(page);
if (this->notebook->getAllowUserTabManagement()) { if (this->notebook_->getAllowUserTabManagement()) {
switch (event->button()) { switch (event->button()) {
case Qt::RightButton: { case Qt::RightButton: {
this->menu.popup(event->globalPos()); this->menu_.popup(event->globalPos());
} break; } break;
default:;
} }
} }
} }
void NotebookTab::mouseReleaseEvent(QMouseEvent *event) void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
{ {
this->mouseDown = false; this->mouseDown_ = false;
auto removeThisPage = [this] { auto removeThisPage = [this] {
auto reply = QMessageBox::question(this, "Remove this tab", auto reply = QMessageBox::question(this, "Remove this tab",
@ -332,7 +325,7 @@ void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
QMessageBox::Yes | QMessageBox::Cancel); QMessageBox::Yes | QMessageBox::Cancel);
if (reply == QMessageBox::Yes) { if (reply == QMessageBox::Yes) {
this->notebook->removePage(this->page); this->notebook_->removePage(this->page);
} }
}; };
@ -341,8 +334,8 @@ void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
removeThisPage(); removeThisPage();
} }
} else { } else {
if (this->hasXButton() && this->mouseDownX && this->getXRect().contains(event->pos())) { if (this->hasXButton() && this->mouseDownX_ && this->getXRect().contains(event->pos())) {
this->mouseDownX = false; this->mouseDownX_ = false;
removeThisPage(); removeThisPage();
} else { } else {
@ -353,15 +346,15 @@ void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
void NotebookTab::enterEvent(QEvent *) void NotebookTab::enterEvent(QEvent *)
{ {
this->mouseOver = true; this->mouseOver_ = true;
this->update(); this->update();
} }
void NotebookTab::leaveEvent(QEvent *) void NotebookTab::leaveEvent(QEvent *)
{ {
this->mouseOverX = false; this->mouseOverX_ = false;
this->mouseOver = false; this->mouseOver_ = false;
this->update(); this->update();
} }
@ -374,8 +367,8 @@ void NotebookTab::dragEnterEvent(QDragEnterEvent *event)
if (!SplitContainer::isDraggingSplit) if (!SplitContainer::isDraggingSplit)
return; return;
if (this->notebook->getAllowUserTabManagement()) { if (this->notebook_->getAllowUserTabManagement()) {
this->notebook->select(this->page); this->notebook_->select(this->page);
} }
} }
@ -383,13 +376,13 @@ void NotebookTab::mouseMoveEvent(QMouseEvent *event)
{ {
auto app = getApp(); auto app = getApp();
if (app->settings->showTabCloseButton && this->notebook->getAllowUserTabManagement()) // if (app->settings->showTabCloseButton && this->notebook_->getAllowUserTabManagement()) //
{ {
bool overX = this->getXRect().contains(event->pos()); bool overX = this->getXRect().contains(event->pos());
if (overX != this->mouseOverX) { if (overX != this->mouseOverX_) {
// Over X state has been changed (we either left or entered it; // Over X state has been changed (we either left or entered it;
this->mouseOverX = overX; this->mouseOverX_ = overX;
this->update(); this->update();
} }
@ -397,16 +390,14 @@ void NotebookTab::mouseMoveEvent(QMouseEvent *event)
QPoint relPoint = this->mapToParent(event->pos()); QPoint relPoint = this->mapToParent(event->pos());
if (this->mouseDown && !this->getDesiredRect().contains(relPoint) && if (this->mouseDown_ && !this->getDesiredRect().contains(relPoint) &&
this->notebook->getAllowUserTabManagement()) // this->notebook_->getAllowUserTabManagement()) //
{ {
int index; int index;
QWidget *clickedPage = notebook->tabAt(relPoint, index, this->width()); QWidget *clickedPage = this->notebook_->tabAt(relPoint, index, this->width());
// assert(clickedPage);
if (clickedPage != nullptr && clickedPage != this->page) { if (clickedPage != nullptr && clickedPage != this->page) {
this->notebook->rearrangePage(this->page, index); this->notebook_->rearrangePage(this->page, index);
} }
} }
} }

View file

@ -55,32 +55,32 @@ protected:
virtual void mouseMoveEvent(QMouseEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override;
private: private:
std::vector<pajlada::Signals::ScopedConnection> managedConnections; std::vector<pajlada::Signals::ScopedConnection> managedConnections_;
QPropertyAnimation positionChangedAnimation; QPropertyAnimation positionChangedAnimation_;
bool positionChangedAnimationRunning = false; bool positionChangedAnimationRunning_ = false;
QPoint positionAnimationDesiredPoint; QPoint positionAnimationDesiredPoint_;
Notebook *notebook; Notebook *notebook_;
QString title; QString title_;
public: public:
bool useDefaultTitle = true; bool useDefaultTitle = true;
private: private:
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;
bool hasXButton(); bool hasXButton();
bool shouldDrawXButton(); bool shouldDrawXButton();
HighlightState highlightState = HighlightState::None; HighlightState highlightState_ = HighlightState::None;
QMenu menu; QMenu menu_;
QRect getXRect(); QRect getXRect();
}; };