Make it possible to style the WaterLevel widget as a bottle
That holds the “remaining” value now.
This commit is contained in:
parent
8ddaa8c5a2
commit
5260efb552
@ -1,9 +1,11 @@
|
|||||||
#include "gwr-water-level.h"
|
#include "gwr-water-level.h"
|
||||||
|
#include "gwr-enums.h"
|
||||||
|
|
||||||
struct _GwrWaterLevel {
|
struct _GwrWaterLevel {
|
||||||
GtkWidget parent_instance;
|
GtkWidget parent_instance;
|
||||||
|
|
||||||
gfloat level;
|
gfloat level;
|
||||||
|
GwrWaterLevelStyle style;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,6 +18,7 @@ G_DEFINE_TYPE(GwrWaterLevel, gwr_water_level, GTK_TYPE_WIDGET)
|
|||||||
enum {
|
enum {
|
||||||
PROP_0,
|
PROP_0,
|
||||||
PROP_LEVEL,
|
PROP_LEVEL,
|
||||||
|
PROP_STYLE,
|
||||||
N_PROPS
|
N_PROPS
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -61,6 +64,25 @@ gwr_water_level_get_level(GwrWaterLevel *self)
|
|||||||
return self->level;
|
return self->level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gwr_water_level_set_style(GwrWaterLevel *self,
|
||||||
|
GwrWaterLevelStyle style)
|
||||||
|
{
|
||||||
|
g_debug("Setting style to %d", style);
|
||||||
|
|
||||||
|
self->style = style;
|
||||||
|
|
||||||
|
gtk_widget_queue_draw(GTK_WIDGET(self));
|
||||||
|
|
||||||
|
g_object_notify_by_pspec(G_OBJECT(self), props[PROP_LEVEL]);
|
||||||
|
}
|
||||||
|
|
||||||
|
GwrWaterLevelStyle
|
||||||
|
gwr_water_level_get_style(GwrWaterLevel *self)
|
||||||
|
{
|
||||||
|
return self->style;
|
||||||
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
gwr_water_level_draw(GtkWidget *widget, cairo_t *cr)
|
gwr_water_level_draw(GtkWidget *widget, cairo_t *cr)
|
||||||
{
|
{
|
||||||
@ -69,38 +91,67 @@ gwr_water_level_draw(GtkWidget *widget, cairo_t *cr)
|
|||||||
gint width = gtk_widget_get_allocated_width(widget);
|
gint width = gtk_widget_get_allocated_width(widget);
|
||||||
gint height = gtk_widget_get_allocated_height(widget);
|
gint height = gtk_widget_get_allocated_height(widget);
|
||||||
GdkRGBA color;
|
GdkRGBA color;
|
||||||
gint left, right;
|
gint left, right, center;
|
||||||
|
gfloat level_y;
|
||||||
|
|
||||||
gtk_render_background(context, cr, 0, 0, width, height);
|
gtk_render_background(context, cr, 0, 0, width, height);
|
||||||
gtk_render_frame(context, cr, 0, 0, width, height);
|
gtk_render_frame(context, cr, 0, 0, width, height);
|
||||||
|
|
||||||
left = right = width / 2.0;
|
center = left = right = width / 2.0;
|
||||||
left -= 3.0 * height / 8.0;
|
left -= 3.0 * height / 8.0;
|
||||||
right += 3.0 * height / 8.0;
|
right += 3.0 * height / 8.0;
|
||||||
width = right - left;
|
width = right - left;
|
||||||
|
|
||||||
cairo_save(cr);
|
gfloat top_y = width * 0.1;
|
||||||
|
|
||||||
gint top_left_x = left + (width * 0.1);
|
|
||||||
gint top_y = width * 0.1;
|
|
||||||
gint top_right_x = right - (width * 0.1);
|
|
||||||
gint bottom_right_x = right - (width * 0.2);
|
|
||||||
gint bottom_y = height - (width * 0.1);
|
gint bottom_y = height - (width * 0.1);
|
||||||
gint bottom_left_x = left + (width * 0.2);
|
|
||||||
float level_y = top_y + (bottom_y - top_y) * (1.0 - self->level);
|
|
||||||
|
|
||||||
cairo_move_to(cr, top_left_x, top_y);
|
cairo_save(cr);
|
||||||
cairo_line_to(cr, top_right_x, top_y);
|
switch (self->style) {
|
||||||
cairo_line_to(cr, bottom_right_x, bottom_y);
|
case GWR_WATER_LEVEL_STYLE_BOTTLE:
|
||||||
cairo_line_to(cr, bottom_left_x, bottom_y);
|
{
|
||||||
cairo_close_path(cr);
|
gfloat left_x = left + (width * 0.2);
|
||||||
cairo_stroke(cr);
|
gfloat right_x = right - (width * 0.2);
|
||||||
|
|
||||||
cairo_move_to(cr, bottom_right_x, bottom_y);
|
cairo_move_to(cr, left_x, bottom_y);
|
||||||
cairo_line_to(cr, bottom_left_x, bottom_y);
|
cairo_line_to(cr, right_x, bottom_y);
|
||||||
cairo_line_to(cr, bottom_left_x - ((top_right_x - bottom_right_x) * self->level), level_y);
|
cairo_line_to(cr, right_x, top_y + (width * 0.2));
|
||||||
cairo_line_to(cr, bottom_right_x + ((top_right_x - bottom_right_x) * self->level), level_y);
|
cairo_line_to(cr, center + (width * 0.1), top_y + (width * 0.1));
|
||||||
cairo_close_path(cr);
|
cairo_line_to(cr, center + (width * 0.1), top_y);
|
||||||
|
cairo_line_to(cr, center - (width * 0.1), top_y);
|
||||||
|
cairo_line_to(cr, center - (width * 0.1), top_y + (width * 0.1));
|
||||||
|
cairo_line_to(cr, left_x, top_y + (width * 0.2));
|
||||||
|
cairo_close_path(cr);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
|
||||||
|
top_y = top_y + (width * 0.2);
|
||||||
|
level_y = top_y + (bottom_y - top_y) * (1.0 - self->level);
|
||||||
|
cairo_rectangle(cr, left_x, level_y, right_x - left_x, bottom_y - level_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
gint top_left_x = left + (width * 0.1);
|
||||||
|
gint top_right_x = right - (width * 0.1);
|
||||||
|
gint bottom_right_x = right - (width * 0.2);
|
||||||
|
gint bottom_left_x = left + (width * 0.2);
|
||||||
|
level_y = top_y + (bottom_y - top_y) * (1.0 - self->level);
|
||||||
|
|
||||||
|
cairo_move_to(cr, top_left_x, top_y);
|
||||||
|
cairo_line_to(cr, top_right_x, top_y);
|
||||||
|
cairo_line_to(cr, bottom_right_x, bottom_y);
|
||||||
|
cairo_line_to(cr, bottom_left_x, bottom_y);
|
||||||
|
cairo_close_path(cr);
|
||||||
|
cairo_stroke(cr);
|
||||||
|
|
||||||
|
cairo_move_to(cr, bottom_right_x, bottom_y);
|
||||||
|
cairo_line_to(cr, bottom_left_x, bottom_y);
|
||||||
|
cairo_line_to(cr, bottom_left_x - ((top_right_x - bottom_right_x) * self->level), level_y);
|
||||||
|
cairo_line_to(cr, bottom_right_x + ((top_right_x - bottom_right_x) * self->level), level_y);
|
||||||
|
cairo_close_path(cr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
cairo_restore(cr);
|
cairo_restore(cr);
|
||||||
|
|
||||||
gtk_style_context_get_color(context, gtk_style_context_get_state(context), &color);
|
gtk_style_context_get_color(context, gtk_style_context_get_state(context), &color);
|
||||||
@ -163,6 +214,10 @@ gwr_water_level_set_property(GObject *gobject,
|
|||||||
case PROP_LEVEL:
|
case PROP_LEVEL:
|
||||||
gwr_water_level_set_level(GWR_WATER_LEVEL(gobject), g_value_get_float(value));
|
gwr_water_level_set_level(GWR_WATER_LEVEL(gobject), g_value_get_float(value));
|
||||||
|
|
||||||
|
break;
|
||||||
|
case PROP_STYLE:
|
||||||
|
gwr_water_level_set_style(GWR_WATER_LEVEL(gobject), g_value_get_enum(value));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
|
||||||
@ -181,6 +236,10 @@ gwr_water_level_get_property(GObject *gobject,
|
|||||||
case PROP_LEVEL:
|
case PROP_LEVEL:
|
||||||
g_value_set_float(value, gwr_water_level_get_level(GWR_WATER_LEVEL(gobject)));
|
g_value_set_float(value, gwr_water_level_get_level(GWR_WATER_LEVEL(gobject)));
|
||||||
|
|
||||||
|
break;
|
||||||
|
case PROP_STYLE:
|
||||||
|
g_value_set_enum(value, gwr_water_level_get_style(GWR_WATER_LEVEL(gobject)));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
|
||||||
@ -208,6 +267,10 @@ gwr_water_level_class_init(GwrWaterLevelClass *klass)
|
|||||||
"level", "level", "The current water level",
|
"level", "level", "The current water level",
|
||||||
0.0, 1.0, 0.0,
|
0.0, 1.0, 0.0,
|
||||||
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
|
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
|
||||||
|
props[PROP_STYLE] = g_param_spec_enum(
|
||||||
|
"style", "style", "The style of the water level",
|
||||||
|
GWR_TYPE_WATER_LEVEL_STYLE, GWR_WATER_LEVEL_STYLE_GLASS,
|
||||||
|
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
|
||||||
g_object_class_install_properties(gobject_class, N_PROPS, props);
|
g_object_class_install_properties(gobject_class, N_PROPS, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,20 @@
|
|||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GWR_WATER_LEVEL_STYLE_GLASS,
|
||||||
|
GWR_WATER_LEVEL_STYLE_BOTTLE
|
||||||
|
} GwrWaterLevelStyle;
|
||||||
|
|
||||||
# define GWR_TYPE_WATER_LEVEL (gwr_water_level_get_type())
|
# define GWR_TYPE_WATER_LEVEL (gwr_water_level_get_type())
|
||||||
G_DECLARE_FINAL_TYPE(GwrWaterLevel, gwr_water_level, GWR, WATER_LEVEL, GtkWidget)
|
G_DECLARE_FINAL_TYPE(GwrWaterLevel, gwr_water_level, GWR, WATER_LEVEL, GtkWidget)
|
||||||
|
|
||||||
void gwr_water_level_set_level(GwrWaterLevel *water_level,
|
void gwr_water_level_set_level(GwrWaterLevel *water_level,
|
||||||
gfloat level);
|
gfloat level);
|
||||||
gfloat gwr_water_level_get_level(GwrWaterLevel *water_level);
|
gfloat gwr_water_level_get_level(GwrWaterLevel *water_level);
|
||||||
|
void gwr_water_level_set_style(GwrWaterLevel *water_level,
|
||||||
|
GwrWaterLevelStyle style);
|
||||||
|
GwrWaterLevelStyle gwr_water_level_get_style(GwrWaterLevel *water_level);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
|
@ -5,12 +5,14 @@ gwr_sources = [
|
|||||||
'gwr-water-level.c',
|
'gwr-water-level.c',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
gwr_enums = gnome.mkenums_simple('gwr-enums', sources: 'gwr-water-level.h')
|
||||||
|
|
||||||
gwr_deps = [
|
gwr_deps = [
|
||||||
dependency('gio-2.0', version: '>= 2.50'),
|
dependency('gio-2.0', version: '>= 2.50'),
|
||||||
dependency('gtk+-3.0', version: '>= 3.22'),
|
dependency('gtk+-3.0', version: '>= 3.22'),
|
||||||
]
|
]
|
||||||
|
|
||||||
executable('gnome-water-reminder', gwr_sources,
|
executable('gnome-water-reminder', gwr_sources, gwr_enums,
|
||||||
dependencies: gwr_deps,
|
dependencies: gwr_deps,
|
||||||
install: true,
|
install: true,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user