diff options
| author | Cristián Maureira-Fredes <cmaureirafredes@gmail.com> | 2019-03-13 18:22:45 +0100 |
|---|---|---|
| committer | hjk <hjk@qt.io> | 2019-03-15 12:50:38 +0000 |
| commit | b503ea7f06147f9e64cd03d9454aafb40997c3f7 (patch) | |
| tree | 3328efa6d368eee9e2fefa2c5f499bd42c510d4c /src/plugins/pythoneditor/pythoneditorplugin.cpp | |
| parent | 1660d3301a3b806fc52eae4c8bac8713b89e462a (diff) | |
| download | qt-creator-b503ea7f06147f9e64cd03d9454aafb40997c3f7.tar.gz | |
Add more Python features
On the wizard related to Main Windows, not there are more
options to specify the name of the files and classes.
It's possible to Add and Remove files on Python projects
that use the new .pyproject file.
Improved the empty application,
to template the .pyproject file and use the proper name
for the file.
Added the icons for file overlay.
Change-Id: Iaba7feda69e0f608260b5fb1d1b04b2a42b08c2d
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/pythoneditor/pythoneditorplugin.cpp')
| -rw-r--r-- | src/plugins/pythoneditor/pythoneditorplugin.cpp | 74 |
1 files changed, 58 insertions, 16 deletions
diff --git a/src/plugins/pythoneditor/pythoneditorplugin.cpp b/src/plugins/pythoneditor/pythoneditorplugin.cpp index 4d71670e7d..dd722283e9 100644 --- a/src/plugins/pythoneditor/pythoneditorplugin.cpp +++ b/src/plugins/pythoneditor/pythoneditorplugin.cpp @@ -94,6 +94,9 @@ public: bool needsConfiguration() const final { return false; } bool needsBuildConfigurations() const final { return false; } + bool writePyProjectFile(const QString &fileName, QString &content, + const QStringList &rawList, QString *errorMessage); + private: RestoreResult fromMap(const QVariantMap &map, QString *errorMessage) override; bool setupTarget(Target *t) override @@ -402,18 +405,61 @@ bool PythonProject::saveRawFileList(const QStringList &rawFileList) bool PythonProject::saveRawList(const QStringList &rawList, const QString &fileName) { FileChangeBlocker changeGuarg(fileName); - // Make sure we can open the file for writing - FileSaver saver(fileName, QIODevice::Text); - if (!saver.hasError()) { - QTextStream stream(saver.file()); - foreach (const QString &filePath, rawList) - stream << filePath << '\n'; - saver.setResult(&stream); + bool result = false; + + // New project file + if (fileName.endsWith(".pyproject")) { + FileSaver saver(fileName, QIODevice::ReadOnly | QIODevice::Text); + if (!saver.hasError()) { + QString content = QTextStream(saver.file()).readAll(); + if (saver.finalize(ICore::mainWindow())) { + QString errorMessage; + result = writePyProjectFile(fileName, content, rawList, &errorMessage); + if (!errorMessage.isEmpty()) + Core::MessageManager::write(errorMessage); + } + } + } else { // Old project file + FileSaver saver(fileName, QIODevice::WriteOnly | QIODevice::Text); + if (!saver.hasError()) { + QTextStream stream(saver.file()); + for (const QString &filePath : rawList) + stream << filePath << '\n'; + saver.setResult(&stream); + result = saver.finalize(ICore::mainWindow()); + } } - bool result = saver.finalize(ICore::mainWindow()); + return result; } +bool PythonProject::writePyProjectFile(const QString &fileName, QString &content, + const QStringList &rawList, QString *errorMessage) +{ + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + *errorMessage = PythonProject::tr("Unable to open \"%1\" for reading: %2") + .arg(fileName, file.errorString()); + return false; + } + + // Build list of files with the current rawList for the JSON file + QString files("["); + for (const QString &f : rawList) + if (!f.endsWith(".pyproject")) + files += QString("\"%1\",").arg(f); + files = files.left(files.lastIndexOf(',')); // Removing leading comma + files += ']'; + + // Removing everything inside square parenthesis + // to replace it with the new list of files for the JSON file. + QRegularExpression pattern(R"(\[.*\])"); + content.replace(pattern, files); + file.write(content.toUtf8()); + + return true; +} + bool PythonProject::addFiles(const QStringList &filePaths) { QStringList newList = m_rawFileList; @@ -422,10 +468,7 @@ bool PythonProject::addFiles(const QStringList &filePaths) foreach (const QString &filePath, filePaths) newList.append(baseDir.relativeFilePath(filePath)); - bool result = saveRawList(newList, projectFilePath().toString()); - refresh(); - - return result; + return saveRawFileList(newList); } bool PythonProject::removeFiles(const QStringList &filePaths) @@ -701,11 +744,10 @@ bool PythonEditorPlugin::initialize(const QStringList &arguments, QString *error void PythonEditorPlugin::extensionsInitialized() { - // Initialize editor actions handler // Add MIME overlay icons (these icons displayed at Project dock panel) - const QIcon icon = QIcon::fromTheme(C_PY_MIME_ICON); - if (!icon.isNull()) - Core::FileIconProvider::registerIconOverlayForMimeType(icon, C_PY_MIMETYPE); + QString imageFile = creatorTheme()->imageFile(Theme::IconOverlayPro, + ProjectExplorer::Constants::FILEOVERLAY_PY); + FileIconProvider::registerIconOverlayForSuffix(imageFile, "py"); TaskHub::addCategory(PythonErrorTaskCategory, "Python", true); } |
