Skip to content

Commit

Permalink
Implemented widget overrides by priority on the graph widget
Browse files Browse the repository at this point in the history
  • Loading branch information
sadko4u committed Dec 17, 2023
1 parent 34c8f8a commit 3da1048
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Many code updates that could cause potential problems related to using another
decimal point for floating-point number formatting.
* FileDialog now synchronizes the state of selected_filter property.
* Implemented widget overrides by priority on the graph widget.
* Updated build scripts.

=== 1.0.18 ===
Expand Down
51 changes: 32 additions & 19 deletions include/lsp-plug.in/tk/widgets/graph/Graph.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <[email protected]>
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2023 Vladimir Sadovnikov <[email protected]>
*
* This file is part of lsp-tk-lib
* Created on: 20 авг. 2020 г.
Expand Down Expand Up @@ -58,9 +58,14 @@ namespace lsp
public:
static const w_class_t metadata;

private:
Graph & operator = (const Graph &);
Graph(const Graph &);
protected:
typedef struct w_alloc_t
{
ws::rectangle_t sRect;
ssize_t nGroup;
ssize_t nPriority;
GraphItem *pWidget;
} w_alloc_t;

protected:
prop::WidgetList<GraphItem> vItems; // Overall list of graph items
Expand Down Expand Up @@ -89,23 +94,31 @@ namespace lsp
static void on_add_item(void *obj, Property *prop, void *w);
static void on_remove_item(void *obj, Property *prop, void *w);

static ssize_t check_collision(const w_alloc_t *a, const w_alloc_t *b);
static ssize_t compare_walloc(const w_alloc_t *a, const w_alloc_t *b);

protected:
virtual Widget *find_widget(ssize_t x, ssize_t y);
virtual void size_request(ws::size_limit_t *r);
virtual void property_changed(Property *prop);
virtual void realize(const ws::rectangle_t *r);
virtual void hide_widget();
virtual Widget *find_widget(ssize_t x, ssize_t y) override;
virtual void size_request(ws::size_limit_t *r) override;
virtual void property_changed(Property *prop) override;
virtual void realize(const ws::rectangle_t *r) override;
virtual void hide_widget() override;

void sync_lists();
void drop_glass();

public:
explicit Graph(Display *dpy);
Graph(const Graph &) = delete;
Graph(Graph &&) = delete;
virtual ~Graph();

Graph & operator = (const Graph &) = delete;
Graph & operator = (Graph &&) = delete;

public:
virtual status_t init();
virtual void destroy();
virtual status_t init() override;
virtual void destroy() override;

public:
LSP_TK_PROPERTY(WidgetList<GraphItem>, items, &vItems);
Expand Down Expand Up @@ -181,19 +194,19 @@ namespace lsp
status_t axis_to_xy(size_t index, ssize_t *x, ssize_t *y, float value);

public:
virtual status_t add(Widget *child);
virtual status_t add(Widget *child) override;

virtual status_t remove(Widget *child);
virtual status_t remove(Widget *child) override;

virtual status_t remove_all();
virtual status_t remove_all() override;

virtual void render(ws::ISurface *s, const ws::rectangle_t *area, bool force);
virtual void render(ws::ISurface *s, const ws::rectangle_t *area, bool force) override;

virtual void draw(ws::ISurface *s);
virtual void draw(ws::ISurface *s) override;
};

}
}
} /* namespace tk */
} /* namespace lsp */


#endif /* LSP_PLUG_IN_TK_WIDGETS_GRAPH_GRAPH_H_ */
44 changes: 30 additions & 14 deletions include/lsp-plug.in/tk/widgets/graph/GraphItem.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <[email protected]>
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2023 Vladimir Sadovnikov <[email protected]>
*
* This file is part of lsp-tk-lib
* Created on: 20 авг. 2020 г.
Expand Down Expand Up @@ -37,6 +37,8 @@ namespace lsp
{
LSP_TK_STYLE_DEF_BEGIN(GraphItem, Widget)
prop::Boolean sSmooth;
prop::Integer sPriorityGroup; // Priority group
prop::Integer sPriority; // Priority inside of a group
LSP_TK_STYLE_DEF_END
}

Expand All @@ -45,26 +47,31 @@ namespace lsp
public:
static const w_class_t metadata;

private:
GraphItem & operator = (const GraphItem &);
GraphItem(const GraphItem &);

protected:
prop::Boolean sSmooth;
prop::Integer sPriorityGroup; // Priority group
prop::Integer sPriority; // Priority inside of a group

protected:
virtual void property_changed(Property *prop);
virtual void property_changed(Property *prop) override;

public:
explicit GraphItem(Display *dpy);
virtual ~GraphItem();
GraphItem(const GraphItem &) = delete;
GraphItem(GraphItem &&) = delete;
virtual ~GraphItem() override;

GraphItem & operator = (const GraphItem &) = delete;
GraphItem & operator = (GraphItem &&) = delete;

public:
virtual status_t init();
virtual void destroy();
virtual status_t init() override;
virtual void destroy() override;

public:
LSP_TK_PROPERTY(Boolean, smooth, &sSmooth);
LSP_TK_PROPERTY(Integer, priority_group, &sPriorityGroup);
LSP_TK_PROPERTY(Integer, priority, &sPriority);

public:
/**
Expand All @@ -73,18 +80,27 @@ namespace lsp
*/
Graph *graph();

virtual void query_draw(size_t flags = REDRAW_SURFACE);
virtual void query_draw(size_t flags = REDRAW_SURFACE) override;

/**
* Check whether mouse pointer is inside of the graph item
* @param x horizontal position of mouse pointer
* @param y vertical position of mouse pointer
* @return true if item is inside of the graph
*/
virtual bool inside(ssize_t x, ssize_t y);
virtual bool inside(ssize_t x, ssize_t y) override;

