root/trunk/FRII/cartwheel-interface/CanalPopulator.cc

Revision 64, 7.8 kB (checked in by t, 2 years ago)

added menu bars (+ new, quit) to single and triple views.

Line 
1 /*
2  * This file is part of the FamilyRelations II source distribution.
3  *
4  * FamilyRelations II is part of the FamilyJewels package for comparative
5  * sequence analysis: http://family.caltech.edu/.
6  *
7  * Contact author: C. Titus Brown, titus@caltech.edu.
8  *
9  * This program and all associated source code files are Copyright (C) 2003,
10  * 2004 the California Institute of Technology, Pasadena, CA, 91125 USA.  It
11  * is under the Lesser GNU Public License; please see the included
12  * LICENSE.txt file for more information, or contact Titus directly.
13  *
14  */
15
16 // Implementation file for a collection of classes that link 'Fl_Toggle_Tree's
17 // to a Cartwheel analysis server (canal) via the canal XML-RPC API.
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <assert.h>
22
23 #include <xmlrpcAPI.hh>         // cartwheel-clients/canal.c++
24
25 #include "load_sister.hh"
26 #include <iff.hh>               // cartwheel-clients/sister.c++
27 #include <sister.hh>            // cartwheel-clients/sister.c++
28
29 #include <FL/Fl_Box.H>
30 #include "../ext/flek/Fl_Toggle_Tree.H"
31 #include "CanalPopulator.hh"
32 #include "load_canal.hh"
33
34 using namespace canal;
35
36 class waiting_box : public Fl_Box {
37 protected:
38   void draw() {
39     fl_color(FL_WHITE);
40     fl_rectf(x(), y(), w(), h());
41     fl_color(FL_BLACK);
42     fl_font(FL_TIMES, 15);
43
44     fl_draw("Loading... please wait.", w() / 2, h() / 2, 0, 0, FL_ALIGN_CENTER);
45    
46   }
47 public:
48   waiting_box(int _x, int _y, int _w, int _h) : Fl_Box(FL_THIN_DOWN_FRAME, _x, _y, _w, _h, "Waiting...") { }
49
50 };
51
52 //
53 // AnalysisGroupOpener
54 //
55
56 class AnalysisGroupOpener : public CanalPopulator {
57 protected:
58   xmlrpcAnalysisGroup * group;
59   xmlrpcFolder * parent;
60   int group_id;
61 public:
62   AnalysisGroupOpener(Fl_Toggle_Tree* t, Fl_Toggle_Node* n,
63                       xmlrpcFolder * p, int id) : CanalPopulator(t, n) {
64     parent = p;
65     group = NULL;
66     group_id = id;
67   };
68
69   //
70   // open -- when called, grab the analysis data & call the appropriate
71   //    loading function.
72   //
73
74   void open() {
75     if (group == NULL) {
76       group = (xmlrpcAnalysisGroup *) parent->get_analysis_group(group_id);
77     }
78
79     std::string type = group->get_type();
80     std::string buf;
81     Fl_Window * top_chooser = (Fl_Window *) tree->user_data();
82
83     if (type == "single") {
84       top_chooser->hide();
85       Fl::wait(.1);             // give the event loop a chance to hide the win
86
87       // bring up a "waiting" window...
88       Fl_Window w(150, 50, "");
89       waiting_box b(0, 0, 150, 50);
90       w.end();
91
92       w.show();
93       Fl::wait(.1);
94       Fl::wait(.1);
95       Fl::wait(.1);
96       Fl::wait(.1);
97      
98       printf("Loading sister XML...\n");
99       buf = group->get_sister_xml(true);
100
101       printf("Parsing...\n");
102       iff::SingleAnalysisGroup * g;
103       g = (iff::SingleAnalysisGroup*) sister::parse_buf(buf);
104
105       printf("Creating gui...\n");
106       load_single(g, this->tree->window());
107
108       w.hide();
109       Fl::wait(.1);
110     } else if (type == "pair") {
111       top_chooser->hide();
112       Fl::wait(.1);             // give the event loop a chance to hide the win
113
114       // bring up a "waiting" window...
115       Fl_Window w(150, 50, "");
116       waiting_box b(0, 0, 150, 50);
117       w.end();
118
119       w.show();
120       Fl::wait(.1);
121       Fl::wait(.1);
122       Fl::wait(.1);
123       Fl::wait(.1);
124      
125       printf("Loading sister XML...\n");
126       buf = group->get_sister_xml(true);
127
128       printf("Parsing...\n");
129       iff::PairAnalysisGroup * g;
130       g = (iff::PairAnalysisGroup*) sister::parse_buf(buf);
131
132       printf("Creating gui...\n");
133       load_pair(g, this->tree->window());
134
135       w.hide();
136     } else if (type == "triple") {
137       top_chooser->hide();
138       Fl::wait(.1);             // give the event loop a chance to hide the win
139
140       // bring up a "waiting" window...
141       Fl_Window w(150, 50, "");
142       waiting_box b(0, 0, 150, 50);
143       w.end();
144
145       w.show();
146       Fl::wait(.1);
147       Fl::wait(.1);
148       Fl::wait(.1);
149       Fl::wait(.1);
150      
151       printf("Loading triple comparison...\n");
152
153       xmlrpcAnalysisGroup * g = (xmlrpcAnalysisGroup *) group;
154       xmlrpcTripleAnalysisGroup * triple = g->get_as_triple_analysis_group();
155
156       printf("Creating gui...\n");
157
158       bool success = load_triple(triple, this->tree->window());
159
160       w.hide();
161       if (!success) {
162         top_chooser->show();
163       }
164     } else {
165       printf("unknown type: %s.  ignoring.\n", type.c_str());
166     }
167   }
168 };
169
170 //
171 // FolderListPopulator
172 //
173
174 class FolderListPopulator : public CanalPopulator {
175 protected:
176   xmlrpcFolder * parent;
177   xmlrpcFolder * folder;
178   int folder_id;
179   bool parent_is_lab;
180 public:
181   FolderListPopulator(Fl_Toggle_Tree* t, Fl_Toggle_Node* n, xmlrpcFolder* p, int id, bool p_is_lab=false) :
182     CanalPopulator(t, n) {
183     parent = p;
184     folder = NULL;
185     folder_id = id;
186     parent_is_lab = p_is_lab;
187   }
188
189   void populate() {
190     if (folder == NULL) {
191       if (parent_is_lab) {      // @CTB hack hack hack
192         folder = (xmlrpcFolder *) ((xmlrpcLab *)parent)->get_folder(folder_id);
193       } else {
194         folder = (xmlrpcFolder *) parent->get_folder(folder_id);
195       }
196     }
197
198     if (!_did_populate) {
199       if (folder->get_type() == "leaf") {
200         Fl_Toggle_Node* new_node;
201         CanalPopulator * p;
202
203         _did_populate = true;
204         tree->traverse_start(node);
205
206         IdNamePairList* groups = folder->get_analysis_group_list();
207         for (unsigned int i = 0; i < groups->size(); i++) {
208           int id = groups->at(i).id();
209           std::string n = groups->at(i).name();
210          
211           new_node = tree->add_sub((char *)n.c_str(), 0, fileSmall);
212           p = new AnalysisGroupOpener(tree, new_node, folder, id);
213           new_node->user_data(p);
214           tree->close(new_node);
215           tree->traverse_up();
216         }
217       } else {
218         Fl_Toggle_Node* new_node;
219
220         _did_populate = true;
221         tree->traverse_start(node);
222
223         IdNamePairList* folders = folder->get_folder_list();
224         for (unsigned int i = 0; i < folders->size(); i++) {
225           int id = folders->at(i).id();
226           std::string n = folders->at(i).name();
227
228           new_node = tree->add_sub((char *)n.c_str(), 1, folderSmall);
229           new_node->user_data(new FolderListPopulator(tree, new_node, folder, id));
230           tree->close(new_node);
231           tree->traverse_up();
232         }
233       }
234
235       tree->sort_tree(Fl_Toggle_Tree::sort_by_label);
236       tree->redraw();
237     }
238   }
239 };
240
241 //
242 // LabTopFolderListPopulator
243 //
244
245 class LabTopFolderListPopulator : public CanalPopulator {
246 protected:
247   xmlrpcLab * lab;
248 public:
249   LabTopFolderListPopulator(Fl_Toggle_Tree* t, Fl_Toggle_Node* n, xmlrpcLab* l) :
250     CanalPopulator(t, n) {
251     lab = l;
252   }
253
254   void populate() {
255     if (!_did_populate) {
256       Fl_Toggle_Node* new_node;
257
258       _did_populate = true;
259       tree->traverse_start(node);
260
261       IdNamePairList* folders = lab->get_folder_list();
262       for (unsigned int i = 0; i < folders->size(); i++) {
263         int id = folders->at(i).id();
264         std::string n = folders->at(i).name();
265
266         new_node = tree->add_sub((char *)n.c_str(), 1, folderSmall);
267        
268         new_node->user_data(new FolderListPopulator(tree, new_node, (xmlrpcFolder *) lab, id, true));
269
270         tree->close(new_node);
271         tree->traverse_up();
272       }
273       tree->sort_tree(Fl_Toggle_Tree::sort_by_label);
274       tree->redraw();
275     }
276   }
277 };
278
279 //
280 // LabListPopulator
281 //
282
283 void LabListPopulator::populate() {
284   if (!_did_populate) {
285     xmlrpcLab * lab, * current;
286     CanalPopulator * p;
287     Fl_Toggle_Node * new_node;
288    
289     _did_populate = true;
290     tree->traverse_start(node);
291
292     current = (xmlrpcLab *) api->get_current_lab();
293
294     IdNamePairList* labs = api->get_lab_list();
295     for (unsigned int i = 0; i < labs->size(); i++) {
296       int id = labs->at(i).id();
297       std::string n = labs->at(i).name();
298       lab = (xmlrpcLab*) api->get_lab(id);
299
300       new_node = tree->add_sub((char *)n.c_str(), 1, folderSmall);
301       p = new LabTopFolderListPopulator(tree,new_node,lab);
302       new_node->user_data(p);
303
304       // leave the current lab open & populate it
305       if (current->id() != id) {
306         tree->close(new_node);
307       } else {
308         p->populate();
309       }
310       tree->traverse_up();
311     }
312     tree->sort_tree(Fl_Toggle_Tree::sort_by_label);
313     tree->redraw();
314   }
315 }
Note: See TracBrowser for help on using the browser.