DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
ParamsConfig.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
9#pragma once
10
11#include <QWidget>
12#include "ui_ParamsConfig.h"
13#include "ChoiceListDialog.h"
14#include "TextEditor.h"
15
16#include "stdafx.h"
17
18namespace DynExp
19{
20 class DynExpCore;
21 class Object;
22 class ObjectLinkBase;
23
28 enum class TextUsageType { Standard, Path, Code };
42}
43
48{
54 ParamInfo(std::string Title, std::string Description)
55 : Title(std::move(Title)), Description(std::move(Description)) {}
56
60 ParamInfo(std::string_view Title, std::string_view Description)
62
63 const std::string Title;
64 const std::string Description;
65};
66
71class ParamsConfigDialog : public QDialog
72{
73 Q_OBJECT
74
75public:
76 using NumberType = double;
81
87 using IndexType = qulonglong;
88
89private:
94 using FunctionsToCallIfAcceptedType = std::function<void(void)>;
95
100 struct Param
101 {
150 Param() = delete;
151
164
166 QWidget* const Widget;
167 const std::any Destiny;
170 };
171
172public:
179 ParamsConfigDialog(QWidget* parent, const DynExp::DynExpCore& Core, std::string Title);
180
182
193 void AddParam(ParamInfo&& Info, const std::any Destiny, const NumberType Value,
194 const NumberType MinValue, const NumberType MaxValue, const NumberType Precision, const NumberType Increment);
195
203 void AddParam(ParamInfo&& Info, const std::any Destiny, const TextType Value, const DynExp::TextUsageType TextUsage);
204
215 void AddParam(ParamInfo&& Info, const std::any Destiny, const TextRefType Value,
216 const TextListType& TextList, const bool AllowResetToDefault);
217
228 void AddParam(ParamInfo&& Info, const std::any Destiny, const TextListIndexType Value,
229 const TextListIndexType DefaultValue, const TextListType& TextList);
230
242 template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int> = 0>
243 void AddParam(ParamInfo&& Info, const std::any Destiny, const EnumType Value,
244 const EnumType DefaultValue, const Util::TextValueListType<EnumType>& TextValueList);
245
257 void AddParam(ParamInfo&& Info, const std::any Destiny, const DynExp::ItemIDType Value,
258 bool IsOptional, std::string_view IconResourcePath, FunctionsToCallIfAcceptedType FunctionToCallIfAccepted,
259 Util::TextValueListType<IndexType>&& ItemIDsWithLabels);
260
271 void AddParam(ParamInfo&& Info, const std::any Destiny, const std::vector<DynExp::ItemIDType>& Values,
272 bool IsOptional, std::string_view IconResourcePath, FunctionsToCallIfAcceptedType FunctionToCallIfAccepted,
273 Util::TextValueListType<IndexType>&& ItemIDsWithLabels);
274
279 size_t GetNumParams() const noexcept { return ParamList.size(); }
280
286 bool IsResetRequired() const noexcept { return ResetRequired; }
287
293 bool Display(DynExp::Object* Object = nullptr);
294
295private:
301 void InsertWidget(ParamInfo&& Info, Param&& ParamData);
302
311 template <typename ParamT>
312 void Assign(ParamT& Param, typename ParamT::UnderlyingType Value);
313
322 template <typename ParamT>
323 void Assign(ParamT& Param, const std::vector<typename ParamT::UnderlyingType::value_type>& Values);
324
330
335
339 std::vector<Param> ParamList;
340
345 std::vector<FunctionsToCallIfAcceptedType> FunctionsToCallIfAccepted;
346
352
358
363 std::vector<TextEditor*> TextEditorDialogs;
364
368 Ui::ParamsConfig ui;
369
370private slots:
371 void OnOpenParam();
372 void OnEditParam();
373 void OnResetParam();
374 virtual void accept() override;
375 virtual void reject() override;
376};
377
378template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int>>
379void ParamsConfigDialog::AddParam(ParamInfo&& Info, const std::any Destiny, const EnumType Value,
380 const EnumType DefaultValue, const Util::TextValueListType<EnumType>& TextValueList)
381{
382 using IntegerType = std::underlying_type_t<EnumType>;
383 constexpr Param::ParamType ParamType = std::is_signed_v<IntegerType> ? Param::ParamType::SignedInteger : Param::ParamType::UnsignedInteger;
384
385 auto ComboBox = new QComboBox(this);
386 int SelectedIndex = 0;
387 int DefaultIndex = 0;
388
389 for (const auto& TextValuePair : TextValueList)
390 {
391 if (Value == TextValuePair.second)
392 SelectedIndex = ComboBox->count();
393 if (DefaultValue == TextValuePair.second)
394 DefaultIndex = ComboBox->count();
395
396 ComboBox->addItem(QString::fromStdString(TextValuePair.first), QVariant(static_cast<IntegerType>(TextValuePair.second)));
397 }
398 ComboBox->setCurrentIndex(SelectedIndex);
399
400 Param ParamData(ParamType, ComboBox, Destiny, DefaultIndex);
401 InsertWidget(std::move(Info), std::move(ParamData));
402}
403
404template <typename ParamT>
405void ParamsConfigDialog::Assign(ParamT& Param, typename ParamT::UnderlyingType Value)
406{
407 ResetRequired |= Param != Value && Param.GetNeedsResetToApplyChange();
408 Param = std::move(Value);
409}
410
411template <typename ParamT>
412void ParamsConfigDialog::Assign(ParamT& Param, const std::vector<typename ParamT::UnderlyingType::value_type>& Values)
413{
414 ResetRequired |= Param.GetNeedsResetToApplyChange();
415 Param = std::move(Values);
416}
Implements a dialog with a list of available items on the left and a list of selected items on the ri...
Implements a dialog to edit and save small pieces of text or (Python) code.
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
Base class for all DynExp Objects like hardware adapters (DynExp::HardwareAdapterBase),...
Definition Object.h:1971
Defines the configuration dialog. The dialog must be displayed by calling ParamsConfigDialog::Display...
Util::TextListIndexType TextListIndexType
List index type of Util::TextListType.
size_t GetNumParams() const noexcept
Returns the number of parameters added to the configuration dialog.
std::vector< TextEditor * > TextEditorDialogs
Keeps a list of text editor dialogs so that their ownership can be removed when the ParamsConfigDialo...
qulonglong IndexType
ID type of objects/items managed by DynExp.
bool ResetRequired
Becomes true if an existing DynExp::Object is edited and needs to be reset to apply changes after cli...
Ui::ParamsConfig ui
Bundles Qt widgets of the ParamsConfigDialog instance's user interface.
void Assign(ParamT &Param, typename ParamT::UnderlyingType Value)
When the settings dialog is closed by accepting it, assign a single value from the user interface to ...
std::function< void(void)> FunctionsToCallIfAcceptedType
Signature of a function which is called when the ParamsConfigDialog is closed by the user by clicking...
double NumberType
Number type used for numeric parameters (DynExp::ParamsBase::Param)
void OnOpenParam()
Called when clicking the 'Browse' button for a DynExp::TextUsageType::Path or DynExp::TextUsageType::...
void OnResetParam()
Called when resetting a parameter to its default value.
DynExp::Object * Object
DynExp::Object whose parameters are edited. nullptr if a new DynExp::Object is created and synchroniz...
const DynExp::DynExpCore & Core
Reference to DynExp's core.
Util::TextRefType TextRefType
Reference-to-string type of text-type parameters (DynExp::ParamsBase::Param)
Util::TextListType TextListType
List type of text-type parameters.
bool IsResetRequired() const noexcept
Determines whether the DynExp::Object instance whose parameters have been edited needs to be reset to...
virtual void reject() override
Called when the settings dialog is rejected clicking its 'Cancel' button.
~ParamsConfigDialog()=default
void AddParam(ParamInfo &&Info, const std::any Destiny, const NumberType Value, const NumberType MinValue, const NumberType MaxValue, const NumberType Precision, const NumberType Increment)
Appends a parameter to the configuration dialog.
void InsertWidget(ParamInfo &&Info, Param &&ParamData)
Creates Qt widgets for editing a parameter and inserts them into the configuration dialog.
virtual void accept() override
Called when the settings dialog is accepted clicking its 'OK' button.
std::vector< Param > ParamList
List of all parameters assigned to this ParamsConfigDialog instance.
std::vector< FunctionsToCallIfAcceptedType > FunctionsToCallIfAccepted
List of functions to be called when acceptig the dialog. Refer to ParamsConfigDialog::FunctionsToCall...
void OnEditParam()
Called when opening a TextEditor for a DynExp::TextUsageType::Code parameter.
void HandleTextEditorDialogsOnClose()
Called when the settings dialog is closed. Removes the parent widget of text editors listed in TextEd...
bool Display(DynExp::Object *Object=nullptr)
Displays the configuration dialog.
Util::TextType TextType
" String type of text-type parameters (DynExp::ParamsBase::Param)
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
TextUsageType
Specifies the usage of a text-type parameter. Setting the right usage allows the ParamsConfigDialog t...
size_t ItemIDType
ID type of objects/items managed by DynExp.
std::vector< TextType > TextListType
List type of text-type parameters.
Definition QtUtil.h:29
size_t TextListIndexType
List index type of Util::TextListType.
Definition QtUtil.h:30
std::string_view TextRefType
Reference-to-string type of text-type parameters (DynExp::ParamsBase::Param)
Definition QtUtil.h:28
std::string TextType
String type of text-type parameters (DynExp::ParamsBase::Param)
Definition QtUtil.h:27
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Definition QtUtil.h:37
Accumulates include statements to provide a precompiled header.
Bundles a parameter's title and description texts.
ParamInfo(std::string Title, std::string Description)
Constructs an instance and initializes Title and Description.
ParamInfo(std::string_view Title, std::string_view Description)
Constructs an instance and initializes Title and Description.
const std::string Title
Refer to DynExp::ParamsBase::ParamBase::ParamTitle.
const std::string Description
Refer to DynExp::ParamsBase::ParamBase::ParamDescription.
Collection of data defining a single parameter to be managed by a ParamsConfigDialog instance....
QWidget *const Widget
Widget to edit/show the parameter's value in the UI.
ParamType
Identifies the type of a parameter shown in the user interface. Also refer to DynExp::TextUsageType.
const ParamType Type
Type of the managed parameter.
const IndexType DefaultIndex
Default selection index for combo box-based parameters used in case of a parameter reset.
const bool AllowResetToDefault
Determines whether to show a reset button in the UI to allow resetting the parameter to its default v...
Param(const ParamType Type, QWidget *const Widget, const std::any Destiny, IndexType DefaultIndex=0, const bool AllowResetToDefault=true)
Constructs a Param instance.
const std::any Destiny
Reference to destination variable where to store the parameter's value after editing.