public:
/**
* Estimate the bounding box for prioritized drawing
* @param s surface for drawing
* @param r rectangle to store the bound box
* @return false if widget does not provide bound box
*/
virtual bool bound_box(ws::ISurface *s, ws::rectangle_t *r);
};

}
}
} /* namespace tk */
} /* namespace lsp */

#endif /* LSP_PLUG_IN_TK_WIDGETS_GRAPH_GRAPHITEM_H_ */
27 changes: 15 additions & 12 deletions include/lsp-plug.in/tk/widgets/graph/GraphText.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <[email protected]>
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2023 Vladimir Sadovnikov <[email protected]>
*
* This file is part of lsp-tk-lib
* Created on: 20 авг. 2020 г.
Expand Down Expand Up @@ -53,10 +53,6 @@ namespace lsp
public:
static const w_class_t metadata;

private:
GraphText & operator = (const GraphText &);
GraphText(const GraphText &);

private:
prop::String sText;
prop::Font sFont;
Expand All @@ -71,13 +67,18 @@ namespace lsp
prop::Integer sOrigin;

protected:
virtual void property_changed(Property *prop);
virtual void property_changed(Property *prop) override;

public:
explicit GraphText(Display *dpy);
virtual ~GraphText();
GraphText(const GraphText &) = delete;
GraphText(GraphText &&) = delete;
virtual ~GraphText() override;

virtual status_t init();
GraphText & operator = (const GraphText &) = delete;
GraphText & operator = (GraphText &&) = delete;

virtual status_t init() override;

public:
LSP_TK_PROPERTY(String, text, &sText)
Expand All @@ -93,10 +94,12 @@ namespace lsp
LSP_TK_PROPERTY(Integer, origin, &sOrigin)

public:
virtual void render(ws::ISurface *s, const ws::rectangle_t *area, bool force);
virtual void render(ws::ISurface *s, const ws::rectangle_t *area, bool force) override;

virtual bool bound_box(ws::ISurface *s, ws::rectangle_t *r) override;
};
}
}
} /* namespace tk */
} /* namespace lsp */


#endif /* LSP_PLUG_IN_TK_WIDGETS_GRAPH_GRAPHTEXT_H_ */
2 changes: 1 addition & 1 deletion src/main/prop/simple/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ namespace lsp
if (!(nFlags & F_LOCALIZED))
{
sCache.truncate();
return (out->set(&sText)) ? STATUS_OK : STATUS_NO_MEM;
return expr::format(out, &sText, &sParams);
}

// Check that value has been cached
Expand Down
74 changes: 72 additions & 2 deletions src/main/widgets/graph/Graph.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <[email protected]>
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2023 Vladimir Sadovnikov <[email protected]>
*
* This file is part of lsp-tk-lib
* Created on: 20 авг. 2020 г.
Expand All @@ -23,6 +23,7 @@
#include <lsp-plug.in/tk/helpers/draw.h>
#include <lsp-plug.in/stdlib/math.h>
#include <lsp-plug.in/common/debug.h>
#include <lsp-plug.in/lltl/ptrset.h>
#include <private/tk/style/BuiltinStyle.h>

namespace lsp
Expand Down Expand Up @@ -315,6 +316,24 @@ namespace lsp
s->clip_end();
}

ssize_t Graph::check_collision(const w_alloc_t *a, const w_alloc_t *b)
{
if ((a->nGroup != b->nGroup) || (a->nPriority == b->nPriority))
return 0;
if (!Size::overlap(&a->sRect, &b->sRect))
return 0;

return b->nPriority - a->nPriority;
}

ssize_t Graph::compare_walloc(const w_alloc_t *a, const w_alloc_t *b)
{
ssize_t diff = a->nGroup - b->nGroup;
if (diff == 0)
diff = a->nPriority - b->nPriority;
return diff;
}

void Graph::draw(ws::ISurface *s)
{
// Clear canvas
Expand All @@ -326,12 +345,63 @@ namespace lsp
// Sync internal lists of axes and origins
sync_lists();

// Compute list of discarded widgets
lltl::ptrset<GraphItem> discarded;
{
lltl::darray<w_alloc_t> grouped;

// Fill all grouped widgets
for (size_t i=0, n=vItems.size(); i<n; ++i)
{
GraphItem *gi = vItems.get(i);
if ((gi == NULL) || (!gi->visibility()->get()))
continue;

// Fill the widget allocation
w_alloc_t wa;
wa.nGroup = gi->priority_group()->get();
if (wa.nGroup < 0)
continue;
if (!gi->bound_box(s, &wa.sRect))
continue;

wa.nPriority = gi->priority()->get();
wa.pWidget = gi;

grouped.add(&wa);
}
grouped.qsort(compare_walloc);

// Scan for conflicting widgets and discard some widgets according to priority
for (size_t i=0, n=grouped.size(); i<n; ++i)
{
w_alloc_t *wa = grouped.uget(i);
if (wa == NULL)
continue;

for (size_t j=0; j<i; ++j)
{
w_alloc_t *wb = grouped.uget(j);
if (wb == NULL)
continue;

if (check_collision(wb, wa) > 0)
{
discarded.put(wa->pWidget);
break;
}
}
}
}

// Draw all objects
for (size_t i=0, n=vItems.size(); i<n; ++i)
{
GraphItem *gi = vItems.get(i);
if ((gi == NULL) || (!gi->visibility()->get()))
continue;
if (discarded.contains(gi))
continue;

gi->render(s, &sICanvas, true);
gi->commit_redraw();
Expand Down
Loading

0 comments on commit 3da1048

Please sign in to comment.