File: stressplot.c

package info (click to toggle)
ggobi 2.1.11-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 13,208 kB
  • ctags: 6,166
  • sloc: ansic: 53,297; xml: 28,411; sh: 11,791; makefile: 264; sed: 16
file content (181 lines) | stat: -rw-r--r-- 4,603 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <gtk/gtk.h>
#include "ggobi.h"
#include "externs.h"
#include "GGobiAPI.h"

#include <stdio.h>
#include <string.h>

#include "plugin.h"
#include "ggvis.h"

static void
stressplot_pixmap_clear (ggvisd *ggv, ggobid *gg)
{
  colorschemed *scheme = gg->activeColorScheme;
  GtkWidget *da = ggv->stressplot_da;

  if (gg->plot_GC == NULL)
    init_plot_GC (ggv->stressplot_pix, gg);

  gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_bg);
  gdk_draw_rectangle (ggv->stressplot_pix, gg->plot_GC,
                      TRUE, 0, 0,
                      da->allocation.width,
                      da->allocation.height);
}


gint
ggv_stressplot_configure_cb (GtkWidget *w, GdkEventExpose *event,
  PluginInstance *inst)
{
  ggvisd *ggv = ggvisFromInst (inst);
  ggobid *gg = inst->gg;
  gboolean retval = true;

  if (ggv == NULL)  /*-- too early to configure --*/
    return retval;
  if (w->allocation.width < 2 || w->allocation.height < 2)
    return retval;

  if (ggv->stressplot_pix != NULL)
    gdk_pixmap_unref (ggv->stressplot_pix);
  ggv->stressplot_pix = gdk_pixmap_new (w->window,
    w->allocation.width, w->allocation.height, -1);
  stressplot_pixmap_clear (ggv, gg);

  gtk_widget_queue_draw (w);

  return retval;
}

void
stressplot_pixmap_copy (ggvisd *ggv, ggobid *gg)
{
  GtkWidget *da = ggv->stressplot_da;

  if (gg->plot_GC == NULL)
    init_plot_GC (ggv->stressplot_pix, gg);

  /* copy the pixmap to the screen */
  gdk_draw_pixmap (da->window, gg->plot_GC, ggv->stressplot_pix,
                   0, 0, 0, 0,
                   da->allocation.width,
                   da->allocation.height);
}

gint
ggv_stressplot_expose_cb (GtkWidget *w, GdkEventExpose *event,
  PluginInstance *inst)
{
  ggobid *gg = inst->gg;
  ggvisd *ggv = ggvisFromInst (inst);
  gboolean retval = true;

  /*-- sanity checks --*/
  if (ggv == NULL)  /*-- too early to expose or configure --*/
    return retval;
  if (ggv->stressplot_pix == NULL)
    return retval;
  if (w->allocation.width < 2 || w->allocation.height < 2)
    return retval;

  /*-- copy the pixmap to the screen --*/
  stressplot_pixmap_copy (ggv, gg);

  return retval;
}

void
reinit_stress (ggvisd *ggv, ggobid *gg)
{
  mds_once (false, ggv, gg);
}


void
draw_stress (ggvisd *ggv, ggobid *gg)
{
  gint i, j, npixels, start, end;
  gfloat x, y;
  GdkPoint axes[3];
  gchar *str;
  GdkPoint strPts[NSTRESSVALUES];
  gfloat height;
  GtkWidget *da = ggv->stressplot_da;
  colorschemed *scheme = gg->activeColorScheme;
  PangoLayout *layout = gtk_widget_create_pango_layout(da, NULL);
  PangoRectangle rect;

  if (gg->plot_GC == NULL)
    init_plot_GC (ggv->stressplot_pix, gg);

  height = (gfloat)da->allocation.height - 2. * (gfloat)STRESSPLOT_MARGIN;

  str = g_strdup_printf ("%s", ".9999");
  layout_text(layout, str, &rect);
  g_free (str);

  if (ggv->stressplot_pix == NULL)
    return;

  /* plotting one point per pixel ... */
  npixels = MIN(da->allocation.width - 2*STRESSPLOT_MARGIN, ggv->nstressvalues);
  start = MAX(0, ggv->nstressvalues - npixels);
  end = ggv->nstressvalues;

  npixels = 0;
  for (i=start, j=0; i<end; i++, j++) {
    x = (gfloat) j ;
    strPts[j].x = (gint) (x + STRESSPLOT_MARGIN);
    y = (gfloat) (1 - ggv->stressvalues.els[i]) * height;
    strPts[j].y = (gint) (y + STRESSPLOT_MARGIN);
    npixels++;
  }

  /* axes */
  axes[0].x = STRESSPLOT_MARGIN;
  axes[0].y = STRESSPLOT_MARGIN;
  axes[1].x = STRESSPLOT_MARGIN;
  axes[1].y = da->allocation.height - STRESSPLOT_MARGIN;
  axes[2].x = da->allocation.width - STRESSPLOT_MARGIN;
  axes[2].y = da->allocation.height - STRESSPLOT_MARGIN;

  /* stress as a fraction */

  stressplot_pixmap_clear (ggv, gg);

  gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_accent);
  gdk_draw_lines (ggv->stressplot_pix, gg->plot_GC, axes, 3);

  if (ggv->nstressvalues > 0) {

    str = g_strdup_printf ("%2.4f",
      ggv->stressvalues.els[ggv->nstressvalues-1]);
	layout_text(layout, str, NULL);
	gdk_draw_layout(ggv->stressplot_pix, gg->plot_GC, 
		da->allocation.width - 2*STRESSPLOT_MARGIN - rect.width,
		STRESSPLOT_MARGIN - rect.height, layout);
    gdk_draw_lines (ggv->stressplot_pix, gg->plot_GC,
      strPts, npixels);
    g_free(str);
  }
  g_object_unref(layout);
  stressplot_pixmap_copy (ggv, gg);
}

void add_stress_value (gdouble stress, ggvisd *ggv)
{
  gint i;

  if (ggv->nstressvalues == NSTRESSVALUES) {
    for (i=0; i < (NSTRESSVALUES-1); i++) {
      ggv->stressvalues.els[i] = ggv->stressvalues.els[i+1];
    }
    ggv->nstressvalues--;
  }

  ggv->stressvalues.els[ggv->nstressvalues] = stress;
  ggv->nstressvalues++;
}