2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-05-04 12:19:22 +02:00
|
|
|
|
|
|
|
|
#include "qmlprojectnodes.h"
|
|
|
|
|
#include "qmlproject.h"
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2010-04-01 17:21:18 +02:00
|
|
|
#include <coreplugin/fileiconprovider.h>
|
2019-01-18 20:38:48 +01:00
|
|
|
#include <coreplugin/documentmanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/documentmodel.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2009-05-04 12:19:22 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2019-01-18 20:38:48 +01:00
|
|
|
#include <texteditor/textdocument.h>
|
2016-10-06 11:31:06 +02:00
|
|
|
#include <utils/algorithm.h>
|
2019-01-18 20:38:48 +01:00
|
|
|
#include <utils/textfileformat.h>
|
|
|
|
|
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
#include <QTextCodec>
|
2016-10-06 11:31:06 +02:00
|
|
|
|
2017-02-28 14:46:14 +01:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
2010-02-16 13:39:13 +01:00
|
|
|
namespace QmlProjectManager {
|
|
|
|
|
namespace Internal {
|
2009-05-04 12:19:22 +02:00
|
|
|
|
2017-03-01 14:26:46 +01:00
|
|
|
QmlProjectNode::QmlProjectNode(QmlProject *project) : ProjectNode(project->projectDirectory()),
|
|
|
|
|
m_project(project)
|
2009-05-04 12:19:22 +02:00
|
|
|
{
|
2015-10-29 17:18:23 +01:00
|
|
|
setDisplayName(project->projectFilePath().toFileInfo().completeBaseName());
|
2017-09-06 11:32:15 +02:00
|
|
|
|
2017-12-13 15:03:57 +01:00
|
|
|
static QIcon qmlProjectIcon = Core::FileIconProvider::directoryIcon(":/projectexplorer/images/fileoverlay_qml.png");
|
2017-09-06 11:32:15 +02:00
|
|
|
setIcon(qmlProjectIcon);
|
2009-05-04 12:19:22 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-31 15:48:45 +02:00
|
|
|
bool QmlProjectNode::supportsAction(ProjectAction action, const Node *node) const
|
2009-05-04 12:19:22 +02:00
|
|
|
{
|
2017-03-15 15:46:48 +01:00
|
|
|
if (action == AddNewFile || action == EraseFile)
|
|
|
|
|
return true;
|
2017-08-13 16:22:19 +03:00
|
|
|
QTC_ASSERT(node, return false);
|
2017-03-15 15:46:48 +01:00
|
|
|
|
2019-02-28 17:19:18 +01:00
|
|
|
if (action == Rename && node->asFileNode()) {
|
2017-08-13 16:22:19 +03:00
|
|
|
const FileNode *fileNode = node->asFileNode();
|
|
|
|
|
QTC_ASSERT(fileNode, return false);
|
2017-03-15 15:46:48 +01:00
|
|
|
return fileNode->fileType() != FileType::Project;
|
2013-12-02 15:30:17 +01:00
|
|
|
}
|
2017-03-15 15:46:48 +01:00
|
|
|
|
|
|
|
|
return false;
|
2009-05-04 12:19:22 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-01 16:13:48 +02:00
|
|
|
bool QmlProjectNode::addFiles(const QStringList &filePaths, QStringList * /*notAdded*/)
|
2009-05-04 12:19:22 +02:00
|
|
|
{
|
2010-02-17 11:20:35 +01:00
|
|
|
return m_project->addFiles(filePaths);
|
2009-05-04 12:19:22 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-01 16:13:48 +02:00
|
|
|
bool QmlProjectNode::deleteFiles(const QStringList & /*filePaths*/)
|
2010-08-10 16:27:35 +02:00
|
|
|
{
|
2010-09-30 11:16:51 +02:00
|
|
|
return true;
|
2010-08-10 16:27:35 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 20:38:48 +01:00
|
|
|
bool QmlProjectNode::renameFile(const QString & filePath, const QString & newFilePath)
|
2009-05-04 12:19:22 +02:00
|
|
|
{
|
2019-01-18 20:38:48 +01:00
|
|
|
if (filePath.endsWith(m_project->mainFile())) {
|
|
|
|
|
m_project->setMainFile(newFilePath);
|
|
|
|
|
|
|
|
|
|
// make sure to change it also in the qmlproject file
|
|
|
|
|
const QString qmlProjectFilePath = m_project->document()->filePath().toString();
|
|
|
|
|
Core::FileChangeBlocker fileChangeBlocker(qmlProjectFilePath);
|
|
|
|
|
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath);
|
|
|
|
|
TextEditor::TextDocument *document = nullptr;
|
|
|
|
|
if (!editors.isEmpty()) {
|
|
|
|
|
document = qobject_cast<TextEditor::TextDocument*>(editors.first()->document());
|
|
|
|
|
if (document && document->isModified())
|
|
|
|
|
if (!Core::DocumentManager::saveDocument(document))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString fileContent;
|
|
|
|
|
QString error;
|
|
|
|
|
Utils::TextFileFormat textFileFormat;
|
|
|
|
|
const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8
|
|
|
|
|
if (Utils::TextFileFormat::readFile(qmlProjectFilePath, codec, &fileContent, &textFileFormat, &error)
|
|
|
|
|
!= Utils::TextFileFormat::ReadSuccess) {
|
|
|
|
|
qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the mainFile and do the file name with brackets in a capture group and mask the . with \.
|
|
|
|
|
QString originalFileName = QFileInfo(filePath).fileName();
|
|
|
|
|
originalFileName.replace(".", "\\.");
|
|
|
|
|
const QRegularExpression expression(QString("mainFile:\\s*\"(%1)\"").arg(originalFileName));
|
|
|
|
|
const QRegularExpressionMatch match = expression.match(fileContent);
|
|
|
|
|
|
|
|
|
|
fileContent.replace(match.capturedStart(1), match.capturedLength(1), QFileInfo(newFilePath).fileName());
|
|
|
|
|
|
|
|
|
|
if (!textFileFormat.writeFile(qmlProjectFilePath, fileContent, &error))
|
|
|
|
|
qWarning() << "Failed to write file" << qmlProjectFilePath << ":" << error;
|
|
|
|
|
m_project->refresh(QmlProject::Everything);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-30 11:16:51 +02:00
|
|
|
return true;
|
2009-05-04 12:19:22 +02:00
|
|
|
}
|
2010-02-16 13:39:13 +01:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QmlProjectManager
|