GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 22, 2014 11:49 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: write changes in gtk-treeview cell in the file
PostPosted: Thu Jan 24, 2013 9:11 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
I am writing a code in gtk, that opens a file, parses it as

Code:
/*Open and scan the file*/
fd = open (filename, O_RDWR);
scanner = g_scanner_new (NULL);
g_scanner_input_file (scanner, fd);
....
....
g_scanner_destroy (scanner);
gtk_label_set_text(GTK_LABEL(flabel), filename);
close (fd);


and view it in a treeview(separate function). The treeview is as:

Code:
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING,
                                                G_TYPE_STRING,
                                                G_TYPE_STRING,
                                                G_TYPE_STRING);

  tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
//  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(tree), TRUE);
/* #1: KEY COLUMN */ 
  cell = gtk_cell_renderer_text_new ();
  g_object_set (cell,
                "editable", TRUE,
                NULL);
  g_signal_connect (cell,
      "edited",G_CALLBACK(cell_edited),
      tree);
  g_object_set_data (G_OBJECT (cell),
      "column", GINT_TO_POINTER (COL_BIB_KEY));

  GtkTreeViewColumn *col_key,*col_year,*col_type,*col_auth,*col_pub;
  col_key=gtk_tree_view_column_new_with_attributes (
      "Key", cell,
      "text", COL_BIB_KEY,
      NULL);
  gtk_tree_view_column_set_sort_column_id( col_key, ID_KEY);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree), col_key);
  gtk_tree_view_column_set_max_width  (col_key,100);

where the cell_edited is a function to write the data in the treeview itself:

Code:
void cell_edited(GtkCellRendererText *renderer,
    gchar *path,
    gchar *new_text,
    GtkTreeView *treeview)
{
  guint column;
  GtkTreeIter iter;
  GtkTreeModel *model;
  gpointer columnptr = g_object_get_data(G_OBJECT(renderer), "column");
  column = GPOINTER_TO_UINT(columnptr);

  if (g_ascii_strcasecmp (new_text, "") != 0)
  {
    model = gtk_tree_view_get_model (treeview);
    if (gtk_tree_model_get_iter_from_string (model, &iter, path))
      gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, new_text, -1);
  }
}


The problem is I also want to update the data in the file. Can I get some help on how to do that?


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Fri Jan 25, 2013 8:44 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Anyone kindly reply


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Fri Jan 25, 2013 9:24 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
It would just simply be the reverse of what you are doing already. E.g. if your process is file->(gscanner)->some_data_stucture->treeview you would have to first extract the cells from treeview, update the data_structure accordingly and then reformat the tokens back in that gscanner took out and then dump it back into the file.
If your file is likely to be large compared with the amount of editing then I'd advise keeping an ordered list of changes rather than rewriting the whole. e.g. struct{guint flag, guint row, guint col, gchar* str} where str is the new value for a cell and flag can be 0: edit existing row, 1: new row, 2: delete row. As a result you don't have to extract the whole treeview when only minor changes are made and you can incorporate an undo/redo button by setting up a pointer to the current state in the list. Processing the list to update your data structure and hence file is a bit tricky as you would have to sift out flag 1 and 2 commands (new/delete rows) to process first and update all the row values of prior commands so that you will be able to map the data structures rows to that of the treeview (e.g. "edit row 4" then "new row 3" would be transformed to the equivalent "new row 3" then "edit row 5"). I've never worked with text editors etc. This is just my guess at how I'd approach the problem.
As you can see how you go about getting it back in the file is a very broad topic that is a multi-stage process. Is there any particular aspect that you are finding difficult. I remember you asking about memory mapped files but I don't know much about them.


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Fri Jan 25, 2013 10:09 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul,
Thanks for your reply.
Currently I am not considering edit/delete complete row etc(so flag may be 0/1).
Suppose, in treeview, i have 4 column and 10 row. Now, as you can see from the cell_edited function, edited value is updated in the treeview.

But, the problem is, the file(bibtex) it works with is not much structured. It looks like
Quote:
@Book{a,
Author="bc",
Title="de",
Year="2013",
}
@Article{q,
Author="cd",
Title="ef",
Year="2012",
}

etc.
where, in treeview,a=>col 1; Author=>col 2 etc.
So, now, if I change "q" of the 2nd entry, in treeview, it is updated, but not in file (obviously)!
So, I am trying to find out, what to do.
Much help needed I guess. :(


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Sat Jan 26, 2013 2:25 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Ok here's an example (haven't tested it though, so don't expect ti to work first time).
Lets say you've updated your datastructure (data) from the treeview and now only have to write to the file. The following'll generate the required strings one bibtex @entry at a time and puts them in the file fout. As is it uses a large buffer for s. You might find it better to split it up into smaller segments and use strjoin to build the @entry in dynamic memory.
Code:
struct YourStruc {gint type, year; ghcar* author, index, title;};
enum {T_ART, T_BOK, T_IBK, T_ICL, T_IPR, T_MSC, T_TRP, T_UPB};
...
YourStruc rowinfo;
gchar* fout=NULL,str,str2=NULL,type;
gchar s[1000];
...
{
  rowinfo=g_array_index(data, YourStruc, j);
  switch (rowinfo.type)
  {
    case T_ART:
    type=g_strdup("Article");
    break;
    case T_BOK:
    type=g_strdup("Book");
    break;
    case T_IBK:
    type=g_strdup("InBook");
    break;
    case T_ICL:
    type=g_strdup("InCollection");
    break;
    case T_IPR:
    type=g_strdup("InProceedings");
    break;
    case T_TRP:
    type=g_strdup("TechReport");
    break;
    case T_UPB:
    type=g_strdup("Unpublished");
    break;
    case T_MSC:
    default:
    type=g_strdup("Misc");
    break;
  }
  g_snprintf(s, 1000, "@%s{%s,\nAuthor=\"%s\",\nTitle=\"%s\",\nYear=\"%d\",\n}", type, rowinfo.index, rowinfo.author, rowinfo.title, rowinfo.year);
  g_free(type);
  str2=g_strjoin("\n",str,s,NULL);
  g_free(str);
  str=g_strdup(str2);
  g_free(str2)
}
g_file_set_contents(fout, cts, -1, &Err);
g_free(str);
g_free(fout);


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Sat Jan 26, 2013 10:54 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul, thanks for your untiring help...
I tried to insert your code in my cell_edited function, with
Code:
struct YourStruc {gint type, year; ghcar* author, index, title;};
enum {T_ART, T_BOK, T_IBK, T_ICL, T_IPR, T_MSC, T_TRP, T_UPB};
lines are in .h file that is included.
Code:
void cell_edited(GtkCellRendererText *renderer,
    gchar *path,
    gchar *new_text,
    GtkTreeView *treeview)
  YourStruc rowinfo;
gchar* fout=NULL,str,str2=NULL,type;
gchar s[1000];
{
  guint column,row;
  GtkTreeIter iter;
  GtkTreeModel *model;
  gpointer columnptr = g_object_get_data(G_OBJECT(renderer), "column");
  column = GPOINTER_TO_UINT(columnptr);

  if (g_ascii_strcasecmp (new_text, "") != 0)
  { g_signal_emit_by_name(NULL,"actfile",NULL);
    model = gtk_tree_view_get_model (treeview);
    if (gtk_tree_model_get_iter_from_string (model, &iter, path))
      gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, new_text, -1);
    rowinfo=g_array_index(data, YourStruc, j);
  switch (rowinfo.type)
  {
    case T_ART:
    type=g_strdup("Article");
    break;
    case T_BOK:
    type=g_strdup("Book");
    break;
    case T_IBK:
    type=g_strdup("InBook");
    break;
    case T_ICL:
    type=g_strdup("InCollection");
    break;
    case T_IPR:
    type=g_strdup("InProceedings");
    break;
    case T_TRP:
    type=g_strdup("TechReport");
    break;
    case T_UPB:
    type=g_strdup("Unpublished");
    break;
    case T_MSC:
    default:
    type=g_strdup("Misc");
    break;
  }
  g_snprintf(s, 1000, "@%s{%s,\nAuthor=\"%s\",\nTitle=\"%s\",\nYear=\"%d\",\n}", type, rowinfo.index, rowinfo.author, rowinfo.title, rowinfo.year);
  g_free(type);
  str2=g_strjoin("\n",str,s,NULL);
  g_free(str);
  str=g_strdup(str2);
  g_free(str2)
}
g_file_set_contents(fout, cts, -1, &Err);
g_free(str);
g_free(fout);
}
}


