25 #include "tools/basetool.h" |
25 #include "tools/basetool.h" |
26 #include "tools/drawtool.h" |
26 #include "tools/drawtool.h" |
27 #include "tools/selecttool.h" |
27 #include "tools/selecttool.h" |
28 #include "tools/transformtool.h" |
28 #include "tools/transformtool.h" |
29 |
29 |
30 static const QMetaObject* const toolMetaObjects[] = { |
|
31 &SelectTool::staticMetaObject, |
|
32 &DrawTool::staticMetaObject, |
|
33 &TransformTool::staticMetaObject, |
|
34 }; |
|
35 |
|
36 Document::Document( |
30 Document::Document( |
37 Model* model, |
31 Model* model, |
38 DocumentManager* documents, |
32 DocumentManager* documents, |
39 const ldraw::ColorTable& colorTable, |
33 const ldraw::ColorTable& colorTable, |
40 QWidget* parent) : |
34 QWidget* parent) : |
156 } |
150 } |
157 } |
151 } |
158 |
152 |
159 void Document::initializeTools() |
153 void Document::initializeTools() |
160 { |
154 { |
161 for (const QMetaObject* const metaObject : ::toolMetaObjects) |
155 this->tools.clear(); |
162 { |
156 this->tools.reserve(3); |
163 QObject* const objectInstance = metaObject->newInstance(Q_ARG(Model*, this->model), Q_ARG(QObject*, this)); |
157 this->tools.push_back(new SelectTool{this->model, this}); |
164 BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance); |
158 this->tools.push_back(new DrawTool{this->model, this}); |
165 if (toolInstance) |
159 this->tools.push_back(new TransformTool{this->model, this}); |
166 { |
160 for (BaseTool* const toolInstance : this->tools) |
167 this->tools.append(toolInstance); |
161 { |
168 QAction* action = new QAction{toolInstance->name(), this}; |
162 QAction* action = new QAction{toolInstance->name(), this}; |
169 action->setCheckable(true); |
163 action->setCheckable(true); |
170 this->toolActions[toolInstance] = action; |
164 this->toolActions[toolInstance] = action; |
171 action->setToolTip(toolInstance->toolTip()); |
165 action->setToolTip(toolInstance->toolTip()); |
172 connect(action, &QAction::triggered, this, &Document::toolActionTriggered); |
166 connect(action, &QAction::triggered, this, &Document::toolActionTriggered); |
173 this->toolsBar->addAction(action); |
167 this->toolsBar->addAction(action); |
174 QWidget* const widget = toolInstance->toolWidget(); |
168 QWidget* const widget = toolInstance->toolWidget(); |
175 if (widget) |
169 if (widget) |
176 { |
170 { |
177 this->ui.toolWidgetStack->addWidget(widget); |
171 this->ui.toolWidgetStack->addWidget(widget); |
178 } |
|
179 else |
|
180 { |
|
181 this->ui.toolWidgetStack->addWidget(new QWidget{this}); |
|
182 } |
|
183 } |
172 } |
184 else |
173 else |
185 { |
174 { |
186 QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className())); |
175 this->ui.toolWidgetStack->addWidget(new QWidget{this}); |
187 } |
176 } |
188 } |
177 } |
189 this->selectTool(this->tools[0]); |
178 this->selectTool(this->tools[0]); |
190 } |
179 } |
191 |
180 |