! * \brief NumberSpinBox::properties * Determine the properties of a numeric value represented in a string * \param v_str string representation of a numeric value * \param p properties of the value * \return */
| 136 | * \return |
| 137 | */ |
| 138 | bool NumberSpinBox::properties(const QString& v_str, NumberProperties& p) const { |
| 139 | const auto decimalpoint = locale().decimalPoint(); |
| 140 | p.fractionPos = v_str.indexOf(decimalpoint); |
| 141 | p.exponentPos = v_str.indexOf(QLatin1Char('e'), p.fractionPos > 0 ? p.fractionPos : 0, Qt::CaseInsensitive); |
| 142 | p.groupSeparators = v_str.indexOf(locale().groupSeparator()) != -1 ? true : false; |
| 143 | const auto number_length = v_str.length(); |
| 144 | |
| 145 | bool ok; |
| 146 | |
| 147 | if (v_str.at(0) == QLatin1Char('+') || v_str.at(0) == QLatin1Char('-')) |
| 148 | p.integerSign = v_str.at(0); |
| 149 | |
| 150 | p.fraction = false; |
| 151 | // integer properties |
| 152 | if (p.fractionPos >= 0) { |
| 153 | p.fraction = true; |
| 154 | const auto integer_str = v_str.mid(!p.integerSign.isNull(), p.fractionPos - !p.integerSign.isNull()); |
| 155 | p.integer = locale().toInt(integer_str, &ok); |
| 156 | if (!ok) |
| 157 | return false; |
| 158 | p.intergerDigits = integer_str.length() - p.groupSeparators; |
| 159 | |
| 160 | QString fraction_str; |
| 161 | if (number_length - 1 > p.fractionPos) { |
| 162 | int end = number_length; |
| 163 | if (p.exponentPos > 0) |
| 164 | end = p.exponentPos; |
| 165 | fraction_str = v_str.mid(p.fractionPos + 1, end - (p.fractionPos + 1)); |
| 166 | } |
| 167 | p.fractionDigits = fraction_str.length(); |
| 168 | } else if (p.exponentPos > 0) { |
| 169 | const auto integer_str = v_str.mid(!p.integerSign.isNull(), p.exponentPos - !p.integerSign.isNull()); |
| 170 | p.integer = locale().toInt(integer_str, &ok); |
| 171 | if (!ok) |
| 172 | return false; |
| 173 | p.intergerDigits = integer_str.length() - p.groupSeparators; |
| 174 | } else { |
| 175 | const auto integer_str = v_str.mid(!p.integerSign.isNull(), number_length - !p.integerSign.isNull()); |
| 176 | p.integer = locale().toInt(integer_str, &ok); |
| 177 | if (!ok) |
| 178 | return false; |
| 179 | p.intergerDigits = integer_str.length() - p.groupSeparators; |
| 180 | } |
| 181 | |
| 182 | if (p.exponentPos > 0) { |
| 183 | if (v_str.at(p.exponentPos + 1) == QLatin1Char('+') || v_str.at(p.exponentPos + 1) == QLatin1Char('-')) |
| 184 | p.exponentSign = v_str.at(p.exponentPos + 1); |
| 185 | const QString& e = v_str.mid(p.exponentPos + 1 + !p.exponentSign.isNull(), number_length - (p.exponentPos + 1 + !p.exponentSign.isNull())); |
| 186 | p.exponentDigits = e.length(); |
| 187 | p.exponent = e.toInt(&ok); |
| 188 | if (!ok) |
| 189 | return false; |
| 190 | p.exponentLetter = v_str.at(p.exponentPos); |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | /*! |
no test coverage detected