Skip to content

Commit

Permalink
Add printf style formatting to numbers in labels.
Browse files Browse the repository at this point in the history
The syntax is, for example [INDEX:First (X1)]{%.3E}
  • Loading branch information
netterfield committed Dec 12, 2016
1 parent 1de1542 commit cd764ef
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
16 changes: 12 additions & 4 deletions pyKst/pykst.py
Original file line number Diff line number Diff line change
Expand Up @@ -2514,12 +2514,20 @@ class Label(ViewItem) :
Scalars and scalar equations can be displayed live in labels.
When the scalar is updated, the label is updated.
The format is::
Scalar: [scalarname] eg [GYRO1:Mean(X4)]
Vector Element: [vectorName[index]] eg [GYRO1 (V2)[4]]
Equation: [=equation] eg [=[GYRO1:Mean(X4)]/[GYRO1:Sigma (X4)]]
Scalar: [scalarname] eg [GYRO1:Mean(X4)]
Vector Element: [vectorName[index]] eg [GYRO1 (V2)[4]]
Equation: [=equation] eg [=[GYRO1:Mean(X4)]/[GYRO1:Sigma (X4)]]
These numerical fields can be formatted by appending a C printf format embedded
in { } immediately after the field. For example::
[GYRO1:Mean(X4)]{%4.2f}
Labels in kst support a derrivitive subset of LaTeX. For example, to display
the equation for the area of a circle, you could set the label to ``A=2\pir^2``.
Unlike LaTeX, it is not necessary to enter math mode using ``$``. Also,
Expand Down
32 changes: 29 additions & 3 deletions src/libkstapp/labelrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "applicationsettings.h"

#include <QDebug>
#include <cstdio>

const double subscript_scale = 0.60;
const double subscript_drop = 0.16;
Expand Down Expand Up @@ -111,13 +112,29 @@ void renderLabel(RenderContext& rc, Label::Chunk *fi, bool cache, bool draw) {
bool ok = false;
const QString s = fi->text.mid(1);
const double eqResult(Equations::interpret(store, s.toLatin1(), &ok, s.length()));
txt = QString::number(eqResult, 'g', rc.precision);
if (fi->formated) {
const int strlen = 100;
char cstring[strlen];
QByteArray format = fi->format.toLatin1();
snprintf(cstring, strlen, format.data(), eqResult); // not using QString::asprintf() because it isn't in qt4.
txt = QString(cstring);
} else {
txt = QString::number(eqResult, 'g', rc.precision);
}
} else {
Kst::ObjectPtr op = store->retrieveObject(fi->text);
Kst::ScalarPtr scp = Kst::kst_cast<Kst::Scalar>(op);
if (scp) {
KstReadLocker l(scp);
txt = QString::number(scp->value(), 'g', rc.precision);
if (fi->formated) {
const int strlen = 100;
char cstring[strlen];
QByteArray format = fi->format.toLatin1();
snprintf(cstring, strlen, format.data(), scp->value()); // not using QString::asprintf() because it isn't in qt4.
txt = QString(cstring);
} else {
txt = QString::number(scp->value(), 'g', rc.precision);
}
if (cache) {
rc.addObject(scp);
}
Expand Down Expand Up @@ -154,7 +171,16 @@ void renderLabel(RenderContext& rc, Label::Chunk *fi, bool cache, bool draw) {
if (ok) {
KstReadLocker l(vp);
const double vVal(vp->value()[int(idx)]);
txt = QString::number(vVal, 'g', rc.precision);
if (fi->formated) {
const int strlen = 100;
char cstring[strlen];
QByteArray format = fi->format.toLatin1();
snprintf(cstring, strlen, format.data(), vVal); // not using QString::asprintf() because it isn't in qt4.
txt = QString(cstring);
} else {
txt = QString::number(vVal, 'g', rc.precision);
}

if (cache) {
rc.addObject(vp);
}
Expand Down
29 changes: 27 additions & 2 deletions src/libkstmath/labelparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using namespace Label;
#endif

Chunk::Chunk(Chunk *parent, VOffset dir, bool isGroup, bool inherit)
: next(0L), prev(0L), up(0L), down(0L), group(0L), scalar(false), linebreak(false), tab(false), vector(false), vOffset(dir) {
: next(0L), prev(0L), up(0L), down(0L), group(0L), scalar(false), linebreak(false), tab(false), vector(false), formated(false), vOffset(dir) {
assert(parent || vOffset == None);
if (parent) { // attach and inherit
switch (vOffset) {
Expand Down Expand Up @@ -622,6 +622,10 @@ static Chunk *parseInternal(Chunk *ctail, const QString& txt, uint& start, uint
int vectorIndexEnd = -1;
int bracketStack = 1;
int pos = -1;
bool format = false;
int formatIndexStart = 0;
int formatIndexEnd = 0;

bool equation = txt[i + 1] == '=';
for (uint searchPt = i + 1; bracketStack != 0 && searchPt < cnt; ++searchPt) {
if (txt[searchPt] == ']') {
Expand All @@ -644,6 +648,19 @@ static Chunk *parseInternal(Chunk *ctail, const QString& txt, uint& start, uint
return 0L;
}

if (pos+3 < cnt) { // ]{%f} is min size.
if ((txt[pos+1]=='{') && (txt[pos+2] == '%')) {
formatIndexStart = pos+1;
for (uint searchPt = pos + 2; searchPt < cnt; ++searchPt) {
if (txt[searchPt] == '}') {
formatIndexEnd = searchPt;
format = true;
break;
}
}
}
}

if (ctail->locked() || !ctail->text.isEmpty()) {
if (ctail->vOffset != Chunk::None) {
ctail = new Chunk(ctail->prev, Chunk::None, false, true);
Expand All @@ -660,7 +677,15 @@ static Chunk *parseInternal(Chunk *ctail, const QString& txt, uint& start, uint
ctail->text = txt.mid(i + 1, pos - i - 1).trimmed();
ctail->scalar = true;
}
i = uint(pos);
if (format) {
i = uint(formatIndexEnd);
ctail->formated = true;
ctail->format = txt.mid(formatIndexStart+1, formatIndexEnd - formatIndexStart-1);

qDebug() << ctail->format;
} else {
i = uint(pos);
}
}
break;
default:
Expand Down
2 changes: 2 additions & 0 deletions src/libkstmath/labelparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace Label {
bool linebreak : 1;
bool tab : 1;
bool vector : 1;
bool formated : 1;
VOffset vOffset
#ifndef Q_OS_WIN32
: 2
Expand All @@ -71,6 +72,7 @@ namespace Label {
ChunkAttributes attributes;
QString text;
QString expression;
QString format;
};


Expand Down
3 changes: 3 additions & 0 deletions src/widgets/labelbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ void LabelBuilder::showHelp() {
"e.g. <FONT FACE=\"Courier New, monospace\">[=[GYRO1:Mean"
"(X4)]/[GYRO1:Sigma (X4)]]</FONT></P>"

"<P STYLE=\"margin-bottom: 0in\"><B>Formatting:</B> Numbers can be formatted using C printf formats. e.g., "
"<FONT FACE=\"Courier New, monospace\">[GYRO1:Mean (X4)]{%4.2f}</FONT>"

"<P ALIGN=LEFT STYLE=\"margin-bottom: 0in\"><FONT SIZE=4><B>Supported LaTeX Subset</B></FONT><br>"
"Labels in <i>kst</i> "
"support a derivative subset of LaTeX. For example, to display the equation for the area of a "
Expand Down

0 comments on commit cd764ef

Please sign in to comment.