core/vgui/impl/qt/vgui_qt_dialog_impl.cxx
Go to the documentation of this file.
00001 // This is core/vgui/impl/qt/vgui_qt_dialog_impl.cxx
00002 #include "vgui_qt_dialog_impl.h"
00003 #include "vgui_qt_adaptor.h"
00004 
00005 #include <vcl_vector.h>
00006 #include <vcl_iostream.h>
00007 
00008 #include <vgui/internals/vgui_dialog_impl.h>
00009 #include <vgui/internals/vgui_simple_field.h>
00010 
00011 #include <qpushbutton.h>
00012 #include <qlabel.h>
00013 #include <qlineedit.h>
00014 #include <qspinbox.h>
00015 #include <qcheckbox.h>
00016 #include <qlayout.h>
00017 #include <qvalidator.h>
00018 #include <qstring.h>
00019 #include <qcombobox.h>
00020 #include <qfiledialog.h>
00021 #include <qcolordialog.h>
00022 
00023 static bool is_modal = true;
00024 void vgui_qt_dialog_impl::modal (bool m) {is_modal=m; }
00025 
00026 //-----------------------------------------------------------------------------
00027 vgui_qt_dialog_impl::vgui_qt_dialog_impl(const char* name)
00028 :  QDialog(0, name, is_modal), vgui_dialog_impl(name)
00029 {
00030    setCaption(name);
00031 }
00032 
00033 
00034 //-----------------------------------------------------------------------------
00035 bool vgui_qt_dialog_impl::ask()
00036 {
00037    bool use_ok_button = !ok_button_text_. empty ();
00038    bool use_cancel_button =  !cancel_button_text_. empty ();
00039 
00040    QPushButton *ok, *cancel;
00041    QVBoxLayout* total = new QVBoxLayout(this, 10, -1, "totallayout");
00042    QVBoxLayout* layout = new QVBoxLayout(total, -1, "vboxlayout");
00043 
00044    if (use_ok_button || use_cancel_button)
00045    {
00046      QHBoxLayout* lower = new QHBoxLayout(total, -1, "buttonslayout");
00047      lower->addStretch(1);
00048 
00049      if (use_ok_button)
00050      {
00051        ok = new QPushButton( ok_button_text_.c_str(), this );
00052        connect( ok, SIGNAL(clicked()), SLOT(accept()) );
00053        lower->addWidget(ok, 0);
00054        lower->addStretch(1);
00055      }
00056      if (use_cancel_button)
00057      {
00058        cancel = new QPushButton( cancel_button_text_.c_str(), this );
00059        connect( cancel, SIGNAL(clicked()), SLOT(reject()) );
00060        lower->addWidget(cancel, 0);
00061        lower->addStretch(1);
00062      }
00063      if (use_ok_button && use_cancel_button)
00064      {
00065        ok->setMinimumSize(cancel->width(), cancel->height());
00066        ok->setMaximumSize(cancel->width(), cancel->height());
00067      }
00068    }
00069 
00070    for (vcl_vector<element>::iterator ei = elements.begin();
00071         ei != elements.end(); ++ei)
00072    {
00073       element l = (*ei);
00074       QWidget* widget = static_cast<QWidget*>(l.widget);
00075       if (!widget) vcl_cerr << "No QWidget defined for element type " << l.type << vcl_endl;
00076       else
00077       {
00078          if (l.type == int_elem    ||
00079              l.type == long_elem   ||
00080              l.type == float_elem  ||
00081              l.type == double_elem ||
00082              l.type == string_elem ||
00083              l.type == bool_elem   ||
00084              l.type == choice_elem)
00085          {
00086             QHBoxLayout* hbox = new QHBoxLayout(layout, -1);
00087             QLabel* label = new QLabel(widget, l.field->label.c_str(), this);
00088             hbox->addWidget(label, 0);
00089             hbox->addStretch(1);
00090             hbox->addWidget(widget, 0);
00091          }
00092          else if (l.type == text_msg)
00093          {
00094             layout->addWidget(widget, 0);
00095          }
00096          else if (l.type == file_bsr)
00097          {
00098             layout->addWidget(widget, 0);
00099          }
00100          else if (l.type == inline_file_bsr)
00101          {
00102             layout->addWidget(widget, 0);
00103          }
00104          else if (l.type == color_csr)
00105          {
00106             layout->addWidget(widget, 0);
00107          }
00108          else if (l.type == inline_color_csr)
00109          {
00110             layout->addWidget(widget, 0);
00111          }
00112          else if (l.type == inline_tabl)
00113          {
00114             layout->addWidget(widget, 0);
00115          }
00116       }
00117    }
00118 
00119    bool result = exec();
00120 
00121    if (result)
00122    {
00123       for (vcl_vector<element>::iterator ei = elements.begin();
00124            ei != elements.end(); ++ei)
00125       {
00126          element l = (*ei);
00127          if (l.type == long_elem ||
00128              l.type == float_elem ||
00129              l.type == double_elem ||
00130              l.type == string_elem)
00131          {
00132             QLineEdit* input = static_cast<QLineEdit*>(l.widget);
00133             l.field->update_value(input->text().ascii());
00134          }
00135          else if (l.type == int_elem)
00136          {
00137             QSpinBox* input = static_cast<QSpinBox*>(l.widget);
00138             l.field->update_value(input->text().ascii());
00139          }
00140          else if (l.type == bool_elem)
00141          {
00142             QCheckBox* button = static_cast<QCheckBox*>(l.widget);
00143             vgui_bool_field* field = static_cast<vgui_bool_field*>(l.field);
00144             field->var = button->isChecked();
00145          }
00146          else if (l.type == choice_elem)
00147          {
00148             QComboBox* box = static_cast<QComboBox*>(l.widget);
00149             vgui_int_field *field = static_cast<vgui_int_field*>(l.field);
00150             field->var = box->currentItem();
00151          }
00152          else if ((l.type == inline_file_bsr) || (l.type == file_bsr))
00153          {
00154             vgui_qt_filebrowser_impl* fileb = static_cast<vgui_qt_filebrowser_impl*>(l.widget);
00155             l.field->update_value(fileb->file());
00156          }
00157          else if ((l.type == inline_color_csr) || (l.type == color_csr))
00158          {
00159             vgui_qt_colorchooser_impl* fileb = static_cast<vgui_qt_colorchooser_impl*>(l.widget);
00160             l.field->update_value(fileb->color());
00161          }
00162       }
00163    }
00164 
00165    return result;
00166 }
00167 
00168 
00169 //-----------------------------------------------------------------------------
00170 void* vgui_qt_dialog_impl::bool_field_widget(const char* txt, bool& v)
00171 {
00172    QCheckBox* widget = new QCheckBox(this);
00173    widget->setChecked(v);
00174    return widget;
00175 }
00176 
00177 
00178 //-----------------------------------------------------------------------------
00179 void* vgui_qt_dialog_impl::int_field_widget(const char* txt, int& v)
00180 {
00181    QSpinBox* widget = new QSpinBox(-65000, 65000, 1, this);
00182    widget->setValue(v);
00183    return widget;
00184 }
00185 
00186 
00187 //-----------------------------------------------------------------------------
00188 void* vgui_qt_dialog_impl::long_field_widget(const char* txt, long& v)
00189 {
00190    QString s; s.setNum(v);
00191    QLineEdit* widget = new QLineEdit(s, this);
00192    widget->setValidator(new QIntValidator(widget));
00193    return widget;
00194 }
00195 
00196 
00197 //-----------------------------------------------------------------------------
00198 void* vgui_qt_dialog_impl::float_field_widget(const char* txt, float& v)
00199 {
00200    QString s; s.setNum(v);
00201    QLineEdit* widget = new QLineEdit(s, this);
00202    widget->setValidator(new QDoubleValidator(widget));
00203    return widget;
00204 }
00205 
00206 
00207 //-----------------------------------------------------------------------------
00208 void* vgui_qt_dialog_impl::double_field_widget(const char* txt, double& v)
00209 {
00210    QString s; s.setNum(v);
00211    QLineEdit* widget = new QLineEdit(s, this);
00212    widget->setValidator(new QDoubleValidator(widget));
00213    return widget;
00214 }
00215 
00216 
00217 //-----------------------------------------------------------------------------
00218 void* vgui_qt_dialog_impl::string_field_widget(const char* txt, vcl_string& v)
00219 {
00220    QLineEdit* widget = new QLineEdit(v.c_str(), this);
00221    return widget;
00222 }
00223 
00224 
00225 //-----------------------------------------------------------------------------
00226 void* vgui_qt_dialog_impl::choice_field_widget(const char* txt, const vcl_vector<vcl_string>& labels, int& v)
00227 {
00228    QComboBox* widget = new QComboBox(this);
00229    for (vcl_vector<vcl_string>::const_iterator si = labels.begin(); si != labels.end(); ++si)
00230    {
00231       widget->insertItem((*si).c_str());
00232    }
00233    widget->setCurrentItem(v);
00234    return widget;
00235 }
00236 
00237 
00238 //-----------------------------------------------------------------------------
00239 void* vgui_qt_dialog_impl::text_message_widget(const char* txt)
00240 {
00241    QLabel* widget = new QLabel(txt, this, "text_message");
00242    return widget;
00243 }
00244 
00245 
00246 //-----------------------------------------------------------------------------
00247 void* vgui_qt_dialog_impl::file_browser_widget(const char* txt, vcl_string& v, vcl_string& s)
00248 {
00249    vgui_qt_filebrowser_impl* widget = new vgui_qt_filebrowser_impl(this, txt, v, s);
00250    return widget;
00251 }
00252 
00253 
00254 //-----------------------------------------------------------------------------
00255 void* vgui_qt_dialog_impl::inline_file_browser_widget(const char * txt,vcl_string& v, vcl_string& s)
00256 {
00257    vgui_qt_filebrowser_impl* widget = new vgui_qt_filebrowser_impl(this, txt, v, s);
00258    return widget;
00259 }
00260 
00261 
00262 //-----------------------------------------------------------------------------
00263 void* vgui_qt_dialog_impl::color_chooser_widget(const char * txt,vcl_string& val)//, vcl_string& s)
00264 {
00265    vgui_qt_colorchooser_impl* widget = new vgui_qt_colorchooser_impl(this, txt, val);
00266    return widget;
00267 }
00268 
00269 
00270 //-----------------------------------------------------------------------------
00271 void* vgui_qt_dialog_impl::inline_tableau_widget(const vgui_tableau_sptr tab,
00272                                                  unsigned int width, unsigned int height)
00273 {
00274    vgui_qt_tableau_impl* widget = new vgui_qt_tableau_impl (this, tab, width, height);
00275    return  widget;
00276 }
00277 
00278 
00279 //-----------------------------------------------------------------------------
00280 void* vgui_qt_dialog_impl::inline_color_chooser_widget(const char * txt,vcl_string& val)//, vcl_string& s)
00281 {
00282    vgui_qt_colorchooser_impl* widget = new vgui_qt_colorchooser_impl(this, txt, val);
00283    return widget;
00284 }
00285 
00286 
00287 //-----------------------------------------------------------------------------
00288 vgui_qt_filebrowser_impl::vgui_qt_filebrowser_impl(QWidget* parent, const char* txt, vcl_string& v, vcl_string& s)
00289 : QHGroupBox(txt, parent), filter_(v)
00290 {
00291    edit_ = new QLineEdit(s.c_str(), this);
00292    QPushButton* push = new QPushButton("Browse", this, "go_browse");
00293    connect(push, SIGNAL(clicked()), this, SLOT(get_a_file()));
00294 }
00295 
00296 
00297 //-----------------------------------------------------------------------------
00298 void vgui_qt_filebrowser_impl::get_a_file()
00299 {
00300    QString r = QFileDialog::getSaveFileName(edit_->text(), filter_.c_str());
00301    if (!r.isEmpty()) edit_->setText(r);
00302 }
00303 
00304 
00305 //-----------------------------------------------------------------------------
00306 vgui_qt_colorchooser_impl::vgui_qt_colorchooser_impl(QWidget* parent, const char* txt, vcl_string& val)
00307 : QHGroupBox(txt, parent), value_(val)
00308 {
00309    frame_ = new QFrame(this, "farm");
00310    frame_->setLineWidth(2);
00311    frame_->setFrameStyle(QFrame::Sunken | QFrame::Panel);
00312    frame_->setBackgroundColor(QColor(val.c_str()));
00313    QPushButton* push = new QPushButton("Pick", this, "go_browse");
00314    connect(push, SIGNAL(clicked()), this, SLOT(get_a_color()));
00315 }
00316 
00317 
00318 //-----------------------------------------------------------------------------
00319 void vgui_qt_colorchooser_impl::get_a_color()
00320 {
00321    QColor c = QColorDialog::getColor(QColor(value_.c_str()));
00322    if (c.isValid())
00323    {
00324       value_ = c.name().ascii();
00325       frame_->setBackgroundColor(c);
00326    }
00327 }
00328 
00329 
00330 //-----------------------------------------------------------------------------
00331 vgui_qt_tableau_impl::vgui_qt_tableau_impl(QWidget* parent,
00332                                            const vgui_tableau_sptr tab,
00333                                            unsigned int width, unsigned int height)
00334 : QWidget(parent, "vgui_qt_inline_gl_main_widget")
00335 {
00336    this-> setFixedWidth (width);
00337    this-> setFixedHeight (height);
00338 
00339    //Create a frame to store the GL widget
00340    QFrame* frame = new QFrame (this, "vgui_qt_inline_gl_frame");
00341    frame->setFrameStyle (QFrame::Sunken | QFrame::StyledPanel);
00342    frame->setLineWidth (2);
00343 
00344    //Create the GL widget and put it in the frame
00345    vgui_qt_adaptor* adaptor = new vgui_qt_adaptor(frame);
00346    adaptor-> set_tableau(tab);
00347 
00348    QHBoxLayout* hlayout = new QHBoxLayout (frame, 2, 2, "vgui_qt_inline_gl_hlayout");
00349    hlayout->addWidget (adaptor, 1);
00350 
00351    //Top level layout
00352    QVBoxLayout* vlayout = new QVBoxLayout (this, 15, 5, "vgui_qt_inline_gl_vlayout");
00353    vlayout->addWidget(frame, 1);
00354 }