Add the frequency ruler

This commit is contained in:
Alexander Kojevnikov 2010-05-17 10:50:07 +10:00
parent c7e088496f
commit c04c76a0b6
2 changed files with 32 additions and 11 deletions

View File

@ -40,16 +40,16 @@ namespace Spek {
this.format_tick = format_tick;
}
public void draw (Context cr) {
public void draw (Context cr, bool horizontal) {
// Mesure the sample label.
TextExtents ext;
cr.text_extents (sample_label, out ext);
var label_width = ext.width;
var size = horizontal ? ext.width : ext.height;
// Select the factor to use, we want some space between the labels.
int factor = 0;
foreach (var f in factors) {
if (unit_to_pixel (f) >= 1.5 * label_width) {
if (unit_to_pixel (f) >= 1.5 * size) {
factor = f;
break;
}
@ -59,7 +59,7 @@ namespace Spek {
int[] ticks = { 0, units };
if (factor > 0) {
for (var tick = factor; tick < units; tick += factor) {
if (unit_to_pixel (units - tick) < label_width) {
if (unit_to_pixel (units - tick) < size) {
break;
}
ticks += tick;
@ -72,13 +72,22 @@ namespace Spek {
double TICK_LEN = 4;
foreach (var tick in ticks) {
var label = format_tick (tick);
var pos = unit_to_pixel (tick);
var pos = unit_to_pixel (horizontal ? tick : units - tick);
cr.text_extents (label, out ext);
// TODO: use font measurements instead ext.height
if (horizontal) {
cr.move_to (pos - ext.width / 2, GAP + ext.height);
} else {
cr.move_to (-ext.width - GAP, pos + ext.height / 2);
}
cr.show_text (label);
if (horizontal) {
cr.move_to (pos, 0);
cr.rel_line_to (0, TICK_LEN);
} else {
cr.move_to (0, pos);
cr.rel_line_to (-TICK_LEN, 0);
}
cr.stroke ();
}
}

View File

@ -30,7 +30,7 @@ namespace Spek {
private ImageSurface image;
private ImageSurface palette;
private const int PADDING = 40;
private const int PADDING = 60;
private const int GAP = 10;
private const int RULER = 10;
@ -120,7 +120,7 @@ namespace Spek {
cr.paint ();
cr.identity_matrix ();
// Prepare to draw the time ruler.
// Prepare to draw the rulers.
cr.set_source_rgb (1, 1, 1);
cr.set_line_width (1);
cr.set_antialias (Antialias.NONE);
@ -136,7 +136,19 @@ namespace Spek {
unit => (w - 2 * PADDING) * unit / duration_seconds,
unit => "%d:%02d".printf (unit / 60, unit % 60));
cr.translate (PADDING, h - PADDING);
time_ruler.draw (cr);
time_ruler.draw (cr, true);
cr.identity_matrix ();
// Frequency ruler.
var freq = source.rate / 2;
var rate_ruler = new Ruler (
"00.0 kHz",
{1000, 2000, 5000, 10000, 20000},
freq,
unit => (h - 2 * PADDING) * unit / freq,
unit => "%d.%01d kHz".printf (unit / 1000, (unit % 1000) / 100));
cr.translate (PADDING, PADDING);
rate_ruler.draw (cr, false);
cr.identity_matrix ();
}