Skip to content

Commit

Permalink
Try to change handling of TPave draw options
Browse files Browse the repository at this point in the history
Not always draw option stored in the pave - normally
list of primitives keep option and it must be used.

Exception is TPaveStats which painted directly and options used from TPaveStats
  • Loading branch information
linev committed Dec 3, 2024
1 parent b0ce288 commit 4af2588
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 44 deletions.
4 changes: 2 additions & 2 deletions modules/draw/TGraphPolarPainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { settings, gStyle, create, BIT, clTPaveText, kTitle } from '../core.mjs'
import { scaleLinear, select as d3_select, pointer as d3_pointer } from '../d3.mjs';
import { DrawOptions, buildSvgCurve, makeTranslate } from '../base/BasePainter.mjs';
import { ObjectPainter, getElementMainPainter } from '../base/ObjectPainter.mjs';
import { TPavePainter } from '../hist/TPavePainter.mjs';
import { TPavePainter, kPosTitle } from '../hist/TPavePainter.mjs';
import { ensureTCanvas } from '../gpad/TCanvasPainter.mjs';
import { TooltipHandler } from '../gpad/TFramePainter.mjs';
import { assignContextMenu, kNoReorder } from '../gui/menu.mjs';
Expand Down Expand Up @@ -665,7 +665,7 @@ class TGraphPolarPainter extends ObjectPainter {

if (draw_title)
pt.AddText(gr.fTitle);
return TPavePainter.draw(pp, pt, 'postitle')
return TPavePainter.draw(pp, pt, kPosTitle)
.then(p => { p?.setSecondaryId(this, kTitle); return this; });
}

