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 "ChoiceListDialog.h"
13#include "TextEditor.h"
14
15#include "stdafx.h"
16
17namespace Ui
18{
19 class ParamsConfig;
20}
21
22namespace DynExp
23{
24 class DynExpCore;
25 class Object;
26 class ObjectLinkBase;
27
32 enum class TextUsageType { Standard, Path, Code };
46}
47
52{
58 ParamInfo(std::string Title, std::string Description)
59 : Title(std::move(Title)), Description(std::move(Description)) {}
60
64 ParamInfo(std::string_view Title, std::string_view Description)
66
67 const std::string Title;
68 const std::string Description;
69};
70
75class ParamsConfigDialog : public QDialog
76{
77 Q_OBJECT
78
79public:
80 using NumberType = double;
85
91 using IndexType = qulonglong;
92
93private:
98 using FunctionsToCallIfAcceptedType = std::function<void(void)>;
99
104 struct Param
105 {
154 Param() = delete;
155
168
170 QWidget* const Widget;
171 const std::any Destiny;
174 };
175
176public:
183 ParamsConfigDialog(QWidget* parent, const DynExp::DynExpCore& Core, std::string Title);
184
186
197 void AddParam(ParamInfo&& Info, const std::any Destiny, const NumberType Value,
198 const NumberType MinValue, const NumberType MaxValue, const NumberType Precision, const NumberType Increment);
199
207 void AddParam(ParamInfo&& Info, const std::any Destiny, const TextType Value, const DynExp::TextUsageType TextUsage);
208
219 void AddParam(ParamInfo&& Info, const std::any Destiny, const TextRefType Value,
220 const TextListType& TextList, const bool AllowResetToDefault);
221
232 void AddParam(ParamInfo&& Info, const std::any Destiny, const TextListIndexType Value,
233 const TextListIndexType DefaultValue, const TextListType& TextList);
234
246 template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int> = 0>
247 void AddParam(ParamInfo&& Info, const std::any Destiny, const EnumType Value,
248 const EnumType DefaultValue, const Util::TextValueListType<EnumType>& TextValueList);
249
261 void AddParam(ParamInfo&& Info, const std::any Destiny, const DynExp::ItemIDType Value,
262 bool IsOptional, std::string_view IconResourcePath, FunctionsToCallIfAcceptedType FunctionToCallIfAccepted,
263 Util::TextValueListType<IndexType>&& ItemIDsWithLabels);
264
275 void AddParam(ParamInfo&& Info, const std::any Destiny, const std::vector<DynExp::ItemIDType>& Values,
276 bool IsOptional, std::string_view IconResourcePath, FunctionsToCallIfAcceptedType FunctionToCallIfAccepted,
277 Util::TextValueListType<IndexType>&& ItemIDsWithLabels);
278
283 size_t GetNumParams() const noexcept { return ParamList.size(); }
284
290 bool IsResetRequired() const noexcept { return ResetRequired; }
291
297 bool Display(DynExp::Object* Object = nullptr);
298
299private:
305 void InsertWidget(ParamInfo&& Info, Param&& ParamData);
306
315 template <typename ParamT>
316 void Assign(ParamT& Param, typename ParamT::UnderlyingType Value);
317
326 template <typename ParamT>
327 void Assign(ParamT& Param, const std::vector<typename ParamT::UnderlyingType::value_type>& Values);
328
334
338 std::unique_ptr<Ui::ParamsConfig> ui;
339
344
348 std::vector<Param> ParamList;
349
354 std::vector<FunctionsToCallIfAcceptedType> FunctionsToCallIfAccepted;
355
361
367
372 std::vector<TextEditor*> TextEditorDialogs;
373
374private slots:
375 void OnOpenParam();
376 void OnEditParam();
377 void OnResetParam();
378 virtual void accept() override;
379 virtual void reject() override;
380};
381
382template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int>>
383void ParamsConfigDialog::AddParam(ParamInfo&& Info, const std::any Destiny, const EnumType Value,
384 const EnumType DefaultValue, const Util::TextValueListType<EnumType>& TextValueList)
385{
386 using IntegerType = std::underlying_type_t<EnumType>;
387 constexpr Param::ParamType ParamType = std::is_signed_v<IntegerType> ? Param::ParamType::SignedInteger : Param::ParamType::UnsignedInteger;
388
389 auto ComboBox = new QComboBox(this);
390 int SelectedIndex = 0;
391 int DefaultIndex = 0;
392
393 for (const auto& TextValuePair : TextValueList)
394 {
395 if (Value == TextValuePair.second)
396 SelectedIndex = ComboBox->count();
397 if (DefaultValue == TextValuePair.second)
398 DefaultIndex = ComboBox->count();
399
400 ComboBox->addItem(QString::fromStdString(TextValuePair.first), QVariant(static_cast<IntegerType>(TextValuePair.second)));
401 }
402 ComboBox->setCurrentIndex(SelectedIndex);
403
404 Param ParamData(ParamType, ComboBox, Destiny, DefaultIndex);
405 InsertWidget(std::move(Info), std::move(ParamData));
406}
407
408template <typename ParamT>
409void ParamsConfigDialog::Assign(ParamT& Param, typename ParamT::UnderlyingType Value)
410{
411 ResetRequired |= Param != Value && Param.GetNeedsResetToApplyChange();
412 Param = std::move(Value);
413}
414
415template <typename ParamT>
416void ParamsConfigDialog::Assign(ParamT& Param, const std::vector<typename ParamT::UnderlyingType::value_type>& Values)
417{
418 ResetRequired |= Param.GetNeedsResetToApplyChange();
419 Param = std::move(Values);
420}
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...
std::unique_ptr< 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.
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.