28#include <unordered_map>
32#include <QAbstractItemView>
34#include <QDialogButtonBox>
38#include <QSignalBlocker>
50 [[nodiscard]] std::optional<std::vector<std::string>>
51 selectConversionChoices(QWidget* parent,
52 std::vector<SILICON::conversion::ConversionChoice> choices)
54 if (choices.size() <= 1) {
55 std::vector<std::string> selected;
56 for (
const auto& choice : choices)
57 selected.push_back(choice.id);
61 std::ranges::sort(choices, [](
const auto& lhs,
const auto& rhs) {
62 if (lhs.dependencies.size() != rhs.dependencies.size())
63 return lhs.dependencies.size() > rhs.dependencies.size();
64 return lhs.label < rhs.label;
67 std::unordered_map<std::string,
const SILICON::conversion::ConversionChoice*,
68 core::TransparentStringHash, core::TransparentStringEqual>
70 for (
const auto& choice : choices)
71 choicesById.emplace(choice.id, &choice);
73 QDialog dialog(parent);
74 dialog.setWindowTitle(QObject::tr(
"Select Conversion Items"));
75 dialog.setModal(
true);
76 dialog.resize(560, 420);
78 auto* layout =
new QVBoxLayout(&dialog);
79 layout->addWidget(
new QLabel(
80 QObject::tr(
"Select one or more items. Their dependencies are converted "
84 auto* tree =
new QTreeWidget(&dialog);
85 tree->setColumnCount(1);
86 tree->setHeaderLabels({QObject::tr(
"Item")});
87 tree->setRootIsDecorated(
true);
88 tree->setSelectionMode(QAbstractItemView::NoSelection);
89 constexpr int EXPLICITLY_SELECTED_ROLE = Qt::UserRole;
90 constexpr int CHOICE_ID_ROLE = Qt::UserRole + 1;
92 const auto addDependencies = [&choicesById](
this auto&& addDependencies,
93 QTreeWidgetItem* parentItem,
94 const std::string_view choiceId) ->
void {
95 const auto choice = choicesById.find(choiceId);
96 if (choice == choicesById.end())
98 for (
const auto& dependency : choice->second->dependencies) {
99 auto* dependencyItem =
new QTreeWidgetItem(parentItem);
100 const auto dependencyChoice = choicesById.find(dependency);
101 dependencyItem->setText(
102 0, QString::fromStdString(dependencyChoice == choicesById.end()
104 : dependencyChoice->second->label));
105 dependencyItem->setFlags(dependencyItem->flags() & ~Qt::ItemIsUserCheckable);
106 addDependencies(dependencyItem, dependency);
110 for (
const auto& choice : choices) {
111 auto* item =
new QTreeWidgetItem(tree);
112 item->setText(0, QString::fromStdString(choice.label));
113 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
114 item->setCheckState(0, Qt::Unchecked);
115 item->setData(0, EXPLICITLY_SELECTED_ROLE,
false);
116 item->setData(0, CHOICE_ID_ROLE, QString::fromStdString(choice.id));
117 addDependencies(item, choice.id);
120 tree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
121 layout->addWidget(tree);
124 new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog);
125 auto* importButton = buttons->button(QDialogButtonBox::Ok);
126 importButton->setText(QObject::tr(
"Import"));
127 importButton->setEnabled(
false);
128 layout->addWidget(buttons);
129 QObject::connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
130 QObject::connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
133 tree, &QTreeWidget::itemChanged, &dialog,
134 [tree, importButton, &choicesById](QTreeWidgetItem* changedItem,
int) {
135 const QSignalBlocker blocker(tree);
136 if (!changedItem->parent()
137 && changedItem->flags().testFlag(Qt::ItemIsEnabled)) {
138 changedItem->setData(0, EXPLICITLY_SELECTED_ROLE,
139 changedItem->checkState(0) == Qt::Checked);
142 std::vector<std::string> roots;
143 for (
int index = 0; index < tree->topLevelItemCount(); ++index) {
144 const auto* item = tree->topLevelItem(index);
145 if (item->data(0, EXPLICITLY_SELECTED_ROLE).toBool())
146 roots.push_back(item->data(0, CHOICE_ID_ROLE).toString().toStdString());
149 importButton->setEnabled(!roots.empty());
150 std::vector<std::string> dependencies;
151 const auto collectDependencies =
152 [&choicesById, &dependencies](
this auto&& collectDependencies,
153 const std::string_view
id) ->
void {
154 const auto choice = choicesById.find(
id);
155 if (choice == choicesById.end())
157 for (
const auto& dependency : choice->second->dependencies) {
158 if (std::ranges::contains(dependencies, dependency))
160 dependencies.push_back(dependency);
161 collectDependencies(dependency);
164 for (
const auto& root : roots)
165 collectDependencies(root);
167 for (
int index = 0; index < tree->topLevelItemCount(); ++index) {
168 auto* item = tree->topLevelItem(index);
169 const auto name = item->data(0, CHOICE_ID_ROLE).toString().toStdString();
170 const bool selected = item->data(0, EXPLICITLY_SELECTED_ROLE).toBool();
171 const bool dependency = std::ranges::contains(dependencies, name);
173 auto flags = item->flags() | Qt::ItemIsUserCheckable;
174 flags.setFlag(Qt::ItemIsEnabled, !dependency);
175 item->setFlags(flags);
176 item->setCheckState(0, selected || dependency ? Qt::Checked : Qt::Unchecked);
178 auto font = item->font(0);
179 font.setBold(selected && !dependency);
180 font.setItalic(dependency);
181 item->setFont(0, font);
185 if (dialog.exec() != QDialog::Accepted)
188 std::vector<std::string> roots;
189 for (
int index = 0; index < tree->topLevelItemCount(); ++index) {
190 const auto* item = tree->topLevelItem(index);
191 if (item->data(0, EXPLICITLY_SELECTED_ROLE).toBool())
192 roots.push_back(item->data(0, CHOICE_ID_ROLE).toString().toStdString());
202 session.projectContext.documents().find(
session.activeDocumentPath);
207 std::vector<SILICON::project::DocumentType> targets;
209 for (
const auto* converter : converters) {
210 if (!converter->available)
212 targets.push_back(converter->target);
218 if (targets.size() == 1) {
224 dialogParent, tr(
"Convert Document"), tr(
"Target format"), labels, 0,
false,
225 [
this, labels = std::move(labels),
226 targets = std::move(targets)](
const QString& selected) {
227 const auto index = labels.indexOf(selected);
228 if (index >= 0 && index <
static_cast<qsizetype
>(targets.size()))
239 const auto sourcePath =
session.activeDocumentPath;
240 const auto& store =
session.projectContext.documents();
241 const auto* source = store.find(sourcePath);
243 throw std::runtime_error(
"The active document no longer exists");
245 && !
workspace.circuitEditor().scene()->currentConnectionReport().valid())
246 throw std::runtime_error(
"Repair the invalid wire connections before conversion");
249 *source, target, store.getDocuments(),
251 auto selected = selectConversionChoices(
dialogParent, prepared.choices);
255 auto result = prepared.execute(*selected);
259 result.activatePath, commandText,
260 tr(
"Code Conversion Error"));
261 }
catch (
const std::exception& error) {
264 tr(
"Failed to convert the active document:\n%1").arg(error.what()));
static ComponentRegistry & instance()
EditorWorkspace & workspace
void convertActiveDocumentTo(SILICON::project::DocumentType target)
void commitDocumentChanges(std::vector< SILICON::project::Document > documents, const std::string &sourcePath, const std::string &activatePath, const QString &commandText, const QString &errorTitle)
void convertActiveDocument()
RTLIL::IdString id(const char *name)
std::vector< const DocumentConverter * > documentConvertersFor(const DocumentType source)
PreparedDocumentConversion prepareDocumentConversion(const Document &source, const DocumentType target, const std::span< const Document > projectDocuments, const SILICON::core::ComponentRegistry ®istry, const SILICON::core::CircuitResolver &resolver)
QString documentTypeName(SILICON::project::DocumentType type)