chore: use DrawnButton instead of NotebookButton (#6371)

This commit is contained in:
pajlada
2025-08-03 13:13:42 +02:00
committed by GitHub
parent 6fb3957580
commit 283be8710c
8 changed files with 74 additions and 214 deletions
+27
View File
@@ -5,6 +5,7 @@
#include <QApplication>
#include <QDebug>
#include <QMimeData>
#include <QPainter>
#include <QScreen>
@@ -97,6 +98,32 @@ void Button::setMenu(std::unique_ptr<QMenu> menu)
}));
}
void Button::enableDrops(const std::vector<QString> &acceptedDropMimes_)
{
this->setAcceptDrops(true);
this->acceptedDropMimes = acceptedDropMimes_;
}
void Button::dragEnterEvent(QDragEnterEvent *event)
{
auto anyMatches = std::ranges::any_of(
this->acceptedDropMimes, [event](const QString &acceptedMime) {
return event->mimeData()->hasFormat(acceptedMime);
});
if (!anyMatches)
{
return;
}
event->acceptProposedAction();
this->addClickEffect(QPoint{
this->width() / 2,
this->height() / 2,
});
}
void Button::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
+13
View File
@@ -100,7 +100,13 @@ public:
/// Setter for #menu()
void setMenu(std::unique_ptr<QMenu> menu);
/// Enable drops for this button
void enableDrops(const std::vector<QString> &acceptedDropMimes_);
Q_SIGNALS:
/// Emitted when a successful drop event has occurred on the button.
void dropEvent(QDropEvent *event) override;
/// @brief Emitted after the user left-clicked the button.
///
/// A click is only emitted if the user released the mouse above the button
@@ -121,6 +127,8 @@ Q_SIGNALS:
void leftMousePress();
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void paintEvent(QPaintEvent * /*event*/) override;
/// @brief Paint the contents to be shown below the button
@@ -216,6 +224,11 @@ private:
bool pixmapValid_ = false;
bool cachePixmap_ = false;
bool opaqueContent_ = false;
/// List of Mimes (e.g. chatterino/split) that are accepted as drop events on this button
///
/// Controlled by the enableDrops function
std::vector<QString> acceptedDropMimes;
};
} // namespace chatterino
-162
View File
@@ -1,162 +0,0 @@
#include "widgets/buttons/NotebookButton.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "singletons/Theme.hpp"
#include "widgets/Notebook.hpp"
#include "widgets/splits/DraggedSplit.hpp"
#include "widgets/splits/Split.hpp"
#include "widgets/splits/SplitContainer.hpp"
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include <QPainterPath>
#include <QRadialGradient>
namespace chatterino {
NotebookButton::NotebookButton(Type type_, Notebook *parent)
: Button(parent)
, parent_(parent)
, type(type_)
{
switch (this->type)
{
case Type::Plus: {
this->setAcceptDrops(true);
}
break;
}
}
void NotebookButton::themeChangedEvent()
{
this->setMouseEffectColor(this->theme->tabs.regular.text);
}
void NotebookButton::paintContent(QPainter &painter)
{
QColor background;
QColor foreground;
if (this->leftMouseButtonDown() || this->mouseOver())
{
background = this->theme->tabs.regular.backgrounds.hover;
foreground = this->theme->tabs.regular.text;
}
else
{
background = this->theme->tabs.regular.backgrounds.regular;
foreground = this->theme->tabs.regular.text;
}
painter.setPen(Qt::NoPen);
float h = height(), w = width();
switch (this->type)
{
case Type::Plus: {
painter.setPen([&] {
QColor tmp = foreground;
if (isDraggingSplit())
{
tmp = this->theme->tabs.selected.line.regular;
}
else if (!this->mouseOver())
{
tmp.setAlpha(180);
}
return tmp;
}());
QRect rect = this->rect();
int s = h * 4 / 9;
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));
}
break;
}
}
void NotebookButton::dragEnterEvent(QDragEnterEvent *event)
{
if (this->type != Type::Plus)
{
return;
}
if (!event->mimeData()->hasFormat("chatterino/split"))
{
return;
}
event->acceptProposedAction();
this->addClickEffect(QPoint{
this->width() / 2,
this->height() / 2,
});
}
void NotebookButton::dropEvent(QDropEvent *event)
{
if (this->type != Type::Plus)
{
return;
}
auto *draggedSplit = dynamic_cast<Split *>(event->source());
if (!draggedSplit)
{
qCDebug(chatterinoWidget)
<< "Dropped something that wasn't a split onto a notebook button";
return;
}
auto *notebook = dynamic_cast<Notebook *>(this->parentWidget());
if (!notebook)
{
qCDebug(chatterinoWidget) << "Dropped a split onto a notebook button "
"without a parent notebook";
return;
}
event->acceptProposedAction();
auto *page = new SplitContainer(notebook);
auto *tab = notebook->addPage(page);
page->setTab(tab);
draggedSplit->setParent(page);
page->insertSplit(draggedSplit);
}
void NotebookButton::hideEvent(QHideEvent *)
{
if (isAppAboutToQuit())
{
return;
}
this->parent_->refresh();
}
void NotebookButton::showEvent(QShowEvent *)
{
if (isAppAboutToQuit())
{
return;
}
this->parent_->refresh();
}
} // namespace chatterino
-38
View File
@@ -1,38 +0,0 @@
#pragma once
#include "widgets/buttons/Button.hpp"
#include <QWidget>
namespace chatterino {
class Notebook;
class NotebookButton : public Button
{
Q_OBJECT
public:
enum class Type : std::uint8_t {
Plus,
};
NotebookButton(Type type_, Notebook *parent);
protected:
void paintContent(QPainter &painter) override;
void themeChangedEvent() override;
void dragEnterEvent(QDragEnterEvent *) override;
void dropEvent(QDropEvent *) override;
void hideEvent(QHideEvent *) override;
void showEvent(QShowEvent *) override;
private:
Notebook *parent_ = nullptr;
QPoint mousePos_;
Type type = Type::Plus;
};
} // namespace chatterino