* @brief Opens a file dialog to import an external JS file. */
| 295 | * @brief Opens a file dialog to import an external JS file. |
| 296 | */ |
| 297 | void DataModel::PainterCodeEditor::import() |
| 298 | { |
| 299 | auto* dialog = new QFileDialog( |
| 300 | nullptr, tr("Select Javascript file to import"), QDir::homePath(), QStringLiteral("*.js")); |
| 301 | dialog->setFileMode(QFileDialog::ExistingFile); |
| 302 | |
| 303 | connect(dialog, &QFileDialog::fileSelected, this, [this, dialog](const QString& path) { |
| 304 | dialog->deleteLater(); |
| 305 | if (path.isEmpty()) |
| 306 | return; |
| 307 | |
| 308 | QMetaObject::invokeMethod( |
| 309 | this, |
| 310 | [this, path]() { |
| 311 | QFile file(path); |
| 312 | if (file.open(QFile::ReadOnly)) { |
| 313 | m_widget.setPlainText(QString::fromUtf8(file.readAll())); |
| 314 | file.close(); |
| 315 | } |
| 316 | }, |
| 317 | Qt::QueuedConnection); |
| 318 | }); |
| 319 | |
| 320 | dialog->open(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @brief Loads the painter code from the currently selected group. |