• Antelope Release 5.9 Mac OS X 10.13.2 2019-05-01

 

NAME

BQGlyphsGraphicsContext - BRTT Qt graphics extension glyphs graphics context class

SYNOPSIS


$(QTNATIVELIBS) -lbqplot_native -lbanner -lbrttutil -lbumapdata $(DBLIBS) $(TRLIBS)

#include "BQ.h"

DESCRIPTION

The BQGlyphsGraphicsContext class is sub-classed from the BQGraphicsContext class and and the BQConfigure class and adds functionality for parsing graphics context configurations and parameters from parameter file strings using BQConfigure. This class also provides methods for interfacing graphics context objects with BQGlyph and BQGlyphs graphics objects.

The graphics context configurations and parameters are determined from several configure options described below. The gcitem_definitions configuration option is a list of individual named graphics context items in a string format described below. The gc_definitions configuration option is a list of named tables with each name corresponsing to one or more graphics contexts consisting of a set of the named graphics context items. These graphics context definitions are then used to display the various graphics objects.

INHERITS FROM

BQGraphicsContext, BQConfigure

CONSTRUCTOR

BQGlyphsGraphicsContext();

METHODS INHERITED FROM BQConfigure

BQGlyphsGraphicsContext METHODS

OBJECT CONFIGURATION PARAMETERS

EXAMPLE

Following is example code that illustrates the use of BQGlyphs, BQGlyph and GlyphsGraphicsContext objects for rapid animated display of a set of display glyphs. This is a variation on the example in BQGlyphs(3).

% cat Makefile
BIN=        bqplot_test_glyphsgraphicscontext

fflags=
ldflags=

cflags = -g
cxxflags = -g -I$(QTINCLUDE)
ldlibs    = -lbqplot_native $(QTNATIVELIBS) -lbanner -lbrttutil $(DBLIBS)

include $(ANTELOPEMAKE)

OBJS= bqplot_test_glyphsgraphicscontext.o
$(BIN) : $(OBJS)
    $(CXX) $(CXXFLAGS) -g -o $(BIN) $(OBJS) $(LDFLAGS) $(LDLIBS)
%
% cat bqplot_test_glyphsgraphicscontext.cc

#include "BQ.h"

#define GCITEM_DEFINITIONS_STR "\
glyphs_gcitem_definitions &Arr{\n\
    outline_color:symbol        constant    \\#80000000\n\
    outline_linewidth:symbol    constant    0.5\n\
    fill_color:symbol           hue         0.0,0.0:240.0,240.0;1.0;0.6\n\
    size:symbol                 size        10.0,10.0:20.0,20.0:10.0:50.0\n\
    opacity:symbol              constant    0.8\n\
}\n"

#define GC_DEFINITIONS_STR "\
glyphs_gc_definitions &Arr{\n\
    glyphs &Tbl{\n\
        size:symbol\n\
        fill_color:symbol\n\
        outline_linewidth:symbol\n\
        outline_color:symbol\n\
        opacity:symbol\n\
    }\n\
}\n"

int
main (int argc, char **argv)

