Files
qt-creator/src/plugins/coreplugin/dialogs/settingsdialog.cpp

145 lines
4.7 KiB
C++

/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/
#include "settingsdialog.h"
#include <extensionsystem/pluginmanager.h>
#include <QtGui/QHeaderView>
#include <QtGui/QPushButton>
using namespace Core;
using namespace Core::Internal;
SettingsDialog::SettingsDialog(QWidget *parent, const QString &initialCategory,
const QString &initialPage)
: QDialog(parent)
{
setupUi(this);
buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply()));
splitter->setCollapsible(1, false);
pageTree->header()->setVisible(false);
connect(pageTree, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
this, SLOT(pageSelected(QTreeWidgetItem *)));
QMap<QString, QTreeWidgetItem *> categories;
QList<IOptionsPage*> pages =
ExtensionSystem::PluginManager::instance()->getObjects<IOptionsPage>();
int index = 0;
foreach (IOptionsPage *page, pages) {
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, page->name());
item->setData(0, Qt::UserRole, index);
QStringList categoriesId = page->category().split(QLatin1Char('|'));
QStringList trCategories = page->trCategory().split(QLatin1Char('|'));
QString currentCategory = categoriesId.at(0);
QTreeWidgetItem *treeitem;
if (!categories.contains(currentCategory)) {
treeitem = new QTreeWidgetItem(pageTree);
treeitem->setText(0, trCategories.at(0));
treeitem->setData(0, Qt::UserRole, index);
categories.insert(currentCategory, treeitem);
}
int catCount = 1;
while (catCount < categoriesId.count()) {
if (!categories.contains(currentCategory + QLatin1Char('|') + categoriesId.at(catCount))) {
treeitem = new QTreeWidgetItem(categories.value(currentCategory));
currentCategory += QLatin1Char('|') + categoriesId.at(catCount);
treeitem->setText(0, trCategories.at(catCount));
treeitem->setData(0, Qt::UserRole, index);
categories.insert(currentCategory, treeitem);
} else {
currentCategory += QLatin1Char('|') + categoriesId.at(catCount);
}
++catCount;
}
categories.value(currentCategory)->addChild(item);
m_pages.append(page);
stackedPages->addWidget(page->createPage(stackedPages));
if (page->name() == initialPage && currentCategory == initialCategory) {
stackedPages->setCurrentIndex(stackedPages->count());
pageTree->setCurrentItem(item);
}
index++;
}
QList<int> sizes;
sizes << 150 << 300;
splitter->setSizes(sizes);
splitter->setStretchFactor(splitter->indexOf(pageTree), 0);
splitter->setStretchFactor(splitter->indexOf(layoutWidget), 1);
}
SettingsDialog::~SettingsDialog()
{
}
void SettingsDialog::pageSelected(QTreeWidgetItem *)
{
QTreeWidgetItem *item = pageTree->currentItem();
int index = item->data(0, Qt::UserRole).toInt();
stackedPages->setCurrentIndex(index);
}
void SettingsDialog::accept()
{
foreach (IOptionsPage *page, m_pages) {
page->apply();
page->finish();
}
done(QDialog::Accepted);
}
void SettingsDialog::reject()
{
foreach (IOptionsPage *page, m_pages)
page->finish();
done(QDialog::Rejected);
}
void SettingsDialog::apply()
{
foreach (IOptionsPage *page, m_pages)
page->apply();
}