Expand Down
3 changes: 2 additions & 1 deletion modules/gpad/TPadPainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,8 @@ class TPadPainter extends ObjectPainter {
leg.fY1NDC = (1 - szy) * (1 - pad.fTopMargin) + szy * pad.fBottomMargin;
leg.fX2NDC = 0.99 - pad.fRightMargin;
leg.fY2NDC = 0.99 - pad.fTopMargin;
if (opt === undefined) opt = 'autoplace';
if (opt === undefined)
opt = 'autoplace';
} else {
leg.fX1NDC = x1;
leg.fY1NDC = y1;
Expand Down
115 changes: 76 additions & 39 deletions modules/hist/TPavePainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { TAxisPainter } from '../gpad/TAxisPainter.mjs';
import { addDragHandler } from '../gpad/TFramePainter.mjs';
import { ensureTCanvas } from '../gpad/TCanvasPainter.mjs';

const kTakeStyle = BIT(17);

const kTakeStyle = BIT(17), kPosTitle = 'postitle', kAutoPlace = 'autoplace', kDefaultDrawOpt = 'brNDC';

/** @summary Returns true if stat box on default place and can be adjusted
* @private */
Expand All @@ -35,8 +36,8 @@ class TPavePainter extends ObjectPainter {
/** @summary constructor
* @param {object|string} dom - DOM element for drawing or element id
* @param {object} pave - TPave-based object */
constructor(dom, pave) {
super(dom, pave);
constructor(dom, pave, opt) {
super(dom, pave, opt);
this.Enabled = true;
this.UseContextMenu = true;
}
Expand Down Expand Up @@ -126,6 +127,23 @@ class TPavePainter extends ObjectPainter {
});
}

/** @summary Get draw option for the pave
* @desc only stats using fOption directly, all other classes - stored in the pad */
getPaveDrawOption() {
let opt = this.getDrawOpt();
if (this.isStats() || !opt)
opt = this.getObject()?.fOption;
return opt ?? kDefaultDrawOpt;
}

/** @summary Change pave draw option */
setPaveDrawOption(opt) {
if (this.isStats())
this.getObject().fOption = opt;
else
this.storeDrawOpt(opt);
}

/** @summary Draw pave and content
* @return {Promise} */
async drawPave(arg) {
Expand All @@ -134,7 +152,8 @@ class TPavePainter extends ObjectPainter {
return this;
}

const pt = this.getObject(), opt = pt.fOption.toUpperCase(),
const pt = this.getObject(),
opt = this.getPaveDrawOption().toUpperCase(),
fp = this.getFramePainter(), pp = this.getPadPainter(),
pad = pp.getRootPad(true);
let interactive_element, width, height;
Expand All @@ -155,8 +174,9 @@ class TPavePainter extends ObjectPainter {
pt.fY1NDC = 0.1;
pt.fY2NDC = 0.9;
}
} else if (opt.indexOf('NDC') >= 0) {
} else if (pt.fOption.indexOf('NDC') >= 0) {
// check if NDC was modified but fInit was not set
// wired - ROOT checks fOption even when absolutely different draw option may be specified,
// happens in stressGraphics.cxx, sg30 where stats box not initialized when call C->Update() in batch mode
if (pt.fX1NDC < 1e-20 && pt.fX2NDC < 1e-20) {
pt.fX1NDC = pt.fX1;
Expand Down Expand Up @@ -335,7 +355,7 @@ class TPavePainter extends ObjectPainter {

drawBorder(draw_g, width, height, arc_radius, diamond) {
const pt = this.getObject(),
opt = pt.fOption.toUpperCase().replaceAll('ARC', '').replaceAll('NDC', ''),
opt = this.getPaveDrawOption().toUpperCase().replaceAll('ARC', '').replaceAll('NDC', ''),
noborder = this.isPalette() || (opt.indexOf('NB') >= 0),
dx = (opt.indexOf('L') >= 0) ? -1 : ((opt.indexOf('R') >= 0) ? 1 : 0),
dy = (opt.indexOf('T') >= 0) ? -1 : ((opt.indexOf('B') >= 0) ? 1 : 0);
Expand Down Expand Up @@ -1163,7 +1183,8 @@ class TPavePainter extends ObjectPainter {

/** @summary Fill context menu items for the TPave object */
fillContextMenuItems(menu) {
const pave = this.getObject();
const pave = this.getObject(),
set_opt = this.isStats() ? 'SetOption' : 'SetDrawOption';

menu.sub('Shadow');
menu.addSizeMenu('size', 0, 12, 1, pave.fBorderSize, arg => {
Expand All @@ -1175,34 +1196,47 @@ class TPavePainter extends ObjectPainter {
this.interactiveRedraw(true, getColorExec(arg, 'SetShadowColor'));
});
const posarr = ['nb', 'tr', 'tl', 'br', 'bl'];
let value = '', remain = pave.fOption;
let value = '', opt = this.getPaveDrawOption(), remain = opt;
posarr.forEach(nn => {
const p = remain.indexOf(nn);
if ((p >= 0) && !value) {
value = nn; remain = remain.slice(0, p) + remain.slice(p + nn.length);
}
});
menu.addSelectMenu('positon', posarr, value || 'nb', arg => {
pave.fOption = arg + remain;
this.interactiveRedraw(true, `exec:SetOption("${pave.fOption}")`);
arg = arg + remain

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-windows (18.x)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-windows (20.x)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-ubuntu (18.x, g++-11)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-macos (18.x)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-ubuntu (18.x, g++-12)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-ubuntu (18.x, g++-13)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-macos (20.x)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-ubuntu (20.x, g++-11)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-ubuntu (20.x, g++-12)

Missing semicolon

Check warning on line 1207 in modules/hist/TPavePainter.mjs

View workflow job for this annotation

GitHub Actions / build-ubuntu (20.x, g++-13)

Missing semicolon
this.setPaveDrawOption(arg);
this.interactiveRedraw(true, `exec:${set_opt}("${arg}")`);
}, 'Direction of pave shadow or nb - off');
menu.endsub();

menu.sub('Corner');
const parc = pave.fOption.toLowerCase().indexOf('arc');
const parc = opt.toLowerCase().indexOf('arc');
menu.addchk(parc >= 0, 'arc', flag => {
if (flag)
pave.fOption += ' arc';
opt += ' arc';
else
pave.fOption = pave.fOption.slice(0, parc) + pave.fOption.slice(parc + 3);
this.interactiveRedraw(true, `exec:SetOption("${pave.fOption}")`);
opt = opt.slice(0, parc) + opt.slice(parc + 3);
this.setPaveDrawOption(opt);
this.interactiveRedraw(true, `exec:${set_opt}("${opt}")`);
}, 'Usage of ARC draw option');
menu.addSizeMenu('radius', 0, 0.2, 0.02, pave.fCornerRadius, val => {
pave.fCornerRadius = val;
this.interactiveRedraw(true, `exec:SetCornerRadius(${val})`);
}, 'Corner radius when ARC is enabled');
menu.endsub();

if (this.isStats() || this.isPaveText() || this.isPavesText()) {
menu.add('Label', () => menu.input('Enter new label', pave.fLabel).then(lbl => {
pave.fLabel = lbl;
this.interactiveRedraw('pad', `exec:SetLabel("${lbl}")`);
}));
menu.addSizeMenu('Margin', 0, 0.2, 0.02, pave.fMargin, val => {
pave.fMargin = val;
this.interactiveRedraw(true, `exec:SetMargin(${val})`);
});
}

if (this.isStats()) {
menu.add('Default position', () => {
pave.fX2NDC = gStyle.fStatX;
Expand Down Expand Up @@ -1292,15 +1326,6 @@ class TPavePainter extends ObjectPainter {

menu.separator();
} else if (this.isPaveText() || this.isPavesText()) {
menu.addSizeMenu('Margin', 0, 0.2, 0.02, pave.fMargin, val => {
pave.fMargin = val;
this.interactiveRedraw(true, `exec:SetMargin(${val})`);
});
menu.add('Label', () => menu.input('Enter new label', pave.fLabel).then(lbl => {
pave.fLabel = lbl;
this.interactiveRedraw('pad', `exec:SetLabel("${lbl}")`);
}));

if (this.isPavesText()) {
menu.addSizeMenu('Paves', 1, 10, 1, pave.fNpaves, val => {
pave.fNpaves = val;
Expand Down Expand Up @@ -1457,9 +1482,11 @@ class TPavePainter extends ObjectPainter {

/** @summary Update TPave object */
updateObject(obj, opt) {
if (!this.matchObjectType(obj)) return false;
if (!this.matchObjectType(obj))
return false;

const pave = this.getObject();
const pave = this.getObject(),
is_auto = opt === kAutoPlace;

if (!pave.$modifiedNDC && !this.isDummyPos(obj)) {
// if position was not modified interactively, update from source object
Expand Down Expand Up @@ -1497,24 +1524,24 @@ class TPavePainter extends ObjectPainter {
case clTDiamond:
case clTPaveText:
pave.fLines = clone(obj.fLines);
return true;
break;
case clTPavesText:
pave.fLines = clone(obj.fLines);
pave.fNpaves = obj.fNpaves;
return true;
break;
case clTPaveLabel:
case clTPaveClass:
pave.fLabel = obj.fLabel;
return true;
break;
case clTPaveStats:
pave.fOptStat = obj.fOptStat;
pave.fOptFit = obj.fOptFit;
return true;
break;
case clTLegend: {
const oldprim = pave.fPrimitives;
pave.fPrimitives = obj.fPrimitives;
pave.fNColumns = obj.fNColumns;
this.AutoPlace = opt === 'autoplace';
this.AutoPlace = is_auto;
if (oldprim?.arr?.length && (oldprim?.arr?.length === pave.fPrimitives?.arr?.length)) {
// try to sync object reference, new object does not displayed automatically
// in ideal case one should use snapids in the entries
Expand All @@ -1529,10 +1556,14 @@ class TPavePainter extends ObjectPainter {
case clTPaletteAxis:
pave.fBorderSize = 1;
pave.fShadowColor = 0;
return true;
break;
default:
return false;
}

return false;
this.storeDrawOpt(is_auto ? kDefaultDrawOpt : opt);

return true;
}

/** @summary redraw pave object */
Expand Down Expand Up @@ -1593,15 +1624,21 @@ class TPavePainter extends ObjectPainter {

/** @summary Draw TPave */
static async draw(dom, pave, opt) {
const painter = new TPavePainter(dom, pave);
const arg_opt = opt,
pos_title = (opt === kPosTitle),
is_auto = (opt === kAutoPlace);
if (pos_title || is_auto || (isStr(opt) && (opt.indexOf(';') >= 0)))
opt = kDefaultDrawOpt; // default for TPave

const painter = new TPavePainter(dom, pave, opt);

return ensureTCanvas(painter, false).then(() => {
if (painter.isTitle()) {
const prev_painter = painter.getPadPainter().findPainterFor(null, kTitle, clTPaveText);
if (prev_painter && (prev_painter !== painter)) {
prev_painter.removeFromPadPrimitives();
prev_painter.cleanup();
} else if ((opt === 'postitle') || painter.isDummyPos(pave)) {
} else if (pos_title || painter.isDummyPos(pave)) {
if (painter.setTitlePosition(pave))
painter.$postitle = true;
}
Expand All @@ -1621,7 +1658,7 @@ class TPavePainter extends ObjectPainter {
painter.UseContextMenu = true;
}

painter.NoFillStats = (opt === 'nofillstats') || (pave.fName !== 'stats');
painter.NoFillStats = pave.fName !== 'stats';

switch (pave._typename) {
case clTPaveLabel:
Expand All @@ -1637,15 +1674,15 @@ class TPavePainter extends ObjectPainter {
painter.paveDrawFunc = painter.drawPaveText;
break;
case clTLegend:
painter.AutoPlace = (opt === 'autoplace');
painter.AutoPlace = is_auto;
painter.paveDrawFunc = painter.drawLegend;
break;
case clTPaletteAxis:
painter.paveDrawFunc = painter.drawPaletteAxis;
break;
}

return painter.drawPave(opt).then(() => {
return painter.drawPave(arg_opt).then(() => {
const adjust_title = painter.$postitle && painter.$titlebox;

if (adjust_title)
Expand All @@ -1654,12 +1691,12 @@ class TPavePainter extends ObjectPainter {
delete painter.$postitle;
delete painter.$titlebox;

return adjust_title ? painter.drawPave(opt) : painter;
return adjust_title ? painter.drawPave(arg_opt) : painter;
});
});
}

} // class TPavePainter


export { TPavePainter };
export { TPavePainter, kPosTitle };
4 changes: 2 additions & 2 deletions modules/hist2d/THistPainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { gStyle, BIT, settings, constants, create, isObject, isFunc, isStr, getP
import { getColor, getColorPalette } from '../base/colors.mjs';
import { DrawOptions } from '../base/BasePainter.mjs';
import { ObjectPainter, EAxisBits, kAxisTime, kAxisLabels } from '../base/ObjectPainter.mjs';
import { TPavePainter } from '../hist/TPavePainter.mjs';
import { TPavePainter, kPosTitle } from '../hist/TPavePainter.mjs';
import { ensureTCanvas } from '../gpad/TCanvasPainter.mjs';
import { gamma_quantile, gamma_quantile_c } from '../base/math.mjs';

Expand Down Expand Up @@ -1385,7 +1385,7 @@ class THistPainter extends ObjectPainter {
fTextFont: st.fTitleFont, fTextSize: st.fTitleFontSize, fTextColor: st.fTitleTextColor, fTextAlign: 22 });

if (draw_title) pt.AddText(histo.fTitle);
return TPavePainter.draw(pp, pt, 'postitle').then(p => { p?.setSecondaryId(this, kTitle); return this; });
return TPavePainter.draw(pp, pt, kPosTitle).then(p => { p?.setSecondaryId(this, kTitle); return this; });
}

/** @summary Live change and update of title drawing
Expand Down

0 comments on commit 4af2588

Please sign in to comment.