mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
ASCII import dialog. Determine default decimal separator
This commit is contained in:
@@ -103,6 +103,17 @@ QString mapCellSeparator(RicPasteAsciiDataToSummaryPlotFeatureUi::CellSeparator
|
|||||||
return "";
|
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:
|
case DECIMAL_COMMA:
|
||||||
parseOptions.decimalSeparator = ",";
|
parseOptions.decimalSeparator = ",";
|
||||||
parseOptions.locale = QLocale::Norwegian;
|
parseOptions.locale = RifCsvUserDataParser::localeFromDecimalSeparator(",");
|
||||||
break;
|
break;
|
||||||
case DECIMAL_DOT:
|
case DECIMAL_DOT:
|
||||||
default:
|
default:
|
||||||
parseOptions.decimalSeparator = ".";
|
parseOptions.decimalSeparator = ".";
|
||||||
parseOptions.locale = QLocale::c();
|
parseOptions.locale = RifCsvUserDataParser::localeFromDecimalSeparator(".");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -340,6 +351,12 @@ void RicPasteAsciiDataToSummaryPlotFeatureUi::initialize(RifCsvUserDataParser* p
|
|||||||
if (!cellSep.isEmpty())
|
if (!cellSep.isEmpty())
|
||||||
{
|
{
|
||||||
m_cellSeparator = mapCellSeparator(cellSep);
|
m_cellSeparator = mapCellSeparator(cellSep);
|
||||||
|
|
||||||
|
QString decimalSep = parser->tryDetermineDecimalSeparator(cellSep);
|
||||||
|
if (!decimalSep.isEmpty())
|
||||||
|
{
|
||||||
|
m_decimalSeparator = mapDecimalSeparator(decimalSep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parser->parseColumnInfo(parseOptions().cellSeparator);
|
parser->parseColumnInfo(parseOptions().cellSeparator);
|
||||||
|
|||||||
@@ -371,6 +371,55 @@ QString RifCsvUserDataParser::tryDetermineCellSeparator()
|
|||||||
return "";
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
#include <QLocale>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -51,6 +52,9 @@ public:
|
|||||||
QString previewText(int lineCount, const QString& cellSeparator);
|
QString previewText(int lineCount, const QString& cellSeparator);
|
||||||
|
|
||||||
QString tryDetermineCellSeparator();
|
QString tryDetermineCellSeparator();
|
||||||
|
QString tryDetermineDecimalSeparator(const QString& cellSeparator);
|
||||||
|
|
||||||
|
static QLocale localeFromDecimalSeparator(const QString& decimalSeparator);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual QTextStream* openDataStream() = 0;
|
virtual QTextStream* openDataStream() = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user