DynExp
Highly flexible laboratory automation for dynamically changing experiments.
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 
18 namespace DynExp
19 {
20  class DynExpCore;
21  class Object;
22  class ObjectLinkBase;
23 
28  enum class TextUsageType { Standard, Path, Code };
42 }
43 
47 struct ParamInfo
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 
71 class ParamsConfigDialog : public QDialog
72 {
73  Q_OBJECT
74 
75 public:
76  using NumberType = double;
81 
87  using IndexType = qulonglong;
88 
89 private:
94  using FunctionsToCallIfAcceptedType = std::function<void(void)>;
95 
100  struct Param
101  {
150  Param() = delete;
151 
160  Param(const ParamType Type, QWidget* const Widget, const std::any Destiny,
161  IndexType DefaultIndex = 0, const bool AllowResetToDefault = true)
164 
165  const ParamType Type;
166  QWidget* const Widget;
167  const std::any Destiny;
169  const bool AllowResetToDefault;
170  };
171 
172 public:
179  ParamsConfigDialog(QWidget* parent, const DynExp::DynExpCore& Core, std::string Title);
180 
181  ~ParamsConfigDialog() = default;
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 
295 private:
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 
370 private slots:
371  void OnOpenParam();
372  void OnEditParam();
373  void OnResetParam();
374  virtual void accept() override;
375  virtual void reject() override;
376 };
377 
378 template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int>>
379 void 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 
404 template <typename ParamT>
405 void ParamsConfigDialog::Assign(ParamT& Param, typename ParamT::UnderlyingType Value)
406 {
407  ResetRequired |= Param != Value && Param.GetNeedsResetToApplyChange();
408  Param = std::move(Value);
409 }
410 
411 template <typename ParamT>
412 void 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...
Definition: ParamsConfig.h:72
Util::TextListIndexType TextListIndexType
List index type of Util::TextListType.
Definition: ParamsConfig.h:80
size_t GetNumParams() const noexcept
Returns the number of parameters added to the configuration dialog.
Definition: ParamsConfig.h:279
std::vector< TextEditor * > TextEditorDialogs
Keeps a list of text editor dialogs so that their ownership can be removed when the ParamsConfigDialo...
Definition: ParamsConfig.h:363
qulonglong IndexType
ID type of objects/items managed by DynExp.
Definition: ParamsConfig.h:87
bool ResetRequired
Becomes true if an existing DynExp::Object is edited and needs to be reset to apply changes after cli...
Definition: ParamsConfig.h:357
Ui::ParamsConfig ui
Bundles Qt widgets of the ParamsConfigDialog instance's user interface.
Definition: ParamsConfig.h:368
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 ...
Definition: ParamsConfig.h:405
std::function< void(void)> FunctionsToCallIfAcceptedType
Signature of a function which is called when the ParamsConfigDialog is closed by the user by clicking...
Definition: ParamsConfig.h:94
double NumberType
Number type used for numeric parameters (DynExp::ParamsBase::Param)
Definition: ParamsConfig.h:76
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...
Definition: ParamsConfig.h:351
const DynExp::DynExpCore & Core
Reference to DynExp's core.
Definition: ParamsConfig.h:334
Util::TextRefType TextRefType
Reference-to-string type of text-type parameters (DynExp::ParamsBase::Param)
Definition: ParamsConfig.h:78
Util::TextListType TextListType
List type of text-type parameters.
Definition: ParamsConfig.h:79
bool IsResetRequired() const noexcept
Determines whether the DynExp::Object instance whose parameters have been edited needs to be reset to...
Definition: ParamsConfig.h:286
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.
ParamsConfigDialog(QWidget *parent, const DynExp::DynExpCore &Core, std::string Title)
Constructs a ParamsConfigDialog instance.
std::vector< Param > ParamList
List of all parameters assigned to this ParamsConfigDialog instance.
Definition: ParamsConfig.h:339
std::vector< FunctionsToCallIfAcceptedType > FunctionsToCallIfAccepted
List of functions to be called when acceptig the dialog. Refer to ParamsConfigDialog::FunctionsToCall...
Definition: ParamsConfig.h:345
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)
Definition: ParamsConfig.h:77
constexpr auto Info
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...
Definition: ParamsConfig.h:28
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.
Definition: ParamsConfig.h:48
ParamInfo(std::string Title, std::string Description)
Constructs an instance and initializes Title and Description.
Definition: ParamsConfig.h:54
ParamInfo(std::string_view Title, std::string_view Description)
Constructs an instance and initializes Title and Description.
Definition: ParamsConfig.h:60
const std::string Title
Refer to DynExp::ParamsBase::ParamBase::ParamTitle.
Definition: ParamsConfig.h:63
const std::string Description
Refer to DynExp::ParamsBase::ParamBase::ParamDescription.
Definition: ParamsConfig.h:64
Collection of data defining a single parameter to be managed by a ParamsConfigDialog instance....
Definition: ParamsConfig.h:101
QWidget *const Widget
Widget to edit/show the parameter's value in the UI.
Definition: ParamsConfig.h:166
ParamType
Identifies the type of a parameter shown in the user interface. Also refer to DynExp::TextUsageType.
Definition: ParamsConfig.h:105
const ParamType Type
Type of the managed parameter.
Definition: ParamsConfig.h:165
const IndexType DefaultIndex
Default selection index for combo box-based parameters used in case of a parameter reset.
Definition: ParamsConfig.h:168
const bool AllowResetToDefault
Determines whether to show a reset button in the UI to allow resetting the parameter to its default v...
Definition: ParamsConfig.h:169
Param(const ParamType Type, QWidget *const Widget, const std::any Destiny, IndexType DefaultIndex=0, const bool AllowResetToDefault=true)
Constructs a Param instance.
Definition: ParamsConfig.h:160
const std::any Destiny
Reference to destination variable where to store the parameter's value after editing.
Definition: ParamsConfig.h:167