KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
graphicsscene.h
1#ifndef GRAPHICSSCENE_H
2#define GRAPHICSSCENE_H
3
4#include <QGraphicsScene>
5#include <QGraphicsItem>
6#include <Kanoop/utility/loggingbaseclass.h>
7#include <Kanoop/gui/libkanoopgui.h>
8
9/**
10 * @brief QGraphicsScene subclass with logging support and typed-item search helpers.
11 *
12 * GraphicsScene adds findChildItems<T>() and findFirstChildItem<T>() template
13 * helpers that search all items in the scene and return only those that dynamic_cast
14 * to the requested pointer type.
15 */
16class LIBKANOOPGUI_EXPORT GraphicsScene : public QGraphicsScene,
17 public LoggingBaseClass
18{
19 Q_OBJECT
20public:
21 /**
22 * @brief Construct with an optional parent object.
23 * @param parent Optional QObject parent
24 */
25 explicit GraphicsScene(QObject *parent = nullptr);
26
27 /**
28 * @brief Return all scene items that dynamic_cast to type T.
29 * @tparam T Pointer type to search for (must derive from QGraphicsItem)
30 * @return List of matching items cast to T
31 */
32 template <typename T>
33 QList<T> findChildItems() const
34 {
35 QList<T> result;
36 for(QGraphicsItem* item : items()) {
37 T candidate = dynamic_cast<T>(item);
38 if(candidate != nullptr) {
39 result.append(candidate);
40 }
41 }
42 return result;
43 }
44
45 /**
46 * @brief Return the first scene item that dynamic_cast to type T.
47 * @tparam T Pointer type to search for (must derive from QGraphicsItem)
48 * @return First matching item cast to T, or nullptr if none found
49 */
50 template <typename T>
52 {
53 T result = nullptr;
54 for(QGraphicsItem* item : items()) {
55 T candidate = dynamic_cast<T>(item);
56 if(candidate != nullptr) {
57 result = candidate;
58 break;
59 }
60 }
61 return result;
62 }
63
64signals:
65
66};
67
68#endif // GRAPHICSSCENE_H
QGraphicsScene subclass with logging support and typed-item search helpers.
QList< T > findChildItems() const
Return all scene items that dynamic_cast to type T.
T findFirstChildItem() const
Return the first scene item that dynamic_cast to type T.
GraphicsScene(QObject *parent=nullptr)
Construct with an optional parent object.