src/documentmanager.cpp

changeset 23
3387a84ddaba
parent 17
a5111f4e6412
child 24
1a0faaaceb84
equal deleted inserted replaced
22:6da867fa5429 23:3387a84ddaba
95 return "untitled-" + QString::number(untitledNameCounter); 95 return "untitled-" + QString::number(untitledNameCounter);
96 } 96 }
97 97
98 void DocumentManager::loadDependenciesForModel( 98 void DocumentManager::loadDependenciesForModel(
99 const QString& modelName, 99 const QString& modelName,
100 const QString& path,
100 const LibraryManager& libraries, 101 const LibraryManager& libraries,
101 QTextStream& errorStream) 102 QTextStream& errorStream)
102 { 103 {
103 QStringList missing; 104 QStringList missing;
104 QStringList processed; 105 QStringList processed;
105 loadDependenciesForModel(modelName, libraries, missing, processed, errorStream); 106 loadDependenciesForModel(modelName, path, libraries, missing, processed, errorStream);
106 if (not missing.empty()) 107 if (not missing.empty())
107 { 108 {
108 missing.sort(Qt::CaseInsensitive); 109 missing.sort(Qt::CaseInsensitive);
109 errorStream << utility::format( 110 errorStream << utility::format(
110 "The following files could not be opened: %1", 111 "The following files could not be opened: %1",
111 missing.join(", ")); 112 missing.join(", "));
112 } 113 }
113 } 114 }
114 115
116 static QString findFile(QString referenceName, const QString& path, const LibraryManager& libraries)
117 {
118 // Try to find the file in the same place as the model itself
119 referenceName.replace("\\", "/");
120 const QDir dir = QFileInfo{path}.dir();
121 QString referencedFilePath = dir.filePath(referenceName);
122 if (not QFileInfo{referencedFilePath}.exists())
123 {
124 // Look for it in the libraries
125 referencedFilePath = libraries.findFile(referenceName);
126 }
127 return referencedFilePath;
128 }
129
115 void DocumentManager::loadDependenciesForModel( 130 void DocumentManager::loadDependenciesForModel(
116 const QString& modelName, 131 const QString& modelName,
132 const QString &path,
117 const LibraryManager& libraries, 133 const LibraryManager& libraries,
118 QStringList& missing, 134 QStringList& missing,
119 QStringList& processed, 135 QStringList& processed,
120 QTextStream& errorStream) 136 QTextStream& errorStream)
121 { 137 {
132 and openModels.find(referenceName) == std::end(openModels) 148 and openModels.find(referenceName) == std::end(openModels)
133 and not missing.contains(referenceName)) 149 and not missing.contains(referenceName))
134 { 150 {
135 try 151 try
136 { 152 {
137 const QString path = libraries.findFile(referenceName); 153 const QString referencedFilePath = findFile(referenceName, path, libraries);
138 if (path.isEmpty()) 154 if (referencedFilePath.isEmpty())
139 { 155 {
140 throw LoadingError{utility::format("'%1' was not found.", referenceName)}; 156 throw LoadingError{utility::format("'%1' was not found.", referenceName)};
141 } 157 }
142 QString errorString; 158 QString errorString;
143 QTextStream localErrorStream{&errorString}; 159 QTextStream localErrorStream{&errorString};
144 QString resultName = this->openModel(path, localErrorStream); 160 QString resultName = this->openModel(referencedFilePath, localErrorStream);
145 if (resultName.isEmpty()) 161 if (resultName.isEmpty())
146 { 162 {
147 throw LoadingError{utility::format( 163 throw LoadingError{utility::format(
148 "could not load '%1': %2", 164 "could not load '%1': %2",
149 path, 165 referencedFilePath,
150 errorString)}; 166 errorString)};
151 } 167 }
152 if (not processed.contains(referenceName)) 168 if (not processed.contains(referenceName))
153 { 169 {
154 loadDependenciesForModel(referenceName, libraries, missing, processed, errorStream); 170 loadDependenciesForModel(referenceName, path, libraries, missing, processed, errorStream);
155 } 171 }
156 } 172 }
157 catch(const LoadingError& error) 173 catch(const LoadingError& error)
158 { 174 {
159 errorStream << error.message << "\n"; 175 errorStream << error.message << "\n";

mercurial