moved two protected methods from JKQTPCoordinateAxis to jkqtpstringtools.h/.cpp

This commit is contained in:
jkriege2 2024-02-06 14:48:21 +01:00
parent e63f053896
commit 0009ff8f64
4 changed files with 46 additions and 31 deletions

View File

@ -835,6 +835,31 @@ QString jkqtp_floattohtmlqstr(double data, int past_comma, bool remove_trail0, d
return QString::fromStdString(jkqtp_floattohtmlstr(data, past_comma, remove_trail0, belowIsZero, minNoExponent, maxNoExponent));
}
QString jkqtp_floattoqstr(const QLocale & loc, double data, char format, int past_comma, bool remove_trail0)
{
QString res=loc.toString(data, format, past_comma);
if (remove_trail0 && res.contains(loc.decimalPoint())) {
QString expo="";
const int expIdx=res.toLower().indexOf('e');
if (expIdx>=0) {
expo=res.mid(expIdx);
res=res.left(expIdx);
}
while (res.size()>0 && res[res.size()-1]=='0') {
res=res.left(res.size()-1);
}
if (res.size()>0 && res[res.size()-1]==loc.decimalPoint()) res=res.left(res.size()-1);
res=res+expo;
}
return res;
}
QString jkqtp_floattoqstr(double data, char format, int past_comma, bool remove_trail0)
{
QLocale loc=QLocale::system();
loc.setNumberOptions(QLocale::OmitGroupSeparator);
return jkqtp_floattoqstr(loc, data, format, past_comma, remove_trail0);
}
QString jkVariantListToString(const QList<QVariant>& data, const QString& separator) {
QString r="";
@ -1184,3 +1209,8 @@ bool jkqtp_rxExactlyMatches(const QString& text, const QString &regex, QStringLi
return res;
#endif
}
QString jkqtp_floattolatexunitqstr(double data, int past_comma, bool remove_trail0, double belowIsZero)
{
return QString::fromStdString(jkqtp_floattolatexunitstr(data, past_comma, remove_trail0, belowIsZero));
}

View File

@ -188,6 +188,16 @@ JKQTCOMMON_LIB_EXPORT std::string jkqtp_uinttostr(unsigned long data);
*/
JKQTCOMMON_LIB_EXPORT std::string jkqtp_floattostr(double data, int past_comma=-1, bool remove_trail0=false, double belowIsZero=1e-16);
/** \brief convert a float to a string using \a format (\c f|e|E|g|G ) with given number of decimals after comma \a past_comma and optional removal of trailing zeros behind decimal separator \a remove_trail0. Uses given QLocal \a loc .
* \ingroup jkqtptools_string
*/
JKQTCOMMON_LIB_EXPORT QString jkqtp_floattoqstr(const QLocale & loc, double data, char format='f', int past_comma=-1, bool remove_trail0=false);
/** \brief convert a float to a string using \a format (\c f|e|E|g|G ) with given number of decimals after comma \a past_comma and optional removal of trailing zeros behind decimal separator \a remove_trail0. Uses system local, disables not use Group-Separator
* \ingroup jkqtptools_string
*/
JKQTCOMMON_LIB_EXPORT QString jkqtp_floattoqstr(double data, char format='f', int past_comma=-1, bool remove_trail0=false);
/** \brief convert a boolean to a string
* \ingroup jkqtptools_string
*/
@ -279,6 +289,10 @@ JKQTCOMMON_LIB_EXPORT QString jkqtp_floattolatexqstr(double data, int past_comma
* \ingroup jkqtptools_string
*/
JKQTCOMMON_LIB_EXPORT QString jkqtp_floattohtmlqstr(double data, int past_comma=5, bool remove_trail0=false, double belowIsZero=1e-16, double minNoExponent=1e-3, double maxNoExponent=1e4);
/** \brief convert a double to a LaTeX-encoded string, encoding powers of ten as characters, e.g. \c jkqtp_floattounitqstr(1000) will result in "1k"
* \ingroup jkqtptools_string
*/
JKQTCOMMON_LIB_EXPORT QString jkqtp_floattolatexunitqstr(double data, int past_comma=5, bool remove_trail0=false, double belowIsZero=std::numeric_limits<double>::min()*4);
/** \brief convert a character to a string
* \ingroup jkqtptools_string

View File

@ -373,31 +373,6 @@ double JKQTPCoordinateAxis::calcLogTickSpacing() {
}
QString JKQTPCoordinateAxis::floattostringWithFormat(const QLocale & loc, double data, char format, int past_comma, bool remove_trail0) const
{
QString res=loc.toString(data, format, past_comma);
if (remove_trail0 && res.contains(QLocale::system().decimalPoint())) {
QString expo="";
const int expIdx=res.toLower().indexOf('e');
if (expIdx>=0) {
expo=res.mid(expIdx);
res=res.left(expIdx);
}
while (res.size()>0 && res[res.size()-1]=='0') {
res=res.left(res.size()-1);
}
if (res.size()>0 && res[res.size()-1]==loc.decimalPoint()) res=res.left(res.size()-1);
res=res+expo;
}
return res;
}
QString JKQTPCoordinateAxis::floattostringWithFormat(double data, char format, int past_comma, bool remove_trail0) const
{
QLocale loc=QLocale::system();
loc.setNumberOptions(QLocale::OmitGroupSeparator);
return floattostringWithFormat(loc, data, format, past_comma, remove_trail0);
}
QString JKQTPCoordinateAxis::floattolabel(double data) const {
const int past_comma=axisStyle.labelDigits;
@ -429,9 +404,9 @@ QString JKQTPCoordinateAxis::floattolabel(double data, int past_comma) const {
case JKQTPCALTcount:
return "";
case JKQTPCALTdefault:
return addTickUnit(floattostringWithFormat(loc, data, 'f', past_comma, remove_trail0));
return addTickUnit(jkqtp_floattoqstr(loc, data, 'f', past_comma, remove_trail0));
case JKQTPCALTscientific:
return addTickUnit(floattostringWithFormat(loc, data, 'e', past_comma, remove_trail0));
return addTickUnit(jkqtp_floattoqstr(loc, data, 'e', past_comma, remove_trail0));
case JKQTPCALTexponent:
return addTickUnit(QString(jkqtp_floattolatexstr(data, past_comma, remove_trail0, belowIsZero, pow(10.0, -static_cast<double>(past_comma)-0.0001), pow(10.0, static_cast<double>(past_comma)+1)).c_str()));
case JKQTPCALTexponentCharacter:

View File

@ -741,10 +741,6 @@ class JKQTPLOTTER_LIB_EXPORT JKQTPCoordinateAxis: public QObject {
const JKQTMathText* getParentMathText() const;
/** \brief convert a float to a string using \a format (\c f|e|E|g|G ) with given number of decimals after comma \a past_comma and optional removal of trailing zeros behind decimal separator \a remove_trail0. Uses current local, disables not use Group-Separator */
QString floattostringWithFormat(const QLocale & loc, double data, char format, int past_comma, bool remove_trail0=true) const;
/** \brief convert a float to a string using \a format (\c f|e|E|g|G ) with given number of decimals after comma \a past_comma and optional removal of trailing zeros behind decimal separator \a remove_trail0. Uses current local, disables not use Group-Separator */
QString floattostringWithFormat(double data, char format, int past_comma, bool remove_trail0=true) const;
/** \brief convert a float to a tick label string */
QString floattolabel(double data) const;