~ubuntu-branches/ubuntu/raring/lordsawar/raring

« back to all changes in this revision

Viewing changes to src/gui/line-chart.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese, Gonéri Le Bouder
  • Date: 2008-06-17 11:15:26 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617111526-yjyvu9df50zmpdo0
Tags: 0.0.9-1
[ Barry deFreese ]
* New upstream release.
  + Fixes gcc-4.3 builds so drop ftbfs_gcc-4.3_fix.diff.
  + Add new build-dependency for libgnet-dev.
* Add simple man page for new lordsawar-tile-editor.
* Add desktop file for lordsawar-tile-editor.
* Remove French translation on install.

[ Gonéri Le Bouder ]
* bump Debian Policy to 3.8.0. No change needed.
* fix wording in the 0.0.8-3 entry of the Debian changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright (C) 2007, 2008 Ben Asselstine
 
2
//
 
3
//  This program is free software; you can redistribute it and/or modify
 
4
//  it under the terms of the GNU General Public License as published by
 
5
//  the Free Software Foundation; either version 2 of the License, or
 
6
//  (at your option) any later version.
 
7
//
 
8
//  This program is distributed in the hope that it will be useful,
 
9
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
//  GNU Library General Public License for more details.
 
12
//
 
13
//  You should have received a copy of the GNU General Public License
 
14
//  along with this program; if not, write to the Free Software
 
15
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
16
//  02110-1301, USA.
 
17
 
1
18
#include "line-chart.h"
2
19
#include <cairomm/context.h>
3
20
 
4
 
LineChart::LineChart(std::list<std::list<unsigned int> > lines, std::list<Gdk::Color> colours, unsigned int max_height_value)
 
21
LineChart::LineChart(std::list<std::list<unsigned int> > lines, 
 
22
                     std::list<Gdk::Color> colours, 
 
23
                     unsigned int max_height_value,
 
24
                     std::string x_axis_description,
 
25
                     std::string y_axis_description)
5
26
{
6
27
  d_lines = lines;
7
28
  d_colours = colours;
8
29
  d_max_height_value = max_height_value;
 
30
  d_x_axis_description = x_axis_description;
 
31
  d_y_axis_description = y_axis_description;
9
32
  d_x_indicator = -1;
10
33
}
11
34
 
29
52
    yc = height / 2;
30
53
    int origin_x = 0;
31
54
    int origin_y = height;
 
55
    unsigned int hoffs = 30;
 
56
    unsigned int voffs = 30;
32
57
 
33
58
    Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
34
59
      
35
 
 
36
 
 
37
60
    // clip to the area indicated by the expose event so that we only redraw
38
61
    // the portion of the window that needs to be redrawn
39
62
    cr->rectangle(event->area.x, event->area.y,
69
92
          }
70
93
      }
71
94
 
 
95
    // ensure the border is big enough for the label.
 
96
    Glib::RefPtr<Pango::Layout> layout = Glib::wrap (pango_cairo_create_layout (cr->cobj ()));
 
97
    std::string text_font = "Sans 8";
 
98
    Pango::FontDescription font_desc (text_font);
 
99
    layout->set_font_description (font_desc);
 
100
    char buf[15];
 
101
    snprintf (buf, sizeof (buf), "%d", d_max_height_value);
 
102
    layout->set_text(buf);
 
103
    int w, h;
 
104
    layout->get_pixel_size (w, h);
 
105
    if (w * (hoffs / 4) > hoffs)
 
106
      hoffs = w + (hoffs / 4);
 
107
 
72
108
    std::list<Gdk::Color>::iterator cit = d_colours.begin();
73
109
    line = d_lines.begin();
74
110
    for (; line!= d_lines.end(), cit != d_colours.end(); line++, cit++)
80
116
        cr->set_source_rgb(red, green, blue);
81
117
        std::list<unsigned int>::iterator it = (*line).begin();
82
118
        unsigned int turn = 1;
83
 
        cr->move_to(origin_x, origin_y);
 
119
        cr->move_to(origin_x + hoffs, origin_y - voffs);
84
120
        for (; it != (*line).end(); it++, turn++)
85
121
          {
86
 
            cr->line_to(((float)turn / (float)max_turn) * width,
87
 
                        (height - (((float)*it / (float)d_max_height_value) * height)));
 
122
            cr->line_to((((float)turn / (float)max_turn) * 
 
123
                         (width - (hoffs * 2))) + hoffs,
 
124
                        height - (voffs * 2) - 
 
125
                        (((float)*it / (float)d_max_height_value) * 
 
126
                         (height - (voffs * 2.0))) + voffs);
88
127
          }
89
128
        cr->stroke();
90
129
      }
91
130
 
 
131
    //draw horizontal axis
 
132
    cr->set_source_rgb(0.3, 0.3, 0.3);
 
133
    cr->move_to(origin_x + hoffs, origin_y - voffs);
 
134
    cr->line_to((width - (hoffs * 2)) + hoffs, origin_y - voffs);
 
135
    cr->stroke();
 
136
 
 
137
    //draw ticks on the horizontal axis
 
138
    cr->set_source_rgb(0.3, 0.3, 0.3);
 
139
    cr->move_to(origin_x + hoffs, origin_y - voffs + 1);
 
140
    cr->line_to(origin_x + hoffs, origin_y - voffs + (voffs / 4) + 1);
 
141
    cr->stroke();
 
142
 
 
143
    cr->move_to((width - (hoffs * 2)) + hoffs - 1, origin_y - voffs + 1);
 
144
    cr->line_to((width - (hoffs * 2)) + hoffs - 1, 
 
145
                origin_y - voffs + (voffs / 4) + 1);
 
