1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "propertiesdialog.h" |
6 | #include "common/widget/pagewidget.h" |
7 | |
8 | #include <QtDebug> |
9 | #include <QtWidgets/QHBoxLayout> |
10 | #include <QtWidgets/QHeaderView> |
11 | #include <QtWidgets/QLabel> |
12 | #include <QtWidgets/QLineEdit> |
13 | #include <QtWidgets/QListView> |
14 | #include <QtWidgets/QTabWidget> |
15 | #include <QtWidgets/QVBoxLayout> |
16 | #include <QtWidgets/QSpacerItem> |
17 | #include <QtWidgets/QPushButton> |
18 | #include <QtWidgets/QStackedWidget> |
19 | #include <QStringListModel> |
20 | |
21 | PropertiesDialog::PropertiesDialog(QWidget *parent) |
22 | : QDialog(parent) |
23 | { |
24 | setupUi(this); |
25 | } |
26 | |
27 | bool PropertiesDialog::insertPropertyPanel(const QString &itemName, PageWidget *panel) |
28 | { |
29 | widgts.insert(itemName, panel); |
30 | leftBarModel->setStringList(leftBarModel->stringList() << itemName); |
31 | int index = stackWidget->count(); |
32 | stackWidget->insertWidget(index, panel); |
33 | leftBarValues.push_back(itemName); |
34 | |
35 | if (index >= 0 && !leftBarValues.isEmpty()) { |
36 | stackWidget->setCurrentIndex(0); |
37 | headTitle->setText(leftBarValues.at(0)); |
38 | } |
39 | |
40 | return true; |
41 | } |
42 | |
43 | void PropertiesDialog::showPropertyPanel(const QString &itemName, const QString &tabName) |
44 | { |
45 | Q_UNUSED(tabName) |
46 | |
47 | // Set leftbar item seleted. |
48 | int index = leftBarModel->stringList().indexOf(itemName); |
49 | if (index >= 0) { |
50 | leftSideBar->selectionModel()->select(leftBarModel->index(index), QItemSelectionModel::Select); |
51 | |
52 | // Update right panel. |
53 | auto widget = widgts.value(itemName); |
54 | stackWidget->setCurrentWidget(widget); |
55 | |
56 | headTitle->setText(itemName); |
57 | } |
58 | |
59 | this->exec(); |
60 | } |
61 | |
62 | void PropertiesDialog::slotLeftBarClicked(const QModelIndex &index) |
63 | { |
64 | qInfo() << "The selected item is :" << index.data().toString(); |
65 | QString itemName = index.data().toString(); |
66 | auto widget = widgts.value(itemName); |
67 | stackWidget->setCurrentWidget(widget); |
68 | |
69 | // Update head title. |
70 | headTitle->setText(itemName); |
71 | widget->readConfig(); |
72 | } |
73 | |
74 | void PropertiesDialog::slotFilterText(const QString &text) |
75 | { |
76 | QString filterText = text.trimmed(); |
77 | if (filterText.isEmpty()) { |
78 | leftBarModel->setStringList(leftBarValues); |
79 | return; |
80 | } |
81 | |
82 | QStringList tempList; |
83 | foreach (auto str, leftBarValues) { |
84 | QString upperStr = str.toUpper(); |
85 | QString uppprText = text.toUpper(); |
86 | if (upperStr.contains(uppprText)) { |
87 | tempList.push_back(str); |
88 | } |
89 | } |
90 | |
91 | leftBarModel->setStringList(tempList); |
92 | } |
93 | |
94 | void PropertiesDialog::setupUi(QDialog *Dialog) |
95 | { |
96 | Dialog->resize(1000, 650); |
97 | setWindowTitle(tr("Project Properties" )); |
98 | |
99 | // Center layout. |
100 | auto mainLayout = new QHBoxLayout(Dialog); |
101 | mainLayout->setSpacing(6); |
102 | mainLayout->setContentsMargins(11, 11, 11, 11); |
103 | |
104 | // Left layout. |
105 | auto leftLayout = new QVBoxLayout(Dialog); |
106 | leftLayout->setSpacing(6); |
107 | filterEdit = new QLineEdit(Dialog); |
108 | filterEdit->setPlaceholderText(tr("Filter" )); |
109 | connect(filterEdit, SIGNAL(textChanged(const QString &)), |
110 | this, SLOT(slotFilterText(const QString &))); |
111 | leftLayout->addWidget(filterEdit); |
112 | |
113 | leftSideBar = new QListView(Dialog); |
114 | leftSideBar->setEditTriggers(QAbstractItemView::NoEditTriggers); |
115 | leftBarModel = new QStringListModel(leftSideBar); |
116 | leftSideBar->setModel(leftBarModel); |
117 | connect(leftSideBar, SIGNAL(clicked(const QModelIndex &)), |
118 | this, SLOT(slotLeftBarClicked(const QModelIndex &))); |
119 | |
120 | leftLayout->addWidget(leftSideBar); |
121 | |
122 | // Right layout. |
123 | auto rightLayout = new QVBoxLayout(Dialog); |
124 | rightLayout->setSpacing(6); |
125 | headTitle = new QLabel(Dialog); |
126 | |
127 | rightLayout->addWidget(headTitle); |
128 | |
129 | stackWidget = new QStackedWidget(Dialog); |
130 | rightLayout->addWidget(stackWidget); |
131 | |
132 | auto buttonLayout = new QHBoxLayout(Dialog); |
133 | buttonLayout->setSpacing(6); |
134 | auto horizontalSpacer = new QSpacerItem(40, 30, QSizePolicy::Expanding, QSizePolicy::Minimum); |
135 | |
136 | buttonLayout->addItem(horizontalSpacer); |
137 | |
138 | auto okBtn = new QPushButton(tr("OK" ), Dialog); |
139 | connect(okBtn, SIGNAL(clicked()), this, SLOT(saveAllConfig())); |
140 | |
141 | auto cancelBtn = new QPushButton(tr("Cancel" ), Dialog); |
142 | connect(cancelBtn, &QPushButton::clicked, [this] { |
143 | // TODO(Mozart) |
144 | this->close(); |
145 | }); |
146 | auto applyBtn = new QPushButton(tr("Apply" ), Dialog); |
147 | connect(applyBtn, SIGNAL(clicked()), this, SLOT(saveSingleConfig())); |
148 | |
149 | buttonLayout->addWidget(okBtn); |
150 | buttonLayout->addWidget(cancelBtn); |
151 | buttonLayout->addWidget(applyBtn); |
152 | |
153 | rightLayout->addLayout(buttonLayout); |
154 | |
155 | // Insert left & right layout to main layout. |
156 | mainLayout->addLayout(leftLayout); |
157 | mainLayout->addLayout(rightLayout); |
158 | |
159 | mainLayout->setStretch(0, 1); |
160 | mainLayout->setStretch(1, 4); |
161 | } |
162 | |
163 | void PropertiesDialog::saveAllConfig() |
164 | { |
165 | for (int index = 0; index < stackWidget->count(); index++) |
166 | { |
167 | PageWidget* widget = dynamic_cast<PageWidget*>(stackWidget->widget(index)); |
168 | if (widget) { |
169 | widget->saveConfig(); |
170 | } |
171 | } |
172 | |
173 | accept(); |
174 | } |
175 | |
176 | void PropertiesDialog::saveSingleConfig() |
177 | { |
178 | int index = stackWidget->currentIndex(); |
179 | if (index >= 0 && index < stackWidget->count()) |
180 | { |
181 | PageWidget* widget = dynamic_cast<PageWidget*>(stackWidget->widget(index)); |
182 | if (widget) { |
183 | widget->saveConfig(); |
184 | } |
185 | } |
186 | } |
187 | |
188 | void PropertiesDialog::readConfig() |
189 | { |
190 | for (int index = 0; index < stackWidget->count(); index++) |
191 | { |
192 | PageWidget* widget = dynamic_cast<PageWidget*>(stackWidget->widget(index)); |
193 | if (widget) { |
194 | widget->readConfig(); |
195 | } |
196 | } |
197 | } |
198 | |