1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "compileargumentswidget.h"
18
19#include <QCheckBox>
20#include <QComboBox>
21#include <QGridLayout>
22#include <QLabel>
23
24CompileArgumentsWidget::CompileArgumentsWidget(QWidget *parent) :
25 QTabWidget(parent)
26{
27
28}
29
30CompileArgumentsWidget::~CompileArgumentsWidget()
31{
32}
33
34QMap<QString, QString> CompileArgumentsWidget::arguments( bool includeUnset) const
35{
36 QMap<QString, QString> args;
37 const QTabWidget* pTab = this;
38 for (int i=0;i<pTab->count();i++) {
39 QString section = pTab->tabText(i);
40 QWidget* pWidget = pTab->widget(i);
41 QGridLayout* pLayout = static_cast<QGridLayout*>(pWidget->layout());
42 if (pLayout != nullptr) {
43 for (int j=1;j<pLayout->rowCount()-1;j++) {
44 QString key = static_cast<QLabel *>(pLayout->itemAtPosition(j,0)->widget())->text();
45 PCompilerOption pOption = CompilerInfoManager::getCompilerOption(mCompilerType,key);
46 if (!pOption)
47 continue;
48 if (pOption->choices.isEmpty()) {
49 QCheckBox* pCheckbox = static_cast<QCheckBox *>(pLayout->itemAtPosition(j,1)->widget());
50 if (pCheckbox->isChecked()) {
51 args.insert(key,COMPILER_OPTION_ON);
52 } else {
53 if (includeUnset)
54 args.insert(key,"");
55 else
56 args.remove(key);
57 }
58 } else {
59 QComboBox* pCombo = static_cast<QComboBox *>(pLayout->itemAtPosition(j,2)->widget());
60 if (!pCombo->currentData().toString().isEmpty()) {
61 args.insert(key,pCombo->currentData().toString());
62 } else {
63 if (includeUnset)
64 args.insert(key,"");
65 else
66 args.remove(key);
67 }
68 }
69 }
70 }
71 }
72 return args;
73}
74
75void CompileArgumentsWidget::resetUI(Settings::PCompilerSet pSet, const QMap<QString,QString>& options)
76{
77 QTabWidget* pTab = this;
78 while (pTab->count()>0) {
79 QWidget* p=pTab->widget(0);
80 if (p!=nullptr) {
81 pTab->removeTab(0);
82 p->setParent(nullptr);
83 delete p;
84 }
85 }
86 if (!pSet)
87 return;
88 mCompilerType = pSet->compilerType();
89
90 foreach (PCompilerOption pOption, CompilerInfoManager::getCompilerOptions(mCompilerType)) {
91 QWidget* pWidget = nullptr;
92 for (int i=0;i<pTab->count();i++) {
93 if (pOption->section == pTab->tabText(i)) {
94 pWidget = pTab->widget(i);
95 break;
96 }
97 }
98 if (pWidget == nullptr) {
99 pWidget = new QWidget();
100 pTab->addTab(pWidget,pOption->section);
101 pWidget->setLayout(new QGridLayout());
102 }
103 QGridLayout *pLayout = static_cast<QGridLayout*>(pWidget->layout());
104 int row = pLayout->rowCount();
105 QLabel* keyLabel = new QLabel(pOption->key,pWidget);
106 keyLabel->setVisible(false);
107 pLayout->addWidget(keyLabel,row,0);
108 if (pOption->choices.isEmpty()) {
109 QCheckBox* pCheckbox = new QCheckBox(pWidget);
110 pCheckbox->setText(pOption->name);
111 pCheckbox->setChecked(options.value(pOption->key,"")==COMPILER_OPTION_ON);
112 pLayout->addWidget(pCheckbox,row,1);
113 } else {
114 pLayout->addWidget(new QLabel(pOption->name,pWidget),row,1);
115 QComboBox* pCombo = new QComboBox(pWidget);
116 pCombo->addItem("","");
117 for (int i=0;i<pOption->choices.length();i++) {
118 const QPair<QString,QString> &choice = pOption->choices[i];
119 pCombo->addItem(choice.first,choice.second);
120 if (options.value(pOption->key,"") == choice.second)
121 pCombo->setCurrentIndex(i+1);
122 }
123 pLayout->addWidget(pCombo,row,2);
124 }
125 }
126 for (int i=0;i<pTab->count();i++) {
127 QWidget* pWidget = pTab->widget(i);
128 QGridLayout *pLayout = static_cast<QGridLayout*>(pWidget->layout());
129 int row = pLayout->rowCount();
130 QSpacerItem* verticalSpacer = new QSpacerItem(10, 100, QSizePolicy::Minimum, QSizePolicy::Expanding);
131 pLayout->addItem(verticalSpacer,row,0);
132 }
133}
134