From 3996bf01caf945570a8e87c666a8e95e8d702af1 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 15 Sep 2024 00:17:51 +0200 Subject: [PATCH] more tests --- src/CMakeLists.txt | 2 + src/TestDelegate.cpp | 110 +++++++++++++----- src/TestDelegate.hpp | 12 ++ src/TestWidget.cpp | 33 ++++++ src/TestWidget.hpp | 42 +++++++ .../highlights/HighlightPhrase.hpp | 5 + src/singletons/Settings.hpp | 2 + .../settingspages/HighlightingPage.cpp | 44 +++++++ 8 files changed, 221 insertions(+), 29 deletions(-) create mode 100644 src/TestWidget.cpp create mode 100644 src/TestWidget.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 45abe21b8..2705eee84 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,8 @@ set(SOURCE_FILES TestView2.hpp TestDelegate.cpp TestDelegate.hpp + TestWidget.cpp + TestWidget.hpp common/Args.cpp common/Args.hpp diff --git a/src/TestDelegate.cpp b/src/TestDelegate.cpp index 297d21077..881fd7cd4 100644 --- a/src/TestDelegate.cpp +++ b/src/TestDelegate.cpp @@ -1,13 +1,27 @@ #include "TestDelegate.hpp" #include "controllers/highlights/HighlightPhrase.hpp" +#include "TestWidget.hpp" #include "widgets/helper/color/Checkerboard.hpp" +#include +#include +#include +#include + namespace chatterino { TestDelegate::TestDelegate(QObject *parent) : QStyledItemDelegate(parent) { + auto *factory = new TestWidgetCreator(); + this->setItemEditorFactory(factory); + this->btn = new QPushButton("xd"); +} + +TestDelegate::~TestDelegate() +{ + this->btn->deleteLater(); } QSize TestDelegate::sizeHint(const QStyleOptionViewItem &option, @@ -15,15 +29,13 @@ QSize TestDelegate::sizeHint(const QStyleOptionViewItem &option, { auto base = QStyledItemDelegate::sizeHint(option, index); - qInfo() << "XXX: size hint when userrole+1 value is" - << index.data(Qt::UserRole + 1).value(); - if (index.data(Qt::UserRole + 1).value()) { - base.setHeight(base.height() * 2); + base.setHeight(base.height() * 5); } else { + base.setHeight(base.height() * 5); // do nothing } @@ -34,39 +46,79 @@ void TestDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { auto data = index.data(Qt::DisplayRole); + auto expanded = index.data(Qt::UserRole + 1).value(); + + // auto *view = qobject_cast(this->parent()); + // view->openPersistentEditor(index); + // if (view->isPersistentEditorOpen(index)) { + // } else { + // return; + // } + + // painter->begin(this->btn->paintEngine) + // this->btn->paint() HighlightPhrase phrase("my phrase", true, false, true, true, true, "", QColor{}); -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - if (data.typeId() != QMetaType::QColor) -#else - if (data.type() != QVariant::Color) -#endif - { - qInfo() << "XXX:" << data.typeId(); - qInfo() << "XXX:" << data.type(); - painter->save(); - painter->setBrush(Qt::white); - painter->drawText(option.rect, data.value()); - painter->setBrush(QColor{255, 0, 255, 20}); - painter->setPen(QColor{255, 0, 255}); - painter->drawRect(option.rect); - painter->restore(); - // return QStyledItemDelegate::paint(painter, option, index); - return; - } - auto color = data.value(); - painter->save(); - if (color.alpha() != 255) + + painter->setBrush(Qt::white); + painter->drawText(option.rect, data.value()); + painter->setBrush(QColor{255, 0, 255, 20}); + painter->setPen(QColor{255, 0, 255}); + // painter->drawRect(option.rect); + + QPoint dist(10, 10); + + // painter->setPen(Qt::PenStyle::DotLine); + + painter->setRenderHint(QPainter::Antialiasing); + + QPen pen; + + pen.setWidth(2); + pen.setColor(QColor{255, 0, 255, 255}); + + painter->setPen(pen); + + if (expanded) { - drawCheckerboard(*painter, option.rect, - std::min(option.rect.height() / 2, 10)); + QPainterPath path; + path.moveTo(option.rect.topRight()); + path.lineTo(option.rect.topRight() - QPoint{10, -10}); + + auto tr = QRect(option.rect.right() - 10, option.rect.top(), 5, + option.rect.height() / 2); + + painter->drawPath(path); } - painter->setBrush(color); - painter->drawRect(option.rect); + else + { + auto tr = QRect(option.rect.right() - 10, option.rect.top(), 5, + option.rect.height()); + painter->drawEllipse(tr); + } + painter->restore(); + // return QStyledItemDelegate::paint(painter, option, index); +} + +void TestDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + auto *realEditor = dynamic_cast(editor); + realEditor->update(index.data().value()); + assert(realEditor); +} + +bool TestDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, + const QStyleOptionViewItem &option, + const QModelIndex &index) +{ + auto *view = qobject_cast(this->parent()); + view->openPersistentEditor(index); + return QStyledItemDelegate::editorEvent(event, model, option, index); } } // namespace chatterino diff --git a/src/TestDelegate.hpp b/src/TestDelegate.hpp index e519776fc..960c8cf94 100644 --- a/src/TestDelegate.hpp +++ b/src/TestDelegate.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -9,12 +10,23 @@ class TestDelegate : public QStyledItemDelegate { public: explicit TestDelegate(QObject *parent); + ~TestDelegate(); QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + + void setEditorData(QWidget *editor, + const QModelIndex &index) const override; + + bool editorEvent(QEvent *event, QAbstractItemModel *model, + const QStyleOptionViewItem &option, + const QModelIndex &index) override; + +private: + QPushButton *btn; }; } // namespace chatterino diff --git a/src/TestWidget.cpp b/src/TestWidget.cpp new file mode 100644 index 000000000..26d3aaa1d --- /dev/null +++ b/src/TestWidget.cpp @@ -0,0 +1,33 @@ +#include "TestWidget.hpp" + +#include +#include +#include +namespace chatterino { + +class TestWidgetImpl +{ +public: + TestWidgetImpl(QWidget *parent) + : btn(new QPushButton("xd", parent)) + { + } + + QPushButton *btn; +}; + +TestWidget::TestWidget(QWidget *parent) + : QWidget(parent) + , impl(new TestWidgetImpl(this)) +{ + auto *layout = new QVBoxLayout(this); + + layout->addWidget(impl->btn); +} + +void TestWidget::update(const QString &data) +{ + this->impl->btn->setText("AAAAA" % data); +} + +} // namespace chatterino diff --git a/src/TestWidget.hpp b/src/TestWidget.hpp new file mode 100644 index 000000000..00dc765b9 --- /dev/null +++ b/src/TestWidget.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include +#include + +namespace chatterino { + +class TestWidgetImpl; + +class TestWidget : public QWidget +{ + Q_OBJECT + +public: + TestWidget(QWidget *parent); + + void update(const QString &data); + +private: + TestWidgetImpl *impl; +}; + +class TestWidgetCreator : public QItemEditorFactory +{ +public: + QWidget *createEditor(int userType, QWidget *parent) const override + { + qInfo() << "XXX: USER TYPE" << userType; + return new TestWidget(parent); + } + + QByteArray valuePropertyName(int userType) const override + { + return "data"; + } +}; + +} // namespace chatterino + +Q_DECLARE_METATYPE(chatterino::TestWidget *); diff --git a/src/controllers/highlights/HighlightPhrase.hpp b/src/controllers/highlights/HighlightPhrase.hpp index ed03fbe96..3469b3989 100644 --- a/src/controllers/highlights/HighlightPhrase.hpp +++ b/src/controllers/highlights/HighlightPhrase.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,8 @@ class HighlightPhrase public: bool operator==(const HighlightPhrase &other) const; + HighlightPhrase(const HighlightPhrase &other) = default; + /** * @brief Create a new HighlightPhrase. * @@ -102,6 +105,8 @@ private: } // namespace chatterino +Q_DECLARE_METATYPE(chatterino::HighlightPhrase *); + namespace pajlada { namespace { diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 39769ad9b..5b41b2def 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -21,6 +21,8 @@ #include #include +#include + using TimeoutButton = std::pair; namespace chatterino { diff --git a/src/widgets/settingspages/HighlightingPage.cpp b/src/widgets/settingspages/HighlightingPage.cpp index 76bfaa49a..1a1545e12 100644 --- a/src/widgets/settingspages/HighlightingPage.cpp +++ b/src/widgets/settingspages/HighlightingPage.cpp @@ -13,6 +13,7 @@ #include "TestDelegate.hpp" #include "TestModel.hpp" #include "TestView.hpp" +#include "TestWidget.hpp" #include "util/Helpers.hpp" #include "util/LayoutCreator.hpp" #include "widgets/dialogs/BadgePickerDialog.hpp" @@ -20,10 +21,12 @@ #include "widgets/helper/color/ColorItemDelegate.hpp" #include "widgets/helper/EditableModelView.hpp" +#include #include #include #include #include +#include #include #include #include @@ -62,6 +65,44 @@ HighlightingPage::HighlightingPage() // TABS auto tabs = layout.emplace(); { + // TEST 2 + { + auto highlights = tabs.appendTab(new QVBoxLayout, "TEST 2"); + + auto *model = + (new HighlightModel(nullptr)) + ->initialized(&getSettings()->highlightedMessages); + auto *view = highlights.emplace(this).getElement(); + view->setHorizontalScrollBarPolicy( + Qt::ScrollBarPolicy::ScrollBarAlwaysOff); + view->setVerticalScrollBarPolicy( + Qt::ScrollBarPolicy::ScrollBarAlwaysOn); + auto *box = new QVBoxLayout; + view->setLayout(box); + + box->setSizeConstraint( + QLayout::SizeConstraint::SetMinAndMaxSize); + QStringList data{ + "1", "2", "3", "4", "5", "6", "7", "8", "9", + "10", "11", "12", "13", "14", "15", "16", "17", "18", + "19", "20", "21", "22", "23", "24", "25", "26", "27", + "19", "20", "21", "22", "23", "24", "25", "26", "27", + "19", "20", "21", "22", "23", "24", "25", "26", "27", + "19", "20", "21", "22", "23", "24", "25", "26", "27", + }; + for (const auto &xd : data) + { + // auto *w = new TestWidget(this); + // w->setMinimumHeight(50); + auto *w = new QPushButton("xd"); + /* + w->setSizePolicy( + {QSizePolicy::Maximum, QSizePolicy::Minimum}); + */ + box->addWidget(w, 1); + } + } + // TEST { auto highlights = tabs.appendTab(new QVBoxLayout, "TEST"); @@ -71,16 +112,19 @@ HighlightingPage::HighlightingPage() ->initialized(&getSettings()->highlightedMessages); auto *view = highlights.emplace(this).getElement(); view->setSpacing(2); + // view->setEditTriggers(QAbstractItemView::AllEditTriggers); QObject::connect( view, &QAbstractItemView::clicked, [view, model](const QModelIndex &index) { qInfo() << "XXX: ITEM CLICKED?" << index.data(Qt::UserRole + 1).type(); + view->openPersistentEditor(index); if (index.data(Qt::UserRole + 1).value()) { auto res = model->setData(index, false, Qt::UserRole + 1); qInfo() << "XXX: ITEM CLICKED? Set to false" << res; + view->edit(index); } else {