8 tank widgets gain set_alarm_hi_hi/hi/lo/lo_lo (member bands_); StockSectorComparison gains set_labels_text (pipe-separated labels); InstControlPanel gains set_grid_rows/set_grid_cols. tools/gen_phase4_props.py parses the C headers and emits the per-widget Inspector tables (C++ and Rust) plus the generator capability table from one source of truth.
193 lines
8.5 KiB
C++
193 lines
8.5 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <functional>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
// InstVerticalTankLevel - Upright cylindrical tank, fill rises from the bottom, percent readout, hi/lo alarm markers.
|
|
// Attribution: XFrame.
|
|
class InstVerticalTankLevel {
|
|
private:
|
|
Display* display_;
|
|
int screen_;
|
|
Window window_;
|
|
GC gc_;
|
|
SStyle_Manager* styles_;
|
|
int x1_, y1_, x2_, y2_;
|
|
std::string name_;
|
|
std::string text_;
|
|
std::string style_name_;
|
|
bool visible_ = true;
|
|
bool enabled_ = true;
|
|
bool focused_ = false;
|
|
bool pressed_ = false;
|
|
bool hovered_ = false;
|
|
double level_;
|
|
xframe::InstAlarmBands bands_;
|
|
std::function<void()> on_click_;
|
|
|
|
// Getter/setter pass (Group D): draw colours/state exposed as properties.
|
|
// Defaults preserve the original rendering.
|
|
Srgb white_color_ = {255, 255, 255};
|
|
Srgb ink_color_ = {60, 60, 60};
|
|
Srgb pale_color_ = {235, 242, 248};
|
|
Srgb fill_color_ = {40, 120, 200};
|
|
Srgb grid_color_ = {210, 210, 210};
|
|
Srgb amber_color_ = {220, 150, 0};
|
|
Srgb red_color_ = {200, 30, 30};
|
|
Srgb hover_color_ = {255, 170, 0};
|
|
public:
|
|
InstVerticalTankLevel(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles,
|
|
int x, int y, int width, int height, double level = xframe::inst_demo_level(42)) {
|
|
display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles;
|
|
x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height;
|
|
name_ = "InstVerticalTankLevel"; text_ = "InstVerticalTankLevel";
|
|
level_ = xframe::inst_clamp_level(level);
|
|
}
|
|
~InstVerticalTankLevel() {}
|
|
void set_bounds(int x, int y, int width, int height) { x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; }
|
|
int width() { return x2_ - x1_; }
|
|
int height() { return y2_ - y1_; }
|
|
bool contains(int x, int y) { return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; }
|
|
void set_visible(bool v) { visible_ = v; }
|
|
bool get_visible() { return visible_; }
|
|
void set_enabled(bool e) { enabled_ = e; }
|
|
bool get_enabled() { return enabled_; }
|
|
void set_style_name(std::string s) { style_name_ = s; }
|
|
void set_text(std::string t) { text_ = t; }
|
|
std::string get_text() { return text_; }
|
|
SStyle style() { SStyle s = styles_->resolve(style_name_); XFontStruct* bf = xframe::inst_bold_font(display_); if (bf) s.font_ = bf; return s; }
|
|
void set_level(double pct) { level_ = xframe::inst_clamp_level(pct); }
|
|
double get_level() { return level_; }
|
|
void set_alarm_bands(const xframe::InstAlarmBands& b) { bands_ = b; }
|
|
void set_alarm_hi_hi(double v) { bands_.hi_hi = v; }
|
|
void set_alarm_hi(double v) { bands_.hi = v; }
|
|
void set_alarm_lo(double v) { bands_.lo = v; }
|
|
void set_alarm_lo_lo(double v) { bands_.lo_lo = v; }
|
|
const xframe::InstAlarmBands& alarm_bands() { return bands_; }
|
|
void set_on_click(std::function<void()> f) { on_click_ = f; }
|
|
static std::string fmt(double v) {
|
|
std::ostringstream oss; oss.precision(2); oss << std::fixed << v;
|
|
std::string s = oss.str();
|
|
while (!s.empty() && s.back() == '0') s.pop_back();
|
|
if (!s.empty() && s.back() == '.') s.pop_back();
|
|
return s;
|
|
}
|
|
double level_at_point(int x, int y) {
|
|
(void)x;
|
|
int ty = y1_ + 30;
|
|
int th = height() - 90;
|
|
double f = 1.0 - (double)(y - ty) / (double)th;
|
|
return xframe::inst_clamp_level(f * 100.0);
|
|
}
|
|
// Exposed draw colours/state (Group D getter/setter pass).
|
|
Srgb get_white_color() const { return white_color_; }
|
|
void set_white_color(Srgb c) { white_color_ = c; }
|
|
Srgb get_ink_color() const { return ink_color_; }
|
|
void set_ink_color(Srgb c) { ink_color_ = c; }
|
|
Srgb get_pale_color() const { return pale_color_; }
|
|
void set_pale_color(Srgb c) { pale_color_ = c; }
|
|
Srgb get_fill_color() const { return fill_color_; }
|
|
void set_fill_color(Srgb c) { fill_color_ = c; }
|
|
Srgb get_grid_color() const { return grid_color_; }
|
|
void set_grid_color(Srgb c) { grid_color_ = c; }
|
|
Srgb get_amber_color() const { return amber_color_; }
|
|
void set_amber_color(Srgb c) { amber_color_ = c; }
|
|
Srgb get_red_color() const { return red_color_; }
|
|
void set_red_color(Srgb c) { red_color_ = c; }
|
|
Srgb get_hover_color() const { return hover_color_; }
|
|
void set_hover_color(Srgb c) { hover_color_ = c; }
|
|
void draw() {
|
|
if (!visible_) return;
|
|
SStyle st = style();
|
|
unsigned long white = styles_->get_color_rgb(white_color_.r, white_color_.g, white_color_.b);
|
|
unsigned long ink = styles_->get_color_rgb(ink_color_.r, ink_color_.g, ink_color_.b);
|
|
unsigned long pale = styles_->get_color_rgb(pale_color_.r, pale_color_.g, pale_color_.b);
|
|
unsigned long fillc = styles_->get_color_rgb(fill_color_.r, fill_color_.g, fill_color_.b);
|
|
unsigned long grid = styles_->get_color_rgb(grid_color_.r, grid_color_.g, grid_color_.b);
|
|
unsigned long amber = styles_->get_color_rgb(amber_color_.r, amber_color_.g, amber_color_.b);
|
|
unsigned long red = styles_->get_color_rgb(red_color_.r, red_color_.g, red_color_.b);
|
|
XSetForeground(display_, gc_, white);
|
|
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
|
|
int tx = x1_ + 60;
|
|
int ty = y1_ + 30;
|
|
int tw = 220;
|
|
int th = height() - 90;
|
|
XSetForeground(display_, gc_, pale);
|
|
XFillRectangle(display_, window_, gc_, tx, ty, tw, th);
|
|
int fh = (int)(th * level_ / 100.0);
|
|
if (fh > 0) {
|
|
XSetForeground(display_, gc_, fillc);
|
|
XFillRectangle(display_, window_, gc_, tx, ty + th - fh, tw, fh);
|
|
}
|
|
XSetForeground(display_, gc_, ink);
|
|
XDrawRectangle(display_, window_, gc_, tx, ty, tw, th);
|
|
XDrawArc(display_, window_, gc_, tx, ty - 12, tw, 24, 0, 180 * 64);
|
|
for (int v = 0; v <= 100; v += 25) {
|
|
int gy = ty + th - (int)(th * v / 100.0);
|
|
XSetForeground(display_, gc_, grid);
|
|
XDrawLine(display_, window_, gc_, tx + tw + 4, gy, tx + tw + 14, gy);
|
|
xf::draw_text_at(display_, window_, gc_, st, tx + tw + 18, gy + 4, std::to_string(v));
|
|
}
|
|
auto mark = [&](double pct, unsigned long c, const char* lab) {
|
|
int gy = ty + th - (int)(th * pct / 100.0);
|
|
XSetForeground(display_, gc_, c);
|
|
XDrawLine(display_, window_, gc_, tx, gy, tx + tw, gy);
|
|
xf::draw_text_at(display_, window_, gc_, st, tx + tw + 52, gy + 4, lab);
|
|
};
|
|
mark(bands_.hi_hi, red, "HI-HI");
|
|
mark(bands_.hi, amber, "HI");
|
|
mark(bands_.lo, amber, "LO");
|
|
mark(bands_.lo_lo, red, "LO-LO");
|
|
std::string pct = fmt(level_) + "%";
|
|
int pw2 = xf::text_width(st, pct);
|
|
xf::draw_text_at(display_, window_, gc_, st, tx + (tw - pw2) / 2, ty + th + 18, pct);
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 16, text_);
|
|
XSetForeground(display_, gc_, hovered_ ? styles_->get_color_rgb(hover_color_.r, hover_color_.g, hover_color_.b) : st.lineColor_);
|
|
XDrawRectangle(display_, window_, gc_, x1_, y1_, width(), height());
|
|
XFlush(display_);
|
|
}
|
|
bool on_button_press(int x, int y, unsigned int button) {
|
|
if (!contains(x, y) || !enabled_) return false;
|
|
focused_ = true;
|
|
if (button == 4) { set_level(level_ + 5.0); return true; }
|
|
if (button == 5) { set_level(level_ - 5.0); return true; }
|
|
if (button == 1) {
|
|
pressed_ = true;
|
|
set_level(level_at_point(x, y));
|
|
if (on_click_) on_click_();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool on_button_release(int x, int y, unsigned int button) {
|
|
(void)x; (void)y;
|
|
if (button == 1 && pressed_) { pressed_ = false; return true; }
|
|
return false;
|
|
}
|
|
bool on_motion(int x, int y) {
|
|
bool h = contains(x, y);
|
|
if (h == hovered_) return false;
|
|
hovered_ = h;
|
|
return true;
|
|
}
|
|
bool on_key(KeySym key, const char* str) {
|
|
(void)str;
|
|
(void)key;
|
|
if (!focused_ || !enabled_) return false;
|
|
switch (key) {
|
|
case XK_Up: case XK_Right: set_level(level_ + 5.0); return true;
|
|
case XK_Down: case XK_Left: set_level(level_ - 5.0); return true;
|
|
case XK_Home: set_level(0.0); return true;
|
|
case XK_End: set_level(100.0); return true;
|
|
case XK_r: case XK_R: set_level(xframe::inst_demo_level(42)); return true;
|
|
}
|
|
return false;
|
|
}
|
|
};
|