Commit 125aaf86 authored by Leandro Melo's avatar Leandro Melo
Browse files

Settings page for the generic highlighter.

parent 154b312d
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (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 http://qt.nokia.com/contact.
**
**************************************************************************/
#include "highlightersettings.h"
#include <coreplugin/icore.h>
#include <QtCore/QSettings>
#include <QtCore/QString>
#include <QtCore/QLatin1String>
#ifdef Q_OS_LINUX
#include <QtCore/QDir>
#include <QtCore/QProcess>
#endif
namespace TextEditor {
namespace Internal {
void applyDefaults(HighlighterSettings *settings)
{
settings->m_definitionFilesPath.clear();
#ifdef Q_OS_LINUX
static const QLatin1String kateSyntax("/share/apps/katepart/syntax");
// Wild guess.
QDir dir(QLatin1String("/usr") + kateSyntax);
if (dir.exists()) {
settings->m_definitionFilesPath = dir.path();
} else {
// Try kde-config.
QProcess process;
process.start(QLatin1String("kde-config"), QStringList(QLatin1String("--prefix")));
if (process.waitForStarted(5000)) {
process.waitForFinished(5000);
QString output = QString::fromLocal8Bit(process.readAllStandardOutput());
output.remove(QLatin1Char('\n'));
dir.setPath(output + kateSyntax);
if (dir.exists())
settings->m_definitionFilesPath = dir.path();
}
}
#endif
if (settings->m_definitionFilesPath.isEmpty())
settings->m_definitionFilesPath = Core::ICore::instance()->resourcePath() +
QLatin1String("/generic-highlighter");
}
} // namespace Internal
} // namespace TextEditor
namespace {
static const QLatin1String kDefinitionFilesPath("DefinitionFilesPath");
static const QLatin1String kGroupPostfix("HighlighterSettings");
QString groupSpecifier(const QString &postFix, const QString &category)
{
if (category.isEmpty())
return postFix;
return QString(category + postFix);
}
} // namespace anonymous
using namespace TextEditor;
using namespace Internal;
HighlighterSettings::HighlighterSettings()
{}
void HighlighterSettings::toSettings(const QString &category, QSettings *s) const
{
const QString &group = groupSpecifier(kGroupPostfix, category);
s->beginGroup(group);
s->setValue(kDefinitionFilesPath, m_definitionFilesPath);
s->endGroup();
}
void HighlighterSettings::fromSettings(const QString &category, QSettings *s)
{
const QString &group = groupSpecifier(kGroupPostfix, category);
s->beginGroup(group);
if (!s->contains(kDefinitionFilesPath))
applyDefaults(this);
else
m_definitionFilesPath = s->value(kDefinitionFilesPath, QString()).toString();
s->endGroup();
}
bool HighlighterSettings::equals(const HighlighterSettings &highlighterSettings) const
{
return m_definitionFilesPath == highlighterSettings.m_definitionFilesPath;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (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 http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef HIGHLIGHTERSETTINGS_H
#define HIGHLIGHTERSETTINGS_H
#include <QtCore/QString>
QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace TextEditor {
struct HighlighterSettings
{
HighlighterSettings();
void toSettings(const QString &category, QSettings *s) const;
void fromSettings(const QString &category, QSettings *s);
bool equals(const HighlighterSettings &highlighterSettings) const;
QString m_definitionFilesPath;
};
inline bool operator==(const HighlighterSettings &a, const HighlighterSettings &b)
{ return a.equals(b); }
inline bool operator!=(const HighlighterSettings &a, const HighlighterSettings &b)
{ return !a.equals(b); }
namespace Internal {
void applyDefaults(HighlighterSettings *settings);
}
} // namespace TextEditor
#endif // HIGHLIGHTERSETTINGS_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (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 http://qt.nokia.com/contact.
**
**************************************************************************/
#include "highlightersettingspage.h"
#include "highlightersettings.h"
#include "manager.h"
#include "ui_highlightersettingspage.h"
#include <coreplugin/icore.h>
using namespace TextEditor;
using namespace Internal;
struct HighlighterSettingsPage::HighlighterSettingsPagePrivate
{
explicit HighlighterSettingsPagePrivate(const QString &id);
const QString m_id;
const QString m_displayName;
const QString m_settingsPrefix;
QString m_searchKeywords;
HighlighterSettings m_settings;
Ui::HighlighterSettingsPage m_page;
};
HighlighterSettingsPage::HighlighterSettingsPagePrivate::
HighlighterSettingsPagePrivate(const QString &id) :
m_id(id),
m_displayName(tr("Generic Highlighter")),
m_settingsPrefix(QLatin1String("text"))
{}
HighlighterSettingsPage::HighlighterSettingsPage(const QString &id, QObject *parent) :
TextEditorOptionsPage(parent),
m_d(new HighlighterSettingsPagePrivate(id))
{
if (QSettings *s = Core::ICore::instance()->settings())
m_d->m_settings.fromSettings(m_d->m_settingsPrefix, s);
connect(this, SIGNAL(definitionsLocationChanged()),
Manager::instance(), SLOT(registerMimeTypes()));
}
HighlighterSettingsPage::~HighlighterSettingsPage()
{
delete m_d;
}
QString HighlighterSettingsPage::id() const
{
return m_d->m_id;
}
QString HighlighterSettingsPage::displayName() const
{
return m_d->m_displayName;
}
QWidget *HighlighterSettingsPage::createPage(QWidget *parent)
{
QWidget *w = new QWidget(parent);
m_d->m_page.setupUi(w);
settingsToUI();
if (m_d->m_searchKeywords.isEmpty()) {
QTextStream(&m_d->m_searchKeywords) << m_d->m_page.definitionFilesGroupBox->title()
<< m_d->m_page.locationLabel->text();
}
connect(m_d->m_page.resetButton, SIGNAL(clicked()), this, SLOT(reset()));
return w;
}
void HighlighterSettingsPage::apply()
{
if (settingsChanged())
settingsFromUI();
}
bool HighlighterSettingsPage::matches(const QString &s) const
{
return m_d->m_searchKeywords.contains(s, Qt::CaseInsensitive);
}
const HighlighterSettings &HighlighterSettingsPage::highlighterSettings() const
{
return m_d->m_settings;
}
void HighlighterSettingsPage::settingsFromUI()
{
bool locationChanged = false;
if (m_d->m_settings.m_definitionFilesPath != m_d->m_page.definitionFilesPath->path())
locationChanged = true;
m_d->m_settings.m_definitionFilesPath = m_d->m_page.definitionFilesPath->path();
if (QSettings *s = Core::ICore::instance()->settings())
m_d->m_settings.toSettings(m_d->m_settingsPrefix, s);
if (locationChanged)
emit definitionsLocationChanged();
}
void HighlighterSettingsPage::settingsToUI(const HighlighterSettings *settings)
{
if (settings) {
m_d->m_page.definitionFilesPath->setPath(settings->m_definitionFilesPath);
} else {
m_d->m_page.definitionFilesPath->setPath(m_d->m_settings.m_definitionFilesPath);
}
}
void HighlighterSettingsPage::reset()
{
HighlighterSettings defaultSettings;
applyDefaults(&defaultSettings);
settingsToUI(&defaultSettings);
}
bool HighlighterSettingsPage::settingsChanged() const
{
if (m_d->m_settings.m_definitionFilesPath != m_d->m_page.definitionFilesPath->path())
return true;
return false;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (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 http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef HIGHLIGHTERSETTINGSPAGE_H
#define HIGHLIGHTERSETTINGSPAGE_H
#include "texteditoroptionspage.h"
namespace TextEditor {
struct HighlighterSettings;
class HighlighterSettingsPage : public TextEditorOptionsPage
{
Q_OBJECT
public:
HighlighterSettingsPage(const QString &id, QObject *parent);
virtual ~HighlighterSettingsPage();
QString id() const;
QString displayName() const;
QWidget *createPage(QWidget *parent);
void apply();
void finish() {}
bool matches(const QString &s) const;
const HighlighterSettings &highlighterSettings() const;
signals:
void definitionsLocationChanged();
private slots:
void reset();
private:
void settingsFromUI();
void settingsToUI(const HighlighterSettings *settings = 0);
bool settingsChanged() const;
struct HighlighterSettingsPagePrivate;
HighlighterSettingsPagePrivate *m_d;
};
} // namespace TextEditor
#endif // HIGHLIGHTERSETTINGSPAGE_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>HighlighterSettingsPage</class>
<widget class="QWidget" name="HighlighterSettingsPage">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>453</width>
<height>230</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="definitionFilesGroupBox">
<property name="title">
<string>Syntax Definition Files</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="locationLabel">
<property name="text">
<string>Location:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="Utils::PathChooser" name="definitionFilesPath" native="true">
<zorder>locationLabel</zorder>
</widget>
</item>
<item>
<widget class="QToolButton" name="resetButton">
<property name="toolTip">
<string>Reset to default</string>
</property>
<property name="text">
<string>R</string>
</property>
<property name="icon">
<iconset resource="../../coreplugin/core.qrc">
<normaloff>:/core/images/reset.png</normaloff>:/core/images/reset.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
<zorder>locationLabel</zorder>
<zorder>definitionFilesPath</zorder>
<zorder>resetButton</zorder>
<zorder>resetButton</zorder>
<zorder>resetButton</zorder>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>146</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Utils::PathChooser</class>
<extends>QWidget</extends>
<header location="global">utils/pathchooser.h</header>
<container>1</container>
<slots>
<signal>editingFinished()</signal>
<signal>browsingFinished()</signal>
</slots>
</customwidget>
</customwidgets>
<resources>
<include location="../../coreplugin/core.qrc"/>
</resources>
<connections/>
</ui>
......@@ -34,6 +34,7 @@
#include "texteditorplugin.h"
#include "texteditorsettings.h"
#include "plaintexteditorfactory.h"
#include "highlightersettings.h"
#include <coreplugin/icore.h>
#include <utils/qtcassert.h>
......@@ -133,6 +134,7 @@ bool Manager::isBuildingDefinition(const QString &id) const
void Manager::registerMimeTypes()
{
clear();
QFuture<Core::MimeType> future =
QtConcurrent::run(&Manager::gatherDefinitionsMimeTypes, this);
m_watcher.setFuture(future);
......@@ -142,8 +144,8 @@ void Manager::registerMimeTypes()
void Manager::gatherDefinitionsMimeTypes(QFutureInterface<Core::MimeType> &future)
{
QDir definitionsDir(Core::ICore::instance()->resourcePath() +
QLatin1String("/generic-highlighter"));
const HighlighterSettings &settings = TextEditorSettings::instance()->highlighterSettings();
QDir definitionsDir(settings.m_definitionFilesPath);
QStringList filter(QLatin1String("*.xml"));
definitionsDir.setNameFilters(filter);
......@@ -247,3 +249,11 @@ void Manager::parseDefinitionMetadata(const QFileInfo &fileInfo,
reader.clear();
definitionFile.close();
}
void Manager::clear()
{
m_priorityComp.m_priorityById.clear();
m_idByName.clear();
m_idByMimeType.clear();
m_definitions.clear();
}
......@@ -42,6 +42,7 @@
QT_BEGIN_NAMESPACE
class QFileInfo;
class QStringList;
class QDir;
template <class> class QFutureInterface;
QT_END_NAMESPACE
......@@ -63,14 +64,17 @@ public:
bool isBuildingDefinition(const QString &id) const;
const QSharedPointer<HighlightDefinition> &definition(const QString &id);
private slots:
public slots:
void registerMimeTypes();
private slots:
void registerMimeType(int index) const;
private:
Manager();
Q_DISABLE_COPY(Manager)
void clear();
void gatherDefinitionsMimeTypes(QFutureInterface<Core::MimeType> &future);
void parseDefinitionMetadata(const QFileInfo &fileInfo,
QString *comment,
......
......@@ -52,7 +52,9 @@ SOURCES += texteditorplugin.cpp \
generichighlighter/highlightdefinition.cpp \
generichighlighter/highlighter.cpp \
generichighlighter/manager.cpp \
generichighlighter/highlightdefinitionhandler.cpp
generichighlighter/highlightdefinitionhandler.cpp \
generichighlighter/highlightersettingspage.cpp \
generichighlighter/highlightersettings.cpp
HEADERS += texteditorplugin.h \
textfilewizard.h \
......@@ -107,12 +109,15 @@ HEADERS += texteditorplugin.h \
generichighlighter/highlightdefinition.h \
generichighlighter/highlighter.h \
generichighlighter/manager.h \