ARB
GEN_graphic.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : GEN_graphic.cxx //
4 // Purpose : //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in 2001 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // =============================================================== //
11 
12 #include "GEN_local.hxx"
13 #include "GEN_gene.hxx"
14 #include "GEN_graphic.hxx"
15 
16 #include <arbdbt.h>
17 
18 #include <aw_awar.hxx>
19 #include <aw_preset.hxx>
20 #include <aw_msg.hxx>
21 #include <aw_root.hxx>
22 
23 #include <climits>
24 
25 using namespace std;
26 using namespace AW;
27 
28 // ---------------------
29 // GEN_graphic
30 
31 GEN_graphic::GEN_graphic(AW_root *aw_root_, GBDATA *gb_main_, GEN_graphic_cb_installer callback_installer_, int window_nr_)
32  : aw_root(aw_root_),
33  gb_main(gb_main_),
34  callback_installer(callback_installer_),
35  window_nr(window_nr_),
36  gen_root(NULp),
37  disp_device(NULp)
38 {
40 
41  LocallyModify<int> allow_flag_modify(exports.get_modifying_flag_ref(), -1); // delays zoom-reset triggered by set_display_style
43 }
44 
46 
48  disp_device = device;
49 
50  AW_gc_manager *gc_manager =
51  AW_manage_GC(aww,
52  scr->get_gc_base_name(),
53  device,
55  makeGcChangedCallback(AWT_GC_changed_cb, scr),
56  "#55C0AA",
57  "Default$#5555ff",
58  "Gene$#000000",
59  "Marked$#ffff00",
60  "Cursor$#ff0000",
61 
62  "&color_groups", // use color groups
63 
64  NULp);
65 
66  return gc_manager;
67 }
68 
70  if (gen_root) {
71 #if defined(DEBUG) && 0
72  fprintf(stderr, "GEN_graphic::show\n");
73 #endif // DEBUG
74 
75  gen_root->paint(device);
76  }
77  else {
78  device->line(GEN_GC_DEFAULT, -100, -100, 100, 100);
79  device->line(GEN_GC_DEFAULT, -100, 100, 100, -100);
80  }
81 }
82 
84 }
85 
87  if (event.type() == AW_Mouse_Press) {
88  switch (event.cmd()) {
89  case AWT_MODE_ZOOM: {
90  break;
91  }
92  case AWT_MODE_SELECT:
93  case AWT_MODE_INFO: {
94  if (event.button()==AW_BUTTON_LEFT) {
95  const AW_clicked_element *clicked = event.best_click();
96  if (clicked) {
97  GEN_gene *gene = (GEN_gene*)clicked->cd1();
98  if (gene) {
99  GB_transaction ta(gb_main);
100  aw_root->awar(AWAR_LOCAL_GENE_NAME(window_nr))->write_string(gene->Name().c_str());
101 
102  if (event.cmd() == AWT_MODE_INFO) {
103  GEN_popup_gene_infowindow(aw_root, gb_main);
104  }
105  }
106  }
107  }
108  break;
109  }
110  default: {
111  gen_assert(0);
112  break;
113  }
114  }
115  }
116 }
117 
118 inline int GEN_root::smart_text(AW_device *device, int gc, const char *str, AW_pos x, AW_pos y) {
119  SizedCstr cstr(str);
120  int res = device->text(gc, cstr, x, y);
121  if (gc == GEN_GC_CURSOR) {
122  int xsize = device->get_string_size(gc, cstr);
123  const AW_font_limits& lim = device->get_font_limits(gc, 0);
124 
125  Position stext = device->transform(Position(x, y));
126  Rectangle srect(stext.xpos(), stext.ypos()-lim.ascent, stext.xpos()+xsize, stext.ypos()+lim.descent);
127  Rectangle wrect = device->rtransform(srect);
128  increase_selected_range(wrect);
129  }
130  return res;
131 }
132 
133 inline int GEN_root::smart_line(AW_device *device, int gc, AW_pos x0, AW_pos y0, AW_pos x1, AW_pos y1) {
134  int res = device->line(gc, x0, y0, x1, y1);
135  if (gc == GEN_GC_CURSOR) increase_selected_range(Rectangle(x0, y0, x1, y1));
136  return res;
137 }
138 
139 enum PaintWhat {
141 
146 
148  PAINT_COUNT, // has to be last (do NOT remove)
149 };
150 
151 inline bool getDrawGcs(GEN_iterator& gene, PaintWhat what, const string& curr_gene_name, int& draw_gc, int& text_gc) {
152  bool draw = false;
153  if (curr_gene_name == gene->Name()) { // current gene
154  draw_gc = text_gc = GEN_GC_CURSOR;
155  draw = (what == PAINT_SELECTED);
156  }
157  else {
158  GBDATA *gb_gene = (GBDATA*)gene->GbGene();
159 
160  if (GB_read_flag(gb_gene)) { // marked genes
161  draw_gc = text_gc = GEN_GC_MARKED;
162  draw = (what == PAINT_MARKED);
163  }
164  else {
165  int color_group = AW_color_groups_active() ? GBT_get_color_group(gb_gene) : 0;
166  if (color_group) {
167  draw_gc = text_gc = GEN_GC_FIRST_COLOR_GROUP+color_group-1;
168  draw = (what == PAINT_COLORED);
169  }
170  else {
171  draw_gc = GEN_GC_GENE;
172  text_gc = GEN_GC_DEFAULT; // see show_all_nds in GEN_root::paint if you change this!!!
173  draw = (what == PAINT_NORMAL);
174  }
175  }
176  }
177  return draw;
178 }
179 
180 void GEN_root::paint(AW_device *device) {
181  if (error_reason.length()) {
182  device->text(GEN_GC_DEFAULT, error_reason.c_str(), 0, 0);
183  return;
184  }
185 
186  clear_selected_range();
187 
188  AW_root *aw_root = gen_graphic->get_aw_root();
189  int arrow_size = aw_root->awar(AWAR_GENMAP_ARROW_SIZE)->read_int();
190  int show_all_nds = aw_root->awar(AWAR_GENMAP_SHOW_ALL_NDS)->read_int();
191 
192  for (PaintWhat paint_what = PAINT_MIN; paint_what <= PAINT_MAX; paint_what = PaintWhat((int(paint_what)+1))) {
193  switch (gen_graphic->get_display_style()) {
195  double w0 = 2.0*M_PI/double(length);
196  double mp2 = M_PI/2;
197  double inside = aw_root->awar(AWAR_GENMAP_RADIAL_INSIDE)->read_float()*1000;
198  double outside = aw_root->awar(AWAR_GENMAP_RADIAL_OUTSIDE)->read_float();
199 
200  GEN_iterator curr = gene_set.begin();
201  GEN_iterator end = gene_set.end();
202 
203  while (curr != end) {
204  int draw_gc, text_gc;
205  if (getDrawGcs(curr, paint_what, gene_name, draw_gc, text_gc)) {
206  double w = w0*curr->StartPos()-mp2;
207  double sinw = sin(w);
208  double cosw = cos(w);
209  int len = curr->Length();
210 
211  int xi = int(cosw*inside+0.5);
212  int yi = int(sinw*inside+0.5);
213  int xo = xi+int(cosw*outside*len+0.5);
214  int yo = yi+int(sinw*outside*len+0.5);
215 
216  AW_click_cd cd(device, (AW_CL)&*curr);
217  if (show_all_nds || text_gc != GEN_GC_DEFAULT) {
218  smart_text(device, text_gc, curr->NodeInfo().c_str(), xo+20, yo);
219  }
220  smart_line(device, draw_gc, xi, yi, xo, yo);
221 
222  int sa = int(sinw*arrow_size+0.5);
223  int ca = int(cosw*arrow_size+0.5);
224 
225  if (curr->Complement()) {
226  int xa = xi-sa+ca;
227  int ya = yi+ca+sa;
228  smart_line(device, draw_gc, xi, yi, xa, ya);
229  }
230  else {
231  int xa = xo+sa-ca;
232  int ya = yo-ca-sa;
233  smart_line(device, draw_gc, xo, yo, xa, ya);
234  }
235  }
236  ++curr;
237  }
238  break;
239  }
241  float factor_x = aw_root->awar(AWAR_GENMAP_VERTICAL_FACTOR_X)->read_float();
242  float factor_y = aw_root->awar(AWAR_GENMAP_VERTICAL_FACTOR_Y)->read_float();
243  int arrow_x = int(factor_x*arrow_size);
244  int arrow_y = int(factor_y*arrow_size);
245 
246  GEN_iterator curr = gene_set.begin();
247  GEN_iterator end = gene_set.end();
248 
249  while (curr != end) {
250  int draw_gc, text_gc;
251  if (getDrawGcs(curr, paint_what, gene_name, draw_gc, text_gc)) {
252  int y = int(curr->StartPos()*factor_y+0.5);
253  int x2 = int(curr->Length()*factor_x+0.5);
254 
255  AW_click_cd cd(device, (AW_CL)&*curr);
256  if (show_all_nds || text_gc != GEN_GC_DEFAULT) {
257  smart_text(device, text_gc, curr->NodeInfo().c_str(), x2+20, y);
258  }
259  smart_line(device, draw_gc, 0, y, x2, y);
260  if (curr->Complement()) {
261  smart_line(device, draw_gc, 0, y, arrow_x, y-arrow_y);
262  }
263  else {
264  smart_line(device, draw_gc, x2, y, x2-arrow_x, y-arrow_y);
265  }
266  }
267  ++curr;
268  }
269  break;
270  }
271  case GEN_DISPLAY_STYLE_BOOK: {
272  int display_width = aw_root->awar(AWAR_GENMAP_BOOK_BASES_PER_LINE)->read_int();
273  float width_factor = aw_root->awar(AWAR_GENMAP_BOOK_WIDTH_FACTOR)->read_float();
274  int line_height = aw_root->awar(AWAR_GENMAP_BOOK_LINE_HEIGHT)->read_int();
275  int line_space = aw_root->awar(AWAR_GENMAP_BOOK_LINE_SPACE)->read_int();
276  int height_of_line = line_height+line_space;
277  int xLeft = 0;
278  int xRight = int(display_width*width_factor+0.5);
279  int arrowMid = line_height/2;
280 
281  GEN_iterator curr = gene_set.begin();
282  GEN_iterator end = gene_set.end();
283 
284  while (curr != end) {
285  int draw_gc, text_gc;
286  if (getDrawGcs(curr, paint_what, gene_name, draw_gc, text_gc)) {
287  int line1 = curr->StartPos()/display_width;
288  int line2 = curr->EndPos() / display_width;
289  int x1 = int((curr->StartPos()-line1*display_width)*width_factor+0.5);
290  int x2 = int((curr->EndPos() -line2*display_width)*width_factor+0.5);
291  int y1 = line1*height_of_line;
292  int y1o = y1-line_height;
293 
294  AW_click_cd cd(device, (AW_CL)&*curr);
295  if (line1 == line2) { // whole gene in one book-line
296  smart_line(device, draw_gc, x1, y1, x1, y1o);
297  smart_line(device, draw_gc, x2, y1, x2, y1o);
298  smart_line(device, draw_gc, x1, y1, x2, y1);
299  smart_line(device, draw_gc, x1, y1o, x2, y1o);
300  if (show_all_nds || text_gc != GEN_GC_DEFAULT) smart_text(device, text_gc, curr->NodeInfo().c_str(), x1+2, y1-2);
301 
302  if (curr->Complement()) {
303  smart_line(device, draw_gc, x2, y1o, x2-arrowMid, y1o+arrowMid);
304  smart_line(device, draw_gc, x2, y1, x2-arrowMid, y1o+arrowMid);
305  }
306  else {
307  smart_line(device, draw_gc, x1, y1o, x1+arrowMid, y1o+arrowMid);
308  smart_line(device, draw_gc, x1, y1, x1+arrowMid, y1o+arrowMid);
309  }
310  }
311  else {
312  int y2 = line2*height_of_line;
313  int y2o = y2-line_height;
314 
315  // upper line (don't draw right border)
316  smart_line(device, draw_gc, x1, y1, x1, y1o);
317  smart_line(device, draw_gc, x1, y1, xRight, y1);
318  smart_line(device, draw_gc, x1, y1o, xRight, y1o);
319  if (show_all_nds || text_gc != GEN_GC_DEFAULT) smart_text(device, text_gc, curr->NodeInfo().c_str(), x1+2, y1-2);
320 
321  // lower line (don't draw left border)
322  smart_line(device, draw_gc, x2, y2, x2, y2o);
323  smart_line(device, draw_gc, xLeft, y2, x2, y2);
324  smart_line(device, draw_gc, xLeft, y2o, x2, y2o);
325  if (show_all_nds || text_gc != GEN_GC_DEFAULT) smart_text(device, text_gc, curr->NodeInfo().c_str(), xLeft+2, y2-2);
326 
327  if (curr->Complement()) {
328  smart_line(device, draw_gc, x2, y2o, x2-arrowMid, y2o+arrowMid);
329  smart_line(device, draw_gc, x2, y2, x2-arrowMid, y2o+arrowMid);
330  }
331  else {
332  smart_line(device, draw_gc, x1, y1o, x1+arrowMid, y1o+arrowMid);
333  smart_line(device, draw_gc, x1, y1, x1+arrowMid, y1o+arrowMid);
334  }
335  }
336  }
337  ++curr;
338  }
339  break;
340  }
341  }
342  }
343 }
344 
345 void GEN_graphic::delete_gen_root(AWT_canvas *scr, bool just_forget_callbacks) {
346  callback_installer(just_forget_callbacks ? FORGET_CBS : REMOVE_CBS, scr, this);
347  delete gen_root;
348  gen_root = NULp;
349 }
350 
351 void GEN_graphic::reinit_gen_root(AWT_canvas *scr, bool force_reinit) {
352  char *organism_name = aw_root->awar(AWAR_LOCAL_ORGANISM_NAME(window_nr))->read_string();
353  char *gene_name = aw_root->awar(AWAR_LOCAL_GENE_NAME(window_nr))->read_string();
354 
356 
357  if (gen_root) {
358  if (force_reinit || (gen_root->OrganismName() != string(organism_name))) {
359  bool just_forget_callbacks = false;
360  if (gen_root->OrganismName().length() == 0) {
361  exports.request_zoom_reset(); // no organism was displayed before
362  }
363  else {
364  GB_transaction ta(gb_main);
365  if (!GEN_find_organism(gb_main, gen_root->OrganismName().c_str())) {
366  just_forget_callbacks = true; // genome already deleted -> just clean up callback table
367  exports.request_zoom_reset(); // invalid (=none) organism was displayed before
368  }
369  }
370  delete_gen_root(scr, just_forget_callbacks);
371  }
372  if (gen_root && gen_root->GeneName() != string(gene_name)) {
373  gen_root->set_GeneName(gene_name);
374  }
375  }
376 
377  if (!gen_root) {
378  gen_root = new GEN_root(organism_name, gene_name, gb_main, aw_root, this);
379  callback_installer(INSTALL_CBS, scr, this);
380  }
381 
382  free(organism_name);
383  free(gene_name);
384 }
385 
387  style = type;
388 
389  switch (style) {
392  break;
393 
397  break;
398  }
399 
402 }
403 
404 
AW::Vector transform(const AW::Vector &vec) const
Definition: aw_device.hxx:144
GB_TYPES type
void show(AW_device *device) OVERRIDE
Definition: GEN_graphic.cxx:69
bool getDrawGcs(GEN_iterator &gene, PaintWhat what, const string &curr_gene_name, int &draw_gc, int &text_gc)
void handle_command(AW_device *device, AWT_graphic_event &event) OVERRIDE
Definition: GEN_graphic.cxx:86
static char * y[maxsp+1]
return string(buffer, length)
#define AWAR_LOCAL_GENE_NAME(window_nr)
Definition: GEN_local.hxx:29
void paint(AW_device *device)
AW_root * get_aw_root() const
Definition: GEN_graphic.hxx:85
void set_GeneName(const std::string &gene_name_)
Definition: GEN_gene.hxx:120
void check_for_DB_update(GBDATA *gbdummy) OVERRIDE
Definition: GEN_graphic.cxx:83
long read_int() const
Definition: AW_awar.cxx:187
#define AWAR_GENMAP_BOOK_BASES_PER_LINE
Definition: GEN_local.hxx:45
void GEN_popup_gene_infowindow(AW_root *aw_root, GBDATA *gb_main)
STL namespace.
void set_standard_default_padding()
Definition: awt_canvas.hxx:177
#define AWAR_GENMAP_VERTICAL_FACTOR_Y
Definition: GEN_local.hxx:51
GBDATA * GEN_find_organism(GBDATA *gb_main, const char *name)
Definition: adGene.cxx:738
#define AWAR_GENMAP_ARROW_SIZE
Definition: GEN_local.hxx:39
#define AWAR_GENMAP_RADIAL_INSIDE
Definition: GEN_local.hxx:54
AW_MouseButton button() const
Definition: awt_canvas.hxx:223
AW_gc_manager * AW_manage_GC(AW_window *aww, const char *gc_base_name, AW_device *device, int base_drag, AW_GCM_AREA area, const GcChangedCallback &changecb, const char *default_background_color,...)
Definition: AW_preset.cxx:969
#define M_PI
#define gen_assert(bed)
Definition: GEN_local.hxx:19
bool flags_writeable() const
Definition: awt_canvas.hxx:138
const AW_font_limits & get_font_limits(int gc, char c) const
Definition: AW_device.cxx:399
#define AWAR_GENMAP_DISPLAY_TYPE(window_nr)
Definition: GEN_local.hxx:36
#define AWAR_GENMAP_RADIAL_OUTSIDE
Definition: GEN_local.hxx:55
void reinit_gen_root(AWT_canvas *scr, bool force_reinit)
double AW_pos
Definition: aw_base.hxx:29
void set_display_style(GEN_DisplayStyle type)
GEN_DisplayStyle
Definition: GEN_graphic.hxx:38
int get_string_size(int gc, long textlen) const
Definition: AW_device.cxx:443
const double & ypos() const
const char * get_gc_base_name() const
Definition: awt_canvas.hxx:391
const std::string & OrganismName() const
Definition: GEN_gene.hxx:116
AWT_fit_mode fit_mode
Definition: awt_canvas.hxx:128
const std::string & Name() const
Definition: GEN_gene.hxx:72
int GBT_get_color_group(GBDATA *gb_item)
Definition: ad_colorset.cxx:16
AW_CL cd1() const
static double xi
bool line(int gc, const AW::LineVector &Line, AW_bitset filteri=AW_ALL_DEVICES_SCALED)
Definition: aw_device.hxx:430
AW::Vector rtransform(const AW::Vector &vec) const
Definition: aw_device.hxx:145
#define AWAR_LOCAL_ORGANISM_NAME(window_nr)
Definition: GEN_local.hxx:28
PaintWhat
int GB_read_flag(GBDATA *gbd)
Definition: arbdb.cxx:2784
#define AWAR_GENMAP_SHOW_ALL_NDS
Definition: GEN_local.hxx:41
char * read_string() const
Definition: AW_awar.cxx:201
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
AW_device * disp_device
Definition: GEN_graphic.hxx:67
~GEN_graphic() OVERRIDE
Definition: GEN_graphic.cxx:45
GEN_gene_set::iterator GEN_iterator
Definition: GEN_gene.hxx:80
float read_float() const
Definition: AW_awar.cxx:180
#define AWAR_GENMAP_BOOK_LINE_HEIGHT
Definition: GEN_local.hxx:46
long AW_CL
Definition: cb.h:21
bool AW_color_groups_active()
Definition: AW_preset.cxx:1163
AWT_COMMAND_MODE cmd() const
Definition: awt_canvas.hxx:222
#define AWAR_GENMAP_BOOK_WIDTH_FACTOR
Definition: GEN_local.hxx:44
AWT_graphic_exports exports
Definition: awt_canvas.hxx:249
#define AWAR_GENMAP_BOOK_LINE_SPACE
Definition: GEN_local.hxx:47
GEN_DisplayStyle get_display_style() const
Definition: GEN_graphic.hxx:76
void(* GEN_graphic_cb_installer)(CbInstallMode install, AWT_canvas *, GEN_graphic *)
Definition: GEN_graphic.hxx:51
AW_event_type type() const
Definition: awt_canvas.hxx:229
const double & xpos() const
#define NULp
Definition: cxxforward.h:97
int & get_modifying_flag_ref()
Definition: awt_canvas.hxx:143
const char * cstr
Definition: defines.h:21
GB_ERROR write_string(const char *aw_string)
AW_gc_manager * init_devices(AW_window *, AW_device *, AWT_canvas *scr) OVERRIDE
Definition: GEN_graphic.cxx:47
GB_transaction ta(gb_var)
AWT_zoom_mode zoom_mode
Definition: awt_canvas.hxx:127
GBDATA * gb_main
Definition: adname.cxx:33
const std::string & GeneName() const
Definition: GEN_gene.hxx:115
GEN_graphic(AW_root *aw_root, GBDATA *gb_main, GEN_graphic_cb_installer callback_installer_, int window_nr_)
Definition: GEN_graphic.cxx:31
bool text(int gc, const SizedCstr &cstr, const AW::Position &pos, AW_pos alignment=0.0, AW_bitset filteri=AW_ALL_DEVICES_UNSCALED)
Definition: aw_device.hxx:440
void AWT_GC_changed_cb(GcChange whatChanged, AWT_canvas *scr)
Definition: AWT_canvas.cxx:394
#define AWAR_GENMAP_VERTICAL_FACTOR_X
Definition: GEN_local.hxx:50