Skip to content

Commit

Permalink
Add HLine label placement/orientation
Browse files Browse the repository at this point in the history
Prior to this commit, the HLine label was
drawn on the left side of the plot under/over
the hline.  This commit allows for placement
of the hline label.  The reason for placement
is that the label could cover up plot curves.
The placement allows one to put the label "left",
"center" or "right" - and allows a placement
with a value between 0 and 1, where 0 is left,
and 1 is right.  A value like 0.75 would put the
label 3/4 way down the hline.
  • Loading branch information
keithvetter committed Sep 4, 2024
1 parent f5d6850 commit a86dfd2
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 2 deletions.
Binary file modified docs/koviz-users-guide.odt
Binary file not shown.
Binary file modified docs/koviz-users-guide.pdf
Binary file not shown.
31 changes: 31 additions & 0 deletions libkoviz/dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,3 +1062,34 @@ void DPHLine::setLabelUnit(const QString &labelUnit)
{
_labelUnit = labelUnit;
}

// 0 is left, 0.5 is center and 1.0 is right
void DPHLine::setLabelPlacement(double placement)
{
if ( placement < 0 ) {
// Clamp placement to 0
_labelPlacement = 0;
} else if ( placement > 1 ) {
// Clamp placement to 1
_labelPlacement = 1;
} else {
_labelPlacement = placement;
}
}

void DPHLine::setLabelOrientation(const QString &orientation)
{
if ( orientation == "left" ) {
_labelPlacement = 0;
} else if ( orientation == "center" ) {
_labelPlacement = 0.5;
} else if ( orientation == "right" ) {
_labelPlacement = 1.0;
} else {
// If unrecognized format, issue error and set orient to left
fprintf(stderr, "koviz [error]: DPHLine::setLabelOrientation: "
"unrecognized orientation=%s. Try left,center,right.\n",
orientation.toLatin1().constData());
_labelPlacement = 0;
}
}
6 changes: 5 additions & 1 deletion libkoviz/dp.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,22 @@ class DPXYPair
class DPHLine
{
public:
DPHLine(double val) : _val(val) {}
DPHLine(double val) : _val(val), _labelPlacement(0) {}

double value() { return _val; }
QString color() { return _color; }
QString label() { return _label; }
QString unit() { return _unit; }
QString labelUnit() { return _labelUnit; }
double labelPlacement() { return _labelPlacement; }

void setValue(double val);
void setColor(const QString& color);
void setLabel(const QString& label);
void setUnit(const QString& unit);
void setLabelUnit(const QString& labelUnit);
void setLabelPlacement(double placement); // left=0 <= placement <= 1=right
void setLabelOrientation(const QString& orientation);

private:
DPHLine() : _val(0) {}
Expand All @@ -134,6 +137,7 @@ class DPHLine
QString _label;
QString _unit;
QString _labelUnit;
double _labelPlacement;
};

class DPCurve
Expand Down
2 changes: 2 additions & 0 deletions libkoviz/dptreewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ void DPTreeWidget::_createDPPages(const QString& dpfile)
_addChild(hlineItem, "HLineColor", dpHLine->color());
_addChild(hlineItem, "HLineUnit", dpHLine->unit());
_addChild(hlineItem, "HLineLabelUnit", dpHLine->labelUnit());
_addChild(hlineItem, "HLineLabelPlacement",
dpHLine->labelPlacement());
}

// Initialize plot math rect
Expand Down
26 changes: 26 additions & 0 deletions libkoviz/layoutitem_curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,32 @@ void CurvesLayoutItem::paintHLines(QPainter *painter,
valStr = valStr.asprintf(valFmt.toLatin1().constData(),origVal);
label.replace(idxVal,idxValEnd-idxVal+1,valStr);
}

double placement = _bookModel->getDataDouble(hlineIdx,
"HLineLabelPlacement");
QFontMetrics fm = painter->fontMetrics();
QRect txtRect = fm.boundingRect(label);
if ( placement == 0 ) {
// drawPt is on left, and already set, so nothing to do
} else if ( placement == 1 ) {
QPointF p(M.right(),0);
p = T.map(p);
drawPt.setX(p.x()-txtRect.width());
} else {
QPointF p(placement*(M.right()-M.left()),0);
p = T.map(p);
drawPt.setX(p.x()-txtRect.width()/2);
if ( drawPt.x() < 0 ) {
// Label would go offscreen on left, clamp to 0
drawPt.setX(0);
} else if ( (drawPt.x()+txtRect.width()) > R.right() ) {
// Label would go offscreen on right, clamp to right
QPointF p(M.right(),0);
p = T.map(p);
drawPt.setX(p.x()-txtRect.width());
}
}

painter->drawText(drawPt,label);
}
painter->setPen(origPen);
Expand Down
2 changes: 2 additions & 0 deletions libkoviz/product_lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ PRESENTATION [Pp][Rr][Ee][Ss][Ee][Nn][Tt][Aa][Tt][Ii][Oo][Nn]
HLINE [Hh][Ll][Ii][Nn][Ee]
COLOR [Cc][Oo][Ll][Oo][Rr]
LABEL_UNITS [Ll][Aa][Bb][Ee][Ll]_[Uu][Nn][Ii][Tt][Ss]
LABEL_ORIENT [Ll][Aa][Bb][Ee][Ll]_[Oo][Rr][Ii][Ee][Nn][Tt]

QSTR \"[^"\n]*["\n]
STR [a-zA-Z][-_a-zA-Z0-9.\,()\[\]/$#{}]*
Expand Down Expand Up @@ -141,6 +142,7 @@ W [ \t]+
{HLINE} { return(DP_HLINE); }
{COLOR} { return(DP_COLOR); }
{LABEL_UNITS} { return(DP_LABEL_UNITS); }
{LABEL_ORIENT} { return(DP_LABEL_ORIENT); }

{QSTR} {

Expand Down
8 changes: 7 additions & 1 deletion libkoviz/product_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ QString dpFileName() {
%token DP_MINOR_X_TICS DP_MINOR_Y_TICS
%token DP_RECT
%token DP_PRESENTATION
%token DP_HLINE DP_COLOR DP_LABEL_UNITS
%token DP_HLINE DP_COLOR DP_LABEL_UNITS DP_LABEL_ORIENT

%token <sval> DP_STR
%token <dval> DP_FLOAT
Expand Down Expand Up @@ -508,6 +508,12 @@ hline: DP_HLINE ':' DP_FLOAT {
| hline DP_LABEL_UNITS ':' DP_STR {
currHLine->setLabelUnit($4);
}
| hline DP_LABEL_ORIENT ':' DP_STR {
currHLine->setLabelOrientation($4);
}
| hline DP_LABEL_ORIENT ':' DP_FLOAT {
currHLine->setLabelPlacement($4);
}
;

float_list: DP_FLOAT {
Expand Down

0 comments on commit a86dfd2

Please sign in to comment.