Skip to content

Commit ec60680

Browse files
committed
add def for settext(text,pythonlist)
1 parent 82a54a6 commit ec60680

File tree

2 files changed

+49
-57
lines changed

2 files changed

+49
-57
lines changed

bindings/interface/TextView.cpp

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@
1515
namespace py = pybind11;
1616
using namespace BPrivate;
1717

18-
/*
19-
struct text_run {
20-
int32_t offset;
21-
BFont font;
22-
rgb_color color;
23-
};
24-
25-
struct text_run_array {
26-
int32_t count;
27-
std::vector<text_run> runs;
28-
text_run_array(int32_t count) : count(count), runs(count) {}
29-
};
30-
*/
31-
3218
class PyBTextView : public BTextView{
3319
public:
3420
using BTextView::BTextView;
@@ -140,57 +126,38 @@ py::class_<text_run>(m, "text_run")
140126
.def_readwrite("font", &text_run::font, "")
141127
.def_readwrite("color", &text_run::color, "")
142128
;
129+
//tentativo di creare text_run gestito da python, produce lo stesso errori
130+
/*m.def("create_text_run", []() { return new text_run(); // Python prende proprietà
131+
}, py::return_value_policy::take_ownership);*/
143132

144133
py::class_<text_run_array>(m, "text_run_array")
145-
.def(py::init<>())
134+
//.def(py::init<>())
135+
/*init has been commented out because it's safer creating a text_run_array with
136+
AllocRunArray static function*/
146137
.def_readwrite("count", &text_run_array::count, "")
147138
//.def_readonly("runs", &text_run_array::runs, "")
148-
/*first attempt
149-
.def_property("runs",[](const text_run_array& self){
150-
return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count);
151-
},[](text_run_array& self, const std::vector<text_run>& new_values) {
152-
if (new_values.size() != self.count){
153-
throw std::runtime_error("Invalid buffer size or dimensions");
154-
}
155-
std::copy(new_values.begin(),new_values.end(),self.runs);
156-
})
157-
;*/
158-
/*
159-
.def_property("runs",[](const text_run_array& self){
160-
return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count);
161-
},[](text_run_array& self, const std::vector<text_run>& new_values) {
162-
if (new_values.size() != self.count){
163-
throw std::runtime_error("Invalid buffer size or dimensions");
164-
}
165-
std::memcpy(self.runs, new_values.data(), new_values.size() * sizeof(text_run));
166-
//std::copy(new_values.begin(),new_values.end(),self.runs);
167-
})
168-
;*/
169-
.def_property("runs",[](const text_run_array& self){
139+
//this method works but gives error on delete(rewrite draw?)
140+
/*.def_property("runs",[](const text_run_array& self){
170141
return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count);
171142
},[](text_run_array& self, const py::list &new_values) {
172143
std::vector<text_run> newruns;
173144
for (auto item : new_values) {
174145
newruns.push_back(item.cast<text_run>());
175146
}
176147
std::memcpy(self.runs, newruns.data(), newruns.size() * sizeof(text_run));
177-
})
148+
})*/
149+
//also this one works but gives errors as above
150+
.def_property("runs", [](const text_run_array& self) -> py::list {
151+
py::list result; for (int i = 0; i < self.count; ++i) {
152+
result.append(self.runs[i]);
153+
} return result;
154+
}, [](text_run_array& self, const py::list& new_values) {
155+
int i = 0; for (auto item : new_values) {
156+
if (i >= self.count) break; // Evita overflow
157+
self.runs[i] = item.cast<text_run>(); ++i;
158+
}
159+
})
178160
;
179-
/* look at this
180-
/*suggested attempt
181-
py::class_<text_run_array>(m, "text_run_array")
182-
.def(py::init<int32_t>(), py::arg("count") = 1)
183-
.def_readwrite("count", &text_run_array::count)
184-
.def_property("runs", [](const text_run_array& self) {
185-
return self.runs;
186-
},
187-
[](text_run_array& self, const std::vector<text_run>& new_values) {
188-
if (new_values.size() != self.count) {
189-
throw std::runtime_error("Invalid buffer size or dimensions");
190-
}
191-
self.runs = new_values;
192-
})
193-
;*/
194161
/*
195162
py::class_<text_run_array>(m, "text_run_array")
196163
.def_readwrite("count", &text_run_array::count, "")
@@ -223,6 +190,18 @@ py::class_<BTextView, PyBTextView, BView, std::unique_ptr<BTextView,py::nodelete
223190
.def("SetText", py::overload_cast<const char *, const text_run_array *>(&BTextView::SetText), "", py::arg("text"), py::arg("runs")=NULL)
224191
.def("SetText", py::overload_cast<const char *, int32, const text_run_array *>(&BTextView::SetText), "", py::arg("text"), py::arg("length"), py::arg("runs")=NULL)
225192
.def("SetText", py::overload_cast<BFile *, int32, int32, const text_run_array *>(&BTextView::SetText), "", py::arg("file"), py::arg("offset"), py::arg("length"), py::arg("runs")=NULL)
193+
.def("SetText", [](BTextView& self, const char* text, const py::list& runs){//&SetTextWrapper, "", py::arg("text"), py::arg("runs")=NULL)
194+
if (!runs.is_none()) {
195+
auto len = runs.size();
196+
text_run_array* tra = BTextView::AllocRunArray(len);
197+
int i = 0; for (auto item : runs) {
198+
if (i >= len) break; // Evita overflow
199+
tra->runs[i] = item.cast<text_run>(); ++i;
200+
}
201+
self.SetText(text, tra);
202+
BTextView::FreeRunArray(tra);
203+
}
204+
}, "", py::arg("text"), py::arg("runs"))
226205
.def("Insert", py::overload_cast<const char *, const text_run_array *>(&BTextView::Insert), "", py::arg("text"), py::arg("runs")=NULL)
227206
.def("Insert", py::overload_cast<const char *, int32, const text_run_array *>(&BTextView::Insert), "", py::arg("text"), py::arg("length"), py::arg("runs")=NULL)
228207
.def("Insert", py::overload_cast<int32, const char *, int32, const text_run_array *>(&BTextView::Insert), "", py::arg("offset"), py::arg("text"), py::arg("length"), py::arg("runs")=NULL)

tmtest.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,16 @@ def __init__(self):
340340
tr2.font=BFont(be_bold_font)
341341
tr2.color=pictura
342342
mytralist=[tr1,tr2]
343+
self.typtap.SetText(stuff,mytralist)
343344
##tra.count=2 #now you have 2 text_runs
344345
##tra.runs[1] = tr2 #assign the second one
345346
##print("tra runs modified",tra.runs)
346347
##self.typtap.SetRunArray(0,len(self.typtap.Text()),tra) #this doesn't work
347-
self.trb=text_run_array()
348+
# self.trb=text_run_array()
348349
#trb=self.typtap.RunArray(0,len(self.typtap.Text()))#this crashes
349-
self.trb.count=2
350-
self.trb.runs=mytralist
351-
self.typtap.SetText(stuff,self.trb)
350+
# self.trb.count=2
351+
# self.trb.runs=mytralist
352+
# self.typtap.SetText(stuff,self.trb)
352353
#self.typtap.SetRunArray(0,len(self.typtap.Text()),trb) #this works, why error exiting and why does trb need tra (or it won't work)
353354
#print(tra.runs[1].color.green)
354355
#self.typtap.SetText(stuff,trb)
@@ -420,7 +421,19 @@ def __init__(self):
420421
def MessageReceived(self, msg):
421422
if msg.what == 1:
422423
self.startimer.SetValue(not(self.startimer.Value()))
424+
425+
423426
elif msg.what == 2:
427+
tr1=text_run()
428+
tr1.offset=0
429+
tr1.font=be_bold_font
430+
tr1.color=rgb_color()
431+
tr2=text_run()
432+
tr2.offset=2
433+
tr2.font=be_plain_font
434+
tr2.color=rgb_color()
435+
tr2.color.blue=255
436+
self.typtap.SetText("Ciao",[tr1,tr2])
424437
x=round(self.statbar.CurrentValue())
425438
if x<100:
426439
outtxt=str(x+1)+"%"

0 commit comments

Comments
 (0)