2026-04-11 16:30:10 +10:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* 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 "MainWindow.h"
|
2026-04-11 20:05:50 +10:00
|
|
|
#include "AppSettings.h"
|
2026-04-11 16:30:10 +10:00
|
|
|
#include "SettingsWindow.h"
|
|
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QMenuBar>
|
|
|
|
|
#include <QFileDialog>
|
2026-04-11 20:49:39 +10:00
|
|
|
#include <QFileInfo>
|
2026-04-11 16:30:10 +10:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QStatusBar>
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QDockWidget>
|
|
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget* parent)
|
|
|
|
|
: QMainWindow(parent)
|
|
|
|
|
{
|
|
|
|
|
setupUi();
|
|
|
|
|
setupMenus();
|
|
|
|
|
|
2026-04-11 20:05:50 +10:00
|
|
|
connect(viewport_, &ViewportWindow::frameStatsUpdated, this, [this](const ViewportWindow::FrameStats& s) {
|
|
|
|
|
if (!stats_label_->isVisible()) return;
|
|
|
|
|
stats_label_->setText(
|
|
|
|
|
QString("%1 fps | %2 ms | %3/%4 obj | %5/%6 tri")
|
|
|
|
|
.arg(s.fps, 0, 'f', 1)
|
|
|
|
|
.arg(s.frame_time_ms, 0, 'f', 1)
|
|
|
|
|
.arg(s.visible_objects)
|
|
|
|
|
.arg(s.total_objects)
|
|
|
|
|
.arg(s.visible_triangles)
|
|
|
|
|
.arg(s.total_triangles));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(&AppSettings::instance(), &AppSettings::showStatsChanged, this, [this](bool show) {
|
|
|
|
|
stats_label_->setVisible(show);
|
|
|
|
|
if (!show) stats_label_->clear();
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-11 16:30:10 +10:00
|
|
|
connect(&element_poll_timer_, &QTimer::timeout, this, &MainWindow::pollNewElements);
|
|
|
|
|
element_poll_timer_.setInterval(100);
|
|
|
|
|
|
|
|
|
|
setWindowTitle("IfcViewer");
|
|
|
|
|
resize(1400, 900);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MainWindow::~MainWindow() {}
|
|
|
|
|
|
|
|
|
|
void MainWindow::setupUi() {
|
|
|
|
|
// 3D Viewport as central widget
|
|
|
|
|
viewport_ = new ViewportWindow();
|
|
|
|
|
viewport_container_ = QWidget::createWindowContainer(viewport_, this);
|
|
|
|
|
viewport_container_->setMinimumSize(400, 300);
|
|
|
|
|
viewport_container_->setFocusPolicy(Qt::StrongFocus);
|
|
|
|
|
setCentralWidget(viewport_container_);
|
|
|
|
|
|
|
|
|
|
connect(viewport_, &ViewportWindow::objectPicked, this, &MainWindow::onObjectPicked);
|
|
|
|
|
|
|
|
|
|
// Element tree dock
|
|
|
|
|
auto* tree_dock = new QDockWidget("Elements", this);
|
|
|
|
|
tree_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
|
|
|
|
|
element_tree_ = new QTreeWidget();
|
|
|
|
|
element_tree_->setHeaderLabels({"Name", "Type", "GUID"});
|
|
|
|
|
element_tree_->setColumnWidth(0, 200);
|
|
|
|
|
element_tree_->setColumnWidth(1, 120);
|
|
|
|
|
element_tree_->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
connect(element_tree_, &QTreeWidget::itemSelectionChanged, this, &MainWindow::onTreeSelectionChanged);
|
|
|
|
|
tree_dock->setWidget(element_tree_);
|
|
|
|
|
addDockWidget(Qt::LeftDockWidgetArea, tree_dock);
|
|
|
|
|
|
|
|
|
|
// Properties dock
|
|
|
|
|
auto* prop_dock = new QDockWidget("Properties", this);
|
|
|
|
|
prop_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
|
|
|
|
|
property_table_ = new QTableWidget();
|
|
|
|
|
property_table_->setColumnCount(2);
|
|
|
|
|
property_table_->setHorizontalHeaderLabels({"Property", "Value"});
|
|
|
|
|
property_table_->horizontalHeader()->setStretchLastSection(true);
|
|
|
|
|
property_table_->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
|
property_table_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
|
prop_dock->setWidget(property_table_);
|
|
|
|
|
addDockWidget(Qt::RightDockWidgetArea, prop_dock);
|
|
|
|
|
|
|
|
|
|
// Status bar with progress
|
|
|
|
|
progress_bar_ = new QProgressBar();
|
|
|
|
|
progress_bar_->setMaximumWidth(200);
|
|
|
|
|
progress_bar_->setVisible(false);
|
|
|
|
|
status_label_ = new QLabel("Ready");
|
2026-04-11 20:05:50 +10:00
|
|
|
stats_label_ = new QLabel();
|
|
|
|
|
stats_label_->setVisible(AppSettings::instance().showStats());
|
2026-04-11 16:30:10 +10:00
|
|
|
statusBar()->addWidget(status_label_, 1);
|
2026-04-11 20:05:50 +10:00
|
|
|
statusBar()->addPermanentWidget(stats_label_);
|
2026-04-11 16:30:10 +10:00
|
|
|
statusBar()->addPermanentWidget(progress_bar_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::setupMenus() {
|
|
|
|
|
auto* file_menu = menuBar()->addMenu("&File");
|
2026-04-11 20:49:39 +10:00
|
|
|
auto* open_action = file_menu->addAction("&Add Files...", this, &MainWindow::onFileOpen);
|
2026-04-11 16:30:10 +10:00
|
|
|
open_action->setShortcut(QKeySequence::Open);
|
|
|
|
|
file_menu->addAction("&Settings...", this, &MainWindow::onFileSettings);
|
|
|
|
|
file_menu->addSeparator();
|
|
|
|
|
file_menu->addAction("&Quit", QKeySequence::Quit, qApp, &QApplication::quit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onFileOpen() {
|
2026-04-11 20:49:39 +10:00
|
|
|
QStringList paths = QFileDialog::getOpenFileNames(
|
|
|
|
|
this, "Add IFC Files", QString(),
|
|
|
|
|
"IFC Files (*.ifc *.ifcxml *.ifczip);;All Files (*)");
|
|
|
|
|
if (!paths.isEmpty()) {
|
|
|
|
|
addFiles(paths);
|
2026-04-11 16:30:10 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onFileSettings() {
|
|
|
|
|
if (settings_ == nullptr) {
|
|
|
|
|
settings_ = new SettingsWindow(this);
|
|
|
|
|
}
|
|
|
|
|
settings_->open();
|
|
|
|
|
settings_->activateWindow();
|
|
|
|
|
settings_->raise();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 20:49:39 +10:00
|
|
|
void MainWindow::addFiles(const QStringList& paths) {
|
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
|
ModelId id = next_model_id_++;
|
|
|
|
|
|
|
|
|
|
ModelHandle handle;
|
|
|
|
|
handle.id = id;
|
|
|
|
|
handle.file_path = path;
|
|
|
|
|
handle.display_name = QFileInfo(path).fileName();
|
|
|
|
|
handle.streamer = new GeometryStreamer(this);
|
|
|
|
|
|
|
|
|
|
// Create top-level tree item for this model
|
|
|
|
|
auto* root = new QTreeWidgetItem(element_tree_);
|
|
|
|
|
root->setText(0, handle.display_name);
|
|
|
|
|
root->setText(1, "IFC Model");
|
|
|
|
|
root->setData(0, Qt::UserRole, static_cast<uint32_t>(0)); // 0 = not a pickable object
|
|
|
|
|
handle.tree_root = root;
|
|
|
|
|
|
|
|
|
|
models_[id] = handle;
|
|
|
|
|
load_queue_.push_back(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading_model_id_ == 0) {
|
|
|
|
|
startNextLoad();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::connectStreamer(GeometryStreamer* streamer) {
|
|
|
|
|
connect(streamer, &GeometryStreamer::progressChanged,
|
|
|
|
|
this, &MainWindow::onProgressChanged, Qt::QueuedConnection);
|
|
|
|
|
connect(streamer, &GeometryStreamer::elementReady,
|
|
|
|
|
this, &MainWindow::onElementReady, Qt::QueuedConnection);
|
|
|
|
|
connect(streamer, &GeometryStreamer::finished,
|
|
|
|
|
this, &MainWindow::onStreamingFinished, Qt::QueuedConnection);
|
|
|
|
|
connect(streamer, &GeometryStreamer::errorOccurred, this, [this](const QString& msg) {
|
|
|
|
|
QMessageBox::warning(this, "Error", msg);
|
|
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::startNextLoad() {
|
|
|
|
|
if (load_queue_.empty()) {
|
|
|
|
|
loading_model_id_ = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loading_model_id_ = load_queue_.front();
|
|
|
|
|
load_queue_.pop_front();
|
|
|
|
|
|
|
|
|
|
auto& model = models_[loading_model_id_];
|
|
|
|
|
connectStreamer(model.streamer);
|
2026-04-11 16:30:10 +10:00
|
|
|
|
|
|
|
|
progress_bar_->setValue(0);
|
|
|
|
|
progress_bar_->setVisible(true);
|
2026-04-11 20:49:39 +10:00
|
|
|
status_label_->setText("Loading: " + model.display_name);
|
2026-04-11 16:30:10 +10:00
|
|
|
|
|
|
|
|
load_timer_.restart();
|
|
|
|
|
element_poll_timer_.start();
|
2026-04-11 20:49:39 +10:00
|
|
|
model.streamer->loadFile(
|
|
|
|
|
model.file_path.toStdString(), next_object_id_, loading_model_id_);
|
2026-04-11 16:30:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onProgressChanged(int percent) {
|
|
|
|
|
progress_bar_->setValue(percent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onElementReady(UploadChunk chunk) {
|
|
|
|
|
viewport_->uploadChunk(chunk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onStreamingFinished() {
|
|
|
|
|
element_poll_timer_.stop();
|
|
|
|
|
pollNewElements(); // drain remaining
|
|
|
|
|
|
2026-04-11 20:49:39 +10:00
|
|
|
// Update next_object_id_ from the streamer that just finished.
|
|
|
|
|
if (loading_model_id_ != 0) {
|
|
|
|
|
auto it = models_.find(loading_model_id_);
|
|
|
|
|
if (it != models_.end()) {
|
|
|
|
|
next_object_id_ = it->second.streamer->lastObjectId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 16:30:10 +10:00
|
|
|
progress_bar_->setVisible(false);
|
|
|
|
|
|
|
|
|
|
qint64 ms = load_timer_.elapsed();
|
|
|
|
|
QString elapsed = (ms >= 1000)
|
|
|
|
|
? QString::number(ms / 1000.0, 'f', 2) + " s"
|
|
|
|
|
: QString::number(ms) + " ms";
|
2026-04-11 20:49:39 +10:00
|
|
|
|
|
|
|
|
size_t total_elements = element_map_.size();
|
|
|
|
|
size_t num_models = models_.size();
|
|
|
|
|
status_label_->setText(QString("%1 elements across %2 model(s) — last loaded in %3")
|
|
|
|
|
.arg(total_elements)
|
|
|
|
|
.arg(num_models)
|
2026-04-11 16:30:10 +10:00
|
|
|
.arg(elapsed));
|
2026-04-11 20:49:39 +10:00
|
|
|
|
|
|
|
|
// Start next model if queued.
|
|
|
|
|
startNextLoad();
|
2026-04-11 16:30:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onObjectPicked(uint32_t object_id) {
|
|
|
|
|
viewport_->setSelectedObjectId(object_id);
|
|
|
|
|
|
|
|
|
|
// Select in tree
|
|
|
|
|
auto it = tree_items_.find(object_id);
|
|
|
|
|
if (it != tree_items_.end()) {
|
|
|
|
|
element_tree_->blockSignals(true);
|
|
|
|
|
element_tree_->setCurrentItem(it->second);
|
|
|
|
|
element_tree_->blockSignals(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
populateProperties(object_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::onTreeSelectionChanged() {
|
|
|
|
|
auto items = element_tree_->selectedItems();
|
|
|
|
|
if (items.isEmpty()) return;
|
|
|
|
|
|
|
|
|
|
uint32_t object_id = items.first()->data(0, Qt::UserRole).toUInt();
|
|
|
|
|
viewport_->setSelectedObjectId(object_id);
|
|
|
|
|
populateProperties(object_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::pollNewElements() {
|
2026-04-11 20:49:39 +10:00
|
|
|
if (loading_model_id_ == 0) return;
|
|
|
|
|
|
|
|
|
|
auto it = models_.find(loading_model_id_);
|
|
|
|
|
if (it == models_.end()) return;
|
|
|
|
|
|
|
|
|
|
auto& model = it->second;
|
|
|
|
|
auto elements = model.streamer->drainElements();
|
|
|
|
|
|
2026-04-11 16:30:10 +10:00
|
|
|
for (auto& info : elements) {
|
|
|
|
|
element_map_[info.object_id] = info;
|
2026-04-11 20:49:39 +10:00
|
|
|
scoped_ifc_id_to_object_id_[scopedKey(info.model_id, info.ifc_id)] = info.object_id;
|
2026-04-11 16:30:10 +10:00
|
|
|
|
2026-04-11 20:49:39 +10:00
|
|
|
// Find parent tree item (scoped to this model)
|
|
|
|
|
QTreeWidgetItem* parent_item = model.tree_root;
|
|
|
|
|
auto parent_obj_it = scoped_ifc_id_to_object_id_.find(
|
|
|
|
|
scopedKey(info.model_id, info.parent_id));
|
|
|
|
|
if (parent_obj_it != scoped_ifc_id_to_object_id_.end()) {
|
2026-04-11 16:30:10 +10:00
|
|
|
auto tree_it = tree_items_.find(parent_obj_it->second);
|
|
|
|
|
if (tree_it != tree_items_.end()) {
|
|
|
|
|
parent_item = tree_it->second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString display_name = QString::fromStdString(info.name);
|
|
|
|
|
if (display_name.isEmpty()) {
|
|
|
|
|
display_name = QString::fromStdString(info.type) + " #" + QString::number(info.ifc_id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 20:49:39 +10:00
|
|
|
auto* item = new QTreeWidgetItem(parent_item);
|
2026-04-11 16:30:10 +10:00
|
|
|
item->setText(0, display_name);
|
|
|
|
|
item->setText(1, QString::fromStdString(info.type));
|
|
|
|
|
item->setText(2, QString::fromStdString(info.guid));
|
|
|
|
|
item->setData(0, Qt::UserRole, info.object_id);
|
|
|
|
|
|
|
|
|
|
tree_items_[info.object_id] = item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::populateProperties(uint32_t object_id) {
|
|
|
|
|
property_table_->setRowCount(0);
|
|
|
|
|
if (object_id == 0) return;
|
|
|
|
|
|
|
|
|
|
auto it = element_map_.find(object_id);
|
|
|
|
|
if (it == element_map_.end()) return;
|
|
|
|
|
|
|
|
|
|
const auto& info = it->second;
|
|
|
|
|
|
|
|
|
|
auto addRow = [this](const QString& key, const QString& value) {
|
|
|
|
|
int row = property_table_->rowCount();
|
|
|
|
|
property_table_->insertRow(row);
|
|
|
|
|
property_table_->setItem(row, 0, new QTableWidgetItem(key));
|
|
|
|
|
property_table_->setItem(row, 1, new QTableWidgetItem(value));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
addRow("IFC ID", QString::number(info.ifc_id));
|
|
|
|
|
addRow("GUID", QString::fromStdString(info.guid));
|
|
|
|
|
addRow("Name", QString::fromStdString(info.name));
|
|
|
|
|
addRow("Type", QString::fromStdString(info.type));
|
|
|
|
|
|
2026-04-11 20:49:39 +10:00
|
|
|
// Find the correct model's file for property lookup
|
|
|
|
|
auto model_it = models_.find(info.model_id);
|
|
|
|
|
if (model_it == models_.end()) return;
|
|
|
|
|
|
|
|
|
|
auto* file = model_it->second.streamer->ifcFile();
|
2026-04-11 16:30:10 +10:00
|
|
|
if (!file) return;
|
|
|
|
|
|
2026-04-11 19:23:10 +10:00
|
|
|
auto product = file->instance_by_id(info.ifc_id);
|
2026-04-11 16:30:10 +10:00
|
|
|
if (!product) return;
|
|
|
|
|
|
|
|
|
|
// Show all direct attributes
|
2026-04-11 19:23:10 +10:00
|
|
|
auto& decl = product.declaration();
|
2026-04-11 16:30:10 +10:00
|
|
|
if (auto* entity = decl.as_entity()) {
|
|
|
|
|
for (size_t i = 0; i < entity->attribute_count(); ++i) {
|
|
|
|
|
auto* attr = entity->attribute_by_index(i);
|
|
|
|
|
try {
|
2026-04-11 19:23:10 +10:00
|
|
|
auto val = product.get_attribute_value(i);
|
2026-04-11 16:30:10 +10:00
|
|
|
if (!val.isNull()) {
|
|
|
|
|
std::string str_val;
|
|
|
|
|
try {
|
|
|
|
|
str_val = static_cast<std::string>(val);
|
|
|
|
|
} catch (...) {
|
2026-04-11 19:23:10 +10:00
|
|
|
str_val = "<" + std::string(ifcopenshell::argument_type_to_string(val.type())) + ">";
|
2026-04-11 16:30:10 +10:00
|
|
|
}
|
|
|
|
|
addRow(QString::fromStdString(attr->name()), QString::fromStdString(str_val));
|
|
|
|
|
}
|
|
|
|
|
} catch (...) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|