Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include "metadialog.h"
  2. #include "configtypes.h"
  3.  
  4. metadialog::metadialog(QWidget *parent)
  5.     : QDialog(parent)
  6. {
  7. }
  8.  
  9. metadialog::~metadialog() {
  10.     //TODO - deallocate everything here? does the ok/reject deallocate the graphical elements automatically?
  11.     foreach(metadata_t m, *thismetalist) {
  12.         if(m.entry) {
  13.             delete m.entry;
  14.             m.entry=NULL;
  15.         }
  16.     }
  17. }
  18.  
  19.  
  20. //brings up the dialog, is non blocking, return code needs to be handled: negative is internal error, zero "ok", one "cancelled"
  21. //textedited signals are used to automatically populate the
  22. qint8 metadialog::rundialog(QList<metadata_t>* meta) {
  23.     quint8 h=0;
  24.     thisdialoglayout = new QVBoxLayout();
  25.     thismetalist=meta;
  26.     foreach(metadata_t m, *meta) {//loop through
  27.         QGroupBox *groupBox;
  28.         QLineEdit *l;
  29.         if(m.type!=META_INVALID) {
  30.             groupBox = new QGroupBox(QCoreApplication::translate(m.title));//create and title the group box
  31.             if(m.type<META_CHECK) {//we will need a lineedit
  32.                 l = new QLineEdit();
  33.                 groupBox->addWidget(l);
  34.             }
  35.         }
  36.         switch(m.type) {
  37.             case META_INVALID:
  38.                 return -1;//error code
  39.             break;
  40.             case META_TEXT:
  41.                 l->setInputMask("AAAAAAAAAAAAAAAAAAA");//check if this works with fewer characters
  42.             break;
  43.             case META_INT:
  44.                 l->setValidator(new QIntValidator(l));
  45.                 l->setMaxLength(20);
  46.             break;
  47.             case META_FLOAT:
  48.                 l->setValidator(new QDoubleValidator(-99999.0,99999.0, 2, l));//TODO - add option to set limits here from config file?
  49.                 l->setMaxLength(20);
  50.             break;
  51.             case META_FREE:
  52.                 l->setMaxLength(20);//its always limited to number of characters
  53.             break;
  54.             case META_CHECK:
  55.                 for(quint8 n=0;n<m.checkboxes.length();n++) {
  56.                     QRadiobutton *r=new QRadioButton(tr(m.checkboxes[n]));
  57.                     groupBox->addWidget(r);
  58.                     connect(r,static_cast<void(QRadioButton::*)(bool)>(&QRadioButton::toggled),[=](bool chk){checkhandler(n,&((*m)[h]));});
  59.                 }
  60.             break;
  61.             default:
  62.                 return -2;//unsupported error code
  63.         }
  64.         if(m.type<META_CHECK)
  65.             connect(l,static_cast<void(QLineEdit::*)(QString)>(&QLineEdit::textEdited),[=](QString txt){lineedithandler(txt,l,&((*m)[h]));});
  66.         h++;//index into the metadata input list
  67.         if(thisdialoglayout)
  68.             thisdialoglayout->addWidget(groupbox);
  69.     }
  70.     //"ok" and "cancel" buttons go here
  71.     QDialogButtonBox *b=new QDialogButtonBox((QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  72.     connect(b, SIGNAL(accepted()), this, SLOT(accept()));
  73.     connect(b, SIGNAL(rejected()), this, SLOT(reject()));
  74.     if(thisdialoglayout) {
  75.         thisdialoglayout->addWidget(b);
  76.         setLayout(thisdialoglayout);
  77.         setWindowTitle(tr("Please enter details:"));
  78.     }
  79.     this.show();
  80.     return 0;
  81. }
  82.  
  83. //this is a slot used for all the lineedit inputs (first 4) attached to textEdited signal
  84. void metadialog::lineedithandler(QString txt, QLineEdit *l, metadata_t* m) {
  85.     if(l->hasAcceptableInput()) {//only if we meet the input rule
  86.         if(m->entry)//wipe any previously allocated entry string
  87.             free m->entry;
  88.         m->entry=new QString(txt);
  89.     }
  90.     else if(m->entry)//try to restore the previous value as it must have met the validator
  91.         l->setText(*(m->entry));
  92. }
  93.  
  94. //this is slot used for the checkbuttons (last type), attached to toggled signal
  95. void metadialog::checkhandler(int button, metadata_t* m) {
  96.     if(button<m->checkboxes.length()) {//this is a sanity check
  97.         if(m->entry)//wipe any previously allocated entry string
  98.             free m->entry;
  99.         m->entry=new QString(m->checkboxes[button]);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement