ASCII import dialog. Determine default decimal separator

This commit is contained in:
Bjørn Erik Jensen 2017-11-27 12:39:12 +01:00
parent e53059e2c8
commit bdc680bc03
3 changed files with 72 additions and 2 deletions

View File

@ -103,6 +103,17 @@ QString mapCellSeparator(RicPasteAsciiDataToSummaryPlotFeatureUi::CellSeparator
return "";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicPasteAsciiDataToSummaryPlotFeatureUi::DecimalSeparator mapDecimalSeparator(const QString& sep)
{
if (sep == ".") return RicPasteAsciiDataToSummaryPlotFeatureUi::DECIMAL_DOT;
if (sep == ",") return RicPasteAsciiDataToSummaryPlotFeatureUi::DECIMAL_COMMA;
return RicPasteAsciiDataToSummaryPlotFeatureUi::DECIMAL_DOT;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -173,12 +184,12 @@ const AsciiDataParseOptions RicPasteAsciiDataToSummaryPlotFeatureUi::parseOption
{
case DECIMAL_COMMA:
parseOptions.decimalSeparator = ",";
parseOptions.locale = QLocale::Norwegian;
parseOptions.locale = RifCsvUserDataParser::localeFromDecimalSeparator(",");
break;
case DECIMAL_DOT:
default:
parseOptions.decimalSeparator = ".";
parseOptions.locale = QLocale::c();
parseOptions.locale = RifCsvUserDataParser::localeFromDecimalSeparator(".");
break;
}
}
@ -340,6 +351,12 @@ void RicPasteAsciiDataToSummaryPlotFeatureUi::initialize(RifCsvUserDataParser* p
if (!cellSep.isEmpty())
{
m_cellSeparator = mapCellSeparator(cellSep);
QString decimalSep = parser->tryDetermineDecimalSeparator(cellSep);
if (!decimalSep.isEmpty())
{
m_decimalSeparator = mapDecimalSeparator(decimalSep);
}
}
parser->parseColumnInfo(parseOptions().cellSeparator);

View File

@ -371,6 +371,55 @@ QString RifCsvUserDataParser::tryDetermineCellSeparator()
return "";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RifCsvUserDataParser::tryDetermineDecimalSeparator(const QString& cellSeparator)
{
QTextStream* dataStream = openDataStream();
std::vector<QString> lines;
int iLine = 0;
int successfulParsesDot = 0;
int successfulParsesComma = 0;
while (iLine < 10 && !dataStream->atEnd())
{
QString line = dataStream->readLine();
if (line.isEmpty()) continue;
for (QString cellData : splitLineAndTrim(line, cellSeparator))
{
bool parseOk;
QLocale locale;
locale = localeFromDecimalSeparator(".");
locale.toDouble(cellData, &parseOk);
if (parseOk) successfulParsesDot++;
locale = localeFromDecimalSeparator(",");
locale.toDouble(cellData, &parseOk);
if (parseOk) successfulParsesComma++;
}
}
closeDataStream();
if (successfulParsesComma > successfulParsesDot) return ",";
else return ".";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QLocale RifCsvUserDataParser::localeFromDecimalSeparator(const QString& decimalSeparator)
{
if (decimalSeparator == ",")
{
return QLocale::Norwegian;
}
return QLocale::c();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -27,6 +27,7 @@
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <QLocale>
#include <vector>
@ -51,6 +52,9 @@ public:
QString previewText(int lineCount, const QString& cellSeparator);
QString tryDetermineCellSeparator();
QString tryDetermineDecimalSeparator(const QString& cellSeparator);
static QLocale localeFromDecimalSeparator(const QString& decimalSeparator);
protected:
virtual QTextStream* openDataStream() = 0;