src/document.cpp

changeset 126
a7c720aff97c
parent 125
f127982d3412
child 127
f64bfb7f5d26
equal deleted inserted replaced
125:f127982d3412 126:a7c720aff97c
41 documents{documents}, 41 documents{documents},
42 colorTable{colorTable}, 42 colorTable{colorTable},
43 vertexMap{model}, 43 vertexMap{model},
44 renderer{new Canvas{model, documents, colorTable, this}}, 44 renderer{new Canvas{model, documents, colorTable, this}},
45 ui{*new Ui::Document}, 45 ui{*new Ui::Document},
46 objectEditor{model, ldraw::NULL_ID, this},
47 toolsBar{new QToolBar{this}} 46 toolsBar{new QToolBar{this}}
48 { 47 {
49 this->ui.setupUi(this); 48 this->ui.setupUi(this);
50 this->ui.toolsBarContainer->setLayout(new QVBoxLayout{this->ui.toolsBarContainer}); 49 this->ui.toolsBarContainer->setLayout(new QVBoxLayout{this->ui.toolsBarContainer});
51 this->ui.toolsBarContainer->layout()->addWidget(this->toolsBar); 50 this->ui.toolsBarContainer->layout()->addWidget(this->toolsBar);
52 this->ui.listView->setModel(model); 51 this->ui.listView->setModel(model);
53 this->ui.viewportFrame->setLayout(new QVBoxLayout{this->ui.listView}); 52 this->ui.viewportFrame->setLayout(new QVBoxLayout{this->ui.listView});
54 this->ui.viewportFrame->layout()->addWidget(this->renderer); 53 this->ui.viewportFrame->layout()->addWidget(this->renderer);
55 this->ui.objectEditorFrame->setLayout(new QVBoxLayout{this->ui.objectEditorFrame});
56 this->ui.objectEditorFrame->layout()->addWidget(&this->objectEditor);
57 this->toolsBar->setOrientation(Qt::Vertical); 54 this->toolsBar->setOrientation(Qt::Vertical);
58 this->setMouseTracking(true); 55 this->setMouseTracking(true);
59 connect(this->ui.splitter, &QSplitter::splitterMoved, this, &Document::splitterChanged); 56 connect(this->ui.splitter, &QSplitter::splitterMoved, this, &Document::splitterChanged);
60 connect(this->renderer, &Canvas::newStatusText, this, &Document::newStatusText); 57 connect(this->renderer, &Canvas::newStatusText, this, &Document::newStatusText);
61 connect(this->renderer, &Canvas::selectionChanged, [&](const QSet<ldraw::id_t>& newSelection) 58 connect(this->renderer, &Canvas::selectionChanged, [&](const QSet<ldraw::id_t>& newSelection)
149 this->vertexMap.apply(fn); 146 this->vertexMap.apply(fn);
150 } 147 }
151 148
152 void Document::selectionChanged(const QSet<ldraw::id_t>& newSelection) 149 void Document::selectionChanged(const QSet<ldraw::id_t>& newSelection)
153 { 150 {
154 if (newSelection.size() == 1) 151 if (this->selectedTool != nullptr)
155 { 152 {
156 this->objectEditor.setObjectId(*newSelection.begin()); 153 this->selectedTool->selectionChanged(newSelection);
157 }
158 else
159 {
160 this->objectEditor.setObjectId(ldraw::NULL_ID);
161 } 154 }
162 } 155 }
163 156
164 void Document::initializeTools() 157 void Document::initializeTools()
165 { 158 {
166 for (const QMetaObject* const metaObject : ::toolMetaObjects) 159 for (const QMetaObject* const metaObject : ::toolMetaObjects)
167 { 160 {
168 QObject* const objectInstance = metaObject->newInstance(Q_ARG(QObject*, this)); 161 QObject* const objectInstance = metaObject->newInstance(Q_ARG(Model*, this->model), Q_ARG(QObject*, this));
169 BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance); 162 BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance);
170 if (toolInstance) 163 if (toolInstance)
171 { 164 {
172 this->tools.append(toolInstance); 165 this->tools.append(toolInstance);
173 QAction* action = new QAction{toolInstance->name(), this}; 166 QAction* action = new QAction{toolInstance->name(), this};
174 action->setCheckable(true); 167 action->setCheckable(true);
175 this->toolActions[toolInstance] = action; 168 this->toolActions[toolInstance] = action;
176 action->setToolTip(toolInstance->toolTip()); 169 action->setToolTip(toolInstance->toolTip());
177 connect(action, &QAction::triggered, this, &Document::toolActionTriggered); 170 connect(action, &QAction::triggered, this, &Document::toolActionTriggered);
178 this->toolsBar->addAction(action); 171 this->toolsBar->addAction(action);
172 QWidget* const widget = toolInstance->toolWidget();
173 if (widget)
174 {
175 this->ui.toolWidgetStack->addWidget(widget);
176 }
177 else
178 {
179 this->ui.toolWidgetStack->addWidget(new QWidget{this});
180 }
179 } 181 }
180 else 182 else
181 { 183 {
182 QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className())); 184 QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className()));
183 } 185 }
213 this->selectedTool = tool; 215 this->selectedTool = tool;
214 for (auto&& pair : items(this->toolActions)) 216 for (auto&& pair : items(this->toolActions))
215 { 217 {
216 pair.value->setChecked(pair.key == tool); 218 pair.value->setChecked(pair.key == tool);
217 } 219 }
220 const int index = this->tools.indexOf(tool);
221 if (index != -1)
222 {
223 this->ui.toolWidgetStack->setCurrentIndex(index);
224 }
218 } 225 }
219 } 226 }
220 227
221 void Document::handleKeyPress(QKeyEvent* event) 228 void Document::handleKeyPress(QKeyEvent* event)
222 { 229 {

mercurial