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
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
#include "qmlmainfileaspect.h"
|
|
|
|
|
|
|
|
|
|
#include "qmlproject.h"
|
|
|
|
|
#include "qmlprojectmanagerconstants.h"
|
2023-01-19 15:36:40 +01:00
|
|
|
#include "qmlprojectmanagertr.h"
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
#include <qmljstools/qmljstoolsconstants.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
|
|
|
|
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2022-06-22 15:43:33 +02:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2020-01-06 14:48:51 +01:00
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
2022-05-17 14:59:01 +02:00
|
|
|
#include <utils/algorithm.h>
|
2022-04-08 11:35:54 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.h>
|
2022-07-27 12:38:07 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
#include <QComboBox>
|
|
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace ProjectExplorer;
|
2022-04-08 11:35:54 +02:00
|
|
|
using namespace Utils;
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
namespace QmlProjectManager {
|
|
|
|
|
|
|
|
|
|
const char M_CURRENT_FILE[] = "CurrentFile";
|
2023-02-10 16:15:49 +01:00
|
|
|
const char CURRENT_FILE[] = QT_TRANSLATE_NOOP("QtC::QmlProjectManager", "<Current File>");
|
2020-01-06 14:48:51 +01:00
|
|
|
|
2023-01-06 09:47:53 +01:00
|
|
|
static bool caseInsensitiveLessThan(const FilePath &s1, const FilePath &s2)
|
2020-01-06 14:48:51 +01:00
|
|
|
{
|
2023-01-06 09:47:53 +01:00
|
|
|
return s1.toString().toCaseFolded() < s2.toString().toCaseFolded();
|
2020-01-06 14:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-13 12:09:31 +02:00
|
|
|
QmlMainFileAspect::QmlMainFileAspect(AspectContainer *container)
|
|
|
|
|
: BaseAspect(container)
|
2020-01-06 14:48:51 +01:00
|
|
|
, m_scriptFile(M_CURRENT_FILE)
|
|
|
|
|
{
|
2022-04-08 11:35:54 +02:00
|
|
|
addDataExtractor(this, &QmlMainFileAspect::mainScript, &Data::mainScript);
|
|
|
|
|
addDataExtractor(this, &QmlMainFileAspect::currentFile, &Data::currentFile);
|
|
|
|
|
|
2020-01-06 14:48:51 +01:00
|
|
|
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
|
|
|
|
|
this, &QmlMainFileAspect::changeCurrentFile);
|
|
|
|
|
connect(EditorManager::instance(), &EditorManager::currentDocumentStateChanged,
|
|
|
|
|
this, [this] { changeCurrentFile(); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlMainFileAspect::~QmlMainFileAspect()
|
|
|
|
|
{
|
|
|
|
|
delete m_fileListCombo;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 17:20:57 +02:00
|
|
|
void QmlMainFileAspect::addToLayout(Layouting::LayoutItem &parent)
|
2020-01-06 14:48:51 +01:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!m_fileListCombo, delete m_fileListCombo);
|
|
|
|
|
m_fileListCombo = new QComboBox;
|
|
|
|
|
m_fileListCombo->setModel(&m_fileListModel);
|
|
|
|
|
|
|
|
|
|
updateFileComboBox();
|
|
|
|
|
|
|
|
|
|
connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::fileListChanged,
|
|
|
|
|
this, &QmlMainFileAspect::updateFileComboBox);
|
2022-07-20 00:35:01 +02:00
|
|
|
connect(m_fileListCombo, &QComboBox::activated, this, &QmlMainFileAspect::setMainScript);
|
2020-01-06 14:48:51 +01:00
|
|
|
|
2023-05-02 17:20:57 +02:00
|
|
|
parent.addItems({Tr::tr("Main QML file:"), m_fileListCombo.data()});
|
2020-01-06 14:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-23 16:11:48 +02:00
|
|
|
void QmlMainFileAspect::toMap(Storage &map) const
|
2020-01-06 14:48:51 +01:00
|
|
|
{
|
|
|
|
|
map.insert(Constants::QML_MAINSCRIPT_KEY, m_scriptFile);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 16:11:48 +02:00
|
|
|
void QmlMainFileAspect::fromMap(const Storage &map)
|
2020-01-06 14:48:51 +01:00
|
|
|
{
|
2020-02-11 11:18:18 +01:00
|
|
|
m_scriptFile = map.value(Constants::QML_MAINSCRIPT_KEY, M_CURRENT_FILE).toString();
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
if (m_scriptFile == M_CURRENT_FILE)
|
|
|
|
|
setScriptSource(FileInEditor);
|
|
|
|
|
else if (m_scriptFile.isEmpty())
|
|
|
|
|
setScriptSource(FileInProjectFile);
|
|
|
|
|
else
|
|
|
|
|
setScriptSource(FileInSettings, m_scriptFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlMainFileAspect::updateFileComboBox()
|
|
|
|
|
{
|
2023-01-06 09:47:53 +01:00
|
|
|
const FilePath projectDir = m_target->project()->projectDirectory();
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
if (mainScriptSource() == FileInProjectFile) {
|
2023-01-06 09:47:53 +01:00
|
|
|
const FilePath mainScriptInFilePath = projectDir.relativeChildPath(mainScript());
|
2020-01-06 14:48:51 +01:00
|
|
|
m_fileListModel.clear();
|
2023-01-06 09:47:53 +01:00
|
|
|
m_fileListModel.appendRow(new QStandardItem(mainScriptInFilePath.toString()));
|
2020-01-06 14:48:51 +01:00
|
|
|
if (m_fileListCombo)
|
|
|
|
|
m_fileListCombo->setEnabled(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_fileListCombo)
|
|
|
|
|
m_fileListCombo->setEnabled(true);
|
|
|
|
|
m_fileListModel.clear();
|
|
|
|
|
m_fileListModel.appendRow(new QStandardItem(CURRENT_FILE));
|
|
|
|
|
QModelIndex currentIndex;
|
|
|
|
|
|
2023-01-06 09:47:53 +01:00
|
|
|
FilePaths sortedFiles = m_target->project()->files(Project::SourceFiles);
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
// make paths relative to project directory
|
2023-01-06 09:47:53 +01:00
|
|
|
FilePaths relativeFiles;
|
|
|
|
|
for (const FilePath &fn : std::as_const(sortedFiles))
|
|
|
|
|
relativeFiles += projectDir.relativeChildPath(fn);
|
2020-01-06 14:48:51 +01:00
|
|
|
sortedFiles = relativeFiles;
|
|
|
|
|
|
|
|
|
|
std::stable_sort(sortedFiles.begin(), sortedFiles.end(), caseInsensitiveLessThan);
|
|
|
|
|
|
2023-01-06 09:47:53 +01:00
|
|
|
FilePath mainScriptPath;
|
2020-01-06 14:48:51 +01:00
|
|
|
if (mainScriptSource() != FileInEditor)
|
2023-01-06 09:47:53 +01:00
|
|
|
mainScriptPath = projectDir.relativeChildPath(mainScript());
|
2020-01-06 14:48:51 +01:00
|
|
|
|
2023-01-06 09:47:53 +01:00
|
|
|
for (const FilePath &fn : std::as_const(sortedFiles)) {
|
|
|
|
|
if (fn.suffixView() != u"qml")
|
2020-01-06 14:48:51 +01:00
|
|
|
continue;
|
|
|
|
|
|
2023-01-06 09:47:53 +01:00
|
|
|
auto item = new QStandardItem(fn.toString());
|
2020-01-06 14:48:51 +01:00
|
|
|
m_fileListModel.appendRow(item);
|
|
|
|
|
|
|
|
|
|
if (mainScriptPath == fn)
|
|
|
|
|
currentIndex = item->index();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_fileListCombo) {
|
|
|
|
|
if (currentIndex.isValid())
|
|
|
|
|
m_fileListCombo->setCurrentIndex(currentIndex.row());
|
|
|
|
|
else
|
|
|
|
|
m_fileListCombo->setCurrentIndex(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlMainFileAspect::MainScriptSource QmlMainFileAspect::mainScriptSource() const
|
|
|
|
|
{
|
|
|
|
|
if (!qmlBuildSystem()->mainFile().isEmpty())
|
|
|
|
|
return FileInProjectFile;
|
|
|
|
|
if (!m_mainScriptFilename.isEmpty())
|
|
|
|
|
return FileInSettings;
|
|
|
|
|
return FileInEditor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlMainFileAspect::setMainScript(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index == 0) {
|
|
|
|
|
setScriptSource(FileInEditor);
|
|
|
|
|
} else {
|
|
|
|
|
const QString path = m_fileListModel.data(m_fileListModel.index(index, 0)).toString();
|
|
|
|
|
setScriptSource(FileInSettings, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 12:09:31 +02:00
|
|
|
void QmlMainFileAspect::setTarget(ProjectExplorer::Target *target)
|
|
|
|
|
{
|
|
|
|
|
m_target = target;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 14:48:51 +01:00
|
|
|
void QmlMainFileAspect::setScriptSource(MainScriptSource source, const QString &settingsPath)
|
|
|
|
|
{
|
|
|
|
|
if (source == FileInEditor) {
|
|
|
|
|
m_scriptFile = M_CURRENT_FILE;
|
|
|
|
|
m_mainScriptFilename.clear();
|
|
|
|
|
} else if (source == FileInProjectFile) {
|
|
|
|
|
m_scriptFile.clear();
|
|
|
|
|
m_mainScriptFilename.clear();
|
|
|
|
|
} else { // FileInSettings
|
|
|
|
|
m_scriptFile = settingsPath;
|
2023-01-06 09:47:53 +01:00
|
|
|
m_mainScriptFilename = m_target->project()->projectDirectory() / m_scriptFile;
|
2020-01-06 14:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit changed();
|
|
|
|
|
updateFileComboBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
Returns absolute path to main script file.
|
|
|
|
|
*/
|
2023-01-06 09:47:53 +01:00
|
|
|
FilePath QmlMainFileAspect::mainScript() const
|
2020-01-06 14:48:51 +01:00
|
|
|
{
|
|
|
|
|
if (!qmlBuildSystem()->mainFile().isEmpty()) {
|
2023-02-13 00:49:09 +01:00
|
|
|
const FilePath pathInProject = qmlBuildSystem()->mainFilePath();
|
2023-01-06 09:47:53 +01:00
|
|
|
return qmlBuildSystem()->canonicalProjectDir().resolvePath(pathInProject);
|
2020-01-06 14:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_mainScriptFilename.isEmpty())
|
|
|
|
|
return m_mainScriptFilename;
|
|
|
|
|
|
|
|
|
|
return m_currentFileFilename;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 09:47:53 +01:00
|
|
|
FilePath QmlMainFileAspect::currentFile() const
|
2019-12-10 16:25:41 +01:00
|
|
|
{
|
|
|
|
|
return m_currentFileFilename;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 14:48:51 +01:00
|
|
|
void QmlMainFileAspect::changeCurrentFile(Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
if (!editor)
|
|
|
|
|
editor = EditorManager::currentEditor();
|
|
|
|
|
|
|
|
|
|
if (editor)
|
2023-01-06 09:47:53 +01:00
|
|
|
m_currentFileFilename = editor->document()->filePath();
|
2020-01-06 14:48:51 +01:00
|
|
|
|
|
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlMainFileAspect::isQmlFilePresent()
|
|
|
|
|
{
|
|
|
|
|
bool qmlFileFound = false;
|
2023-06-06 23:29:16 +02:00
|
|
|
if (mainScriptSource() == FileInEditor && !mainScript().isEmpty()) {
|
2020-01-06 14:48:51 +01:00
|
|
|
IDocument *document = EditorManager::currentDocument();
|
2022-06-14 16:28:46 +02:00
|
|
|
const MimeType mainScriptMimeType = mimeTypeForFile(mainScript());
|
2020-01-06 14:48:51 +01:00
|
|
|
if (document) {
|
2023-01-06 09:47:53 +01:00
|
|
|
m_currentFileFilename = document->filePath();
|
2020-01-06 14:48:51 +01:00
|
|
|
if (mainScriptMimeType.matchesName(ProjectExplorer::Constants::QML_MIMETYPE)
|
|
|
|
|
|| mainScriptMimeType.matchesName(ProjectExplorer::Constants::QMLUI_MIMETYPE)) {
|
|
|
|
|
qmlFileFound = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!document
|
|
|
|
|
|| mainScriptMimeType.matchesName(QmlJSTools::Constants::QMLPROJECT_MIMETYPE)) {
|
|
|
|
|
// find a qml file with lowercase filename. This is slow, but only done
|
|
|
|
|
// in initialization/other border cases.
|
2023-01-06 09:47:53 +01:00
|
|
|
const FilePaths files = m_target->project()->files(Project::SourceFiles);
|
2022-06-14 16:28:46 +02:00
|
|
|
for (const FilePath &filename : files) {
|
|
|
|
|
if (!filename.isEmpty() && filename.baseName().at(0).isLower()) {
|
|
|
|
|
const MimeType type = mimeTypeForFile(filename);
|
2020-01-06 14:48:51 +01:00
|
|
|
if (type.matchesName(ProjectExplorer::Constants::QML_MIMETYPE)
|
|
|
|
|
|| type.matchesName(ProjectExplorer::Constants::QMLUI_MIMETYPE)) {
|
2023-01-06 09:47:53 +01:00
|
|
|
m_currentFileFilename = filename;
|
2020-01-06 14:48:51 +01:00
|
|
|
qmlFileFound = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { // use default one
|
|
|
|
|
qmlFileFound = !mainScript().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
return qmlFileFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlBuildSystem *QmlMainFileAspect::qmlBuildSystem() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<QmlBuildSystem *>(m_target->buildSystem());
|
|
|
|
|
}
|
2023-01-06 09:47:53 +01:00
|
|
|
|
|
|
|
|
} // QmlProjectManager
|