36 #include <QtCore/QDebug>
37 #include <QtCore/QSettings>
38 #include <QHeaderView>
40 #include <QPushButton>
54 using namespace Core::Internal;
59 const UAVGadgetOptionsPageDecorator *gp1 =
60 qobject_cast<
const UAVGadgetOptionsPageDecorator *>(p1);
61 const UAVGadgetOptionsPageDecorator *gp2 =
62 qobject_cast<
const UAVGadgetOptionsPageDecorator *>(p2);
63 if (gp1 && (gp2 == NULL))
66 if (gp2 && (gp1 == NULL))
69 if (
const int cc = QString::localeAwareCompare(p1->trCategory(), p2->trCategory()))
72 return QString::localeAwareCompare(p1->trName(), p2->trName()) < 0;
78 ExtensionSystem::PluginManager::instance()->getObjects<IOptionsPage>();
83 SettingsDialog::SettingsDialog(QWidget *parent,
const QString &categoryId,
const QString &pageId)
91 setWindowTitle(tr(
"Preferences"));
93 setWindowTitle(tr(
"Options"));
95 QString initialCategory = categoryId;
96 QString initialPage = pageId;
97 qDebug() <<
"SettingsDialog constructor initial category: " << initialCategory
98 <<
", initial page: " << initialPage;
99 if (initialCategory.isEmpty() && initialPage.isEmpty()) {
102 settings->value(
"General/LastPreferenceCategory", QVariant(QString())).toString();
103 initialPage = settings->value(
"General/LastPreferencePage", QVariant(QString())).toString();
104 qDebug() <<
"SettingsDialog settings initial category: " << initialCategory
105 <<
", initial page: " << initialPage;
106 m_windowWidth = settings->value(
"General/SettingsWindowWidth", 0).toInt();
107 m_windowHeight = settings->value(
"General/SettingsWindowHeight", 0).toInt();
109 if (m_windowWidth > 0 && m_windowHeight > 0)
110 resize(m_windowWidth, m_windowHeight);
112 buttonBox->button(QDialogButtonBox::Ok)->setDefault(
true);
114 connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()),
this, SLOT(apply()));
123 SLOT(categoryItemSelectedShowChildInstead()), Qt::QueuedConnection);
125 splitter->setCollapsible(0,
false);
126 splitter->setCollapsible(1,
false);
127 pageTree->header()->setVisible(
false);
130 connect(pageTree, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
this,
131 SLOT(pageSelected()));
133 QMap<QString, QTreeWidgetItem *> categories;
138 bool firstUavGadgetOptionsPageFound =
false;
139 QTreeWidgetItem *initialItem =
nullptr;
142 pageData.index = index;
143 pageData.category = page->
category();
144 pageData.id = page->
id();
146 QTreeWidgetItem *item =
new QTreeWidgetItem;
147 item->setText(0, page->
trName());
148 item->setData(0, Qt::UserRole, qVariantFromValue(pageData));
151 QString currentCategory = page->
category();
153 QTreeWidgetItem *categoryItem;
154 if (!categories.contains(currentCategory)) {
156 if (!firstUavGadgetOptionsPageFound) {
160 firstUavGadgetOptionsPageFound =
true;
161 QTreeWidgetItem *separator =
new QTreeWidgetItem(pageTree);
162 separator->setFlags(separator->flags() & ~Qt::ItemIsSelectable
163 & ~Qt::ItemIsEnabled);
164 separator->setText(0, QString(30, 0xB7));
167 categoryItem =
new QTreeWidgetItem(pageTree);
168 categoryItem->setIcon(0, page->
icon());
169 categoryItem->setText(0, trCategories);
170 categoryItem->setData(0, Qt::UserRole, qVariantFromValue(pageData));
171 categories.insert(currentCategory, categoryItem);
175 if (!categoryItemList) {
177 m_categoryItemsMap.insert(currentCategory, categoryItemList);
179 categoryItemList->append(item);
181 m_pages.append(page);
186 QLabel *placeholderWidget =
new QLabel(stackedPages);
187 stackedPages->addWidget(placeholderWidget);
189 if (page->
id() == initialPage && currentCategory == initialCategory) {
196 foreach (QString category, m_categoryItemsMap.keys()) {
198 if (categoryItemList->size() > 1) {
199 foreach (QTreeWidgetItem *item, *categoryItemList) {
200 QTreeWidgetItem *categoryItem = categories.value(category);
201 categoryItem->addChild(item);
207 if (!initialItem->parent()) {
210 initialItem = categories.value(initialCategory);
212 pageTree->setCurrentItem(initialItem);
217 splitter->setSizes(sizes);
219 splitter->setStretchFactor(splitter->indexOf(pageTree), 0);
220 splitter->setStretchFactor(splitter->indexOf(layoutWidget), 1);
225 foreach (QString category, m_categoryItemsMap.keys()) {
227 delete categoryItemList;
230 for (
int i = 0;
i < stackedPages->count();
i++) {
231 QLabel *widget =
dynamic_cast<QLabel *
>(stackedPages->widget(
i));
238 void SettingsDialog::pageSelected()
240 QTreeWidgetItem *item = pageTree->currentItem();
244 PageData
data = item->data(0, Qt::UserRole).value<PageData>();
245 int index = data.index;
246 m_currentCategory = data.category;
247 m_currentPage = data.id;
249 QWidget *widget =
dynamic_cast<QLabel *
>(stackedPages->widget(index));
252 stackedPages->removeWidget(widget);
256 stackedPages->insertWidget(index, page->
createPage(stackedPages));
258 stackedPages->setCurrentIndex(index);
261 if ((pageTree->indexOfTopLevelItem(item) >= 0) && (item->childCount() > 0)) {
266 void SettingsDialog::categoryItemSelectedShowChildInstead()
268 QTreeWidgetItem *item = pageTree->currentItem();
269 item->setExpanded(
true);
270 pageTree->setCurrentItem(item->child(0), 0, QItemSelectionModel::SelectCurrent);
275 QTreeWidgetItem *item = pageTree->currentItem();
276 PageData data = item->data(0, Qt::UserRole).value<PageData>();
277 QString category = data.category;
279 QTreeWidgetItem *parentItem = item->parent();
280 parentItem->removeChild(item);
281 categoryItemList->removeOne(item);
282 if (parentItem->childCount() == 1) {
283 parentItem->removeChild(parentItem->child(0));
291 pageData.index = m_pages.count();
292 pageData.category = page->
category();
293 pageData.id = page->
id();
295 QTreeWidgetItem *categoryItem =
nullptr;
296 for (
int i = 0;
i < pageTree->topLevelItemCount(); ++
i) {
297 QTreeWidgetItem *tw = pageTree->topLevelItem(
i);
298 PageData data = tw->data(0, Qt::UserRole).value<PageData>();
299 if (data.category == page->
category()) {
310 if (categoryItem->childCount() == 0) {
311 QTreeWidgetItem *defaultItem = categoryItemList->at(0);
312 categoryItem->addChild(defaultItem);
315 QTreeWidgetItem *item =
new QTreeWidgetItem;
316 item->setText(0, page->
trName());
317 item->setData(0, Qt::UserRole, qVariantFromValue(pageData));
319 categoryItem->addChild(item);
320 categoryItemList->append(item);
322 m_pages.append(page);
323 stackedPages->addWidget(page->
createPage(stackedPages));
325 stackedPages->setCurrentIndex(stackedPages->count());
326 pageTree->setCurrentItem(item);
331 QTreeWidgetItem *item = pageTree->currentItem();
332 item->setText(0, text);
337 buttonBox->button(QDialogButtonBox::Apply)->setDisabled(disable);
338 buttonBox->button(QDialogButtonBox::Ok)->setDisabled(disable);
341 void SettingsDialog::accept()
344 for (
int i = 0;
i < m_pages.size();
i++) {
345 QWidget *widget =
dynamic_cast<QLabel *
>(stackedPages->widget(
i));
352 done(QDialog::Accepted);
355 void SettingsDialog::reject()
357 for (
int i = 0;
i < m_pages.size();
i++) {
358 QWidget *widget =
dynamic_cast<QLabel *
>(stackedPages->widget(
i));
360 IOptionsPage *page = m_pages.at(
i);
364 done(QDialog::Rejected);
367 void SettingsDialog::apply()
369 for (
int i = 0;
i < m_pages.size();
i++) {
370 QWidget *widget =
dynamic_cast<QLabel *
>(stackedPages->widget(
i));
372 IOptionsPage *page = m_pages.at(
i);
391 settings->setValue(
"General/LastPreferenceCategory", m_currentCategory);
392 settings->setValue(
"General/LastPreferencePage", m_currentPage);
393 settings->setValue(
"General/SettingsWindowWidth", this->width());
394 settings->setValue(
"General/SettingsWindowHeight", this->height());
virtual QString category() const
void disableApplyOk(bool disable)
void categoryItemSelected()
virtual QString id() const
void updateText(QString text)
Q_DECLARE_METATYPE(::PageData) using namespace Core
static ICore * instance()
virtual QString trName() const
bool optionsPageLessThan(const IOptionsPage *p1, const IOptionsPage *p2)
virtual QWidget * createPage(QWidget *parent)=0
virtual QSettings * settings(QSettings::Scope scope=QSettings::UserScope) const =0
Returns the application's main settings object.
virtual QString trCategory() const
void insertPage(IOptionsPage *page)
The IOptionsPage is an interface for providing options pages.
void settingsDialogShown(Core::Internal::SettingsDialog *)
void settingsDialogRemoved()
virtual UAVGadgetInstanceManager * uavGadgetInstanceManager() const =0