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:
parent
e53059e2c8
commit
bdc680bc03
ApplicationCode
Commands/SummaryPlotCommands
FileInterface
@ -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);
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user