Designer: Use more FilePath

Change-Id: I9571c25c6c3dd430ccd3f7233a4f7290c9ba508b
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2022-06-14 17:08:36 +02:00
parent a9245bf186
commit 386c462a72

View File

@@ -27,7 +27,6 @@
#include "formeditorfactory.h" #include "formeditorfactory.h"
#include "formeditorw.h" #include "formeditorw.h"
#include "formtemplatewizardpage.h" #include "formtemplatewizardpage.h"
#include "formwindoweditor.h"
#ifdef CPP_ENABLED #ifdef CPP_ENABLED
# include "cpp/formclasswizard.h" # include "cpp/formclasswizard.h"
@@ -58,6 +57,7 @@
using namespace Core; using namespace Core;
using namespace Designer::Constants; using namespace Designer::Constants;
using namespace Utils;
namespace Designer { namespace Designer {
namespace Internal { namespace Internal {
@@ -141,24 +141,24 @@ void FormEditorPlugin::extensionsInitialized()
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
// Find out current existing editor file // Find out current existing editor file
static QString currentFile() static FilePath currentFile()
{ {
if (const IDocument *document = EditorManager::currentDocument()) { if (const IDocument *document = EditorManager::currentDocument()) {
const QString fileName = document->filePath().toString(); const FilePath filePath = document->filePath();
if (!fileName.isEmpty() && QFileInfo(fileName).isFile()) if (!filePath.isEmpty() && filePath.isFile())
return fileName; return filePath;
} }
return QString(); return {};
} }
// Switch between form ('ui') and source file ('cpp'): // Switch between form ('ui') and source file ('cpp'):
// Find corresponding 'other' file, simply assuming it is in the same directory. // Find corresponding 'other' file, simply assuming it is in the same directory.
static QString otherFile() static FilePath otherFile()
{ {
// Determine mime type of current file. // Determine mime type of current file.
const QString current = currentFile(); const FilePath current = currentFile();
if (current.isEmpty()) if (current.isEmpty())
return QString(); return {};
const Utils::MimeType currentMimeType = Utils::mimeTypeForFile(current); const Utils::MimeType currentMimeType = Utils::mimeTypeForFile(current);
// Determine potential suffixes of candidate files // Determine potential suffixes of candidate files
// 'ui' -> 'cpp', 'cpp/h' -> 'ui'. // 'ui' -> 'cpp', 'cpp/h' -> 'ui'.
@@ -169,22 +169,21 @@ static QString otherFile()
|| currentMimeType.matchesName(CppEditor::Constants::CPP_HEADER_MIMETYPE)) { || currentMimeType.matchesName(CppEditor::Constants::CPP_HEADER_MIMETYPE)) {
candidateSuffixes += Utils::mimeTypeForName(FORM_MIMETYPE).suffixes(); candidateSuffixes += Utils::mimeTypeForName(FORM_MIMETYPE).suffixes();
} else { } else {
return QString(); return {};
} }
// Try to find existing file with desired suffix // Try to find existing file with desired suffix
const QFileInfo currentFI(current); const FilePath currentBaseName = current.parentDir().pathAppended(current.baseName() + '.');
const QString currentBaseName = currentFI.path() + '/' + currentFI.baseName() + '.';
for (const QString &candidateSuffix : qAsConst(candidateSuffixes)) { for (const QString &candidateSuffix : qAsConst(candidateSuffixes)) {
const QFileInfo fi(currentBaseName + candidateSuffix); const FilePath filePath = currentBaseName.stringAppended(candidateSuffix);
if (fi.isFile()) if (filePath.isFile())
return fi.absoluteFilePath(); return filePath.absoluteFilePath();
} }
return QString(); return {};
} }
void FormEditorPlugin::switchSourceForm() void FormEditorPlugin::switchSourceForm()
{ {
const auto fileToOpen = Utils::FilePath::fromString(otherFile()); const FilePath fileToOpen = otherFile();
if (!fileToOpen.isEmpty()) if (!fileToOpen.isEmpty())
EditorManager::openEditor(fileToOpen); EditorManager::openEditor(fileToOpen);
} }