ifcviewer: merge Federation classes into the lib

Move src/ifcviewer-full/Federation.{h,cpp} (and its tests) into
src/ifcviewer/ so the lib stays the single source of truth for the
federation data model.  Restores the original "agnostic lib usable
from ifcviewer-full and ifcviewer-minimal alike" framing.

Drop the unused per-model transform[16] / has_transform field — it
was round-trip-only with no UI to author it, and is being replaced
by an intent-based ModelTransform in the next commit.  No real
.ifcfed in the wild populated this field; old files still load
(unknown JSON keys ignored), they just lose the unused transform.

Replaces the pure-data-model Federation.{h,cpp} that was added a
few commits earlier — that file's structs and compose helpers
return as part of the merged Federation in commit 6.

ifcviewer-full's per-app tests dir is removed (test_federation was
the only one); BUILD_IFCVIEWER_TESTS now wires test_federation in
under src/ifcviewer/tests/, with the Qt6::Core/Gui/Test dependency
declared inline since unlike the other Tier-1 tests it has to pull
Qt in.  All 31 tests pass.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-05-01 14:41:41 +10:00
parent 540f3acf52
commit ecf0a5a4e1
8 changed files with 343 additions and 608 deletions
-4
View File
@@ -34,7 +34,3 @@ set_target_properties(IfcViewerFull PROPERTIES
target_link_libraries(IfcViewerFull PRIVATE IfcViewer)
install(TARGETS IfcViewerFull EXPORT ${IFCOPENSHELL_EXPORT_TARGETS})
if(BUILD_IFCVIEWER_TESTS)
add_subdirectory(tests)
endif()
-308
View File
@@ -1,308 +0,0 @@
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include "Federation.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QSaveFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QUuid>
namespace {
constexpr const char* kSchema = "ifcfed/1";
QString resolvePath(const QString& fed_dir, const QString& stored) {
if (stored.isEmpty()) return stored;
QFileInfo fi(stored);
if (fi.isAbsolute()) return QDir::cleanPath(stored);
return QDir::cleanPath(QDir(fed_dir).absoluteFilePath(stored));
}
// Returns abs_path relative to fed_dir if abs_path lives under fed_dir,
// otherwise returns abs_path unchanged.
QString relativizePath(const QString& fed_dir, const QString& abs_path) {
QString fed_canon = QDir::cleanPath(fed_dir);
QString abs_canon = QDir::cleanPath(abs_path);
if (!fed_canon.endsWith('/')) fed_canon += '/';
if (abs_canon.startsWith(fed_canon)) {
return QDir(fed_canon).relativeFilePath(abs_canon);
}
return abs_canon;
}
} // namespace
Federation::Federation(QObject* parent) : QObject(parent) {}
QString Federation::generateId() {
return QUuid::createUuid().toString(QUuid::WithoutBraces);
}
bool Federation::isFederationPath(const QString& path) {
return path.endsWith(".ifcfed", Qt::CaseInsensitive);
}
void Federation::clear() {
file_path_.clear();
name_.clear();
created_ = QDateTime();
modified_ = QDateTime();
models_.clear();
has_home_view_ = false;
home_view_ = HomeView{};
setDirty(false);
}
void Federation::markClean() {
setDirty(false);
}
void Federation::setDirty(bool d) {
if (dirty_ == d) return;
dirty_ = d;
emit dirtyChanged(d);
}
const Federation::Model* Federation::findById(const QString& fed_id) const {
for (const auto& m : models_) {
if (m.id == fed_id) return &m;
}
return nullptr;
}
QString Federation::addModel(const QString& source_path,
const QString& display_name) {
if (source_path.isEmpty()) return {};
if (isFederationPath(source_path)) return {}; // no nested federations
Model m;
m.id = generateId();
m.display_name = display_name.isEmpty()
? QFileInfo(source_path).fileName()
: display_name;
m.source_kind = "local";
m.source_path = QDir::cleanPath(QFileInfo(source_path).absoluteFilePath());
models_.push_back(std::move(m));
setDirty(true);
return models_.back().id;
}
void Federation::removeModel(const QString& fed_id) {
for (auto it = models_.begin(); it != models_.end(); ++it) {
if (it->id == fed_id) {
models_.erase(it);
setDirty(true);
return;
}
}
}
void Federation::setHomeView(const HomeView& hv) {
home_view_ = hv;
has_home_view_ = true;
setDirty(true);
}
void Federation::clearHomeView() {
if (!has_home_view_) return;
has_home_view_ = false;
home_view_ = HomeView{};
setDirty(true);
}
bool Federation::load(const QString& path,
QStringList* warnings,
QString* err) {
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) {
if (err) *err = QString("Cannot open %1: %2").arg(path, f.errorString());
return false;
}
QByteArray bytes = f.readAll();
f.close();
QJsonParseError pe;
QJsonDocument doc = QJsonDocument::fromJson(bytes, &pe);
if (doc.isNull() || !doc.isObject()) {
if (err) *err = QString("Parse error in %1: %2").arg(path, pe.errorString());
return false;
}
QJsonObject root = doc.object();
clear();
file_path_ = QDir::cleanPath(QFileInfo(path).absoluteFilePath());
QString fed_dir = QFileInfo(file_path_).absolutePath();
QString schema = root.value("schema").toString();
if (schema != kSchema && warnings) {
*warnings << QString("Unknown schema '%1' (expected '%2'); attempting to load anyway.")
.arg(schema, kSchema);
}
name_ = root.value("name").toString();
created_ = QDateTime::fromString(root.value("created").toString(), Qt::ISODate);
modified_ = QDateTime::fromString(root.value("modified").toString(), Qt::ISODate);
QJsonArray arr = root.value("models").toArray();
for (int i = 0; i < arr.size(); ++i) {
if (!arr[i].isObject()) {
if (warnings) *warnings << QString("models[%1] is not an object; skipping.").arg(i);
continue;
}
QJsonObject mo = arr[i].toObject();
Model m;
m.id = mo.value("id").toString();
if (m.id.isEmpty()) m.id = generateId();
m.display_name = mo.value("display_name").toString();
QJsonObject so = mo.value("source").toObject();
m.source_kind = so.value("kind").toString("local");
if (m.source_kind != "local") {
if (warnings)
*warnings << QString("models[%1]: unsupported source kind '%2'; entry kept but not loaded.")
.arg(i).arg(m.source_kind);
// Keep raw stored path so save() round-trips correctly.
m.source_path = so.value("path").toString();
models_.push_back(std::move(m));
continue;
}
QString stored = so.value("path").toString();
if (stored.isEmpty()) {
if (warnings) *warnings << QString("models[%1]: missing source.path; skipping.").arg(i);
continue;
}
m.source_path = resolvePath(fed_dir, stored);
if (m.display_name.isEmpty())
m.display_name = QFileInfo(m.source_path).fileName();
QJsonValue tv = mo.value("transform");
if (tv.isArray()) {
QJsonArray ta = tv.toArray();
if (ta.size() == 16) {
for (int k = 0; k < 16; ++k) m.transform[k] = float(ta[k].toDouble());
m.has_transform = true;
} else if (warnings) {
*warnings << QString("models[%1]: transform must be 16 floats; ignored.").arg(i);
}
}
QJsonValue vv = mo.value("visible");
if (vv.isBool()) m.visible = vv.toBool();
models_.push_back(std::move(m));
}
QJsonValue hv = root.value("home_view");
if (hv.isObject()) {
QJsonObject ho = hv.toObject();
QJsonArray ta = ho.value("target").toArray();
HomeView v;
if (ta.size() == 3) {
v.target = QVector3D(float(ta[0].toDouble()),
float(ta[1].toDouble()),
float(ta[2].toDouble()));
}
v.distance = float(ho.value("distance").toDouble(50.0));
v.yaw = float(ho.value("yaw").toDouble(45.0));
v.pitch = float(ho.value("pitch").toDouble(30.0));
home_view_ = v;
has_home_view_ = true;
}
setDirty(false);
return true;
}
bool Federation::save(const QString& path, QString* err) {
QString abs_path = QDir::cleanPath(QFileInfo(path).absoluteFilePath());
QString fed_dir = QFileInfo(abs_path).absolutePath();
QJsonObject root;
root["schema"] = kSchema;
if (!name_.isEmpty()) root["name"] = name_;
if (!created_.isValid()) created_ = QDateTime::currentDateTimeUtc();
modified_ = QDateTime::currentDateTimeUtc();
root["created"] = created_.toUTC().toString(Qt::ISODate);
root["modified"] = modified_.toUTC().toString(Qt::ISODate);
QJsonArray arr;
for (const auto& m : models_) {
QJsonObject mo;
mo["id"] = m.id;
mo["display_name"] = m.display_name;
QJsonObject so;
so["kind"] = m.source_kind;
if (m.source_kind == "local") {
so["path"] = relativizePath(fed_dir, m.source_path);
} else {
// Round-trip raw value for unsupported kinds.
so["path"] = m.source_path;
}
mo["source"] = so;
if (m.has_transform) {
QJsonArray ta;
for (float v : m.transform) ta.append(double(v));
mo["transform"] = ta;
}
if (!m.visible) mo["visible"] = false;
arr.append(mo);
}
root["models"] = arr;
if (has_home_view_) {
QJsonObject ho;
QJsonArray ta;
ta.append(double(home_view_.target.x()));
ta.append(double(home_view_.target.y()));
ta.append(double(home_view_.target.z()));
ho["target"] = ta;
ho["distance"] = double(home_view_.distance);
ho["yaw"] = double(home_view_.yaw);
ho["pitch"] = double(home_view_.pitch);
root["home_view"] = ho;
} else {
root["home_view"] = QJsonValue(); // null
}
QSaveFile f(abs_path);
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
if (err) *err = QString("Cannot write %1: %2").arg(abs_path, f.errorString());
return false;
}
f.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
if (!f.commit()) {
if (err) *err = QString("Failed to commit %1: %2").arg(abs_path, f.errorString());
return false;
}
file_path_ = abs_path;
setDirty(false);
return true;
}
-105
View File
@@ -1,105 +0,0 @@
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef FEDERATION_H
#define FEDERATION_H
#include <QObject>
#include <QString>
#include <QStringList>
#include <QDateTime>
#include <QVector3D>
#include <array>
#include <vector>
// In-memory representation of an .ifcfed file (IFC federation).
//
// A federation is a named, ordered list of model sources plus an optional
// "home view" camera state. Source paths can be relative (resolved against
// the .ifcfed's directory) or absolute. Save() reserialises paths relative
// when they live under the federation file's directory tree, absolute
// otherwise — Save As recomputes against the new location.
//
// Round-trip-only fields today (no UI to edit, but preserved across load/
// save): per-model `transform` (4x4, column-major), per-model `visible`,
// future cloud `source.kind`s.
class Federation : public QObject {
Q_OBJECT
public:
struct HomeView {
QVector3D target;
float distance = 50.0f;
float yaw = 45.0f; // degrees
float pitch = 30.0f; // degrees
};
struct Model {
QString id; // stable, persisted
QString display_name;
QString source_kind = "local"; // future: "http", "speckle", ...
QString source_path; // resolved absolute when kind == "local"
bool has_transform = false;
std::array<float, 16> transform{}; // column-major; identity when !has_transform
bool visible = true;
};
explicit Federation(QObject* parent = nullptr);
// Round-trip
bool load(const QString& path, QStringList* warnings, QString* err);
bool save(const QString& path, QString* err);
// Mutations
void clear();
QString addModel(const QString& source_path,
const QString& display_name = QString());
void removeModel(const QString& fed_id);
void setHomeView(const HomeView& hv);
void clearHomeView();
// Accessors
const std::vector<Model>& models() const { return models_; }
const Model* findById(const QString& fed_id) const;
bool isDirty() const { return dirty_; }
void markClean();
QString filePath() const { return file_path_; }
QString name() const { return name_; }
bool hasHomeView() const { return has_home_view_; }
const HomeView& homeView() const { return home_view_; }
signals:
void dirtyChanged(bool dirty);
private:
void setDirty(bool d);
static QString generateId();
static bool isFederationPath(const QString& path);
QString file_path_;
QString name_;
QDateTime created_;
QDateTime modified_;
std::vector<Model> models_;
bool has_home_view_ = false;
HomeView home_view_;
bool dirty_ = false;
};
#endif // FEDERATION_H
-41
View File
@@ -1,41 +0,0 @@
################################################################################
# #
# This file is part of IfcOpenShell. #
# #
# IfcOpenShell is free software: you can redistribute it and/or modify #
# it under the terms of the Lesser GNU General Public License as published by #
# the Free Software Foundation, either version 3.0 of the License, or #
# (at your option) any later version. #
# #
# IfcOpenShell is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# Lesser GNU General Public License for more details. #
# #
# You should have received a copy of the Lesser GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################
# Tier-1 tests for ifcviewer-full. Federation is QObject-derived but only
# uses Qt6::Core (no event loop, no GL), so tests can construct it directly.
set(IFCVIEWER_FULL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/..)
# Federation::HomeView holds a QVector3D (defined in QtGui), and QSignalSpy
# / QTest live in Qt6::Test.
find_package(Qt${QT_VERSION} COMPONENTS Core Gui Test REQUIRED PATHS ${QT_DIR})
add_executable(test_federation
test_federation.cpp
${IFCVIEWER_FULL_SRC}/Federation.cpp
)
set_target_properties(test_federation PROPERTIES AUTOMOC ON)
target_include_directories(test_federation PRIVATE ${IFCVIEWER_FULL_SRC})
target_link_libraries(test_federation PRIVATE
Catch2::Catch2WithMain
Qt${QT_VERSION}::Core
Qt${QT_VERSION}::Gui # Federation::HomeView uses QVector3D from QtGui
Qt${QT_VERSION}::Test # QSignalSpy
)
catch_discover_tests(test_federation)
@@ -1,306 +0,0 @@
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include "Federation.h"
#include <catch2/catch_test_macros.hpp>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSignalSpy>
#include <QTemporaryDir>
#include <QVector3D>
#include <atomic>
namespace {
// Catch2 owns main(), so QCoreApplication can't live in a TU constructor.
// Lazily construct it (intentionally leaked) the first time any test asks.
void ensureQApp() {
if (QCoreApplication::instance()) return;
static int argc = 1;
static char arg0[] = "test_federation";
static char* argv[] = { arg0, nullptr };
new QCoreApplication(argc, argv);
}
QString writeStubFile(const QString& path) {
// Federation::addModel cleanPath()s + absolutePath()s; the file doesn't
// need to exist to be added, but for some tests we want a real path under
// a temp dir so QFileInfo gives a stable answer.
QFileInfo fi(path);
QDir().mkpath(fi.absolutePath());
QFile f(path);
REQUIRE(f.open(QIODevice::WriteOnly));
f.write("stub");
f.close();
return QDir::cleanPath(fi.absoluteFilePath());
}
QJsonObject readJsonFile(const QString& path) {
QFile f(path);
REQUIRE(f.open(QIODevice::ReadOnly));
QJsonDocument doc = QJsonDocument::fromJson(f.readAll());
REQUIRE(doc.isObject());
return doc.object();
}
} // namespace
TEST_CASE("Federation starts empty and not dirty", "[federation]") {
ensureQApp();
Federation fed;
REQUIRE(fed.models().empty());
REQUIRE_FALSE(fed.isDirty());
REQUIRE_FALSE(fed.hasHomeView());
REQUIRE(fed.filePath().isEmpty());
}
TEST_CASE("addModel emits dirty=true; markClean clears it; remove re-dirties", "[federation]") {
ensureQApp();
QTemporaryDir tmp;
REQUIRE(tmp.isValid());
Federation fed;
QSignalSpy spy(&fed, &Federation::dirtyChanged);
QString abs = writeStubFile(tmp.filePath("a.ifc"));
QString id = fed.addModel(abs);
REQUIRE_FALSE(id.isEmpty());
REQUIRE(fed.isDirty());
REQUIRE(spy.count() == 1);
REQUIRE(spy.takeFirst().at(0).toBool() == true);
fed.markClean();
REQUIRE_FALSE(fed.isDirty());
REQUIRE(spy.count() == 1);
REQUIRE(spy.takeFirst().at(0).toBool() == false);
fed.removeModel(id);
REQUIRE(fed.isDirty());
REQUIRE(spy.count() == 1);
REQUIRE(spy.takeFirst().at(0).toBool() == true);
}
TEST_CASE("addModel rejects empty paths and nested .ifcfed sources", "[federation]") {
ensureQApp();
Federation fed;
REQUIRE(fed.addModel("").isEmpty());
REQUIRE(fed.addModel("nested.ifcfed").isEmpty());
REQUIRE(fed.addModel("nested.IfcFed").isEmpty()); // case-insensitive
REQUIRE(fed.models().empty());
REQUIRE_FALSE(fed.isDirty());
}
TEST_CASE("setHomeView / clearHomeView toggle dirty + has_home_view", "[federation]") {
ensureQApp();
Federation fed;
QSignalSpy spy(&fed, &Federation::dirtyChanged);
Federation::HomeView hv;
hv.target = QVector3D(1, 2, 3);
hv.distance = 12.5f;
hv.yaw = 33.0f;
hv.pitch = 22.0f;
fed.setHomeView(hv);
REQUIRE(fed.hasHomeView());
REQUIRE(fed.isDirty());
REQUIRE(spy.count() == 1);
fed.markClean();
spy.clear();
fed.clearHomeView();
REQUIRE_FALSE(fed.hasHomeView());
REQUIRE(fed.isDirty());
REQUIRE(spy.count() == 1);
// Idempotent when already cleared.
fed.markClean();
spy.clear();
fed.clearHomeView();
REQUIRE_FALSE(fed.isDirty());
REQUIRE(spy.count() == 0);
}
TEST_CASE("save then load round-trips models, transform, visibility, home view", "[federation]") {
ensureQApp();
QTemporaryDir tmp;
REQUIRE(tmp.isValid());
QString src1 = writeStubFile(tmp.filePath("models/wall.ifc"));
QString src2 = writeStubFile(tmp.filePath("models/slab.ifc"));
QString fed_path = tmp.filePath("project.ifcfed");
Federation src;
QString id1 = src.addModel(src1, "Wall");
QString id2 = src.addModel(src2); // default display_name from filename
REQUIRE_FALSE(id1.isEmpty());
REQUIRE_FALSE(id2.isEmpty());
Federation::HomeView hv;
hv.target = QVector3D(10, 20, 30);
hv.distance = 77.0f;
hv.yaw = 11.0f;
hv.pitch = 7.0f;
src.setHomeView(hv);
QString err;
REQUIRE(src.save(fed_path, &err));
REQUIRE(err.isEmpty());
REQUIRE_FALSE(src.isDirty());
REQUIRE(QFileInfo::exists(fed_path));
Federation dst;
QStringList warnings;
REQUIRE(dst.load(fed_path, &warnings, &err));
REQUIRE(err.isEmpty());
REQUIRE(warnings.isEmpty());
REQUIRE(dst.models().size() == 2);
REQUIRE(dst.models()[0].id == id1);
REQUIRE(dst.models()[0].display_name == "Wall");
REQUIRE(dst.models()[0].source_path == src1);
REQUIRE(dst.models()[1].id == id2);
REQUIRE(dst.models()[1].display_name == "slab.ifc");
REQUIRE(dst.models()[1].source_path == src2);
REQUIRE(dst.hasHomeView());
REQUIRE(dst.homeView().target == QVector3D(10, 20, 30));
REQUIRE(dst.homeView().distance == 77.0f);
REQUIRE(dst.homeView().yaw == 11.0f);
REQUIRE(dst.homeView().pitch == 7.0f);
REQUIRE_FALSE(dst.isDirty());
REQUIRE(QFileInfo(dst.filePath()) == QFileInfo(fed_path));
}
TEST_CASE("save stores paths relative when under fed_dir, absolute otherwise", "[federation]") {
ensureQApp();
QTemporaryDir root;
REQUIRE(root.isValid());
// Layout:
// <root>/fed_root/project.ifcfed
// <root>/fed_root/sub/inside.ifc (under fed_dir)
// <root>/elsewhere/outside.ifc (not under fed_dir)
QString fed_dir = root.filePath("fed_root");
QDir().mkpath(fed_dir);
QString fed_path = fed_dir + "/project.ifcfed";
QString inside = writeStubFile(fed_dir + "/sub/inside.ifc");
QString outside = writeStubFile(root.filePath("elsewhere/outside.ifc"));
Federation fed;
fed.addModel(inside);
fed.addModel(outside);
QString err;
REQUIRE(fed.save(fed_path, &err));
QJsonObject root_obj = readJsonFile(fed_path);
QJsonArray models = root_obj.value("models").toArray();
REQUIRE(models.size() == 2);
QString stored_inside = models[0].toObject().value("source").toObject()
.value("path").toString();
QString stored_outside = models[1].toObject().value("source").toObject()
.value("path").toString();
REQUIRE_FALSE(QFileInfo(stored_inside).isAbsolute());
REQUIRE(stored_inside == "sub/inside.ifc");
REQUIRE(QFileInfo(stored_outside).isAbsolute());
REQUIRE(QDir::cleanPath(stored_outside) == outside);
// Reload: source_path is resolved back to absolute either way.
Federation reload;
QStringList warnings;
REQUIRE(reload.load(fed_path, &warnings, &err));
REQUIRE(reload.models()[0].source_path == inside);
REQUIRE(reload.models()[1].source_path == outside);
}
TEST_CASE("Save-As to a different directory recomputes path relativity", "[federation]") {
ensureQApp();
QTemporaryDir root;
REQUIRE(root.isValid());
// Original layout: source lives under fed_root, fed file under fed_root.
QString fed_dir_a = root.filePath("fed_a");
QString fed_dir_b = root.filePath("fed_b");
QDir().mkpath(fed_dir_a);
QDir().mkpath(fed_dir_b);
QString src = writeStubFile(fed_dir_a + "/sub/m.ifc");
QString fed_a = fed_dir_a + "/proj.ifcfed";
QString fed_b = fed_dir_b + "/proj.ifcfed";
Federation fed;
fed.addModel(src);
QString err;
REQUIRE(fed.save(fed_a, &err));
QString stored_a = readJsonFile(fed_a).value("models").toArray()[0]
.toObject().value("source").toObject()
.value("path").toString();
REQUIRE_FALSE(QFileInfo(stored_a).isAbsolute());
// Save-As under a sibling directory: source is no longer under fed_dir,
// so it must be stored as absolute.
REQUIRE(fed.save(fed_b, &err));
QString stored_b = readJsonFile(fed_b).value("models").toArray()[0]
.toObject().value("source").toObject()
.value("path").toString();
REQUIRE(QFileInfo(stored_b).isAbsolute());
REQUIRE(QDir::cleanPath(stored_b) == src);
// After Save-As, filePath() reflects the new location.
REQUIRE(QFileInfo(fed.filePath()) == QFileInfo(fed_b));
}
TEST_CASE("load on a missing file fails with an error and does not crash", "[federation]") {
ensureQApp();
Federation fed;
QStringList warnings;
QString err;
REQUIRE_FALSE(fed.load("/this/path/does/not/exist.ifcfed", &warnings, &err));
REQUIRE_FALSE(err.isEmpty());
}
TEST_CASE("load on malformed JSON fails with an error", "[federation]") {
ensureQApp();
QTemporaryDir tmp;
REQUIRE(tmp.isValid());
QString bad = tmp.filePath("bad.ifcfed");
{
QFile f(bad);
REQUIRE(f.open(QIODevice::WriteOnly));
f.write("{ this is not json");
f.close();
}
Federation fed;
QStringList warnings;
QString err;
REQUIRE_FALSE(fed.load(bad, &warnings, &err));
REQUIRE_FALSE(err.isEmpty());
}