00001
00002 #include "vgui_qt_dialog_impl.h"
00003 #include "vgui_qt_adaptor.h"
00004
00005 #include <vcl_vector.h>
00006 #include <vcl_sstream.h>
00007 #include <vcl_iostream.h>
00008 #include <vcl_limits.h>
00009
00010 #include <vgui/internals/vgui_dialog_impl.h>
00011 #include <vgui/internals/vgui_simple_field.h>
00012
00013 #include <QPushButton>
00014 #include <QLabel>
00015 #include <QSpinBox>
00016 #include <QCheckBox>
00017 #include <QComboBox>
00018 #include <QFileDialog>
00019 #include <QColorDialog>
00020 #include <QHBoxLayout>
00021 #include <QFrame>
00022 #include <QVBoxLayout>
00023
00024 static bool is_modal = true;
00025 void vgui_qt_dialog_impl::modal (bool m) {is_modal=m; }
00026
00027
00028 vgui_qt_dialog_impl::vgui_qt_dialog_impl(const char* name)
00029 : QDialog(), vgui_dialog_impl(name)
00030 {
00031 setWindowTitle(name);
00032 setModal(is_modal);
00033 }
00034
00035
00036
00037 bool vgui_qt_dialog_impl::ask()
00038 {
00039 bool use_ok_button = !ok_button_text_. empty ();
00040 bool use_cancel_button = !cancel_button_text_. empty ();
00041
00042 QVBoxLayout* total = new QVBoxLayout;
00043 this->setLayout(total);
00044 QVBoxLayout* layout = new QVBoxLayout;
00045 total->addLayout(layout);
00046
00047 if (use_ok_button || use_cancel_button)
00048 {
00049 QHBoxLayout* lower = new QHBoxLayout;
00050 total->addLayout(lower);
00051 lower->addStretch(1);
00052
00053 QPushButton *ok = 0;
00054 if (use_ok_button)
00055 {
00056 ok = new QPushButton( ok_button_text_.c_str(), this );
00057 connect( ok, SIGNAL(clicked()), SLOT(accept()) );
00058 lower->addWidget(ok, 0);
00059 lower->addStretch(1);
00060 }
00061 if (use_cancel_button)
00062 {
00063 QPushButton *cancel = new QPushButton(cancel_button_text_.c_str(), this);
00064 connect( cancel, SIGNAL(clicked()), SLOT(reject()) );
00065 lower->addWidget(cancel, 0);
00066 lower->addStretch(1);
00067 if (use_ok_button)
00068 {
00069 ok->setMinimumSize(cancel->width(), cancel->height());
00070 ok->setMaximumSize(cancel->width(), cancel->height());
00071 }
00072 }
00073 }
00074
00075 for (vcl_vector<element>::iterator ei = elements.begin();
00076 ei != elements.end(); ++ei)
00077 {
00078 element l = (*ei);
00079 QWidget* widget = static_cast<QWidget*>(l.widget);
00080 if (!widget) vcl_cerr << "No QWidget defined for element type " << l.type << vcl_endl;
00081 else
00082 {
00083 if (l.type == int_elem ||
00084 l.type == long_elem ||
00085 l.type == float_elem ||
00086 l.type == double_elem ||
00087 l.type == string_elem ||
00088 l.type == bool_elem ||
00089 l.type == choice_elem)
00090 {
00091 QHBoxLayout* hbox = new QHBoxLayout;
00092 layout->addLayout(hbox);
00093 QLabel* label = new QLabel(l.field->label.c_str(), this);
00094 hbox->addWidget(label, 0);
00095 hbox->addStretch(1);
00096 hbox->addWidget(widget, 0);
00097 }
00098 else if (l.type == text_msg)
00099 {
00100 layout->addWidget(widget, 0);
00101 }
00102 else if (l.type == file_bsr)
00103 {
00104 layout->addWidget(widget, 0);
00105 }
00106 else if (l.type == inline_file_bsr)
00107 {
00108 layout->addWidget(widget, 0);
00109 }
00110 else if (l.type == color_csr)
00111 {
00112 layout->addWidget(widget, 0);
00113 }
00114 else if (l.type == inline_color_csr)
00115 {
00116 layout->addWidget(widget, 0);
00117 }
00118 else if (l.type == inline_tabl)
00119 {
00120 layout->addWidget(widget, 0);
00121 }
00122 }
00123 }
00124
00125 bool result = exec();
00126
00127 if (result)
00128 {
00129 for (vcl_vector<element>::iterator ei = elements.begin();
00130 ei != elements.end(); ++ei)
00131 {
00132 element l = (*ei);
00133 if (l.type == long_elem ||
00134 l.type == float_elem ||
00135 l.type == double_elem ||
00136 l.type == string_elem)
00137 {
00138 QLineEdit* input = static_cast<QLineEdit*>(l.widget);
00139 l.field->update_value(input->text().toAscii().data());
00140 }
00141 else if (l.type == int_elem)
00142 {
00143 QSpinBox* input = static_cast<QSpinBox*>(l.widget);
00144 l.field->update_value(input->text().toAscii().data());
00145 }
00146 else if (l.type == bool_elem)
00147 {
00148 QCheckBox* button = static_cast<QCheckBox*>(l.widget);
00149 vgui_bool_field* field = static_cast<vgui_bool_field*>(l.field);
00150 field->var = button->isChecked();
00151 }
00152 else if (l.type == choice_elem)
00153 {
00154 QComboBox* box = static_cast<QComboBox*>(l.widget);
00155 vgui_int_field *field = static_cast<vgui_int_field*>(l.field);
00156 field->var = box->currentIndex();
00157 }
00158 else if ((l.type == inline_file_bsr) || (l.type == file_bsr))
00159 {
00160 vgui_qt_filebrowser_impl* fileb = static_cast<vgui_qt_filebrowser_impl*>(l.widget);
00161 l.field->update_value(fileb->file());
00162 }
00163 else if ((l.type == inline_color_csr) || (l.type == color_csr))
00164 {
00165 vgui_qt_colorchooser_impl* fileb = static_cast<vgui_qt_colorchooser_impl*>(l.widget);
00166 l.field->update_value(fileb->color());
00167 }
00168 }
00169 }
00170
00171 return result;
00172 }
00173
00174
00175
00176 void* vgui_qt_dialog_impl::bool_field_widget(const char* txt, bool& v)
00177 {
00178 QCheckBox* widget = new QCheckBox(this);
00179 widget->setChecked(v);
00180 return widget;
00181 }
00182
00183
00184
00185 #undef max // picking up max defined in windef.h
00186 void* vgui_qt_dialog_impl::int_field_widget(const char* txt, int& v)
00187 {
00188 QSpinBox* widget = new QSpinBox(this);
00189 widget->setRange(-vcl_numeric_limits<int>::max(),
00190 vcl_numeric_limits<int>::max());
00191 widget->setValue(v);
00192 return widget;
00193 }
00194
00195
00196
00197 void* vgui_qt_dialog_impl::long_field_widget(const char* txt, long& v)
00198 {
00199 QString s; s.setNum(v);
00200 QLineEdit* widget = new QLineEdit(s, this);
00201 widget->setValidator(new QIntValidator(widget));
00202 return widget;
00203 }
00204
00205
00206
00207 void* vgui_qt_dialog_impl::float_field_widget(const char* txt, float& v)
00208 {
00209 QString s; s.setNum(v);
00210 QLineEdit* widget = new QLineEdit(s, this);
00211 widget->setValidator(new QDoubleValidator(widget));
00212 return widget;
00213 }
00214
00215
00216
00217 void* vgui_qt_dialog_impl::double_field_widget(const char* txt, double& v)
00218 {
00219 QString s; s.setNum(v);
00220 QLineEdit* widget = new QLineEdit(s, this);
00221 widget->setValidator(new QDoubleValidator(widget));
00222 return widget;
00223 }
00224
00225
00226
00227 void* vgui_qt_dialog_impl::string_field_widget(const char* txt, vcl_string& v)
00228 {
00229 QLineEdit* widget = new QLineEdit(v.c_str(), this);
00230 return widget;
00231 }
00232
00233
00234
00235 void* vgui_qt_dialog_impl::choice_field_widget(const char* txt, const vcl_vector<vcl_string>& labels, int& v)
00236 {
00237 QComboBox* widget = new QComboBox(this);
00238 for (unsigned int i=0; i<labels.size(); ++i)
00239 widget->insertItem(i, labels[i].c_str());
00240
00241 widget->setCurrentIndex(v);
00242 return widget;
00243 }
00244
00245
00246
00247 void* vgui_qt_dialog_impl::text_message_widget(const char* txt)
00248 {
00249 QLabel* widget = new QLabel(txt, this);
00250 return widget;
00251 }
00252
00253
00254
00255 void* vgui_qt_dialog_impl::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::inline_file_browser_widget(const char * txt,vcl_string& v, vcl_string& s)
00264 {
00265 vgui_qt_filebrowser_impl* widget = new vgui_qt_filebrowser_impl(this, txt, v, s);
00266 return widget;
00267 }
00268
00269
00270
00271 void* vgui_qt_dialog_impl::color_chooser_widget(const char * txt,vcl_string& val)
00272 {
00273 vgui_qt_colorchooser_impl* widget = new vgui_qt_colorchooser_impl(this, txt, val);
00274 return widget;
00275 }
00276
00277
00278
00279 void* vgui_qt_dialog_impl::inline_tableau_widget(const vgui_tableau_sptr tab,
00280 unsigned int width, unsigned int height)
00281 {
00282 vgui_qt_tableau_impl* widget = new vgui_qt_tableau_impl (this, tab, width, height);
00283 return widget;
00284 }
00285
00286
00287
00288 void* vgui_qt_dialog_impl::inline_color_chooser_widget(const char * txt,vcl_string& val)
00289 {
00290 vgui_qt_colorchooser_impl* widget = new vgui_qt_colorchooser_impl(this, txt, val);
00291 return widget;
00292 }
00293
00294
00295
00296 vgui_qt_filebrowser_impl::vgui_qt_filebrowser_impl(QWidget* parent, const vcl_string& t, const vcl_string& v, const vcl_string& s)
00297 : QGroupBox(t.c_str(), parent), title_(t), filter_(v)
00298 {
00299 edit_ = new QLineEdit(s.c_str());
00300 QPushButton* push = new QPushButton("Browse");
00301
00302 QHBoxLayout *hbox = new QHBoxLayout;
00303 hbox->setMargin(2);
00304 hbox->addWidget(edit_);
00305 hbox->addWidget(push);
00306 hbox->addStretch(1);
00307 this->setLayout(hbox);
00308
00309 connect(push, SIGNAL(clicked()), this, SLOT(get_a_file()));
00310 }
00311
00312
00313
00314 void vgui_qt_filebrowser_impl::get_a_file()
00315 {
00316 QString r = QFileDialog::getOpenFileName(this, title_.c_str(), edit_->text(), filter_.c_str());
00317 if (!r.isEmpty()) edit_->setText(r);
00318 }
00319
00320
00321
00322 vgui_qt_colorchooser_impl::vgui_qt_colorchooser_impl(QWidget* parent, const char* txt, vcl_string& val)
00323 : QGroupBox(txt, parent), value_(val)
00324 {
00325 frame_ = new QFrame;
00326 frame_->setFrameStyle(QFrame::Panel | QFrame::Raised);
00327 frame_->setMinimumSize( 32, 32 );
00328 frame_->setMaximumSize( 32, 32 );
00329 frame_->setAutoFillBackground(true);
00330 vcl_stringstream sval(val);
00331 float r,g,b,a=1.0;
00332 sval >> r >> g >> b;
00333 if (sval.good())
00334 sval >> a;
00335
00336 color_.setRgb(static_cast<int>(255*r),
00337 static_cast<int>(255*g),
00338 static_cast<int>(255*b),
00339 static_cast<int>(255*a));
00340
00341 QPushButton* pick = new QPushButton("Pick");
00342 rbox_ = new QSpinBox;
00343 rbox_->setRange(0,255);
00344 gbox_ = new QSpinBox;
00345 gbox_->setRange(0,255);
00346 bbox_ = new QSpinBox;
00347 bbox_->setRange(0,255);
00348 abox_ = new QSpinBox;
00349 abox_->setRange(0,255);
00350
00351 QGridLayout *grid = new QGridLayout;
00352 QHBoxLayout *hbox = new QHBoxLayout;
00353 hbox->addWidget(frame_);
00354 hbox->addWidget(pick);
00355 grid->addLayout(hbox,1,0,1,4);
00356 grid->addWidget(new QLabel("R"),0,0);
00357 grid->addWidget(rbox_,0,1);
00358 grid->addWidget(new QLabel("G"),0,2);
00359 grid->addWidget(gbox_,0,3);
00360 grid->addWidget(new QLabel("B"),0,4);
00361 grid->addWidget(bbox_,0,5);
00362 grid->addWidget(new QLabel("A"),1,4);
00363 grid->addWidget(abox_,1,5);
00364 this->setLayout(grid);
00365
00366 update_color_string();
00367
00368 connect(pick, SIGNAL(clicked()), this, SLOT(get_a_color()));
00369 connect(rbox_, SIGNAL(valueChanged(int)), this, SLOT(change_red(int)));
00370 connect(gbox_, SIGNAL(valueChanged(int)), this, SLOT(change_green(int)));
00371 connect(bbox_, SIGNAL(valueChanged(int)), this, SLOT(change_blue(int)));
00372 connect(abox_, SIGNAL(valueChanged(int)), this, SLOT(change_alpha(int)));
00373 }
00374
00375
00376
00377 void vgui_qt_colorchooser_impl::update_color_string()
00378 {
00379 if (color_.isValid()) {
00380 int r,g,b,a;
00381 color_.getRgb(&r,&g,&b,&a);
00382 vcl_stringstream sval;
00383 sval << r/255.0 << ' ' << g/255.0 << ' ' << b/255.0 << ' ' << a/255.0;
00384 value_ = sval.str();
00385
00386 rbox_->setValue(r);
00387 gbox_->setValue(g);
00388 bbox_->setValue(b);
00389 abox_->setValue(a);
00390
00391 frame_->setPalette(QPalette(color_,color_));
00392 }
00393 }
00394
00395
00396 void vgui_qt_colorchooser_impl::get_a_color()
00397 {
00398 color_ = QColorDialog::getColor(color_);
00399 update_color_string();
00400 }
00401
00402
00403
00404 void vgui_qt_colorchooser_impl::change_red(int r)
00405 {
00406 color_.setRed(r);
00407 update_color_string();
00408 }
00409
00410
00411 void vgui_qt_colorchooser_impl::change_green(int g)
00412 {
00413 color_.setGreen(g);
00414 update_color_string();
00415 }
00416
00417
00418 void vgui_qt_colorchooser_impl::change_blue(int b)
00419 {
00420 color_.setBlue(b);
00421 update_color_string();
00422 }
00423
00424
00425 void vgui_qt_colorchooser_impl::change_alpha(int a)
00426 {
00427 color_.setAlpha(a);
00428 update_color_string();
00429 }
00430
00431
00432 vgui_qt_tableau_impl::vgui_qt_tableau_impl(QWidget* parent,
00433 const vgui_tableau_sptr tab,
00434 unsigned int width, unsigned int height)
00435 : QWidget(parent)
00436 {
00437 this-> setFixedWidth (width);
00438 this-> setFixedHeight (height);
00439
00440
00441 QFrame* frame = new QFrame (this);
00442 frame->setFrameStyle (QFrame::Sunken | QFrame::StyledPanel);
00443 frame->setLineWidth (1);
00444
00445
00446 vgui_qt_adaptor* adaptor = new vgui_qt_adaptor(frame);
00447 adaptor-> set_tableau(tab);
00448
00449 QHBoxLayout* hlayout = new QHBoxLayout (frame);
00450 hlayout->addWidget (adaptor, 1);
00451
00452
00453 QVBoxLayout* vlayout = new QVBoxLayout (this);
00454 vlayout->addWidget(frame, 1);
00455 }