• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • ekos
  • guide
scroll_graph.cpp
Go to the documentation of this file.
1 /* Ekos guide tool
2  Copyright (C) 2012 Andrew Stepanenko
3 
4  Modified by Jasem Mutlaq <mutlaqja@ikarustech.com> for KStars.
5 
6  This application is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <QtGui>
13 #include <QWidget>
14 
15 #include "scroll_graph.h"
16 #include "common.h"
17 
18 
19 
20 cscroll_graph::cscroll_graph( QWidget *own, int client_width, int client_height )
21 {
22  owner = own;
23 
24  client_rect_wd = client_width;
25  client_rect_ht = client_height;
26 
27  buffer = new QImage( client_rect_wd, client_rect_ht, QImage::Format_RGB32 );
28 
29  vis_range_x = client_rect_wd; // horizontal range in ticks
30  vis_range_y = 100; // whole visible vertical range in arcsecs!
31 
32  grid_N = 6;
33 
34  data_cnt = 10*grid_N*10;
35  data.line[ RA_LINE ] = new double[ data_cnt ];
36  data.line[ DEC_LINE ] = new double[ data_cnt ];
37  reset_data();
38 
39  //graphics...
40  pen.setStyle( Qt::SolidLine );
41  pen.setWidth(1);
42  brush.setStyle(Qt::SolidPattern);
43 
44  RA_COLOR = QColor( DEF_RA_COLOR[0], DEF_RA_COLOR[1], DEF_RA_COLOR[2] );
45  DEC_COLOR = QColor( DEF_DEC_COLOR[0], DEF_DEC_COLOR[1], DEF_DEC_COLOR[2] );
46  GRID_COLOR = QColor( DEF_GRID_COLOR[0], DEF_GRID_COLOR[1], DEF_GRID_COLOR[2] );
47  BKGD_COLOR = QColor( DEF_BKGD_COLOR[0], DEF_BKGD_COLOR[1], DEF_BKGD_COLOR[2] );
48  WHITE_COLOR = QColor( DEF_WHITE_COLOR[0], DEF_WHITE_COLOR[1], DEF_WHITE_COLOR[2] );
49  GRID_FONT_COLOR = QColor( DEF_GRID_FONT_COLOR[0], DEF_GRID_FONT_COLOR[1], DEF_GRID_FONT_COLOR[2] );
50  brush.setColor( BKGD_COLOR );
51 
52 
53  // init...
54  init_render_vars();
55 
56  need_refresh = true;
57 
58 
59 }
60 
61 cscroll_graph::~cscroll_graph()
62 {
63  delete buffer;
64  delete [] data.line[ RA_LINE ];
65  delete [] data.line[ DEC_LINE ];
66 }
67 
68 
69 void cscroll_graph::init_render_vars( void )
70 {
71  half_buffer_size_wd = client_rect_wd / 2;
72  half_buffer_size_ht = client_rect_ht / 2;
73 
74 
75  grid_view_step_x = (double)client_rect_wd / (double)grid_N;
76  grid_view_step_y = (double)client_rect_ht / (double)grid_N;
77 
78  grid_step_x = (double)vis_range_x / (double)grid_N;
79  grid_step_y = (double)vis_range_y / (double)grid_N;
80 
81  half_vis_range_x = vis_range_x / 2;
82  half_vis_range_y = vis_range_y / 2;
83 
84 }
85 
86 
87 void cscroll_graph::set_visible_ranges( int rx, int ry )
88 {
89  if( rx >= 10*grid_N && rx < (double)data_cnt )
90  {
91  if( vis_range_x != rx )
92  need_refresh = true;
93  vis_range_x = rx;
94  }
95  else
96  {
97  u_msg("set_visible_ranges: must be >= %d and < %d", 10*grid_N, data_cnt);
98  return;
99  }
100 
101  if( ry >= 5*grid_N )
102  {
103  if( vis_range_x != ry )
104  need_refresh = true;
105  vis_range_y = ry;
106  }
107  else
108  {
109  u_msg("set_visible_ranges: must be >= %d and < %d", 5*grid_N, data_cnt);
110  return;
111  }
112 
113  init_render_vars();
114 }
115 
116 
117 void cscroll_graph::get_visible_ranges( int *rx, int *ry )
118 {
119  *rx = vis_range_x;
120  *ry = vis_range_y;
121 }
122 
123 
124 int cscroll_graph::get_grid_N( void )
125 {
126  return grid_N;
127 }
128 
129 
130 void cscroll_graph::reset_view( void )
131 {
132 
133  set_visible_ranges( client_rect_wd, 100 );
134 
135  init_render_vars();
136 
137  need_refresh = true;
138 }
139 
140 
141 void cscroll_graph::reset_data( void )
142 {
143  memset( data.line[RA_LINE], 0, sizeof(double)*data_cnt );
144  memset( data.line[DEC_LINE], 0, sizeof(double)*data_cnt );
145  data_idx = 0;
146 }
147 
148 
149 
150 QImage *cscroll_graph::get_buffer( void )
151 {
152  return buffer;
153 }
154 
155 
156 void cscroll_graph::get_screen_size( int *sx, int *sy )
157 {
158  *sx = client_rect_wd;
159  *sy = client_rect_ht;
160 }
161 
162 
163 void cscroll_graph::on_paint( void )
164 {
165  canvas.begin( buffer );
166 
167  refresh();
168 
169  canvas.end();
170 }
171 
172 
173 /*************
174 *
175 * Main Drawing function
176 *
177 **************/
178 void cscroll_graph::refresh( void )
179 {
180  int i, j, k;
181  double kx, ky, step;
182  double *data_ptr;
183  int start_idx;
184  int /*band1_wd,*/ band1_start, band1_end;
185  int band2_wd, band2_start, band2_end;
186  int x, y;
187  int px, py;
188 
189  if( !need_refresh )
190  return;
191 
192  font_ht_k = canvas.fontMetrics().ascent();
193 
194  // fill background
195  canvas.fillRect( 0, 0, client_rect_wd, client_rect_ht, brush);
196 
197  start_idx = (data_idx + data_cnt - vis_range_x) % data_cnt;
198  // split visible region in 2 ranges
199  if( data_idx > start_idx ) // only 1 band
200  {
201  //band1_wd = data_idx - start_idx; // = vis_range_x
202  band1_start = start_idx;
203  band1_end = data_idx; // -1;
204  band2_start = band2_end = band2_wd = 0;
205  }
206  else // 2 bands
207  {
208  //band1_wd = data_idx;
209  band1_start = 0;
210  band1_end = data_idx; //-1;
211 
212  band2_wd = data_cnt - start_idx;
213  band2_start = start_idx;
214  band2_end = data_cnt-1;
215  }
216 
217  // Rasterizing coefficients
218  kx = (double)client_rect_wd / vis_range_x;
219  ky = (double)client_rect_ht / vis_range_y;
220 
221  draw_grid( kx, ky );
222 
223  // analize kx and select optimal algorithm
224  if( client_rect_wd <= vis_range_x )
225  {
226  step = 1.0 / kx;
227 
228  for( k = 0;k < 2;k++ )
229  {
230  data_ptr = data.line[k];
231 
232  if( k == RA_LINE )
233  pen.setColor( RA_COLOR );
234  else
235  pen.setColor( DEC_COLOR );
236 
237  canvas.setPen( pen );
238 
239  // process band 1
240  px = client_rect_wd;;
241  py = half_buffer_size_ht - (int)(data_ptr[band1_end] * ky);
242 
243  x = client_rect_wd;
244 
245  for( i = band1_end, j = 0;i >= band1_start; )
246  {
247  y = half_buffer_size_ht - (int)(data_ptr[i] * ky);
248  x--;
249 
250  canvas.drawLine( px, py, x, y );
251 
252  px = x;
253  py = y;
254 
255  //------------------------------------------
256  ++j;
257  i = band1_end - (int)((double)j*step);
258  }
259 
260  // process band 2
261  for( i = band2_end, j = 0;i > band2_start; )
262  {
263  y = half_buffer_size_ht - (int)(data_ptr[i] * ky);
264  x--;
265 
266  canvas.drawLine( px, py, x, y );
267 
268  px = x;
269  py = y;
270 
271  //------------------------------------------
272  ++j;
273  i = band2_end - (int)((double)j*step);
274  }
275  }
276  }
277  else
278  {
279  step = kx;
280 
281  for( k = 0;k < 2;k++ )
282  {
283  data_ptr = data.line[k];
284 
285  if( k == RA_LINE )
286  pen.setColor( RA_COLOR );
287  else
288  pen.setColor( DEC_COLOR );
289 
290  canvas.setPen( pen );
291 
292  // process band 1
293  px = client_rect_wd;
294  py = half_buffer_size_ht - (int)(data_ptr[band1_end] * ky);
295 
296  x = client_rect_wd;
297 
298  for( i = band1_end, j = 0;i >= band1_start;i--, ++j )
299  {
300  y = half_buffer_size_ht - (int)(data_ptr[i] * ky);
301  x = client_rect_wd - (int)((double)j*step) - 1;
302 
303  canvas.drawLine( px, py, x, y );
304 
305  px = x;
306  py = y;
307 
308  }
309 
310 
311  // process band 2
312  for( i = band2_end;i > band2_start;i--, ++j )
313  {
314  y = half_buffer_size_ht - (int)(data_ptr[i] * ky);
315  x = client_rect_wd - (int)((double)j*step) - 1;
316 
317  canvas.drawLine( px, py, x, y );
318 
319  px = x;
320  py = y;
321  }
322 
323  }
324  }
325 
326  need_refresh = false;
327 }
328 
329 
330 void cscroll_graph::draw_grid( double kx, double )
331 {
332  int i, x, sx, y;
333  int grid_column, val;
334  QString str;
335 
336  pen.setColor( GRID_COLOR );
337  canvas.setPen( pen );
338 
339  grid_column = data_idx / (int)grid_step_x * (int)grid_step_x;
340  sx = client_rect_wd - (double)(data_idx % (int)grid_step_x)*kx;
341 
342  for( i = 0;i < grid_N;++i )
343  {
344  x = sx - (double)i*grid_view_step_x;
345  y = (double)i*grid_view_step_y;
346 
347  canvas.drawLine( x, 0, x, client_rect_ht );
348 
349  if( i == grid_N/2 )
350  {
351  pen.setColor( WHITE_COLOR );
352  canvas.setPen( pen );
353  canvas.drawLine( 0, y, client_rect_wd, y );
354  pen.setColor( GRID_COLOR );
355  canvas.setPen( pen );
356  }
357  else
358  canvas.drawLine( 0, y, client_rect_wd, y );
359  }
360 
361  // draw all digits
362  pen.setColor( GRID_FONT_COLOR );
363  canvas.setPen( pen );
364  for( i = 0;i < grid_N;++i )
365  {
366  x = sx - (double)i*grid_view_step_x;
367  y = (double)i*grid_view_step_y;
368 
369  if( (val = grid_column - i*(int)grid_step_x) >= 0 )
370  {
371  str.setNum( val );
372  canvas.drawText( x, half_buffer_size_ht + font_ht_k, str );
373  }
374  str.setNum( (int)(half_vis_range_y - grid_step_y*i) );
375  canvas.drawText( 2, y + font_ht_k, str );
376  }
377 
378 }
379 
380 
381 
382 bool cscroll_graph::add_point( double ra, double dec )
383 {
384  data_idx++;
385 
386  if( data_idx == data_cnt-1 )
387  data_idx = 0;
388 
389  data.line[ RA_LINE ][ data_idx ] = ra;
390  data.line[ DEC_LINE ][ data_idx ] = dec;
391 
392  need_refresh = true;
393 
394  return true;
395 }
396 
DEF_GRID_COLOR
const u_char DEF_GRID_COLOR[3]
Definition: common.cpp:20
cscroll_graph::get_grid_N
int get_grid_N(void)
Definition: scroll_graph.cpp:124
cscroll_graph::on_paint
void on_paint(void)
Definition: scroll_graph.cpp:163
cscroll_graph::~cscroll_graph
virtual ~cscroll_graph()
Definition: scroll_graph.cpp:61
DEF_WHITE_COLOR
const u_char DEF_WHITE_COLOR[3]
Definition: common.cpp:21
QWidget
DEF_DEC_COLOR
const u_char DEF_DEC_COLOR[3]
Definition: common.cpp:19
cscroll_graph::reset_view
void reset_view(void)
Definition: scroll_graph.cpp:130
u_msg
void u_msg(const char *fmt,...)
Definition: common.cpp:26
cscroll_graph::reset_data
void reset_data(void)
Definition: scroll_graph.cpp:141
RA_LINE
#define RA_LINE
Definition: scroll_graph.h:21
cscroll_graph::add_point
bool add_point(double ra, double dec)
Definition: scroll_graph.cpp:382
cscroll_graph::set_visible_ranges
void set_visible_ranges(int rx, int ry)
Definition: scroll_graph.cpp:87
DEF_BKGD_COLOR
const u_char DEF_BKGD_COLOR[3]
Definition: common.cpp:17
cscroll_graph::get_visible_ranges
void get_visible_ranges(int *rx, int *ry)
Definition: scroll_graph.cpp:117
DEC_LINE
#define DEC_LINE
Definition: scroll_graph.h:22
cscroll_graph::get_screen_size
void get_screen_size(int *sx, int *sy)
Definition: scroll_graph.cpp:156
cscroll_graph
Definition: scroll_graph.h:31
common.h
delta_data_t::line
double * line[2]
Definition: scroll_graph.h:26
scroll_graph.h
DEF_GRID_FONT_COLOR
const u_char DEF_GRID_FONT_COLOR[3]
Definition: common.cpp:22
cscroll_graph::get_buffer
QImage * get_buffer(void)
Definition: scroll_graph.cpp:150
cscroll_graph::cscroll_graph
cscroll_graph(QWidget *own, int client_width, int client_height)
Definition: scroll_graph.cpp:20
DEF_RA_COLOR
const u_char DEF_RA_COLOR[3]
Definition: common.cpp:18
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal