plot.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : plot.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-09-03 Grissiom first version
  13. */
  14. #include <rtgui/rtgui_system.h>
  15. #include <rtgui/dc.h>
  16. #include <rtgui/widgets/plot.h>
  17. static void _rtgui_plot_constructor(struct rtgui_plot *plot)
  18. {
  19. plot->base_point.x = plot->base_point.y = 0;
  20. plot->curve_container.curve = RT_NULL;
  21. plot->curve_container.next = RT_NULL;
  22. /* init widget and set event handler */
  23. rtgui_object_set_event_handler(RTGUI_OBJECT(plot), rtgui_plot_event_handler);
  24. }
  25. static void _free_curve_container_recursive(struct rtgui_plot_curve_container *cnt)
  26. {
  27. if (!cnt)
  28. return;
  29. _free_curve_container_recursive(cnt->next);
  30. rtgui_free(cnt);
  31. }
  32. static void _rtgui_plot_destructor(struct rtgui_plot *plot)
  33. {
  34. _free_curve_container_recursive(plot->curve_container.next);
  35. }
  36. DEFINE_CLASS_TYPE(plot, "plot",
  37. RTGUI_WIDGET_TYPE,
  38. _rtgui_plot_constructor,
  39. _rtgui_plot_destructor,
  40. sizeof(struct rtgui_plot));
  41. struct rtgui_plot *rtgui_plot_create(struct rtgui_plot_curve *curve)
  42. {
  43. struct rtgui_plot *plot;
  44. plot = (struct rtgui_plot *)rtgui_widget_create(RTGUI_PLOT_TYPE);
  45. plot->curve_container.curve = curve;
  46. return plot;
  47. }
  48. RTM_EXPORT(rtgui_plot_create);
  49. void rtgui_plot_destroy(struct rtgui_plot *plot)
  50. {
  51. rtgui_widget_destroy(RTGUI_WIDGET(plot));
  52. }
  53. RTM_EXPORT(rtgui_plot_destroy);
  54. void rtgui_plot_set_base_point(struct rtgui_plot *plot, rt_uint16_t x, rt_uint16_t y)
  55. {
  56. plot->base_point.x = x;
  57. plot->base_point.y = y;
  58. }
  59. RTM_EXPORT(rtgui_plot_set_base_point);
  60. void rtgui_plot_append_curve(struct rtgui_plot *plot, struct rtgui_plot_curve *curve)
  61. {
  62. struct rtgui_plot_curve_container *cur_cnt, *next_cnt;
  63. RT_ASSERT(plot);
  64. next_cnt = rtgui_malloc(sizeof(*next_cnt));
  65. next_cnt->curve = curve;
  66. next_cnt->next = RT_NULL;
  67. cur_cnt = &plot->curve_container;
  68. while (cur_cnt->next)
  69. {
  70. cur_cnt = cur_cnt->next;
  71. }
  72. cur_cnt->next = next_cnt;
  73. }
  74. RTM_EXPORT(rtgui_plot_append_curve);
  75. static void _rtgui_plot_curve_onpaint(
  76. struct rtgui_dc *dc,
  77. struct rtgui_plot_curve *curve,
  78. struct rtgui_point base)
  79. {
  80. struct rtgui_rect rect;
  81. rt_uint16_t height;
  82. int last_x, last_y;
  83. rtgui_color_t old_color;
  84. rtgui_dc_get_rect(dc, &rect);
  85. height = rtgui_rect_height(rect);
  86. old_color = RTGUI_DC_FC(dc);
  87. RTGUI_DC_FC(dc) = curve->color;
  88. if (curve->x_data)
  89. {
  90. rt_size_t i;
  91. last_x = curve->x_data[0] + base.x;
  92. last_y = height - curve->y_data[0] - base.y;
  93. for (i = 1; i < curve->length; i++)
  94. {
  95. int cur_x = curve->x_data[i] + base.x;
  96. int cur_y = height - curve->y_data[i] - base.y;
  97. rtgui_dc_draw_line(dc,
  98. last_x, last_y,
  99. cur_x, cur_y);
  100. last_x = cur_x;
  101. last_y = cur_y;
  102. }
  103. }
  104. else
  105. {
  106. rt_size_t i;
  107. last_x = 0 + base.x;
  108. last_y = height - curve->y_data[0] - base.y;
  109. for (i = 1; i < curve->length; i++)
  110. {
  111. int cur_x = i + base.x;
  112. int cur_y = height - curve->y_data[i] - base.y;
  113. rtgui_dc_draw_line(dc,
  114. last_x, last_y,
  115. cur_x, cur_y);
  116. last_x = cur_x;
  117. last_y = cur_y;
  118. }
  119. }
  120. RTGUI_DC_FC(dc) = old_color;
  121. }
  122. static void _rtgui_plot_onpaint(struct rtgui_object *object, struct rtgui_event *event)
  123. {
  124. struct rtgui_dc *dc;
  125. struct rtgui_rect rect;
  126. struct rtgui_plot *plot = RTGUI_PLOT(object);
  127. struct rtgui_plot_curve_container *cnt;
  128. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot));
  129. if (dc == RT_NULL)
  130. return;
  131. rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
  132. rtgui_dc_fill_rect(dc, &rect);
  133. for (cnt = &plot->curve_container; cnt; cnt = cnt->next)
  134. {
  135. _rtgui_plot_curve_onpaint(dc, cnt->curve, plot->base_point);
  136. }
  137. rtgui_dc_end_drawing(dc);
  138. }
  139. rt_bool_t rtgui_plot_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  140. {
  141. struct rtgui_plot *plot;
  142. RTGUI_WIDGET_EVENT_HANDLER_PREPARE;
  143. plot = RTGUI_PLOT(object);
  144. switch (event->type)
  145. {
  146. case RTGUI_EVENT_PAINT:
  147. _rtgui_plot_onpaint(object, event);
  148. break;
  149. default:
  150. return rtgui_widget_event_handler(object, event);
  151. }
  152. return RT_FALSE;
  153. }
  154. RTM_EXPORT(rtgui_plot_event_handler);