Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "metadialog.h"
- #include "configtypes.h"
- metadialog::metadialog(QWidget *parent)
- : QDialog(parent)
- {
- }
- metadialog::~metadialog() {
- //TODO - deallocate everything here? does the ok/reject deallocate the graphical elements automatically?
- foreach(metadata_t m, *thismetalist) {
- if(m.entry) {
- delete m.entry;
- m.entry=NULL;
- }
- }
- }
- //brings up the dialog, is non blocking, return code needs to be handled: negative is internal error, zero "ok", one "cancelled"
- //textedited signals are used to automatically populate the
- qint8 metadialog::rundialog(QList<metadata_t>* meta) {
- quint8 h=0;
- thisdialoglayout = new QVBoxLayout();
- thismetalist=meta;
- foreach(metadata_t m, *meta) {//loop through
- QGroupBox *groupBox;
- QLineEdit *l;
- if(m.type!=META_INVALID) {
- groupBox = new QGroupBox(QCoreApplication::translate(m.title));//create and title the group box
- if(m.type<META_CHECK) {//we will need a lineedit
- l = new QLineEdit();
- groupBox->addWidget(l);
- }
- }
- switch(m.type) {
- case META_INVALID:
- return -1;//error code
- break;
- case META_TEXT:
- l->setInputMask("AAAAAAAAAAAAAAAAAAA");//check if this works with fewer characters
- break;
- case META_INT:
- l->setValidator(new QIntValidator(l));
- l->setMaxLength(20);
- break;
- case META_FLOAT:
- l->setValidator(new QDoubleValidator(-99999.0,99999.0, 2, l));//TODO - add option to set limits here from config file?
- l->setMaxLength(20);
- break;
- case META_FREE:
- l->setMaxLength(20);//its always limited to number of characters
- break;
- case META_CHECK:
- for(quint8 n=0;n<m.checkboxes.length();n++) {
- QRadiobutton *r=new QRadioButton(tr(m.checkboxes[n]));
- groupBox->addWidget(r);
- connect(r,static_cast<void(QRadioButton::*)(bool)>(&QRadioButton::toggled),[=](bool chk){checkhandler(n,&((*m)[h]));});
- }
- break;
- default:
- return -2;//unsupported error code
- }
- if(m.type<META_CHECK)
- connect(l,static_cast<void(QLineEdit::*)(QString)>(&QLineEdit::textEdited),[=](QString txt){lineedithandler(txt,l,&((*m)[h]));});
- h++;//index into the metadata input list
- if(thisdialoglayout)
- thisdialoglayout->addWidget(groupbox);
- }
- //"ok" and "cancel" buttons go here
- QDialogButtonBox *b=new QDialogButtonBox((QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
- connect(b, SIGNAL(accepted()), this, SLOT(accept()));
- connect(b, SIGNAL(rejected()), this, SLOT(reject()));
- if(thisdialoglayout) {
- thisdialoglayout->addWidget(b);
- setLayout(thisdialoglayout);
- setWindowTitle(tr("Please enter details:"));
- }
- this.show();
- return 0;
- }
- //this is a slot used for all the lineedit inputs (first 4) attached to textEdited signal
- void metadialog::lineedithandler(QString txt, QLineEdit *l, metadata_t* m) {
- if(l->hasAcceptableInput()) {//only if we meet the input rule
- if(m->entry)//wipe any previously allocated entry string
- free m->entry;
- m->entry=new QString(txt);
- }
- else if(m->entry)//try to restore the previous value as it must have met the validator
- l->setText(*(m->entry));
- }
- //this is slot used for the checkbuttons (last type), attached to toggled signal
- void metadialog::checkhandler(int button, metadata_t* m) {
- if(button<m->checkboxes.length()) {//this is a sanity check
- if(m->entry)//wipe any previously allocated entry string
- free m->entry;
- m->entry=new QString(m->checkboxes[button]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement