play around with custom rendering

This commit is contained in:
Rasmus Karlsson 2024-09-01 11:14:05 +02:00
parent 61b04dbe7b
commit 35d5e7fb63
No known key found for this signature in database
14 changed files with 415 additions and 2 deletions

View file

@ -15,6 +15,13 @@ set(SOURCE_FILES
RunGui.cpp RunGui.cpp
RunGui.hpp RunGui.hpp
TestView.cpp
TestView.hpp
TestView2.cpp
TestView2.hpp
TestDelegate.cpp
TestDelegate.hpp
common/Args.cpp common/Args.cpp
common/Args.hpp common/Args.hpp
common/Channel.cpp common/Channel.cpp

72
src/TestDelegate.cpp Normal file
View file

@ -0,0 +1,72 @@
#include "TestDelegate.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
#include "widgets/helper/color/Checkerboard.hpp"
namespace chatterino {
TestDelegate::TestDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QSize TestDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
auto base = QStyledItemDelegate::sizeHint(option, index);
qInfo() << "XXX: size hint when userrole+1 value is"
<< index.data(Qt::UserRole + 1).value<bool>();
if (index.data(Qt::UserRole + 1).value<bool>())
{
base.setHeight(base.height() * 2);
}
else
{
// do nothing
}
return base;
}
void TestDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
auto data = index.data(Qt::DisplayRole);
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<QString>());
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<QColor>();
painter->save();
if (color.alpha() != 255)
{
drawCheckerboard(*painter, option.rect,
std::min(option.rect.height() / 2, 10));
}
painter->setBrush(color);
painter->drawRect(option.rect);
painter->restore();
}
} // namespace chatterino

20
src/TestDelegate.hpp Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <QSize>
#include <QStyledItemDelegate>
namespace chatterino {
class TestDelegate : public QStyledItemDelegate
{
public:
explicit TestDelegate(QObject *parent);
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
};
} // namespace chatterino

52
src/TestModel.cpp Normal file
View file

@ -0,0 +1,52 @@
#include "TestModel.hpp"
namespace chatterino {
bool TestModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
int row = index.row();
int column = index.column();
if (row < 0 || column < 0 || row >= this->rows_.size() ||
column >= this->columnCount_)
{
return false;
}
Row &rowItem = this->rows_[row];
assert(this->columnCount_ == rowItem.items.size());
auto &cell = rowItem.items[column];
cell->setData(value, role);
if (rowItem.isCustomRow)
{
this->customRowSetData(rowItem.items, column, value, role, row);
}
else
{
int vecRow = this->getVectorIndexFromModelIndex(row);
// TODO: This is only a safety-thing for when we modify data that's being modified right now.
// It should not be necessary, but it would require some rethinking about this surrounding logic
if (vecRow >= this->vector_->readOnly()->size())
{
return false;
}
this->vector_->removeAt(vecRow, this);
assert(this->rows_[row].original);
TVectorItem item = this->getItemFromRow(
this->rows_[row].items, this->rows_[row].original.value());
this->vector_->insert(item, vecRow, this);
QVector<int> roles = QVector<int>();
roles.append(role);
emit dataChanged(index, index, roles);
}
return true;
}
} // namespace chatterino

14
src/TestModel.hpp Normal file
View file

@ -0,0 +1,14 @@
#pragma once
#include <QAbstractListModel>
#include <QVariant>
namespace chatterino {
class TestModel
{
public:
TestModel() = default;
};
} // namespace chatterino

57
src/TestView.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "TestView.hpp"
namespace chatterino {
TestView::TestView(QWidget *parent)
: QAbstractItemView(parent)
{
}
QModelIndex TestView::indexAt(const QPoint &point) const
{
return {};
}
void TestView::scrollTo(const QModelIndex &index,
QAbstractItemView::ScrollHint hint)
{
}
QRect TestView::visualRect(const QModelIndex &index) const
{
return {};
}
int TestView::horizontalOffset() const
{
return {};
}
int TestView::verticalOffset() const
{
return {};
}
bool TestView::isIndexHidden(const QModelIndex &index) const
{
return {};
}
QModelIndex TestView::moveCursor(QAbstractItemView::CursorAction cursorAction,
Qt::KeyboardModifiers modifiers)
{
return {};
}
void TestView::setSelection(const QRect &rect,
QItemSelectionModel::SelectionFlags flags)
{
}
QRegion TestView::visualRegionForSelection(
const QItemSelection &selection) const
{
return {};
}
} // namespace chatterino

