Commit c158ae13 authored by Kai Koehne's avatar Kai Koehne

QmlProject: Make sure 'mainFile: "xxx"' is always respected

Prevent user from overriding the file to launch through the run configuration
dialog. The old logic was broken (main file was always the editor), and
arbitrary files can now be previewed conveniently with
Tools->External->Qt Quick->Preview.

Task-number: QTCREATORBUG-6683
Change-Id: Icee1d11e5063ca634e835bb57ffc77bf973ee93f
Reviewed-by: default avatarDaniel Teske <daniel.teske@nokia.com>
parent ed7b5c9c
......@@ -65,7 +65,6 @@ QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent)
m_qtVersionId(-1),
m_scriptFile(M_CURRENT_FILE),
m_projectTarget(parent),
m_usingCurrentFile(true),
m_isEnabled(false)
{
ctor();
......@@ -79,7 +78,6 @@ QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent,
m_scriptFile(source->m_scriptFile),
m_qmlViewerArgs(source->m_qmlViewerArgs),
m_projectTarget(parent),
m_usingCurrentFile(source->m_usingCurrentFile),
m_isEnabled(source->m_isEnabled),
m_userEnvironmentChanges(source->m_userEnvironmentChanges)
{
......@@ -227,13 +225,12 @@ Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() cons
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
if (m_usingCurrentFile) {
return FileInEditor;
}
if (!qmlTarget()->qmlProject()->mainFile().isEmpty())
return FileInProjectFile;
if (!m_mainScriptFilename.isEmpty()) {
return FileInSettings;
}
return FileInProjectFile;
return FileInEditor;
}
/**
......@@ -241,23 +238,18 @@ QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScr
*/
QString QmlProjectRunConfiguration::mainScript() const
{
if (m_usingCurrentFile) {
return m_currentFileFilename;
if (!qmlTarget()->qmlProject()->mainFile().isEmpty()) {
const QString pathInProject = qmlTarget()->qmlProject()->mainFile();
if (QFileInfo(pathInProject).isAbsolute())
return pathInProject;
else
return qmlTarget()->qmlProject()->projectDir().absoluteFilePath(pathInProject);
}
if (!m_mainScriptFilename.isEmpty()) {
if (!m_mainScriptFilename.isEmpty())
return m_mainScriptFilename;
}
const QString path = qmlTarget()->qmlProject()->mainFile();
if (path.isEmpty()) {
return m_currentFileFilename;
}
if (QFileInfo(path).isAbsolute()) {
return path;
} else {
return qmlTarget()->qmlProject()->projectDir().absoluteFilePath(path);
}
return m_currentFileFilename;
}
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
......@@ -266,16 +258,13 @@ void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
if (source == FileInEditor) {
m_scriptFile = M_CURRENT_FILE;
m_mainScriptFilename.clear();
m_usingCurrentFile = true;
} else if (source == FileInProjectFile) {
m_scriptFile.clear();
m_mainScriptFilename.clear();
m_usingCurrentFile = false;
} else { // FileInSettings
m_scriptFile = settingsPath;
m_mainScriptFilename
= qmlTarget()->qmlProject()->projectDir().absoluteFilePath(m_scriptFile);
m_usingCurrentFile = false;
}
updateEnabled();
if (m_configurationWidget)
......@@ -340,7 +329,7 @@ void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
void QmlProjectRunConfiguration::updateEnabled()
{
bool qmlFileFound = false;
if (m_usingCurrentFile) {
if (mainScriptSource() == FileInEditor) {
Core::IEditor *editor = Core::EditorManager::instance()->currentEditor();
Core::MimeDatabase *db = ICore::instance()->mimeDatabase();
if (editor) {
......
......@@ -141,7 +141,6 @@ private:
Internal::QmlProjectTarget *m_projectTarget;
QWeakPointer<Internal::QmlProjectRunConfigurationWidget> m_configurationWidget;
bool m_usingCurrentFile;
bool m_isEnabled;
QList<Utils::EnvironmentItem> m_userEnvironmentChanges;
......
......@@ -179,16 +179,21 @@ void QmlProjectRunConfigurationWidget::updateFileComboBox()
QmlProject *project = m_runConfiguration->qmlTarget()->qmlProject();
QDir projectDir = project->projectDir();
if (m_runConfiguration->mainScriptSource() == QmlProjectRunConfiguration::FileInProjectFile) {
const QString mainScriptInFilePath
= projectDir.relativeFilePath(m_runConfiguration->mainScript());
m_fileListModel->clear();
m_fileListModel->appendRow(new QStandardItem(mainScriptInFilePath));
m_fileListCombo->setEnabled(false);
return;
}
m_fileListCombo->setEnabled(true);
m_fileListModel->clear();
m_fileListModel->appendRow(new QStandardItem(CURRENT_FILE));
QModelIndex currentIndex;
QModelIndex fileInQmlProjectIndex;
const QString mainScriptInFilePath = projectDir.absoluteFilePath(project->mainFile());
QStringList sortedFiles = project->files();
if (!sortedFiles.contains(mainScriptInFilePath))
sortedFiles += mainScriptInFilePath;
// make paths relative to project directory
QStringList relativeFiles;
......@@ -213,9 +218,6 @@ void QmlProjectRunConfigurationWidget::updateFileComboBox()
if (mainScriptPath == fn)
currentIndex = item->index();
if (mainScriptInFilePath == fn)
fileInQmlProjectIndex = item->index();
}
if (currentIndex.isValid()) {
......@@ -223,27 +225,15 @@ void QmlProjectRunConfigurationWidget::updateFileComboBox()
} else {
m_fileListCombo->setCurrentIndex(0);
}
if (fileInQmlProjectIndex.isValid()) {
QFont font;
font.setBold(true);
m_fileListModel->setData(fileInQmlProjectIndex, font, Qt::FontRole);
}
}
void QmlProjectRunConfigurationWidget::setMainScript(int index)
{
QmlProject *project = m_runConfiguration->qmlTarget()->qmlProject();
QDir projectDir = project->projectDir();
if (index == 0) {
m_runConfiguration->setScriptSource(QmlProjectRunConfiguration::FileInEditor);
} else {
const QString path = m_fileListModel->data(m_fileListModel->index(index, 0)).toString();
if (projectDir.relativeFilePath(project->mainFile()) == path) {
m_runConfiguration->setScriptSource(QmlProjectRunConfiguration::FileInProjectFile);
} else {
m_runConfiguration->setScriptSource(QmlProjectRunConfiguration::FileInSettings, path);
}
m_runConfiguration->setScriptSource(QmlProjectRunConfiguration::FileInSettings, path);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment