KParts: cleanup/remove Event base class
Open, Needs TriagePublic

Description

Deprecate eventName() now, change base class to QEvent for KF6, use type() and static_cast instead?

Related Objects

dfaure created this task.Nov 24 2019, 2:09 PM
alex added a subscriber: alex.EditedSep 23 2021, 5:10 AM

What would be your strategy regarding Event::test(const QEvent *event, const char *name)?

There the event name stuff is used. Or could the (event->type() == (QEvent::Type)(QEvent::User + KPARTS_EVENT_MAGIC)); check already be sufficient?

alex moved this task from Backlog to Needs Input on the KF6 board.Sep 23 2021, 5:12 AM

String comparisons are an awful solution for a dynamic_cast-like solution. We were young and inexperienced...
Nowadays we should use QEvent's type() instead.
I suggest to remove eventName() and the Event(const char*) constructor in KF6 and replace it all with event type ids like QEvent does. I would also remove KPARTS_EVENT_MAGIC, the base class should be abstract, it makes not sense to instanciate it as is.
This means

protected:
    explicit Event(int eventType);

and subclasses would do

static const int s_openUrlEventType = 0x0583C101;
OpenUrlEvent::OpenUrlEvent(...)
     : Event(s_openUrlEventType) { ... }

bool OpenUrlEvent::test(const QEvent *event) /* KF6 TODO: const */
{
    return event->type() == s_openUrlEventType;
}

where the type id was generated with the shell command
printf "0x%08X\n" $(shuf -i0-4294967295 -n 1)
(though it will give you a different number if you try) :-)

To be repeated in each subclass.

I think it can be done in KF5 if the test() method does both tests (id and eventName) for now.

dfaure moved this task from Needs Input to Backlog on the KF6 board.Oct 4 2021, 3:11 PM
alex claimed this task.Dec 14 2021, 4:27 PM
alex added a comment.Dec 22 2021, 10:01 AM

But I am still not sure if we even need the Event baseclass then. All the individual events could extend from QEvent and then provide a static test method.

alex moved this task from Backlog to Needs Input on the KF6 board.Jan 6 2022, 11:36 AM

You're right, after removing the string-comparison stuff there's nothing left in KParts::Event, it could be removed completely.

alex moved this task from Waiting on KF6 Branching to In Progress on the KF6 board.Feb 6 2023, 7:26 PM
alex added a comment.Feb 6 2023, 8:45 PM

where the type id was generated with the shell command
printf "0x%08X\n" $(shuf -i0-4294967295 -n 1)
(though it will give you a different number if you try) :-)

Is shuf -i1000-65535 -n 1 more correct, because it respects the user-id space?

User = 1000,                            // first user event id
MaxUser = 65535                         // last user event id

https://invent.kde.org/frameworks/kparts/-/merge_requests/57 is the MR

alex moved this task from In Progress to Done on the KF6 board.Feb 17 2023, 1:57 PM