src/main.cpp

changeset 204
52e10e8d88cc
parent 203
1909a0123c72
child 205
1a4342d80de7
equal deleted inserted replaced
203:1909a0123c72 204:52e10e8d88cc
259 if (widget == nullptr) { 259 if (widget == nullptr) {
260 widget = new QWidget{parent}; 260 widget = new QWidget{parent};
261 } 261 }
262 ui->toolWidgetStack->addWidget(widget); 262 ui->toolWidgetStack->addWidget(widget);
263 } 263 }
264 }
265
266 constexpr bool sortModelIndexesByRow(const QModelIndex& a, const QModelIndex& b)
267 {
268 return a.row() < b.row();
269 }
270
271 std::vector<int> rows(const QModelIndexList& indexList)
272 {
273 std::vector<int> result;
274 result.reserve(indexList.size());
275 for (const QModelIndex& index : indexList)
276 {
277 result.push_back(index.row());
278 }
279 return result;
264 } 280 }
265 281
266 int main(int argc, char *argv[]) 282 int main(int argc, char *argv[])
267 { 283 {
268 doQtRegistrations(); 284 doQtRegistrations();
301 openModelFromPath(path, &libraries, &documents, &mainWindow); 317 openModelFromPath(path, &libraries, &documents, &mainWindow);
302 } 318 }
303 ); 319 );
304 } 320 }
305 }; 321 };
322 static constexpr auto executeAction = [&](
323 Model* model, const ModelAction& action
324 ) {
325 std::visit(overloaded{
326 [model](const AppendToModel& action){
327 model->append(action.newElement);
328 },
329 [model](const DeleteFromModel& action){
330 model->remove(action.position);
331 },
332 }, action);
333 };
306 const auto restoreSettings = [&]{ 334 const auto restoreSettings = [&]{
307 recentlyOpenedFiles = settings.recentFiles(); 335 recentlyOpenedFiles = settings.recentFiles();
308 documentSplitterState = settings.mainSplitterState(); 336 documentSplitterState = settings.mainSplitterState();
309 renderPreferences = loadRenderPreferences(&settings); 337 renderPreferences = loadRenderPreferences(&settings);
310 changeLanguage(settings.locale(), &translator); 338 changeLanguage(settings.locale(), &translator);
327 updateRecentlyOpenedDocumentsMenu(); 355 updateRecentlyOpenedDocumentsMenu();
328 }; 356 };
329 const auto openModelForEditing = [&](const ModelId modelId){ 357 const auto openModelForEditing = [&](const ModelId modelId){
330 Model* model = documents.getModelById(modelId); 358 Model* model = documents.getModelById(modelId);
331 EditorTabWidget* document = new EditorTabWidget{model, &documents, colorTable}; 359 EditorTabWidget* document = new EditorTabWidget{model, &documents, colorTable};
360 QObject::connect(
361 document,
362 &EditorTabWidget::modelAction,
363 std::bind(executeAction, model, std::placeholders::_1));
332 QItemSelectionModel* selectionModel = new QItemSelectionModel{model}; 364 QItemSelectionModel* selectionModel = new QItemSelectionModel{model};
333 itemSelectionModels[model] = selectionModel; 365 itemSelectionModels[model] = selectionModel;
334 QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged, 366 QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged,
335 [model, document](const QItemSelection& selected, const QItemSelection& deselected) 367 [model, document](const QItemSelection& selected, const QItemSelection& deselected)
336 { 368 {
445 save(*modelId); 477 save(*modelId);
446 } 478 }
447 } 479 }
448 } 480 }
449 }); 481 });
482 QObject::connect(ui.actionDelete, &QAction::triggered, [&]{
483 EditorTabWidget* tab = currentTabWidget(&ui);
484 if (tab != nullptr) {
485 std::vector<int> selectedRows = rows(ui.modelListView->selectionModel()->selectedRows());
486 std::sort(selectedRows.begin(), selectedRows.end(), std::greater<int>{});
487 for (int row : selectedRows) {
488 executeAction(tab->model, DeleteFromModel{.position = row});
489 }
490 }
491 });
450 QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){ 492 QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
451 renderPreferences.drawAxes = drawAxes; 493 renderPreferences.drawAxes = drawAxes;
452 saveSettings(); 494 saveSettings();
453 updateRenderPreferences(&ui, &renderPreferences); 495 updateRenderPreferences(&ui, &renderPreferences);
454 }); 496 });
475 checkEditingModeAction(mode); 517 checkEditingModeAction(mode);
476 } 518 }
477 }); 519 });
478 } 520 }
479 QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated, 521 QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
480 [&](QMdiSubWindow* subWindow){ 522 [&](QMdiSubWindow* subWindow) {
481 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget()); 523 if (subWindow != nullptr) {
482 if (tab != nullptr) { 524 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
483 checkEditingModeAction(tab->currentEditingMode()); 525 if (tab != nullptr) {
484 QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model); 526 checkEditingModeAction(tab->currentEditingMode());
485 if (selectionModel != nullptr) { 527 QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model);
486 ui.modelListView->setModel(tab->model); 528 if (selectionModel != nullptr) {
487 ui.modelListView->setSelectionModel(selectionModel); 529 ui.modelListView->setModel(tab->model);
530 ui.modelListView->setSelectionModel(selectionModel);
531 }
488 } 532 }
489 } 533 }
490 }); 534 });
491 mainWindow.setWindowTitle(title()); 535 mainWindow.setWindowTitle(title());
492 mainWindow.restoreGeometry(settings.mainWindowGeometry()); 536 mainWindow.restoreGeometry(settings.mainWindowGeometry());

mercurial