diff options
-rw-r--r-- | src/plugins/cppeditor/cppclasswizard.cpp | 6 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppfilewizard.cpp | 2 | ||||
-rw-r--r-- | src/plugins/cpptools/abstracteditorsupport.cpp | 9 | ||||
-rw-r--r-- | src/plugins/cpptools/cppfilesettingspage.cpp | 149 | ||||
-rw-r--r-- | src/plugins/cpptools/cppfilesettingspage.h | 14 | ||||
-rw-r--r-- | src/plugins/cpptools/cppfilesettingspage.ui | 18 | ||||
-rw-r--r-- | src/plugins/cpptools/cppmodelmanagerinterface.h | 2 | ||||
-rw-r--r-- | src/plugins/designer/cpp/formclasswizardparameters.cpp | 7 | ||||
-rw-r--r-- | src/plugins/qt4projectmanager/wizards/consoleappwizard.cpp | 4 | ||||
-rw-r--r-- | src/plugins/qt4projectmanager/wizards/guiappwizard.cpp | 8 | ||||
-rw-r--r-- | src/plugins/qt4projectmanager/wizards/librarywizard.cpp | 8 |
11 files changed, 215 insertions, 12 deletions
diff --git a/src/plugins/cppeditor/cppclasswizard.cpp b/src/plugins/cppeditor/cppclasswizard.cpp index 98ba8842b2..a14d35bbe6 100644 --- a/src/plugins/cppeditor/cppclasswizard.cpp +++ b/src/plugins/cppeditor/cppclasswizard.cpp @@ -31,6 +31,7 @@ #include "cppeditorconstants.h" #include <cpptools/cpptoolsconstants.h> +#include <cpptools/cppmodelmanagerinterface.h> #include <coreplugin/icore.h> #include <coreplugin/mimedatabase.h> @@ -224,12 +225,14 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par if (namespaceList.empty()) // Paranoia! return false; + const QString license = CppTools::AbstractEditorSupport::licenseTemplate(); + const QString unqualifiedClassName = namespaceList.takeLast(); const QString guard = Core::Utils::headerGuard(params.headerFile); // == Header file == QTextStream headerStr(header); - headerStr << "#ifndef " << guard + headerStr << license << "#ifndef " << guard << "\n#define " << guard << '\n'; const QRegExp qtClassExpr(QLatin1String("^Q[A-Z3].+")); @@ -262,6 +265,7 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par // == Source file == QTextStream sourceStr(source); + sourceStr << license; Core::Utils::writeIncludeFileDirective(params.headerFile, false, sourceStr); Core::Utils::writeOpeningNameSpaces(namespaceList, QString(), sourceStr); diff --git a/src/plugins/cppeditor/cppfilewizard.cpp b/src/plugins/cppeditor/cppfilewizard.cpp index c284255713..7b311ef316 100644 --- a/src/plugins/cppeditor/cppfilewizard.cpp +++ b/src/plugins/cppeditor/cppfilewizard.cpp @@ -31,6 +31,7 @@ #include "cppeditor.h" #include "cppeditorconstants.h" +#include <cpptools/cppmodelmanagerinterface.h> #include <utils/codegeneration.h> #include <QtCore/QTextStream> @@ -70,6 +71,7 @@ QString CppFileWizard::fileContents(FileType type, const QString &fileName) cons const QString baseName = QFileInfo(fileName).completeBaseName(); QString contents; QTextStream str(&contents); + str << CppTools::AbstractEditorSupport::licenseTemplate(); switch (type) { case Header: { const QString guard = Core::Utils::headerGuard(fileName); diff --git a/src/plugins/cpptools/abstracteditorsupport.cpp b/src/plugins/cpptools/abstracteditorsupport.cpp index c776496138..a0c442882a 100644 --- a/src/plugins/cpptools/abstracteditorsupport.cpp +++ b/src/plugins/cpptools/abstracteditorsupport.cpp @@ -28,6 +28,8 @@ **************************************************************************/ #include "cppmodelmanagerinterface.h" +#include "cpptoolsconstants.h" +#include "cppfilesettingspage.h" #include <cplusplus/Overview.h> #include <cplusplus/CppDocument.h> @@ -37,6 +39,8 @@ #include <Symbols.h> #include <Scope.h> +#include <coreplugin/icore.h> + namespace CppTools { AbstractEditorSupport::AbstractEditorSupport(CppModelManagerInterface *modelmanager) : @@ -71,4 +75,9 @@ QString AbstractEditorSupport::functionAt(const CppModelManagerInterface *modelM return QString(); } +QString AbstractEditorSupport::licenseTemplate() +{ + return Internal::CppFileSettings::licenseTemplate(); } +} + diff --git a/src/plugins/cpptools/cppfilesettingspage.cpp b/src/plugins/cpptools/cppfilesettingspage.cpp index 11f776784d..f3e3e574c8 100644 --- a/src/plugins/cpptools/cppfilesettingspage.cpp +++ b/src/plugins/cpptools/cppfilesettingspage.cpp @@ -32,15 +32,33 @@ #include "ui_cppfilesettingspage.h" #include <coreplugin/icore.h> +#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/mimedatabase.h> +#include <cppeditor/cppeditorconstants.h> + #include <extensionsystem/pluginmanager.h> #include <QtCore/QSettings> #include <QtCore/QDebug> +#include <QtCore/QFile> #include <QtCore/QCoreApplication> +#include <QtCore/QDate> +#include <QtCore/QLocale> + +#include <QtGui/QFileDialog> +#include <QtGui/QMessageBox> static const char *headerSuffixKeyC = "HeaderSuffix"; static const char *sourceSuffixKeyC = "SourceSuffix"; +static const char *licenseTemplatePathKeyC = "LicenseTemplate"; + +const char *licenseTemplateTemplate = QT_TRANSLATE_NOOP("CppTools::Internal::CppFileSettingsWidget", +"/**************************************************************************\n" +"** Qt Creator license header template\n" +"** Special keywords: %USER% %DATE% %YEAR%\n" +"** Environment variables: %$VARIABLE%\n" +"** To protect a percent sign, use '%%'.\n" +"**************************************************************************/\n"); namespace CppTools { namespace Internal { @@ -56,6 +74,7 @@ void CppFileSettings::toSettings(QSettings *s) const s->setValue(QLatin1String(headerSuffixKeyC), headerSuffix); s->setValue(QLatin1String(sourceSuffixKeyC), sourceSuffix); s->setValue(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), lowerCaseFiles); + s->setValue(QLatin1String(licenseTemplatePathKeyC), licenseTemplatePath); s->endGroup(); } @@ -66,6 +85,7 @@ void CppFileSettings::fromSettings(QSettings *s) sourceSuffix = s->value(QLatin1String(sourceSuffixKeyC), QLatin1String("cpp")).toString(); const bool lowerCaseDefault = Constants::lowerCaseFilesDefault; lowerCaseFiles = s->value(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), QVariant(lowerCaseDefault)).toBool(); + licenseTemplatePath = s->value(QLatin1String(licenseTemplatePathKeyC), QString()).toString(); s->endGroup(); } @@ -80,7 +100,96 @@ bool CppFileSettings::equals(const CppFileSettings &rhs) const { return lowerCaseFiles == rhs.lowerCaseFiles && headerSuffix == rhs.headerSuffix - && sourceSuffix == rhs.sourceSuffix; + && sourceSuffix == rhs.sourceSuffix + && licenseTemplatePath == rhs.licenseTemplatePath; +} + +// Replacements of special license template keywords. +static QString keyWordReplacement(const QString &keyWord) +{ + if (keyWord == QLatin1String("%YEAR%")) { + return QString::number(QDate::currentDate().year()); + } + 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)); + } + return QDate::currentDate().toString(format); + } + if (keyWord == QLatin1String("%USER%")) { +#ifdef Q_OS_WIN + return QString::fromLocal8Bit(qgetenv("USERNAME")); +#else + return QString::fromLocal8Bit(qgetenv("USER")); +#endif + } + // Environment variables (for example '%$EMAIL%'). + if (keyWord.startsWith(QLatin1String("%$"))) { + const QString varName = keyWord.mid(2, keyWord.size() - 3); + return QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit())); + } + return QString(); +} + +// Parse a license template, scan for %KEYWORD% and replace if known. +// Replace '%%' by '%'. +static void parseLicenseTemplatePlaceholders(QString *t) +{ + 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); + const QString replacement = keyWordReplacement(keyWord); + if (replacement.isEmpty()) { + pos = endPlaceHolderPos + 1; + } else { + t->replace(placeHolderPos, keyWord.size(), replacement); + pos = placeHolderPos + replacement.size(); + } + } + } while (pos < t->size()); +} + +// Convenience that returns the formatted license template. +QString CppFileSettings::licenseTemplate() +{ + + const QSettings *s = Core::ICore::instance()->settings(); + QString key = QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP); + 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(); + } + QString license = QString::fromUtf8(file.readAll()); + parseLicenseTemplatePlaceholders(&license); + // Ensure exactly one additional new line separating stuff + const QChar newLine = QLatin1Char('\n'); + if (!license.endsWith(newLine)) + license += newLine; + license += newLine; + return license; } // ------------------ CppFileSettingsWidget @@ -99,6 +208,8 @@ CppFileSettingsWidget::CppFileSettingsWidget(QWidget *parent) : if (const Core::MimeType headerMt = mdb->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE))) foreach (const QString &suffix, headerMt.suffixes()) m_ui->headerSuffixComboBox->addItem(suffix); + m_ui->licenseTemplatePathChooser->setExpectedKind(Core::Utils::PathChooser::File); + m_ui->licenseTemplatePathChooser->addButton(tr("Edit..."), this, SLOT(slotEdit())); } CppFileSettingsWidget::~CppFileSettingsWidget() @@ -106,12 +217,23 @@ CppFileSettingsWidget::~CppFileSettingsWidget() delete m_ui; } +QString CppFileSettingsWidget::licenseTemplatePath() const +{ + return m_ui->licenseTemplatePathChooser->path(); +} + +void CppFileSettingsWidget::setLicenseTemplatePath(const QString &lp) +{ + m_ui->licenseTemplatePathChooser->setPath(lp); +} + CppFileSettings CppFileSettingsWidget::settings() const { CppFileSettings rc; rc.lowerCaseFiles = m_ui->lowerCaseFileNamesCheckBox->isChecked(); rc.headerSuffix = m_ui->headerSuffixComboBox->currentText(); rc.sourceSuffix = m_ui->sourceSuffixComboBox->currentText(); + rc.licenseTemplatePath = licenseTemplatePath(); return rc; } @@ -126,6 +248,31 @@ void CppFileSettingsWidget::setSettings(const CppFileSettings &s) m_ui->lowerCaseFileNamesCheckBox->setChecked(s.lowerCaseFiles); setComboText(m_ui->headerSuffixComboBox, s.headerSuffix); setComboText(m_ui->sourceSuffixComboBox, s.sourceSuffix); + setLicenseTemplatePath(s.licenseTemplatePath); +} + +void CppFileSettingsWidget::slotEdit() +{ + QString path = licenseTemplatePath(); + // Edit existing file with C++ + if (!path.isEmpty()) { + Core::EditorManager::instance()->openEditor(path, QLatin1String(CppEditor::Constants::CPPEDITOR_KIND)); + return; + } + // Pick a file name and write new template, edit with C++ + path = QFileDialog::getSaveFileName(this, tr("Choose a new license template file")); + if (path.isEmpty()) + return; + QFile file(path); + if (!file.open(QIODevice::ReadWrite|QIODevice::Text)) { + QMessageBox::warning(this, tr("Template write error"), + tr("Cannot write to %1: %2").arg(path, file.errorString())); + return; + } + file.write(tr(licenseTemplateTemplate).toUtf8()); + file.close(); + setLicenseTemplatePath(path); + Core::EditorManager::instance()->openEditor(path, QLatin1String(CppEditor::Constants::CPPEDITOR_KIND)); } // --------------- CppFileSettingsPage diff --git a/src/plugins/cpptools/cppfilesettingspage.h b/src/plugins/cpptools/cppfilesettingspage.h index 42ba376cad..9529adc116 100644 --- a/src/plugins/cpptools/cppfilesettingspage.h +++ b/src/plugins/cpptools/cppfilesettingspage.h @@ -51,10 +51,16 @@ struct CppFileSettings { QString headerSuffix; QString sourceSuffix; bool lowerCaseFiles; + QString licenseTemplatePath; void toSettings(QSettings *) const; void fromSettings(QSettings *); - bool applySuffixesToMimeDB(); + bool applySuffixesToMimeDB(); + + // Convenience to return a license template completely formatted. + // Currently made public in + static QString licenseTemplate(); + bool equals(const CppFileSettings &rhs) const; }; @@ -71,7 +77,13 @@ public: CppFileSettings settings() const; void setSettings(const CppFileSettings &s); +private slots: + void slotEdit(); + private: + inline QString licenseTemplatePath() const; + inline void setLicenseTemplatePath(const QString &); + Ui::CppFileSettingsPage *m_ui; }; diff --git a/src/plugins/cpptools/cppfilesettingspage.ui b/src/plugins/cpptools/cppfilesettingspage.ui index 7c55c16f2b..ea074c2ff8 100644 --- a/src/plugins/cpptools/cppfilesettingspage.ui +++ b/src/plugins/cpptools/cppfilesettingspage.ui @@ -53,6 +53,16 @@ </property> </widget> </item> + <item row="3" column="0"> + <widget class="QLabel" name="licenseTemplateLabel"> + <property name="text"> + <string>License Template:</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="Core::Utils::PathChooser" name="licenseTemplatePathChooser" native="true"/> + </item> </layout> </widget> </item> @@ -71,6 +81,14 @@ </item> </layout> </widget> + <customwidgets> + <customwidget> + <class>Core::Utils::PathChooser</class> + <extends>QWidget</extends> + <header location="global">utils/pathchooser.h</header> + <container>1</container> + </customwidget> + </customwidgets> <resources/> <connections/> </ui> diff --git a/src/plugins/cpptools/cppmodelmanagerinterface.h b/src/plugins/cpptools/cppmodelmanagerinterface.h index 57577edc87..ce56686858 100644 --- a/src/plugins/cpptools/cppmodelmanagerinterface.h +++ b/src/plugins/cpptools/cppmodelmanagerinterface.h @@ -110,6 +110,8 @@ public: const QString &fileName, int line, int column); + static QString licenseTemplate(); + private: CppModelManagerInterface *m_modelmanager; }; diff --git a/src/plugins/designer/cpp/formclasswizardparameters.cpp b/src/plugins/designer/cpp/formclasswizardparameters.cpp index 314210e03b..b9a88e4a2c 100644 --- a/src/plugins/designer/cpp/formclasswizardparameters.cpp +++ b/src/plugins/designer/cpp/formclasswizardparameters.cpp @@ -31,6 +31,7 @@ #include "formtemplatewizardpage.h" #include <utils/codegeneration.h> +#include <cpptools/cppmodelmanagerinterface.h> #include <QtCore/QTextStream> #include <QtCore/QFileInfo> @@ -50,7 +51,7 @@ FormClassWizardParameters::FormClassWizardParameters() : bool FormClassWizardParameters::generateCpp(QString *header, QString *source, int indentation) const { - const QString indent = QString(indentation, QLatin1Char(' ')); + const QString indent = QString(indentation, QLatin1Char(' ')); QString formBaseClass; QString uiClassName; if (!FormTemplateWizardPage::getUIXmlData(uiTemplate, &formBaseClass, &uiClassName)) { @@ -72,6 +73,7 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in const QString unqualifiedClassName = namespaceList.takeLast(); + const QString license = CppTools::AbstractEditorSupport::licenseTemplate(); // Include guards const QString guard = Core::Utils::headerGuard(headerFile); @@ -81,7 +83,7 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in // 1) Header file QTextStream headerStr(header); - headerStr << "#ifndef " << guard + headerStr << license << "#ifndef " << guard << "\n#define " << guard << '\n' << '\n'; // Include 'ui_' @@ -136,6 +138,7 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in // 2) Source file QTextStream sourceStr(source); + sourceStr << license; Core::Utils::writeIncludeFileDirective(headerFile, false, sourceStr); if (embedding == PointerAggregatedUiClass) Core::Utils::writeIncludeFileDirective(uiInclude, false, sourceStr); diff --git a/src/plugins/qt4projectmanager/wizards/consoleappwizard.cpp b/src/plugins/qt4projectmanager/wizards/consoleappwizard.cpp index 6db15fc714..a9670ba41a 100644 --- a/src/plugins/qt4projectmanager/wizards/consoleappwizard.cpp +++ b/src/plugins/qt4projectmanager/wizards/consoleappwizard.cpp @@ -34,6 +34,7 @@ #include "qt4projectmanagerconstants.h" #include <utils/pathchooser.h> +#include <cpptools/cppmodelmanagerinterface.h> #include <QtCore/QDir> #include <QtCore/QTextStream> @@ -77,12 +78,13 @@ Core::GeneratedFiles const ConsoleAppWizardDialog *wizard = qobject_cast< const ConsoleAppWizardDialog *>(w); const QtProjectParameters params = wizard->parameters(); const QString projectPath = params.projectPath(); + const QString license = CppTools::AbstractEditorSupport::licenseTemplate(); // Create files: Source const QString sourceFileName = Core::BaseFileWizard::buildFileName(projectPath, QLatin1String(mainSourceFileC), sourceSuffix()); Core::GeneratedFile source(sourceFileName); - source.setContents(QLatin1String(mainCppC)); + source.setContents(license + QLatin1String(mainCppC)); // Create files: Profile const QString profileName = Core::BaseFileWizard::buildFileName(projectPath, params.name,profileSuffix()); diff --git a/src/plugins/qt4projectmanager/wizards/guiappwizard.cpp b/src/plugins/qt4projectmanager/wizards/guiappwizard.cpp index b984a7fcad..8dcaeca883 100644 --- a/src/plugins/qt4projectmanager/wizards/guiappwizard.cpp +++ b/src/plugins/qt4projectmanager/wizards/guiappwizard.cpp @@ -36,6 +36,7 @@ #include <utils/pathchooser.h> #include <projectexplorer/projectnodes.h> +#include <cpptools/cppmodelmanagerinterface.h> #include <QtCore/QDir> #include <QtCore/QFile> @@ -95,6 +96,7 @@ Core::GeneratedFiles GuiAppWizard::generateFiles(const QWizard *w, const QtProjectParameters projectParams = dialog->projectParameters(); const QString projectPath = projectParams.projectPath(); const GuiAppParameters params = dialog->parameters(); + const QString license = CppTools::AbstractEditorSupport::licenseTemplate(); // Generate file names. Note that the path for the project files is the // newly generated project directory. @@ -105,21 +107,21 @@ Core::GeneratedFiles GuiAppWizard::generateFiles(const QWizard *w, Core::GeneratedFile mainSource(mainSourceFileName); if (!parametrizeTemplate(templatePath, QLatin1String("main.cpp"), params, &contents, errorMessage)) return Core::GeneratedFiles(); - mainSource.setContents(contents); + mainSource.setContents(license + contents); // Create files: form source const QString formSourceTemplate = params.designerForm ? QLatin1String("mywidget_form.cpp") : QLatin1String("mywidget.cpp"); const QString formSourceFileName = buildFileName(projectPath, params.sourceFileName, sourceSuffix()); Core::GeneratedFile formSource(formSourceFileName); if (!parametrizeTemplate(templatePath, formSourceTemplate, params, &contents, errorMessage)) return Core::GeneratedFiles(); - formSource.setContents(contents); + formSource.setContents(license + contents); // Create files: form header const QString formHeaderName = buildFileName(projectPath, params.headerFileName, headerSuffix()); const QString formHeaderTemplate = params.designerForm ? QLatin1String("mywidget_form.h") : QLatin1String("mywidget.h"); Core::GeneratedFile formHeader(formHeaderName); if (!parametrizeTemplate(templatePath, formHeaderTemplate, params, &contents, errorMessage)) return Core::GeneratedFiles(); - formHeader.setContents(contents); + formHeader.setContents(license + contents); // Create files: form QSharedPointer<Core::GeneratedFile> form; if (params.designerForm) { diff --git a/src/plugins/qt4projectmanager/wizards/librarywizard.cpp b/src/plugins/qt4projectmanager/wizards/librarywizard.cpp index 82e24a3977..9707f9dd43 100644 --- a/src/plugins/qt4projectmanager/wizards/librarywizard.cpp +++ b/src/plugins/qt4projectmanager/wizards/librarywizard.cpp @@ -34,6 +34,7 @@ #include <utils/codegeneration.h> #include <utils/pathchooser.h> +#include <cpptools/cppmodelmanagerinterface.h> #include <QtCore/QDir> #include <QtCore/QFileInfo> @@ -73,6 +74,7 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w, const QtProjectParameters projectParams = dialog->parameters(); const QString projectPath = projectParams.projectPath(); const LibraryParameters params = dialog->libraryParameters(); + const QString license = CppTools::AbstractEditorSupport::licenseTemplate(); const QString sharedLibExportMacro = QtProjectParameters::exportMacro(projectParams.name); @@ -91,7 +93,7 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w, const QString globalHeaderName = buildFileName(projectPath, projectParams.name + QLatin1String(sharedHeaderPostfixC), headerSuffix()); Core::GeneratedFile globalHeader(globalHeaderName); globalHeaderFileName = QFileInfo(globalHeader.path()).fileName(); - globalHeader.setContents(LibraryParameters::generateSharedHeader(globalHeaderFileName, projectParams.name, sharedLibExportMacro)); + globalHeader.setContents(license + LibraryParameters::generateSharedHeader(globalHeaderFileName, projectParams.name, sharedLibExportMacro)); rc.push_back(globalHeader); } @@ -101,8 +103,8 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w, globalHeaderFileName, sharedLibExportMacro, /* indentation*/ 4, &headerContents, &sourceContents); - source.setContents(sourceContents); - header.setContents(headerContents); + source.setContents(license + sourceContents); + header.setContents(license + headerContents); rc.push_back(source); rc.push_back(header); // Create files: profile |