Advertisement
Guest User

Untitled

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