MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / buildTemplates

Method buildTemplates

app/src/MQTT/PublisherScriptEditor.cpp:748–799  ·  view source on GitHub ↗

* @brief Loads MQTT script templates from the JSON manifest and resource files. */

Source from the content-addressed store, hash-verified

746 * @brief Loads MQTT script templates from the JSON manifest and resource files.
747 */
748void MQTT::PublisherScriptEditor::buildTemplates()
749{
750 QString selectedFile;
751 if (m_templateCombo) {
752 const int currentIndex = m_templateCombo->currentIndex();
753 if (currentIndex > 0 && currentIndex <= m_templates.size())
754 selectedFile = m_templates[currentIndex - 1].file;
755 }
756
757 const auto definitions =
758 DataModel::loadScriptTemplateManifest(QStringLiteral(":/scripts/mqtt/templates.json"));
759
760 m_templates.clear();
761 m_templates.reserve(definitions.size());
762
763 for (const auto& def : definitions) {
764 m_templates.append({
765 def.file,
766 def.name,
767 DataModel::readTextResource(DataModel::templateResourcePath(
768 QStringLiteral(":/scripts/mqtt/lua"), def.file, QStringLiteral(".lua"))),
769 DataModel::readTextResource(DataModel::templateResourcePath(
770 QStringLiteral(":/scripts/mqtt/js"), def.file, QStringLiteral(".js"))),
771 });
772 }
773
774 if (!m_templateCombo)
775 return;
776
777 m_templateCombo->blockSignals(true);
778 m_templateCombo->clear();
779 m_templateCombo->addItem(tr("Select Template…"));
780 for (const auto& tmpl : std::as_const(m_templates))
781 m_templateCombo->addItem(tmpl.name);
782
783 int selectedIndex = 0;
784 if (!selectedFile.isEmpty()) {
785 for (int i = 0; i < m_templates.size(); ++i) {
786 if (m_templates.at(i).file == selectedFile) {
787 selectedIndex = i + 1;
788 break;
789 }
790 }
791 } else {
792 const int detected = detectTemplate();
793 if (detected >= 0)
794 selectedIndex = detected + 1;
795 }
796
797 m_templateCombo->setCurrentIndex(selectedIndex);
798 m_templateCombo->blockSignals(false);
799}
800
801#endif // BUILD_COMMERCIAL

Callers

nothing calls this directly

Calls 6

currentIndexMethod · 0.80
isEmptyMethod · 0.80
setCurrentIndexMethod · 0.80
sizeMethod · 0.45
clearMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected