Skip to content

Commit

Permalink
Make Mayo GUI and CLI distinct applications
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Mar 14, 2024
1 parent 66c7c54 commit c88b13c
Show file tree
Hide file tree
Showing 61 changed files with 1,416 additions and 737 deletions.
442 changes: 278 additions & 164 deletions CMakeLists.txt

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions cmake/OpenCascadeWin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ if(OpenCASCADE_FOUND)
)
endforeach()

# Add OpenCASCADE library directories
# Note: QtCreator option "Run->Add build library search to PATH" will add to PATH env variable
# the contents of Mayo_LinkDirectories variable. For convenience, let's add also the
# directories containing DLLs
list(
APPEND Mayo_LinkDirectories
${OpenCASCADE_BINARY_DIR}
${OpenCASCADE_LIBRARY_DIR}
${OpenCASCADE_3RDPARTY_BINARY_DIRS}
)

# List all 3rd-party DLLs required by OpenCASCADE
set(OpenCASCADE_3RDPARTY_DLLS)
foreach(OccBINDIR ${OpenCASCADE_3RDPARTY_BINARY_DIRS})
Expand Down
168 changes: 135 additions & 33 deletions src/app/app_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,72 @@
#include "../base/settings.h"
#include "../gui/gui_application.h"
#include "../gui/gui_document.h"
#include "qtcore_utils.h"
#include "filepath_conv.h"
#include "qstring_conv.h"
#include "../qtcommon/filepath_conv.h"
#include "../qtcommon/qstring_conv.h"
#include "../qtcommon/qtcore_utils.h"

#include <BRepBndLib.hxx>

#include <QtCore/QDataStream>
#include <QtCore/QDir>
#include <QtCore/QtDebug>
#include <QtGui/QGuiApplication>

#include <fmt/format.h>
#include <iterator>

