]> https://gitweb.dealii.org/ - parameter_gui.git/commitdiff
Handle selection parameters with spaces correctly 5/head
authorRene Gassmoeller <rene.gassmoeller@mailbox.org>
Thu, 30 Mar 2017 19:44:51 +0000 (13:44 -0600)
committerRene Gassmoeller <rene.gassmoeller@mailbox.org>
Thu, 30 Mar 2017 19:56:47 +0000 (13:56 -0600)
parameter_delegate.cpp

index 462486aafc46dbf2fff4830435e909cfb7b2fbc3..7a3dcd803768a3f5d6677c164d10b2edc49ba723 100644 (file)
@@ -173,33 +173,28 @@ namespace dealii
             }
           else if (rx_selection.indexIn (pattern_description) != -1)           // and selections
             {
-              QComboBox * combo_box = new QComboBox(parent);                   // we assume, that pattern_desctiption is of the form
-                                                                               // "Type: [Selection item1|item2| ....|item ]    "
-              std::vector<std::string> choices;                                        // list with the different items
-              std::string  tmp(pattern_description.toStdString());
-
-              if (tmp.find("[") != std::string::npos)                          // delete all char before [
-                tmp.erase (0, tmp.find("[")+1);
-
-              if (tmp.find("]") != std::string::npos)                          // delete all char after ]
-                tmp.erase (tmp.find("]"),tmp.length());
+              QComboBox * combo_box = new QComboBox(parent);
 
-              if (tmp.find(" ") != std::string::npos)                          // delete all char before " "
-                tmp.erase (0, tmp.find(" ")+1);
+              // we assume, that pattern_description is of the form
+              // "Type: [Selection item1|item2| ....|item ]".
+              // Find the first space after the first '[',
+              // which indicates the start of the first option
+              int begin_pattern = pattern_description.indexOf("[");
+              begin_pattern = pattern_description.indexOf(" ",begin_pattern) + 1;
 
-              while (tmp.find('|') != std::string::npos)                       // extract items
-                {
-                  choices.push_back(std::string(tmp, 0, tmp.find('|')));
-                  tmp.erase (0, tmp.find('|')+1);
-                };
+              // Find the last ']', which signals the end of the options
+              const int end_pattern = pattern_description.lastIndexOf("]");
 
-              if (tmp.find(" ") != std::string::npos)                          // delete " "
-                tmp.erase (tmp.find(" "));
+              // Extract the options from the string
+              QString pattern = pattern_description.mid(begin_pattern,end_pattern-begin_pattern);
 
-              choices.push_back(tmp);                                          // add last item
+              // Remove trailing whitespaces
+              while (pattern.endsWith(' '))
+                pattern.chop(1);
 
-              for (unsigned int i=0; i<choices.size(); ++i)                    // add items to the combo box
-                combo_box->addItem (tr(choices[i].c_str()), tr(choices[i].c_str()));
+              // Split the list
+              const QStringList choices = pattern.split("|");
+              combo_box->addItems(choices);
 
               combo_box->setEditable(false);
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.