Hi to all. I'm a spanish student working on his finall project. I'm a begginer programming, so don't be rude with me.
We have a dsPIC reading temperatures and humidity, saving it into a .txt. As this is hard to manage, I have the purpose of writing a gtk aplication that reads the file, takes 2 dates, and show the data contained in the file and it's between the 2 dates with a graph.
I have used a GTK assistant, a gtk_file_chooser, 2 gtk calendars and a gtk curve because of it's easyness- but the problem is that when i set the curve as a spline or linear, only interpolates 9 points, and that's not a good way to show the data :( So i think i will have to draw it by my own, but due my lack of experience and knowledgement, i can't even put a drawnable area. every single example i have seen does not compile under codeblocks (I hope someone can help with this)
But here is my example:
main.cpp
Code:
/*INCLUDES*/
#include <cairo/cairo.h>
#include <pango/pango.h>
#include <string.h>
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
//#include "demo-common.h"
#include <gtk/gtkwindow.h>
//#include "func.c"
GtkWidget* do_assistant (GtkWidget *do_widget);
gchar *parametro_1, *parametro_2, *parametro_3=NULL;
/***************************************************************************************/
/*DEFINICION DE FUNCIONES*/
/***************************************************************************************/
static GtkWidget *window = NULL;
/***************************************************************************************/
/*FUNCION PRINCIPAL*/
/***************************************************************************************/
int main(int argc, char *argv[])
{
unsigned int i=0;
gtk_init(&argc, &argv);
if (argc>=3)
{
for (i=0;argv[i];i++)
{
printf("argumento %u: %s\n",i,argv[i]);
}
parametro_1 = argv[1];
parametro_2 = argv[2];
parametro_3 = argv[3];
printf("hay %d argumentos\n",argc);
printf("path encontrado %s\n",parametro_3);
}
window = do_assistant(window);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
func.cpp
/***************************************************************************************/
/*INCLUDES*/
/***************************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtkwindow.h>
struct reg_sensor {
char nombremaquina[3];
char fecha[12];
char hora[12];
double temp1, temp2;
double humedad1, humedad2;
double reservado1, reservado2, reservado3, reservado4;
double luminosidad1;
double reservado5, reservado6, reservado7;
};
typedef struct reg_sensor registro_sensor;
unsigned int nregistro = 0;
unsigned int arraytemp[500000][3];
unsigned int fecha_inicial[3];
unsigned int fecha_final[3];
unsigned int fecha3[3];
gchar date_start[16], date_end[16];
registro_sensor *arrayRegistros=NULL;
/***************************************************************************************/
/*DEFINICION DE FUNCIONES*/
/***************************************************************************************/
gfloat vector1[30];
static void on_assistant_apply (GtkWidget *widget, gpointer data);
static void on_assistant_close_cancel (GtkWidget *widget, gpointer data);
static void on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data);
static void on_entry_changed (GtkWidget *widget, gpointer data);
static void create_page1 (GtkWidget *assistant);
static void create_page2 (GtkWidget *assistant);
static void create_page3 (GtkWidget *assistant);
static void create_page4 (GtkWidget *assistant);
static void create_page5 (GtkWidget *assistant);
void seleccion(GtkCalendar *calendar,gpointer user_data);
void seleccion2(GtkCalendar *calendar,gpointer user_data);
static void curva_temp1();
static void curva_temp2();
static void curva_hume1();
static void curva_hume2();
static void curva_linear ();
static void curva_spline ();
static void curva_free ();
GtkWidget* do_assistant (GtkWidget *do_widget);
GtkWidget *assistant,*label1, *label2, *label3, *label4;
//GtkWidget* lookup_widget (GtkWidget *widget,
// const gchar *widget_name);
static GtkWidget *entry = NULL;
static GtkWidget *entry1 = NULL;
static GtkWidget *entry2 = NULL;
static GtkWidget *curva = NULL;
static gchar *path;
extern gchar *parametro_1, *parametro_2, *parametro_3;
void abrir_archivo();
void seleccionar_fecha_1();
void leer_archivo(const gchar *selected_filename);
//void ver_archivo(GtkWidget *pFileSelection, char *pFileContent);
void about();
/***************************************************************************************/
/*CREACION DEL ASISTENTE*/
/*************************************************************************************/
GtkWidget* do_assistant (GtkWidget *do_widget)
{
entry = gtk_entry_new();
gchar fetemp1[16], fetemp2[16], fetemp3[16];
if (!assistant)
{
assistant = gtk_assistant_new ();
gtk_window_set_default_size (GTK_WINDOW (assistant), 480, 300);
gtk_window_set_position(GTK_WINDOW(assistant), GTK_WIN_POS_CENTER);
//gtk_window_set_screen (GTK_WINDOW (assistant), gtk_widget_get_screen (do_widget));
create_page1 (assistant);
create_page2 (assistant);
create_page3 (assistant);
create_page4 (assistant);
create_page5 (assistant);
g_signal_connect (G_OBJECT (assistant), "cancel", G_CALLBACK (on_assistant_close_cancel), &assistant);
g_signal_connect (G_OBJECT (assistant), "close", G_CALLBACK (on_assistant_close_cancel), &assistant);
g_signal_connect (G_OBJECT (assistant), "apply", G_CALLBACK (on_assistant_apply), NULL);
g_signal_connect (G_OBJECT (assistant), "prepare", G_CALLBACK (on_assistant_prepare), NULL);
if (parametro_3 != NULL)
{
gtk_entry_set_text(GTK_ENTRY(entry), parametro_3);
g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (on_entry_changed), assistant);
}
}
if (parametro_1 != NULL)
{
gtk_entry_set_text(GTK_ENTRY(entry1), parametro_1);
fetemp1[0]=parametro_1[0];
fetemp1[1]=parametro_1[1];
fetemp1[2]='\0';
fecha_inicial[0]=atoi(fetemp1);
printf("el dia inicial pasado por parametro es: %d\n",fecha_inicial[0]);
fetemp2[0]=parametro_1[3];
fetemp2[1]=parametro_1[4];
fetemp2[2]='\0';
fecha_inicial[1]=(atoi(fetemp2));
printf("el mes inicial pasado por parametro es: %d",fecha_inicial[1]);
fetemp3[0] = parametro_1[6];
fetemp3[1] = parametro_1[7];
fetemp3[2] = parametro_1[8];
fetemp3[3] = parametro_1[9];
fetemp3[4] = '\0';
fecha_inicial[2]=(atoi(fetemp3));
printf("el anno inicial pasado por parametro es: %d\n",fecha_inicial[2]);
}
if (parametro_2 != NULL)
{
gtk_entry_set_text(GTK_ENTRY(entry2), parametro_2);
fetemp1[0]=parametro_2[0];
fetemp1[1]=parametro_2[1];
fetemp1[2]='\0';
fecha_final[0]=atoi(fetemp1);
printf("el dia inicial pasado por parametro es: %d\n",fecha_final[0]);
fetemp2[0]=parametro_2[3];
fetemp2[1]=parametro_2[4];
fetemp2[2]='\0';
fecha_final[1]=(atoi(fetemp2));
printf("el mes inicial pasado por parametro es: %d",fecha_final[1]);
fetemp3[0]=parametro_2[6];
fetemp3[1]=parametro_2[7];
fetemp3[2]=parametro_2[8];
fetemp3[3]=parametro_2[9];
fetemp3[4]='\0';
fecha_final[2]=(atoi(fetemp3));
printf("el anno inicial pasado por parametro es: %d\n",fecha_final[2]);
}
if (!GTK_WIDGET_VISIBLE (assistant))
{
gtk_widget_show (assistant);
}
else
{
gtk_widget_destroy (assistant);
assistant = NULL;
}
return assistant;
}
/***************************************************************************************/
/*ULTIMA VENTANA DEL ASISTENTE*/
/***************************************************************************************/
static void on_assistant_apply (GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(assistant),
GTK_DIALOG_MODAL,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"Asistente Completado.\n\nDibujar Gr\u00E1fico");
gtk_window_set_title(GTK_WINDOW(dialog), "Finalizar");
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
switch(gtk_dialog_run(GTK_DIALOG(dialog)))
{
case GTK_RESPONSE_OK: // Selección del botón "Si"
g_print("Aceptar\n\n");
gtk_widget_destroy(GTK_WIDGET(assistant));
break;
default: // Selección del botón cerrar "X"
g_print("Cerrar\n\n");
break;
}
gtk_widget_destroy(GTK_WIDGET(dialog));
}
/***************************************************************************************/
/*CERRAMOS EL ASISTENTE ANTES DE HORA*/
/***************************************************************************************/
static void on_assistant_close_cancel (GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(assistant),
GTK_DIALOG_MODAL,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_YES_NO,
"\u00BFDesea salir de la aplicaci\u00F3n?");
gtk_window_set_title(GTK_WINDOW(dialog), "Advertencia");
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
switch(gtk_dialog_run(GTK_DIALOG(dialog)))
{
case GTK_RESPONSE_YES: // Selección del botón "Si"
g_print("Si\n");
gtk_widget_destroy(GTK_WIDGET(assistant));
break;
case GTK_RESPONSE_NO: // Selección del botón "No"
g_print("No \n\n");
break;
default: // Selección del botón cerrar "X"
g_print("Cerrar\n\n");
break;
}
gtk_widget_destroy(GTK_WIDGET(dialog));
}
/***************************************************************************************/
/*TITULO DE LA VENTANA*/
/***************************************************************************************/
static void on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data)
{
gint current_page, n_pages;
gchar *title;
current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (widget));
n_pages = gtk_assistant_get_n_pages (GTK_ASSISTANT (widget));
title = g_strdup_printf ("Asistente (%d de %d)", current_page + 1, n_pages);
gtk_window_set_title (GTK_WINDOW (widget), title);
g_free (title);
}
/**************************************************************************************/
/***************************************************************************************/
static void on_entry_changed (GtkWidget *widget, gpointer data)
{
GtkAssistant *assistant = GTK_ASSISTANT (data);
GtkWidget *current_page;
gint page_number;
const gchar *text;
// page_number = gtk_assistant_get_current_page (assistant);
page_number = 1;
current_page = gtk_assistant_get_nth_page (assistant, page_number);
text = gtk_entry_get_text (GTK_ENTRY (widget));
if (text && text[0])
{
gtk_assistant_set_page_complete (assistant, current_page, TRUE);
}
else
{
gtk_assistant_set_page_complete (assistant, current_page, FALSE);
}
}
/***************************************************************************************/
/***************************************************************************************/
static void create_page1 (GtkWidget *assistant)
{
GtkWidget *label;
GdkPixbuf *pixbuf;
label = gtk_label_new ("Este asistente le ayudar\u00E1 a mostrar gr\u00E1ficamente\nlas estad\u00EDsticas obtenidas con sus sensores.\n\nPulse adelante para continuar.");
gtk_widget_show (label);
gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, "P\u00E1gina 1: Bienvenido");
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_INTRO);
pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);
gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), label, pixbuf);
g_object_unref (pixbuf);
}
/***************************************************************************************/
/***************************************************************************************/
static void create_page2 (GtkWidget *assistant)
{
GtkWidget *box, *label, *button, *table;
GdkPixbuf *pixbuf;
box = gtk_hbox_new (FALSE, 12);
table = gtk_table_new (4, 4, FALSE);
//gtk_table_set_col_spacing (GTK_TABLE (table), 10, 10);
gtk_table_set_row_spacings (GTK_TABLE (table), 40);
gtk_container_add (GTK_CONTAINER (box), table);
gtk_container_border_width(GTK_CONTAINER (table), 24);
label = gtk_label_new ("Seleccione el archivo:");
g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (on_entry_changed), assistant);
button = gtk_button_new_with_label ("Examinar...");
g_signal_connect (G_OBJECT(button), "clicked", G_CALLBACK(abrir_archivo), (gpointer) assistant);
gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 2, 3);
gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 2, 3);
gtk_widget_show_all (box);
gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "P\u00E1gina 2: Selecciona el fichero");
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_CONTENT);
pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_DIALOG, NULL);
gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);
g_object_unref (pixbuf);
}
/***************************************************************************************/
/***************************************************************************************/
static void create_page3 (GtkWidget *assistant)
{
GtkWidget *box, *calendar1, *calendar2, *table,*label1, *label2;
GdkPixbuf *pixbuf;
box = gtk_hbox_new (FALSE, 12);
table = gtk_table_new (4, 4, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 40);
gtk_container_add (GTK_CONTAINER (box), table);
gtk_container_border_width(GTK_CONTAINER (table), 24);
label1 = gtk_label_new ("Seleccione la fecha inicial");
label2 = gtk_label_new ("Seleccione la fecha final");
entry1 = gtk_entry_new ();
entry2 = gtk_entry_new ();
g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (on_entry_changed), assistant);
calendar1 = gtk_calendar_new ();
gtk_widget_set_size_request (calendar1, 241, 186);
gtk_calendar_display_options (GTK_CALENDAR (calendar1),
(GtkCalendarDisplayOptions) (GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES) );
g_signal_connect (G_OBJECT (calendar1), "day_selected", G_CALLBACK (seleccion), assistant);
calendar2 = gtk_calendar_new ();
gtk_widget_set_size_request (calendar2, 241, 186);
gtk_calendar_display_options (GTK_CALENDAR (calendar2),
(GtkCalendarDisplayOptions) (GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES) );
g_signal_connect ((gpointer) calendar2, "day_selected",
G_CALLBACK (seleccion2), NULL);
gtk_table_attach_defaults (GTK_TABLE (table), label1, 0, 1, 0, 1);
gtk_table_attach_defaults (GTK_TABLE (table), label2, 1, 2, 0, 1);
gtk_table_attach_defaults (GTK_TABLE (table), entry1, 0, 1, 1, 2);
gtk_table_attach_defaults (GTK_TABLE (table), entry2, 1, 2, 1, 2);
gtk_table_attach_defaults (GTK_TABLE (table), calendar1, 0, 1, 2, 3);
gtk_table_attach_defaults (GTK_TABLE (table), calendar2, 1, 2, 2, 3);
gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "P\u00E1gina 3: Selecciona los datos");
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_CONTENT);
pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG, NULL);
gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);
g_object_unref (pixbuf);
}
/***************************************************************************************/
/***************************************************************************************/
static void create_page4_prepare (GtkWidget *assistant);
static void create_page4 (GtkWidget *assistant)
{
GtkWidget *box, *table, *boton1, *boton2, *boton3, *boton4, *boton5, *boton6, *boton7;
GdkPixbuf *pixbuf;
box = gtk_vbox_new (FALSE, 12);
table = gtk_table_new (6, 6, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 40);
gtk_container_add (GTK_CONTAINER (box), table);
gtk_container_border_width(GTK_CONTAINER (table), 24);
curva = gtk_curve_new();
gtk_curve_set_range (GTK_CURVE(curva), -2, 2, -2, 2);
label1 = gtk_label_new ("valor maximo: ");
label2 = gtk_label_new ("valor minimo: ");
label3 = gtk_label_new ("Fecha inicial: ");
label4 = gtk_label_new ("Fecha final: ");
//gtk_label_set_text (GTK_LABEL (label1), "valor maximo 2:");
boton1 = gtk_button_new_with_label ("Temperaturas 1");
g_signal_connect (G_OBJECT(boton1), "clicked", G_CALLBACK(curva_temp1), (gpointer) assistant);
boton2 = gtk_button_new_with_label ("Temperaturas 2");
g_signal_connect (G_OBJECT(boton2), "clicked", G_CALLBACK(curva_temp2), (gpointer) assistant);
boton3 = gtk_button_new_with_label ("Humedad 1");
g_signal_connect (G_OBJECT(boton3), "clicked", G_CALLBACK(curva_hume1), (gpointer) assistant);
boton4 = gtk_button_new_with_label ("Humedad 2");
g_signal_connect (G_OBJECT(boton4), "clicked", G_CALLBACK(curva_hume2), (gpointer) assistant);
boton5 = gtk_button_new_with_label ("linear");
g_signal_connect (G_OBJECT(boton5), "clicked", G_CALLBACK(curva_linear), (gpointer) assistant);
boton6 = gtk_button_new_with_label ("spline");
g_signal_connect (G_OBJECT(boton6), "clicked", G_CALLBACK(curva_spline), (gpointer) assistant);
boton7 = gtk_button_new_with_label ("free");
g_signal_connect (G_OBJECT(boton7), "clicked", G_CALLBACK(curva_free), (gpointer) assistant);
gtk_table_attach_defaults (GTK_TABLE (table), curva, 1, 5, 0, 4);
gtk_table_attach_defaults (GTK_TABLE (table), label1, 0, 1, 0, 1);
gtk_table_attach_defaults (GTK_TABLE (table), label2, 0, 1, 3, 4);
gtk_table_attach_defaults (GTK_TABLE (table), label3, 1, 2, 4, 5);
gtk_table_attach_defaults (GTK_TABLE (table), label4, 4, 5, 4, 5);
gtk_table_attach_defaults (GTK_TABLE (table), boton1, 1, 2, 5, 6);
gtk_table_attach_defaults (GTK_TABLE (table), boton2, 2, 3, 5, 6);
gtk_table_attach_defaults (GTK_TABLE (table), boton3, 3, 4, 5, 6);
gtk_table_attach_defaults (GTK_TABLE (table), boton4, 4, 5, 5, 6);
gtk_table_attach_defaults (GTK_TABLE (table), boton5, 1, 2, 6, 7);
gtk_table_attach_defaults (GTK_TABLE (table), boton6, 2, 3, 6, 7);
gtk_table_attach_defaults (GTK_TABLE (table), boton7, 3, 4, 6, 7);
/*static void curva_linear ();
static void curva_spline ();
static void curva_interpolated ();
static void curva_free ();
*/
g_signal_connect (G_OBJECT(assistant), "prepare", G_CALLBACK(create_page4_prepare), (gpointer) assistant);
/*
checkbutton = gtk_check_button_new_with_label ("This is optional data, you may continue even if you do not check this");
gtk_box_pack_start (GTK_BOX (box), checkbutton, FALSE, FALSE, 0);
*/
gtk_widget_show_all (box);
gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "P\u00E1gina 4: Selecciona fechas");
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_CONTENT);
pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);
gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);
g_object_unref (pixbuf);
}
static void create_page4_prepare (GtkWidget *assistant)
{
int page_number = gtk_assistant_get_current_page((GtkAssistant *)assistant);
if (page_number!=3) return;
leer_archivo(gtk_entry_get_text(GTK_ENTRY(entry)));
curva_temp1();
}
/***************************************************************************************/
/***************************************************************************************/
static void create_page5 (GtkWidget *assistant)
{
GtkWidget *label;
GdkPixbuf *pixbuf;
label = gtk_label_new ("This is a confirmation page, press 'Apply' to apply changes");
gtk_widget_show (label);
gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, "P\u00E1gina 5: Confirmaci\u00F3n");
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_CONFIRM);
pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);
gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), label, pixbuf);
g_object_unref (pixbuf);
}
/***************************************************************************************/
/* Función que muestra una explorador para seleccionar el archivo a leer */
/***************************************************************************************/
void abrir_archivo()
{
GtkWidget *pFileSelection;
printf("abriendo archivo %s\n",path);
pFileSelection = gtk_file_chooser_dialog_new("Abrir archivo...",
GTK_WINDOW(assistant),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
gtk_window_set_modal(GTK_WINDOW(pFileSelection), TRUE); // Limitar las acciones a esta ventana
//gtk_window_set_default_size(GTK_WINDOW(pFileSelection), 300, 300);
gtk_window_position(GTK_WINDOW(pFileSelection), GTK_WIN_POS_CENTER);
switch(gtk_dialog_run(GTK_DIALOG(pFileSelection)))
{
case GTK_RESPONSE_ACCEPT: // Selección del botón "Abrir"
g_print("Abrir\n\n");
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(pFileSelection));
gtk_entry_set_text(GTK_ENTRY(entry), path);
break;
case GTK_RESPONSE_CANCEL: // Selección del botón "Cancelar"
g_print("Cancelar\n\n");
break;
default: // Selección del botón cerrar "X"
g_print("Cerrar\n\n");
break;
}
gtk_widget_destroy(pFileSelection);
}
double number2double(const gchar *number)
{
double d=0;
gchar *n = (gchar *) number;
int i;
for (i=0;n[i];i++)
{
if (n[i] == '+') n[i]='0';
if (n[i] == ',') n[i]='.';
}
d=atof(n); // Espera un número de la forma 11.11 o -11.11 o 1111
return d;
}
void procesar_campoN(const gchar *field, int ncolumna, registro_sensor *registro)
{
/*
char nombremaquina[3];
char fecha[12];
char hora[12];
double temp1, temp2;
double humedad1, humedad2;
double reservado1, reservado2, reservado3, reservado4;
double luminosidad1;
double reservado5, reservado6, reservado7;
*/
// printf("Campo %d: %s\n",ncolumna,field);
switch(ncolumna)
{
case 0:
// registro->nombremaquina
strcpy(registro->nombremaquina, field);
break;
case 1:
// registro->fecha
strcpy(registro->fecha, field);
break;
case 2:
// registro->hora
strcpy(registro->hora, field);
break;
case 3:
registro->temp1 = number2double(field);
break;
case 4:
registro->temp2 = number2double(field);
break;
case 5:
registro->humedad1 = number2double(field);
break;
case 6:
registro->humedad2 = number2double(field);
break;
case 11:
registro->luminosidad1 = number2double(field);
break;
}
}
void crear_registro_sensor(const gchar *linea, registro_sensor *registro)
{
int i, j;
char field[25];
int c = 0;
int modo = 0; // 0 - texto; 1 - columna nueva.
char cr;
j=0;
for (i=0;linea[i];i++)
{
cr = linea[i];
if (modo == 1 && cr == ' ') continue; else modo = 0;
// Leyendo texto
if (cr == ' ')
{
// Procesar texto en var field.
field[j] = '\0';
procesar_campoN(field, c, registro);
//////////////////////////////////////////
c+=1;
modo = 1;
j = 0;
continue;
}
// Leer texto.
field[j++] = cr;
}
// Procesar texto en var field.
field[j] = '\0';
procesar_campoN(field, c, registro);
}
/***************************************************************************************/
/* Función que lee el formato del contenido del archivo abierto */
/***************************************************************************************/
void leer_archivo(const gchar *selected_filename)
{
FILE *archivo;
gchar caracter;
gchar contenido[1024];
static gint contador;
registro_sensor registro;
gchar chartemp[16], fetemp1[16], fetemp2[16], fetemp3[16];
unsigned int i,j=0;
if (arrayRegistros) {delete arrayRegistros;arrayRegistros=NULL;}
arrayRegistros = new registro_sensor[200000];
printf("Array creado");
archivo = fopen(selected_filename, "r"); // abrir archivo como lectura "r"
if (archivo)
{
g_print("Archivo seleccionado: %s\n\n", selected_filename);
while(!feof(archivo))
{
caracter = getc(archivo);
if ((caracter == '\r') || (caracter == '\n'))
{
contenido[contador] = '\0';
crear_registro_sensor(contenido,®istro);
arrayRegistros[nregistro++] = registro;
contador = 0;
continue;
}
contenido[contador] = caracter;
contador++;
}
for (i=0; i <= nregistro-1; i++)
{
for (j=0; arrayRegistros[i].fecha[j]; j++)
{
chartemp[j]=arrayRegistros[i].fecha[j];
}
/* for (k=0;k<=1;k++)
{
fetemp1[k]=chartemp[k];
}
*/
fetemp1[0] = chartemp[0];
fetemp1[1] = chartemp[1];
fetemp1[2] = '\0';
arraytemp[i][0] = atoi(fetemp1);
fetemp2[0] = chartemp[3];
fetemp2[1] = chartemp[4];
fetemp2[2] = '\0';
arraytemp[i][1] = (atoi(fetemp2));
fetemp3[0] = chartemp[6];
fetemp3[1] = chartemp[7];
fetemp3[2] = chartemp[8];
fetemp3[3] = chartemp[9];
fetemp3[4] = '\0';
arraytemp[i][2] = (atoi(fetemp3));
}
}
else
{
g_print("Error al abrir el archivo: %s.\n\n", selected_filename);
}
g_print("Fecha inicial del archivo: %d/%2d/%d\n",arraytemp[0][0],arraytemp[0][1],arraytemp[0][2]);
g_print("Fecha final del archivo: %d/%2d/%d\n",arraytemp[nregistro-1][0],arraytemp[nregistro-1][1],arraytemp[nregistro-1][2]);
fclose(archivo);
}
/***************************************************************************************/
/* Función que muestra un dialogo de "Acerca de..." */
/***************************************************************************************/
void about()
{
GtkWidget *dialog;
dialog = gtk_dialog_new_with_buttons("Acerca del Asistente",
GTK_WINDOW (assistant),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
NULL);
gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
switch(gtk_dialog_run(GTK_DIALOG (dialog)))
{
case GTK_RESPONSE_OK: // Selección del botón "Aceptar"
g_print("Aceptar\n\n");
break;
default: // Selección del botón cerrar "X"
g_print("Cerrar\n\n");
break;
}
gtk_widget_destroy(dialog);
}
/***************************************************************************************/
/* Función que lee la fecha 1 */
/***************************************************************************************/
void seleccion (GtkCalendar *calendar, gpointer user_data)
{
unsigned int anno, mes, dia;
gtk_calendar_get_date(GTK_CALENDAR(calendar),&anno,&mes,&dia);
sprintf(date_start, "%02d/%02d/%04d", dia,mes+1,anno);
gtk_label_set_text (GTK_LABEL (label3), date_start);
fecha_inicial[0]=dia;
fecha_inicial[1]=mes+1;
fecha_inicial[2]=anno;
g_print("\nfecha inicial: %s\n",date_start);
gtk_entry_set_text(GTK_ENTRY(entry1), date_start);
}
/***************************************************************************************/
/* Función que lee la fecha 2 */
/***************************************************************************************/
void seleccion2 (GtkCalendar *calendar,
gpointer user_data)
{
unsigned int anno, mes, dia;
gtk_calendar_get_date(GTK_CALENDAR(calendar),&anno,&mes,&dia);
sprintf(date_end, "%02d/%02d/%04d", dia,mes+1,anno);
gtk_label_set_text (GTK_LABEL (label4), date_end);
fecha_final[0]=dia;
fecha_final[1]=mes+1;
fecha_final[2]=anno;
g_print("\nfecha final: %s\n",date_end);
gtk_entry_set_text(GTK_ENTRY(entry2), date_end);
}
void curva_temp1 ()
{
unsigned int i, k=0;
gfloat vector_T1[nregistro];
gfloat valor_maximo, valor_minimo;
char cadena_temporal[30];
g_print("CURVA (Temperatura 1):\n\nFecha Inicial: %s\nFecha Final : %s\n\n", date_start, date_end);
for (i=0; i < nregistro; i++)
{
if ((arraytemp[i][2] >= fecha_inicial[2]) && (arraytemp[i][2] <= fecha_final[2]))
{
if ((arraytemp[i][1] >= fecha_inicial[1]) && (arraytemp[i][1] <= fecha_final[1]))
{
if ((arraytemp[i][0] >= fecha_inicial[0]) && (arraytemp[i][0] <= fecha_final[0]))
{
vector_T1[k] = arrayRegistros[i].temp1;
k++;
}
}
}
}
valor_maximo=vector_T1[0];
valor_minimo=vector_T1[0];
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
for (i=0;i<=k-1;i++)
{
if(vector_T1[i]>=valor_maximo) {valor_maximo=vector_T1[i];}
if(vector_T1[i]<=valor_minimo) {valor_minimo=vector_T1[i];}
}
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
sprintf(cadena_temporal,"Valor maximo: %2.2f",valor_maximo);
gtk_label_set_text (GTK_LABEL (label1), cadena_temporal);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_minimo);
gtk_label_set_text (GTK_LABEL (label2), cadena_temporal);
gtk_curve_set_range (GTK_CURVE(curva), 0, k-1, valor_minimo, valor_maximo);
gtk_curve_set_vector (GTK_CURVE(curva), k-1, vector_T1);
}
void curva_temp2 ()
{
unsigned int i,k=0;
gfloat vector_T2[nregistro];
gfloat valor_maximo, valor_minimo;
char cadena_temporal[30];
g_print("CURVA (Temperatura 1):\n\nFecha Inicial: %s\nFecha Final : %s\n\n", date_start, date_end);
for (i=0; i < nregistro; i++)
{
if ((arraytemp[i][2] >= fecha_inicial[2]) && (arraytemp[i][2] <= fecha_final[2]))
{
if ((arraytemp[i][1] >= fecha_inicial[1]) && (arraytemp[i][1] <= fecha_final[1]))
{
if ((arraytemp[i][0] >= fecha_inicial[0]) && (arraytemp[i][0] <= fecha_final[0]))
{
vector_T2[k] = arrayRegistros[i].temp2;
k++;
}
}
}
}
valor_maximo=vector_T2[0];
valor_minimo=vector_T2[0];
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
for (i=0;i<=k-1;i++)
{
if(vector_T2[i]>=valor_maximo) {valor_maximo=vector_T2[i];}
if(vector_T2[i]<=valor_minimo) {valor_minimo=vector_T2[i];}
}
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_maximo);
gtk_label_set_text (GTK_LABEL (label1), cadena_temporal);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_minimo);
gtk_label_set_text (GTK_LABEL (label2), cadena_temporal);
gtk_curve_set_range (GTK_CURVE(curva), 0, k-1, valor_minimo, valor_maximo);
gtk_curve_set_vector (GTK_CURVE(curva), k-1, vector_T2);
}
void curva_hume1 ()
{
unsigned int i,k=0;
gfloat vector_H1[nregistro];
gfloat valor_maximo, valor_minimo;
char cadena_temporal[30];
g_print("CURVA (Temperatura 1):\n\nFecha Inicial: %s\nFecha Final : %s\n\n", date_start, date_end);
for (i=0; i < nregistro; i++)
{
if ((arraytemp[i][2] >= fecha_inicial[2]) && (arraytemp[i][2] <= fecha_final[2]))
{
if ((arraytemp[i][1] >= fecha_inicial[1]) && (arraytemp[i][1] <= fecha_final[1]))
{
if ((arraytemp[i][0] >= fecha_inicial[0]) && (arraytemp[i][0] <= fecha_final[0]))
{
vector_H1[k] = arrayRegistros[i].humedad1;
k++;
}
}
}
}
valor_maximo=vector_H1[0];
valor_minimo=vector_H1[0];
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
for (i=0;i<=k-1;i++)
{
if(vector_H1[i]>=valor_maximo) {valor_maximo=vector_H1[i];}
if(vector_H1[i]<=valor_minimo) {valor_minimo=vector_H1[i];}
}
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_maximo);
gtk_label_set_text (GTK_LABEL (label1), cadena_temporal);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_minimo);
gtk_label_set_text (GTK_LABEL (label2), cadena_temporal);
gtk_curve_set_range (GTK_CURVE(curva), 0, k-1, valor_minimo, valor_maximo);
gtk_curve_set_vector (GTK_CURVE(curva), k-1, vector_H1);
}
void curva_hume2 ()
{
unsigned int i, k=0;
gfloat vector_H2[nregistro];
gfloat valor_maximo, valor_minimo;
char cadena_temporal[30];
g_print("CURVA (Temperatura 1):\n\nFecha Inicial: %s\nFecha Final : %s\n\n", date_start, date_end);
for (i=0; i < nregistro; i++)
{
if ((arraytemp[i][2] >= fecha_inicial[2]) && (arraytemp[i][2] <= fecha_final[2]))
{
if ((arraytemp[i][1] >= fecha_inicial[1]) && (arraytemp[i][1] <= fecha_final[1]))
{
if ((arraytemp[i][0] >= fecha_inicial[0]) && (arraytemp[i][0] <= fecha_final[0]))
{
vector_H2[k] = arrayRegistros[i].humedad2;
k++;
}
}
}
}
valor_maximo=vector_H2[0];
valor_minimo=vector_H2[0];
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
for (i=0;i<=k-1;i++)
{
if(vector_H2[i]>=valor_maximo) {valor_maximo=vector_H2[i];}
if(vector_H2[i]<=valor_minimo) {valor_minimo=vector_H2[i];}
}
g_print("valor maximo: %f\n",valor_maximo);
g_print("valor minimo: %f\n",valor_minimo);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_maximo);
gtk_label_set_text (GTK_LABEL (label1), cadena_temporal);
sprintf(cadena_temporal,"Valor minimo: %2.2f",valor_minimo);
gtk_label_set_text (GTK_LABEL (label2), cadena_temporal);
gtk_curve_set_range (GTK_CURVE(curva), 0, k-1, valor_minimo, valor_maximo);
gtk_curve_set_vector (GTK_CURVE(curva), k-1, vector_H2);
}
//linear, spline interpolated, or free
void curva_linear ()
{
gtk_curve_set_curve_type(GTK_CURVE(curva), GTK_CURVE_TYPE_LINEAR);
}
void curva_spline ()
{
gtk_curve_set_curve_type(GTK_CURVE(curva), GTK_CURVE_TYPE_SPLINE);
}
void curva_free ()
{
gtk_curve_set_curve_type(GTK_CURVE(curva), GTK_CURVE_TYPE_FREE);
}
Any sugestions about the curve???