]> https://gitweb.dealii.org/ - parameter_gui.git/commitdiff
Simplify data item delegate, respect bounds more strictly 8/head
authorRene Gassmoeller <rene.gassmoeller@mailbox.org>
Fri, 7 Apr 2017 23:07:04 +0000 (17:07 -0600)
committerRene Gassmoeller <rene.gassmoeller@mailbox.org>
Fri, 7 Apr 2017 23:23:58 +0000 (17:23 -0600)
parameter_delegate.cpp

index f4c7a40360791613d9c8a82eb4ddb5e0338d9b6a..c28a1f2d5217e1b89b13e9e6fef4e4b29ba3d06f 100644 (file)
@@ -260,63 +260,22 @@ namespace dealii
     {
       if (index.column() == value_column)
         {
-          QString pattern_description = index.data(Qt::StatusTipRole).toString();      // load pattern description
-                                                                                       // stored in the StatusLine
-
-
-          QRegExp  rx_string("\\b(Anything|MultipleSelection|Map)\\b"),
-                   rx_list("\\b(List)\\b"),
-                   rx_filename("\\b(FileName)\\b"),
-                   rx_dirname("\\b(DirectoryName)\\b"),
-                   rx_selection("\\b(Selection)\\b");
-
-          if (rx_string.indexIn (pattern_description) != -1)                          // if the type is "Anything"
-            {
-              QItemDelegate::setEditorData(editor, index);
-            }
-          else if (rx_list.indexIn (pattern_description) != -1)                   // if the type is "List"
-            {
-              if (rx_filename.indexIn (pattern_description) != -1)
-                {
-                  QString  file_name = index.data(Qt::DisplayRole).toString();
-
-                  BrowseLineEdit *filename_editor = qobject_cast<BrowseLineEdit *>(editor); // set the text of the editor
-                  filename_editor->setText(file_name);
-                }
-              else
-                {
-                  QItemDelegate::setEditorData(editor, index);
-                }
-            }
-          else if (rx_filename.indexIn (pattern_description) != -1)                    // if the type is "FileName"
+          if (BrowseLineEdit * filename_editor = qobject_cast<BrowseLineEdit *>(editor))
             {
-              QString  file_name = index.data(Qt::DisplayRole).toString();
-
-              BrowseLineEdit *filename_editor = qobject_cast<BrowseLineEdit *>(editor);        // set the text of the editor
+              QString file_name = index.data(Qt::DisplayRole).toString();
               filename_editor->setText(file_name);
             }
-          else if (rx_dirname.indexIn (pattern_description) != -1)                     // if the type is "DirectoryName"
-            {
-              QString  dir_name = index.data(Qt::DisplayRole).toString();
-
-              BrowseLineEdit *dirname_editor = qobject_cast<BrowseLineEdit *>(editor); // set the text of the editor
-              dirname_editor->setText(dir_name);
-            }
-          else if (rx_selection.indexIn (pattern_description) != -1)                   // if we have a combo box,
+          else if (QComboBox * combo_box = qobject_cast<QComboBox *>(editor))
             {
               QRegExp  rx(index.data(Qt::DisplayRole).toString());
 
-              QComboBox * combo_box = qobject_cast<QComboBox *>(editor);
-
-              for (int i=0; i<combo_box->count(); ++i)                                 // we look, which index
-                if (rx.exactMatch(combo_box->itemText(i)))                             // the data has and set
-                  combo_box->setCurrentIndex(i);                                       // it to the combo_box
+              for (int i=0; i<combo_box->count(); ++i)                                  // we look, which index
+                if (rx.exactMatch(combo_box->itemText(i)))                              // the data has and set
+                  combo_box->setCurrentIndex(i);
             }
           else
-            QItemDelegate::setEditorData(editor, index);                               // if it is not FileName,
-                                                                                       // DirectoryName or Selection
-                                                                                       // use the standard delegate
-        };
+            QItemDelegate::setEditorData(editor, index);
+        }
     }
 
 
@@ -335,54 +294,36 @@ namespace dealii
     {
       if (index.column() == value_column)
         {
-          QString pattern_description = index.data(Qt::StatusTipRole).toString();      // load pattern description
-                                                                                       // stored in the StatusLine
-
-          QRegExp  rx_string("\\b(Anything|MultipleSelection|Map)\\b"),
-                   rx_list("\\b(List)\\b"),
-                   rx_filename("\\b(FileName)\\b"),
-                   rx_dirname("\\b(DirectoryName)\\b"),
-                   rx_selection("\\b(Selection)\\b");
-
-          if (rx_string.indexIn (pattern_description) != -1)                          // if the type is "Anything"
+          if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(editor))
             {
-              QItemDelegate::setModelData(editor, model, index);
-            }
-          else if (rx_list.indexIn (pattern_description) != -1)                   // if the type is "List"
-            {
-              if (rx_filename.indexIn (pattern_description) != -1)
+              QString line_edit_content = line_edit->text();
+              int position = 0;
+
+              // If the editor has a validator, only accept the content if it contains
+              // correct input. Otherwise bad cases
+              // can happen, when some unfinished number violates the bounds, and
+              // the user clicks elsewhere to lose the focus of the editor.
+              if (line_edit->validator())
                 {
-                  BrowseLineEdit * filename_editor = qobject_cast<BrowseLineEdit *>(editor);        // set the text from the editor
-                  QString value = filename_editor->text();
-                  model->setData(index, value);
+                  if (line_edit->validator()->validate(line_edit_content,position) == QValidator::Acceptable)
+                    model->setData(index, line_edit_content);
                 }
               else
-                {
-                  QItemDelegate::setModelData(editor, model, index);
-                }
+                model->setData(index, line_edit_content);
             }
-          else if (rx_filename.indexIn (pattern_description) != -1)                            // if the type is "FileName"
+          else if (BrowseLineEdit * filename_editor = qobject_cast<BrowseLineEdit *>(editor))
             {
-              BrowseLineEdit * filename_editor = qobject_cast<BrowseLineEdit *>(editor);       // set the text from the editor
               QString value = filename_editor->text();
               model->setData(index, value);
             }
-          else if (rx_dirname.indexIn (pattern_description) != -1)                     // if the type is "DirectoryName"
+          else if (QComboBox * combo_box = qobject_cast<QComboBox *>(editor))
             {
-              BrowseLineEdit * dirname_editor = qobject_cast<BrowseLineEdit *>(editor);        // set the text from the editor
-              QString value = dirname_editor->text();
-              model->setData(index, value);
-            }
-          else if (rx_selection.indexIn (pattern_description) != -1)                   // if the type is "Selection"
-            {
-              QComboBox * combo_box = qobject_cast<QComboBox *>(editor);               // set the text from the editor
               QString value = combo_box->currentText();
               model->setData(index, value);
             }
           else
-            QItemDelegate::setModelData(editor, model, index);                         // if it is not FileName or DirectoryName,
-                                                                                       // use the standard delegate
-        };
+            QItemDelegate::setModelData(editor, model, index);
+        }
     }
   }
 }

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.