236 for (int i = 0; i < countof(editingModesInfo); ++i) { |
236 for (int i = 0; i < countof(editingModesInfo); ++i) { |
237 const auto& editingModeInfo = editingModesInfo[i]; |
237 const auto& editingModeInfo = editingModesInfo[i]; |
238 QAction* action = new QAction{editingModeInfo.name, parent}; |
238 QAction* action = new QAction{editingModeInfo.name, parent}; |
239 action->setCheckable(true); |
239 action->setCheckable(true); |
240 action->setEnabled(false); |
240 action->setEnabled(false); |
241 action->setData(static_cast<EditingMode>(i)); |
241 action->setData(QVariant::fromValue(static_cast<editing_mode_e>(i))); |
242 action->setToolTip(editingModeInfo.tooltip); |
242 action->setToolTip(editingModeInfo.tooltip); |
243 action->setIcon(QPixmap{editingModeInfo.icon}); |
243 action->setIcon(QPixmap{editingModeInfo.icon}); |
244 ui->editingModesToolBar->addAction(action); |
244 ui->editingModesToolBar->addAction(action); |
245 QWidget* widget = editingModeInfo.widget; |
245 QWidget* widget = editingModeInfo.widget; |
246 if (widget == nullptr) { |
246 if (widget == nullptr) { |
369 QObject::connect( |
369 QObject::connect( |
370 data->tools.get(), |
370 data->tools.get(), |
371 &EditTools::suggestCursor, |
371 &EditTools::suggestCursor, |
372 data->canvas.get(), |
372 data->canvas.get(), |
373 &QWidget::setCursor); |
373 &QWidget::setCursor); |
374 data->tools->setEditMode(SelectMode); |
374 data->tools->setEditMode(editing_mode_e::select); |
375 const QFileInfo fileInfo{*state->documents.modelPath(modelId)}; |
375 const QFileInfo fileInfo{*state->documents.modelPath(modelId)}; |
376 auto* const subWindow = createSubWindow<ModelSubWindow>(state->mainWindow.mdiArea, modelId); |
376 auto* const subWindow = createSubWindow<ModelSubWindow>(state->mainWindow.mdiArea, modelId); |
377 subWindow->setMinimumSize({96, 96}); |
377 subWindow->setMinimumSize({96, 96}); |
378 subWindow->resize({320, 200}); |
378 subWindow->resize({320, 200}); |
379 subWindow->setWidget(data->canvas.get()); |
379 subWindow->setWidget(data->canvas.get()); |
466 saveCurrentModel(state, *modelId); |
466 saveCurrentModel(state, *modelId); |
467 } |
467 } |
468 } |
468 } |
469 } |
469 } |
470 |
470 |
471 static void checkEditingModeAction(MainState* state, const EditingMode mode) |
471 static void checkEditingModeAction(MainState* state, const editing_mode_e mode) |
472 { |
472 { |
473 const bool hasDocument = currentModelData(&state->mainWindow, &state->documents) != nullptr; |
473 const bool hasDocument = currentModelData(&state->mainWindow, &state->documents) != nullptr; |
474 for (QAction* action : state->mainWindow.editingModesToolBar->actions()) |
474 for (QAction* action : state->mainWindow.editingModesToolBar->actions()) |
475 { |
475 { |
476 action->setEnabled(hasDocument); |
476 action->setEnabled(hasDocument); |
477 action->setChecked(hasDocument and action->data().value<EditingMode>() == mode); |
477 action->setChecked(hasDocument and action->data().value<editing_mode_e>() == mode); |
478 } |
478 } |
479 } |
479 } |
480 |
480 |
481 static void update_model_grid_matrix(MainState* state) |
481 static void update_model_grid_matrix(MainState* state) |
482 { |
482 { |
498 const glm::mat4 original = state->mainWindow.gridMatrix->value(); |
498 const glm::mat4 original = state->mainWindow.gridMatrix->value(); |
499 const glm::mat4 unscaled = unscale_matrix(original).unscaled; |
499 const glm::mat4 unscaled = unscale_matrix(original).unscaled; |
500 const glm::mat4 rescaled = factor * unscaled; |
500 const glm::mat4 rescaled = factor * unscaled; |
501 state->mainWindow.gridMatrix->setValue(rescaled); |
501 state->mainWindow.gridMatrix->setValue(rescaled); |
502 update_model_grid_matrix(state); |
502 update_model_grid_matrix(state); |
|
503 } |
|
504 |
|
505 static void replace_color_in_selected_code(QTextCursor* cursor, const ColorIndex color) |
|
506 { |
|
507 const auto pattern = R"(^(\s*(?:1|2|3|4|5)\s+)\d+)"; |
|
508 static const QRegularExpression regular_expression{pattern, QRegularExpression::MultilineOption}; |
|
509 QString text = cursor->selectedText(); |
|
510 // Qt has decided to be "smart" and uses strange unicode characters instead of newlines |
|
511 text.replace("\u2029", "\n"); |
|
512 text.replace(regular_expression, QStringLiteral(R"(\1%1)").arg(color.index)); |
|
513 cursor->removeSelectedText(); |
|
514 cursor->insertText(text); |
503 } |
515 } |
504 |
516 |
505 int main(int argc, char *argv[]) |
517 int main(int argc, char *argv[]) |
506 { |
518 { |
507 doQtRegistrations(); |
519 doQtRegistrations(); |
607 for (QAction* action : state.mainWindow.editingModesToolBar->actions()) { |
619 for (QAction* action : state.mainWindow.editingModesToolBar->actions()) { |
608 QObject::connect(action, &QAction::triggered, [action, &state] |
620 QObject::connect(action, &QAction::triggered, [action, &state] |
609 { |
621 { |
610 if (ModelData* data = currentModelData(&state.mainWindow, &state.documents)) |
622 if (ModelData* data = currentModelData(&state.mainWindow, &state.documents)) |
611 { |
623 { |
612 const EditingMode mode = action->data().value<EditingMode>(); |
624 const editing_mode_e mode = action->data().value<editing_mode_e>(); |
613 data->tools->setEditMode(mode); |
625 data->tools->setEditMode(mode); |
614 checkEditingModeAction(&state, mode); |
626 checkEditingModeAction(&state, mode); |
615 } |
627 } |
616 }); |
628 }); |
617 } |
629 } |
629 state.mainWindow.modelEdit->setFont(codeEditorFontFromSettings()); |
641 state.mainWindow.modelEdit->setFont(codeEditorFontFromSettings()); |
630 } |
642 } |
631 } |
643 } |
632 else |
644 else |
633 { |
645 { |
634 checkEditingModeAction(&state, EditingMode::SelectMode); |
646 checkEditingModeAction(&state, editing_mode_e::select); |
635 } |
647 } |
636 state.mainWindow.modelEdit->setEnabled(modelSubWindow != nullptr); |
648 state.mainWindow.modelEdit->setEnabled(modelSubWindow != nullptr); |
637 }); |
649 }); |
638 state.mainWindow.messageLog->setModel(&state.messageLog); |
650 state.mainWindow.messageLog->setModel(&state.messageLog); |
639 QObject::connect(&state.documents, &DocumentManager::message, &state.messageLog, &MessageLog::addMessage); |
651 QObject::connect(&state.documents, &DocumentManager::message, &state.messageLog, &MessageLog::addMessage); |
711 &QAction::triggered, |
723 &QAction::triggered, |
712 [&state]{ |
724 [&state]{ |
713 set_grid_scale(&state, 0.1f); |
725 set_grid_scale(&state, 0.1f); |
714 } |
726 } |
715 ); |
727 ); |
|
728 QObject::connect( |
|
729 state.mainWindow.action_make_stuff_red, |
|
730 &QAction::triggered, |
|
731 [&state]{ |
|
732 QTextCursor cursor = state.mainWindow.modelEdit->textCursor(); |
|
733 replace_color_in_selected_code(&cursor, ColorIndex{4}); |
|
734 } |
|
735 ); |
716 restoreSettings(&state); |
736 restoreSettings(&state); |
717 updateRenderPreferences(&state.mainWindow, &state.renderPreferences, &state.documents); |
737 updateRenderPreferences(&state.mainWindow, &state.renderPreferences, &state.documents); |
718 const int result = app.exec(); |
738 const int result = app.exec(); |
719 saveSettings(&state); |
739 saveSettings(&state); |
720 return result; |
740 return result; |