QCAD MainWindow 原型
一个简单QCAD窗体原型应用程序。
准备界面UI
\\support\\examples\\mainwindow\\MyDisplay.ui
QFrame
类型的Viewport00
用来加载View
渲染QCAD
实体对象。这里对应于模板
\\scripts\\Widgets\\ViewportWidget\\Templates\\00_Single.ui
。
MyDisplay.ui
00_Single.ui
渲染的基本流程
构造函数中,模拟了加载QCAD
加载dxf
到绘制的基本流程。
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(800, 600);
createMenu();
// init script handler for ECMAScript:
RScriptHandlerRegistry::registerScriptHandler(
RScriptHandlerEcma::factory,
RScriptHandlerEcma::getSupportedFileExtensionsStatic()
);
RScriptHandlerEcma* handler =
dynamic_cast<RScriptHandlerEcma*>(RScriptHandlerRegistry::getGlobalScriptHandler("js"));
//QString autofile;
//handler->init(autofile);
// create central widget from UI file:
// this widget contains a child called 'Viewport00'. This child
// widget will be filled with our graphics display with scroll
// bars, etc.
QString uiFileName = "support/examples/mainwindow/MyDisplay.ui";
QFile file(uiFileName);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Cannot open file " << uiFileName;
return;
}
QUiLoader* loader = new QUiLoader();
QWidget* widget = loader->load(&file, this);
file.close();
setCentralWidget(widget);
// create drawing document with document interface:
RMemoryStorage* storage = new RMemoryStorage();
RSpatialIndexNavel* spatialIndex = new RSpatialIndexNavel();
RDocument* document = new RDocument(*storage, *spatialIndex);
documentInterface = new RDocumentInterface(*document);
// import a DXF file into the drawing:
//documentInterface->importFile("support/examples/mainwindow/drawing_file_to_display.dxf");
// operation used to add various objects:
RAddObjectOperation* op;
// add a layer 'MyLayer':
QSharedPointer<RLayer> layer =
QSharedPointer<RLayer>(
new RLayer(document, "MyLayer")
);
layer->setLinetypeId(document->getLinetypeId("continuous"));
layer->setLineweight(RLineweight::Weight025);
layer->setColor(RColor(255,0,0));
op = new RAddObjectOperation(layer);
documentInterface->applyOperation(op);
RLayer::Id layerId = layer->getId();
// create and add a line entity:
QSharedPointer<RLineEntity> lineEntity =
QSharedPointer<RLineEntity>(
new RLineEntity(document, RLineData(RVector(0,0), RVector(100,0)))
);
// set attributes:
lineEntity->setLayerId(layerId);
lineEntity->setColor(RColor(0,128,255));
lineEntity->setLinetypeId(document->getLinetypeId("dashed"));
lineEntity->setLineweight(RLineweight::Weight100);
// set a custom property:
lineEntity->setCustomProperty("MyApp", "MyIntProperty", 77);
lineEntity->setCustomProperty("MyApp", "MyStringProperty", "Some text");
op = new RAddObjectOperation(
lineEntity,
false // false: don't use current attributes but
// custom attributes set above
);
// add the line entity to the drawing:
documentInterface->applyOperation(op);
// make 'widget' and 'documentInterface' available for the script engine:
QScriptEngine& engine = handler->getScriptEngine();
QScriptValue globalObject = engine.globalObject();
globalObject.setProperty("widget", engine.newQObject(widget));
globalObject.setProperty("documentInterface", qScriptValueFromValue(&engine, documentInterface));
// call script to initialize viewport (add scroll bars, rulers, etc.):
handler->doScript("support/examples/mainwindow/init_viewport.js");
}
void MainWindow::showEvent(QShowEvent* event) {
Q_UNUSED(event)
// trigger auto zoom on first show only:
static bool first = true;
if (first) {
documentInterface->autoZoom();
first = false;
}
}
support/examples/mainwindow/init_viewport.js
include('scripts/Widgets/ViewportWidget/ViewportWidget.js');
var viewports = ViewportWidget.getViewports(widget, documentInterface);
// creates a scene for the view,
// attaches the scene to the document interface,
// sets default navigation,
// adds scrollbars and rulers:
ViewportWidget.initializeViewports(viewports);
// init event handlers for drag and drop and scrolling:
ViewportWidget.initEventHandler(viewports);
应用程序入口
一些初始化操作、加载插件和显示窗口等等。
\\support\\examples\\mainwindow\\main.cpp
(简单版)
\\src\\run\\main.cpp
(复杂版)
//`\\support\\examples\\mainwindow\\main.cpp`
int main(int argc, char **argv)
{
QApplication* app = new QApplication(argc, argv);
// set current working directory:
QDir::setCurrent(RSettings::getApplicationPath());
// only use plugins dir of QCAD, none of the default plugin dirs of Qt self:
app->setLibraryPaths(QStringList() << RSettings::getPluginPath());
qRegisterMetaType<RColor>();
qRegisterMetaTypeStreamOperators<RColor>("RColor");
qRegisterMetaType<RVector>();
qRegisterMetaTypeStreamOperators<RVector>("RVector");
RMath::init();
// compile font list from 'fonts' dir:
RFontList::init();
// compile hatch pattern lists from 'patterns' dir:
RPatternListMetric::init();
RPatternListImperial::init();
// init object properties:
RObject::init();
REntity::init();
RDocumentVariables::init();
RArcEntity::init();
RBlockReferenceEntity::init();
RCircleEntity::init();
RDimensionEntity::init();
RDimLinearEntity::init();
RDimAlignedEntity::init();
RDimAngularEntity::init();
RDimAngular2LEntity::init();
RDimAngular3PEntity::init();
RDimArcLengthEntity::init();
RDimDiametricEntity::init();
RDimOrdinateEntity::init();
RDimRadialEntity::init();
RDimRotatedEntity::init();
REllipseEntity::init();
RImageEntity::init();
RHatchEntity::init();
RLeaderEntity::init();
RLineEntity::init();
RPointEntity::init();
RPolylineEntity::init();
RSolidEntity::init();
RTraceEntity::init();
RFaceEntity::init();
RSplineEntity::init();
RXLineEntity::init();
RRayEntity::init();
RViewportEntity::init();
RTextBasedEntity::init();
RTextEntity::init();
RAttributeDefinitionEntity::init();
RAttributeEntity::init();
RUcs::init();
RLayer::init();
RLayerState::init();
RLayout::init();
RLinetype::init();
RBlock::init();
RView::init();
// load plugins (DWG support, etc.):
RPluginLoader::loadPlugins(true);
RLinetypeListMetric::init();
RLinetypeListImperial::init();
// show main window:
MainWindow* window = new MainWindow();
window->show();
// exec:
int ret = app->exec();
// uninit:
RPluginLoader::unloadPlugins();
RSettings::uninit();
RFontList::uninit();
RPatternListMetric::uninit();
RPatternListImperial::uninit();
RSingleton::cleanUp();
RMath::uninit();
RDocumentInterface::deleteClipboard();
RDebug::printCounters();
return ret;
}
原文链接:https://blog.csdn.net/mrbaolong/article/details/111907798
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容