GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Apr 23, 2014 9:06 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: improvements in gtktreeview
PostPosted: Wed Sep 26, 2012 8:16 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
below I have copied the way I am creating a treeview. Maybe, you may find some part is not necessary, but still I put them to show you how I am doing the things.

I need help in few things:
1) for my typical testing file, which has ~150 entry(to be parsed by parse.sh, create file "fauth.dat" and then read by fauth) It takes around 5 second! I am afraid what will happen for a bigger file. I tried to create a parser in C that will directly store the entries of fauth.dat in array, but failed(Any help in this will be hugely welcome).
Is there anyway to sppedup the process ?
2) Entries in "COL_BIB_NAME" can be arbitraryly long(I have set up max length to 500). Problem is in my present settings, the full line is written in a single line. Can I set the width of each sell fixed? So, that when the entry is more then the width, they will break up in several line? In other word, currently, my cells have fixed hight, variable width. can I change it to variable hight, fixed width?
3) These two is my matter of concern. But being a novice, I am looking for any other advice as well.

Code:
static GtkTreeModel *
create_and_fill_model(void) {

    treestore = gtk_tree_store_new(NUM_COLS,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_STRING);
    return GTK_TREE_MODEL(treestore);
}

static GtkWidget *
create_view_and_model(void) {
    GtkTreeViewColumn *col;
    GtkCellRenderer *renderer;
    GtkWidget *view;
    GtkTreeModel *model;

    view = gtk_tree_view_new();

    /* --- Column #0 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Type");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_TYPE);

    /* --- Column #1 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Name");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_NAME);


    /* --- Column #2 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Year");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_YEAR);
/*    g_object_set(renderer,
            "weight", PANGO_WEIGHT_BOLD,
            "weight-set", TRUE,
            NULL);
*/      

    /* --- Column #3 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Journal");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_PUB);
    /* connect a cell data function */
    //  gtk_tree_view_column_set_cell_data_func(col, renderer, age_cell_data_func, NULL, NULL);


    model = create_and_fill_model();

    gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);

    g_object_unref(model); /* destroy model automatically with view */

    gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
            GTK_SELECTION_NONE);

    return view;
}

static void open_file(GtkWidget *widget, gpointer data) {
    GtkWidget *dialog; //, *entry;
    GtkFileFilter *filter;
    dialog = gtk_file_chooser_dialog_new("Open File", NULL,
            GTK_FILE_CHOOSER_ACTION_OPEN,
            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
            NULL);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "All files (*.*)");
    gtk_file_filter_add_pattern(filter, "*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
    gtk_file_filter_add_pattern(filter, "*.bib");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        char pcomm[1028];
        sprintf(pcomm, "bash parse.sh %s", filename);
        g_print("%s", pcomm);
        system((char *) pcomm);
    }
    int i=0;//,j=0,k=0;
    int num_line=0, ch;
    FILE *fauth, *fyear, *ftitle;
   FILE* tfauth=fopen("fauth.dat","r");
    do{
      ch=fgetc(tfauth);
      if(ch == '\n')
       num_line++;
      }while (ch != EOF);
/*   if(ch != '\n' && num_line != 0)
    num_line++;*/
    fclose(tfauth);
      printf("%d\n",num_line);

    char aauth[num_line][500],ayear[num_line][15],atitle[num_line][500];//, buffer[500];

    fauth=fopen("fauth.dat","r");
    fyear=fopen("fyear.dat","r");
    ftitle=fopen("ftitle.dat","r");
                                   
    if(!fauth||!ftitle||!fyear){
            printf("fauth failed\n");
    }
    else{
  while (i < num_line && fgets(aauth[i],  sizeof aauth[i], fauth)
        && fgets(ayear[i],  sizeof ayear[i], fyear)
        && fgets(atitle[i],  sizeof atitle[i], ftitle))
     i++;
    }
   fclose(fauth);
   fclose(fyear);
   fclose(ftitle);
    for (i = 0; i < num_line; i++){
                gtk_tree_store_append(GTK_TREE_STORE(treestore), &toplevel,NULL);
                gtk_tree_store_set(treestore, &toplevel,
                      COL_BIB_TYPE, NULL,
                      COL_BIB_NAME, aauth[i],
                      COL_BIB_YEAR, ayear[i],
                      COL_BIB_PUB,  atitle[i],
                      -1);
    }

    gtk_label_set_text(GTK_LABEL(flabel), filename);
    gtk_widget_destroy(dialog);
}


Top
 Profile  
 
 Post subject: Re: improvements in gtktreeview
PostPosted: Wed Sep 26, 2012 10:19 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Most of your troubles stem from the fact that you're reading file on a character-by-character basis just to get the number of rows then you reopen the file for the second time to do the "real" reading.

My advice would be to use GIO for file loading, since it'll abstract most of the inefficient code away. Second, that bash parsing script may also contribute quite a substantial amount of time to the whole process. Did you time the bash script on it's own?

The only thing you can do from GTK+'s side is to make sure your tree store is disconnected from the view when adding entries, since this will remove some unnecessary calculations and delegate them to the final addition of store to view.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: improvements in gtktreeview
PostPosted: Wed Sep 26, 2012 12:06 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
tadeboro wrote:
Hi.
My advice would be to use GIO for file loading, since it'll abstract most of the inefficient code away. Second, that bash parsing script may also contribute quite a substantial amount of time to the whole process. Did you time the bash script on it's own?

Thanks Tadeboro for your reply.
Given by your advice, I tried the following code to open and read the content:
Code:
gchar *content, *tfauth;gsize bytes;
tfauth=g_fopen("fauth.dat","r");
g_file_get_contents(tfauth, &content,&bytes, NULL);
g_print("Content%s\n", content);

Its giving an warning:
Quote:
$ make -s
mkbib.c: In function ‘open_file’:
mkbib.c:228:7: warning: assignment makes pointer from integer without a cast [enabled by default]

where the line 228 is the g_fopen.
also,
g_print returns null:
Quote:
Content(null)


Kindly show me the error here.


Top
 Profile  
 
 Post subject: Re: improvements in gtktreeview
PostPosted: Wed Sep 26, 2012 9:53 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
I have corrected the error, as I was mistaking GIO's with glib's file utility.
Now, that portion of the code is:

Code:
GString *content=g_string_new(NULL);
GIOChannel *tfauth;
gsize bytes;
tfauth=g_io_channel_new_file("fauth.dat","r",NULL);
do{
  num_line++;
}while(g_io_channel_read_line_string(tfauth, content,&bytes, NULL) != G_IO_STATUS_EOF);
//g_print("Content%d\n", num_line);

Needless to say, any suggestion is welcome.

But as Tadeboro pointed out, only the parse script is also taking huge time(considering 168 line)
Quote:
$ time ./parse.sh biblio.bib
168
real 0m2.548s
user 0m1.400s
sys 0m5.453s

the effect is twice as bad as I have to read files created by parse.sh to achive my goal.
I will be grateful if someone help me on using lexical scanner as requested here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group