39
src/TestView.hpp Normal file
View file

@ -0,0 +1,39 @@
#pragma once
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include <QItemSelectionModel>
#include <QPoint>
#include <QRect>
#include <QRegion>
#include <QWidget>
namespace chatterino {
class TestView : public QAbstractItemView
{
Q_OBJECT
public:
TestView(QWidget *parent);
QModelIndex indexAt(const QPoint &point) const override;
void scrollTo(const QModelIndex &index,
QAbstractItemView::ScrollHint hint =
QAbstractItemView::ScrollHint::EnsureVisible) override;
QRect visualRect(const QModelIndex &index) const override;
protected:
int horizontalOffset() const override;
int verticalOffset() const override;
bool isIndexHidden(const QModelIndex &index) const override;
QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) override;
void setSelection(const QRect &rect,
QItemSelectionModel::SelectionFlags flags) override;
QRegion visualRegionForSelection(
const QItemSelection &selection) const override;
};
} // namespace chatterino

57
src/TestView2.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "TestView2.hpp"
namespace chatterino {
TestView2::TestView2(QWidget *parent)
: QAbstractItemView(parent)
{
}
QModelIndex TestView2::indexAt(const QPoint &point) const
{
return {};
}
void TestView2::scrollTo(const QModelIndex &index,
QAbstractItemView::ScrollHint hint)
{
}
QRect TestView2::visualRect(const QModelIndex &index) const
{
return {};
}
int TestView2::horizontalOffset() const
{
return {};
}
int TestView2::verticalOffset() const
{
return {};
}
bool TestView2::isIndexHidden(const QModelIndex &index) const
{
return {};
}
QModelIndex TestView2::moveCursor(QAbstractItemView::CursorAction cursorAction,
Qt::KeyboardModifiers modifiers)
{
return {};
}
void TestView2::setSelection(const QRect &rect,
QItemSelectionModel::SelectionFlags flags)
{
}
QRegion TestView2::visualRegionForSelection(
const QItemSelection &selection) const
{
return {};
}
} // namespace chatterino

39
src/TestView2.hpp Normal file
View file

@ -0,0 +1,39 @@
#pragma once
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include <QItemSelectionModel>
#include <QPoint>
#include <QRect>
#include <QRegion>
#include <QWidget>
namespace chatterino {
class TestView2 : public QAbstractItemView
{
Q_OBJECT
public:
TestView2(QWidget *parent);
QModelIndex indexAt(const QPoint &point) const override;
void scrollTo(const QModelIndex &index,
QAbstractItemView::ScrollHint hint =
QAbstractItemView::ScrollHint::EnsureVisible) override;
QRect visualRect(const QModelIndex &index) const override;
protected:
int horizontalOffset() const override;
int verticalOffset() const override;
bool isIndexHidden(const QModelIndex &index) const override;
QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) override;
void setSelection(const QRect &rect,
QItemSelectionModel::SelectionFlags flags) override;
QRegion visualRegionForSelection(
const QItemSelection &selection) const override;
};
} // namespace chatterino

View file

