File indexing completed on 2024-05-26 03:51:01

0001 /*
0002     File                 : String2DoubleFilter.h
0003     Project              : AbstractColumn
0004     Description          : Locale-aware conversion filter QString -> double.
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2007 Knut Franke <knut.franke*gmx.de (use @ for *)>
0007     SPDX-FileCopyrightText: 2020 Stefan Gerlach <stefan.gerlach@uni.kn>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef STRING2DOUBLE_FILTER_H
0012 #define STRING2DOUBLE_FILTER_H
0013 
0014 #include "../AbstractSimpleFilter.h"
0015 #include <QLocale>
0016 #include <cmath>
0017 
0018 //! Locale-aware conversion filter QString -> double.
0019 class String2DoubleFilter : public AbstractSimpleFilter {
0020     Q_OBJECT
0021 
0022 public:
0023     String2DoubleFilter() {
0024     }
0025 
0026     double valueAt(int row) const override {
0027         // DEBUG("String2Double::valueAt()");
0028 
0029         if (!m_inputs.value(0))
0030             return 0;
0031 
0032         double result;
0033         bool valid;
0034         if (m_useDefaultLocale) // we need a new QLocale instance here in case the default changed since the last call
0035             result = QLocale().toDouble(m_inputs.value(0)->textAt(row), &valid);
0036         else
0037             result = m_numberLocale.toDouble(m_inputs.value(0)->textAt(row), &valid);
0038 
0039         if (valid)
0040             return result;
0041         return NAN;
0042     }
0043 
0044     //! Return the data type of the column
0045     AbstractColumn::ColumnMode columnMode() const override {
0046         return AbstractColumn::ColumnMode::Double;
0047     }
0048 
0049 protected:
0050     //! Using typed ports: only string inputs are accepted.
0051     bool inputAcceptable(int, const AbstractColumn* source) override {
0052         return source->columnMode() == AbstractColumn::ColumnMode::Text;
0053     }
0054 };
0055 
0056 #endif // ifndef STRING2DOUBLE_FILTER_H