Commit b5646d35 authored by Kai Koehne's avatar Kai Koehne
Browse files

Don't show error message if file added to qmlproject is picked up

Check for the case that a new file to be added to a .qmlproject
is also automatically covered e.g. by a wildcard filter. In this
case we don't have to do anything.
parent 24a225de
......@@ -89,6 +89,47 @@ QStringList FileFilterBaseItem::files() const
return m_files.toList();
}
/**
Check whether filter matches a file path - regardless whether the file already exists or not.
@param filePath: absolute file path
*/
bool FileFilterBaseItem::matchesFile(const QString &filePath) const
{
foreach (const QString &explicitFile, m_explicitFiles) {
if (absolutePath(explicitFile) == filePath)
return true;
}
bool regexMatches = false;
const QString &fileName = QFileInfo(filePath).fileName();
foreach (const QRegExp &exp, m_regExpList) {
if (exp.exactMatch(fileName)) {
regexMatches = true;
break;
}
}
if (!regexMatches)
return false;
const QStringList watchedDirectories = m_fsWatcher.directories();
const QDir fileDir = QFileInfo(filePath).absoluteDir();
foreach (const QString &watchedDirectory, watchedDirectories) {
if (QDir(watchedDirectory) == fileDir)
return true;
}
return false;
}
QString FileFilterBaseItem::absolutePath(const QString &path) const
{
if (QFileInfo(path).isAbsolute())
return path;
return QDir(absoluteDir()).absoluteFilePath(path);
}
QString FileFilterBaseItem::absoluteDir() const
{
QString absoluteDir;
......@@ -110,11 +151,7 @@ void FileFilterBaseItem::updateFileList()
QSet<QString> dirsToBeWatched;
QSet<QString> newFiles;
foreach (const QString &explicitPath, m_explicitFiles) {
if (QFileInfo(explicitPath).isAbsolute()) {
newFiles << explicitPath;
} else {
newFiles << QDir(projectDir).absoluteFilePath(explicitPath);
}
newFiles << absolutePath(explicitPath);
}
if (!m_regExpList.isEmpty() && m_explicitFiles.isEmpty())
newFiles += filesInSubTree(QDir(m_defaultDir), QDir(projectDir), &dirsToBeWatched);
......
......@@ -38,6 +38,7 @@ public:
void setPathsProperty(const QString &path);
virtual QStringList files() const;
bool matchesFile(const QString &filePath) const;
signals:
void directoryChanged();
......@@ -50,6 +51,7 @@ private slots:
void updateFileList();
private:
QString absolutePath(const QString &path) const;
QString absoluteDir() const;
QSet<QString> filesInSubTree(const QDir &rootDir, const QDir &dir, QSet<QString> *parsedDirs = 0);
......
......@@ -115,6 +115,26 @@ QStringList QmlProjectItem::files() const
return files;
}
/**
Check whether the project would include a file path
- regardless whether the file already exists or not.
@param filePath: absolute file path to check
*/
bool QmlProjectItem::matchesFile(const QString &filePath) const
{
const Q_D(QmlProjectItem);
for (int i = 0; i < d->content.size(); ++i) {
QmlProjectContentItem *contentElement = d->content.at(i);
FileFilterBaseItem *fileFilter = qobject_cast<FileFilterBaseItem*>(contentElement);
if (fileFilter) {
if (fileFilter->matchesFile(filePath))
return true;
}
}
return false;
}
} // namespace QmlProjectManager
QML_DEFINE_NOCREATE_TYPE(QmlProjectManager::QmlProjectContentItem)
......
......@@ -40,6 +40,7 @@ public:
void setLibraryPaths(const QStringList &paths);
QStringList files() const;
bool matchesFile(const QString &filePath) const;
signals:
void qmlFilesChanged();
......
......@@ -242,6 +242,16 @@ QStringList QmlProject::libraryPaths() const
return libraryPaths;
}
bool QmlProject::addFiles(const QStringList &filePaths)
{
QStringList toAdd;
foreach (const QString &filePath, filePaths) {
if (!m_projectItem.data()->matchesFile(filePath))
toAdd << filePaths;
}
return toAdd.isEmpty();
}
void QmlProject::refreshProjectFile()
{
refresh(QmlProject::ProjectFile | Files);
......
......@@ -99,6 +99,8 @@ public:
QStringList files() const;
QStringList libraryPaths() const;
bool addFiles(const QStringList &filePaths);
private slots:
void refreshProjectFile();
void refreshFiles();
......
......@@ -183,9 +183,9 @@ bool QmlProjectNode::removeSubProjects(const QStringList &proFilePaths)
}
bool QmlProjectNode::addFiles(const ProjectExplorer::FileType /*fileType*/,
const QStringList & /*filePaths*/, QStringList * /*notAdded*/)
const QStringList &filePaths, QStringList * /*notAdded*/)
{
return false;
return m_project->addFiles(filePaths);
}
bool QmlProjectNode::removeFiles(const ProjectExplorer::FileType /*fileType*/,
......
......@@ -15,6 +15,7 @@ public:
private slots:
void testFileFilter();
void testMatchesFile();
void testLibraryPaths();
};
......@@ -205,6 +206,41 @@ void TestProject::testFileFilter()
}
}
void TestProject::testMatchesFile()
{
//
// search for qml files in local directory
//
QString projectFile = QLatin1String(
"import QmlProject 1.0\n"
"Project {\n"
" QmlFiles {"
" recursive: true"
" }"
" JavaScriptFiles {"
" paths: [\"script.js\"]"
" }"
"}\n");
QmlEngine engine;
QmlComponent component(&engine);
component.setData(projectFile.toUtf8(), QUrl());
if (!component.isReady())
qDebug() << component.errorsString();
QVERIFY(component.isReady());
QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
QVERIFY(project);
project->setSourceDirectory(testDataDir);
QVERIFY(project->matchesFile(testDataDir + "/file1.qml"));
QVERIFY(project->matchesFile(testDataDir + "/notyetexistingfile.qml"));
QVERIFY(project->matchesFile(testDataDir + "/subdir/notyetexistingfile.qml"));
QVERIFY(project->matchesFile(testDataDir + "/script.js"));
QVERIFY(!project->matchesFile(testDataDir + "/script.css"));
}
void TestProject::testLibraryPaths()
{
//
......
Supports Markdown
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