{
    QApplication qapp(argc, argv);

    qapp.setApplicationName("bqplot_test_graphicscontext");

    QWidget *widget = new QWidget();
    widget->setWindowTitle("bqplot_test_graphicscontext");
    widget->show();
    widget->resize (800, 800);

    BQViewport *myvp = new BQViewport(widget);

    myvp->configure (
            "xleft",      "-1.0",
            "xright",     "5.0",
            "ybottom",    "-1.1",
            "ytop",       "1.1",
            "mleft",      "80",
            "mright",     "20",
            "mbottom",    "50",
            "mtop",       "30",
            "fill_frame", "lightblue",
            "fill",       "#e0e0e0",
            NULL);

    BQAxes *myax = new BQAxes(myvp);
    myax->configure (
             "xlabel",                "My X-stuff",
             "ylabel",                "My Y-stuff",
             "xformat",               "%.1f",
             "yformat",               "%.1f",
             "linewidth_box",         "0",
             "linewidth_tics",        "2",
             "linewidth_tics_small",  "0",
             "axis_style",            "sw",
             "tic_style",             "siwinoeo",
             "linewidth_grids",       "2",
             "linewidth_grids_small", "0",
             "color_grids",           "black",
             "color_grids_small",     "#c0c0c0",
             NULL);

    BQ_XY points[] = {
        {1.2, 0.4},
        {-0.5, -0.7},
        {3.7, 0.3},
        {2.1, 0.9},
        {4.1, -0.5}
    };

    int n = sizeof(points) / sizeof(BQ_XY);

    int symbols[] = {
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_CIRCLE,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_SQUARE,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_TRIANGLE,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_DIAMOND,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_STAR,
    };

    BQGlyphsGraphicsContext *ggc = new BQGlyphsGraphicsContext ();
    ggc->configure (
            "gcitem_definitions",    GCITEM_DEFINITIONS_STR,
            "gc_definitions",        GC_DEFINITIONS_STR,
             NULL);

    int gcindex = ggc->getGcIndex ( "glyphs" );
    int hue_index = ggc->getCodingIndex (gcindex, "hue" );
    int size_index = ggc->getCodingIndex (gcindex, "size" );

    BQGlyphs *glyphs = new BQGlyphs(myvp, ggc);
    glyphs->setInteractionMode(BQ_INTERACTION_MODE_MOVE);

    QList<BQGlyph *> glyph_list;
    for (int i=0; i<n; i++) {
        BQGlyph *glyph = new BQGlyph(myvp, glyphs);
        glyph->setPosition (points[i].x, points[i].y);
        glyph->setGlyph (symbols[i], "glyphs", NULL);
        ggc->setCoding (gcindex, hue_index, i*(360.0/n), glyph);
        ggc->setCoding (gcindex, size_index, 10.0+i*(40.0/n), glyph);
        glyph_list.append (glyph);
    }

    double t0 = now();
    QTimer *timer = new QTimer (widget);
    widget->connect (timer, &QTimer::timeout, [=]() {
            foreach (BQGlyph *glyph, glyph_list) {
                double hue_increment = 5.0;
                double size_increment = 0.5 - sin((now() - t0)*0.3);
                double hue;
                ggc->getCoding (gcindex, hue_index, &hue, glyph);
                hue += hue_increment;
                ggc->setCoding (gcindex, hue_index, hue, glyph);
                double size;
                ggc->getCoding (gcindex, size_index, &size, glyph);
                size += size_increment;
                ggc->setCoding (gcindex, size_index, size, glyph);
                double xwalk = ((double)qrand()/RAND_MAX) - 0.5;
                double ywalk = ((double)qrand()/RAND_MAX) - 0.5;
                double xw, yw;
                glyph->getPosition (&xw, &yw);
                xw += xwalk*0.02;
                yw += ywalk*0.02;
                glyph->setPosition (xw, yw);
            }
            widget->update();
        });
    timer->start (100);

    qapp.exec();

    exit (0);
 }

In the next example we use a coding callback function given as a lambda expression to effect the changes in the glyph colors and sizes.


% cat Makefile
BIN=        bqplot_test_glyphsgraphicscontext2

fflags=
ldflags=

cflags = -g
cxxflags = -g -I$(QTINCLUDE)
ldlibs    = -lbqplot_native $(QTNATIVELIBS) -lbanner -lbrttutil $(DBLIBS)

include $(ANTELOPEMAKE)

OBJS= bqplot_test_glyphsgraphicscontext2.o
$(BIN) : $(OBJS)
    $(CXX) $(CXXFLAGS) -g -o $(BIN) $(OBJS) $(LDFLAGS) $(LDLIBS)
%
% cat bqplot_test_glyphsgraphicscontext2.cc

#include "BQ.h"

#define GCITEM_DEFINITIONS_STR "\
glyphs_gcitem_definitions &Arr{\n\
    outline_color:symbol        constant    \\#80000000\n\
    outline_linewidth:symbol    constant    0.5\n\
    fill_color:symbol           hue         0.0,0.0:240.0,240.0;1.0;0.6\n\
    size:symbol                 size        10.0,10.0:20.0,20.0:10.0:50.0\n\
    opacity:symbol              constant    0.8\n\
}\n"

#define GC_DEFINITIONS_STR "\
glyphs_gc_definitions &Arr{\n\
    glyphs &Tbl{\n\
        size:symbol\n\
        fill_color:symbol\n\
        outline_linewidth:symbol\n\
        outline_color:symbol\n\
        opacity:symbol\n\
    }\n\
}\n"

int
main (int argc, char **argv)

{
    QApplication qapp(argc, argv);

    qapp.setApplicationName("bqplot_test_graphicscontext");

    QWidget *widget = new QWidget();
    widget->setWindowTitle("bqplot_test_graphicscontext");
    widget->show();
    widget->resize (800, 800);

    BQViewport *myvp = new BQViewport(widget);

    myvp->configure (
            "xleft",      "-1.0",
            "xright",     "5.0",
            "ybottom",    "-1.1",
            "ytop",       "1.1",
            "mleft",      "80",
            "mright",     "20",
            "mbottom",    "50",
            "mtop",       "30",
            "fill_frame", "lightblue",
            "fill",       "#e0e0e0",
            NULL);

    BQAxes *myax = new BQAxes(myvp);
    myax->configure (
             "xlabel",                "My X-stuff",
             "ylabel",                "My Y-stuff",
             "xformat",               "%.1f",
             "yformat",               "%.1f",
             "linewidth_box",         "0",
             "linewidth_tics",        "2",
             "linewidth_tics_small",  "0",
             "axis_style",            "sw",
             "tic_style",             "siwinoeo",
             "linewidth_grids",       "2",
             "linewidth_grids_small", "0",
             "color_grids",           "black",
             "color_grids_small",     "#c0c0c0",
             NULL);

    BQ_XY points[] = {
        {1.2, 0.4},
        {-0.5, -0.7},
        {3.7, 0.3},
        {2.1, 0.9},
        {4.1, -0.5}
    };

    int n = sizeof(points) / sizeof(BQ_XY);

    int symbols[] = {
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_CIRCLE,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_SQUARE,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_TRIANGLE,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_DIAMOND,
        BQ_GRAPHICS_CONTEXT_SYMBOL_TYPE_STAR,
    };

    BQGlyphsGraphicsContext *ggc = new BQGlyphsGraphicsContext ();
    ggc->configure (
            "gcitem_definitions",   GCITEM_DEFINITIONS_STR,
            "gc_definitions",       GC_DEFINITIONS_STR,
            NULL);

    BQGlyphs *glyphs = new BQGlyphs(myvp, ggc);
    glyphs->setInteractionMode(BQ_INTERACTION_MODE_MOVE);

    double t0 = now();
    ggc->setCodingCallback ( [&](BQGlyph *glyph, QString definitions_name, QString coding_name, double *value, BQ_ClientData client_data) {
            if (coding_name == "hue") {
                double hue_increment = 5.0;
                double hue = *value;
                if (hue == (float)BQ_FLOAT_NULL) {
                    hue = glyph->getIndex()*(360.0/n);
                }
                hue += hue_increment;
                *value = hue;
                return (1);
            } else if (coding_name == "size") {
                double size_increment = 0.5 - sin((now() - t0)*0.3);
                double size = *value;
                if (size == (float)BQ_FLOAT_NULL) {
                    size = 10.0+glyph->getIndex()*(40.0/n);
                }
                size += size_increment;
                *value = size;
                return (1);
            }
            return (0);
        }, NULL);

    for (int i=0; i<n; i++) {
        BQGlyph *glyph = new BQGlyph(myvp, glyphs);
        glyph->setPosition (points[i].x, points[i].y);
        glyph->setGlyph (symbols[i], "glyphs", NULL);
    }

    QTimer *timer = new QTimer (widget);
    widget->connect (timer, &QTimer::timeout, [widget, glyphs]() {
            glyphs->updateCoding();
            widget->update();
        });
    timer->start (100);

    qapp.exec();

    exit (0);
 }

SEE ALSO

bqplot(3), BQGraphicsContext(3), BQGlyph(3), BQGlyphs(3), BQPolyline(3), BQPolypoint(3), BQMapevents(3), BQConfigure(3)

AUTHOR

Danny Harvey, BRTT