146
    cr->stroke();
 
147
 
 
148
    //draw vertical axis
 
149
    cr->set_source_rgb(0.3, 0.3, 0.3);
 
150
    cr->move_to(origin_x + hoffs, origin_y - voffs);
 
151
    cr->line_to(origin_x + hoffs, voffs);
 
152
    cr->stroke();
 
153
 
 
154
    //draw ticks on the vertical axis
 
155
    cr->move_to(origin_x + hoffs - 1, origin_y - voffs);
 
156
    cr->line_to(origin_x + hoffs - (hoffs / 4) - 1, origin_y - voffs);
 
157
    cr->stroke();
 
158
    cr->move_to(origin_x + hoffs - 1, voffs + 1);
 
159
    cr->line_to(origin_x + hoffs - (hoffs / 4) - 1, voffs + 1);
 
160
    cr->stroke();
 
161
 
 
162
    // draw the labels on the horizontal axis
 
163
    layout->set_font_description (font_desc);
 
164
    layout->set_text("0");
 
165
    layout->get_pixel_size (w, h);
 
166
    cr->move_to(hoffs - (w / 2), origin_y - voffs + (voffs / 4));
 
167
    cr->set_source_rgb (0.0, 0.0, 0.0);
 
168
    cr->set_operator (Cairo::OPERATOR_ATOP);
 
169
    pango_cairo_show_layout (cr->cobj (), layout->gobj ());
 
170
 
 
171
    snprintf (buf, sizeof (buf), "%d", max_turn);
 
172
    layout->set_text(buf);
 
173
    layout->get_pixel_size (w, h);
 
174
    cr->move_to((width - (hoffs * 2)) + hoffs - (w / 2), 
 
175
                origin_y - voffs + (voffs / 4) + 1);
 
176
    cr->set_source_rgb (0.0, 0.0, 0.0);
 
177
    cr->set_operator (Cairo::OPERATOR_ATOP);
 
178
    pango_cairo_show_layout (cr->cobj (), layout->gobj ());
 
179
 
 
180
    layout->set_text(d_y_axis_description);
 
181
    layout->get_pixel_size (w, h);
 
182
    cr->move_to((width / 2 - (hoffs * 1)) + hoffs - (w / 2), 
 
183
                origin_y - voffs + (voffs / 2) + 1);
 
184
    cr->set_source_rgb (0.0, 0.0, 0.0);
 
185
    cr->set_operator (Cairo::OPERATOR_ATOP);
 
186
    pango_cairo_show_layout (cr->cobj (), layout->gobj ());
 
187
 
 
188
    // draw the labels on the vertical axis
 
189
    layout->set_font_description (font_desc);
 
190
    layout->set_text("0");
 
191
    layout->get_pixel_size (w, h);
 
192
    cr->move_to(origin_x + hoffs - (hoffs / 4) - 1 - w, origin_y - voffs - (h/2));
 
193
    cr->set_source_rgb (0.0, 0.0, 0.0);
 
194
    cr->set_operator (Cairo::OPERATOR_ATOP);
 
195
    pango_cairo_show_layout (cr->cobj (), layout->gobj ());
 
196
 
 
197
    snprintf (buf, sizeof (buf), "%d", d_max_height_value);
 
198
    layout->set_text(buf);
 
199
    layout->get_pixel_size (w, h);
 
200
    cr->move_to(origin_x + hoffs - (hoffs / 4) - 1 - w, voffs + 1 - (h/2));
 
201
    cr->set_source_rgb (0.0, 0.0, 0.0);
 
202
    cr->set_operator (Cairo::OPERATOR_ATOP);
 
203
    pango_cairo_show_layout (cr->cobj (), layout->gobj ());
 
204
 
 
205
    PangoContext *context;
 
206
    PangoCairoFontMap *fontmap;
 
207
    fontmap = (PangoCairoFontMap *) pango_cairo_font_map_get_default ();
 
208
    context = pango_cairo_font_map_create_context (fontmap);
 
209
    pango_context_set_base_gravity(context, PANGO_GRAVITY_EAST);
 
210
    pango_context_set_gravity_hint(context, PANGO_GRAVITY_HINT_STRONG);
 
211
    layout->context_changed();
 
212
 
 
213
    layout->set_text(d_x_axis_description);
 
214
    layout->get_pixel_size (w, h);
 
215
    cr->move_to(0, height - (voffs * 2) - 
 
216
                (0.50 * (height - (voffs * 2.0))) + voffs + (w / 2));
 
217
    cr->rotate(-90 / (180.0 / G_PI));
 
218
    pango_cairo_show_layout (cr->cobj (), layout->gobj ());
 
219
 
 
220
    //draw the indicator line
92
221
    if (d_x_indicator > -1 && d_x_indicator <= max_turn)
93
222
      {
94
223
        //draw a line at turn x
95
224
        cr->set_source_rgb(0.0, 0.0, 0.0);
96
 
        cr->move_to(((float)d_x_indicator/ (float)max_turn) * width, 0);
97
 
        cr->line_to(((float)d_x_indicator/ (float)max_turn) * width, height);
 
225
        cr->move_to((((float)d_x_indicator/ (float)max_turn) * (width - (hoffs * 2))) + hoffs, voffs);
 
226
        cr->line_to((((float)d_x_indicator/ (float)max_turn) * (width - (hoffs * 2))) + hoffs, height - voffs);
98
227
        cr->stroke();
99
228
      }
100
229
  }
101
230
 
102
 
  //FIXME: add a horizontal axis, put some ticks on it, and label it.
103
 
  //FIXME: add a vertical axis, put some ticks on it, and label it.
104
231
  return true;
105
232
}
106
233