|
| 1 | +<br/> |
| 2 | +*This is what the decimal action looks like with only the `DecimalAction::WidgetFlag::Slider` widget flag.* |
| 3 | + |
| 4 | +<details> |
| 5 | +<summary> |
| 6 | +<strong>Example: customizing the default look and feel of a decimal action</strong><br/> |
| 7 | +</summary> |
| 8 | + |
| 9 | +```cpp |
| 10 | +// Create the decimal actions |
| 11 | +auto decimalActionDefault = new DecimalAction(this, "Decimal action (default flags)"); |
| 12 | +auto decimalActionSpinbox = new DecimalAction(this, "Decimal action (spinbox only)"); |
| 13 | +auto decimalActionSlider = new DecimalAction(this, "Decimal action (slider only)"); |
| 14 | + |
| 15 | +// Set default widget flags |
| 16 | +decimalActionSpinbox->setDefaultWidgetFlags(DecimalAction::WidgetFlag::SpinBox); |
| 17 | +decimalActionSlider->setDefaultWidgetFlags(DecimalAction::WidgetFlag::Slider); |
| 18 | + |
| 19 | +// Add the action widgets to the widget layout |
| 20 | +layout->addWidget(decimalActionDefault->createWidget(this)); |
| 21 | +layout->addWidget(decimalActionSpinbox->createWidget(this)); |
| 22 | +layout->addWidget(decimalActionSlider->createWidget(this)); |
| 23 | +``` |
| 24 | +
|
| 25 | +</details> |
| 26 | +
|
| 27 | +### 2. During the creation of the action's widget |
| 28 | +The look and feel of an action's GUI can also be modified when creating the action widget. This is achieved by supplying `WidgetAction::createWidget()` with additional widget flags. Building on the previous example, the code then becomes: |
| 29 | +
|
| 30 | +```cpp |
| 31 | +
|
| 32 | +// Create a single decimal action |
| 33 | +auto decimalAction = new DecimalAction(this, "Decimal action"); |
| 34 | +
|
| 35 | +// Add decimal action widget with spinbox and slider |
| 36 | +layout->addWidget(decimalAction->createWidget(this, DecimalAction::WidgetFlag::Default)); |
| 37 | +
|
| 38 | +// Add decimal action widget with spinbox only |
| 39 | +layout->addWidget(decimalAction->createWidget(this, DecimalAction::WidgetFlag::Spinbox)); |
| 40 | +
|
| 41 | +// Add decimal action widget with slider only |
| 42 | +layout->addWidget(decimalAction->createWidget(this, DecimalAction::WidgetFlag::Slider)); |
| 43 | +``` |
| 44 | + |
| 45 | +### 3. When adding an action to a group-based action |
| 46 | +When an action is added to a [mv::gui::GroupAction](https://github.com/ManiVaultStudio/core/blob/master/ManiVault/src/actions/GroupAction.h) derived action, custom widget flags can be specified (effectively overriding the default widget flags): |
| 47 | + |
| 48 | +```cpp |
| 49 | +// Create the group and decimal actions |
| 50 | +auto decimalActions = new HorizontalGroupAction(this, "Decimal actions"); |
| 51 | +auto decimalActionA = new DecimalAction(this, "Decimal action A"); |
| 52 | +auto decimalActionB = new DecimalAction(this, "Decimal action B"); |
| 53 | + |
| 54 | +// Add decimal action A and B to the group |
| 55 | +decimalActions->addAction(decimalActionA); |
| 56 | +decimalActions->addAction(decimalActionB, DecimalAction::WidgetFlag::LineEdit); // This overrides the default widget flags |
| 57 | + |
| 58 | +// Add the group action widget to the layout, the widgets for decimalActionA and decimalActionB are generated by decimalActions |
| 59 | +layout->addWidget(decimalActions->createWidget(this)); |
| 60 | +``` |
| 61 | +
|
| 62 | +## Customization using widget post-processing |
| 63 | +In scenarios where the built-in behavior and look are insufficient, the created widget can be modified immediately after creation using the [mv::gui::WidgetAction::WidgetConfigurationFunction](https://github.com/ManiVaultStudio/core/blob/5edd73e3e9ce1d8601864f25896226651de4f509/ManiVault/src/actions/WidgetAction.h#L41). This function is set in two ways: |
| 64 | +`action->setWidgetConfigurationFunction()` |
| 65 | +or during widget creation: |
| 66 | +```cpp |
| 67 | +
|
| 68 | +// Create the decimal action |
| 69 | +auto decimalAction = new DecimalAction(this, "My decimal"); |
| 70 | +
|
| 71 | +// Create the lambda that will be executed immediately after creation |
| 72 | +auto widgetEdit = [this](WidgetAction* action, QWidget* widget) -> void { |
| 73 | + auto spinBoxWidget = widget->findChild<QSpinBox*>("SpinBox"); |
| 74 | +
|
| 75 | + Q_ASSERT(spinBoxWidget); |
| 76 | +
|
| 77 | + if (!spinBoxWidget ) |
| 78 | + return; |
| 79 | +
|
| 80 | + // Your widget customization goes here... |
| 81 | +}; |
| 82 | +
|
| 83 | +// Widget configuration function widgetEdit will be executed immediately after creation |
| 84 | +action->createWidget(this, widgetEdit); |
| 85 | + |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
0 commit comments