Deprecate eventName() now, change base class to QEvent for KF6, use type() and static_cast instead?
Description
Status | Assigned | Task | ||
---|---|---|---|---|
Open | None | T12223 Meta task: KParts | ||
Open | alex | T12232 KParts: cleanup/remove Event base class |
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?
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.
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.
You're right, after removing the string-comparison stuff there's nothing left in KParts::Event, it could be removed completely.
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