1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: MIT |
3 | |
4 | #include <QtWidgets/QtWidgets> |
5 | #include <slint-interpreter.h> |
6 | |
7 | #include "ui_interface.h" |
8 | |
9 | struct LoadedFile |
10 | { |
11 | slint::ComponentHandle<slint::interpreter::ComponentInstance> instance; |
12 | QWidget *widget; |
13 | }; |
14 | |
15 | void show_diagnostics(QWidget *root, |
16 | const slint::SharedVector<slint::interpreter::Diagnostic> &diags) |
17 | { |
18 | QString text; |
19 | |
20 | for (auto diagnostic : diags) { |
21 | text += (diagnostic.level == slint::interpreter::DiagnosticLevel::Warning |
22 | ? QApplication::translate("qt_viewer" , "warning: %1\n" ) |
23 | : QApplication::translate("qt_viewer" , "error: %1\n" )) |
24 | .arg(QString::fromUtf8(diagnostic.message.data())); |
25 | |
26 | text += QApplication::translate("qt_viewer" , "location: %1" ) |
27 | .arg(QString::fromUtf8(diagnostic.source_file.data())); |
28 | if (diagnostic.line > 0) |
29 | text += ":" + QString::number(diagnostic.line); |
30 | if (diagnostic.column > 0) |
31 | text += ":" + QString::number(diagnostic.column); |
32 | text += "\n" ; |
33 | } |
34 | |
35 | QMessageBox::critical(root, QApplication::translate("qt_viewer" , "Compilation error" ), text, |
36 | QMessageBox::StandardButton::Ok); |
37 | } |
38 | |
39 | int main(int argc, char **argv) |
40 | { |
41 | QApplication app(argc, argv); |
42 | QWidget main; |
43 | Ui::Interface ui; |
44 | ui.setupUi(&main); |
45 | QHBoxLayout layout(ui.my_content); |
46 | |
47 | std::unique_ptr<LoadedFile> loaded_file; |
48 | slint::interpreter::Value::Type currentType; |
49 | |
50 | auto load_file = [&](const QString &fileName) { |
51 | loaded_file.reset(); |
52 | slint::interpreter::ComponentCompiler compiler; |
53 | auto def = compiler.build_from_path(path: fileName.toUtf8().data()); |
54 | if (!def) { |
55 | show_diagnostics(&main, compiler.diagnostics()); |
56 | return; |
57 | } |
58 | auto instance = def->create(); |
59 | QWidget *wid = instance->qwidget(); |
60 | if (!wid) { |
61 | QMessageBox::critical(&main, QApplication::translate("qt_viewer" , "No Qt backend" ), |
62 | QApplication::translate( |
63 | "qt_viewer" , "Slint is not running with the Qt backend." ), |
64 | QMessageBox::StandardButton::Ok); |
65 | return; |
66 | } |
67 | wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
68 | layout.addWidget(wid); |
69 | loaded_file = std::make_unique<LoadedFile>(args: LoadedFile { instance, wid }); |
70 | }; |
71 | |
72 | auto args = app.arguments(); |
73 | if (args.count() == 2) { |
74 | load_file(args.at(1)); |
75 | } |
76 | |
77 | QObject::connect(ui.load_button, &QPushButton::clicked, [&] { |
78 | QString fileName = QFileDialog::getOpenFileName( |
79 | &main, QApplication::translate("qt_viewer" , "Open Slint File" ), {}, |
80 | QApplication::translate("qt_viewer" , "Slint File (*.slint)" )); |
81 | if (fileName.isEmpty()) |
82 | return; |
83 | load_file(fileName); |
84 | }); |
85 | |
86 | QObject::connect(ui.prop_name, &QLineEdit::textChanged, [&] { |
87 | if (!loaded_file) |
88 | return; |
89 | if (auto val = loaded_file->instance->get_property(ui.prop_name->text().toUtf8().data())) { |
90 | currentType = val->type(); |
91 | switch (currentType) { |
92 | case slint::interpreter::Value::Type::String: |
93 | ui.prop_value->setText(QString::fromUtf8(val->to_string()->data())); |
94 | break; |
95 | |
96 | case slint::interpreter::Value::Type::Number: |
97 | ui.prop_value->setText(QString::number(*val->to_number())); |
98 | break; |
99 | |
100 | default: |
101 | ui.prop_value->clear(); |
102 | break; |
103 | } |
104 | } |
105 | }); |
106 | |
107 | QObject::connect(ui.set_button, &QPushButton::clicked, [&] { |
108 | if (!loaded_file) |
109 | return; |
110 | slint::interpreter::Value val; |
111 | switch (currentType) { |
112 | case slint::interpreter::Value::Type::String: |
113 | val = slint::SharedString(ui.prop_value->text().toUtf8().data()); |
114 | break; |
115 | case slint::interpreter::Value::Type::Number: { |
116 | bool ok; |
117 | val = ui.prop_value->text().toDouble(&ok); |
118 | if (!ok) { |
119 | QMessageBox::critical( |
120 | &main, QApplication::translate("qt_viewer" , "Set Property Error" ), |
121 | QApplication::translate("qt_viewer" , "Invalid conversion to number" ), |
122 | QMessageBox::StandardButton::Ok); |
123 | return; |
124 | } |
125 | break; |
126 | } |
127 | default: |
128 | QMessageBox::critical( |
129 | &main, QApplication::translate("qt_viewer" , "Set Property Error" ), |
130 | QApplication::translate("qt_viewer" , "Cannot set properties of this type" ), |
131 | QMessageBox::StandardButton::Ok); |
132 | return; |
133 | } |
134 | if (!loaded_file->instance->set_property(ui.prop_name->text().toUtf8().data(), val)) { |
135 | QMessageBox::critical(&main, QApplication::translate("qt_viewer" , "Set Property Error" ), |
136 | QApplication::translate("qt_viewer" , "Could not set property" ), |
137 | QMessageBox::StandardButton::Ok); |
138 | } |
139 | }); |
140 | |
141 | main.show(); |
142 | return app.exec(); |
143 | } |
144 | |