Files
IfcOpenShell/src/bonsaiviewer/modules/models/AddModelDialog.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

163 lines
6.9 KiB
C++
Raw Normal View History

2026-05-09 22:00:19 +10:00
// This file was generated with the assistance of an AI coding tool.
/********************************************************************************
* *
* 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 "AddModelDialog.h"
#include "../../components/Dialog.h"
#include "../../components/Buttons.h"
#include "../../components/Section.h"
#include "../../components/Style.h"
#include <QEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <QVBoxLayout>
2026-05-20 09:38:10 +10:00
namespace bonsaiviewer::modules::models {
2026-05-09 22:00:19 +10:00
namespace {
class HoverDescriptionFilter : public QObject {
public:
HoverDescriptionFilter(QLabel* label, QString hover_text, QString default_text)
: label_(label), hover_text_(std::move(hover_text)), default_text_(std::move(default_text)) {}
protected:
bool eventFilter(QObject* watched, QEvent* event) override {
Q_UNUSED(watched);
if (event->type() == QEvent::Enter) {
label_->setText(hover_text_);
} else if (event->type() == QEvent::Leave) {
label_->setText(default_text_);
}
return false;
}
private:
QLabel* label_ = nullptr;
QString hover_text_;
QString default_text_;
};
} // namespace
AddModelDialog::AddModelDialog(QWidget* parent)
: components::Dialog(parent)
{
setObjectName("appDialog");
setWindowTitle("Add Model");
setModal(true);
setupUi();
}
void AddModelDialog::setupUi() {
if (auto* root = qobject_cast<QVBoxLayout*>(layout())) {
root->setSizeConstraint(QLayout::SetFixedSize);
}
const QString default_description = "Choose what to add to the project";
2026-05-20 20:56:19 +10:00
2026-05-09 22:00:19 +10:00
auto* description_section = new components::Section("", components::SectionHeaderMode::Hidden, this);
auto* description = new QLabel(default_description, description_section);
description->setProperty("textRole", "secondary");
description->setWordWrap(true);
description->setAlignment(Qt::AlignCenter);
description_section->addBodyWidget(description);
auto* choices_section = new components::Section("", components::SectionHeaderMode::Hidden, this);
auto* choices = new QWidget(choices_section);
auto* row = new QHBoxLayout(choices);
row->setContentsMargins(0, 0, 0, 0);
row->setSpacing(components::style::metrics::padding);
2026-05-20 20:56:19 +10:00
struct Choice {
SourceMode mode;
QString text;
QString icon;
QString hover;
};
const QList<Choice> local_choices = {
{SourceMode::IfcFile, "Add IFC File", ":/icons/cube.svg",
"Add IFC files and load both geometry and data."},
{SourceMode::IfcDatabase, "Add IFC\nDatabase", ":/icons/database.svg",
"Add IFC RDB databases for optimised performance"},
{SourceMode::GeometryOnly, "Add Geometry", ":/icons/cube-bandage.svg",
"Add pure geometry for fast visualisation"},
};
const QList<Choice> cloud_choices = {
{SourceMode::CloudModel, "Add From\nCloud", ":/icons/cloud-square.svg",
"Browse a cloud connector and add one or more models from there."},
};
const QList<Choice> tool_choices = {
{SourceMode::ConvertToDatabase, "Convert IFC File\nto Database", ":/icons/database-restore.svg",
"Convert IFC files to databases for smaller filesizes, reduced memory, "
"and faster access. No data is lost."},
{SourceMode::ExportGeometryDatabase, "Export Geometry\nDatabase", ":/icons/database-restore.svg",
"Convert IFC files to a read-only geometry database for smaller filesizes, "
"reduced memory, and faster access. Ideal for cloud read-only coordination "
"workflows. Only parametric geometry editing capabilities are lost."},
};
QStringList descriptions = {default_description};
auto build_group = [&](const QString& title, const QList<Choice>& group_choices) {
QList<QToolButton*> buttons;
for (const Choice& choice : group_choices) {
auto* button = components::buttons::makeButton(choice.text, choice.icon, choices);
const SourceMode mode = choice.mode;
connect(button, &QToolButton::clicked, this, [this, mode]() {
selected_mode_ = mode;
accept();
});
button->installEventFilter(
new HoverDescriptionFilter(description, choice.hover, default_description));
descriptions << choice.hover;
buttons << button;
}
return components::buttons::makeButtonGroup(title, buttons, choices, 8);
};
2026-05-20 20:56:19 +10:00
components::buttons::addButtonGroups(row, {
build_group("LOCAL", local_choices),
build_group("CLOUD", cloud_choices),
build_group("TOOLS", tool_choices),
});
2026-05-09 22:00:19 +10:00
choices_section->addBodyWidget(choices);
2026-05-20 20:56:19 +10:00
// Hovering a button swaps in a longer description; with a free-growing
// label that reflow shoves the buttons below it downward. Lock the label
// to the tallest string it will ever show. The label is laid out at the
// choices' width (both sit in Section bodies with identical margins), so
// measure every string at that width and keep the largest result.
const int label_width = choices->sizeHint().width();
int reserved_height = 0;
for (const QString& text : descriptions) {
description->setText(text);
reserved_height = qMax(reserved_height, description->heightForWidth(label_width));
}
description->setText(default_description);
description->setFixedHeight(reserved_height);
2026-05-09 22:00:19 +10:00
addBodyWidget(description_section);
addBodyWidget(choices_section);
}
2026-05-20 09:38:10 +10:00
} // namespace bonsaiviewer::modules::models