mirror-chatterino2/src/widgets/helper/NotebookButton.cpp

223 lines
5.6 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "widgets/helper/NotebookButton.hpp"
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
#include "common/QLogging.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-08-08 15:35:54 +02:00
#include "widgets/helper/Button.hpp"
#include "widgets/Notebook.hpp"
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
#include "widgets/splits/DraggedSplit.hpp"
#include "widgets/splits/Split.hpp"
#include "widgets/splits/SplitContainer.hpp"
2016-12-30 12:19:31 +01:00
#include <QMimeData>
2017-01-18 04:52:47 +01:00
#include <QMouseEvent>
#include <QPainter>
#include <QPainterPath>
2017-04-12 17:46:44 +02:00
#include <QRadialGradient>
2017-01-18 04:52:47 +01:00
2018-01-24 22:09:26 +01:00
#define nuuls nullptr
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
NotebookButton::NotebookButton(Notebook *parent)
2018-08-08 15:35:54 +02:00
: Button(parent)
, parent_(parent)
2016-12-30 12:19:31 +01:00
{
2018-01-24 22:09:26 +01:00
this->setAcceptDrops(true);
2016-12-30 12:19:31 +01:00
}
2016-12-30 12:20:26 +01:00
void NotebookButton::setIcon(Icon icon)
{
this->icon_ = icon;
this->update();
}
NotebookButton::Icon NotebookButton::getIcon() const
{
return this->icon_;
}
2018-07-06 17:11:37 +02:00
void NotebookButton::themeChangedEvent()
2018-04-05 23:44:46 +02:00
{
2018-07-06 17:11:37 +02:00
this->setMouseEffectColor(this->theme->tabs.regular.text);
2018-04-05 23:44:46 +02:00
}
void NotebookButton::paintEvent(QPaintEvent *event)
2016-12-30 12:20:26 +01:00
{
QPainter painter(this);
QColor background;
QColor foreground;
2018-10-21 13:43:02 +02:00
if (mouseDown_ || mouseOver_)
{
2023-03-17 20:53:03 +01:00
background = this->theme->tabs.regular.backgrounds.hover;
2018-07-06 17:11:37 +02:00
foreground = this->theme->tabs.regular.text;
2018-10-21 13:43:02 +02:00
}
else
{
2023-03-17 20:53:03 +01:00
background = this->theme->tabs.regular.backgrounds.regular;
2018-07-06 17:11:37 +02:00
foreground = this->theme->tabs.regular.text;
2016-12-30 12:20:26 +01:00
}
2017-01-01 02:30:42 +01:00
painter.setPen(Qt::NoPen);
2016-12-30 12:20:26 +01:00
2017-04-12 17:46:44 +02:00
float h = height(), w = width();
2016-12-30 12:20:26 +01:00
2018-10-21 13:43:02 +02:00
switch (icon_)
{
2019-09-26 00:51:05 +02:00
case Plus: {
painter.setPen([&] {
QColor tmp = foreground;
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
if (isDraggingSplit())
2018-10-21 13:43:02 +02:00
{
tmp = this->theme->tabs.selected.line.regular;
2018-10-21 13:43:02 +02:00
}
else if (!this->mouseOver_)
{
tmp.setAlpha(180);
}
return tmp;
}());
QRect rect = this->rect();
int s = h * 4 / 9;
2018-08-06 21:17:03 +02:00
painter.drawLine(rect.left() + rect.width() / 2 - (s / 2),
rect.top() + rect.height() / 2,
rect.left() + rect.width() / 2 + (s / 2),
rect.top() + rect.height() / 2);
painter.drawLine(rect.left() + rect.width() / 2,
rect.top() + rect.height() / 2 - (s / 2),
rect.left() + rect.width() / 2,
rect.top() + rect.height() / 2 + (s / 2));
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case User: {
painter.setRenderHint(QPainter::Antialiasing);
auto a = w / 8;
QPainterPath path;
path.arcMoveTo(a, 4 * a, 6 * a, 6 * a, 0);
path.arcTo(a, 4 * a, 6 * a, 6 * a, 0, 180);
2020-02-28 19:48:04 +01:00
QPainterPath remove;
remove.addEllipse(2 * a, 1 * a, 4 * a, 4 * a);
path = path.subtracted(remove);
2020-02-28 19:52:15 +01:00
path.addEllipse(2.5 * a, 1.5 * a, 3 * a, 3 * a);
2020-02-28 19:48:04 +01:00
painter.fillPath(path, foreground);
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case Settings: {
painter.setRenderHint(QPainter::Antialiasing);
auto a = w / 8;
QPainterPath path;
path.arcMoveTo(a, a, 6 * a, 6 * a, 0 - (360 / 32.0));
2018-10-21 13:43:02 +02:00
for (int i = 0; i < 8; i++)
{
2018-08-06 21:17:03 +02:00
path.arcTo(a, a, 6 * a, 6 * a, i * (360 / 8.0) - (360 / 32.0),
(360 / 32.0));
2018-08-06 21:17:03 +02:00
path.arcTo(2 * a, 2 * a, 4 * a, 4 * a,
i * (360 / 8.0) + (360 / 32.0), (360 / 32.0));
}
2017-01-01 02:30:42 +01:00
2020-02-28 19:48:04 +01:00
QPainterPath remove;
remove.addEllipse(3 * a, 3 * a, 2 * a, 2 * a);
2016-12-30 12:20:26 +01:00
2020-02-28 19:48:04 +01:00
painter.fillPath(path.subtracted(remove), foreground);
2018-10-21 13:43:02 +02:00
}
break;
2017-01-01 02:30:42 +01:00
default:;
2016-12-30 12:20:26 +01:00
}
2018-08-08 15:35:54 +02:00
Button::paintEvent(event);
2016-12-30 12:20:26 +01:00
}
2017-04-12 17:46:44 +02:00
void NotebookButton::mouseReleaseEvent(QMouseEvent *event)
2016-12-30 12:20:26 +01:00
{
2018-10-21 13:43:02 +02:00
if (event->button() == Qt::LeftButton)
{
2018-06-06 10:46:23 +02:00
mouseDown_ = false;
2016-12-30 12:20:26 +01:00
2017-04-12 17:46:44 +02:00
update();
2017-01-01 18:43:52 +01:00
emit leftClicked();
2017-01-01 18:43:52 +01:00
}
2016-12-30 12:20:26 +01:00
2018-08-08 15:35:54 +02:00
Button::mouseReleaseEvent(event);
2016-12-30 12:20:26 +01:00
}
2018-01-24 22:09:26 +01:00
void NotebookButton::dragEnterEvent(QDragEnterEvent *event)
{
2018-10-21 13:43:02 +02:00
if (!event->mimeData()->hasFormat("chatterino/split"))
2018-01-24 22:09:26 +01:00
return;
event->acceptProposedAction();
auto e = new QMouseEvent(QMouseEvent::MouseButtonPress,
2018-08-06 21:17:03 +02:00
QPointF(this->width() / 2, this->height() / 2),
Qt::LeftButton, Qt::LeftButton, {});
2018-08-08 15:35:54 +02:00
Button::mousePressEvent(e);
2018-01-24 22:09:26 +01:00
delete e;
}
void NotebookButton::dragLeaveEvent(QDragLeaveEvent *)
{
2018-06-06 10:46:23 +02:00
this->mouseDown_ = true;
2018-01-24 22:09:26 +01:00
this->update();
auto e = new QMouseEvent(QMouseEvent::MouseButtonRelease,
2018-08-06 21:17:03 +02:00
QPointF(this->width() / 2, this->height() / 2),
Qt::LeftButton, Qt::LeftButton, {});
2018-08-08 15:35:54 +02:00
Button::mouseReleaseEvent(e);
2018-01-24 22:09:26 +01:00
delete e;
}
void NotebookButton::dropEvent(QDropEvent *event)
{
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
auto *draggedSplit = dynamic_cast<Split *>(event->source());
if (!draggedSplit)
2018-10-21 13:43:02 +02:00
{
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
qCDebug(chatterinoWidget)
<< "Dropped something that wasn't a split onto a notebook button";
return;
}
2018-01-24 22:09:26 +01:00
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
auto *notebook = dynamic_cast<Notebook *>(this->parentWidget());
if (!notebook)
{
qCDebug(chatterinoWidget) << "Dropped a split onto a notebook button "
"without a parent notebook";
return;
}
2018-01-24 22:09:26 +01:00
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
event->acceptProposedAction();
2018-01-24 22:09:26 +01:00
SplitContainer refactor (#4261) * Remove unused include util/Helpers.hpp * SplitContainer::setTag fix parameter naming * autofy/constify where possible * More const auto ptr magicifying * Make SplitNode::Type an enum class * Move QuickSwitcherPopup includes from header to source file * Remove unused DropRegion code * use empty() instead of size() == 0 * Add curly braces everywhere * Remove useless reinterpret_cast It was casting Node* to Node* * Clarify that the connect is QObject::connect * SplitContainer::setSelected fix parameter naming * Rename function variables to remove unneccesary underscore Also move addSpacing parameter out of the layout function * emplace_back where possible * Name parameters * Remove ineffective const from return type * Make node getters const * Flatten Node::releaseSplit * Rename in-function variable to match code style * [ACTUAL CODE CHANGE/MOVE] Move clamp logic to its own function * name params * applyFromDescriptorRecursively: rename node param to baseNode * [ACTUAL CODE CHANGE/MOVE] Remove the many overloads for append/insertSplit This utilizes the C++20 designed initializers aggregate initialization feature * Remove unused includes * [ACTUAL CODE CHANGE] Clean up dragging logic There's no need to keep a pointer around to which split is being dragged, it's already stored in the QDropEvent source() * UNRELATED .clang-tidy: Only suggest UPPER_CASE for constant global variables * Remove unused SplitContainer::getSplitCount function * Use std::max in Node's clamp function * Remove test code * DraggedSplit.hpp: remove unused include * Split `setDraggingSplit` into two functions, `startDraggingSplit` and `stopDraggingSplit`
2022-12-25 12:09:25 +01:00
auto *page = new SplitContainer(notebook);
auto *tab = notebook->addPage(page);
page->setTab(tab);
draggedSplit->setParent(page);
page->insertSplit(draggedSplit);
2018-01-24 22:09:26 +01:00
}
void NotebookButton::hideEvent(QHideEvent *)
{
this->parent_->performLayout();
}
void NotebookButton::showEvent(QShowEvent *)
{
this->parent_->performLayout();
}
} // namespace chatterino