📄 paramdialog.cc
字号:
/** @file CommandLine - class representing command window (editation of command and output of previous commands)*/#include "paramdialog.h"#include "base.h"#include "util.h"#include <QComboBox>#include <QDoubleValidator>#include <QFrame>#include <QGridLayout>#include <QIntValidator>#include <QLabel>#include <QLayout>#include <QLineEdit>#include <QPushButton>#include <QResizeEvent>#include <QSizePolicy>#include <assert.h>using namespace std;using namespace util;namespace gui {/** Widget for editing single parameter */class ParamEditor: public QWidget {public: /** Constructor @param parent Parent widget */ ParamEditor(QWidget *parent=0) : QWidget(parent) {} /** Set value to be edited @param value to be edited in widget */ virtual void setValue(const QString &value)=0; /** Get value @return edited value */ virtual QString getValue() const=0; /** destructor */ virtual ~ParamEditor() {}};/** Widget for editing single parameter of type string */class StringEditor: public ParamEditor {protected: /** Lineedit for the value */ QLineEdit *ed;public: /** Constructor @param parent Parent widget */ StringEditor(QWidget *parent=0) : ParamEditor(parent) { ed=new QLineEdit(this); } virtual void setValue(const QString &value) { ed->setText(value); } virtual QString getValue() const { return ed->text(); } /** return size hint @return size hint from inner editbox */ QSize sizeHint() const { return ed->sizeHint(); } /** destructor */ virtual ~StringEditor() {}protected: /** Called on resizing Will simply set the same fixed size to inner editbox @param e resize event */ virtual void resizeEvent (QResizeEvent *e) { ed->setFixedSize(e->size()); }};/** Widget for editing single parameter of type int (integer value) */class IntEditor: public StringEditor {public: /** Constructor @param parent Parent widget */ IntEditor(QWidget *parent=0) : StringEditor(parent) { ed->setValidator(new QIntValidator(ed)); }};/** Widget for editing single parameter of type double (floating point value) */class DoubleEditor: public StringEditor {public: /** Constructor @param parent Parent widget */ DoubleEditor(QWidget *parent=0) : StringEditor(parent) { ed->setValidator(new QDoubleValidator(ed)); }};/** Widget for editing single parameter by using a combobox */class ComboEditor: public ParamEditor {protected: /** Lineedit for the value */ QComboBox *ed; /** List of values in the control. */ QStringList values;public: /** Constructor @param parent Parent widget @param _values List to use as item values @param _desc List to use as item descriptions */ ComboEditor(QWidget *parent,const QStringList &_values, const QStringList &_desc) : ParamEditor(parent) { ed=new QComboBox(this); values=_values; ed->setEditable(false); ed->setDuplicatesEnabled(true); ed->insertItems(0,_desc); ed->setInsertPolicy(QComboBox::NoInsert); } virtual void setValue(const QString &value) { if (value.isNull()) return; //Look for item in the list int valuesCount=values.count(); int idx=0; for(int i=0;i<valuesCount;i++) { if (values[i]==value) { idx=i; break; } } //Use first match ed->setCurrentIndex(idx); } virtual QString getValue() const { int idx=ed->currentIndex(); return values[idx]; } /** return size hint @return size hint from inner editbox */ QSize sizeHint() const { return ed->sizeHint(); } /** destructor */ virtual ~ComboEditor() {}protected: /** Called on resizing Will simply set the same fixed size to inner editbox @param e resize event */ virtual void resizeEvent (QResizeEvent *e) { ed->setFixedSize(e->size()); }};/** Constructor of Dialog. Create basic empty dialog @param windowTitle Window title for the dialog @param parent Parent widget*/ParamDialog::ParamDialog(const QString &windowTitle,Base *_base,QWidget *parent/*=0*/):QDialog(parent) { base=_base; nObjects=0; setWindowTitle(windowTitle); QGridLayout* grl_up=new QGridLayout(this); grl_up->setRowStretch(0,1); grl_up->setRowStretch(1,0); //create main widget main=new QWidget(this); ml=new QGridLayout(main); ml->setColumnStretch(0,1); ml->setColumnStretch(1,10); ml->setSpacing(4); ml->setMargin(8); grl_up->addWidget(main,0,0); //Bottom part QFrame *low=new QFrame(this); grl_up->addWidget(low,1,0); QGridLayout* grl=new QGridLayout(low); grl->setColumnStretch(0,10); grl->setColumnStretch(1,1); grl->setColumnStretch(2,1); grl->setSpacing(4); grl->setMargin(4); QWidget *leftLabel=new QWidget(low); grl->addWidget(leftLabel,0,0); QPushButton* btOk= new QPushButton(QObject::tr("&Ok"),low); QPushButton* btCancel=new QPushButton(QObject::tr("&Cancel"),low); grl->addWidget(btOk,0,1); grl->addWidget(btCancel,0,2); QObject::connect(btCancel, SIGNAL(clicked()), this, SLOT(reject())); QObject::connect(btOk, SIGNAL(clicked()), this, SLOT(accept()));}/** Return widget for editing values of given type @param type Identifier of value type*/ParamEditor* ParamDialog::getEditor(const QString &type) { if (type=="int") return new IntEditor(main); if (type=="float") return new DoubleEditor(main); //Check for enum if (base->isEnumType(type)) { return new ComboEditor(main,base->enumKeys(type),base->enumValues(type)); } //TODO: All types //else - default string return new StringEditor(main);}/** Add textual help to the window @param text text to add*/void ParamDialog::addText(const QString &text) { assert(main); QLabel *txt=new QLabel(text,main); assert(txt); txt->setTextFormat(Qt::RichText); txt->setWordWrap(true); assert(ml); ml->addWidget(txt,nObjects,0,1,2); ml->setRowMinimumHeight(nObjects,txt->sizeHint().height()); nObjects++;}/** add parameter to the window @param name Name (id) of the parameter @param caption Caption of the parameter @param type Type of the parameter @param value Current value of the parameter @param helpText helptext for current parameter */void ParamDialog::addParam(const QString &name, const QString &caption, const QString &type, const QString &value/*=QString::null*/, const QString &helpText/*=QString::null*/) { ParamEditor *widget=getEditor(type); if (!value.isNull()) widget->setValue(value); params[name]=widget; addParam(caption,widget); if (!helpText.isNull()) addText(helpText);}/** add parameter to the window @param caption Caption of the parameter @param param Widget for editing parameter */void ParamDialog::addParam(const QString &caption,QWidget *param) { QLabel *label; label=new QLabel(caption,main); int labelHeight=label->sizeHint().height(); int paramHeight=param->sizeHint().height(); int lineHeight=MAX(labelHeight,paramHeight); ml->setRowMinimumHeight(nObjects,lineHeight); ml->addWidget(label,nObjects,0); ml->addWidget(param,nObjects,1); label->setFixedHeight(lineHeight); param->setFixedHeight(lineHeight); nObjects++; param->show(); label->show();}/** Set value of parameter with given name @param name name of parameter @param value New value*/void ParamDialog::setValue(const QString &name, const QString &value) const { ParamEditor *p=params.value(name); if (!p) return; p->setValue(value);}/** Return value of parameter with given name @param name name of parameter*/QString ParamDialog::getValue(const QString &name) const { ParamEditor *p=params.value(name); if (!p) return QString::null; return p->getValue();}/** default destructor */ParamDialog::~ParamDialog() {}} // namespace gui
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -