Browse Source

Disallow entry of negative quantities of green coffee in new batch window

Neal Wilson 5 years ago
parent
commit
a56c5cfb29
Signed by: neal <neal@typica.us> GPG Key ID: D6F25C90DD5819DF
2 changed files with 33 additions and 8 deletions
  1. 1
    1
      config/Windows/newbatch.xml
  2. 32
    7
      src/typica.w

+ 1
- 1
config/Windows/newbatch.xml View File

@@ -48,7 +48,7 @@
48 48
                                 <label>Green Coffee:</label>
49 49
                                 <sqltablearray columns="3" id="greens">
50 50
                                     <column name="Coffee" delegate="sql" showdata="true" null="true" nulltext="Delete" nulldata="delete" data="0" display="1">SELECT id, name FROM coffees WHERE quantity &lt;&gt; 0 ORDER BY name</column>
51
-                                    <column name="Weight" delegate="numeric" />
51
+                                    <column name="Weight" delegate="positivenumeric" />
52 52
                                     <column name="Remaining" />
53 53
                                 </sqltablearray>
54 54
                                 <layout type="horizontal">

+ 32
- 7
src/typica.w View File

@@ -5984,6 +5984,10 @@ else if(currentElement.attribute("delegate") == "numeric")
5984 5984
 {
5985 5985
     @<Assign numeric column delegate@>@;
5986 5986
 }
5987
+else if(currentElement.attribute("delegate") == "positivenumeric")
5988
+{
5989
+	@<Assign positive numeric column delegate@>@;
5990
+}
5987 5991
 
5988 5992
 @ When using a |SaltModel|, there are times where the array values being
5989 5993
 inserted are identification numbers representing some record that already exists
@@ -6028,14 +6032,19 @@ widget->addSqlOptions(currentElement.text());
6028 6032
 delegate->setWidget(widget);
6029 6033
 view->setItemDelegateForColumn(currentColumn, delegate);
6030 6034
 
6031
-@ Another common use is allowing numeric values. At present this only
6032
-restricts input to numbers, however it may be useful to provide other options
6033
-such as restricting the range of allowed values in the future.
6035
+@ Another common use is allowing numeric values.
6034 6036
 
6035 6037
 @<Assign numeric column delegate@>=
6036 6038
 NumericDelegate *delegate = new NumericDelegate;
6037 6039
 view->setItemDelegateForColumn(currentColumn, delegate);
6038 6040
 
6041
+@ It is also possible to restrict allowed numeric values to non-negative
6042
+values.
6043
+
6044
+@<Assign positive numeric column delegate@>=
6045
+NumericDelegate *delegate = new NumericDelegate(true);
6046
+view->setItemDelegateForColumn(currentColumn, delegate);
6047
+
6039 6048
 @ The |NumericDelegate| will only set the display value to a number, but it
6040 6049
 will perform mathematical calculations that are entered into the editor as
6041 6050
 well. This allows a person to type something like $13.26+5.06$ with the result
@@ -6046,7 +6055,7 @@ class NumericDelegate : public QItemDelegate@/
6046 6055
 {
6047 6056
     @[Q_OBJECT@]@;
6048 6057
     public:@/
6049
-        NumericDelegate(QObject *parent = NULL);
6058
+        NumericDelegate(bool positiveOnly = false, QObject *parent = NULL);
6050 6059
         QWidget *createEditor(QWidget *parent,
6051 6060
                               const QStyleOptionViewItem &option,@|
6052 6061
                               const QModelIndex &index) const;
@@ -6056,13 +6065,15 @@ class NumericDelegate : public QItemDelegate@/
6056 6065
         void updateEditorGeometry(QWidget *editor,
6057 6066
                                   const QStyleOptionViewItem &option,@|
6058 6067
                                   const QModelIndex &index) const;
6068
+	private:
6069
+		bool m_positiveOnly;
6059 6070
 };
6060 6071
 
6061 6072
 @ There is nothing special about the constructor.
6062 6073
 
6063 6074
 @<NumericDelegate implementation@>=
6064
-NumericDelegate::NumericDelegate(QObject *parent) :
6065
-    QItemDelegate(parent)
6075
+NumericDelegate::NumericDelegate(bool positiveOnly, QObject *parent) :
6076
+    QItemDelegate(parent), m_positiveOnly(positiveOnly)
6066 6077
 {
6067 6078
     /* Nothing needs to be done here. */
6068 6079
 }
@@ -6099,7 +6110,21 @@ void NumericDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
6099 6110
     QScriptValue result = engine->evaluate(line->text());
6100 6111
     if(result.isNumber())
6101 6112
     {
6102
-        model->setData(index, result.toVariant(), Qt::DisplayRole);
6113
+		if(m_positiveOnly)
6114
+		{
6115
+			if(result.toNumber() < 0)
6116
+			{
6117
+				model->setData(index, QVariant(), Qt::DisplayRole);
6118
+			}
6119
+			else
6120
+			{
6121
+				model->setData(index, result.toVariant(), Qt::DisplayRole);
6122
+			}
6123
+		}
6124
+		else
6125
+		{
6126
+			model->setData(index, result.toVariant(), Qt::DisplayRole);
6127
+		}
6103 6128
     }
6104 6129
     else
6105 6130
     {

Loading…
Cancel
Save