Import FDesigner C++/GTK3 designer with StockChart Pro
GUI designer (C++/GTK3, Glade .ui, code generator) plus indicator/drawing libraries, chart tests and test matrix.
This commit is contained in:
1 parent
f4126b4298
commit
9c94d93ae5
31 files changed
+18194
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
fdesigner
|
||||||
|
*.o
|
||||||
|
tests/gen_matrix
|
||||||
@@ -0,0 +1,228 @@
|
|||||||
|
# FDesigner — C++/GTK conversion design
|
||||||
|
|
||||||
|
## What we're building
|
||||||
|
|
||||||
|
A C++ rebuild of Chris's fdesigner (Tkinter Rapid Designer). The original is a
|
||||||
|
~7,000-line Python/Tkinter app (`original/tkinter_designer/fdesigner7.py`):
|
||||||
|
place widgets on a canvas at absolute positions, edit their properties, then
|
||||||
|
generate runnable source code for the layout.
|
||||||
|
|
||||||
|
Per Chris's decision (2026-11-25) the C++ version **generates C++/GTK code**,
|
||||||
|
not Tkinter Python. The designer's own UI is also GTK, defined in Glade .ui
|
||||||
|
files. No Qt anywhere.
|
||||||
|
|
||||||
|
## Stack
|
||||||
|
|
||||||
|
- **GTK 3.24** — Glade's target toolkit. (GTK 4 has no Glade; its successor is
|
||||||
|
Cambalache. Glade means GTK 3.)
|
||||||
|
- **C++ with the GTK C API**, windows loaded via **GtkBuilder** from .ui files
|
||||||
|
authored in Glade. No gtkmm: GtkBuilder is the native path, and this keeps
|
||||||
|
the generated code and the designer itself on the same API.
|
||||||
|
- Runs on **X11** (GTK 3's X11 backend), same as the original Tk app.
|
||||||
|
- Build: g++ + pkg-config (`gtk+-3.0`), plain Makefile, like Chris's other
|
||||||
|
tools.
|
||||||
|
- Toolchain verified on Muse's VM 2026-11-25: GTK 3.24.41 headers + Glade
|
||||||
|
installed; a C++ test program including <gtk/gtk.h> compiles, links, and
|
||||||
|
runs under Xvfb. (VM quirk: apt's libzstd-dev version pin conflicts with the
|
||||||
|
VM's patched libzstd1, so the -dev packages were installed from downloaded
|
||||||
|
.debs with `dpkg --force-depends` — headers only, no runtime change. On
|
||||||
|
Chris's Parrot box a plain `apt install libgtk-3-dev glade` should work.)
|
||||||
|
|
||||||
|
## Generated output
|
||||||
|
|
||||||
|
For each designed window the generator emits two files, mirroring the spirit
|
||||||
|
of the Python tool's generated class + stub handlers:
|
||||||
|
|
||||||
|
1. **`<name>.ui`** — a Glade/GtkBuilder file holding the layout: a GtkWindow
|
||||||
|
containing a GtkFixed, with each widget placed at its designed x/y and
|
||||||
|
sized with width/height requests (the GTK equivalent of Tk's `.place()`).
|
||||||
|
2. **`<name>.h` / `<name>.cpp`** — a C++ class that loads the .ui with
|
||||||
|
GtkBuilder, fetches each widget by id, and declares one stub signal
|
||||||
|
handler per connected signal (`// Code block here`, like the original's
|
||||||
|
stubs), ready for the user to fill in.
|
||||||
|
|
||||||
|
The generated project compiles standalone with:
|
||||||
|
`g++ -o app *.cpp $(pkg-config --cflags --libs gtk+-3.0)`
|
||||||
|
|
||||||
|
## Widget mapping (Tk -> GTK)
|
||||||
|
|
||||||
|
| Tk (original palette) | GTK 3 |
|
||||||
|
|---|---|
|
||||||
|
| Root / Toplevel | GtkWindow |
|
||||||
|
| Frame / Labelframe | GtkFrame |
|
||||||
|
| Button | GtkButton |
|
||||||
|
| Label | GtkLabel |
|
||||||
|
| Entry | GtkEntry |
|
||||||
|
| Checkbutton | GtkCheckButton |
|
||||||
|
| Radiobutton | GtkRadioButton |
|
||||||
|
| Combobox (ttk) | GtkComboBoxText |
|
||||||
|
| Listbox | GtkListBox (inside GtkScrolledWindow) |
|
||||||
|
| Menu | GtkMenu / GtkMenuBar |
|
||||||
|
| Menubutton | GtkMenuButton |
|
||||||
|
| Canvas | GtkDrawingArea |
|
||||||
|
| Scale | GtkScale |
|
||||||
|
| Scrollbar | GtkScrollbar |
|
||||||
|
| Text | GtkTextView (inside GtkScrolledWindow) |
|
||||||
|
| Spreadsheet (custom) | GtkGrid of GtkEntry (later phase) |
|
||||||
|
|
||||||
|
## Internal model
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
struct Widget {
|
||||||
|
std::string name; // e.g. "Button_001"
|
||||||
|
std::string type; // palette type key, e.g. "Button"
|
||||||
|
int x, y, width, height; // canvas geometry
|
||||||
|
std::map<std::string,std::string> props; // GTK properties, as strings
|
||||||
|
std::vector<std::string> signals; // signals to stub out in generated code
|
||||||
|
};
|
||||||
|
struct Project {
|
||||||
|
std::string title, class_name;
|
||||||
|
int win_width, win_height;
|
||||||
|
std::string background; // window background colour
|
||||||
|
std::vector<Widget> widgets; // Root is implicit (the window itself)
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## Project file format
|
||||||
|
|
||||||
|
**JSON** (`.fdj`), replacing Python pickle, which C++ cannot read. If Chris
|
||||||
|
has old .pkl designs he wants to keep, a one-off Python converter script
|
||||||
|
(pkl -> fdj) can be written later; pickle stays on the Python side only.
|
||||||
|
|
||||||
|
## What does not port directly
|
||||||
|
|
||||||
|
- **pickle** save/load -> JSON (above).
|
||||||
|
- **pyperclip** -> GtkClipboard (built into GTK).
|
||||||
|
- **openpyxl** spreadsheet modeller -> later phase; the on-canvas spreadsheet
|
||||||
|
widget is a GtkGrid; xlsx import, if wanted, needs a separate library.
|
||||||
|
- The huge generated colour files in `library/` (super_color.py ~31k lines,
|
||||||
|
colormenu.py ~15k lines) are data, not logic: they become colour-name data
|
||||||
|
tables / GTK colour chooser integration, not hand-ported code.
|
||||||
|
|
||||||
|
## Phases
|
||||||
|
|
||||||
|
1. **Skeleton + canvas**: main window from Glade .ui; palette adds widgets to
|
||||||
|
a GtkFixed canvas; click to select, drag to move, delete. *(skeleton done)*
|
||||||
|
2. **Property editor**: per-type property tables (ported from the original's
|
||||||
|
Entry_001..019 mapping), plus the original's bulk operations — set
|
||||||
|
parameter on all / selected / filtered widgets, clear parameter.
|
||||||
|
3. **Code generator**: emit .ui + .h/.cpp per the format above; generated
|
||||||
|
project compiles and runs.
|
||||||
|
4. **Save/load**: JSON project files; optional .pkl -> .fdj converter.
|
||||||
|
5. **Extras**: colour palette, menu designer, spreadsheet widget, canvas
|
||||||
|
widget (drawing area) support.
|
||||||
|
|
||||||
|
**Status 2026-11-25: all five phases built and verified.** The self-test
|
||||||
|
(`FDESIGNER_SELFTEST=1 ./fdesigner`) exercises placement, properties, bulk
|
||||||
|
ops, generation, save/load round-trips, and the Phase 5 extras; generated
|
||||||
|
projects compile with their own Makefile and run. Known v1 limits:
|
||||||
|
designing *inside* a Toplevel or a generated menu's submenus is not
|
||||||
|
supported (Toplevel content starts as an empty GtkFixed; menus are a
|
||||||
|
one-level text spec), and there is no .pkl converter unless old designs
|
||||||
|
need importing.
|
||||||
|
|
||||||
|
**Generator revision 2026-11-25 (Chris's direction):** generated projects
|
||||||
|
are ALL CODE (no .ui — so a stepping debugger can walk construction) and
|
||||||
|
every widget is a STANDALONE CLASS in its own .h/.cpp pair, mirroring the
|
||||||
|
Python generator's boilerplate: per-widget `// #region`, creation +
|
||||||
|
`.place()`-equivalent + config in the constructor, a `util_error_print`
|
||||||
|
helper, and the same nine mouse handlers per widget (buttonPress1-3,
|
||||||
|
dblbuttonPress1-3, buttonRelease1-3) routed by two dispatchers; the
|
||||||
|
window class keeps the Root_001 handler set and just instantiates the
|
||||||
|
widget classes. Verified by `tests/run_matrix.sh`: 13 generated designs
|
||||||
|
(every type, every property, every enum value, menus, hostile strings,
|
||||||
|
class-name edge cases, 40 widgets, spreadsheet extremes) each compile
|
||||||
|
with -Wall -Wextra, zero warnings, and run clean. The matrix caught a
|
||||||
|
real bug: property ranges (e.g. Label xalign 0..1) were not enforced, so
|
||||||
|
an out-of-range value could reach GTK — canonicalize() now enforces the
|
||||||
|
property tables' lo/hi for Int and Double.
|
||||||
|
|
||||||
|
**StockChart widget added 2026-11-25:** a candle chart for Massive
|
||||||
|
aggregates CSVs (header `ticker,volume,open,close,high,low,window_start,
|
||||||
|
transactions`, matched by column name). Properties: `data-file`,
|
||||||
|
`up-color`, `down-color`, `max-candles`. The designer plots a built-in
|
||||||
|
preview series on the canvas and swaps in real data when data-file is
|
||||||
|
set; the generated StockChart class is self-contained — it embeds the
|
||||||
|
CSV loader (`load_csv`, plus `set_candles`/`candle_count`) and cairo
|
||||||
|
plotting (wicks + bodies, min/max scaled). Fake data for trying it:
|
||||||
|
`tests/data/SPY_fake.csv` (120 synthetic SPY minute bars). The matrix
|
||||||
|
grew to 14 cases plus a render test that draws the generated chart
|
||||||
|
offscreen and counts up/down-coloured pixels, proving it plots.
|
||||||
|
|
||||||
|
**Indicator submodules added 2026-11-25:** `src/indicators.h` is ONE
|
||||||
|
self-contained header holding **250 named technical indicators** —
|
||||||
|
moving averages and Ehlers/Hilbert filters, bands/channels, five pivot
|
||||||
|
families, momentum oscillators, volume indicators, volatility and
|
||||||
|
statistics, and all 61 TA-Lib candlestick patterns plus 5 extras. Each
|
||||||
|
is a registry module (namespace `fdi`) that consumes the chart's OHLCV
|
||||||
|
bars and draws itself: price-pane overlays share the price scale,
|
||||||
|
oscillators get their own labelled pane strip, patterns draw markers.
|
||||||
|
They have no palette button; a StockChart enables them via its
|
||||||
|
`indicators` property (`"SMA(20), RSI, MACD(12,26,9), Engulfing"`) in
|
||||||
|
the designer, or `add_indicator("SMA(20)")` in generated code. The
|
||||||
|
generator embeds the header text (via `src/indicators_embedded.h`,
|
||||||
|
regenerated by `tools/embed_indicators.sh` from the Makefile rule) and
|
||||||
|
copies it into any generated project that contains a StockChart.
|
||||||
|
Candle structs carry `volume` (OBV/VWAP/MFI need it). Testing:
|
||||||
|
`tests/indicator_test.cpp` computes and renders every indicator over
|
||||||
|
`tests/data/SPY_fake_long.csv` (1,170 bars, 3 days), counts palette-ink
|
||||||
|
pixels per render, hand-checks 18 patterns on crafted candle sequences
|
||||||
|
and bounded oscillators against their legal ranges, and writes a
|
||||||
|
labelled contact sheet of all 250 plots to
|
||||||
|
`tests/out/indicators_contact_sheet.png`. Result: 244 render-verified
|
||||||
|
on data; 6 ultra-rare gap patterns verified by crafted sequences.
|
||||||
|
Matrix case 15 covers a generated chart with indicators attached.
|
||||||
|
|
||||||
|
**StockChart Pro added 2026-11-25:** the chart gained a full
|
||||||
|
annotation system, comparative plotting, a percent scale, a time axis
|
||||||
|
and wheel zoom, in both the designer preview and generated code.
|
||||||
|
|
||||||
|
- `src/drawings.h` is ONE self-contained header (namespace `fdw`)
|
||||||
|
holding the drawing module: 11 kinds — trend, ray, horizontal and
|
||||||
|
vertical lines, rectangle, ellipse, arrow, text labels, Fibonacci
|
||||||
|
retracement, freehand, and a measure tool (Δ price / % / bars).
|
||||||
|
Anchors are DATA coordinates (bar index, price), never pixels, so
|
||||||
|
annotations survive widget resizes, zooms and pans; every render
|
||||||
|
re-maps through `fdw::Map`. Drawings come from text specs
|
||||||
|
(`"trend 10 655 200 672.5 red; hline 650 blue dash"`) via
|
||||||
|
`fdw::parse_specs`, or interactively in generated apps: keys
|
||||||
|
T H V R L A X F D M E S select trend/hline/vline/rect/ellipse/arrow/
|
||||||
|
text/fib/freehand/measure/erase/select-move, Esc returns to pan,
|
||||||
|
Delete removes the selected drawing, and a text tool click asks for
|
||||||
|
the label in a dialog. The generator embeds the header
|
||||||
|
(`src/drawings_embedded.h` via `tools/embed_drawings.sh`) and copies
|
||||||
|
`drawings.h` into StockChart projects, like indicators.h.
|
||||||
|
- New StockChart properties: `scale-mode` (Price|Percent — percent
|
||||||
|
relabels the right axis as change from the first visible close),
|
||||||
|
`compare-files` (comma-separated CSVs of other tickers, aligned by
|
||||||
|
timestamp and rebased onto the first visible close, drawn as
|
||||||
|
coloured lines with a legend), and `drawings` (semicolon-separated
|
||||||
|
specs). The bottom axis plots UTC time from `window_start` (date
|
||||||
|
shown at day changes); gridlines use nice 1/2/5 steps.
|
||||||
|
- View: the mouse wheel zooms around the cursor (`zoom_at(factor,
|
||||||
|
frac)` in code), drag pans with no tool active, `reset_view()`
|
||||||
|
restores the max-candles window. The designer preview zooms with
|
||||||
|
the wheel too.
|
||||||
|
- Testing: `tests/drawings_test.cpp` renders every kind and checks
|
||||||
|
palette ink, verifies a trend line's endpoints remap at 1x and 2x
|
||||||
|
plot sizes, and exercises hit testing. Matrix case 16
|
||||||
|
(`16_stockchart_pro`, a 1024x768 window) designs a percent-mode
|
||||||
|
chart with a QQQ comparison (`tests/data/QQQ_fake_long.csv`, same
|
||||||
|
timestamp grid as SPY, made by `tests/make_compare_data.py`), SMA +
|
||||||
|
RSI and 7 annotations; `tests/chart_pro_render_test.cpp` asserts
|
||||||
|
candle/comparison/arrow/fib ink, percent and time axis labels,
|
||||||
|
zoom in/out/clamp/reset, and that arrow + fib still draw after the
|
||||||
|
widget is resized smaller. `tests/chart_screenshot.cpp` captures
|
||||||
|
the generated app at exactly 1024x768 to
|
||||||
|
`tests/out/chart_pro_1024x768.png`. Full regression after the
|
||||||
|
change: designer self-test PASS, matrix 21/21.
|
||||||
|
- Gotcha found by the matrix: the generated chart's event handlers
|
||||||
|
must not be named `on_press`/`on_release` — those are the widget
|
||||||
|
stub dispatchers. They are `on_chart_press` etc.
|
||||||
|
|
||||||
|
## Layout of this workspace
|
||||||
|
|
||||||
|
- `original/` — the Python source, read-only reference (do not run it here).
|
||||||
|
- `ui/designer.ui` — the designer's own main window (Glade).
|
||||||
|
- `src/main.cpp` — entry point; loads the .ui via GtkBuilder.
|
||||||
|
- `Makefile` — builds `./fdesigner`.
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
CXX ?= g++
|
||||||
|
CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17
|
||||||
|
GTKFLAGS := $(shell pkg-config --cflags gtk+-3.0)
|
||||||
|
GTKLIBS := $(shell pkg-config --libs gtk+-3.0)
|
||||||
|
|
||||||
|
fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/model.h src/props.h src/generator.h src/project_io.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/project_io.cpp $(GTKFLAGS) $(GTKLIBS)
|
||||||
|
|
||||||
|
src/indicators_embedded.h: src/indicators.h tools/embed_indicators.sh
|
||||||
|
sh tools/embed_indicators.sh src/indicators.h $@
|
||||||
|
|
||||||
|
src/drawings_embedded.h: src/drawings.h tools/embed_drawings.sh
|
||||||
|
sh tools/embed_drawings.sh src/drawings.h $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f fdesigner
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 145 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.3 MiB |
+597
@@ -0,0 +1,597 @@
|
|||||||
|
// drawings.h — chart annotation module for the StockChart widget.
|
||||||
|
//
|
||||||
|
// One self-contained header (namespace fdw), in the same spirit as
|
||||||
|
// indicators.h. A Drawing is stored in DATA coordinates — an anchor is
|
||||||
|
// (bar index, price) — never in pixels, so annotations stay glued to
|
||||||
|
// the candles they were drawn on through widget resizes, zooms and
|
||||||
|
// pans: every render re-maps data -> pixels through the current Map.
|
||||||
|
//
|
||||||
|
// Kinds: trend line, ray, horizontal line, vertical line, rectangle,
|
||||||
|
// ellipse, arrow, text label, Fibonacci retracement, freehand stroke,
|
||||||
|
// and a measure tool (delta price / percent / bars between two points).
|
||||||
|
//
|
||||||
|
// Drawings can be created three ways: interactively (the generated
|
||||||
|
// StockChart's mouse tools build them), from a text spec via
|
||||||
|
// parse_spec()/parse_specs(), or directly in code. Spec grammar
|
||||||
|
// (semicolon-separated in parse_specs):
|
||||||
|
//
|
||||||
|
// trend x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// ray x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// arrow x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// rect x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// ellipse x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// fib x1 y1 x2 y2 [colour]
|
||||||
|
// measure x1 y1 x2 y2 [colour]
|
||||||
|
// hline price [colour] [dash]
|
||||||
|
// vline bar [colour] [dash]
|
||||||
|
// text x y [colour] label words...
|
||||||
|
// free x1 y1 x2 y2 ... [colour]
|
||||||
|
//
|
||||||
|
// x is a bar index (fractional allowed), y a price. Colours by name:
|
||||||
|
// blue red green orange purple teal magenta brown black slate.
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cairo.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace fdw {
|
||||||
|
|
||||||
|
// One anchor point in data coordinates.
|
||||||
|
struct Anchor {
|
||||||
|
double bar = 0; // bar index (fractional = between bars)
|
||||||
|
double price = 0; // price
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Kind {
|
||||||
|
Trend,
|
||||||
|
Ray,
|
||||||
|
HLine,
|
||||||
|
VLine,
|
||||||
|
Rect,
|
||||||
|
Ellipse,
|
||||||
|
Arrow,
|
||||||
|
Text,
|
||||||
|
Fib,
|
||||||
|
Freehand,
|
||||||
|
Measure
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Drawing {
|
||||||
|
Kind kind = Kind::Trend;
|
||||||
|
std::vector<Anchor> pts; // 1 for hline/vline/text, 2 for most,
|
||||||
|
// N for freehand
|
||||||
|
int color = 0; // palette index
|
||||||
|
double width = 1.5; // line width, px
|
||||||
|
bool dash = false;
|
||||||
|
std::string text; // Text kind only
|
||||||
|
};
|
||||||
|
|
||||||
|
// Drawing palette (strong, saturated colours that read on white).
|
||||||
|
inline const double kPalette[][3] = {
|
||||||
|
{0.10, 0.25, 0.80}, // 0 blue
|
||||||
|
{0.85, 0.10, 0.20}, // 1 red
|
||||||
|
{0.00, 0.55, 0.15}, // 2 green
|
||||||
|
{0.95, 0.50, 0.05}, // 3 orange
|
||||||
|
{0.55, 0.20, 0.75}, // 4 purple
|
||||||
|
{0.00, 0.60, 0.60}, // 5 teal
|
||||||
|
{0.85, 0.20, 0.65}, // 6 magenta
|
||||||
|
{0.55, 0.35, 0.15}, // 7 brown
|
||||||
|
{0.10, 0.10, 0.10}, // 8 black
|
||||||
|
{0.35, 0.40, 0.50}, // 9 slate
|
||||||
|
};
|
||||||
|
inline const int kPaletteSize = 10;
|
||||||
|
|
||||||
|
inline int color_index_of(const std::string& name) {
|
||||||
|
static const char* kNames[] = {"blue", "red", "green", "orange",
|
||||||
|
"purple", "teal", "magenta", "brown",
|
||||||
|
"black", "slate"};
|
||||||
|
std::string low;
|
||||||
|
for (char c : name) low += (char)std::tolower((unsigned char)c);
|
||||||
|
for (int i = 0; i < kPaletteSize; ++i)
|
||||||
|
if (low == kNames[i]) return i;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const char* kind_name(Kind k) {
|
||||||
|
switch (k) {
|
||||||
|
case Kind::Trend: return "trend";
|
||||||
|
case Kind::Ray: return "ray";
|
||||||
|
case Kind::HLine: return "hline";
|
||||||
|
case Kind::VLine: return "vline";
|
||||||
|
case Kind::Rect: return "rect";
|
||||||
|
case Kind::Ellipse: return "ellipse";
|
||||||
|
case Kind::Arrow: return "arrow";
|
||||||
|
case Kind::Text: return "text";
|
||||||
|
case Kind::Fib: return "fib";
|
||||||
|
case Kind::Freehand: return "freehand";
|
||||||
|
case Kind::Measure: return "measure";
|
||||||
|
}
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool kind_from_name(const std::string& s, Kind& out) {
|
||||||
|
std::string low;
|
||||||
|
for (char c : s) low += (char)std::tolower((unsigned char)c);
|
||||||
|
if (low == "trend") out = Kind::Trend;
|
||||||
|
else if (low == "ray") out = Kind::Ray;
|
||||||
|
else if (low == "hline" || low == "horizontal") out = Kind::HLine;
|
||||||
|
else if (low == "vline" || low == "vertical") out = Kind::VLine;
|
||||||
|
else if (low == "rect" || low == "rectangle") out = Kind::Rect;
|
||||||
|
else if (low == "ellipse" || low == "oval") out = Kind::Ellipse;
|
||||||
|
else if (low == "arrow") out = Kind::Arrow;
|
||||||
|
else if (low == "text" || low == "label") out = Kind::Text;
|
||||||
|
else if (low == "fib" || low == "fibonacci") out = Kind::Fib;
|
||||||
|
else if (low == "free" || low == "freehand") out = Kind::Freehand;
|
||||||
|
else if (low == "measure") out = Kind::Measure;
|
||||||
|
else return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The data -> pixel mapping for one render. x/y/w/h is the price-pane
|
||||||
|
// plot rectangle in pixels; begin/count is the visible window in bar
|
||||||
|
// units; lo/hi is the visible price range. Bar centres sit at
|
||||||
|
// (bar - begin + 0.5) slots, matching the candle layout.
|
||||||
|
struct Map {
|
||||||
|
double x = 0, y = 0, w = 0, h = 0;
|
||||||
|
double begin = 0, count = 1;
|
||||||
|
double lo = 0, hi = 1;
|
||||||
|
|
||||||
|
double px(double bar) const {
|
||||||
|
return x + (bar - begin + 0.5) * (w / count);
|
||||||
|
}
|
||||||
|
double py(double price) const {
|
||||||
|
return y + (hi - price) / (hi - lo) * h;
|
||||||
|
}
|
||||||
|
double bar_at(double px_) const {
|
||||||
|
return begin + (px_ - x) / (w / count) - 0.5;
|
||||||
|
}
|
||||||
|
double price_at(double py_) const {
|
||||||
|
return hi - (py_ - y) / h * (hi - lo);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
inline void set_style(cairo_t* cr, const Drawing& d, bool selected) {
|
||||||
|
int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize;
|
||||||
|
const double* c = kPalette[ci];
|
||||||
|
cairo_set_source_rgba(cr, c[0], c[1], c[2], 0.92);
|
||||||
|
cairo_set_line_width(cr, d.width + (selected ? 1.0 : 0.0));
|
||||||
|
if (d.dash) {
|
||||||
|
static const double kDash[] = {6, 4};
|
||||||
|
cairo_set_dash(cr, kDash, 2, 0);
|
||||||
|
} else {
|
||||||
|
cairo_set_dash(cr, nullptr, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double seg_dist(double px, double py, double x1, double y1,
|
||||||
|
double x2, double y2) {
|
||||||
|
double dx = x2 - x1, dy = y2 - y1;
|
||||||
|
double len2 = dx * dx + dy * dy;
|
||||||
|
double t = len2 > 0 ? ((px - x1) * dx + (py - y1) * dy) / len2 : 0;
|
||||||
|
t = std::max(0.0, std::min(1.0, t));
|
||||||
|
double ex = x1 + t * dx - px, ey = y1 + t * dy - py;
|
||||||
|
return std::sqrt(ex * ex + ey * ey);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A small filled label with a light backing box (text + measure).
|
||||||
|
inline void label_box(cairo_t* cr, double x, double y,
|
||||||
|
const std::string& s, const double* col) {
|
||||||
|
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
|
||||||
|
CAIRO_FONT_WEIGHT_NORMAL);
|
||||||
|
cairo_set_font_size(cr, 11);
|
||||||
|
cairo_text_extents_t te;
|
||||||
|
cairo_text_extents(cr, s.c_str(), &te);
|
||||||
|
cairo_set_source_rgba(cr, 1, 1, 1, 0.85);
|
||||||
|
cairo_rectangle(cr, x - 3, y - te.height - 4, te.width + 6,
|
||||||
|
te.height + 5);
|
||||||
|
cairo_fill(cr);
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0);
|
||||||
|
cairo_move_to(cr, x, y - 3);
|
||||||
|
cairo_show_text(cr, s.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// Render one drawing. Clips to the plot rectangle (rays and lines may
|
||||||
|
// run off the edge). `selected` draws it slightly bolder.
|
||||||
|
inline void draw(cairo_t* cr, const Map& m, const Drawing& d,
|
||||||
|
bool selected = false) {
|
||||||
|
if (d.pts.empty() || m.w < 2 || m.h < 2 || m.hi - m.lo < 1e-12)
|
||||||
|
return;
|
||||||
|
int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize;
|
||||||
|
const double* col = kPalette[ci];
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_rectangle(cr, m.x - 40, m.y - 40, m.w + 80, m.h + 80);
|
||||||
|
cairo_clip(cr);
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
auto X = [&](const Anchor& a) { return m.px(a.bar); };
|
||||||
|
auto Y = [&](const Anchor& a) { return m.py(a.price); };
|
||||||
|
switch (d.kind) {
|
||||||
|
case Kind::Trend:
|
||||||
|
case Kind::Measure: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0]));
|
||||||
|
cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1]));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
if (d.kind == Kind::Measure) {
|
||||||
|
double dp = d.pts[1].price - d.pts[0].price;
|
||||||
|
double pct = d.pts[0].price != 0
|
||||||
|
? dp / std::fabs(d.pts[0].price) * 100
|
||||||
|
: 0;
|
||||||
|
long bars = std::lround(d.pts[1].bar - d.pts[0].bar);
|
||||||
|
char buf[96];
|
||||||
|
std::snprintf(buf, sizeof buf, "%+.2f (%+.2f%%) / %ld bars",
|
||||||
|
dp, pct, bars);
|
||||||
|
detail::label_box(cr,
|
||||||
|
(X(d.pts[0]) + X(d.pts[1])) / 2,
|
||||||
|
(Y(d.pts[0]) + Y(d.pts[1])) / 2 - 4,
|
||||||
|
buf, col);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Ray: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = x1 + (X(d.pts[1]) - x1) * 1000;
|
||||||
|
double y2 = y1 + (Y(d.pts[1]) - y1) * 1000;
|
||||||
|
cairo_move_to(cr, x1, y1);
|
||||||
|
cairo_line_to(cr, x2, y2);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Arrow: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = X(d.pts[1]), y2 = Y(d.pts[1]);
|
||||||
|
cairo_move_to(cr, x1, y1);
|
||||||
|
cairo_line_to(cr, x2, y2);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
double ang = std::atan2(y2 - y1, x2 - x1);
|
||||||
|
double alen = 11;
|
||||||
|
cairo_move_to(cr, x2, y2);
|
||||||
|
cairo_line_to(cr, x2 - alen * std::cos(ang - 0.42),
|
||||||
|
y2 - alen * std::sin(ang - 0.42));
|
||||||
|
cairo_move_to(cr, x2, y2);
|
||||||
|
cairo_line_to(cr, x2 - alen * std::cos(ang + 0.42),
|
||||||
|
y2 - alen * std::sin(ang + 0.42));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::HLine: {
|
||||||
|
double y = Y(d.pts[0]);
|
||||||
|
cairo_move_to(cr, m.x, y);
|
||||||
|
cairo_line_to(cr, m.x + m.w, y);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
char buf[32];
|
||||||
|
std::snprintf(buf, sizeof buf, "%.2f", d.pts[0].price);
|
||||||
|
detail::label_box(cr, m.x + 4, y - 2, buf, col);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::VLine: {
|
||||||
|
double x = X(d.pts[0]);
|
||||||
|
cairo_move_to(cr, x, m.y);
|
||||||
|
cairo_line_to(cr, x, m.y + m.h);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Rect:
|
||||||
|
case Kind::Ellipse: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = X(d.pts[1]), y2 = Y(d.pts[1]);
|
||||||
|
double rx = std::min(x1, x2), ry = std::min(y1, y2);
|
||||||
|
double rw = std::fabs(x2 - x1), rh = std::fabs(y2 - y1);
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.08);
|
||||||
|
if (d.kind == Kind::Rect) {
|
||||||
|
cairo_rectangle(cr, rx, ry, rw, rh);
|
||||||
|
cairo_fill(cr);
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
cairo_rectangle(cr, rx, ry, rw, rh);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
} else {
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_translate(cr, rx + rw / 2, ry + rh / 2);
|
||||||
|
cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1));
|
||||||
|
cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI);
|
||||||
|
cairo_restore(cr);
|
||||||
|
cairo_fill(cr);
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_translate(cr, rx + rw / 2, ry + rh / 2);
|
||||||
|
cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1));
|
||||||
|
cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI);
|
||||||
|
cairo_restore(cr);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Text: {
|
||||||
|
if (d.text.empty()) break;
|
||||||
|
detail::label_box(cr, X(d.pts[0]) + 2, Y(d.pts[0]) - 2,
|
||||||
|
d.text, col);
|
||||||
|
// Anchor dot.
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0);
|
||||||
|
cairo_arc(cr, X(d.pts[0]), Y(d.pts[0]), 2.5, 0, 2 * M_PI);
|
||||||
|
cairo_fill(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Fib: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
static const double kLevels[] = {0, 0.236, 0.382, 0.5,
|
||||||
|
0.618, 0.786, 1.0};
|
||||||
|
double x1 = X(d.pts[0]);
|
||||||
|
cairo_set_line_width(cr, 1.0);
|
||||||
|
for (double lv : kLevels) {
|
||||||
|
double price =
|
||||||
|
d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv;
|
||||||
|
double y = m.py(price);
|
||||||
|
static const double kDash[] = {4, 3};
|
||||||
|
cairo_set_dash(cr, kDash, 2, 0);
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.75);
|
||||||
|
cairo_move_to(cr, x1, y);
|
||||||
|
cairo_line_to(cr, m.x + m.w, y);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
cairo_set_dash(cr, nullptr, 0, 0);
|
||||||
|
char buf[48];
|
||||||
|
std::snprintf(buf, sizeof buf, "%.1f%% %.2f", lv * 100,
|
||||||
|
price);
|
||||||
|
detail::label_box(cr, x1 + 4, y - 2, buf, col);
|
||||||
|
}
|
||||||
|
// Connector between the anchors.
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0]));
|
||||||
|
cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1]));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Freehand: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0]));
|
||||||
|
for (size_t i = 1; i < d.pts.size(); ++i)
|
||||||
|
cairo_line_to(cr, X(d.pts[i]), Y(d.pts[i]));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (selected) {
|
||||||
|
// Corner dots on the anchors.
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0);
|
||||||
|
for (const Anchor& a : d.pts) {
|
||||||
|
cairo_arc(cr, X(a), Y(a), 3, 0, 2 * M_PI);
|
||||||
|
cairo_fill(cr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cairo_restore(cr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is pixel (px, py) within `tol` px of the drawing? Used by the
|
||||||
|
// select/move and erase tools.
|
||||||
|
inline bool hit_test(const Map& m, const Drawing& d, double px,
|
||||||
|
double py, double tol = 6) {
|
||||||
|
if (d.pts.empty()) return false;
|
||||||
|
auto X = [&](const Anchor& a) { return m.px(a.bar); };
|
||||||
|
auto Y = [&](const Anchor& a) { return m.py(a.price); };
|
||||||
|
switch (d.kind) {
|
||||||
|
case Kind::Trend:
|
||||||
|
case Kind::Ray:
|
||||||
|
case Kind::Arrow:
|
||||||
|
case Kind::Measure:
|
||||||
|
if (d.pts.size() < 2) return false;
|
||||||
|
return detail::seg_dist(px, py, X(d.pts[0]), Y(d.pts[0]),
|
||||||
|
X(d.pts[1]), Y(d.pts[1])) <= tol;
|
||||||
|
case Kind::HLine:
|
||||||
|
return std::fabs(py - Y(d.pts[0])) <= tol && px >= m.x &&
|
||||||
|
px <= m.x + m.w;
|
||||||
|
case Kind::VLine:
|
||||||
|
return std::fabs(px - X(d.pts[0])) <= tol && py >= m.y &&
|
||||||
|
py <= m.y + m.h;
|
||||||
|
case Kind::Rect:
|
||||||
|
case Kind::Ellipse: {
|
||||||
|
if (d.pts.size() < 2) return false;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = X(d.pts[1]), y2 = Y(d.pts[1]);
|
||||||
|
bool inside = px >= std::min(x1, x2) && px <= std::max(x1, x2) &&
|
||||||
|
py >= std::min(y1, y2) && py <= std::max(y1, y2);
|
||||||
|
bool near_edge =
|
||||||
|
detail::seg_dist(px, py, x1, y1, x2, y1) <= tol ||
|
||||||
|
detail::seg_dist(px, py, x2, y1, x2, y2) <= tol ||
|
||||||
|
detail::seg_dist(px, py, x2, y2, x1, y2) <= tol ||
|
||||||
|
detail::seg_dist(px, py, x1, y2, x1, y1) <= tol;
|
||||||
|
return inside || near_edge;
|
||||||
|
}
|
||||||
|
case Kind::Text: {
|
||||||
|
double x = X(d.pts[0]), y = Y(d.pts[0]);
|
||||||
|
double w = 10 + (double)d.text.size() * 6.5;
|
||||||
|
return px >= x - 4 && px <= x + w && py >= y - 16 &&
|
||||||
|
py <= y + 4;
|
||||||
|
}
|
||||||
|
case Kind::Fib: {
|
||||||
|
if (d.pts.size() < 2) return false;
|
||||||
|
static const double kLevels[] = {0, 0.236, 0.382, 0.5,
|
||||||
|
0.618, 0.786, 1.0};
|
||||||
|
for (double lv : kLevels) {
|
||||||
|
double price =
|
||||||
|
d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv;
|
||||||
|
if (std::fabs(py - m.py(price)) <= tol && px >= X(d.pts[0]))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
case Kind::Freehand:
|
||||||
|
for (size_t i = 1; i < d.pts.size(); ++i)
|
||||||
|
if (detail::seg_dist(px, py, X(d.pts[i - 1]),
|
||||||
|
Y(d.pts[i - 1]), X(d.pts[i]),
|
||||||
|
Y(d.pts[i])) <= tol)
|
||||||
|
return true;
|
||||||
|
return d.pts.size() == 1 &&
|
||||||
|
std::hypot(px - X(d.pts[0]), py - Y(d.pts[0])) <= tol;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shift every anchor by a data-space delta (select/move drags).
|
||||||
|
inline void move(Drawing& d, double dbar, double dprice) {
|
||||||
|
for (Anchor& a : d.pts) {
|
||||||
|
a.bar += dbar;
|
||||||
|
a.price += dprice;
|
||||||
|
}
|
||||||
|
if (d.kind == Kind::HLine)
|
||||||
|
for (Anchor& a : d.pts) a.bar -= dbar; // bar is meaningless
|
||||||
|
if (d.kind == Kind::VLine)
|
||||||
|
for (Anchor& a : d.pts) a.price -= dprice;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse one spec (grammar in the header comment).
|
||||||
|
inline std::optional<Drawing> parse_spec(const std::string& spec) {
|
||||||
|
// Tokenize on whitespace, remembering where the tail starts (text).
|
||||||
|
std::vector<std::string> tok;
|
||||||
|
std::vector<size_t> tok_pos;
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < spec.size()) {
|
||||||
|
while (i < spec.size() &&
|
||||||
|
(spec[i] == ' ' || spec[i] == '\t'))
|
||||||
|
++i;
|
||||||
|
if (i >= spec.size()) break;
|
||||||
|
size_t s = i;
|
||||||
|
while (i < spec.size() && spec[i] != ' ' && spec[i] != '\t')
|
||||||
|
++i;
|
||||||
|
tok.push_back(spec.substr(s, i - s));
|
||||||
|
tok_pos.push_back(s);
|
||||||
|
}
|
||||||
|
if (tok.empty()) return std::nullopt;
|
||||||
|
Drawing d;
|
||||||
|
if (!kind_from_name(tok[0], d.kind)) return std::nullopt;
|
||||||
|
size_t t = 1;
|
||||||
|
auto number = [&](double& v) {
|
||||||
|
if (t >= tok.size()) return false;
|
||||||
|
char* end = nullptr;
|
||||||
|
v = std::strtod(tok[t].c_str(), &end);
|
||||||
|
if (end == tok[t].c_str()) return false;
|
||||||
|
++t;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
auto anchor = [&](Anchor& a) {
|
||||||
|
return number(a.bar) && number(a.price);
|
||||||
|
};
|
||||||
|
auto tail_options = [&]() {
|
||||||
|
while (t < tok.size()) {
|
||||||
|
int ci = color_index_of(tok[t]);
|
||||||
|
if (ci >= 0) {
|
||||||
|
d.color = ci;
|
||||||
|
++t;
|
||||||
|
} else if (tok[t] == "dash") {
|
||||||
|
d.dash = true;
|
||||||
|
++t;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
switch (d.kind) {
|
||||||
|
case Kind::HLine: {
|
||||||
|
double p;
|
||||||
|
if (!number(p)) return std::nullopt;
|
||||||
|
d.pts.push_back({0, p});
|
||||||
|
tail_options();
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
case Kind::VLine: {
|
||||||
|
double b;
|
||||||
|
if (!number(b)) return std::nullopt;
|
||||||
|
d.pts.push_back({b, 0});
|
||||||
|
tail_options();
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
case Kind::Text: {
|
||||||
|
Anchor a;
|
||||||
|
if (!anchor(a)) return std::nullopt;
|
||||||
|
d.pts.push_back(a);
|
||||||
|
tail_options();
|
||||||
|
if (t < tok.size())
|
||||||
|
d.text = spec.substr(tok_pos[t]);
|
||||||
|
if (d.text.empty()) return std::nullopt;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
case Kind::Freehand: {
|
||||||
|
while (t < tok.size()) {
|
||||||
|
char* end = nullptr;
|
||||||
|
double v = std::strtod(tok[t].c_str(), &end);
|
||||||
|
if (end == tok[t].c_str()) break;
|
||||||
|
Anchor a;
|
||||||
|
a.bar = v;
|
||||||
|
++t;
|
||||||
|
if (!number(a.price)) return std::nullopt;
|
||||||
|
d.pts.push_back(a);
|
||||||
|
}
|
||||||
|
tail_options();
|
||||||
|
if (d.pts.size() < 2) return std::nullopt;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
default: { // two-anchor kinds
|
||||||
|
Anchor a, b;
|
||||||
|
if (!anchor(a) || !anchor(b)) return std::nullopt;
|
||||||
|
d.pts.push_back(a);
|
||||||
|
d.pts.push_back(b);
|
||||||
|
tail_options();
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse a semicolon-separated list of specs; bad entries are skipped.
|
||||||
|
inline std::vector<Drawing> parse_specs(const std::string& specs) {
|
||||||
|
std::vector<Drawing> out;
|
||||||
|
size_t start = 0;
|
||||||
|
for (size_t i = 0; i <= specs.size(); ++i) {
|
||||||
|
if (i == specs.size() || specs[i] == ';') {
|
||||||
|
auto d = parse_spec(specs.substr(start, i - start));
|
||||||
|
if (d) out.push_back(*d);
|
||||||
|
start = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Canonical spec for a drawing (parse_spec(to_spec(d)) round-trips).
|
||||||
|
inline std::string to_spec(const Drawing& d) {
|
||||||
|
static const char* kNames[] = {"blue", "red", "green", "orange",
|
||||||
|
"purple", "teal", "magenta", "brown",
|
||||||
|
"black", "slate"};
|
||||||
|
int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize;
|
||||||
|
char buf[64];
|
||||||
|
std::string s = kind_name(d.kind);
|
||||||
|
auto num = [&](double v) {
|
||||||
|
std::snprintf(buf, sizeof buf, " %g", v);
|
||||||
|
s += buf;
|
||||||
|
};
|
||||||
|
if (d.kind == Kind::HLine) {
|
||||||
|
num(d.pts.empty() ? 0 : d.pts[0].price);
|
||||||
|
} else if (d.kind == Kind::VLine) {
|
||||||
|
num(d.pts.empty() ? 0 : d.pts[0].bar);
|
||||||
|
} else {
|
||||||
|
for (const Anchor& a : d.pts) {
|
||||||
|
num(a.bar);
|
||||||
|
num(a.price);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s += " ";
|
||||||
|
s += kNames[ci];
|
||||||
|
if (d.dash) s += " dash";
|
||||||
|
if (d.kind == Kind::Text) {
|
||||||
|
s += " ";
|
||||||
|
s += d.text;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace fdw
|
||||||
@@ -0,0 +1,602 @@
|
|||||||
|
// Generated from src/drawings.h by tools/embed_drawings.sh.
|
||||||
|
// Do not edit; run 'make' to regenerate.
|
||||||
|
#pragma once
|
||||||
|
static const char kDrawingsHeader[] = R"FDDRAWINGS9b2c(
|
||||||
|
// drawings.h — chart annotation module for the StockChart widget.
|
||||||
|
//
|
||||||
|
// One self-contained header (namespace fdw), in the same spirit as
|
||||||
|
// indicators.h. A Drawing is stored in DATA coordinates — an anchor is
|
||||||
|
// (bar index, price) — never in pixels, so annotations stay glued to
|
||||||
|
// the candles they were drawn on through widget resizes, zooms and
|
||||||
|
// pans: every render re-maps data -> pixels through the current Map.
|
||||||
|
//
|
||||||
|
// Kinds: trend line, ray, horizontal line, vertical line, rectangle,
|
||||||
|
// ellipse, arrow, text label, Fibonacci retracement, freehand stroke,
|
||||||
|
// and a measure tool (delta price / percent / bars between two points).
|
||||||
|
//
|
||||||
|
// Drawings can be created three ways: interactively (the generated
|
||||||
|
// StockChart's mouse tools build them), from a text spec via
|
||||||
|
// parse_spec()/parse_specs(), or directly in code. Spec grammar
|
||||||
|
// (semicolon-separated in parse_specs):
|
||||||
|
//
|
||||||
|
// trend x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// ray x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// arrow x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// rect x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// ellipse x1 y1 x2 y2 [colour] [dash]
|
||||||
|
// fib x1 y1 x2 y2 [colour]
|
||||||
|
// measure x1 y1 x2 y2 [colour]
|
||||||
|
// hline price [colour] [dash]
|
||||||
|
// vline bar [colour] [dash]
|
||||||
|
// text x y [colour] label words...
|
||||||
|
// free x1 y1 x2 y2 ... [colour]
|
||||||
|
//
|
||||||
|
// x is a bar index (fractional allowed), y a price. Colours by name:
|
||||||
|
// blue red green orange purple teal magenta brown black slate.
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cairo.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace fdw {
|
||||||
|
|
||||||
|
// One anchor point in data coordinates.
|
||||||
|
struct Anchor {
|
||||||
|
double bar = 0; // bar index (fractional = between bars)
|
||||||
|
double price = 0; // price
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Kind {
|
||||||
|
Trend,
|
||||||
|
Ray,
|
||||||
|
HLine,
|
||||||
|
VLine,
|
||||||
|
Rect,
|
||||||
|
Ellipse,
|
||||||
|
Arrow,
|
||||||
|
Text,
|
||||||
|
Fib,
|
||||||
|
Freehand,
|
||||||
|
Measure
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Drawing {
|
||||||
|
Kind kind = Kind::Trend;
|
||||||
|
std::vector<Anchor> pts; // 1 for hline/vline/text, 2 for most,
|
||||||
|
// N for freehand
|
||||||
|
int color = 0; // palette index
|
||||||
|
double width = 1.5; // line width, px
|
||||||
|
bool dash = false;
|
||||||
|
std::string text; // Text kind only
|
||||||
|
};
|
||||||
|
|
||||||
|
// Drawing palette (strong, saturated colours that read on white).
|
||||||
|
inline const double kPalette[][3] = {
|
||||||
|
{0.10, 0.25, 0.80}, // 0 blue
|
||||||
|
{0.85, 0.10, 0.20}, // 1 red
|
||||||
|
{0.00, 0.55, 0.15}, // 2 green
|
||||||
|
{0.95, 0.50, 0.05}, // 3 orange
|
||||||
|
{0.55, 0.20, 0.75}, // 4 purple
|
||||||
|
{0.00, 0.60, 0.60}, // 5 teal
|
||||||
|
{0.85, 0.20, 0.65}, // 6 magenta
|
||||||
|
{0.55, 0.35, 0.15}, // 7 brown
|
||||||
|
{0.10, 0.10, 0.10}, // 8 black
|
||||||
|
{0.35, 0.40, 0.50}, // 9 slate
|
||||||
|
};
|
||||||
|
inline const int kPaletteSize = 10;
|
||||||
|
|
||||||
|
inline int color_index_of(const std::string& name) {
|
||||||
|
static const char* kNames[] = {"blue", "red", "green", "orange",
|
||||||
|
"purple", "teal", "magenta", "brown",
|
||||||
|
"black", "slate"};
|
||||||
|
std::string low;
|
||||||
|
for (char c : name) low += (char)std::tolower((unsigned char)c);
|
||||||
|
for (int i = 0; i < kPaletteSize; ++i)
|
||||||
|
if (low == kNames[i]) return i;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const char* kind_name(Kind k) {
|
||||||
|
switch (k) {
|
||||||
|
case Kind::Trend: return "trend";
|
||||||
|
case Kind::Ray: return "ray";
|
||||||
|
case Kind::HLine: return "hline";
|
||||||
|
case Kind::VLine: return "vline";
|
||||||
|
case Kind::Rect: return "rect";
|
||||||
|
case Kind::Ellipse: return "ellipse";
|
||||||
|
case Kind::Arrow: return "arrow";
|
||||||
|
case Kind::Text: return "text";
|
||||||
|
case Kind::Fib: return "fib";
|
||||||
|
case Kind::Freehand: return "freehand";
|
||||||
|
case Kind::Measure: return "measure";
|
||||||
|
}
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool kind_from_name(const std::string& s, Kind& out) {
|
||||||
|
std::string low;
|
||||||
|
for (char c : s) low += (char)std::tolower((unsigned char)c);
|
||||||
|
if (low == "trend") out = Kind::Trend;
|
||||||
|
else if (low == "ray") out = Kind::Ray;
|
||||||
|
else if (low == "hline" || low == "horizontal") out = Kind::HLine;
|
||||||
|
else if (low == "vline" || low == "vertical") out = Kind::VLine;
|
||||||
|
else if (low == "rect" || low == "rectangle") out = Kind::Rect;
|
||||||
|
else if (low == "ellipse" || low == "oval") out = Kind::Ellipse;
|
||||||
|
else if (low == "arrow") out = Kind::Arrow;
|
||||||
|
else if (low == "text" || low == "label") out = Kind::Text;
|
||||||
|
else if (low == "fib" || low == "fibonacci") out = Kind::Fib;
|
||||||
|
else if (low == "free" || low == "freehand") out = Kind::Freehand;
|
||||||
|
else if (low == "measure") out = Kind::Measure;
|
||||||
|
else return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The data -> pixel mapping for one render. x/y/w/h is the price-pane
|
||||||
|
// plot rectangle in pixels; begin/count is the visible window in bar
|
||||||
|
// units; lo/hi is the visible price range. Bar centres sit at
|
||||||
|
// (bar - begin + 0.5) slots, matching the candle layout.
|
||||||
|
struct Map {
|
||||||
|
double x = 0, y = 0, w = 0, h = 0;
|
||||||
|
double begin = 0, count = 1;
|
||||||
|
double lo = 0, hi = 1;
|
||||||
|
|
||||||
|
double px(double bar) const {
|
||||||
|
return x + (bar - begin + 0.5) * (w / count);
|
||||||
|
}
|
||||||
|
double py(double price) const {
|
||||||
|
return y + (hi - price) / (hi - lo) * h;
|
||||||
|
}
|
||||||
|
double bar_at(double px_) const {
|
||||||
|
return begin + (px_ - x) / (w / count) - 0.5;
|
||||||
|
}
|
||||||
|
double price_at(double py_) const {
|
||||||
|
return hi - (py_ - y) / h * (hi - lo);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
inline void set_style(cairo_t* cr, const Drawing& d, bool selected) {
|
||||||
|
int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize;
|
||||||
|
const double* c = kPalette[ci];
|
||||||
|
cairo_set_source_rgba(cr, c[0], c[1], c[2], 0.92);
|
||||||
|
cairo_set_line_width(cr, d.width + (selected ? 1.0 : 0.0));
|
||||||
|
if (d.dash) {
|
||||||
|
static const double kDash[] = {6, 4};
|
||||||
|
cairo_set_dash(cr, kDash, 2, 0);
|
||||||
|
} else {
|
||||||
|
cairo_set_dash(cr, nullptr, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double seg_dist(double px, double py, double x1, double y1,
|
||||||
|
double x2, double y2) {
|
||||||
|
double dx = x2 - x1, dy = y2 - y1;
|
||||||
|
double len2 = dx * dx + dy * dy;
|
||||||
|
double t = len2 > 0 ? ((px - x1) * dx + (py - y1) * dy) / len2 : 0;
|
||||||
|
t = std::max(0.0, std::min(1.0, t));
|
||||||
|
double ex = x1 + t * dx - px, ey = y1 + t * dy - py;
|
||||||
|
return std::sqrt(ex * ex + ey * ey);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A small filled label with a light backing box (text + measure).
|
||||||
|
inline void label_box(cairo_t* cr, double x, double y,
|
||||||
|
const std::string& s, const double* col) {
|
||||||
|
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
|
||||||
|
CAIRO_FONT_WEIGHT_NORMAL);
|
||||||
|
cairo_set_font_size(cr, 11);
|
||||||
|
cairo_text_extents_t te;
|
||||||
|
cairo_text_extents(cr, s.c_str(), &te);
|
||||||
|
cairo_set_source_rgba(cr, 1, 1, 1, 0.85);
|
||||||
|
cairo_rectangle(cr, x - 3, y - te.height - 4, te.width + 6,
|
||||||
|
te.height + 5);
|
||||||
|
cairo_fill(cr);
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0);
|
||||||
|
cairo_move_to(cr, x, y - 3);
|
||||||
|
cairo_show_text(cr, s.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// Render one drawing. Clips to the plot rectangle (rays and lines may
|
||||||
|
// run off the edge). `selected` draws it slightly bolder.
|
||||||
|
inline void draw(cairo_t* cr, const Map& m, const Drawing& d,
|
||||||
|
bool selected = false) {
|
||||||
|
if (d.pts.empty() || m.w < 2 || m.h < 2 || m.hi - m.lo < 1e-12)
|
||||||
|
return;
|
||||||
|
int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize;
|
||||||
|
const double* col = kPalette[ci];
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_rectangle(cr, m.x - 40, m.y - 40, m.w + 80, m.h + 80);
|
||||||
|
cairo_clip(cr);
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
auto X = [&](const Anchor& a) { return m.px(a.bar); };
|
||||||
|
auto Y = [&](const Anchor& a) { return m.py(a.price); };
|
||||||
|
switch (d.kind) {
|
||||||
|
case Kind::Trend:
|
||||||
|
case Kind::Measure: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0]));
|
||||||
|
cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1]));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
if (d.kind == Kind::Measure) {
|
||||||
|
double dp = d.pts[1].price - d.pts[0].price;
|
||||||
|
double pct = d.pts[0].price != 0
|
||||||
|
? dp / std::fabs(d.pts[0].price) * 100
|
||||||
|
: 0;
|
||||||
|
long bars = std::lround(d.pts[1].bar - d.pts[0].bar);
|
||||||
|
char buf[96];
|
||||||
|
std::snprintf(buf, sizeof buf, "%+.2f (%+.2f%%) / %ld bars",
|
||||||
|
dp, pct, bars);
|
||||||
|
detail::label_box(cr,
|
||||||
|
(X(d.pts[0]) + X(d.pts[1])) / 2,
|
||||||
|
(Y(d.pts[0]) + Y(d.pts[1])) / 2 - 4,
|
||||||
|
buf, col);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Ray: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = x1 + (X(d.pts[1]) - x1) * 1000;
|
||||||
|
double y2 = y1 + (Y(d.pts[1]) - y1) * 1000;
|
||||||
|
cairo_move_to(cr, x1, y1);
|
||||||
|
cairo_line_to(cr, x2, y2);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Arrow: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = X(d.pts[1]), y2 = Y(d.pts[1]);
|
||||||
|
cairo_move_to(cr, x1, y1);
|
||||||
|
cairo_line_to(cr, x2, y2);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
double ang = std::atan2(y2 - y1, x2 - x1);
|
||||||
|
double alen = 11;
|
||||||
|
cairo_move_to(cr, x2, y2);
|
||||||
|
cairo_line_to(cr, x2 - alen * std::cos(ang - 0.42),
|
||||||
|
y2 - alen * std::sin(ang - 0.42));
|
||||||
|
cairo_move_to(cr, x2, y2);
|
||||||
|
cairo_line_to(cr, x2 - alen * std::cos(ang + 0.42),
|
||||||
|
y2 - alen * std::sin(ang + 0.42));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::HLine: {
|
||||||
|
double y = Y(d.pts[0]);
|
||||||
|
cairo_move_to(cr, m.x, y);
|
||||||
|
cairo_line_to(cr, m.x + m.w, y);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
char buf[32];
|
||||||
|
std::snprintf(buf, sizeof buf, "%.2f", d.pts[0].price);
|
||||||
|
detail::label_box(cr, m.x + 4, y - 2, buf, col);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::VLine: {
|
||||||
|
double x = X(d.pts[0]);
|
||||||
|
cairo_move_to(cr, x, m.y);
|
||||||
|
cairo_line_to(cr, x, m.y + m.h);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Rect:
|
||||||
|
case Kind::Ellipse: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = X(d.pts[1]), y2 = Y(d.pts[1]);
|
||||||
|
double rx = std::min(x1, x2), ry = std::min(y1, y2);
|
||||||
|
double rw = std::fabs(x2 - x1), rh = std::fabs(y2 - y1);
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.08);
|
||||||
|
if (d.kind == Kind::Rect) {
|
||||||
|
cairo_rectangle(cr, rx, ry, rw, rh);
|
||||||
|
cairo_fill(cr);
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
cairo_rectangle(cr, rx, ry, rw, rh);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
} else {
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_translate(cr, rx + rw / 2, ry + rh / 2);
|
||||||
|
cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1));
|
||||||
|
cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI);
|
||||||
|
cairo_restore(cr);
|
||||||
|
cairo_fill(cr);
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_translate(cr, rx + rw / 2, ry + rh / 2);
|
||||||
|
cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1));
|
||||||
|
cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI);
|
||||||
|
cairo_restore(cr);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Text: {
|
||||||
|
if (d.text.empty()) break;
|
||||||
|
detail::label_box(cr, X(d.pts[0]) + 2, Y(d.pts[0]) - 2,
|
||||||
|
d.text, col);
|
||||||
|
// Anchor dot.
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0);
|
||||||
|
cairo_arc(cr, X(d.pts[0]), Y(d.pts[0]), 2.5, 0, 2 * M_PI);
|
||||||
|
cairo_fill(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Fib: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
static const double kLevels[] = {0, 0.236, 0.382, 0.5,
|
||||||
|
0.618, 0.786, 1.0};
|
||||||
|
double x1 = X(d.pts[0]);
|
||||||
|
cairo_set_line_width(cr, 1.0);
|
||||||
|
for (double lv : kLevels) {
|
||||||
|
double price =
|
||||||
|
d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv;
|
||||||
|
double y = m.py(price);
|
||||||
|
static const double kDash[] = {4, 3};
|
||||||
|
cairo_set_dash(cr, kDash, 2, 0);
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.75);
|
||||||
|
cairo_move_to(cr, x1, y);
|
||||||
|
cairo_line_to(cr, m.x + m.w, y);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
cairo_set_dash(cr, nullptr, 0, 0);
|
||||||
|
char buf[48];
|
||||||
|
std::snprintf(buf, sizeof buf, "%.1f%% %.2f", lv * 100,
|
||||||
|
price);
|
||||||
|
detail::label_box(cr, x1 + 4, y - 2, buf, col);
|
||||||
|
}
|
||||||
|
// Connector between the anchors.
|
||||||
|
detail::set_style(cr, d, selected);
|
||||||
|
cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0]));
|
||||||
|
cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1]));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kind::Freehand: {
|
||||||
|
if (d.pts.size() < 2) break;
|
||||||
|
cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0]));
|
||||||
|
for (size_t i = 1; i < d.pts.size(); ++i)
|
||||||
|
cairo_line_to(cr, X(d.pts[i]), Y(d.pts[i]));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (selected) {
|
||||||
|
// Corner dots on the anchors.
|
||||||
|
cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0);
|
||||||
|
for (const Anchor& a : d.pts) {
|
||||||
|
cairo_arc(cr, X(a), Y(a), 3, 0, 2 * M_PI);
|
||||||
|
cairo_fill(cr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cairo_restore(cr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is pixel (px, py) within `tol` px of the drawing? Used by the
|
||||||
|
// select/move and erase tools.
|
||||||
|
inline bool hit_test(const Map& m, const Drawing& d, double px,
|
||||||
|
double py, double tol = 6) {
|
||||||
|
if (d.pts.empty()) return false;
|
||||||
|
auto X = [&](const Anchor& a) { return m.px(a.bar); };
|
||||||
|
auto Y = [&](const Anchor& a) { return m.py(a.price); };
|
||||||
|
switch (d.kind) {
|
||||||
|
case Kind::Trend:
|
||||||
|
case Kind::Ray:
|
||||||
|
case Kind::Arrow:
|
||||||
|
case Kind::Measure:
|
||||||
|
if (d.pts.size() < 2) return false;
|
||||||
|
return detail::seg_dist(px, py, X(d.pts[0]), Y(d.pts[0]),
|
||||||
|
X(d.pts[1]), Y(d.pts[1])) <= tol;
|
||||||
|
case Kind::HLine:
|
||||||
|
return std::fabs(py - Y(d.pts[0])) <= tol && px >= m.x &&
|
||||||
|
px <= m.x + m.w;
|
||||||
|
case Kind::VLine:
|
||||||
|
return std::fabs(px - X(d.pts[0])) <= tol && py >= m.y &&
|
||||||
|
py <= m.y + m.h;
|
||||||
|
case Kind::Rect:
|
||||||
|
case Kind::Ellipse: {
|
||||||
|
if (d.pts.size() < 2) return false;
|
||||||
|
double x1 = X(d.pts[0]), y1 = Y(d.pts[0]);
|
||||||
|
double x2 = X(d.pts[1]), y2 = Y(d.pts[1]);
|
||||||
|
bool inside = px >= std::min(x1, x2) && px <= std::max(x1, x2) &&
|
||||||
|
py >= std::min(y1, y2) && py <= std::max(y1, y2);
|
||||||
|
bool near_edge =
|
||||||
|
detail::seg_dist(px, py, x1, y1, x2, y1) <= tol ||
|
||||||
|
detail::seg_dist(px, py, x2, y1, x2, y2) <= tol ||
|
||||||
|
detail::seg_dist(px, py, x2, y2, x1, y2) <= tol ||
|
||||||
|
detail::seg_dist(px, py, x1, y2, x1, y1) <= tol;
|
||||||
|
return inside || near_edge;
|
||||||
|
}
|
||||||
|
case Kind::Text: {
|
||||||
|
double x = X(d.pts[0]), y = Y(d.pts[0]);
|
||||||
|
double w = 10 + (double)d.text.size() * 6.5;
|
||||||
|
return px >= x - 4 && px <= x + w && py >= y - 16 &&
|
||||||
|
py <= y + 4;
|
||||||
|
}
|
||||||
|
case Kind::Fib: {
|
||||||
|
if (d.pts.size() < 2) return false;
|
||||||
|
static const double kLevels[] = {0, 0.236, 0.382, 0.5,
|
||||||
|
0.618, 0.786, 1.0};
|
||||||
|
for (double lv : kLevels) {
|
||||||
|
double price =
|
||||||
|
d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv;
|
||||||
|
if (std::fabs(py - m.py(price)) <= tol && px >= X(d.pts[0]))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
case Kind::Freehand:
|
||||||
|
for (size_t i = 1; i < d.pts.size(); ++i)
|
||||||
|
if (detail::seg_dist(px, py, X(d.pts[i - 1]),
|
||||||
|
Y(d.pts[i - 1]), X(d.pts[i]),
|
||||||
|
Y(d.pts[i])) <= tol)
|
||||||
|
return true;
|
||||||
|
return d.pts.size() == 1 &&
|
||||||
|
std::hypot(px - X(d.pts[0]), py - Y(d.pts[0])) <= tol;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shift every anchor by a data-space delta (select/move drags).
|
||||||
|
inline void move(Drawing& d, double dbar, double dprice) {
|
||||||
|
for (Anchor& a : d.pts) {
|
||||||
|
a.bar += dbar;
|
||||||
|
a.price += dprice;
|
||||||
|
}
|
||||||
|
if (d.kind == Kind::HLine)
|
||||||
|
for (Anchor& a : d.pts) a.bar -= dbar; // bar is meaningless
|
||||||
|
if (d.kind == Kind::VLine)
|
||||||
|
for (Anchor& a : d.pts) a.price -= dprice;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse one spec (grammar in the header comment).
|
||||||
|
inline std::optional<Drawing> parse_spec(const std::string& spec) {
|
||||||
|
// Tokenize on whitespace, remembering where the tail starts (text).
|
||||||
|
std::vector<std::string> tok;
|
||||||
|
std::vector<size_t> tok_pos;
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < spec.size()) {
|
||||||
|
while (i < spec.size() &&
|
||||||
|
(spec[i] == ' ' || spec[i] == '\t'))
|
||||||
|
++i;
|
||||||
|
if (i >= spec.size()) break;
|
||||||
|
size_t s = i;
|
||||||
|
while (i < spec.size() && spec[i] != ' ' && spec[i] != '\t')
|
||||||
|
++i;
|
||||||
|
tok.push_back(spec.substr(s, i - s));
|
||||||
|
tok_pos.push_back(s);
|
||||||
|
}
|
||||||
|
if (tok.empty()) return std::nullopt;
|
||||||
|
Drawing d;
|
||||||
|
if (!kind_from_name(tok[0], d.kind)) return std::nullopt;
|
||||||
|
size_t t = 1;
|
||||||
|
auto number = [&](double& v) {
|
||||||
|
if (t >= tok.size()) return false;
|
||||||
|
char* end = nullptr;
|
||||||
|
v = std::strtod(tok[t].c_str(), &end);
|
||||||
|
if (end == tok[t].c_str()) return false;
|
||||||
|
++t;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
auto anchor = [&](Anchor& a) {
|
||||||
|
return number(a.bar) && number(a.price);
|
||||||
|
};
|
||||||
|
auto tail_options = [&]() {
|
||||||
|
while (t < tok.size()) {
|
||||||
|
int ci = color_index_of(tok[t]);
|
||||||
|
if (ci >= 0) {
|
||||||
|
d.color = ci;
|
||||||
|
++t;
|
||||||
|
} else if (tok[t] == "dash") {
|
||||||
|
d.dash = true;
|
||||||
|
++t;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
switch (d.kind) {
|
||||||
|
case Kind::HLine: {
|
||||||
|
double p;
|
||||||
|
if (!number(p)) return std::nullopt;
|
||||||
|
d.pts.push_back({0, p});
|
||||||
|
tail_options();
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
case Kind::VLine: {
|
||||||
|
double b;
|
||||||
|
if (!number(b)) return std::nullopt;
|
||||||
|
d.pts.push_back({b, 0});
|
||||||
|
tail_options();
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
case Kind::Text: {
|
||||||
|
Anchor a;
|
||||||
|
if (!anchor(a)) return std::nullopt;
|
||||||
|
d.pts.push_back(a);
|
||||||
|
tail_options();
|
||||||
|
if (t < tok.size())
|
||||||
|
d.text = spec.substr(tok_pos[t]);
|
||||||
|
if (d.text.empty()) return std::nullopt;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
case Kind::Freehand: {
|
||||||
|
while (t < tok.size()) {
|
||||||
|
char* end = nullptr;
|
||||||
|
double v = std::strtod(tok[t].c_str(), &end);
|
||||||
|
if (end == tok[t].c_str()) break;
|
||||||
|
Anchor a;
|
||||||
|
a.bar = v;
|
||||||
|
++t;
|
||||||
|
if (!number(a.price)) return std::nullopt;
|
||||||
|
d.pts.push_back(a);
|
||||||
|
}
|
||||||
|
tail_options();
|
||||||
|
if (d.pts.size() < 2) return std::nullopt;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
default: { // two-anchor kinds
|
||||||
|
Anchor a, b;
|
||||||
|
if (!anchor(a) || !anchor(b)) return std::nullopt;
|
||||||
|
d.pts.push_back(a);
|
||||||
|
d.pts.push_back(b);
|
||||||
|
tail_options();
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse a semicolon-separated list of specs; bad entries are skipped.
|
||||||
|
inline std::vector<Drawing> parse_specs(const std::string& specs) {
|
||||||
|
std::vector<Drawing> out;
|
||||||
|
size_t start = 0;
|
||||||
|
for (size_t i = 0; i <= specs.size(); ++i) {
|
||||||
|
if (i == specs.size() || specs[i] == ';') {
|
||||||
|
auto d = parse_spec(specs.substr(start, i - start));
|
||||||
|
if (d) out.push_back(*d);
|
||||||
|
start = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Canonical spec for a drawing (parse_spec(to_spec(d)) round-trips).
|
||||||
|
inline std::string to_spec(const Drawing& d) {
|
||||||
|
static const char* kNames[] = {"blue", "red", "green", "orange",
|
||||||
|
"purple", "teal", "magenta", "brown",
|
||||||
|
"black", "slate"};
|
||||||
|
int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize;
|
||||||
|
char buf[64];
|
||||||
|
std::string s = kind_name(d.kind);
|
||||||
|
auto num = [&](double v) {
|
||||||
|
std::snprintf(buf, sizeof buf, " %g", v);
|
||||||
|
s += buf;
|
||||||
|
};
|
||||||
|
if (d.kind == Kind::HLine) {
|
||||||
|
num(d.pts.empty() ? 0 : d.pts[0].price);
|
||||||
|
} else if (d.kind == Kind::VLine) {
|
||||||
|
num(d.pts.empty() ? 0 : d.pts[0].bar);
|
||||||
|
} else {
|
||||||
|
for (const Anchor& a : d.pts) {
|
||||||
|
num(a.bar);
|
||||||
|
num(a.price);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s += " ";
|
||||||
|
s += kNames[ci];
|
||||||
|
if (d.dash) s += " dash";
|
||||||
|
if (d.kind == Kind::Text) {
|
||||||
|
s += " ";
|
||||||
|
s += d.text;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace fdw
|
||||||
|
)FDDRAWINGS9b2c";
|
||||||
+1740
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,29 @@
|
|||||||
|
// FDesigner code generator.
|
||||||
|
// Turns the designed window (Project + widgets) into a compilable C++/GTK
|
||||||
|
// project. The output is ALL CODE — no .ui file — and every widget is a
|
||||||
|
// STANDALONE CLASS in its own .h/.cpp pair, mirroring the Python
|
||||||
|
// generator's boilerplate: the class creates and configures its widget in
|
||||||
|
// its constructor and carries the same nine mouse-event stub handlers
|
||||||
|
// (buttonPress1-3, dblbuttonPress1-3, buttonRelease1-3) plus a
|
||||||
|
// util_error_print helper. The generated window class just instantiates
|
||||||
|
// one widget class per designed widget. Because construction is ordinary
|
||||||
|
// sequential code in per-widget classes, a stepping debugger can walk it
|
||||||
|
// line by line, and adding a widget by hand means copying one class.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "model.h"
|
||||||
|
|
||||||
|
struct GenResult {
|
||||||
|
bool ok = false;
|
||||||
|
std::string error;
|
||||||
|
std::vector<std::string> files; // paths written, in order
|
||||||
|
};
|
||||||
|
|
||||||
|
// Writes the project into out_dir (created if needed).
|
||||||
|
GenResult generate_project(const Project& project,
|
||||||
|
const std::vector<Widget>& widgets,
|
||||||
|
const std::string& out_dir);
|
||||||
+3885
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+2443
File diff suppressed because it is too large.
Load diff
+65
@@ -0,0 +1,65 @@
|
|||||||
|
// FDesigner model — the designed window as data.
|
||||||
|
// Mirrors the Python original's widget dict: every placed widget has a name,
|
||||||
|
// a type, absolute geometry (Tk .place() style), and a property map.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
struct Widget {
|
||||||
|
std::string name; // e.g. "Button_001"
|
||||||
|
std::string type; // palette type key, e.g. "Button"
|
||||||
|
int x = 0, y = 0;
|
||||||
|
int width = 100, height = 30;
|
||||||
|
std::map<std::string, std::string> props; // GTK properties, as strings
|
||||||
|
std::vector<std::string> signals; // signals to stub in generated code
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Project {
|
||||||
|
std::string title = "Main Window";
|
||||||
|
std::string class_name = "MyWindow";
|
||||||
|
int win_width = 800;
|
||||||
|
int win_height = 600;
|
||||||
|
std::string background; // window background colour, "" = default
|
||||||
|
// Menu bar spec, one menu per line: "File: New | Open | - | Quit"
|
||||||
|
// ("-" is a separator). Empty = no menu bar.
|
||||||
|
std::string menubar;
|
||||||
|
std::vector<Widget> widgets;
|
||||||
|
};
|
||||||
|
|
||||||
|
// One row of the palette: type key, button label in the .ui, default size,
|
||||||
|
// and whether this phase implements it (Toplevel/Spreadsheet arrive later).
|
||||||
|
struct TypeInfo {
|
||||||
|
const char* key;
|
||||||
|
const char* palette_id; // GtkBuilder id of the palette button
|
||||||
|
int def_width;
|
||||||
|
int def_height;
|
||||||
|
bool phase1;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline const TypeInfo kTypes[] = {
|
||||||
|
{"Button", "pal_button", 100, 30, true },
|
||||||
|
{"Label", "pal_label", 100, 24, true },
|
||||||
|
{"Entry", "pal_entry", 160, 28, true },
|
||||||
|
{"Checkbutton", "pal_checkbutton", 120, 26, true },
|
||||||
|
{"Radiobutton", "pal_radiobutton", 120, 26, true },
|
||||||
|
{"Frame", "pal_frame", 200, 120, true },
|
||||||
|
{"Combobox", "pal_combobox", 160, 28, true },
|
||||||
|
{"Listbox", "pal_listbox", 180, 110, true },
|
||||||
|
{"Menubutton", "pal_menubutton", 110, 30, true },
|
||||||
|
{"Canvas", "pal_canvas", 200, 140, true },
|
||||||
|
{"Scale", "pal_scale", 160, 28, true },
|
||||||
|
{"Scrollbar", "pal_scrollbar", 160, 18, true },
|
||||||
|
{"Text", "pal_text", 220, 120, true },
|
||||||
|
{"Toplevel", "pal_toplevel", 300, 200, true },
|
||||||
|
{"Spreadsheet", "pal_spreadsheet", 320, 180, true },
|
||||||
|
{"StockChart", "pal_stockchart", 320, 200, true },
|
||||||
|
};
|
||||||
|
|
||||||
|
inline const TypeInfo* find_type(const std::string& key) {
|
||||||
|
for (const TypeInfo& t : kTypes)
|
||||||
|
if (key == t.key) return &t;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
@@ -0,0 +1,358 @@
|
|||||||
|
// FDesigner project files (.fdj) — see project_io.h.
|
||||||
|
|
||||||
|
#include "project_io.h"
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <fstream>
|
||||||
|
#include <map>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------- writing
|
||||||
|
|
||||||
|
static std::string json_escape(const std::string& s) {
|
||||||
|
std::string out;
|
||||||
|
for (unsigned char c : s) {
|
||||||
|
switch (c) {
|
||||||
|
case '"': out += "\\\""; break;
|
||||||
|
case '\\': out += "\\\\"; break;
|
||||||
|
case '\n': out += "\\n"; break;
|
||||||
|
case '\r': out += "\\r"; break;
|
||||||
|
case '\t': out += "\\t"; break;
|
||||||
|
default:
|
||||||
|
if (c < 0x20) {
|
||||||
|
char buf[8];
|
||||||
|
std::snprintf(buf, sizeof buf, "\\u%04x", c);
|
||||||
|
out += buf;
|
||||||
|
} else {
|
||||||
|
out += (char)c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool save_project_file(const std::string& path, const Project& p,
|
||||||
|
std::string& err) {
|
||||||
|
std::ostringstream o;
|
||||||
|
o << "{\n"
|
||||||
|
<< " \"format\": \"fdesigner\",\n"
|
||||||
|
<< " \"version\": 1,\n"
|
||||||
|
<< " \"window\": {\n"
|
||||||
|
<< " \"title\": \"" << json_escape(p.title) << "\",\n"
|
||||||
|
<< " \"class_name\": \"" << json_escape(p.class_name) << "\",\n"
|
||||||
|
<< " \"width\": " << p.win_width << ",\n"
|
||||||
|
<< " \"height\": " << p.win_height << ",\n"
|
||||||
|
<< " \"background\": \"" << json_escape(p.background) << "\",\n"
|
||||||
|
<< " \"menubar\": \"" << json_escape(p.menubar) << "\"\n"
|
||||||
|
<< " },\n"
|
||||||
|
<< " \"widgets\": [";
|
||||||
|
for (size_t i = 0; i < p.widgets.size(); ++i) {
|
||||||
|
const Widget& w = p.widgets[i];
|
||||||
|
o << (i ? ",\n" : "\n") << " {\n"
|
||||||
|
<< " \"name\": \"" << json_escape(w.name) << "\",\n"
|
||||||
|
<< " \"type\": \"" << json_escape(w.type) << "\",\n"
|
||||||
|
<< " \"x\": " << w.x << ", \"y\": " << w.y
|
||||||
|
<< ", \"width\": " << w.width << ", \"height\": " << w.height
|
||||||
|
<< ",\n"
|
||||||
|
<< " \"props\": {";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& [k, v] : w.props) {
|
||||||
|
o << (first ? "" : ",") << "\n \"" << json_escape(k)
|
||||||
|
<< "\": \"" << json_escape(v) << "\"";
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
o << (w.props.empty() ? "" : "\n ") << "}\n"
|
||||||
|
<< " }";
|
||||||
|
}
|
||||||
|
o << (p.widgets.empty() ? "" : "\n ") << "]\n}\n";
|
||||||
|
|
||||||
|
std::ofstream f(path);
|
||||||
|
if (!f) {
|
||||||
|
err = "cannot open " + path + " for writing";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
f << o.str();
|
||||||
|
if (!f) {
|
||||||
|
err = "write failed for " + path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------- reading
|
||||||
|
|
||||||
|
struct Json {
|
||||||
|
enum Type { Null, Bool, Num, Str, Arr, Obj } type = Null;
|
||||||
|
bool boolean = false;
|
||||||
|
double num = 0;
|
||||||
|
std::string str;
|
||||||
|
std::vector<Json> arr;
|
||||||
|
std::map<std::string, Json> obj;
|
||||||
|
const Json* find(const std::string& key) const {
|
||||||
|
if (type != Obj) return nullptr;
|
||||||
|
auto it = obj.find(key);
|
||||||
|
return it == obj.end() ? nullptr : &it->second;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Parser {
|
||||||
|
const char* p;
|
||||||
|
std::string err;
|
||||||
|
|
||||||
|
void ws() {
|
||||||
|
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') ++p;
|
||||||
|
}
|
||||||
|
bool consume(char c) {
|
||||||
|
ws();
|
||||||
|
if (*p == c) {
|
||||||
|
++p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
static void utf8_encode(std::string& out, unsigned cp) {
|
||||||
|
if (cp < 0x80) {
|
||||||
|
out += (char)cp;
|
||||||
|
} else if (cp < 0x800) {
|
||||||
|
out += (char)(0xC0 | (cp >> 6));
|
||||||
|
out += (char)(0x80 | (cp & 0x3F));
|
||||||
|
} else if (cp < 0x10000) {
|
||||||
|
out += (char)(0xE0 | (cp >> 12));
|
||||||
|
out += (char)(0x80 | ((cp >> 6) & 0x3F));
|
||||||
|
out += (char)(0x80 | (cp & 0x3F));
|
||||||
|
} else {
|
||||||
|
out += (char)(0xF0 | (cp >> 18));
|
||||||
|
out += (char)(0x80 | ((cp >> 12) & 0x3F));
|
||||||
|
out += (char)(0x80 | ((cp >> 6) & 0x3F));
|
||||||
|
out += (char)(0x80 | (cp & 0x3F));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool parse_string(std::string& out) {
|
||||||
|
if (*p != '"') {
|
||||||
|
err = "expected string";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
while (*p && *p != '"') {
|
||||||
|
unsigned char c = (unsigned char)*p;
|
||||||
|
if (c == '\\') {
|
||||||
|
++p;
|
||||||
|
switch (*p) {
|
||||||
|
case 'n': out += '\n'; break;
|
||||||
|
case 't': out += '\t'; break;
|
||||||
|
case 'r': out += '\r'; break;
|
||||||
|
case 'b': out += '\b'; break;
|
||||||
|
case 'f': out += '\f'; break;
|
||||||
|
case '/': out += '/'; break;
|
||||||
|
case '\\': out += '\\'; break;
|
||||||
|
case '"': out += '"'; break;
|
||||||
|
case 'u': {
|
||||||
|
unsigned cp = 0;
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
++p;
|
||||||
|
if (!std::isxdigit((unsigned char)*p)) {
|
||||||
|
err = "bad \\u escape";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
cp = cp * 16 +
|
||||||
|
(unsigned)(std::isdigit((unsigned char)*p)
|
||||||
|
? *p - '0'
|
||||||
|
: (std::tolower(
|
||||||
|
(unsigned char)*p) -
|
||||||
|
'a' + 10));
|
||||||
|
}
|
||||||
|
// Surrogate pair?
|
||||||
|
if (cp >= 0xD800 && cp <= 0xDBFF && p[1] == '\\' &&
|
||||||
|
p[2] == 'u') {
|
||||||
|
unsigned lo = 0;
|
||||||
|
bool okpair = true;
|
||||||
|
const char* save = p;
|
||||||
|
p += 2;
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
++p;
|
||||||
|
if (!std::isxdigit((unsigned char)*p)) {
|
||||||
|
okpair = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lo = lo * 16 +
|
||||||
|
(unsigned)(std::isdigit(
|
||||||
|
(unsigned char)*p)
|
||||||
|
? *p - '0'
|
||||||
|
: (std::tolower(
|
||||||
|
(unsigned char)*p) -
|
||||||
|
'a' + 10));
|
||||||
|
}
|
||||||
|
if (okpair && lo >= 0xDC00 && lo <= 0xDFFF)
|
||||||
|
cp = 0x10000 + ((cp - 0xD800) << 10) +
|
||||||
|
(lo - 0xDC00);
|
||||||
|
else
|
||||||
|
p = save;
|
||||||
|
}
|
||||||
|
utf8_encode(out, cp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = "bad escape";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
} else {
|
||||||
|
out += (char)c;
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (*p != '"') {
|
||||||
|
err = "unterminated string";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool parse_value(Json& v) {
|
||||||
|
ws();
|
||||||
|
if (*p == '{') {
|
||||||
|
++p;
|
||||||
|
v.type = Json::Obj;
|
||||||
|
if (consume('}')) return true;
|
||||||
|
while (true) {
|
||||||
|
ws();
|
||||||
|
std::string key;
|
||||||
|
if (!parse_string(key)) return false;
|
||||||
|
if (!consume(':')) {
|
||||||
|
err = "expected ':'";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Json child;
|
||||||
|
if (!parse_value(child)) return false;
|
||||||
|
v.obj[key] = child;
|
||||||
|
if (consume(',')) continue;
|
||||||
|
if (consume('}')) return true;
|
||||||
|
err = "expected ',' or '}'";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (*p == '[') {
|
||||||
|
++p;
|
||||||
|
v.type = Json::Arr;
|
||||||
|
if (consume(']')) return true;
|
||||||
|
while (true) {
|
||||||
|
Json child;
|
||||||
|
if (!parse_value(child)) return false;
|
||||||
|
v.arr.push_back(child);
|
||||||
|
if (consume(',')) continue;
|
||||||
|
if (consume(']')) return true;
|
||||||
|
err = "expected ',' or ']'";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (*p == '"') {
|
||||||
|
v.type = Json::Str;
|
||||||
|
return parse_string(v.str);
|
||||||
|
}
|
||||||
|
if (std::strncmp(p, "true", 4) == 0) {
|
||||||
|
p += 4;
|
||||||
|
v.type = Json::Bool;
|
||||||
|
v.boolean = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (std::strncmp(p, "false", 5) == 0) {
|
||||||
|
p += 5;
|
||||||
|
v.type = Json::Bool;
|
||||||
|
v.boolean = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (std::strncmp(p, "null", 4) == 0) {
|
||||||
|
p += 4;
|
||||||
|
v.type = Json::Null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
char* end = nullptr;
|
||||||
|
v.num = std::strtod(p, &end);
|
||||||
|
if (end == p) {
|
||||||
|
err = "unexpected character";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p = end;
|
||||||
|
v.type = Json::Num;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::string jstr(const Json* j, const std::string& dflt) {
|
||||||
|
return j && j->type == Json::Str ? j->str : dflt;
|
||||||
|
}
|
||||||
|
static int jint(const Json* j, int dflt) {
|
||||||
|
return j && j->type == Json::Num ? (int)j->num : dflt;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load_project_file(const std::string& path, Project& out,
|
||||||
|
std::string& err) {
|
||||||
|
std::ifstream f(path);
|
||||||
|
if (!f) {
|
||||||
|
err = "cannot open " + path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << f.rdbuf();
|
||||||
|
std::string text = ss.str();
|
||||||
|
|
||||||
|
Parser parser{text.c_str(), ""};
|
||||||
|
Json root;
|
||||||
|
if (!parser.parse_value(root)) {
|
||||||
|
err = "invalid JSON in " + path + ": " + parser.err;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
parser.ws();
|
||||||
|
if (*parser.p) {
|
||||||
|
err = "trailing data after JSON in " + path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (jstr(root.find("format"), "") != "fdesigner") {
|
||||||
|
err = path + " is not an fdesigner project file";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (jint(root.find("version"), 0) != 1) {
|
||||||
|
err = path + " has an unsupported format version";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Project p;
|
||||||
|
if (const Json* win = root.find("window")) {
|
||||||
|
p.title = jstr(win->find("title"), p.title);
|
||||||
|
p.class_name = jstr(win->find("class_name"), p.class_name);
|
||||||
|
p.win_width = jint(win->find("width"), p.win_width);
|
||||||
|
p.win_height = jint(win->find("height"), p.win_height);
|
||||||
|
p.background = jstr(win->find("background"), "");
|
||||||
|
p.menubar = jstr(win->find("menubar"), "");
|
||||||
|
}
|
||||||
|
if (p.win_width < 50 || p.win_width > 20000) p.win_width = 800;
|
||||||
|
if (p.win_height < 50 || p.win_height > 20000) p.win_height = 600;
|
||||||
|
|
||||||
|
if (const Json* widgets = root.find("widgets");
|
||||||
|
widgets && widgets->type == Json::Arr) {
|
||||||
|
for (const Json& jw : widgets->arr) {
|
||||||
|
Widget w;
|
||||||
|
w.name = jstr(jw.find("name"), "");
|
||||||
|
w.type = jstr(jw.find("type"), "");
|
||||||
|
if (w.name.empty() || w.type.empty()) continue;
|
||||||
|
w.x = jint(jw.find("x"), 0);
|
||||||
|
w.y = jint(jw.find("y"), 0);
|
||||||
|
w.width = jint(jw.find("width"), 100);
|
||||||
|
w.height = jint(jw.find("height"), 30);
|
||||||
|
if (w.width < 10) w.width = 10;
|
||||||
|
if (w.height < 10) w.height = 10;
|
||||||
|
if (const Json* props = jw.find("props");
|
||||||
|
props && props->type == Json::Obj) {
|
||||||
|
for (const auto& [k, v] : props->obj)
|
||||||
|
if (v.type == Json::Str) w.props[k] = v.str;
|
||||||
|
}
|
||||||
|
p.widgets.push_back(w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out = p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// FDesigner project files (.fdj) — JSON save/load, replacing the Python
|
||||||
|
// original's pickle files. Hand-rolled JSON (the model is small; no new
|
||||||
|
// dependencies). Format version 1:
|
||||||
|
//
|
||||||
|
// { "format": "fdesigner", "version": 1,
|
||||||
|
// "window": { "title", "class_name", "width", "height", "background" },
|
||||||
|
// "widgets": [ { "name", "type", "x", "y", "width", "height",
|
||||||
|
// "props": { name: value, ... } } ] }
|
||||||
|
//
|
||||||
|
// Loading is tolerant of unknown keys and missing fields, but strict about
|
||||||
|
// the format tag. Property values are NOT validated here — the designer
|
||||||
|
// re-validates every property through set_prop() when it rebuilds the
|
||||||
|
// canvas, so a hand-edited file can't smuggle in bad values.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "model.h"
|
||||||
|
|
||||||
|
bool save_project_file(const std::string& path, const Project& project,
|
||||||
|
std::string& err);
|
||||||
|
bool load_project_file(const std::string& path, Project& project_out,
|
||||||
|
std::string& err);
|
||||||
+153
@@ -0,0 +1,153 @@
|
|||||||
|
// FDesigner property tables — the GTK counterpart of the original's
|
||||||
|
// per-widget parameter mapping (the Entry_001..019 tables in fdesigner7.py).
|
||||||
|
// Each palette type lists the properties the designer can edit; values are
|
||||||
|
// stored in Widget.props as canonical strings and applied live with
|
||||||
|
// g_object_set (or, for the two pseudo-properties background/foreground,
|
||||||
|
// through a per-widget CSS provider — GTK's equivalent of Tk's bg/fg config).
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
enum class PropKind { Str, Bool, Int, Double, Color, Enum };
|
||||||
|
|
||||||
|
struct PropDef {
|
||||||
|
const char* name; // GTK property name, or background/foreground
|
||||||
|
PropKind kind;
|
||||||
|
const char* def; // default value (canonical string), "" = none
|
||||||
|
double lo = 0, hi = 0; // spin range for Int/Double
|
||||||
|
const char* enum_labels = nullptr; // Enum: "Label A|Label B|..."
|
||||||
|
const int* enum_values = nullptr; // Enum: value per label
|
||||||
|
};
|
||||||
|
|
||||||
|
// Enum value tables (GTK 3 constants, written out so this header stays
|
||||||
|
// independent of the GTK headers).
|
||||||
|
inline const int kReliefValues[] = {0, 1, 2}; // NORMAL, HALF, NONE
|
||||||
|
inline const int kShadowValues[] = {0, 1, 2, 3, 4}; // NONE, IN, OUT, ETCHED_IN, ETCHED_OUT
|
||||||
|
inline const int kSelModeValues[] = {0, 1, 2, 3}; // NONE, SINGLE, BROWSE, MULTIPLE
|
||||||
|
inline const int kArrowValues[] = {0, 1, 2, 3, 4}; // UP, DOWN, LEFT, RIGHT, NONE
|
||||||
|
inline const int kWrapValues[] = {0, 1, 2, 3}; // NONE, CHAR, WORD, WORD_CHAR
|
||||||
|
inline const int kScaleModeValues[] = {0, 1}; // PRICE, PERCENT
|
||||||
|
|
||||||
|
// Properties every widget gets (the original configures bg/fg on nearly
|
||||||
|
// every widget type).
|
||||||
|
inline const std::vector<PropDef>& common_props() {
|
||||||
|
static const std::vector<PropDef> v = {
|
||||||
|
{"background", PropKind::Color, ""},
|
||||||
|
{"foreground", PropKind::Color, ""},
|
||||||
|
{"tooltip-text", PropKind::Str, ""},
|
||||||
|
};
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const std::vector<PropDef>& props_for(const std::string& type) {
|
||||||
|
using K = PropKind;
|
||||||
|
static const std::vector<PropDef> none;
|
||||||
|
static const std::vector<PropDef> button = {
|
||||||
|
{"label", K::Str, ""},
|
||||||
|
{"relief", K::Enum, "0", 0, 0, "Normal|Half|None", kReliefValues},
|
||||||
|
{"focus-on-click", K::Bool, "true"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> label = {
|
||||||
|
{"label", K::Str, ""},
|
||||||
|
{"xalign", K::Double, "0.5", 0.0, 1.0},
|
||||||
|
{"selectable", K::Bool, "false"},
|
||||||
|
{"wrap", K::Bool, "false"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> entry = {
|
||||||
|
{"text", K::Str, ""},
|
||||||
|
{"placeholder-text", K::Str, ""},
|
||||||
|
{"max-length", K::Int, "0", 0, 100000},
|
||||||
|
{"width-chars", K::Int, "-1", -1, 1000},
|
||||||
|
{"editable", K::Bool, "true"},
|
||||||
|
{"visibility", K::Bool, "true"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> checkbutton = {
|
||||||
|
{"label", K::Str, ""},
|
||||||
|
{"active", K::Bool, "false"},
|
||||||
|
{"inconsistent", K::Bool, "false"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> radiobutton = {
|
||||||
|
{"label", K::Str, ""},
|
||||||
|
{"active", K::Bool, "false"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> frame = {
|
||||||
|
{"label", K::Str, ""},
|
||||||
|
{"shadow-type", K::Enum, "3", 0, 0, "None|In|Out|Etched in|Etched out",
|
||||||
|
kShadowValues},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> combobox = {
|
||||||
|
{"active", K::Int, "0", -1, 1000},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> listbox = {
|
||||||
|
{"selection-mode", K::Enum, "1", 0, 0, "None|Single|Browse|Multiple",
|
||||||
|
kSelModeValues},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> menubutton = {
|
||||||
|
{"label", K::Str, ""},
|
||||||
|
{"direction", K::Enum, "1", 0, 0, "Up|Down|Left|Right|None",
|
||||||
|
kArrowValues},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> scale = {
|
||||||
|
{"value", K::Double, "0", 0, 100},
|
||||||
|
{"digits", K::Int, "1", -1, 10},
|
||||||
|
{"draw-value", K::Bool, "true"},
|
||||||
|
{"inverted", K::Bool, "false"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> scrollbar = {
|
||||||
|
{"value", K::Double, "0", 0, 100},
|
||||||
|
{"inverted", K::Bool, "false"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> text = {
|
||||||
|
{"editable", K::Bool, "true"},
|
||||||
|
{"cursor-visible", K::Bool, "true"},
|
||||||
|
{"wrap-mode", K::Enum, "0", 0, 0, "None|Char|Word|Word-char",
|
||||||
|
kWrapValues},
|
||||||
|
};
|
||||||
|
if (type == "Button") return button;
|
||||||
|
if (type == "Label") return label;
|
||||||
|
if (type == "Entry") return entry;
|
||||||
|
if (type == "Checkbutton") return checkbutton;
|
||||||
|
if (type == "Radiobutton") return radiobutton;
|
||||||
|
if (type == "Frame") return frame;
|
||||||
|
if (type == "Combobox") return combobox;
|
||||||
|
if (type == "Listbox") return listbox;
|
||||||
|
if (type == "Menubutton") return menubutton;
|
||||||
|
if (type == "Scale") return scale;
|
||||||
|
if (type == "Scrollbar") return scrollbar;
|
||||||
|
if (type == "Text") return text;
|
||||||
|
static const std::vector<PropDef> toplevel = {
|
||||||
|
{"title", K::Str, ""},
|
||||||
|
{"resizable", K::Bool, "true"},
|
||||||
|
};
|
||||||
|
static const std::vector<PropDef> spreadsheet = {
|
||||||
|
{"rows", K::Int, "5", 1, 50},
|
||||||
|
{"columns", K::Int, "4", 1, 26},
|
||||||
|
};
|
||||||
|
if (type == "Toplevel") return toplevel;
|
||||||
|
if (type == "Spreadsheet") return spreadsheet;
|
||||||
|
static const std::vector<PropDef> stockchart = {
|
||||||
|
{"data-file", K::Str, ""},
|
||||||
|
{"up-color", K::Color, "rgb(0,200,0)"},
|
||||||
|
{"down-color", K::Color, "rgb(220,0,0)"},
|
||||||
|
{"max-candles", K::Int, "120", 1, 5000},
|
||||||
|
{"indicators", K::Str, ""},
|
||||||
|
{"scale-mode", K::Enum, "0", 0, 0, "Price|Percent",
|
||||||
|
kScaleModeValues},
|
||||||
|
{"compare-files", K::Str, ""},
|
||||||
|
{"drawings", K::Str, ""},
|
||||||
|
};
|
||||||
|
if (type == "StockChart") return stockchart;
|
||||||
|
return none; // Canvas: common props only
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find a property definition for a widget type (type table, then common).
|
||||||
|
inline const PropDef* find_prop(const std::string& type,
|
||||||
|
const std::string& name) {
|
||||||
|
for (const PropDef& d : props_for(type))
|
||||||
|
if (name == d.name) return &d;
|
||||||
|
for (const PropDef& d : common_props())
|
||||||
|
if (name == d.name) return &d;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
// Renders the generated StockChart class offscreen and verifies that
|
||||||
|
// up-coloured and down-coloured candle pixels actually appear — i.e. the
|
||||||
|
// widget doesn't just compile, it PLOTS.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "StockChart_001.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
gtk_init(&argc, &argv);
|
||||||
|
GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
GtkWidget* fixed = gtk_fixed_new();
|
||||||
|
gtk_container_add(GTK_CONTAINER(win), fixed);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(win), 900, 700);
|
||||||
|
StockChart_001 chart(fixed);
|
||||||
|
if (chart.candle_count() == 0) {
|
||||||
|
std::printf("NO DATA LOADED\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
gtk_widget_show_all(win);
|
||||||
|
for (int i = 0; i < 20; ++i)
|
||||||
|
while (gtk_events_pending()) gtk_main_iteration();
|
||||||
|
GtkWidget* w = chart.widget();
|
||||||
|
int width = gtk_widget_get_allocated_width(w);
|
||||||
|
int height = gtk_widget_get_allocated_height(w);
|
||||||
|
if (width < 50 || height < 50) {
|
||||||
|
std::printf("BAD SIZE %dx%d\n", width, height);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
cairo_surface_t* surf =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
gtk_widget_draw(w, cr);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
unsigned char* px = cairo_image_surface_get_data(surf);
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
long green = 0, red = 0;
|
||||||
|
for (int y = 0; y < height; ++y)
|
||||||
|
for (int x = 0; x < width; ++x) {
|
||||||
|
unsigned char* p = px + y * stride + x * 4; // BGRA
|
||||||
|
int b = p[0], g = p[1], r = p[2];
|
||||||
|
if (g > 150 && r < 110 && b < 110) ++green;
|
||||||
|
if (r > 170 && g < 110 && b < 110) ++red;
|
||||||
|
}
|
||||||
|
std::printf("candles=%d size=%dx%d GREEN %ld RED %ld\n",
|
||||||
|
chart.candle_count(), width, height, green, red);
|
||||||
|
return (green > 200 && red > 200) ? 0 : 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
// Renders the generated StockChart Pro (matrix case 16: percent scale,
|
||||||
|
// QQQ comparison, seven annotations, SMA + RSI) offscreen and verifies
|
||||||
|
// every feature actually draws — then exercises wheel-zoom math via
|
||||||
|
// zoom_at() and re-renders at a smaller size to prove the annotations
|
||||||
|
// survive resizes.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "StockChart_001.h"
|
||||||
|
|
||||||
|
static void pump() {
|
||||||
|
for (int i = 0; i < 25; ++i)
|
||||||
|
while (gtk_events_pending()) gtk_main_iteration();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Counts {
|
||||||
|
long green = 0, red = 0, orange = 0, magenta = 0, purple = 0;
|
||||||
|
long axis_text = 0, time_text = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Counts render(StockChart_001& chart, int W, int H) {
|
||||||
|
GtkWidget* w = chart.widget();
|
||||||
|
gtk_widget_queue_draw(w);
|
||||||
|
pump();
|
||||||
|
int width = gtk_widget_get_allocated_width(w);
|
||||||
|
int height = gtk_widget_get_allocated_height(w);
|
||||||
|
cairo_surface_t* surf =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
gtk_widget_draw(w, cr);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
unsigned char* px = cairo_image_surface_get_data(surf);
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
Counts c;
|
||||||
|
(void)W;
|
||||||
|
(void)H;
|
||||||
|
for (int y = 0; y < height; ++y)
|
||||||
|
for (int x = 0; x < width; ++x) {
|
||||||
|
unsigned char* p = px + y * stride + x * 4; // BGRA
|
||||||
|
int b = p[0], g = p[1], r = p[2];
|
||||||
|
if (g > 150 && r < 110 && b < 110) ++c.green;
|
||||||
|
if (r > 170 && g < 110 && b < 110) ++c.red;
|
||||||
|
if (r > 200 && g > 80 && g < 170 && b < 90) ++c.orange;
|
||||||
|
if (r > 180 && g > 30 && g < 95 && b > 130 && b < 210)
|
||||||
|
++c.magenta;
|
||||||
|
if (r > 105 && r < 175 && g > 25 && g < 85 && b > 165)
|
||||||
|
++c.purple;
|
||||||
|
// Axis labels are anti-aliased 0.35-grey text on white:
|
||||||
|
// count unsaturated mid-grey pixels (gridlines at 0.08
|
||||||
|
// alpha land ~235 and stay excluded).
|
||||||
|
bool grey_text = r > 50 && r < 205 && std::abs(r - g) < 15 &&
|
||||||
|
std::abs(g - b) < 15;
|
||||||
|
// In the right strip nothing but label text draws (grid-
|
||||||
|
// lines stop at the plot edge), so a looser bound is safe.
|
||||||
|
bool axis_grey = r > 40 && r < 225 && std::abs(r - g) < 18 &&
|
||||||
|
std::abs(g - b) < 18;
|
||||||
|
if (axis_grey && x >= width - 56 && y < height - 18)
|
||||||
|
++c.axis_text;
|
||||||
|
if (grey_text && y >= height - 18) ++c.time_text;
|
||||||
|
}
|
||||||
|
cairo_surface_destroy(surf);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
gtk_init(&argc, &argv);
|
||||||
|
int fails = 0;
|
||||||
|
auto check = [&](bool cond, const char* what) {
|
||||||
|
std::printf("%s %s\n", cond ? "OK " : "FAIL", what);
|
||||||
|
if (!cond) ++fails;
|
||||||
|
};
|
||||||
|
GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
GtkWidget* fixed = gtk_fixed_new();
|
||||||
|
gtk_container_add(GTK_CONTAINER(win), fixed);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(win), 1024, 768);
|
||||||
|
StockChart_001 chart(fixed);
|
||||||
|
check(chart.candle_count() == 1170, "1170 candles loaded");
|
||||||
|
check(chart.compare_count() == 1, "QQQ comparison loaded");
|
||||||
|
check(chart.drawing_count() == 7, "7 annotations from design");
|
||||||
|
check(chart.scale_mode() == 1, "percent scale mode");
|
||||||
|
check(chart.visible_count() == 400, "initial window = max-candles");
|
||||||
|
gtk_widget_show_all(win);
|
||||||
|
pump();
|
||||||
|
|
||||||
|
Counts c = render(chart, 1024, 768);
|
||||||
|
std::printf(" green=%ld red=%ld orange=%ld magenta=%ld "
|
||||||
|
"purple=%ld axis=%ld time=%ld\n",
|
||||||
|
c.green, c.red, c.orange, c.magenta, c.purple,
|
||||||
|
c.axis_text, c.time_text);
|
||||||
|
check(c.green > 200 && c.red > 200, "candles plotted");
|
||||||
|
check(c.orange > 100, "QQQ comparison line plotted");
|
||||||
|
check(c.magenta > 25, "arrow annotation plotted");
|
||||||
|
check(c.purple > 25, "fib annotation plotted");
|
||||||
|
check(c.axis_text > 40, "percent axis labels plotted");
|
||||||
|
check(c.time_text > 60, "time axis labels plotted");
|
||||||
|
|
||||||
|
// Wheel-zoom math (what on_scroll calls): in, back out, clamp.
|
||||||
|
chart.zoom_at(0.5, 0.5);
|
||||||
|
check(chart.visible_count() == 200, "zoom in halves the window");
|
||||||
|
chart.zoom_at(2.0, 0.5);
|
||||||
|
check(chart.visible_count() == 400, "zoom out restores the window");
|
||||||
|
chart.zoom_at(10.0, 0.5);
|
||||||
|
check(chart.visible_count() == 1170, "zoom clamps at the full series");
|
||||||
|
chart.reset_view();
|
||||||
|
check(chart.visible_count() == 400, "reset_view restores max-candles");
|
||||||
|
|
||||||
|
// Resize: shrink the chart widget itself (its frame in the fixed
|
||||||
|
// layout); annotations are data-anchored, so the arrow and fib
|
||||||
|
// must still draw at their remapped positions.
|
||||||
|
gtk_widget_set_size_request(gtk_widget_get_parent(chart.widget()),
|
||||||
|
620, 460);
|
||||||
|
gtk_window_resize(GTK_WINDOW(win), 660, 520);
|
||||||
|
pump();
|
||||||
|
check(gtk_widget_get_allocated_width(chart.widget()) < 700,
|
||||||
|
"chart widget actually resized");
|
||||||
|
Counts s = render(chart, 640, 480);
|
||||||
|
std::printf(" small: magenta=%ld purple=%ld orange=%ld\n",
|
||||||
|
s.magenta, s.purple, s.orange);
|
||||||
|
check(s.magenta > 12, "arrow survives resize");
|
||||||
|
check(s.purple > 12, "fib survives resize");
|
||||||
|
check(s.orange > 50, "comparison survives resize");
|
||||||
|
|
||||||
|
std::printf("CHART PRO TEST %s (%d failures)\n",
|
||||||
|
fails ? "FAIL" : "PASS", fails);
|
||||||
|
return fails ? 1 : 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
// Captures the generated case-16 application window (MyWindow with
|
||||||
|
// the StockChart Pro) at exactly 1024x768 to a PNG, for a visual
|
||||||
|
// check of the whole feature set working together.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "MyWindow.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
const char* out = argc > 1 ? argv[1] : "chart_pro_1024x768.png";
|
||||||
|
gtk_init(&argc, &argv);
|
||||||
|
MyWindow app;
|
||||||
|
app.show();
|
||||||
|
GtkWidget* win = app.window();
|
||||||
|
gtk_window_resize(GTK_WINDOW(win), 1024, 768);
|
||||||
|
// Let layout + first draws settle.
|
||||||
|
for (int i = 0; i < 60; ++i) {
|
||||||
|
while (gtk_events_pending()) gtk_main_iteration();
|
||||||
|
g_usleep(10000);
|
||||||
|
}
|
||||||
|
GdkWindow* gw = gtk_widget_get_window(win);
|
||||||
|
int w = gdk_window_get_width(gw);
|
||||||
|
int h = gdk_window_get_height(gw);
|
||||||
|
GdkPixbuf* pb = gdk_pixbuf_get_from_window(gw, 0, 0, w, h);
|
||||||
|
if (!pb) {
|
||||||
|
std::printf("SCREENSHOT FAIL: no pixbuf\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
GError* err = nullptr;
|
||||||
|
if (!gdk_pixbuf_save(pb, out, "png", &err, nullptr)) {
|
||||||
|
std::printf("SCREENSHOT FAIL: %s\n", err ? err->message : "?");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::printf("SCREENSHOT OK %s (%dx%d)\n", out, w, h);
|
||||||
|
return (w == 1024 && h == 768) ? 0 : 1;
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,121 @@
|
|||||||
|
ticker,volume,open,close,high,low,window_start,transactions
|
||||||
|
SPY,17171,660.00,659.49,660.24,659.31,1794493800000000000,1561
|
||||||
|
SPY,31882,659.49,660.23,660.35,659.32,1794493860000000000,1275
|
||||||
|
SPY,2460,660.23,659.71,660.35,659.51,1794493920000000000,102
|
||||||
|
SPY,17173,659.71,659.37,659.77,659.06,1794493980000000000,1561
|
||||||
|
SPY,1878,659.37,659.11,659.75,658.68,1794494040000000000,170
|
||||||
|
SPY,27854,659.11,659.36,659.52,658.72,1794494100000000000,1392
|
||||||
|
SPY,42048,659.36,659.00,659.40,658.66,1794494160000000000,2628
|
||||||
|
SPY,37479,659.00,658.42,659.05,658.12,1794494220000000000,3747
|
||||||
|
SPY,15216,658.42,658.14,658.62,658.02,1794494280000000000,1268
|
||||||
|
SPY,16665,658.14,658.42,658.44,657.88,1794494340000000000,724
|
||||||
|
SPY,10169,658.42,658.37,658.82,658.33,1794494400000000000,635
|
||||||
|
SPY,31564,658.37,658.67,658.93,658.17,1794494460000000000,2630
|
||||||
|
SPY,12123,658.67,658.20,658.98,657.98,1794494520000000000,505
|
||||||
|
SPY,10316,658.20,657.57,658.27,657.32,1794494580000000000,1289
|
||||||
|
SPY,10175,657.57,657.13,657.91,656.92,1794494640000000000,484
|
||||||
|
SPY,35318,657.13,656.29,657.25,655.89,1794494700000000000,3924
|
||||||
|
SPY,28367,656.29,657.04,657.30,655.92,1794494760000000000,1289
|
||||||
|
SPY,30316,657.04,657.20,657.37,656.74,1794494820000000000,3368
|
||||||
|
SPY,10098,657.20,658.09,658.44,657.15,1794494880000000000,1262
|
||||||
|
SPY,42874,658.09,658.94,659.35,657.86,1794494940000000000,2256
|
||||||
|
SPY,10492,658.94,659.24,659.41,658.54,1794495000000000000,1165
|
||||||
|
SPY,35345,659.24,659.39,659.80,659.01,1794495060000000000,2718
|
||||||
|
SPY,25115,659.39,659.97,660.00,659.33,1794495120000000000,1793
|
||||||
|
SPY,22783,659.97,659.91,660.30,659.71,1794495180000000000,1340
|
||||||
|
SPY,19127,659.91,660.09,660.54,659.73,1794495240000000000,796
|
||||||
|
SPY,4644,660.09,659.78,660.18,659.47,1794495300000000000,258
|
||||||
|
SPY,16670,659.78,660.12,660.52,659.64,1794495360000000000,1515
|
||||||
|
SPY,14061,660.12,660.54,660.81,659.91,1794495420000000000,937
|
||||||
|
SPY,4331,660.54,661.48,661.89,660.28,1794495480000000000,188
|
||||||
|
SPY,11557,661.48,661.81,661.89,661.22,1794495540000000000,502
|
||||||
|
SPY,32484,661.81,662.63,662.85,661.65,1794495600000000000,4060
|
||||||
|
SPY,44926,662.63,661.75,662.94,661.40,1794495660000000000,4492
|
||||||
|
SPY,30274,661.75,661.76,662.08,661.50,1794495720000000000,3784
|
||||||
|
SPY,33649,661.76,661.11,662.06,660.93,1794495780000000000,2243
|
||||||
|
SPY,35639,661.11,661.90,662.00,661.04,1794495840000000000,2969
|
||||||
|
SPY,38016,661.90,661.09,662.30,660.98,1794495900000000000,2000
|
||||||
|
SPY,42237,661.09,662.00,662.12,660.89,1794495960000000000,3249
|
||||||
|
SPY,14868,662.00,661.48,662.32,661.38,1794496020000000000,1062
|
||||||
|
SPY,15920,661.48,662.33,662.57,661.31,1794496080000000000,796
|
||||||
|
SPY,6394,662.33,662.69,663.13,661.97,1794496140000000000,710
|
||||||
|
SPY,23912,662.69,661.86,663.07,661.81,1794496200000000000,1494
|
||||||
|
SPY,3717,661.86,660.98,661.92,660.67,1794496260000000000,154
|
||||||
|
SPY,40972,660.98,660.67,661.38,660.49,1794496320000000000,2926
|
||||||
|
SPY,9986,660.67,660.44,660.86,660.31,1794496380000000000,907
|
||||||
|
SPY,39576,660.44,660.91,660.95,660.21,1794496440000000000,2826
|
||||||
|
SPY,22013,660.91,660.02,661.01,659.99,1794496500000000000,1000
|
||||||
|
SPY,42255,660.02,660.22,660.31,660.00,1794496560000000000,3521
|
||||||
|
SPY,42488,660.22,659.69,660.45,659.40,1794496620000000000,1847
|
||||||
|
SPY,26294,659.69,659.54,659.78,659.20,1794496680000000000,2921
|
||||||
|
SPY,24471,659.54,658.85,659.70,658.44,1794496740000000000,2447
|
||||||
|
SPY,26095,658.85,659.61,659.86,658.53,1794496800000000000,1863
|
||||||
|
SPY,30384,659.61,659.45,659.67,659.36,1794496860000000000,3798
|
||||||
|
SPY,21961,659.45,659.31,659.76,659.20,1794496920000000000,1464
|
||||||
|
SPY,21675,659.31,659.60,659.67,658.94,1794496980000000000,2709
|
||||||
|
SPY,42310,659.60,659.60,659.84,659.43,1794497040000000000,2115
|
||||||
|
SPY,32772,659.60,659.97,660.01,659.21,1794497100000000000,1724
|
||||||
|
SPY,2744,659.97,659.15,660.08,659.02,1794497160000000000,182
|
||||||
|
SPY,35643,659.15,658.31,659.34,657.92,1794497220000000000,3960
|
||||||
|
SPY,36255,658.31,658.98,659.18,657.95,1794497280000000000,1812
|
||||||
|
SPY,22461,658.98,658.82,659.20,658.57,1794497340000000000,2041
|
||||||
|
SPY,36668,658.82,659.34,659.37,658.43,1794497400000000000,1666
|
||||||
|
SPY,22626,659.34,659.97,660.35,659.06,1794497460000000000,1740
|
||||||
|
SPY,24273,659.97,660.81,660.99,659.68,1794497520000000000,1213
|
||||||
|
SPY,30522,660.81,660.99,661.40,660.54,1794497580000000000,1526
|
||||||
|
SPY,35566,660.99,660.46,661.25,660.36,1794497640000000000,3951
|
||||||
|
SPY,14789,660.46,660.05,660.77,659.92,1794497700000000000,821
|
||||||
|
SPY,36057,660.05,660.49,660.53,659.79,1794497760000000000,3004
|
||||||
|
SPY,42294,660.49,660.65,660.71,660.16,1794497820000000000,2226
|
||||||
|
SPY,40939,660.65,660.81,660.94,660.53,1794497880000000000,4093
|
||||||
|
SPY,35446,660.81,661.01,661.18,660.55,1794497940000000000,2085
|
||||||
|
SPY,22412,661.01,660.74,661.45,660.42,1794498000000000000,974
|
||||||
|
SPY,15587,660.74,659.93,660.78,659.65,1794498060000000000,677
|
||||||
|
SPY,37522,659.93,659.15,660.01,659.03,1794498120000000000,4169
|
||||||
|
SPY,44124,659.15,659.29,659.32,658.71,1794498180000000000,2595
|
||||||
|
SPY,30318,659.29,659.35,659.44,659.13,1794498240000000000,2165
|
||||||
|
SPY,14773,659.35,659.57,659.65,659.12,1794498300000000000,984
|
||||||
|
SPY,17174,659.57,660.12,660.19,659.23,1794498360000000000,2146
|
||||||
|
SPY,15354,660.12,660.79,661.12,659.80,1794498420000000000,1096
|
||||||
|
SPY,16247,660.79,660.53,661.20,660.42,1794498480000000000,1477
|
||||||
|
SPY,12390,660.53,660.84,660.89,660.46,1794498540000000000,516
|
||||||
|
SPY,41682,660.84,660.93,661.13,660.44,1794498600000000000,2977
|
||||||
|
SPY,19672,660.93,660.14,661.35,659.99,1794498660000000000,894
|
||||||
|
SPY,3291,660.14,659.43,660.51,659.30,1794498720000000000,205
|
||||||
|
SPY,42129,659.43,658.99,659.63,658.66,1794498780000000000,5266
|
||||||
|
SPY,6277,658.99,659.32,659.37,658.79,1794498840000000000,251
|
||||||
|
SPY,20502,659.32,659.61,659.74,659.05,1794498900000000000,976
|
||||||
|
SPY,24747,659.61,658.86,659.88,658.78,1794498960000000000,1455
|
||||||
|
SPY,33696,658.86,658.99,659.21,658.47,1794499020000000000,2246
|
||||||
|
SPY,21472,658.99,658.22,659.07,657.94,1794499080000000000,894
|
||||||
|
SPY,24628,658.22,657.72,658.31,657.40,1794499140000000000,1231
|
||||||
|
SPY,21257,657.72,657.38,658.12,657.03,1794499200000000000,1180
|
||||||
|
SPY,43832,657.38,657.07,657.51,656.73,1794499260000000000,5479
|
||||||
|
SPY,13251,657.07,656.61,657.20,656.44,1794499320000000000,662
|
||||||
|
SPY,22692,656.61,657.09,657.34,656.49,1794499380000000000,1031
|
||||||
|
SPY,19955,657.09,656.62,657.43,656.26,1794499440000000000,1425
|
||||||
|
SPY,20763,656.62,655.86,656.92,655.62,1794499500000000000,1038
|
||||||
|
SPY,22494,655.86,655.01,656.13,654.79,1794499560000000000,1730
|
||||||
|
SPY,6533,655.01,654.72,655.19,654.32,1794499620000000000,435
|
||||||
|
SPY,27082,654.72,655.13,655.39,654.32,1794499680000000000,1593
|
||||||
|
SPY,43522,655.13,655.23,655.58,655.04,1794499740000000000,3108
|
||||||
|
SPY,31582,655.23,655.56,655.61,654.83,1794499800000000000,1503
|
||||||
|
SPY,10061,655.56,656.47,656.65,655.51,1794499860000000000,914
|
||||||
|
SPY,14964,656.47,656.26,656.62,656.06,1794499920000000000,598
|
||||||
|
SPY,16125,656.26,655.51,656.64,655.13,1794499980000000000,645
|
||||||
|
SPY,21701,655.51,654.91,655.72,654.67,1794500040000000000,1085
|
||||||
|
SPY,36817,654.91,655.68,655.92,654.71,1794500100000000000,1753
|
||||||
|
SPY,39741,655.68,655.68,655.84,655.53,1794500160000000000,2207
|
||||||
|
SPY,13451,655.68,656.26,656.47,655.24,1794500220000000000,791
|
||||||
|
SPY,26108,656.26,656.23,656.57,655.86,1794500280000000000,2610
|
||||||
|
SPY,40304,656.23,655.80,656.45,655.71,1794500340000000000,3664
|
||||||
|
SPY,17056,655.80,655.48,655.87,655.27,1794500400000000000,682
|
||||||
|
SPY,2739,655.48,654.67,655.80,654.24,1794500460000000000,210
|
||||||
|
SPY,34501,654.67,654.78,655.03,654.45,1794500520000000000,3450
|
||||||
|
SPY,6849,654.78,655.44,655.49,654.50,1794500580000000000,342
|
||||||
|
SPY,2777,655.44,655.19,655.54,655.13,1794500640000000000,173
|
||||||
|
SPY,35876,655.19,654.85,655.57,654.77,1794500700000000000,2562
|
||||||
|
SPY,17708,654.85,655.57,655.93,654.44,1794500760000000000,1180
|
||||||
|
SPY,27839,655.57,656.02,656.21,655.26,1794500820000000000,2141
|
||||||
|
SPY,14012,656.02,656.12,656.14,655.81,1794500880000000000,1401
|
||||||
|
SPY,15679,656.12,655.88,656.53,655.62,1794500940000000000,1742
|
||||||
|
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,155 @@
|
|||||||
|
// Unit + render test for the drawings.h annotation module: every
|
||||||
|
// kind parses from its spec and draws visible ink in its palette
|
||||||
|
// colour; a trend line re-maps correctly when the plot size changes
|
||||||
|
// (resize safety, since anchors are data coordinates); hit testing
|
||||||
|
// finds lines and misses empty space.
|
||||||
|
#include <cairo.h>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "drawings.h"
|
||||||
|
|
||||||
|
static long count_ink(cairo_surface_t* surf, int ci) {
|
||||||
|
const double* col = fdw::kPalette[ci];
|
||||||
|
int w = cairo_image_surface_get_width(surf);
|
||||||
|
int h = cairo_image_surface_get_height(surf);
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
unsigned char* d = cairo_image_surface_get_data(surf);
|
||||||
|
long n = 0;
|
||||||
|
for (int y = 0; y < h; ++y)
|
||||||
|
for (int x = 0; x < w; ++x) {
|
||||||
|
unsigned char* px = d + y * stride + x * 4; // BGRA
|
||||||
|
double r = px[2] / 255.0, g = px[1] / 255.0, b = px[0] / 255.0;
|
||||||
|
double dr = r - col[0], dg = g - col[1], db = b - col[2];
|
||||||
|
if (dr * dr + dg * dg + db * db < 0.09) ++n;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long count_dark(cairo_surface_t* surf) {
|
||||||
|
int w = cairo_image_surface_get_width(surf);
|
||||||
|
int h = cairo_image_surface_get_height(surf);
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
unsigned char* d = cairo_image_surface_get_data(surf);
|
||||||
|
long n = 0;
|
||||||
|
for (int y = 0; y < h; ++y)
|
||||||
|
for (int x = 0; x < w; ++x) {
|
||||||
|
unsigned char* px = d + y * stride + x * 4;
|
||||||
|
if (px[0] < 90 && px[1] < 90 && px[2] < 90) ++n;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ink of palette colour ci within a small box around (cx, cy).
|
||||||
|
static long ink_near(cairo_surface_t* surf, int ci, double cx, double cy) {
|
||||||
|
const double* col = fdw::kPalette[ci];
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
unsigned char* d = cairo_image_surface_get_data(surf);
|
||||||
|
long n = 0;
|
||||||
|
for (int y = (int)cy - 6; y <= (int)cy + 6; ++y)
|
||||||
|
for (int x = (int)cx - 6; x <= (int)cx + 6; ++x) {
|
||||||
|
if (x < 0 || y < 0) continue;
|
||||||
|
unsigned char* px = d + y * stride + x * 4;
|
||||||
|
double r = px[2] / 255.0, g = px[1] / 255.0, b = px[0] / 255.0;
|
||||||
|
double dr = r - col[0], dg = g - col[1], db = b - col[2];
|
||||||
|
if (dr * dr + dg * dg + db * db < 0.09) ++n;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int fails = 0;
|
||||||
|
auto check = [&](bool cond, const char* what) {
|
||||||
|
std::printf("%s %s\n", cond ? "OK " : "FAIL", what);
|
||||||
|
if (!cond) ++fails;
|
||||||
|
};
|
||||||
|
fdw::Map map;
|
||||||
|
map.x = 0;
|
||||||
|
map.y = 0;
|
||||||
|
map.w = 480;
|
||||||
|
map.h = 300;
|
||||||
|
map.begin = 0;
|
||||||
|
map.count = 100;
|
||||||
|
map.lo = 90;
|
||||||
|
map.hi = 110;
|
||||||
|
|
||||||
|
struct Case {
|
||||||
|
const char* spec;
|
||||||
|
int color; // palette index, or -1 for dark-text check
|
||||||
|
};
|
||||||
|
const Case cases[] = {
|
||||||
|
{"trend 5 95 90 108 red", 1},
|
||||||
|
{"ray 5 95 20 100 orange", 3},
|
||||||
|
{"hline 105 blue", 0},
|
||||||
|
{"vline 50 green", 2},
|
||||||
|
{"rect 10 96 60 106 teal", 5},
|
||||||
|
{"ellipse 10 96 60 106 purple", 4},
|
||||||
|
{"arrow 5 95 90 108 magenta", 6},
|
||||||
|
{"text 10 100 black Hello chart", -1},
|
||||||
|
{"fib 5 95 90 108 brown", 7},
|
||||||
|
{"free 5 95 30 108 60 97 90 104 slate", 9},
|
||||||
|
{"measure 5 95 90 108 red", 1},
|
||||||
|
};
|
||||||
|
for (const Case& c : cases) {
|
||||||
|
auto d = fdw::parse_spec(c.spec);
|
||||||
|
check(d.has_value(), c.spec);
|
||||||
|
if (!d) continue;
|
||||||
|
cairo_surface_t* surf =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 480, 300);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||||
|
cairo_paint(cr);
|
||||||
|
fdw::draw(cr, map, *d);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
long ink = c.color >= 0 ? count_ink(surf, c.color) : count_dark(surf);
|
||||||
|
std::printf(" ink=%ld\n", ink);
|
||||||
|
check(ink > 15, " draws ink");
|
||||||
|
cairo_surface_destroy(surf);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize safety: the same trend line rendered at 480x300 and at
|
||||||
|
// 960x600 must put ink at the scaled endpoint positions.
|
||||||
|
auto trend = fdw::parse_spec("trend 10 95 90 108 red");
|
||||||
|
check(trend.has_value(), "resize trend parses");
|
||||||
|
for (double scale : {1.0, 2.0}) {
|
||||||
|
int W = (int)(480 * scale), H = (int)(300 * scale);
|
||||||
|
fdw::Map m2 = map;
|
||||||
|
m2.w = W;
|
||||||
|
m2.h = H;
|
||||||
|
cairo_surface_t* surf =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, W, H);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||||
|
cairo_paint(cr);
|
||||||
|
fdw::draw(cr, m2, *trend);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
double x1 = m2.px(10), y1 = m2.py(95);
|
||||||
|
double x2 = m2.px(90), y2 = m2.py(108);
|
||||||
|
long a = ink_near(surf, 1, x1, y1);
|
||||||
|
long b = ink_near(surf, 1, x2, y2);
|
||||||
|
std::printf(" scale=%.0f endpoints ink=%ld/%ld\n", scale, a, b);
|
||||||
|
check(a > 2 && b > 2, " endpoints remapped");
|
||||||
|
cairo_surface_destroy(surf);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hit testing: on the line / off the line.
|
||||||
|
{
|
||||||
|
double mx = map.px(47.5), my = map.py(101.5);
|
||||||
|
check(fdw::hit_test(map, *trend, mx, my), "hit on trend midpoint");
|
||||||
|
check(!fdw::hit_test(map, *trend, map.px(47.5), map.py(92)),
|
||||||
|
"miss away from trend");
|
||||||
|
auto hl = fdw::parse_spec("hline 105 blue");
|
||||||
|
check(hl && fdw::hit_test(map, *hl, 200, map.py(105)),
|
||||||
|
"hit on hline");
|
||||||
|
check(hl && !fdw::hit_test(map, *hl, 200, map.py(100)),
|
||||||
|
"miss away from hline");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::printf("DRAWINGS TEST %s (%d failures)\n",
|
||||||
|
fails ? "FAIL" : "PASS", fails);
|
||||||
|
return fails ? 1 : 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,340 @@
|
|||||||
|
// Extensive generator test matrix: builds many designs through the real
|
||||||
|
// generator API and writes each as a project under tests/out/<case>/.
|
||||||
|
// run_matrix.sh then compiles every project (with -Wextra) and runs it.
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../src/generator.h"
|
||||||
|
#include "../src/props.h"
|
||||||
|
|
||||||
|
static int g_case = 0;
|
||||||
|
static std::string g_base;
|
||||||
|
|
||||||
|
static Widget mk(const std::string& type, const std::string& name, int x,
|
||||||
|
int y) {
|
||||||
|
Widget w;
|
||||||
|
w.name = name;
|
||||||
|
w.type = type;
|
||||||
|
w.x = x;
|
||||||
|
w.y = y;
|
||||||
|
const TypeInfo* t = find_type(type);
|
||||||
|
w.width = t ? t->def_width : 100;
|
||||||
|
w.height = t ? t->def_height : 30;
|
||||||
|
for (const PropDef& d : props_for(type))
|
||||||
|
if (d.def && d.def[0]) w.props[d.name] = d.def;
|
||||||
|
for (const PropDef& d : common_props())
|
||||||
|
if (d.def && d.def[0]) w.props[d.name] = d.def;
|
||||||
|
if (find_prop(type, "label")) w.props["label"] = name;
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void emit(const std::string& case_name, Project p) {
|
||||||
|
std::string dir = g_base + "/" + case_name;
|
||||||
|
GenResult r = generate_project(p, p.widgets, dir);
|
||||||
|
std::printf("case %-28s %s (%zu files)\n", case_name.c_str(),
|
||||||
|
r.ok ? "generated" : ("FAILED: " + r.error).c_str(),
|
||||||
|
r.files.size());
|
||||||
|
if (!r.ok) ++g_case; // count failures in exit code via marker
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
g_base = argc > 1 ? argv[1] : "tests/out";
|
||||||
|
|
||||||
|
// 1. Empty design.
|
||||||
|
emit("01_empty", Project());
|
||||||
|
|
||||||
|
// 2. One of every widget type, default properties.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
int i = 0;
|
||||||
|
for (const TypeInfo& t : kTypes) {
|
||||||
|
std::string name = std::string(t.key) + "_001";
|
||||||
|
p.widgets.push_back(mk(t.key, name, 10 + (i % 4) * 190,
|
||||||
|
10 + (i / 4) * 130));
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
emit("02_all_types", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Every property of every type set to a non-default test value.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
int i = 0;
|
||||||
|
for (const TypeInfo& t : kTypes) {
|
||||||
|
Widget w = mk(t.key, std::string(t.key) + "_001",
|
||||||
|
10 + (i % 4) * 190, 10 + (i / 4) * 130);
|
||||||
|
std::vector<const PropDef*> defs;
|
||||||
|
for (const PropDef& d : props_for(t.key)) defs.push_back(&d);
|
||||||
|
for (const PropDef& d : common_props()) defs.push_back(&d);
|
||||||
|
for (const PropDef* d : defs) {
|
||||||
|
switch (d->kind) {
|
||||||
|
case PropKind::Str:
|
||||||
|
w.props[d->name] = "Test value";
|
||||||
|
break;
|
||||||
|
case PropKind::Bool:
|
||||||
|
w.props[d->name] =
|
||||||
|
std::string(d->def) == "true" ? "false" : "true";
|
||||||
|
break;
|
||||||
|
case PropKind::Int:
|
||||||
|
w.props[d->name] = d->def;
|
||||||
|
break;
|
||||||
|
case PropKind::Double: {
|
||||||
|
// 42.5, clamped into the property's valid range
|
||||||
|
// (the designer's set_prop rejects out-of-range).
|
||||||
|
double v = 42.5;
|
||||||
|
if (d->hi > d->lo) {
|
||||||
|
if (v < d->lo) v = d->lo;
|
||||||
|
if (v > d->hi) v = d->hi;
|
||||||
|
}
|
||||||
|
char buf[64];
|
||||||
|
std::snprintf(buf, sizeof buf, "%g", v);
|
||||||
|
w.props[d->name] = buf;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PropKind::Enum: {
|
||||||
|
int last = 0;
|
||||||
|
for (int k = 0; d->enum_values && d->enum_values[k];
|
||||||
|
++k)
|
||||||
|
last = d->enum_values[k];
|
||||||
|
w.props[d->name] = std::to_string(last);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PropKind::Color:
|
||||||
|
w.props[d->name] = "#336699";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (find_prop(t.key, "label")) w.props["label"] = "Test value";
|
||||||
|
w.props["background"] = "#336699";
|
||||||
|
w.props["foreground"] = "white";
|
||||||
|
w.props["tooltip-text"] = "tip with \"quotes\" and \\ slash";
|
||||||
|
p.widgets.push_back(w);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
emit("03_all_props", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Every enum property at every value.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
int x = 10;
|
||||||
|
auto add = [&](const std::string& type, const std::string& name,
|
||||||
|
const std::string& prop, const std::string& val) {
|
||||||
|
Widget w = mk(type, name, x, 10);
|
||||||
|
w.props[prop] = val;
|
||||||
|
p.widgets.push_back(w);
|
||||||
|
x += 130;
|
||||||
|
};
|
||||||
|
for (int v = 0; v <= 2; ++v)
|
||||||
|
add("Button", "Btn_relief_" + std::to_string(v), "relief",
|
||||||
|
std::to_string(v));
|
||||||
|
for (int v = 0; v <= 4; ++v)
|
||||||
|
add("Frame", "Frm_shadow_" + std::to_string(v), "shadow-type",
|
||||||
|
std::to_string(v));
|
||||||
|
for (int v = 0; v <= 3; ++v)
|
||||||
|
add("Listbox", "Lst_sel_" + std::to_string(v), "selection-mode",
|
||||||
|
std::to_string(v));
|
||||||
|
for (int v = 0; v <= 4; ++v)
|
||||||
|
add("Menubutton", "Mnu_dir_" + std::to_string(v), "direction",
|
||||||
|
std::to_string(v));
|
||||||
|
for (int v = 0; v <= 3; ++v)
|
||||||
|
add("Text", "Txt_wrap_" + std::to_string(v), "wrap-mode",
|
||||||
|
std::to_string(v));
|
||||||
|
emit("04_enum_values", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Menus: several menus, separators, spaces and symbols in labels,
|
||||||
|
// plus junk lines that must be ignored.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.menubar =
|
||||||
|
"File: New | Open | Save As | - | Quit\n"
|
||||||
|
"Edit: Cut | Copy & Paste | Select All\n"
|
||||||
|
"this line has no colon and is ignored\n"
|
||||||
|
": empty menu label ignored\n"
|
||||||
|
"Help: About FDesigner\n";
|
||||||
|
p.widgets.push_back(mk("Button", "Button_001", 10, 10));
|
||||||
|
emit("05_menus", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Hostile strings: quotes, backslashes, newline, tab, UTF-8,
|
||||||
|
// format-specifier lookalikes.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.title = "My \"Quoted\" \\ App\nSecond line";
|
||||||
|
p.background = "#102030";
|
||||||
|
Widget b = mk("Button", "Button_001", 10, 10);
|
||||||
|
b.props["label"] = "Click \"me\" \\ 100% %s %d";
|
||||||
|
p.widgets.push_back(b);
|
||||||
|
Widget l = mk("Label", "Label_001", 10, 60);
|
||||||
|
l.props["label"] = "café — ☕\t tabbed";
|
||||||
|
p.widgets.push_back(l);
|
||||||
|
Widget e = mk("Entry", "Entry_001", 10, 110);
|
||||||
|
e.props["text"] = "a\"b\\c\nd";
|
||||||
|
e.props["placeholder-text"] = "type \"here\"";
|
||||||
|
p.widgets.push_back(e);
|
||||||
|
emit("06_hostile_strings", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Class-name sanitization: spaces/punctuation, leading digit,
|
||||||
|
// empty (falls back to MyWindow).
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.class_name = "my cool app!";
|
||||||
|
p.widgets.push_back(mk("Button", "Button_001", 10, 10));
|
||||||
|
emit("07_class_spaces", p);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.class_name = "9lives";
|
||||||
|
p.widgets.push_back(mk("Label", "Label_001", 10, 10));
|
||||||
|
emit("08_class_digit", p);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.class_name = "";
|
||||||
|
p.widgets.push_back(mk("Entry", "Entry_001", 10, 10));
|
||||||
|
emit("09_class_empty", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8. Spreadsheet extremes + several toplevels.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
Widget big = mk("Spreadsheet", "Spreadsheet_001", 10, 10);
|
||||||
|
big.props["rows"] = "50";
|
||||||
|
big.props["columns"] = "26";
|
||||||
|
big.width = 900;
|
||||||
|
big.height = 500;
|
||||||
|
p.widgets.push_back(big);
|
||||||
|
Widget tiny = mk("Spreadsheet", "Spreadsheet_002", 10, 520);
|
||||||
|
tiny.props["rows"] = "1";
|
||||||
|
tiny.props["columns"] = "1";
|
||||||
|
p.widgets.push_back(tiny);
|
||||||
|
for (int i = 1; i <= 3; ++i) {
|
||||||
|
Widget t = mk("Toplevel", "Toplevel_00" + std::to_string(i),
|
||||||
|
100 * i, 100 * i);
|
||||||
|
t.props["title"] = "Window " + std::to_string(i);
|
||||||
|
t.props["resizable"] = i == 2 ? "false" : "true";
|
||||||
|
p.widgets.push_back(t);
|
||||||
|
}
|
||||||
|
emit("10_spreadsheet_toplevels", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9. Forty mixed widgets.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
const char* types[] = {"Button", "Label", "Entry", "Checkbutton",
|
||||||
|
"Frame", "Combobox", "Scale", "Text"};
|
||||||
|
for (int i = 0; i < 40; ++i) {
|
||||||
|
std::string type = types[i % 8];
|
||||||
|
char buf[32];
|
||||||
|
std::snprintf(buf, sizeof buf, "%s_%03d", type.c_str(), i + 1);
|
||||||
|
p.widgets.push_back(
|
||||||
|
mk(type, buf, 10 + (i % 5) * 170, 10 + (i / 5) * 70));
|
||||||
|
}
|
||||||
|
emit("11_forty_widgets", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10. Window variations: no background, huge size, tiny size.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.win_width = 3840;
|
||||||
|
p.win_height = 2160;
|
||||||
|
p.widgets.push_back(mk("Label", "Label_001", 10, 10));
|
||||||
|
emit("12_window_huge", p);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.win_width = 50;
|
||||||
|
p.win_height = 50;
|
||||||
|
p.background = "red";
|
||||||
|
emit("13_window_tiny", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 11. StockChart fed with the fake Massive-format CSV.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
Widget chart = mk("StockChart", "StockChart_001", 10, 10);
|
||||||
|
chart.width = 640;
|
||||||
|
chart.height = 320;
|
||||||
|
std::string csv = std::filesystem::absolute(
|
||||||
|
g_base + "/14_stockchart/SPY_fake.csv").string();
|
||||||
|
chart.props["data-file"] = csv;
|
||||||
|
p.widgets.push_back(chart);
|
||||||
|
p.widgets.push_back(mk("Label", "Label_001", 10, 340));
|
||||||
|
emit("14_stockchart", p);
|
||||||
|
std::ifstream src("tests/data/SPY_fake.csv", std::ios::binary);
|
||||||
|
std::ofstream dst(g_base + "/14_stockchart/SPY_fake.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
dst << src.rdbuf();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 12. StockChart with indicator submodules attached (overlay + pane
|
||||||
|
// + markers), fed with the 3-day series.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
Widget chart = mk("StockChart", "StockChart_001", 10, 10);
|
||||||
|
chart.width = 900;
|
||||||
|
chart.height = 500;
|
||||||
|
std::string csv = std::filesystem::absolute(
|
||||||
|
g_base + "/15_stockchart_indicators/SPY_fake_long.csv")
|
||||||
|
.string();
|
||||||
|
chart.props["data-file"] = csv;
|
||||||
|
chart.props["max-candles"] = "400";
|
||||||
|
chart.props["indicators"] =
|
||||||
|
"SMA(20), EMA(50), Bollinger Bands, RSI, MACD, Engulfing";
|
||||||
|
p.widgets.push_back(chart);
|
||||||
|
emit("15_stockchart_indicators", p);
|
||||||
|
std::ifstream src("tests/data/SPY_fake_long.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
std::ofstream dst(g_base +
|
||||||
|
"/15_stockchart_indicators/SPY_fake_long.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
dst << src.rdbuf();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 13. StockChart Pro: percent scale, QQQ comparison series, and a
|
||||||
|
// full set of annotations, in a 1024x768 window.
|
||||||
|
{
|
||||||
|
Project p;
|
||||||
|
p.win_width = 1024;
|
||||||
|
p.win_height = 768;
|
||||||
|
Widget chart = mk("StockChart", "StockChart_001", 10, 10);
|
||||||
|
chart.width = 1004;
|
||||||
|
chart.height = 748;
|
||||||
|
std::string csv = std::filesystem::absolute(
|
||||||
|
g_base + "/16_stockchart_pro/SPY_fake_long.csv").string();
|
||||||
|
std::string qqq = std::filesystem::absolute(
|
||||||
|
g_base + "/16_stockchart_pro/QQQ_fake_long.csv").string();
|
||||||
|
chart.props["data-file"] = csv;
|
||||||
|
chart.props["max-candles"] = "400";
|
||||||
|
chart.props["indicators"] = "SMA(20), RSI";
|
||||||
|
chart.props["scale-mode"] = "1";
|
||||||
|
chart.props["compare-files"] = qqq;
|
||||||
|
chart.props["drawings"] =
|
||||||
|
"trend 790 676 1150 690 red; hline 688 blue; "
|
||||||
|
"fib 800 674 1000 692 purple; "
|
||||||
|
"text 830 691 orange Resistance zone; "
|
||||||
|
"rect 860 678 960 684 teal; "
|
||||||
|
"arrow 920 690 1090 680 magenta; "
|
||||||
|
"measure 840 677 1010 689 black";
|
||||||
|
p.widgets.push_back(chart);
|
||||||
|
emit("16_stockchart_pro", p);
|
||||||
|
std::ifstream src("tests/data/SPY_fake_long.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
std::ofstream dst(g_base + "/16_stockchart_pro/SPY_fake_long.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
dst << src.rdbuf();
|
||||||
|
std::ifstream src2("tests/data/QQQ_fake_long.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
std::ofstream dst2(g_base + "/16_stockchart_pro/QQQ_fake_long.csv",
|
||||||
|
std::ios::binary);
|
||||||
|
dst2 << src2.rdbuf();
|
||||||
|
}
|
||||||
|
|
||||||
|
return g_case == 0 ? 0 : 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
// Renders the generated StockChart (matrix case 15, with indicator
|
||||||
|
// submodules attached by design) offscreen and verifies the indicators
|
||||||
|
// actually PLOT: the SMA's palette-blue line must appear in the price
|
||||||
|
// pane, and palette-coloured pixels must appear in the oscillator pane
|
||||||
|
// strip at the bottom (RSI/MACD live there).
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "StockChart_001.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
gtk_init(&argc, &argv);
|
||||||
|
GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
GtkWidget* fixed = gtk_fixed_new();
|
||||||
|
gtk_container_add(GTK_CONTAINER(win), fixed);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(win), 1000, 700);
|
||||||
|
StockChart_001 chart(fixed);
|
||||||
|
if (chart.candle_count() == 0) {
|
||||||
|
std::printf("NO DATA LOADED\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
gtk_widget_show_all(win);
|
||||||
|
for (int i = 0; i < 20; ++i)
|
||||||
|
while (gtk_events_pending()) gtk_main_iteration();
|
||||||
|
GtkWidget* w = chart.widget();
|
||||||
|
int width = gtk_widget_get_allocated_width(w);
|
||||||
|
int height = gtk_widget_get_allocated_height(w);
|
||||||
|
if (width < 50 || height < 50) {
|
||||||
|
std::printf("BAD SIZE %dx%d\n", width, height);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
cairo_surface_t* surf =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
gtk_widget_draw(w, cr);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
unsigned char* px = cairo_image_surface_get_data(surf);
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
// Palette blue (SMA and RSI line colour): b high, r low, g mid.
|
||||||
|
long blue_top = 0, blue_bottom = 0;
|
||||||
|
int split = (int)(height * 0.66);
|
||||||
|
for (int y = 0; y < height; ++y)
|
||||||
|
for (int x = 0; x < width; ++x) {
|
||||||
|
unsigned char* p = px + y * stride + x * 4; // BGRA
|
||||||
|
int b = p[0], g = p[1], r = p[2];
|
||||||
|
if (b > 150 && r < 110 && g > 40 && g < 170) {
|
||||||
|
if (y < split)
|
||||||
|
++blue_top;
|
||||||
|
else
|
||||||
|
++blue_bottom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::printf("candles=%d size=%dx%d BLUE price-pane %ld pane %ld\n",
|
||||||
|
chart.candle_count(), width, height, blue_top,
|
||||||
|
blue_bottom);
|
||||||
|
return (blue_top > 100 && blue_bottom > 50) ? 0 : 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,433 @@
|
|||||||
|
// indicator_test.cpp — visual + numeric test of all 250 StockChart
|
||||||
|
// indicator submodules in src/indicators.h.
|
||||||
|
//
|
||||||
|
// For every registered indicator:
|
||||||
|
// 1. compute() over the 3-day fake Massive series must produce finite
|
||||||
|
// values (line/bar indicators) — patterns may legitimately not fire;
|
||||||
|
// 2. the indicator is rendered (with candles) onto its own cairo
|
||||||
|
// surface and we count pixels painted in the indicator's palette
|
||||||
|
// colours — proof it actually draws;
|
||||||
|
// 3. a labelled contact sheet of all 250 renders is written to
|
||||||
|
// tests/out/indicators_contact_sheet.png for eyeballing.
|
||||||
|
// Plus crafted-bar detection checks for famous patterns and range
|
||||||
|
// checks for bounded oscillators.
|
||||||
|
//
|
||||||
|
// Build: g++ -O2 -std=c++17 -I src tests/indicator_test.cpp -o /tmp/itest
|
||||||
|
// $(pkg-config --cflags --libs gtk+-3.0)
|
||||||
|
// Run: /tmp/itest tests/data/SPY_fake_long.csv tests/out
|
||||||
|
|
||||||
|
#include <cairo.h>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "indicators.h"
|
||||||
|
|
||||||
|
using fdi::Bar;
|
||||||
|
|
||||||
|
static std::vector<Bar> load_csv(const std::string& path) {
|
||||||
|
std::ifstream in(path);
|
||||||
|
std::vector<Bar> out;
|
||||||
|
std::string line;
|
||||||
|
if (!std::getline(in, line)) return out; // header
|
||||||
|
while (std::getline(in, line)) {
|
||||||
|
std::stringstream ss(line);
|
||||||
|
std::string f[8];
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
if (!std::getline(ss, f[i], ',')) f[i] = "0";
|
||||||
|
Bar b;
|
||||||
|
b.volume = std::atof(f[1].c_str());
|
||||||
|
b.open = std::atof(f[2].c_str());
|
||||||
|
b.close = std::atof(f[3].c_str());
|
||||||
|
b.high = std::atof(f[4].c_str());
|
||||||
|
b.low = std::atof(f[5].c_str());
|
||||||
|
b.time = std::atoll(f[6].c_str());
|
||||||
|
out.push_back(b);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw light-grey candles for bars [begin, begin+count) into an area.
|
||||||
|
static void draw_candles(cairo_t* cr, const std::vector<Bar>& bars,
|
||||||
|
size_t begin, size_t count, double x, double y,
|
||||||
|
double w, double h, double& lo, double& hi) {
|
||||||
|
lo = 1e300;
|
||||||
|
hi = -1e300;
|
||||||
|
for (size_t i = begin; i < begin + count && i < bars.size(); ++i) {
|
||||||
|
lo = std::min(lo, bars[i].low);
|
||||||
|
hi = std::max(hi, bars[i].high);
|
||||||
|
}
|
||||||
|
if (hi - lo < 1e-9) hi = lo + 1;
|
||||||
|
double slot = w / count;
|
||||||
|
for (size_t k = 0; k < count && begin + k < bars.size(); ++k) {
|
||||||
|
const Bar& b = bars[begin + k];
|
||||||
|
double cx = x + slot * k + slot / 2;
|
||||||
|
auto Y = [&](double v) { return y + (hi - v) / (hi - lo) * h; };
|
||||||
|
cairo_set_source_rgb(cr, 0.78, 0.78, 0.78);
|
||||||
|
cairo_set_line_width(cr, 1.0);
|
||||||
|
cairo_move_to(cr, cx, Y(b.high));
|
||||||
|
cairo_line_to(cr, cx, Y(b.low));
|
||||||
|
cairo_stroke(cr);
|
||||||
|
double y1 = Y(std::max(b.open, b.close));
|
||||||
|
double y2 = Y(std::min(b.open, b.close));
|
||||||
|
if (y2 - y1 < 1) y2 = y1 + 1;
|
||||||
|
cairo_rectangle(cr, cx - std::max(0.5, slot * 0.35), y1,
|
||||||
|
std::max(1.0, slot * 0.7), y2 - y1);
|
||||||
|
cairo_fill(cr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count pixels close to any palette colour (indicator ink).
|
||||||
|
static long count_ink(cairo_surface_t* surf) {
|
||||||
|
int w = cairo_image_surface_get_width(surf);
|
||||||
|
int h = cairo_image_surface_get_height(surf);
|
||||||
|
int stride = cairo_image_surface_get_stride(surf);
|
||||||
|
unsigned char* data = cairo_image_surface_get_data(surf);
|
||||||
|
long ink = 0;
|
||||||
|
for (int yy = 0; yy < h; ++yy) {
|
||||||
|
for (int xx = 0; xx < w; ++xx) {
|
||||||
|
unsigned char* px = data + yy * stride + xx * 4;
|
||||||
|
double r = px[2] / 255.0, g = px[1] / 255.0, b = px[0] / 255.0;
|
||||||
|
for (int c = 0; c < fdi::kPaletteSize; ++c) {
|
||||||
|
double dr = r - fdi::kPalette[c][0];
|
||||||
|
double dg = g - fdi::kPalette[c][1];
|
||||||
|
double db = b - fdi::kPalette[c][2];
|
||||||
|
if (dr * dr + dg * dg + db * db < 0.09) {
|
||||||
|
++ink;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ink;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A pattern-rich synthetic series: overnight gaps, varied bodies and
|
||||||
|
// occasional dojis — minute bars from a continuous feed rarely gap, so
|
||||||
|
// gap patterns (Kicking, Abandoned Baby, stars...) can never fire on
|
||||||
|
// the plain random walk. Patterns are tested on both series.
|
||||||
|
static std::vector<Bar> make_gapped_bars() {
|
||||||
|
std::vector<Bar> out;
|
||||||
|
unsigned long long s = 88172645463325252ULL;
|
||||||
|
auto rnd = [&]() {
|
||||||
|
s ^= s << 13;
|
||||||
|
s ^= s >> 7;
|
||||||
|
s ^= s << 17;
|
||||||
|
return (s >> 11) / 9007199254740992.0;
|
||||||
|
};
|
||||||
|
double price = 100;
|
||||||
|
for (int i = 0; i < 3000; ++i) {
|
||||||
|
Bar b;
|
||||||
|
double gap = (rnd() - 0.5) * 1.6;
|
||||||
|
b.open = price + gap;
|
||||||
|
double body = (rnd() - 0.48) * 3.0;
|
||||||
|
if (i % 37 == 0) body = (rnd() - 0.5) * 0.06; // forced doji
|
||||||
|
if (i % 53 == 0) body = (rnd() > 0.5 ? 1 : -1) * (2.5 + rnd());
|
||||||
|
b.close = b.open + body;
|
||||||
|
b.high = std::max(b.open, b.close) + rnd() * 0.9;
|
||||||
|
b.low = std::min(b.open, b.close) - rnd() * 0.9;
|
||||||
|
b.volume = 1000 + rnd() * 40000;
|
||||||
|
b.time = (long long)i * 60000000000LL;
|
||||||
|
out.push_back(b);
|
||||||
|
price = b.close;
|
||||||
|
if (price < 20) price = 100;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
std::string csv = argc > 1 ? argv[1] : "tests/data/SPY_fake_long.csv";
|
||||||
|
std::string outdir = argc > 2 ? argv[2] : "tests/out";
|
||||||
|
std::vector<Bar> bars = load_csv(csv);
|
||||||
|
if (bars.size() < 200) {
|
||||||
|
std::printf("FAIL: only %zu bars loaded from %s\n", bars.size(),
|
||||||
|
csv.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::printf("loaded %zu bars\n", bars.size());
|
||||||
|
std::vector<Bar> gapped = make_gapped_bars();
|
||||||
|
std::printf("plus %zu gapped pattern-test bars\n", gapped.size());
|
||||||
|
const size_t VISIBLE = 400;
|
||||||
|
size_t begin = bars.size() - VISIBLE;
|
||||||
|
|
||||||
|
int passed = 0, failed = 0, nofire = 0;
|
||||||
|
std::vector<std::string> fail_names, nofire_names;
|
||||||
|
|
||||||
|
// Contact sheet geometry: 5 columns x 50 rows.
|
||||||
|
const int COLS = 5, CELL_W = 380, CELL_H = 112;
|
||||||
|
int rows = (int)((fdi::all_defs().size() + COLS - 1) / COLS);
|
||||||
|
cairo_surface_t* sheet = cairo_image_surface_create(
|
||||||
|
CAIRO_FORMAT_ARGB32, COLS * CELL_W, rows * CELL_H);
|
||||||
|
cairo_t* scr = cairo_create(sheet);
|
||||||
|
cairo_set_source_rgb(scr, 1, 1, 1);
|
||||||
|
cairo_paint(scr);
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
for (const fdi::Def& def : fdi::all_defs()) {
|
||||||
|
auto ind = fdi::make_indicator(def.name);
|
||||||
|
if (!ind) {
|
||||||
|
std::printf("FAIL %-28s cannot construct\n", def.name);
|
||||||
|
++failed;
|
||||||
|
fail_names.push_back(def.name);
|
||||||
|
++idx;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 1. Compute. Patterns also run on the gapped series.
|
||||||
|
fdi::Lines lines = ind->compute(bars);
|
||||||
|
long finite = 0, markers_fired = 0;
|
||||||
|
for (const auto& s : lines)
|
||||||
|
for (double v : s) {
|
||||||
|
if (std::isfinite(v)) ++finite;
|
||||||
|
if (def.kind == fdi::PlotKind::Markers && v != 0 &&
|
||||||
|
std::isfinite(v))
|
||||||
|
++markers_fired;
|
||||||
|
}
|
||||||
|
// Render dataset: patterns render on the gapped series, windowed
|
||||||
|
// to end at the last firing so a triangle is in view.
|
||||||
|
const std::vector<Bar>* rbars = &bars;
|
||||||
|
size_t rbegin = begin;
|
||||||
|
if (def.kind == fdi::PlotKind::Markers) {
|
||||||
|
fdi::Lines gl = ind->compute(gapped);
|
||||||
|
long gfired = 0;
|
||||||
|
size_t last_fire = 0;
|
||||||
|
if (!gl.empty())
|
||||||
|
for (size_t i = 0; i < gl[0].size(); ++i)
|
||||||
|
if (gl[0][i] != 0 && std::isfinite(gl[0][i])) {
|
||||||
|
++gfired;
|
||||||
|
last_fire = i;
|
||||||
|
}
|
||||||
|
markers_fired += gfired;
|
||||||
|
if (gfired > 0) {
|
||||||
|
rbars = &gapped;
|
||||||
|
rbegin = last_fire >= VISIBLE - 1 ? last_fire - VISIBLE + 1
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 2. Render on its own surface and count ink.
|
||||||
|
cairo_surface_t* surf =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 480, 300);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||||
|
cairo_paint(cr);
|
||||||
|
double lo, hi;
|
||||||
|
bool is_pane = def.kind == fdi::PlotKind::Pane;
|
||||||
|
draw_candles(cr, *rbars, rbegin, VISIBLE, 4, 4, 472,
|
||||||
|
is_pane ? 190 : 292, lo, hi);
|
||||||
|
fdi::PlotArea area;
|
||||||
|
area.cr = cr;
|
||||||
|
area.begin = rbegin;
|
||||||
|
area.count = VISIBLE;
|
||||||
|
if (is_pane) {
|
||||||
|
area.x = 4;
|
||||||
|
area.y = 206;
|
||||||
|
area.w = 472;
|
||||||
|
area.h = 90;
|
||||||
|
} else {
|
||||||
|
area.x = 4;
|
||||||
|
area.y = 4;
|
||||||
|
area.w = 472;
|
||||||
|
area.h = 292;
|
||||||
|
area.lo = lo;
|
||||||
|
area.hi = hi;
|
||||||
|
}
|
||||||
|
ind->draw(area, *rbars);
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
long ink = count_ink(surf);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
cairo_surface_destroy(surf);
|
||||||
|
|
||||||
|
bool ok = true;
|
||||||
|
const char* note = "";
|
||||||
|
if (def.kind == fdi::PlotKind::Markers) {
|
||||||
|
if (markers_fired == 0) {
|
||||||
|
ok = false;
|
||||||
|
note = "pattern did not fire on this data";
|
||||||
|
++nofire;
|
||||||
|
nofire_names.push_back(def.name);
|
||||||
|
} else if (ink == 0) {
|
||||||
|
ok = false;
|
||||||
|
note = "fired but drew nothing";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (finite == 0) {
|
||||||
|
ok = false;
|
||||||
|
note = "no finite values";
|
||||||
|
} else if (ink == 0) {
|
||||||
|
ok = false;
|
||||||
|
note = "computed but drew nothing";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ok) {
|
||||||
|
++passed;
|
||||||
|
} else if (def.kind != fdi::PlotKind::Markers ||
|
||||||
|
markers_fired > 0) {
|
||||||
|
++failed;
|
||||||
|
fail_names.push_back(std::string(def.name) + " (" + note + ")");
|
||||||
|
}
|
||||||
|
std::printf("%-4s %-28s finite=%-7ld fired=%-4ld ink=%-6ld %s\n",
|
||||||
|
ok ? "PASS" : (def.kind == fdi::PlotKind::Markers &&
|
||||||
|
markers_fired == 0
|
||||||
|
? "NOFIRE"
|
||||||
|
: "FAIL"),
|
||||||
|
def.name, finite, markers_fired, ink, note);
|
||||||
|
|
||||||
|
// 3. Contact sheet cell.
|
||||||
|
int cx = (int)(idx % COLS) * CELL_W, cy = (int)(idx / COLS) * CELL_H;
|
||||||
|
cairo_set_source_rgb(scr, 1, 1, 1);
|
||||||
|
cairo_rectangle(scr, cx, cy, CELL_W, CELL_H);
|
||||||
|
cairo_fill(scr);
|
||||||
|
cairo_set_source_rgb(scr, 0.6, 0.6, 0.6);
|
||||||
|
cairo_set_line_width(scr, 0.5);
|
||||||
|
cairo_rectangle(scr, cx + 0.5, cy + 0.5, CELL_W - 1, CELL_H - 1);
|
||||||
|
cairo_stroke(scr);
|
||||||
|
cairo_set_source_rgb(scr, 0.15, 0.15, 0.15);
|
||||||
|
cairo_select_font_face(scr, "Sans", CAIRO_FONT_SLANT_NORMAL,
|
||||||
|
CAIRO_FONT_WEIGHT_BOLD);
|
||||||
|
cairo_set_font_size(scr, 10);
|
||||||
|
cairo_move_to(scr, cx + 5, cy + 13);
|
||||||
|
cairo_show_text(scr, def.name);
|
||||||
|
size_t sbegin = bars.size() - 300;
|
||||||
|
const std::vector<Bar>* cbars = &bars;
|
||||||
|
if (def.kind == fdi::PlotKind::Markers && rbars == &gapped) {
|
||||||
|
cbars = &gapped;
|
||||||
|
sbegin = rbegin + 100;
|
||||||
|
}
|
||||||
|
double slo, shi;
|
||||||
|
draw_candles(scr, *cbars, sbegin, 300, cx + 4, cy + 18, CELL_W - 8,
|
||||||
|
is_pane ? 52 : CELL_H - 24, slo, shi);
|
||||||
|
fdi::PlotArea cell;
|
||||||
|
cell.cr = scr;
|
||||||
|
cell.begin = sbegin;
|
||||||
|
cell.count = 300;
|
||||||
|
if (is_pane) {
|
||||||
|
cell.x = cx + 4;
|
||||||
|
cell.y = cy + 74;
|
||||||
|
cell.w = CELL_W - 8;
|
||||||
|
cell.h = CELL_H - 78;
|
||||||
|
} else {
|
||||||
|
cell.x = cx + 4;
|
||||||
|
cell.y = cy + 18;
|
||||||
|
cell.w = CELL_W - 8;
|
||||||
|
cell.h = CELL_H - 24;
|
||||||
|
cell.lo = slo;
|
||||||
|
cell.hi = shi;
|
||||||
|
}
|
||||||
|
ind->draw(cell, *cbars);
|
||||||
|
++idx;
|
||||||
|
}
|
||||||
|
std::string sheet_path = outdir + "/indicators_contact_sheet.png";
|
||||||
|
cairo_surface_write_to_png(sheet, sheet_path.c_str());
|
||||||
|
cairo_destroy(scr);
|
||||||
|
cairo_surface_destroy(sheet);
|
||||||
|
std::printf("contact sheet: %s\n", sheet_path.c_str());
|
||||||
|
|
||||||
|
// 4. Crafted-bar pattern detection checks.
|
||||||
|
auto mk = [](double o, double h, double l, double c) {
|
||||||
|
Bar b;
|
||||||
|
b.open = o;
|
||||||
|
b.high = h;
|
||||||
|
b.low = l;
|
||||||
|
b.close = c;
|
||||||
|
b.volume = 1000;
|
||||||
|
return b;
|
||||||
|
};
|
||||||
|
int crafted_fail = 0;
|
||||||
|
auto expect = [&](const char* name, const std::vector<Bar>& seq,
|
||||||
|
size_t at, double sign) {
|
||||||
|
auto ind = fdi::make_indicator(name);
|
||||||
|
fdi::Lines lines = ind->compute(seq);
|
||||||
|
double got = lines.empty() || at >= lines[0].size() ? 0 : lines[0][at];
|
||||||
|
bool ok = (sign > 0 && got > 0) || (sign < 0 && got < 0);
|
||||||
|
std::printf("crafted %-16s at %zu: got %+.0f %s\n", name, at, got,
|
||||||
|
ok ? "OK" : "MISMATCH");
|
||||||
|
if (!ok) ++crafted_fail;
|
||||||
|
};
|
||||||
|
expect("Doji", {mk(100, 101, 99, 100.05)}, 0, +1);
|
||||||
|
expect("Engulfing",
|
||||||
|
{mk(102, 102.5, 99.5, 100), mk(99.5, 103, 99, 102.5)}, 1, +1);
|
||||||
|
expect("Hammer",
|
||||||
|
{mk(100, 100.5, 96.5, 97), mk(100, 100.5, 96.8, 100.4)}, 1, +1);
|
||||||
|
expect("Morning Star",
|
||||||
|
{mk(110, 110.5, 99.5, 100), mk(98, 98.8, 97.5, 98.5),
|
||||||
|
mk(99, 107.5, 98.5, 107)},
|
||||||
|
2, +1);
|
||||||
|
expect("Shooting Star",
|
||||||
|
{mk(100, 101, 99, 100.8), mk(102, 105, 101.9, 102.15)}, 1,
|
||||||
|
-1);
|
||||||
|
expect("Takuri",
|
||||||
|
{mk(100, 101.3, 99.7, 101), mk(101, 102.3, 100.7, 102),
|
||||||
|
mk(102, 103.3, 101.7, 103), mk(100, 100.4, 97.5, 100.3)},
|
||||||
|
3, +1);
|
||||||
|
expect("Thrusting",
|
||||||
|
{mk(100, 110.4, 99.6, 110), mk(111, 111.3, 106.5, 107)}, 1,
|
||||||
|
-1);
|
||||||
|
expect("Two Crows",
|
||||||
|
{mk(100, 110.4, 99.6, 110), mk(112, 112.3, 107.6, 108),
|
||||||
|
mk(111, 111.3, 104.6, 105)},
|
||||||
|
2, -1);
|
||||||
|
expect("Concealing Baby Swallow",
|
||||||
|
{mk(110, 110.2, 103.8, 104), mk(103, 103.4, 98.6, 99),
|
||||||
|
mk(100.5, 101, 96.6, 97), mk(102, 102.3, 94.7, 95)},
|
||||||
|
3, +1);
|
||||||
|
expect("Kicking By Length",
|
||||||
|
{mk(103, 103.2, 99.8, 100), mk(106, 112.2, 105.8, 112)}, 1,
|
||||||
|
+1);
|
||||||
|
expect("Gap Side Side White",
|
||||||
|
{mk(100, 101.3, 99.7, 101), mk(103, 105.4, 102.5, 105),
|
||||||
|
mk(103.1, 106.3, 102.9, 106)},
|
||||||
|
2, +1);
|
||||||
|
expect("Piercing Line",
|
||||||
|
{mk(110, 110.5, 99.5, 100), mk(99, 106.5, 98.8, 106)}, 1, +1);
|
||||||
|
expect("Dark Cloud Cover",
|
||||||
|
{mk(100, 110.5, 99.5, 110), mk(111, 111.5, 103.5, 104)}, 1,
|
||||||
|
-1);
|
||||||
|
expect("Three White Soldiers",
|
||||||
|
{mk(100, 104.5, 99.5, 104), mk(103, 107.5, 102.5, 107),
|
||||||
|
mk(106, 110.5, 105.5, 110)},
|
||||||
|
2, +1);
|
||||||
|
expect("Abandoned Baby",
|
||||||
|
{mk(110, 110.5, 99.5, 100), mk(97, 97.3, 96.7, 97.05),
|
||||||
|
mk(99, 106.5, 98.5, 106)},
|
||||||
|
2, +1);
|
||||||
|
expect("Kicking",
|
||||||
|
{mk(105, 105.2, 99.8, 100), mk(107, 112.2, 106.8, 112)}, 1,
|
||||||
|
+1);
|
||||||
|
expect("Tristar",
|
||||||
|
{mk(100, 100.6, 99.4, 100), mk(102, 102.6, 101.4, 102),
|
||||||
|
mk(100, 100.6, 99.4, 100)},
|
||||||
|
2, -1);
|
||||||
|
|
||||||
|
// 5. Bounded-oscillator range checks.
|
||||||
|
auto range_check = [&](const char* name, double lo, double hi) {
|
||||||
|
auto ind = fdi::make_indicator(name);
|
||||||
|
fdi::Lines lines = ind->compute(bars);
|
||||||
|
bool ok = true, any = false;
|
||||||
|
for (const auto& s : lines)
|
||||||
|
for (double v : s)
|
||||||
|
if (std::isfinite(v)) {
|
||||||
|
any = true;
|
||||||
|
if (v < lo - 1e-6 || v > hi + 1e-6) ok = false;
|
||||||
|
}
|
||||||
|
std::printf("range %-16s in [%g, %g]: %s\n", name, lo, hi,
|
||||||
|
ok && any ? "OK" : "MISMATCH");
|
||||||
|
if (!ok || !any) ++crafted_fail;
|
||||||
|
};
|
||||||
|
range_check("RSI", 0, 100);
|
||||||
|
range_check("Stochastic Fast", 0, 100);
|
||||||
|
range_check("Williams %R", -100, 0);
|
||||||
|
range_check("MFI", 0, 100);
|
||||||
|
range_check("Stochastic RSI", 0, 100);
|
||||||
|
|
||||||
|
std::printf("\nSUMMARY: %d passed, %d failed, %d patterns not fired, "
|
||||||
|
"%d crafted/range mismatches\n",
|
||||||
|
passed, failed, nofire, crafted_fail);
|
||||||
|
for (auto& f : fail_names) std::printf(" FAIL: %s\n", f.c_str());
|
||||||
|
for (auto& f : nofire_names) std::printf(" NOFIRE: %s\n", f.c_str());
|
||||||
|
return (failed || crafted_fail) ? 1 : 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Generate QQQ_fake_long.csv: a comparison series on the exact same
|
||||||
|
window_start grid as SPY_fake_long.csv (different ticker, price level
|
||||||
|
and walk), for testing the StockChart comparative plot."""
|
||||||
|
import csv
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
|
||||||
|
src_path = sys.argv[1] if len(sys.argv) > 1 else "tests/data/SPY_fake_long.csv"
|
||||||
|
dst_path = sys.argv[2] if len(sys.argv) > 2 else "tests/data/QQQ_fake_long.csv"
|
||||||
|
|
||||||
|
with open(src_path) as f:
|
||||||
|
rows = list(csv.DictReader(f))
|
||||||
|
|
||||||
|
random.seed(424242)
|
||||||
|
price = 512.0
|
||||||
|
out = []
|
||||||
|
for r in rows:
|
||||||
|
o = price
|
||||||
|
c = price + (random.random() - 0.475) * 2.6
|
||||||
|
h = max(o, c) + random.random() * 0.7
|
||||||
|
lo = min(o, c) - random.random() * 0.7
|
||||||
|
vol = int(3000 + random.random() * 30000)
|
||||||
|
out.append(
|
||||||
|
{
|
||||||
|
"ticker": "QQQ",
|
||||||
|
"volume": vol,
|
||||||
|
"open": f"{o:.2f}",
|
||||||
|
"close": f"{c:.2f}",
|
||||||
|
"high": f"{h:.2f}",
|
||||||
|
"low": f"{lo:.2f}",
|
||||||
|
"window_start": r["window_start"],
|
||||||
|
"transactions": int(vol / 7),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
price = c
|
||||||
|
|
||||||
|
with open(dst_path, "w", newline="") as f:
|
||||||
|
w = csv.DictWriter(
|
||||||
|
f,
|
||||||
|
fieldnames=[
|
||||||
|
"ticker",
|
||||||
|
"volume",
|
||||||
|
"open",
|
||||||
|
"close",
|
||||||
|
"high",
|
||||||
|
"low",
|
||||||
|
"window_start",
|
||||||
|
"transactions",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
w.writeheader()
|
||||||
|
w.writerows(out)
|
||||||
|
print(f"wrote {len(out)} QQQ bars to {dst_path}")
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Extensive generator compile test: generate the matrix of designs, then
|
||||||
|
# compile EVERY generated project with -Wall -Wextra and run it briefly
|
||||||
|
# under a virtual display. A generated app that starts and stays up
|
||||||
|
# (killed by timeout, exit 124) counts as a run pass.
|
||||||
|
set -u
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
echo "== building harness =="
|
||||||
|
g++ -O2 -Wall -std=c++17 -o tests/gen_matrix tests/gen_matrix.cpp src/generator.cpp || exit 1
|
||||||
|
|
||||||
|
rm -rf tests/out
|
||||||
|
echo "== generating =="
|
||||||
|
tests/gen_matrix tests/out || exit 1
|
||||||
|
|
||||||
|
pass=0; fail=0; failed=""
|
||||||
|
for d in tests/out/*/; do
|
||||||
|
name=$(basename "$d")
|
||||||
|
if ! (cd "$d" && make CXXFLAGS="-O2 -Wall -Wextra -std=c++17" >build.log 2>&1); then
|
||||||
|
echo "FAIL $name (compile) — see $d/build.log"
|
||||||
|
fail=$((fail+1)); failed="$failed $name(compile)"; continue
|
||||||
|
fi
|
||||||
|
if grep -q "warning" "$d/build.log"; then
|
||||||
|
echo "FAIL $name (warnings) — see $d/build.log"
|
||||||
|
fail=$((fail+1)); failed="$failed $name(warnings)"; continue
|
||||||
|
fi
|
||||||
|
timeout 3 xvfb-run -a "$d/app" >"$d/run.log" 2>&1
|
||||||
|
rc=$?
|
||||||
|
if [ $rc -ne 124 ]; then
|
||||||
|
echo "FAIL $name (run exit $rc) — see $d/run.log"
|
||||||
|
fail=$((fail+1)); failed="$failed $name(run:$rc)"; continue
|
||||||
|
fi
|
||||||
|
# The Adwaita pixbuf-loader warning is VM environment noise (missing
|
||||||
|
# mime database under Xvfb), not a defect in generated code; any other
|
||||||
|
# CRITICAL/WARNING fails the case.
|
||||||
|
bad=$(grep -E "CRITICAL|WARNING \*\*" "$d/run.log" | grep -v "Could not load a pixbuf" || true)
|
||||||
|
if [ -n "$bad" ]; then
|
||||||
|
echo "FAIL $name (runtime warnings) — see $d/run.log"
|
||||||
|
fail=$((fail+1)); failed="$failed $name(runtime)"; continue
|
||||||
|
fi
|
||||||
|
echo "PASS $name"
|
||||||
|
pass=$((pass+1))
|
||||||
|
done
|
||||||
|
echo "== candle render test =="
|
||||||
|
if g++ -O2 -Wall -std=c++17 -o tests/out/14_stockchart/render_test \
|
||||||
|
tests/candle_render_test.cpp tests/out/14_stockchart/StockChart_001.cpp \
|
||||||
|
-Itests/out/14_stockchart $(pkg-config --cflags --libs gtk+-3.0) \
|
||||||
|
2>tests/out/14_stockchart/render_build.log; then
|
||||||
|
if timeout 10 xvfb-run -a tests/out/14_stockchart/render_test; then
|
||||||
|
echo "PASS candle-render"; pass=$((pass+1))
|
||||||
|
else
|
||||||
|
echo "FAIL candle-render"; fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "FAIL candle-render (build) — see tests/out/14_stockchart/render_build.log"
|
||||||
|
fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== indicator render test =="
|
||||||
|
if g++ -O2 -Wall -std=c++17 -o tests/out/15_stockchart_indicators/render_test \
|
||||||
|
tests/indicator_render_test.cpp tests/out/15_stockchart_indicators/StockChart_001.cpp \
|
||||||
|
-Itests/out/15_stockchart_indicators $(pkg-config --cflags --libs gtk+-3.0) \
|
||||||
|
2>tests/out/15_stockchart_indicators/render_build.log; then
|
||||||
|
if timeout 10 xvfb-run -a tests/out/15_stockchart_indicators/render_test; then
|
||||||
|
echo "PASS indicator-render"; pass=$((pass+1))
|
||||||
|
else
|
||||||
|
echo "FAIL indicator-render"; fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "FAIL indicator-render (build) — see tests/out/15_stockchart_indicators/render_build.log"
|
||||||
|
fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== drawings module test =="
|
||||||
|
if g++ -O2 -Wall -Wextra -std=c++17 -o tests/out/drawings_test \
|
||||||
|
tests/drawings_test.cpp -I src \
|
||||||
|
$(pkg-config --cflags --libs cairo) \
|
||||||
|
2>tests/out/drawings_build.log; then
|
||||||
|
if tests/out/drawings_test >tests/out/drawings_test.log 2>&1; then
|
||||||
|
echo "PASS drawings"; pass=$((pass+1))
|
||||||
|
else
|
||||||
|
echo "FAIL drawings — see tests/out/drawings_test.log"
|
||||||
|
fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "FAIL drawings (build) — see tests/out/drawings_build.log"
|
||||||
|
fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== chart pro render test =="
|
||||||
|
if g++ -O2 -Wall -std=c++17 -o tests/out/16_stockchart_pro/render_test \
|
||||||
|
tests/chart_pro_render_test.cpp tests/out/16_stockchart_pro/StockChart_001.cpp \
|
||||||
|
-Itests/out/16_stockchart_pro $(pkg-config --cflags --libs gtk+-3.0) \
|
||||||
|
2>tests/out/16_stockchart_pro/render_build.log; then
|
||||||
|
if timeout 15 xvfb-run -a tests/out/16_stockchart_pro/render_test; then
|
||||||
|
echo "PASS chart-pro-render"; pass=$((pass+1))
|
||||||
|
else
|
||||||
|
echo "FAIL chart-pro-render"; fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "FAIL chart-pro-render (build) — see tests/out/16_stockchart_pro/render_build.log"
|
||||||
|
fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== chart pro screenshot (1024x768) =="
|
||||||
|
if g++ -O2 -Wall -std=c++17 -o tests/out/16_stockchart_pro/screenshot \
|
||||||
|
tests/chart_screenshot.cpp tests/out/16_stockchart_pro/MyWindow.cpp \
|
||||||
|
tests/out/16_stockchart_pro/StockChart_001.cpp \
|
||||||
|
-Itests/out/16_stockchart_pro $(pkg-config --cflags --libs gtk+-3.0) \
|
||||||
|
2>tests/out/16_stockchart_pro/shot_build.log; then
|
||||||
|
if timeout 20 xvfb-run -a tests/out/16_stockchart_pro/screenshot \
|
||||||
|
tests/out/chart_pro_1024x768.png; then
|
||||||
|
echo "PASS chart-pro-screenshot"; pass=$((pass+1))
|
||||||
|
else
|
||||||
|
echo "FAIL chart-pro-screenshot"; fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "FAIL chart-pro-screenshot (build) — see tests/out/16_stockchart_pro/shot_build.log"
|
||||||
|
fail=$((fail+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== matrix: $pass passed, $fail failed =="
|
||||||
|
[ $fail -eq 0 ]
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Wrap src/drawings.h as a C++ raw-string header so the generator can
|
||||||
|
# emit it into generated projects. Usage: embed_drawings.sh IN OUT
|
||||||
|
set -e
|
||||||
|
delim="FDDRAWINGS9b2c"
|
||||||
|
if grep -q "$delim" "$1"; then
|
||||||
|
echo "embed_drawings: delimiter collision in $1" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
{
|
||||||
|
echo "// Generated from src/drawings.h by tools/embed_drawings.sh."
|
||||||
|
echo "// Do not edit; run 'make' to regenerate."
|
||||||
|
echo "#pragma once"
|
||||||
|
printf 'static const char kDrawingsHeader[] = R"%s(\n' "$delim"
|
||||||
|
cat "$1"
|
||||||
|
printf ')%s";\n' "$delim"
|
||||||
|
} > "$2"
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Wrap src/indicators.h as a C++ raw-string header so the generator can
|
||||||
|
# emit it into generated projects. Usage: embed_indicators.sh IN OUT
|
||||||
|
set -e
|
||||||
|
delim="FDINDICATORS7f3a"
|
||||||
|
if grep -q "$delim" "$1"; then
|
||||||
|
echo "embed_indicators: delimiter collision in $1" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
{
|
||||||
|
echo "// Generated from src/indicators.h by tools/embed_indicators.sh."
|
||||||
|
echo "// Do not edit; run 'make' to regenerate."
|
||||||
|
echo "#pragma once"
|
||||||
|
printf 'static const char kIndicatorsHeader[] = R"%s(\n' "$delim"
|
||||||
|
cat "$1"
|
||||||
|
printf ')%s";\n' "$delim"
|
||||||
|
} > "$2"
|
||||||
+282
@@ -0,0 +1,282 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!-- FDesigner main window. Authored for Glade / GtkBuilder, GTK 3.24. -->
|
||||||
|
<interface>
|
||||||
|
<requires lib="gtk+" version="3.24"/>
|
||||||
|
<object class="GtkWindow" id="main_window">
|
||||||
|
<property name="title">FDesigner — C++/GTK</property>
|
||||||
|
<property name="default_width">1150</property>
|
||||||
|
<property name="default_height">720</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="root_box">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuBar" id="menubar">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuItem" id="menu_file">
|
||||||
|
<property name="label">File</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child type="submenu">
|
||||||
|
<object class="GtkMenu" id="file_menu">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuItem" id="mi_new">
|
||||||
|
<property name="label">New</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuItem" id="mi_open">
|
||||||
|
<property name="label">Open…</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuItem" id="mi_save">
|
||||||
|
<property name="label">Save…</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkSeparatorMenuItem" id="mi_sep1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuItem" id="mi_generate">
|
||||||
|
<property name="label">Generate C++/GTK…</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkSeparatorMenuItem" id="mi_sep2">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkMenuItem" id="mi_quit">
|
||||||
|
<property name="label">Quit</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkPaned" id="main_paned">
|
||||||
|
<property name="orientation">horizontal</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkScrolledWindow" id="palette_scroll">
|
||||||
|
<property name="width_request">170</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="palette_box">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="spacing">4</property>
|
||||||
|
<property name="margin_start">6</property>
|
||||||
|
<property name="margin_end">6</property>
|
||||||
|
<property name="margin_top">6</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="palette_label">
|
||||||
|
<property name="label"><b>Widgets</b></property>
|
||||||
|
<property name="use_markup">True</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_button">
|
||||||
|
<property name="label">Button</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_label">
|
||||||
|
<property name="label">Label</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_entry">
|
||||||
|
<property name="label">Entry</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_checkbutton">
|
||||||
|
<property name="label">CheckButton</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_radiobutton">
|
||||||
|
<property name="label">RadioButton</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_frame">
|
||||||
|
<property name="label">Frame</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_combobox">
|
||||||
|
<property name="label">ComboBox</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_listbox">
|
||||||
|
<property name="label">ListBox</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_menubutton">
|
||||||
|
<property name="label">MenuButton</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_canvas">
|
||||||
|
<property name="label">Canvas</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_scale">
|
||||||
|
<property name="label">Scale</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_scrollbar">
|
||||||
|
<property name="label">Scrollbar</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_text">
|
||||||
|
<property name="label">Text</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_toplevel">
|
||||||
|
<property name="label">Toplevel</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_spreadsheet">
|
||||||
|
<property name="label">Spreadsheet</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="pal_stockchart">
|
||||||
|
<property name="label">StockChart</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="resize">False</property>
|
||||||
|
<property name="shrink">False</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkPaned" id="canvas_props_paned">
|
||||||
|
<property name="orientation">horizontal</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkScrolledWindow" id="canvas_scroll">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkFixed" id="canvas">
|
||||||
|
<property name="width_request">800</property>
|
||||||
|
<property name="height_request">600</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="resize">True</property>
|
||||||
|
<property name="shrink">True</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="props_box">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="width_request">280</property>
|
||||||
|
<property name="spacing">4</property>
|
||||||
|
<property name="margin_start">6</property>
|
||||||
|
<property name="margin_end">6</property>
|
||||||
|
<property name="margin_top">6</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="props_label">
|
||||||
|
<property name="label"><b>Properties</b></property>
|
||||||
|
<property name="use_markup">True</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="selected_label">
|
||||||
|
<property name="label">No widget selected</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkGrid" id="props_grid">
|
||||||
|
<property name="row_spacing">4</property>
|
||||||
|
<property name="column_spacing">6</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="resize">False</property>
|
||||||
|
<property name="shrink">False</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="resize">True</property>
|
||||||
|
<property name="shrink">True</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkStatusbar" id="statusbar">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</interface>
|
||||||
Reference in new issue
Block a user