it complains:
Quote:
error: unknown type name ‘YourStruc’
error: parameter ‘fout’ is initialized
error: parameter ‘str2’ is initialized

etc...

Can you kindly have a look? I am feelling like a dumb!


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Sun Jan 27, 2013 3:48 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Don't feel stupid, it was my advice that got you here. I said it was untested and I expected things to go wrong, just find it hard to believe I put together such bad code (personally I'm going to blame it on the red wine I've been using for anaesthetic for broken bones and hot Sydney weather and save judgements of self-stupidity for my next completely sober blunder). Ah well every experience is a learning experience, if for no other purpose than obliterating any delusion that expertise can be obtained rather than continually pursued.
What I did wrong was to omit the struct identifier for declaring rowinfo. Instead of "YourStruct rowinfo;" it should have read "struct YourStruct rowinfo;" even better would to make it a pointer "struct YourStruct* rowinfo;" in which case all later "."'s should be "->"'s. Using typedef is probably also preferable, e.g.:
Code:
typedef struct {gint type, year; ghcar* author, index, title;} YourStruct;
in which case the declarifier "struct" is not needed.
I'd also imagine that the line decalring fout etc. should instead explicitly have pointers on each item:
Code:
gchar *fout=NULL, *str, *str2=NULL, *type

Guess the rest of what I did wrong was hitting the submit button before properly communicating what I was doing and making my assumptions evident. You'll need to actually put a filename value in fout. I assumed this was placed within a save as callback where the filename would be extracted from a g_filechooser call. Another assumption was that it would be placed in a loop over the rows with j representing the row number. I should have put a for or while on the front to make it clear but I think that I was confusing it with the case of only editing particular rows (which isn't really relevant - maybe for reconstructing the YourStruct structure but not for the file itself - in most cases when it comes to files, unless your appending, you may as well recreate the whole thing from scratch). Not too sure why it's saying str2 is initialised when I would expect it to have more of a problem with str being uninitialised. Maybe it was the lack of the * pointer in the declaration. Either way I should have said to set up str with either the first bibliographic item or some comment as a header (e.g. this bibtex file was produced by rudra's famous GTK bibtex GUI downloadable from ....).


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Sun Jan 27, 2013 11:55 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul Childs wrote:
(e.g. this bibtex file was produced by rudra's famous GTK bibtex GUI downloadable from ....).

famous!! oops!
thanks for using it!


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Mon Jan 28, 2013 3:13 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul,I am still feeling confused (ok..not dumb) with GArray! I tried last one day to make reason with GArray and still .....
Suppose, for the timebeing, I only change the Key (first column). and want to replace the element. GLib is taking too much!
I am putting the view.c as attachment for your kind attention.

I have changed a few line here and there.
The header file is
Code:
typedef struct {
  gint
    type,
    year;
  gchar*
    author,
    index,
    title;
} YourStruct;
enum {
  T_ART,
  T_BOK,
  T_IBK,
  T_ICL,
  T_IPR,
  T_MSC,
  T_TRP,
  T_UPB
};


Attachments:
view.c.gz [2.62 KiB]
Downloaded 72 times
Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Tue Jan 29, 2013 9:50 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul,
I know you have tried too much for my help.
but please, if you find some time, give the code a look


Top
 Profile  
 
 Post subject: Re: write changes in gtk-treeview cell in the file
PostPosted: Thu Jan 31, 2013 10:03 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
solved. The problem looks like I should ALWAYS write the COMPLETE buffer to file. ALL OR NONE law!


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group