File: QMultipleInputDialog.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (93 lines) | stat: -rw-r--r-- 2,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Note: this structure is inspired from the QInputDialog that allows
// the user to easily create a dialog to get one value (integer,
// double or string). This structure allows the user to easily create
// a form inside a QDialog to get as manu values as needed.

#ifndef CGAL_QMULTIPLEINPUTDIALOG_H
#define CGAL_QMULTIPLEINPUTDIALOG_H

#include <QDialog>
#include <QFormLayout>
#include <QDialogButtonBox>
#include <QRadioButton>

class QMultipleInputDialog
{
  QDialog* dialog;
  QFormLayout* form;
  std::map<std::string, QWidget*> map_widgets;


public:
  QMultipleInputDialog (const char* name, QWidget* parent)
  {
    dialog = new QDialog (parent);
    dialog->setWindowTitle (name);
    form = new QFormLayout(dialog);
  }
  ~QMultipleInputDialog ()
  {
    delete dialog;
  }

  template <typename QObjectType>
  QObjectType* add (const char* name, const char* key = nullptr)
  {
    QObjectType* out = nullptr;

    if (std::is_same<QObjectType, QRadioButton>::value)
    {
      out = dynamic_cast<QObjectType*>(new QRadioButton (QString(name), dialog));
      form->addRow (out);
    }
    else
    {
      out = new QObjectType (dialog);
      form->addRow (QString(name), out);
    }

    if (key != nullptr)
      map_widgets.insert (std::make_pair (key, out));

    return out;
  }

  template <typename QObjectType>
  const QObjectType* get (const char* key) const
  {
    typename std::map<std::string, QWidget*>::const_iterator
      found = map_widgets.find (key);
    if (found == map_widgets.end())
      return nullptr;

    QWidget* widget_out = found->second;
    return qobject_cast<QObjectType*>(widget_out);
  }

  int exec()
  {
    QDialogButtonBox* oknotok = new QDialogButtonBox
      (QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
       Qt::Horizontal, dialog);

    form->addRow (oknotok);
    QObject::connect (oknotok, SIGNAL(accepted()), dialog, SLOT(accept()));
    QObject::connect (oknotok, SIGNAL(rejected()), dialog, SLOT(reject()));

    return dialog->exec();
  }

  void exec_no_cancel()
  {
    QDialogButtonBox* ok = new QDialogButtonBox
      (QDialogButtonBox::Ok, Qt::Horizontal, dialog);

    form->addRow (ok);
    QObject::connect (ok, SIGNAL(accepted()), dialog, SLOT(accept()));
    dialog->exec();
  }
};



#endif // CGAL_QMULTIPLEINPUTDIALOG_H