inputDialog.cpp
Go to the documentation of this file.
1 
12 #include <stdio.h>
13 #include "gui/inputDialog.hpp"
14 #include <QGridLayout>
15 #include <QLineEdit>
16 #include <QDoubleValidator>
17 #include <QPushButton>
18 
19 
20 /* The dialog is build with a given parent and title.
21  *
22  * Parameter parent the parent widget of the dialog,
23  * title the title of the dialog,
24  * msg the message displayed on the top.
25  */
26 OkCancelDialog::OkCancelDialog(QWidget* parent, const char* title,
27  const char* msg)
28  : QDialog(parent), layout(this), text( tr(msg) ),
29  buttons_layout(this), index(1) {
30  setModal(true); // sets modality
31  setWindowTitle( tr(title) ); // defines the title
32  layout.addWidget(&text); // inserts the upper label
33  layout.addWidget(&buttons_layout); // and the button layout
34  const int nb_btn = 2, dft_btn = 1; // number of buttons, default #
35  QDialogButtonBox::StandardButton std_btn[nb_btn] // the buttons
36  = {QDialogButtonBox::Cancel, QDialogButtonBox::Ok};
37  // the array of methods associated to the buttons
38  const char* btn_slots[nb_btn]
39  = {SLOT( reject() ), SLOT( accept() )};
40  for(int btn_idx = 0; btn_idx < nb_btn; btn_idx++) {
41  // creates each button, add it to the layout
42  QPushButton* btn = buttons_layout.addButton(std_btn[btn_idx]);
43  // set it to be the default or not
44  btn->setDefault(btn_idx == dft_btn);
45  // connects the button to the right slot
46  connect(btn, SIGNAL( clicked() ), this, btn_slots[btn_idx]);
47  } // end of for (each button)
48 } // end of OkCancelDialog::OkCancelDialog(QWidget*, ...) ------------
49 
50 // Asks in a dialog for a set of doubles, and returns it.
51 //
52 // Parameter parent the parent widget of the dialog,
53 // title the dialog title,
54 // msg the dialog message,
55 // nb_lines the number of lines in the dialog,
56 // nb_col the number of columns in the dialog,
57 // nameVal the array of names displayed,
58 // isGreek the array indicating wether name is in Greek,
59 // val the array of doubles to get from the user.
60 //
61 // Precondition the arrays need to have at least nb_lines x nb_col
62 // elements, or a segmentation violation may arrise.
63 //
64 // Return True if the array was changed, false otherwise.
66 (QWidget* parent, const char* title, const char* msg,
67  const int nb_lines, const int nb_col, const char* nameVal[],
68  const bool isGreek[], double val[]) {
69  const int nbVal = nb_lines * nb_col;
70  OkCancelDialog dialog(parent, title, msg); // the dialog
71  QWidget values_wdgt(&dialog); // the values widget
72  QGridLayout values_layout(&values_wdgt); // values layout (grid)
73  QLineEdit valBox[nbVal]; // its value boxes
74  QDoubleValidator doubleValid(&values_wdgt); // they take doubles
75  int idx;
76  for(idx = 0; idx < nbVal; idx++) { // fills the widget
77  const int lg = idx / nb_col, col = 2 * (idx % nb_col);
78  char name[25]; // text label: emphasize if not greek
79  sprintf(name, "%s%s%s", isGreek[idx] ? "<span>" : "<em>",
80  nameVal[idx], isGreek[idx] ? "</span>:" : "</em>:");
81  values_layout.addWidget(new QLabel( tr(name) ), lg, col);
82  valBox[idx].setText( QString("%L1").arg(val[idx]) );
83  valBox[idx].setValidator(&doubleValid); // accepts only double
84  values_layout.addWidget(&valBox[idx], lg, col + 1);
85  } // end of for (each value)
86  dialog.insertWidget(values_wdgt); // add the values widget
87  // shows the dialog, waits until it closes
88  // and check whether changes were set
89  const bool res = (dialog.exec() == QDialog::Accepted);
90  for(idx = 0; res && (idx < nbVal); idx++) // extracts the values
91  val[idx] = valBox[idx].text().toDouble(); // from the boxes
92  return res;
93 } // end of void OkCancelDialog::getDoubleArray(...) -----------------
94 
95 // Asks in a dialog for an oriented point.
96 //
97 // Parameter parent the parent widget of the dialog,
98 // title the dialog title,
99 // msg the dialog message,
100 // q the oriented point to be set.
101 //
102 // Return True if the array was changed, false otherwise.
103 bool OkCancelDialog::getOrPtConfig(QWidget* parent,
104  const char* title, const char* msg,
105  iSeeML::rob::OrPtConfig &q) {
106  static const int nb_val = 3; // config. -> 3 values
107  static const char* name_val[nb_val]
108  = {"x", "y", "&theta;"};
109  static const bool val_Greek[nb_val]
110  = {false, false, true};
111  std::string ext_msg(msg);
112  ext_msg += " (angle is in degres):";
113  double val[nb_val] = {0, 0, 0};
114  const bool res = getDoubleArray(parent, title, ext_msg.c_str(), 1,
115  nb_val, name_val, val_Greek, val);
116  if (res) {
117  val[2] = iSeeML::Object::deg2rad(val[2]);
118  q = iSeeML::rob::OrPtConfig(iSeeML::geom::Point(val[0], val[1]),
119  val[2]);
120  }
121  return res;
122 } // end of OkCancelDialog::getOrPtConfig(...) -----------------------
123 
124 // Asks in a dialog for an state.
125 //
126 // Parameter parent the parent widget of the dialog,
127 // title the dialog title,
128 // msg the dialog message,
129 // state the state to be set.
130 //
131 // Return True if the array was changed, false otherwise.
132 bool OkCancelDialog::getState(QWidget* parent, const char* title,
133  const char* msg, State &state) {
134  static const int nb_val = 6; // state -> 6 values
135  static const char* name_val[nb_val]
136  = {"t", "x", "y", "&theta;", "v", "&omega;"};
137  static const bool val_Greek[nb_val]
138  = {false, false, false, true, false, true};
139  std::string ext_msg(msg);
140  ext_msg += " (angle are in degres):";
141  double val[nb_val] = {0, 0, 0, 0, 0, 0};
142  const bool res = getDoubleArray(parent, title, ext_msg.c_str(), 2,
143  nb_val / 2, name_val, val_Greek, val);
144  if (res) {
145  val[3] = iSeeML::Object::deg2rad(val[3]);
146  val[5] = iSeeML::Object::deg2rad(val[5]);
147  state = State( val[0], iSeeML::rob::OrPtConfig
148  (iSeeML::geom::Point(val[1], val[2]), val[3]),
149  val[4], val[5] );
150  }
151  return res;
152 } // end of OkCancelDialog::getState(...) ----------------------------
Qt dialogs to get an array of double values or an oriented point, and also base class of the controll...
This class defines a sub-class of dialog windows which lower part is a horizontal button panel with o...
Definition: inputDialog.hpp:27
QLabel text
The upper label.
Definition: inputDialog.hpp:29
OkCancelDialog(QWidget *parent, const char *title, const char *msg)
The dialog is build with a given parent and title.
Definition: inputDialog.cpp:26
static bool getDoubleArray(QWidget *parent, const char *title, const char *msg, const int nb_lines, const int nb_col, const char *nameVal[], const bool isGreek[], double val[])
Asks in a dialog for a set of doubles, and returns it.
Definition: inputDialog.cpp:66
void insertWidget(QWidget &widget)
Inserts the given widget (containing the inputs) in the dialog, between message and buttons...
Definition: inputDialog.hpp:92
This class defines a state, i.e. a configuration and its (translation and rotation) velocities...
Definition: state.hpp:22
QDialogButtonBox buttons_layout
The button layout.
Definition: inputDialog.hpp:30
static bool getOrPtConfig(QWidget *parent, const char *title, const char *msg, iSeeML::rob::OrPtConfig &q)
Asks in a dialog for an oriented point.
QVBoxLayout layout
The main layout (vertic.)
Definition: inputDialog.hpp:28
static bool getState(QWidget *parent, const char *title, const char *msg, State &state)
Asks in a dialog for a state.


qt_ctrl
Author(s): Alexis Scheuer
autogenerated on Wed Dec 16 2020 15:51:32