@ -161,6 +161,7 @@ public:
if (rowItem.isCustomRow) if (rowItem.isCustomRow)
{ {
this->customRowSetData(rowItem.items, column, value, role, row); this->customRowSetData(rowItem.items, column, value, role, row);
emit dataChanged(index, index, {role});
} }
else else
{ {

View file

@ -240,7 +240,7 @@ void SettingsDialog::addTabs()
this->addTab([]{return new NicknamesPage;}, "Nicknames", ":/settings/accounts.svg"); this->addTab([]{return new NicknamesPage;}, "Nicknames", ":/settings/accounts.svg");
this->ui_.tabContainer->addSpacing(16); this->ui_.tabContainer->addSpacing(16);
this->addTab([]{return new CommandPage;}, "Commands", ":/settings/commands.svg"); this->addTab([]{return new CommandPage;}, "Commands", ":/settings/commands.svg");
this->addTab([]{return new HighlightingPage;}, "Highlights", ":/settings/notifications.svg"); this->addTab([]{return new HighlightingPage;}, "Highlights", ":/settings/notifications.svg", SettingsTabId::Highlights);
this->addTab([]{return new IgnoresPage;}, "Ignores", ":/settings/ignore.svg"); this->addTab([]{return new IgnoresPage;}, "Ignores", ":/settings/ignore.svg");
this->addTab([]{return new FiltersPage;}, "Filters", ":/settings/filters.svg"); this->addTab([]{return new FiltersPage;}, "Filters", ":/settings/filters.svg");
this->ui_.tabContainer->addSpacing(16); this->ui_.tabContainer->addSpacing(16);
@ -364,6 +364,11 @@ void SettingsDialog::showDialog(QWidget *parent,
} }
break; break;
case SettingsDialogPreference::Highlights: {
instance->selectTab(SettingsTabId::Highlights);
}
break;
case SettingsDialogPreference::StreamerMode: { case SettingsDialogPreference::StreamerMode: {
instance->selectTab(SettingsTabId::General); instance->selectTab(SettingsTabId::General);
} }

View file

@ -29,6 +29,7 @@ enum class SettingsDialogPreference {
NoPreference, NoPreference,
StreamerMode, StreamerMode,
Accounts, Accounts,
Highlights,
ModerationActions, ModerationActions,
About, About,
}; };
@ -40,7 +41,7 @@ class SettingsDialog : public BaseWindow
public: public:
static void showDialog(QWidget *parent, static void showDialog(QWidget *parent,
SettingsDialogPreference preferredTab = SettingsDialogPreference preferredTab =
SettingsDialogPreference::NoPreference); SettingsDialogPreference::Highlights);
protected: protected:
void scaleChangedEvent(float newDpi) override; void scaleChangedEvent(float newDpi) override;

View file

@ -17,6 +17,7 @@ enum class SettingsTabId {
None, None,
General, General,
Accounts, Accounts,
Highlights,
Moderation, Moderation,
About, About,
}; };

View file

@ -10,6 +10,9 @@
#include "controllers/highlights/UserHighlightModel.hpp" #include "controllers/highlights/UserHighlightModel.hpp"
#include "providers/colors/ColorProvider.hpp" #include "providers/colors/ColorProvider.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "TestDelegate.hpp"
#include "TestModel.hpp"
#include "TestView.hpp"
#include "util/Helpers.hpp" #include "util/Helpers.hpp"
#include "util/LayoutCreator.hpp" #include "util/LayoutCreator.hpp"
#include "widgets/dialogs/BadgePickerDialog.hpp" #include "widgets/dialogs/BadgePickerDialog.hpp"
@ -19,10 +22,14 @@
#include <QFileDialog> #include <QFileDialog>
#include <QHeaderView> #include <QHeaderView>
#include <QListView>
#include <QPushButton> #include <QPushButton>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QTableView> #include <QTableView>
#include <QTabWidget> #include <QTabWidget>
#include <QTreeView>
#include "TestView2.hpp"
namespace chatterino { namespace chatterino {
@ -55,6 +62,47 @@ HighlightingPage::HighlightingPage()
// TABS // TABS
auto tabs = layout.emplace<QTabWidget>(); auto tabs = layout.emplace<QTabWidget>();
{ {
// TEST
{
auto highlights = tabs.appendTab(new QVBoxLayout, "TEST");
auto *model =
(new HighlightModel(nullptr))
->initialized(&getSettings()->highlightedMessages);
auto *view = highlights.emplace<QListView>(this).getElement();
view->setSpacing(2);
QObject::connect(
view, &QAbstractItemView::clicked,
[view, model](const QModelIndex &index) {
qInfo() << "XXX: ITEM CLICKED?"
<< index.data(Qt::UserRole + 1).type();
if (index.data(Qt::UserRole + 1).value<bool>())
{
auto res =
model->setData(index, false, Qt::UserRole + 1);
qInfo() << "XXX: ITEM CLICKED? Set to false" << res;
}
else
{
auto res =
model->setData(index, QVariant::fromValue(true),
Qt::UserRole + 1);
qInfo() << "XXX: ITEM CLICKED? Set to true" << res;
}
view->update();
view->viewport()->update();
view->updateGeometry();
view->viewport()->updateGeometry();
// view->update();
// view->repaint();
});
view->setModel(model);
view->setHorizontalScrollBarPolicy(
Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
view->setItemDelegate(new TestDelegate(this));
}
// HIGHLIGHTS // HIGHLIGHTS
auto highlights = tabs.appendTab(new QVBoxLayout, "Messages"); auto highlights = tabs.appendTab(new QVBoxLayout, "Messages");
{ {