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