namespace Mayo {

namespace {

void readRecentFile(QDataStream& stream, RecentFile* recentFile)
{
QString strFilepath;
stream >> strFilepath;
if (stream.status() != QDataStream::Ok)
return;

recentFile->filepath = filepathFrom(strFilepath);
stream >> recentFile->thumbnail.imageData;
if (stream.status() != QDataStream::Ok)
return;

recentFile->thumbnail.imageCacheKey = -1;
// Read thumbnail timestamp
// Warning: qint64 and int64_t may not be the exact same type(eg __int64 and longlong with Windows/MSVC)
qint64 timestamp;
stream >> timestamp;
if (stream.status() != QDataStream::Ok)
return;

recentFile->thumbnailTimestamp = timestamp;
}

QuantityLength shapeChordalDeflection(const TopoDS_Shape& shape)
{
// Excerpted from Prs3d::GetDeflection(...)
constexpr QuantityLength baseDeviation = 1 * Quantity_Millimeter;

Bnd_Box bndBox;
constexpr bool useTriangulation = true;
BRepBndLib::Add(shape, bndBox, !useTriangulation);
if (bndBox.IsVoid())
return baseDeviation;

if (BndUtils::isOpen(bndBox)) {
if (!BndUtils::hasFinitePart(bndBox))
return baseDeviation;

bndBox = BndUtils::finitePart(bndBox);
}

const auto coords = BndBoxCoords::get(bndBox);
const gp_XYZ diag = coords.maxVertex().XYZ() - coords.minVertex().XYZ();
const double diagMaxComp = std::max({ diag.X(), diag.Y(), diag.Z() });
return 4 * diagMaxComp * baseDeviation;
}

} // namespace

AppModule::AppModule()
: m_settings(new Settings),
m_props(m_settings),
Expand Down Expand Up @@ -111,7 +162,7 @@ Settings::Variant AppModule::toVariant(const Property& prop) const
const auto& filesProp = constRef<PropertyRecentFiles>(prop);
QByteArray blob;
QDataStream stream(&blob, QIODevice::WriteOnly);
stream << filesProp.value();
AppModule::writeRecentFiles(stream, filesProp.value());
Variant varBlob(blob.toStdString());
varBlob.setByteArray(true);
return varBlob;
Expand All @@ -124,13 +175,10 @@ Settings::Variant AppModule::toVariant(const Property& prop) const
bool AppModule::fromVariant(Property* prop, const Settings::Variant& variant) const
{
if (isType<PropertyRecentFiles>(prop)) {
if (qobject_cast<QGuiApplication*>(QCoreApplication::instance()) == nullptr)
return true;

const QByteArray blob = QtCoreUtils::QByteArray_frowRawData(variant.toConstRefString());
QDataStream stream(blob);
RecentFiles recentFiles;
stream >> recentFiles;
AppModule::readRecentFiles(stream, &recentFiles);
ptr<PropertyRecentFiles>(prop)->setValue(recentFiles);
return stream.status() == QDataStream::Ok;
}
Expand Down Expand Up @@ -201,20 +249,21 @@ void AppModule::recordRecentFileThumbnail(GuiDocument* guiDoc)

const RecentFile* recentFile = this->findRecentFile(guiDoc->document()->filePath());
if (!recentFile) {
qDebug() << fmt::format("RecentFile object is null\n"
" Function: {}\n Document: {}\n RecentFilesCount: {}",
Q_FUNC_INFO,
guiDoc->document()->filePath().u8string(),
m_props.recentFiles.value().size())
.c_str();
qDebug() << fmt::format(
"RecentFile object is null\n"
" Function: {}\n Document: {}\n RecentFilesCount: {}",
Q_FUNC_INFO,
guiDoc->document()->filePath().u8string(),
m_props.recentFiles.value().size()
).c_str();
return;
}

if (!recentFile->isThumbnailOutOfSync())
return;

RecentFile newRecentFile = *recentFile;
const bool okRecord = newRecentFile.recordThumbnail(guiDoc, this->recentFileThumbnailSize());
const bool okRecord = this->impl_recordRecentFileThumbnail(&newRecentFile, guiDoc);
if (!okRecord)
return;

Expand All @@ -238,7 +287,7 @@ void AppModule::recordRecentFileThumbnails(GuiApplication* guiApp)
continue; // Skip

RecentFile newRecentFile = *recentFile;
if (newRecentFile.recordThumbnail(guiDoc, this->recentFileThumbnailSize())) {
if (this->impl_recordRecentFileThumbnail(&newRecentFile, guiDoc)) {
auto indexRecentFile = std::distance(&listRecentFile.front(), recentFile);
newListRecentFile.at(indexRecentFile) = newRecentFile;
}
Expand All @@ -247,28 +296,51 @@ void AppModule::recordRecentFileThumbnails(GuiApplication* guiApp)
m_props.recentFiles.setValue(newListRecentFile);
}

static QuantityLength shapeChordalDeflection(const TopoDS_Shape& shape)
void AppModule::setRecentFileThumbnailRecorder(std::function<Thumbnail(GuiDocument*, QSize)> fn)
{
// Excerpted from Prs3d::GetDeflection(...)
constexpr QuantityLength baseDeviation = 1 * Quantity_Millimeter;
m_fnRecentFileThumbnailRecorder = std::move(fn);
}

Bnd_Box bndBox;
constexpr bool useTriangulation = true;
BRepBndLib::Add(shape, bndBox, !useTriangulation);
if (bndBox.IsVoid())
return baseDeviation;

if (BndUtils::isOpen(bndBox)) {
if (!BndUtils::hasFinitePart(bndBox))
return baseDeviation;
void AppModule::readRecentFiles(QDataStream& stream, RecentFiles* recentFiles)
{
auto fnCheckStreamStatus = [](QDataStream::Status status) {
if (status != QDataStream::Ok) {
qDebug() << fmt::format(
"QDataStream error\n Function: {}\n Status: {}",
Q_FUNC_INFO, MetaEnum::name(status)
).c_str();
return false;
}

bndBox = BndUtils::finitePart(bndBox);
return true;
};

uint32_t count = 0;
stream >> count;
if (!fnCheckStreamStatus(stream.status()))
return; // Stream extraction error, abort

recentFiles->clear();
for (uint32_t i = 0; i < count; ++i) {
RecentFile recent;
readRecentFile(stream, &recent);
if (!fnCheckStreamStatus(stream.status()))
return; // Stream extraction error, abort

if (!recent.filepath.empty() && recent.thumbnailTimestamp != 0)
recentFiles->push_back(std::move(recent));
}
}

const auto coords = BndBoxCoords::get(bndBox);
const gp_XYZ diag = coords.maxVertex().XYZ() - coords.minVertex().XYZ();
const double diagMaxComp = std::max({ diag.X(), diag.Y(), diag.Z() });
return 4 * diagMaxComp * baseDeviation;
void AppModule::writeRecentFiles(QDataStream& stream, const RecentFiles& recentFiles)
{
stream << uint32_t(recentFiles.size());
for (const RecentFile& rf : recentFiles) {
stream << filepathTo<QString>(rf.filepath);
stream << rf.thumbnail.imageData;
stream << qint64(rf.thumbnailTimestamp);
}
}

OccBRepMeshParameters AppModule::brepMeshParameters(const TopoDS_Shape& shape) const
Expand Down Expand Up @@ -347,4 +419,34 @@ AppModule::~AppModule()
m_settings = nullptr;
}

bool AppModule::impl_recordRecentFileThumbnail(RecentFile* recentFile, GuiDocument* guiDoc)
{
if (!recentFile)
return false;

if (!guiDoc)
return false;

if (!m_fnRecentFileThumbnailRecorder)
return false;

if (!filepathEquivalent(recentFile->filepath, guiDoc->document()->filePath())) {
qDebug() << fmt::format(
"Filepath mismatch with GUI document\n"
" Function: {}\n Filepath: {}\n Document: {}",
Q_FUNC_INFO,
recentFile->filepath.u8string(),
guiDoc->document()->filePath().u8string()
).c_str();
return false;
}

if (recentFile->thumbnailTimestamp == RecentFile::timestampLastModified(recentFile->filepath))
return true;

recentFile->thumbnail = m_fnRecentFileThumbnailRecorder(guiDoc, this->recentFileThumbnailSize());
recentFile->thumbnailTimestamp = RecentFile::timestampLastModified(recentFile->filepath);
return true;
}

} // namespace Mayo
9 changes: 9 additions & 0 deletions src/app/app_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
#include "../base/settings.h"
#include "../base/unit_system.h"

#include <QtCore/QSize>

#include <locale>
#include <mutex>

class QDataStream;
class TDF_Label;
class TopoDS_Shape;

Expand Down Expand Up @@ -83,6 +86,9 @@ class AppModule :
void recordRecentFileThumbnail(GuiDocument* guiDoc);
void recordRecentFileThumbnails(GuiApplication* guiApp);
QSize recentFileThumbnailSize() const { return { 190, 150 }; }
void setRecentFileThumbnailRecorder(std::function<Thumbnail(GuiDocument*, QSize)> fn);
static void readRecentFiles(QDataStream& stream, RecentFiles* recentFiles);
static void writeRecentFiles(QDataStream& stream, const RecentFiles& recentFiles);

// Meshing of BRep shapes
OccBRepMeshParameters brepMeshParameters(const TopoDS_Shape& shape) const;
Expand Down Expand Up @@ -113,6 +119,8 @@ class AppModule :
AppModule(const AppModule&) = delete; // Not copyable
AppModule& operator=(const AppModule&) = delete; // Not copyable

bool impl_recordRecentFileThumbnail(RecentFile* recentFile, GuiDocument* guiDoc);

Settings* m_settings = nullptr;
IO::System m_ioSystem;
AppModuleProperties m_props;
Expand All @@ -121,6 +129,7 @@ class AppModule :
std::locale m_stdLocale;
QLocale m_qtLocale;
std::vector<std::unique_ptr<DocumentTreeNodePropertiesProvider>> m_vecDocTreeNodePropsProvider;
std::function<Thumbnail(GuiDocument*, QSize)> m_fnRecentFileThumbnailRecorder;
};

} // namespace Mayo
2 changes: 1 addition & 1 deletion src/app/app_module_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ AppModuleProperties::AppModuleProperties(Settings* settings)
#endif
});
settings->addResetFunction(groupId_graphics, [=]{
this->navigationStyle.setValue(WidgetOccViewController::NavigationStyle::Mayo);
this->navigationStyle.setValue(View3dNavigationStyle::Mayo);
this->defaultShowOriginTrihedron.setValue(true);
this->instantZoomFactor.setValue(5.);
this->turnViewAngleIncrement.setQuantity(5 * Quantity_Degree);
Expand Down
4 changes: 2 additions & 2 deletions src/app/app_module_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "../base/property_enumeration.h"
#include "../base/settings.h"
#include "../base/unit_system.h"
#include "widget_occ_view_controller.h"
#include "view3d_navigation_style.h"

#include <memory>
#include <unordered_map>
Expand Down Expand Up @@ -60,7 +60,7 @@ class AppModuleProperties : public PropertyGroup {
PropertyAngle meshingAngularDeflection{ this, textId("meshingAngularDeflection") };
PropertyBool meshingRelative{ this, textId("meshingRelative") };
// Graphics
PropertyEnum<WidgetOccViewController::NavigationStyle> navigationStyle{ this, textId("navigationStyle") };
PropertyEnum<View3dNavigationStyle> navigationStyle{ this, textId("navigationStyle") };
PropertyBool defaultShowOriginTrihedron{ this, textId("defaultShowOriginTrihedron") };
PropertyDouble instantZoomFactor{ this, textId("instantZoomFactor") };
PropertyAngle turnViewAngleIncrement{ this, textId("turnViewAngleIncrement") };
Expand Down
2 changes: 1 addition & 1 deletion src/app/command_system_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

#include "app_module.h"
#include "command_system_information_occopengl.h"
#include "qstring_conv.h"
#include "qtwidgets_utils.h"
#include "../base/meta_enum.h"
#include "../base/filepath.h"
#include "../base/io_system.h"
#include "../qtcommon/qstring_conv.h"
#include <common/mayo_version.h>

#include <QtCore/QDir>
Expand Down
2 changes: 1 addition & 1 deletion src/app/commands_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "../gui/gui_application.h"
#include "../gui/gui_document.h"
#include "../gui/v3d_view_controller.h"
#include "../qtcommon/qstring_conv.h"
#include "app_module.h"
#include "qstring_conv.h"
#include "theme.h"

#include <V3d_View.hxx>
Expand Down
4 changes: 2 additions & 2 deletions src/app/commands_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "../base/application.h"
#include "../base/task_manager.h"
#include "../gui/gui_application.h"
#include "../qtcommon/filepath_conv.h"
#include "../qtcommon/qstring_conv.h"
#include "app_module.h"
#include "filepath_conv.h"
#include "qstring_conv.h"
#include "recent_files.h"
#include "theme.h"

Expand Down
2 changes: 1 addition & 1 deletion src/app/commands_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "dialog_about.h"
#include "qtwidgets_utils.h"
#include "qstring_conv.h"
#include "../qtcommon/qstring_conv.h"

#include <QtCore/QDir>
#include <QtCore/QUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/app/dialog_about.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "dialog_about.h"

#include "ui_dialog_about.h"
#include "qstring_conv.h"
#include "../qtcommon/qstring_conv.h"
#include <common/mayo_version.h>

#include <Standard_Version.hxx>
Expand Down
4 changes: 2 additions & 2 deletions src/app/dialog_inspect_xde.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "../base/meta_enum.h"
#include "../base/settings.h"
#include "../base/tkernel_utils.h"
#include "../qtcommon/filepath_conv.h"
#include "../qtcommon/qstring_conv.h"
#include "app_module.h"
#include "filepath_conv.h"
#include "qmeta_tdf_label.h"
#include "qstring_conv.h"
#include "qstring_utils.h"
#include "qtgui_utils.h"
#include "qtwidgets_utils.h"
Expand Down
Loading

0 comments on commit c88b13c

Please sign in to comment.