ruby-qt6-rice 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ // This file is part of [RubyQt6](https://github.com/souk4711/ruby-qt6).
2
+ //
3
+ // It is licensed under the LGPLv3, included below.
4
+ //
5
+ // As a special exception to the GNU Lesser General Public License version 3
6
+ // ("LGPL3"), the copyright holders of this Library give you permission to
7
+ // convey to a third party a Combined Work that links statically or dynamically
8
+ // to this Library without providing any Minimal Corresponding Source or
9
+ // Minimal Application Code as set out in 4d or providing the installation
10
+ // information set out in section 4e, provided that you comply with the other
11
+ // provisions of LGPL3 and provided that you meet, for the Application the
12
+ // terms and conditions of the license(s) which apply to the Application.
13
+ //
14
+ // Except as stated in this special exception, the provisions of LGPL3 will
15
+ // continue to comply in full to this Library. If you modify this Library, you
16
+ // may apply this exception to your version of this Library, but you are not
17
+ // obliged to do so. If you do not wish to do so, delete this exception
18
+ // statement from your version. This exception does not (and cannot) modify any
19
+ // license terms which apply to the Application, with which you must still
20
+ // comply.
21
+
22
+ #ifndef BANDO_QWIDGET_HPP
23
+ #define BANDO_QWIDGET_HPP
24
+
25
+ #include <bando/common.hpp>
26
+ #include <QActionEvent>
27
+ #include <QEvent>
28
+ #include <QChildEvent>
29
+ #include <QCloseEvent>
30
+ #include <QContextMenuEvent>
31
+ #include <QDragEnterEvent>
32
+ #include <QDragLeaveEvent>
33
+ #include <QDragMoveEvent>
34
+ #include <QDropEvent>
35
+ #include <QEnterEvent>
36
+ #include <QFocusEvent>
37
+ #include <QFocusEvent>
38
+ #include <QHideEvent>
39
+ #include <QInputMethodEvent>
40
+ #include <QKeyEvent>
41
+ #include <QKeyEvent>
42
+ #include <QEvent>
43
+ #include <QMouseEvent>
44
+ #include <QMouseEvent>
45
+ #include <QMouseEvent>
46
+ #include <QMouseEvent>
47
+ #include <QMoveEvent>
48
+ #include <QPaintEvent>
49
+ #include <QResizeEvent>
50
+ #include <QShowEvent>
51
+ #include <QTabletEvent>
52
+ #include <QTimerEvent>
53
+ #include <QWheelEvent>
54
+
55
+ template <typename Class_T, typename... Arg_Ts> class BandoQWidget : public Class_T
56
+ {
57
+ public:
58
+ BandoQWidget(Arg_Ts... args) : Class_T(args...), value_(Qnil), value_address_(nullptr), mo_(nullptr) {};
59
+ BandoQWidget(const BandoQWidget &) = delete;
60
+ BandoQWidget &operator=(const BandoQWidget &) = delete;
61
+ BandoQWidget(BandoQWidget &&) = delete;
62
+ BandoQWidget &operator=(BandoQWidget &&) = delete;
63
+ ~BandoQWidget() override { bando_finalizer<BandoQWidget>(this); };
64
+
65
+ void initializeValue(Rice::Object value, QMetaObject *mo) { bando_initializeValue<BandoQWidget>(this, value, mo); };
66
+ Rice::Object value() { return this->value_; };
67
+
68
+ const QMetaObject *metaObject() const override { return bando_metaObject<BandoQWidget, Class_T>(this); };
69
+ int qt_metacall(QMetaObject::Call call, int id, void **args) override { return bando_qt_metacall<BandoQWidget>(this, call, id, args); };
70
+
71
+ bool event(QEvent *event) override { return bando_handleQObjectEvent<BandoQWidget>(this, event); };
72
+ bool eventFilter(QObject *watched, QEvent *event) override { return bando_handleQObjectEventFilter<BandoQWidget>(this, watched, event); };
73
+
74
+ void childEvent(QChildEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::childEvent); };
75
+ void timerEvent(QTimerEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::timerEvent); };
76
+ QObject *sender() const { return this->Class_T::sender(); }
77
+
78
+ void actionEvent(QActionEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::actionEvent); };
79
+ void changeEvent(QEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::changeEvent); };
80
+ void closeEvent(QCloseEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::closeEvent); };
81
+ void contextMenuEvent(QContextMenuEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::contextMenuEvent); };
82
+ void dragEnterEvent(QDragEnterEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::dragEnterEvent); };
83
+ void dragLeaveEvent(QDragLeaveEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::dragLeaveEvent); };
84
+ void dragMoveEvent(QDragMoveEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::dragMoveEvent); };
85
+ void dropEvent(QDropEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::dropEvent); };
86
+ void enterEvent(QEnterEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::enterEvent); };
87
+ void focusInEvent(QFocusEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::focusInEvent); };
88
+ void focusOutEvent(QFocusEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::focusOutEvent); };
89
+ void hideEvent(QHideEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::hideEvent); };
90
+ void inputMethodEvent(QInputMethodEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::inputMethodEvent); };
91
+ void keyPressEvent(QKeyEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::keyPressEvent); };
92
+ void keyReleaseEvent(QKeyEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::keyReleaseEvent); };
93
+ void leaveEvent(QEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::leaveEvent); };
94
+ void mouseDoubleClickEvent(QMouseEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::mouseDoubleClickEvent); };
95
+ void mouseMoveEvent(QMouseEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::mouseMoveEvent); };
96
+ void mousePressEvent(QMouseEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::mousePressEvent); };
97
+ void mouseReleaseEvent(QMouseEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::mouseReleaseEvent); };
98
+ void moveEvent(QMoveEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::moveEvent); };
99
+ void paintEvent(QPaintEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::paintEvent); };
100
+ void resizeEvent(QResizeEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::resizeEvent); };
101
+ void showEvent(QShowEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::showEvent); };
102
+ void tabletEvent(QTabletEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::tabletEvent); };
103
+ void wheelEvent(QWheelEvent *event) override { bando_handleEvent<BandoQWidget>(this, event, bando_FunctionName::wheelEvent); };
104
+
105
+ QSize minimumSizeHint() const override {
106
+ Q_ASSERT(this->value_.rb_type() != RUBY_T_NONE);
107
+ auto rb_name = Rice::Identifier("minimum_size_hint");
108
+ auto rb_return = this->value_.call(rb_name);
109
+ return Rice::detail::From_Ruby<QSize>().convert(rb_return);
110
+ };
111
+
112
+ QSize sizeHint() const override {
113
+ Q_ASSERT(this->value_.rb_type() != RUBY_T_NONE);
114
+ auto rb_name = Rice::Identifier("size_hint");
115
+ auto rb_return = this->value_.call(rb_name);
116
+ return Rice::detail::From_Ruby<QSize>().convert(rb_return);
117
+ };
118
+
119
+ public:
120
+ bool Class_T_handleQObjectEvent(QEvent *event) { return this->Class_T::event(event); };
121
+ bool Class_T_handleQObjectEventFilter(QObject *watched, QEvent *event) { return this->Class_T::eventFilter(watched, event); };
122
+
123
+ void Class_T_handleEvent(bando_FunctionName name, QEvent *event) {
124
+ switch (name)
125
+ {
126
+ case bando_FunctionName::childEvent: return this->Class_T::childEvent(static_cast<QChildEvent *>(event));
127
+ case bando_FunctionName::timerEvent: return this->Class_T::timerEvent(static_cast<QTimerEvent *>(event));
128
+
129
+ case bando_FunctionName::actionEvent: return this->Class_T::actionEvent(static_cast<QActionEvent *>(event));
130
+ case bando_FunctionName::changeEvent: return this->Class_T::changeEvent(event);
131
+ case bando_FunctionName::closeEvent: return this->Class_T::closeEvent(static_cast<QCloseEvent *>(event));
132
+ case bando_FunctionName::contextMenuEvent: return this->Class_T::contextMenuEvent(static_cast<QContextMenuEvent *>(event));
133
+ case bando_FunctionName::dragEnterEvent: return this->Class_T::dragEnterEvent(static_cast<QDragEnterEvent *>(event));
134
+ case bando_FunctionName::dragLeaveEvent: return this->Class_T::dragLeaveEvent(static_cast<QDragLeaveEvent *>(event));
135
+ case bando_FunctionName::dragMoveEvent: return this->Class_T::dragMoveEvent(static_cast<QDragMoveEvent *>(event));
136
+ case bando_FunctionName::dropEvent: return this->Class_T::dropEvent(static_cast<QDropEvent *>(event));
137
+ case bando_FunctionName::enterEvent: return this->Class_T::enterEvent(static_cast<QEnterEvent *>(event));
138
+ case bando_FunctionName::focusInEvent: return this->Class_T::focusInEvent(static_cast<QFocusEvent *>(event));
139
+ case bando_FunctionName::focusOutEvent: return this->Class_T::focusOutEvent(static_cast<QFocusEvent *>(event));
140
+ case bando_FunctionName::hideEvent: return this->Class_T::hideEvent(static_cast<QHideEvent *>(event));
141
+ case bando_FunctionName::inputMethodEvent: return this->Class_T::inputMethodEvent(static_cast<QInputMethodEvent *>(event));
142
+ case bando_FunctionName::keyPressEvent: return this->Class_T::keyPressEvent(static_cast<QKeyEvent *>(event));
143
+ case bando_FunctionName::keyReleaseEvent: return this->Class_T::keyReleaseEvent(static_cast<QKeyEvent *>(event));
144
+ case bando_FunctionName::leaveEvent: return this->Class_T::leaveEvent(event);
145
+ case bando_FunctionName::mouseDoubleClickEvent: return this->Class_T::mouseDoubleClickEvent(static_cast<QMouseEvent *>(event));
146
+ case bando_FunctionName::mouseMoveEvent: return this->Class_T::mouseMoveEvent(static_cast<QMouseEvent *>(event));
147
+ case bando_FunctionName::mousePressEvent: return this->Class_T::mousePressEvent(static_cast<QMouseEvent *>(event));
148
+ case bando_FunctionName::mouseReleaseEvent: return this->Class_T::mouseReleaseEvent(static_cast<QMouseEvent *>(event));
149
+ case bando_FunctionName::moveEvent: return this->Class_T::moveEvent(static_cast<QMoveEvent *>(event));
150
+ case bando_FunctionName::paintEvent: return this->Class_T::paintEvent(static_cast<QPaintEvent *>(event));
151
+ case bando_FunctionName::resizeEvent: return this->Class_T::resizeEvent(static_cast<QResizeEvent *>(event));
152
+ case bando_FunctionName::showEvent: return this->Class_T::showEvent(static_cast<QShowEvent *>(event));
153
+ case bando_FunctionName::tabletEvent: return this->Class_T::tabletEvent(static_cast<QTabletEvent *>(event));
154
+ case bando_FunctionName::wheelEvent: return this->Class_T::wheelEvent(static_cast<QWheelEvent *>(event));
155
+ default: Q_UNREACHABLE(); break;
156
+ }
157
+ };
158
+
159
+ public:
160
+ template <typename BC_T, typename C_T> friend const QMetaObject *bando_metaObject(const BC_T *self);
161
+
162
+ Rice::Object value_;
163
+ VALUE *value_address_;
164
+
165
+ QMetaObject *mo_;
166
+ };
167
+
168
+ template <typename BC_T, typename C_T>
169
+ Rice::Data_Type<BC_T> define_bando_qwidget_under(Rice::Module module, char const *name)
170
+ {
171
+ Rice::Data_Type<BC_T> bando_qlass =
172
+ Rice::define_class_under<BC_T, C_T>(module, name)
173
+ .define_method("_initialize_ruby_value", &BC_T::initializeValue, Rice::Arg("value"), Rice::Arg("mo"))
174
+ .define_method("_ruby_value", &BC_T::value)
175
+ .define_method("_ruby_value_handle_event", &BC_T::Class_T_handleEvent, Rice::Arg("name"), Rice::Arg("event"))
176
+ .define_method("_event", &BC_T::Class_T_handleQObjectEvent, Rice::Arg("event"))
177
+ .define_method("_event_filter", &BC_T::Class_T_handleQObjectEventFilter, Rice::Arg("watched"), Rice::Arg("event"))
178
+ .define_method("sender", &BC_T::sender);
179
+
180
+ bando_qlass
181
+ .define_method("minimum_size_hint", [](BC_T *self) -> QSize { return self->C_T::minimumSizeHint(); })
182
+ .define_method("size_hint", [](BC_T *self) -> QSize { return self->C_T::sizeHint(); });
183
+
184
+ return bando_qlass;
185
+ }
186
+
187
+ #endif
@@ -0,0 +1,53 @@
1
+ // This file is part of [RubyQt6](https://github.com/souk4711/ruby-qt6).
2
+ //
3
+ // It is licensed under the LGPLv3, included below.
4
+ //
5
+ // As a special exception to the GNU Lesser General Public License version 3
6
+ // ("LGPL3"), the copyright holders of this Library give you permission to
7
+ // convey to a third party a Combined Work that links statically or dynamically
8
+ // to this Library without providing any Minimal Corresponding Source or
9
+ // Minimal Application Code as set out in 4d or providing the installation
10
+ // information set out in section 4e, provided that you comply with the other
11
+ // provisions of LGPL3 and provided that you meet, for the Application the
12
+ // terms and conditions of the license(s) which apply to the Application.
13
+ //
14
+ // Except as stated in this special exception, the provisions of LGPL3 will
15
+ // continue to comply in full to this Library. If you modify this Library, you
16
+ // may apply this exception to your version of this Library, but you are not
17
+ // obliged to do so. If you do not wish to do so, delete this exception
18
+ // statement from your version. This exception does not (and cannot) modify any
19
+ // license terms which apply to the Application, with which you must still
20
+ // comply.
21
+
22
+ #ifndef RICE_QDBUSREPLY_HPP
23
+ #define RICE_QDBUSREPLY_HPP
24
+
25
+ #include <rice/rice.hpp>
26
+ #include <rice/stl.hpp>
27
+
28
+ #include <QDBusReply>
29
+
30
+ using namespace Rice;
31
+
32
+ template <typename Value_T>
33
+ void define_qdbusreply_under(Module module)
34
+ {
35
+ using QDBusReply_T = QDBusReply<Value_T>;
36
+ using QDBusPendingReply_T = QDBusPendingReply<Value_T>;
37
+
38
+ detail::TypeMapper<QDBusReply_T> typeMapper;
39
+ std::string klassName = typeMapper.rubyName();
40
+ Identifier id(klassName);
41
+
42
+ define_class_under<QDBusReply_T>(module, id)
43
+ .define_constructor(Constructor<QDBusReply_T, const QDBusError &>(), Arg("dbus_error") = static_cast<const QDBusError &>(QDBusError()))
44
+ .define_constructor(Constructor<QDBusReply_T, const QDBusMessage &>(), Arg("reply"))
45
+ .define_constructor(Constructor<QDBusReply_T, const QDBusPendingCall &>(), Arg("pcall"))
46
+ .define_constructor(Constructor<QDBusReply_T, const QDBusPendingReply_T &>(), Arg("reply"))
47
+ .define_constructor(Constructor<QDBusReply_T, const QDBusReply_T &>(), Arg("other"))
48
+ .template define_method<const QDBusError &(QDBusReply_T::*)()>("error", &QDBusReply_T::error)
49
+ .template define_method("valid?", &QDBusReply_T::isValid)
50
+ .template define_method("value", &QDBusReply_T::value);
51
+ };
52
+
53
+ #endif
@@ -0,0 +1,51 @@
1
+ // This file is part of [RubyQt6](https://github.com/souk4711/ruby-qt6).
2
+ //
3
+ // It is licensed under the LGPLv3, included below.
4
+ //
5
+ // As a special exception to the GNU Lesser General Public License version 3
6
+ // ("LGPL3"), the copyright holders of this Library give you permission to
7
+ // convey to a third party a Combined Work that links statically or dynamically
8
+ // to this Library without providing any Minimal Corresponding Source or
9
+ // Minimal Application Code as set out in 4d or providing the installation
10
+ // information set out in section 4e, provided that you comply with the other
11
+ // provisions of LGPL3 and provided that you meet, for the Application the
12
+ // terms and conditions of the license(s) which apply to the Application.
13
+ //
14
+ // Except as stated in this special exception, the provisions of LGPL3 will
15
+ // continue to comply in full to this Library. If you modify this Library, you
16
+ // may apply this exception to your version of this Library, but you are not
17
+ // obliged to do so. If you do not wish to do so, delete this exception
18
+ // statement from your version. This exception does not (and cannot) modify any
19
+ // license terms which apply to the Application, with which you must still
20
+ // comply.
21
+
22
+ #ifndef RICE_QENUM_HPP
23
+ #define RICE_QENUM_HPP
24
+
25
+ #include <rice/rice.hpp>
26
+ #include <rice/stl.hpp>
27
+
28
+ #include <QVariant>
29
+
30
+ using namespace Rice;
31
+
32
+ template<typename Enum_T>
33
+ Data_Type<Enum_T> define_qenum_under(Module module, char const* name)
34
+ {
35
+ Data_Type<Enum_T> qenum =
36
+ define_class_under<Enum_T>(module, name)
37
+ .template define_method("to_i", [](Enum_T *self) -> int { return (int)(*self); })
38
+ .define_singleton_function("_qvariant_register_metatype", []() -> int { return qRegisterMetaType<Enum_T>(); })
39
+ .define_singleton_function("_qvariant_from_value", [](const Enum_T &value) -> QVariant { return QVariant::fromValue(value); })
40
+ .define_singleton_function("_qvariant_to_value", [](const QVariant &qvariant) -> Enum_T { return qvariant.value<Enum_T>(); });
41
+ return qenum;
42
+ }
43
+
44
+ template<typename Enum_T>
45
+ void define_qenum_value_under(Data_Type<Enum_T> qenum, std::string name, Enum_T value)
46
+ {
47
+ Data_Object<Enum_T> object(value, true, Data_Type<Enum_T>::klass());
48
+ qenum.const_set(name, object);
49
+ }
50
+
51
+ #endif
@@ -0,0 +1,58 @@
1
+ // This file is part of [RubyQt6](https://github.com/souk4711/ruby-qt6).
2
+ //
3
+ // It is licensed under the LGPLv3, included below.
4
+ //
5
+ // As a special exception to the GNU Lesser General Public License version 3
6
+ // ("LGPL3"), the copyright holders of this Library give you permission to
7
+ // convey to a third party a Combined Work that links statically or dynamically
8
+ // to this Library without providing any Minimal Corresponding Source or
9
+ // Minimal Application Code as set out in 4d or providing the installation
10
+ // information set out in section 4e, provided that you comply with the other
11
+ // provisions of LGPL3 and provided that you meet, for the Application the
12
+ // terms and conditions of the license(s) which apply to the Application.
13
+ //
14
+ // Except as stated in this special exception, the provisions of LGPL3 will
15
+ // continue to comply in full to this Library. If you modify this Library, you
16
+ // may apply this exception to your version of this Library, but you are not
17
+ // obliged to do so. If you do not wish to do so, delete this exception
18
+ // statement from your version. This exception does not (and cannot) modify any
19
+ // license terms which apply to the Application, with which you must still
20
+ // comply.
21
+
22
+ #ifndef RICE_QFLAGS_HPP
23
+ #define RICE_QFLAGS_HPP
24
+
25
+ #include <rice/rice.hpp>
26
+ #include <rice/stl.hpp>
27
+
28
+ #include <QFlags>
29
+
30
+ using namespace Rice;
31
+
32
+ template<typename Enum_T>
33
+ Data_Type<QFlags<Enum_T>> define_qflags_under(Module module, char const* name)
34
+ {
35
+ using QFlags_T = QFlags<Enum_T>;
36
+
37
+ Data_Type<QFlags_T> qflags = define_class_under<QFlags_T>(module, name)
38
+ .define_constructor(Constructor<QFlags_T>())
39
+ .define_constructor(Constructor<QFlags_T, Enum_T>(), Arg("flags"))
40
+ .template define_method("to_i", [](QFlags_T *self) -> int { return (int)(*self); })
41
+ .template define_method("~", &QFlags_T::operator~)
42
+ .template define_method<QFlags_T (QFlags_T::*)(Enum_T) const noexcept>("&", &QFlags_T::operator&, Arg("mask"))
43
+ .template define_method<QFlags_T (QFlags_T::*)(QFlags_T) const noexcept>("&", &QFlags_T::operator&, Arg("mask"))
44
+ .template define_method<QFlags_T &(QFlags_T::*)(Enum_T) noexcept>("&=", &QFlags_T::operator&=, Arg("mask"))
45
+ .template define_method<QFlags_T &(QFlags_T::*)(QFlags_T) noexcept>("&=", &QFlags_T::operator&=, Arg("mask"))
46
+ .template define_method<QFlags_T (QFlags_T::*)(Enum_T) const noexcept>("^", &QFlags_T::operator^, Arg("other"))
47
+ .template define_method<QFlags_T (QFlags_T::*)(QFlags_T) const noexcept>("^", &QFlags_T::operator^, Arg("other"))
48
+ .template define_method<QFlags_T &(QFlags_T::*)(Enum_T) noexcept>("^=", &QFlags_T::operator^=, Arg("other"))
49
+ .template define_method<QFlags_T &(QFlags_T::*)(QFlags_T) noexcept>("^=", &QFlags_T::operator^=, Arg("other"))
50
+ .template define_method<QFlags_T (QFlags_T::*)(Enum_T) const noexcept>("|", &QFlags_T::operator|, Arg("other"))
51
+ .template define_method<QFlags_T (QFlags_T::*)(QFlags_T) const noexcept>("|", &QFlags_T::operator|, Arg("other"))
52
+ .template define_method<QFlags_T &(QFlags_T::*)(Enum_T) noexcept>("|=", &QFlags_T::operator|=, Arg("other"))
53
+ .template define_method<QFlags_T &(QFlags_T::*)(QFlags_T) noexcept>("|=", &QFlags_T::operator|=, Arg("other"))
54
+ .template define_singleton_function("from_int", [](int i) -> QFlags_T { return QFlags_T::fromInt(i); }, Arg("i"));
55
+ return qflags;
56
+ }
57
+
58
+ #endif
@@ -0,0 +1,238 @@
1
+ // This file is part of [RubyQt6](https://github.com/souk4711/ruby-qt6).
2
+ //
3
+ // It is licensed under the LGPLv3, included below.
4
+ //
5
+ // As a special exception to the GNU Lesser General Public License version 3
6
+ // ("LGPL3"), the copyright holders of this Library give you permission to
7
+ // convey to a third party a Combined Work that links statically or dynamically
8
+ // to this Library without providing any Minimal Corresponding Source or
9
+ // Minimal Application Code as set out in 4d or providing the installation
10
+ // information set out in section 4e, provided that you comply with the other
11
+ // provisions of LGPL3 and provided that you meet, for the Application the
12
+ // terms and conditions of the license(s) which apply to the Application.
13
+ //
14
+ // Except as stated in this special exception, the provisions of LGPL3 will
15
+ // continue to comply in full to this Library. If you modify this Library, you
16
+ // may apply this exception to your version of this Library, but you are not
17
+ // obliged to do so. If you do not wish to do so, delete this exception
18
+ // statement from your version. This exception does not (and cannot) modify any
19
+ // license terms which apply to the Application, with which you must still
20
+ // comply.
21
+
22
+ #ifndef RICE_QLIST_HPP
23
+ #define RICE_QLIST_HPP
24
+
25
+ #include <rice/rice.hpp>
26
+ #include <rice/stl.hpp>
27
+
28
+ #include <QList>
29
+
30
+ using namespace Rice;
31
+
32
+ template<typename QList_T>
33
+ class DefineQListMethods
34
+ {
35
+ using Value_T = typename QList_T::value_type;
36
+ using Parameter_T = typename QList_T::parameter_type;
37
+
38
+ public:
39
+ DefineQListMethods(Data_Type<QList_T> klass) : klass_(klass)
40
+ {
41
+ this->define_constructors();
42
+ this->define_constructable_methods();
43
+ this->define_capacity_methods();
44
+ this->define_access_methods();
45
+ this->define_comparable_methods();
46
+ this->define_insert_methods();
47
+ this->define_remove_methods();
48
+ this->define_replace_methods();
49
+ this->define_enumerable();
50
+ this->define_to_array();
51
+ this->define_to_s();
52
+ }
53
+
54
+ private:
55
+ void define_constructors()
56
+ {
57
+ klass_.define_constructor(Constructor<QList_T>());
58
+ klass_.define_constructor(Constructor<QList_T, qsizetype>(), Arg("size"));
59
+ klass_.define_constructor(Constructor<QList_T, qsizetype, const Parameter_T>(), Arg("size"), Arg("value"));
60
+ klass_.define_constructor(Constructor<QList_T, const QList_T&>(), Arg("other"));
61
+ }
62
+
63
+ void define_constructable_methods()
64
+ {
65
+ klass_.template define_method<void (QList_T::*)(qsizetype)>("resize", &QList_T::resize, Arg("size"));
66
+ klass_.template define_method<void (QList_T::*)(qsizetype, Parameter_T)>("resize", &QList_T::resize, Arg("size"), Arg("value"));
67
+ }
68
+
69
+ void define_capacity_methods()
70
+ {
71
+ klass_.define_method("capacity", &QList_T::capacity);
72
+ klass_.define_method("empty?", &QList_T::isEmpty);
73
+ klass_.define_method("length", &QList_T::length);
74
+ klass_.define_method("max_size", &QList_T::max_size);
75
+ klass_.define_method("reserve", &QList_T::reserve);
76
+ klass_.define_method("shrink_to_fit", &QList_T::shrink_to_fit);
77
+ klass_.define_method("size", &QList_T::size);
78
+ }
79
+
80
+ void define_access_methods()
81
+ {
82
+ klass_.define_method("at", [](QList_T *self, qsizetype index) -> std::optional<Value_T> {
83
+ if (index < 0) {
84
+ index = index + self->size();
85
+ if (index < 0) return std::nullopt;
86
+ } else if (index >= self->size()) {
87
+ return std::nullopt;
88
+ }
89
+ return self->at(index);
90
+ });
91
+
92
+ klass_.define_method("first", [](QList_T *self) -> std::optional<Value_T> {
93
+ if (self->size() > 0)
94
+ return self->first();
95
+ return std::nullopt;
96
+ });
97
+
98
+ klass_.define_method("last", [](QList_T *self) -> std::optional<Value_T> {
99
+ if (self->size() > 0)
100
+ return self->last();
101
+ return std::nullopt;
102
+ });
103
+
104
+ rb_define_alias(klass_, "[]", "at");
105
+ }
106
+
107
+ void define_comparable_methods()
108
+ {
109
+ klass_.define_method("count", [](QList_T *self) -> qsizetype {
110
+ return self->size();
111
+ });
112
+ klass_.define_method("count", [](QList_T *self, Parameter_T element) -> qsizetype {
113
+ return self->count(element);
114
+ });
115
+
116
+ klass_.define_method("include?", [](QList_T *self, Parameter_T element) -> bool {
117
+ return self->contains(element);
118
+ });
119
+
120
+ klass_.define_method("index", [](QList_T *self, Parameter_T element) -> std::optional<qsizetype> {
121
+ qsizetype index = self->indexOf(element);
122
+ if (index != -1)
123
+ return index;
124
+ return std::nullopt;
125
+ });
126
+ }
127
+
128
+ void define_insert_methods()
129
+ {
130
+ klass_.define_method("insert", [](QList_T *self, qsizetype index, Parameter_T element) -> QList_T* {
131
+ if (index < 0) {
132
+ index = index + self->size() + 1;
133
+ if (index < 0) throw std::out_of_range("index " + std::to_string(index - 1 - self->size()) + " too small for array; minimum: -" + std::to_string(self->size() + 1));
134
+ } else if (index > self->size()) {
135
+ throw std::out_of_range("index " + std::to_string(index) + " too big");
136
+ }
137
+ self->insert(index, element);
138
+ return self;
139
+ });
140
+
141
+ klass_.define_method("push", [](QList_T *self, Parameter_T element) -> QList_T* {
142
+ self->push_back(element);
143
+ return self;
144
+ });
145
+
146
+ klass_.define_method("pop", [](QList_T *self) -> std::optional<Value_T> {
147
+ if (self->size() > 0)
148
+ return self->takeLast();
149
+ return std::nullopt;
150
+ });
151
+
152
+ rb_define_alias(klass_, "<<", "push");
153
+ }
154
+
155
+ void define_remove_methods()
156
+ {
157
+ klass_.define_method("clear", [](QList_T *self) -> QList_T* {
158
+ self->clear();
159
+ return self;
160
+ });
161
+
162
+ klass_.define_method("delete", [](QList_T *self, Parameter_T element) -> std::optional<Value_T> {
163
+ qsizetype index = self->lastIndexOf(element);
164
+ if (index != -1) {
165
+ Value_T result = self->takeAt(index);
166
+ self->removeAll(element);
167
+ return result;
168
+ }
169
+ return std::nullopt;
170
+ });
171
+
172
+ klass_.define_method("delete_at", [](QList_T *self, qsizetype index) -> std::optional<Value_T> {
173
+ if (index < 0) {
174
+ index = index + self->size();
175
+ if (index < 0) return std::nullopt;
176
+ } else if (index >= self->size()) {
177
+ return std::nullopt;
178
+ }
179
+ return self->takeAt(index);
180
+ });
181
+ }
182
+
183
+ void define_replace_methods()
184
+ {
185
+ klass_.define_method("[]=", [](QList_T*self, qsizetype index, Parameter_T element) -> Parameter_T {
186
+ if (index < 0) {
187
+ index = index + self->size();
188
+ if (index < 0) throw std::out_of_range("index " + std::to_string(index - self->size()) + " too small for array; minimum: -" + std::to_string(self->size()));
189
+ } else if (index > self->size()) {
190
+ throw std::out_of_range("index " + std::to_string(index) + " too big");
191
+ }
192
+ if (index == self->size()) {
193
+ self->push_back(element);
194
+ } else {
195
+ self->replace(index, element);
196
+ }
197
+ return element;
198
+ });
199
+ }
200
+
201
+ void define_enumerable()
202
+ {
203
+ klass_.include_module(rb_mEnumerable);
204
+ klass_.define_method("each", [](QList_T *self) -> QList_T* {
205
+ for (qsizetype i = 0; i < self->size(); i++) {
206
+ Rice::detail::protect(rb_yield, Rice::detail::to_ruby(self->at(i)));
207
+ }
208
+ return self;
209
+ });
210
+ }
211
+
212
+ void define_to_array()
213
+ {
214
+ rb_define_alias(klass_, "to_ary", "to_a");
215
+ }
216
+
217
+ void define_to_s()
218
+ {
219
+ }
220
+
221
+ private:
222
+ Data_Type<QList_T> klass_;
223
+ };
224
+
225
+ template <typename Value_T>
226
+ void define_qlist_under(Module module)
227
+ {
228
+ using QList_T = QList<Value_T>;
229
+
230
+ detail::TypeMapper<QList_T> typeMapper;
231
+ std::string klassName = typeMapper.rubyName();
232
+ Identifier id(klassName);
233
+
234
+ Data_Type<QList_T> qlist = define_class_under<QList_T>(module, id);
235
+ DefineQListMethods<QList_T> qlist_(qlist);
236
+ }
237
+
238
+ #endif