/************************************************************************** ** ** This file is part of Qt Creator ** ** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). ** ** Contact: http://www.qt-project.org/ ** ** ** GNU Lesser General Public License Usage ** ** 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. ** ** In addition, as a special exception, Nokia gives you certain additional ** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** Other Usage ** ** Alternatively, this file may be used in accordance with the terms and ** conditions contained in a signed written agreement between you and Nokia. ** ** **************************************************************************/ #include "environmentitemswidget.h" #include #include #include #include #include namespace ProjectExplorer { class EnvironmentItemsWidgetPrivate { public: QList cleanUp( const QList &items) const; TextEditor::BaseTextEditorWidget *m_editor; }; QList EnvironmentItemsWidgetPrivate::cleanUp( const QList &items) const { QList uniqueItems; QSet uniqueSet; for (int i = items.count() - 1; i >= 0; i--) { Utils::EnvironmentItem item = items.at(i); #if defined(Q_OS_WIN) item.name = item.name.toUpper(); #endif const QString &itemName = item.name; QString emptyName = itemName; emptyName.remove(QLatin1Char(' ')); if (!emptyName.isEmpty() && !uniqueSet.contains(itemName)) { uniqueItems.prepend(item); uniqueSet.insert(itemName); } } return uniqueItems; } EnvironmentItemsWidget::EnvironmentItemsWidget(QWidget *parent) : QWidget(parent), d(new EnvironmentItemsWidgetPrivate) { d->m_editor = new TextEditor::SnippetEditorWidget(this); TextEditor::TextEditorSettings *settings = TextEditor::TextEditorSettings::instance(); d->m_editor->setFontSettings(settings->fontSettings()); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(d->m_editor); } EnvironmentItemsWidget::~EnvironmentItemsWidget() { delete d; } void EnvironmentItemsWidget::setEnvironmentItems(const QList &items) { QList sortedItems = items; Utils::EnvironmentItem::sort(&sortedItems); QStringList list = Utils::EnvironmentItem::toStringList(sortedItems); d->m_editor->document()->setPlainText(list.join(QLatin1String("\n"))); } QList EnvironmentItemsWidget::environmentItems() const { const QStringList list = d->m_editor->document()->toPlainText().split(QLatin1String("\n")); QList items = Utils::EnvironmentItem::fromStringList(list); return d->cleanUp(items); } class EnvironmentItemsDialogPrivate { public: EnvironmentItemsWidget *m_editor; }; EnvironmentItemsDialog::EnvironmentItemsDialog(QWidget *parent) : QDialog(parent), d(new EnvironmentItemsDialogPrivate) { resize(640, 480); d->m_editor = new EnvironmentItemsWidget(this); QDialogButtonBox *box = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); connect(box, SIGNAL(accepted()), this, SLOT(accept())); connect(box, SIGNAL(rejected()), this, SLOT(reject())); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(d->m_editor); layout->addWidget(box); setWindowTitle(tr("Edit Environment")); } EnvironmentItemsDialog::~EnvironmentItemsDialog() { delete d; } void EnvironmentItemsDialog::setEnvironmentItems(const QList &items) { d->m_editor->setEnvironmentItems(items); } QList EnvironmentItemsDialog::environmentItems() const { return d->m_editor->environmentItems(); } QList EnvironmentItemsDialog::getEnvironmentItems(QWidget *parent, const QList &initial, bool *ok) { EnvironmentItemsDialog dlg(parent); dlg.setEnvironmentItems(initial); bool result = dlg.exec() == QDialog::Accepted; if (ok) *ok = result; if (result) return dlg.environmentItems(); return QList(); } } // namespace ProjectExplorer