2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2009-03-18 16:43:01 +01:00
|
|
|
|
|
|
|
|
#include "cppfilesettingspage.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
#include "cppeditorplugin.h"
|
2023-01-11 20:43:10 +01:00
|
|
|
#include "cppeditortr.h"
|
2009-03-18 16:43:01 +01:00
|
|
|
|
2018-01-02 17:13:57 +01:00
|
|
|
#include <app/app_version.h>
|
|
|
|
|
|
2009-03-18 16:43:01 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2009-06-22 16:22:15 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
|
2015-01-10 23:40:32 +02:00
|
|
|
#include <utils/fileutils.h>
|
2019-02-20 12:28:52 +01:00
|
|
|
#include <utils/hostosinfo.h>
|
2022-11-17 13:54:37 +01:00
|
|
|
#include <utils/layoutbuilder.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.h>
|
2022-11-17 13:54:37 +01:00
|
|
|
#include <utils/pathchooser.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
|
2022-11-17 13:54:37 +01:00
|
|
|
#include <QCheckBox>
|
|
|
|
|
#include <QComboBox>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QCoreApplication>
|
2020-06-17 06:35:31 +02:00
|
|
|
#include <QFile>
|
2022-11-17 13:54:37 +01:00
|
|
|
#include <QLineEdit>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QLocale>
|
2020-06-17 06:35:31 +02:00
|
|
|
#include <QSettings>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextStream>
|
2009-03-18 16:43:01 +01:00
|
|
|
|
2021-08-17 16:36:42 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
namespace CppEditor::Internal {
|
2020-02-04 16:24:37 +01:00
|
|
|
|
|
|
|
|
const char headerPrefixesKeyC[] = "HeaderPrefixes";
|
|
|
|
|
const char sourcePrefixesKeyC[] = "SourcePrefixes";
|
|
|
|
|
const char headerSuffixKeyC[] = "HeaderSuffix";
|
|
|
|
|
const char sourceSuffixKeyC[] = "SourceSuffix";
|
|
|
|
|
const char headerSearchPathsKeyC[] = "HeaderSearchPaths";
|
|
|
|
|
const char sourceSearchPathsKeyC[] = "SourceSearchPaths";
|
|
|
|
|
const char headerPragmaOnceC[] = "HeaderPragmaOnce";
|
|
|
|
|
const char licenseTemplatePathKeyC[] = "LicenseTemplate";
|
2009-06-22 16:22:15 +02:00
|
|
|
|
2023-02-10 16:15:49 +01:00
|
|
|
const char *licenseTemplateTemplate = QT_TRANSLATE_NOOP("QtC::CppEditor",
|
2009-06-22 16:22:15 +02:00
|
|
|
"/**************************************************************************\n"
|
2018-01-02 17:13:57 +01:00
|
|
|
"** %1 license header template\n"
|
2009-06-22 16:22:15 +02:00
|
|
|
"** Special keywords: %USER% %DATE% %YEAR%\n"
|
|
|
|
|
"** Environment variables: %$VARIABLE%\n"
|
|
|
|
|
"** To protect a percent sign, use '%%'.\n"
|
|
|
|
|
"**************************************************************************/\n");
|
2009-03-18 16:43:01 +01:00
|
|
|
|
|
|
|
|
void CppFileSettings::toSettings(QSettings *s) const
|
|
|
|
|
{
|
2021-01-20 17:39:04 +01:00
|
|
|
const CppFileSettings def;
|
2021-08-30 10:58:08 +02:00
|
|
|
s->beginGroup(Constants::CPPEDITOR_SETTINGSGROUP);
|
2021-01-20 17:39:04 +01:00
|
|
|
QtcSettings::setValueWithDefault(s, headerPrefixesKeyC, headerPrefixes, def.headerPrefixes);
|
|
|
|
|
QtcSettings::setValueWithDefault(s, sourcePrefixesKeyC, sourcePrefixes, def.sourcePrefixes);
|
|
|
|
|
QtcSettings::setValueWithDefault(s, headerSuffixKeyC, headerSuffix, def.headerSuffix);
|
|
|
|
|
QtcSettings::setValueWithDefault(s, sourceSuffixKeyC, sourceSuffix, def.sourceSuffix);
|
|
|
|
|
QtcSettings::setValueWithDefault(s,
|
|
|
|
|
headerSearchPathsKeyC,
|
|
|
|
|
headerSearchPaths,
|
|
|
|
|
def.headerSearchPaths);
|
|
|
|
|
QtcSettings::setValueWithDefault(s,
|
|
|
|
|
sourceSearchPathsKeyC,
|
|
|
|
|
sourceSearchPaths,
|
|
|
|
|
def.sourceSearchPaths);
|
|
|
|
|
QtcSettings::setValueWithDefault(s,
|
|
|
|
|
Constants::LOWERCASE_CPPFILES_KEY,
|
|
|
|
|
lowerCaseFiles,
|
|
|
|
|
def.lowerCaseFiles);
|
|
|
|
|
QtcSettings::setValueWithDefault(s, headerPragmaOnceC, headerPragmaOnce, def.headerPragmaOnce);
|
|
|
|
|
QtcSettings::setValueWithDefault(s,
|
|
|
|
|
licenseTemplatePathKeyC,
|
|
|
|
|
licenseTemplatePath,
|
|
|
|
|
def.licenseTemplatePath);
|
2009-03-18 16:43:01 +01:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFileSettings::fromSettings(QSettings *s)
|
|
|
|
|
{
|
2021-01-20 17:39:04 +01:00
|
|
|
const CppFileSettings def;
|
2021-08-30 10:58:08 +02:00
|
|
|
s->beginGroup(Constants::CPPEDITOR_SETTINGSGROUP);
|
2021-01-20 17:39:04 +01:00
|
|
|
headerPrefixes = s->value(headerPrefixesKeyC, def.headerPrefixes).toStringList();
|
|
|
|
|
sourcePrefixes = s->value(sourcePrefixesKeyC, def.sourcePrefixes).toStringList();
|
|
|
|
|
headerSuffix = s->value(headerSuffixKeyC, def.headerSuffix).toString();
|
|
|
|
|
sourceSuffix = s->value(sourceSuffixKeyC, def.sourceSuffix).toString();
|
|
|
|
|
headerSearchPaths = s->value(headerSearchPathsKeyC, def.headerSearchPaths).toStringList();
|
|
|
|
|
sourceSearchPaths = s->value(sourceSearchPathsKeyC, def.sourceSearchPaths).toStringList();
|
|
|
|
|
lowerCaseFiles = s->value(Constants::LOWERCASE_CPPFILES_KEY, def.lowerCaseFiles).toBool();
|
|
|
|
|
headerPragmaOnce = s->value(headerPragmaOnceC, def.headerPragmaOnce).toBool();
|
|
|
|
|
licenseTemplatePath = s->value(licenseTemplatePathKeyC, def.licenseTemplatePath).toString();
|
2009-03-18 16:43:01 +01:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 09:04:59 +02:00
|
|
|
bool CppFileSettings::applySuffixesToMimeDB()
|
2009-03-18 16:43:01 +01:00
|
|
|
{
|
2015-02-04 09:32:46 +01:00
|
|
|
Utils::MimeType mt;
|
2021-08-30 10:58:08 +02:00
|
|
|
mt = Utils::mimeTypeForName(QLatin1String(Constants::CPP_SOURCE_MIMETYPE));
|
2015-02-04 09:32:46 +01:00
|
|
|
if (!mt.isValid())
|
|
|
|
|
return false;
|
|
|
|
|
mt.setPreferredSuffix(sourceSuffix);
|
2021-08-30 10:58:08 +02:00
|
|
|
mt = Utils::mimeTypeForName(QLatin1String(Constants::CPP_HEADER_MIMETYPE));
|
2015-02-04 09:32:46 +01:00
|
|
|
if (!mt.isValid())
|
|
|
|
|
return false;
|
|
|
|
|
mt.setPreferredSuffix(headerSuffix);
|
|
|
|
|
return true;
|
2009-03-18 16:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppFileSettings::equals(const CppFileSettings &rhs) const
|
|
|
|
|
{
|
|
|
|
|
return lowerCaseFiles == rhs.lowerCaseFiles
|
2019-01-03 10:56:36 +01:00
|
|
|
&& headerPragmaOnce == rhs.headerPragmaOnce
|
2014-01-20 09:15:50 +02:00
|
|
|
&& headerPrefixes == rhs.headerPrefixes
|
|
|
|
|
&& sourcePrefixes == rhs.sourcePrefixes
|
2009-03-18 16:43:01 +01:00
|
|
|
&& headerSuffix == rhs.headerSuffix
|
2009-06-22 16:22:15 +02:00
|
|
|
&& sourceSuffix == rhs.sourceSuffix
|
2013-08-25 22:42:37 +03:00
|
|
|
&& headerSearchPaths == rhs.headerSearchPaths
|
|
|
|
|
&& sourceSearchPaths == rhs.sourceSearchPaths
|
2009-06-22 16:22:15 +02:00
|
|
|
&& licenseTemplatePath == rhs.licenseTemplatePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replacements of special license template keywords.
|
2010-08-09 12:23:21 +02:00
|
|
|
static bool keyWordReplacement(const QString &keyWord,
|
|
|
|
|
QString *value)
|
2009-06-22 16:22:15 +02:00
|
|
|
{
|
|
|
|
|
if (keyWord == QLatin1String("%YEAR%")) {
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QLatin1String("%{CurrentDate:yyyy}");
|
2010-08-09 12:23:21 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-10-28 12:13:47 +02:00
|
|
|
if (keyWord == QLatin1String("%MONTH%")) {
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QLatin1String("%{CurrentDate:M}");
|
2010-10-28 12:13:47 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (keyWord == QLatin1String("%DAY%")) {
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QLatin1String("%{CurrentDate:d}");
|
2010-10-28 12:13:47 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-08-09 12:23:21 +02:00
|
|
|
if (keyWord == QLatin1String("%CLASS%")) {
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QLatin1String("%{Cpp:License:ClassName}");
|
2010-08-09 12:23:21 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (keyWord == QLatin1String("%FILENAME%")) {
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QLatin1String("%{Cpp:License:FileName}");
|
2010-08-09 12:23:21 +02:00
|
|
|
return true;
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
if (keyWord == QLatin1String("%DATE%")) {
|
|
|
|
|
static QString format;
|
|
|
|
|
// ensure a format with 4 year digits. Some have locales have 2.
|
|
|
|
|
if (format.isEmpty()) {
|
|
|
|
|
QLocale loc;
|
|
|
|
|
format = loc.dateFormat(QLocale::ShortFormat);
|
|
|
|
|
const QChar ypsilon = QLatin1Char('y');
|
|
|
|
|
if (format.count(ypsilon) == 2)
|
|
|
|
|
format.insert(format.indexOf(ypsilon), QString(2, ypsilon));
|
2019-06-17 17:51:55 +02:00
|
|
|
format.replace('/', "\\/");
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QString::fromLatin1("%{CurrentDate:") + format + QLatin1Char('}');
|
2010-08-09 12:23:21 +02:00
|
|
|
return true;
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
if (keyWord == QLatin1String("%USER%")) {
|
2019-02-20 12:28:52 +01:00
|
|
|
*value = Utils::HostOsInfo::isWindowsHost() ? QLatin1String("%{Env:USERNAME}")
|
|
|
|
|
: QLatin1String("%{Env:USER}");
|
2010-08-09 12:23:21 +02:00
|
|
|
return true;
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
// Environment variables (for example '%$EMAIL%').
|
|
|
|
|
if (keyWord.startsWith(QLatin1String("%$"))) {
|
|
|
|
|
const QString varName = keyWord.mid(2, keyWord.size() - 3);
|
2015-09-14 10:56:10 +02:00
|
|
|
*value = QString::fromLatin1("%{Env:") + varName + QLatin1Char('}');
|
2010-08-09 12:23:21 +02:00
|
|
|
return true;
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
2010-08-09 12:23:21 +02:00
|
|
|
return false;
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse a license template, scan for %KEYWORD% and replace if known.
|
|
|
|
|
// Replace '%%' by '%'.
|
2015-09-14 10:56:10 +02:00
|
|
|
static void parseLicenseTemplatePlaceholders(QString *t)
|
2009-06-22 16:22:15 +02:00
|
|
|
{
|
|
|
|
|
int pos = 0;
|
|
|
|
|
const QChar placeHolder = QLatin1Char('%');
|
|
|
|
|
do {
|
|
|
|
|
const int placeHolderPos = t->indexOf(placeHolder, pos);
|
|
|
|
|
if (placeHolderPos == -1)
|
|
|
|
|
break;
|
|
|
|
|
const int endPlaceHolderPos = t->indexOf(placeHolder, placeHolderPos + 1);
|
|
|
|
|
if (endPlaceHolderPos == -1)
|
|
|
|
|
break;
|
|
|
|
|
if (endPlaceHolderPos == placeHolderPos + 1) { // '%%' -> '%'
|
|
|
|
|
t->remove(placeHolderPos, 1);
|
|
|
|
|
pos = placeHolderPos + 1;
|
|
|
|
|
} else {
|
|
|
|
|
const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos);
|
2010-08-09 12:23:21 +02:00
|
|
|
QString replacement;
|
2015-09-14 10:56:10 +02:00
|
|
|
if (keyWordReplacement(keyWord, &replacement)) {
|
2009-06-22 16:22:15 +02:00
|
|
|
t->replace(placeHolderPos, keyWord.size(), replacement);
|
|
|
|
|
pos = placeHolderPos + replacement.size();
|
2010-08-09 12:23:21 +02:00
|
|
|
} else {
|
|
|
|
|
// Leave invalid keywords as is.
|
|
|
|
|
pos = endPlaceHolderPos + 1;
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (pos < t->size());
|
2015-09-14 10:56:10 +02:00
|
|
|
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convenience that returns the formatted license template.
|
2015-09-14 10:56:10 +02:00
|
|
|
QString CppFileSettings::licenseTemplate()
|
2009-06-22 16:22:15 +02:00
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
const QSettings *s = Core::ICore::settings();
|
2021-08-30 10:58:08 +02:00
|
|
|
QString key = QLatin1String(Constants::CPPEDITOR_SETTINGSGROUP);
|
2009-06-22 16:22:15 +02:00
|
|
|
key += QLatin1Char('/');
|
|
|
|
|
key += QLatin1String(licenseTemplatePathKeyC);
|
|
|
|
|
const QString path = s->value(key, QString()).toString();
|
|
|
|
|
if (path.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
QFile file(path);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
|
|
|
|
|
qWarning("Unable to open the license template %s: %s", qPrintable(path), qPrintable(file.errorString()));
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
2010-11-11 11:00:40 +01:00
|
|
|
|
|
|
|
|
QTextStream licenseStream(&file);
|
|
|
|
|
licenseStream.setAutoDetectUnicode(true);
|
|
|
|
|
QString license = licenseStream.readAll();
|
|
|
|
|
|
2015-09-14 10:56:10 +02:00
|
|
|
parseLicenseTemplatePlaceholders(&license);
|
2015-09-09 16:23:26 +02:00
|
|
|
|
|
|
|
|
// Ensure at least one newline at the end of the license template to separate it from the code
|
2009-06-22 16:22:15 +02:00
|
|
|
const QChar newLine = QLatin1Char('\n');
|
|
|
|
|
if (!license.endsWith(newLine))
|
|
|
|
|
license += newLine;
|
2015-09-09 16:23:26 +02:00
|
|
|
|
2009-06-22 16:22:15 +02:00
|
|
|
return license;
|
2009-03-18 16:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------ CppFileSettingsWidget
|
|
|
|
|
|
2020-02-04 16:24:37 +01:00
|
|
|
class CppFileSettingsWidget final : public Core::IOptionsPageWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit CppFileSettingsWidget(CppFileSettings *settings);
|
|
|
|
|
|
|
|
|
|
void apply() final;
|
|
|
|
|
void setSettings(const CppFileSettings &s);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void slotEdit();
|
2021-08-17 16:36:42 +02:00
|
|
|
FilePath licenseTemplatePath() const;
|
|
|
|
|
void setLicenseTemplatePath(const FilePath &);
|
2020-02-04 16:24:37 +01:00
|
|
|
|
|
|
|
|
CppFileSettings *m_settings = nullptr;
|
2022-11-17 13:54:37 +01:00
|
|
|
|
|
|
|
|
QComboBox *m_headerSuffixComboBox = nullptr;
|
|
|
|
|
QLineEdit *m_headerSearchPathsEdit = nullptr;
|
|
|
|
|
QLineEdit *m_headerPrefixesEdit = nullptr;
|
|
|
|
|
QCheckBox *m_headerPragmaOnceCheckBox = nullptr;
|
|
|
|
|
QComboBox *m_sourceSuffixComboBox = nullptr;
|
|
|
|
|
QLineEdit *m_sourceSearchPathsEdit = nullptr;
|
|
|
|
|
QLineEdit *m_sourcePrefixesEdit = nullptr;
|
|
|
|
|
QCheckBox *m_lowerCaseFileNamesCheckBox = nullptr;
|
|
|
|
|
PathChooser *m_licenseTemplatePathChooser = nullptr;
|
2020-02-04 16:24:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CppFileSettingsWidget::CppFileSettingsWidget(CppFileSettings *settings)
|
|
|
|
|
: m_settings(settings)
|
2022-11-17 13:54:37 +01:00
|
|
|
, m_headerSuffixComboBox(new QComboBox)
|
|
|
|
|
, m_headerSearchPathsEdit(new QLineEdit)
|
|
|
|
|
, m_headerPrefixesEdit(new QLineEdit)
|
2023-01-11 20:43:10 +01:00
|
|
|
, m_headerPragmaOnceCheckBox(new QCheckBox(Tr::tr("Use \"#pragma once\" instead of \"#ifndef\" guards")))
|
2022-11-17 13:54:37 +01:00
|
|
|
, m_sourceSuffixComboBox(new QComboBox)
|
|
|
|
|
, m_sourceSearchPathsEdit(new QLineEdit)
|
|
|
|
|
, m_sourcePrefixesEdit(new QLineEdit)
|
2023-01-11 20:43:10 +01:00
|
|
|
, m_lowerCaseFileNamesCheckBox(new QCheckBox(Tr::tr("&Lower case file names")))
|
2022-11-17 13:54:37 +01:00
|
|
|
, m_licenseTemplatePathChooser(new PathChooser)
|
2009-03-18 16:43:01 +01:00
|
|
|
{
|
2023-01-11 20:43:10 +01:00
|
|
|
m_headerSearchPathsEdit->setToolTip(Tr::tr("Comma-separated list of header paths.\n"
|
2022-11-17 13:54:37 +01:00
|
|
|
"\n"
|
|
|
|
|
"Paths can be absolute or relative to the directory of the current open document.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"These paths are used in addition to current directory on Switch Header/Source."));
|
2023-01-11 20:43:10 +01:00
|
|
|
m_headerPrefixesEdit->setToolTip(Tr::tr("Comma-separated list of header prefixes.\n"
|
2022-11-17 13:54:37 +01:00
|
|
|
"\n"
|
|
|
|
|
"These prefixes are used in addition to current file name on Switch Header/Source."));
|
|
|
|
|
m_headerPragmaOnceCheckBox->setToolTip(
|
2023-01-11 20:43:10 +01:00
|
|
|
Tr::tr("Uses \"#pragma once\" instead of \"#ifndef\" include guards."));
|
|
|
|
|
m_sourceSearchPathsEdit->setToolTip(Tr::tr("Comma-separated list of source paths.\n"
|
2022-11-17 13:54:37 +01:00
|
|
|
"\n"
|
|
|
|
|
"Paths can be absolute or relative to the directory of the current open document.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"These paths are used in addition to current directory on Switch Header/Source."));
|
2023-01-11 20:43:10 +01:00
|
|
|
m_sourcePrefixesEdit->setToolTip(Tr::tr("Comma-separated list of source prefixes.\n"
|
2022-11-17 13:54:37 +01:00
|
|
|
"\n"
|
|
|
|
|
"These prefixes are used in addition to current file name on Switch Header/Source."));
|
|
|
|
|
|
|
|
|
|
using namespace Layouting;
|
|
|
|
|
|
|
|
|
|
Column {
|
|
|
|
|
Group {
|
|
|
|
|
title("Headers"),
|
|
|
|
|
Form {
|
2023-01-11 20:43:10 +01:00
|
|
|
Tr::tr("&Suffix:"), m_headerSuffixComboBox, st, br,
|
|
|
|
|
Tr::tr("S&earch paths:"), m_headerSearchPathsEdit, br,
|
|
|
|
|
Tr::tr("&Prefixes:"), m_headerPrefixesEdit, br,
|
|
|
|
|
Tr::tr("Include guards"), m_headerPragmaOnceCheckBox
|
2022-11-17 13:54:37 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Group {
|
|
|
|
|
title("Sources"),
|
|
|
|
|
Form {
|
2023-01-11 20:43:10 +01:00
|
|
|
Tr::tr("S&uffix:"), m_sourceSuffixComboBox, st, br,
|
|
|
|
|
Tr::tr("Se&arch paths:"), m_sourceSearchPathsEdit, br,
|
|
|
|
|
Tr::tr("P&refixes:"), m_sourcePrefixesEdit
|
2022-11-17 13:54:37 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
m_lowerCaseFileNamesCheckBox,
|
|
|
|
|
Form {
|
2023-01-11 20:43:10 +01:00
|
|
|
Tr::tr("License &template:"), m_licenseTemplatePathChooser
|
2022-11-17 13:54:37 +01:00
|
|
|
},
|
|
|
|
|
st
|
|
|
|
|
}.attachTo(this);
|
|
|
|
|
|
2009-03-18 16:43:01 +01:00
|
|
|
// populate suffix combos
|
2022-11-17 13:54:37 +01:00
|
|
|
const MimeType sourceMt = Utils::mimeTypeForName(QLatin1String(Constants::CPP_SOURCE_MIMETYPE));
|
2015-02-04 09:32:46 +01:00
|
|
|
if (sourceMt.isValid()) {
|
2022-05-05 15:51:12 +02:00
|
|
|
const QStringList suffixes = sourceMt.suffixes();
|
|
|
|
|
for (const QString &suffix : suffixes)
|
2022-11-17 13:54:37 +01:00
|
|
|
m_sourceSuffixComboBox->addItem(suffix);
|
2015-02-04 09:32:46 +01:00
|
|
|
}
|
2009-03-18 16:43:01 +01:00
|
|
|
|
2022-11-17 13:54:37 +01:00
|
|
|
const MimeType headerMt = Utils::mimeTypeForName(QLatin1String(Constants::CPP_HEADER_MIMETYPE));
|
2015-02-04 09:32:46 +01:00
|
|
|
if (headerMt.isValid()) {
|
2022-05-05 15:51:12 +02:00
|
|
|
const QStringList suffixes = headerMt.suffixes();
|
|
|
|
|
for (const QString &suffix : suffixes)
|
2022-11-17 13:54:37 +01:00
|
|
|
m_headerSuffixComboBox->addItem(suffix);
|
2015-02-04 09:32:46 +01:00
|
|
|
}
|
2022-11-17 13:54:37 +01:00
|
|
|
m_licenseTemplatePathChooser->setExpectedKind(PathChooser::File);
|
|
|
|
|
m_licenseTemplatePathChooser->setHistoryCompleter(QLatin1String("Cpp.LicenseTemplate.History"));
|
2023-01-11 20:43:10 +01:00
|
|
|
m_licenseTemplatePathChooser->addButton(Tr::tr("Edit..."), this, [this] { slotEdit(); });
|
2009-03-18 16:43:01 +01:00
|
|
|
|
2020-02-04 16:24:37 +01:00
|
|
|
setSettings(*m_settings);
|
2009-03-18 16:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:36:42 +02:00
|
|
|
FilePath CppFileSettingsWidget::licenseTemplatePath() const
|
2009-06-22 16:22:15 +02:00
|
|
|
{
|
2022-11-17 13:54:37 +01:00
|
|
|
return m_licenseTemplatePathChooser->filePath();
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:36:42 +02:00
|
|
|
void CppFileSettingsWidget::setLicenseTemplatePath(const FilePath &lp)
|
2009-06-22 16:22:15 +02:00
|
|
|
{
|
2022-11-17 13:54:37 +01:00
|
|
|
m_licenseTemplatePathChooser->setFilePath(lp);
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-25 22:42:37 +03:00
|
|
|
static QStringList trimmedPaths(const QString &paths)
|
|
|
|
|
{
|
|
|
|
|
QStringList res;
|
2022-05-05 15:51:12 +02:00
|
|
|
for (const QString &path : paths.split(QLatin1Char(','), Qt::SkipEmptyParts))
|
2013-08-25 22:42:37 +03:00
|
|
|
res << path.trimmed();
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 16:24:37 +01:00
|
|
|
void CppFileSettingsWidget::apply()
|
2009-03-18 16:43:01 +01:00
|
|
|
{
|
|
|
|
|
CppFileSettings rc;
|
2022-11-17 13:54:37 +01:00
|
|
|
rc.lowerCaseFiles = m_lowerCaseFileNamesCheckBox->isChecked();
|
|
|
|
|
rc.headerPragmaOnce = m_headerPragmaOnceCheckBox->isChecked();
|
|
|
|
|
rc.headerPrefixes = trimmedPaths(m_headerPrefixesEdit->text());
|
|
|
|
|
rc.sourcePrefixes = trimmedPaths(m_sourcePrefixesEdit->text());
|
|
|
|
|
rc.headerSuffix = m_headerSuffixComboBox->currentText();
|
|
|
|
|
rc.sourceSuffix = m_sourceSuffixComboBox->currentText();
|
|
|
|
|
rc.headerSearchPaths = trimmedPaths(m_headerSearchPathsEdit->text());
|
|
|
|
|
rc.sourceSearchPaths = trimmedPaths(m_sourceSearchPathsEdit->text());
|
2021-08-17 16:36:42 +02:00
|
|
|
rc.licenseTemplatePath = licenseTemplatePath().toString();
|
2020-02-04 16:24:37 +01:00
|
|
|
|
|
|
|
|
if (rc == *m_settings)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
*m_settings = rc;
|
|
|
|
|
m_settings->toSettings(Core::ICore::settings());
|
|
|
|
|
m_settings->applySuffixesToMimeDB();
|
2021-08-30 10:58:08 +02:00
|
|
|
CppEditorPlugin::clearHeaderSourceCache();
|
2009-03-18 16:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void setComboText(QComboBox *cb, const QString &text, int defaultIndex = 0)
|
|
|
|
|
{
|
|
|
|
|
const int index = cb->findText(text);
|
|
|
|
|
cb->setCurrentIndex(index == -1 ? defaultIndex: index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFileSettingsWidget::setSettings(const CppFileSettings &s)
|
|
|
|
|
{
|
2014-08-23 01:19:53 +02:00
|
|
|
const QChar comma = QLatin1Char(',');
|
2022-11-17 13:54:37 +01:00
|
|
|
m_lowerCaseFileNamesCheckBox->setChecked(s.lowerCaseFiles);
|
|
|
|
|
m_headerPragmaOnceCheckBox->setChecked(s.headerPragmaOnce);
|
|
|
|
|
m_headerPrefixesEdit->setText(s.headerPrefixes.join(comma));
|
|
|
|
|
m_sourcePrefixesEdit->setText(s.sourcePrefixes.join(comma));
|
|
|
|
|
setComboText(m_headerSuffixComboBox, s.headerSuffix);
|
|
|
|
|
setComboText(m_sourceSuffixComboBox, s.sourceSuffix);
|
|
|
|
|
m_headerSearchPathsEdit->setText(s.headerSearchPaths.join(comma));
|
|
|
|
|
m_sourceSearchPathsEdit->setText(s.sourceSearchPaths.join(comma));
|
2021-08-17 16:36:42 +02:00
|
|
|
setLicenseTemplatePath(FilePath::fromString(s.licenseTemplatePath));
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFileSettingsWidget::slotEdit()
|
|
|
|
|
{
|
2021-08-17 16:36:42 +02:00
|
|
|
FilePath path = licenseTemplatePath();
|
2011-03-30 15:15:15 +02:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
|
// Pick a file name and write new template, edit with C++
|
2023-01-11 20:43:10 +01:00
|
|
|
path = FileUtils::getSaveFilePath(this, Tr::tr("Choose Location for New License Template File"));
|
2011-03-30 15:15:15 +02:00
|
|
|
if (path.isEmpty())
|
|
|
|
|
return;
|
2021-08-17 16:36:42 +02:00
|
|
|
FileSaver saver(path, QIODevice::Text);
|
2023-01-11 20:43:10 +01:00
|
|
|
saver.write(Tr::tr(licenseTemplateTemplate).arg(Core::Constants::IDE_DISPLAY_NAME).toUtf8());
|
2011-03-30 15:15:15 +02:00
|
|
|
if (!saver.finalize(this))
|
|
|
|
|
return;
|
|
|
|
|
setLicenseTemplatePath(path);
|
2009-06-22 16:22:15 +02:00
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
// Edit (now) existing file with C++
|
2013-05-31 12:52:53 +02:00
|
|
|
Core::EditorManager::openEditor(path, CppEditor::Constants::CPPEDITOR_ID);
|
2009-03-18 16:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------- CppFileSettingsPage
|
2020-02-04 16:24:37 +01:00
|
|
|
|
|
|
|
|
CppFileSettingsPage::CppFileSettingsPage(CppFileSettings *settings)
|
2009-03-18 16:43:01 +01:00
|
|
|
{
|
2013-01-16 15:22:58 +01:00
|
|
|
setId(Constants::CPP_FILE_SETTINGS_ID);
|
2023-01-11 20:43:10 +01:00
|
|
|
setDisplayName(Tr::tr("File Naming"));
|
2012-12-29 03:37:27 +01:00
|
|
|
setCategory(Constants::CPP_SETTINGS_CATEGORY);
|
2020-02-04 16:24:37 +01:00
|
|
|
setWidgetCreator([settings] { return new CppFileSettingsWidget(settings); });
|
2009-11-20 16:55:23 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
} // namespace CppEditor::Internal
|