~valavanisalex/ubuntu/maverick/scidavis/fix-604811

« back to all changes in this revision

Viewing changes to scidavis/src/QwtBarCurve.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : QwtBarCurve.cpp
 
3
    Project              : SciDAVis
 
4
    --------------------------------------------------------------------
 
5
    Copyright            : (C) 2006 by Ion Vasilief, Tilman Benkert
 
6
    Email (use @ for *)  : ion_vasilief*yahoo.fr, thzs*gmx.net
 
7
    Description          : Bar curve
 
8
 
 
9
 ***************************************************************************/
 
10
 
 
11
/***************************************************************************
 
12
 *                                                                         *
 
13
 *  This program is free software; you can redistribute it and/or modify   *
 
14
 *  it under the terms of the GNU General Public License as published by   *
 
15
 *  the Free Software Foundation; either version 2 of the License, or      *
 
16
 *  (at your option) any later version.                                    *
 
17
 *                                                                         *
 
18
 *  This program is distributed in the hope that it will be useful,        *
 
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
21
 *  GNU General Public License for more details.                           *
 
22
 *                                                                         *
 
23
 *   You should have received a copy of the GNU General Public License     *
 
24
 *   along with this program; if not, write to the Free Software           *
 
25
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
26
 *   Boston, MA  02110-1301  USA                                           *
 
27
 *                                                                         *
 
28
 ***************************************************************************/
 
29
#include "QwtBarCurve.h"
 
30
#include <QPainter>
 
31
 
 
32
QwtBarCurve::QwtBarCurve(BarStyle style, Table *t, const QString& xColName, const char *name, int startRow, int endRow):
 
33
    DataCurve(t, xColName, name, startRow, endRow)
 
34
{
 
35
bar_offset=0;
 
36
bar_gap=0;
 
37
bar_style=style;
 
38
 
 
39
setPen(QPen(Qt::black, 1, Qt::SolidLine));
 
40
setBrush(QBrush(Qt::red));
 
41
 
 
42
if (bar_style == Vertical)
 
43
        setType(Graph::VerticalBars);
 
44
else
 
45
        setType(Graph::HorizontalBars);
 
46
}
 
47
 
 
48
void QwtBarCurve::copy(const QwtBarCurve *b)
 
49
{
 
50
bar_gap = b->bar_gap;
 
51
bar_offset = b->bar_offset;
 
52
bar_style = b->bar_style;
 
53
}
 
54
 
 
55
void QwtBarCurve::draw(QPainter *painter,
 
56
    const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
 
57
{
 
58
   if ( !painter || dataSize() <= 0 )
 
59
        return;
 
60
 
 
61
    if (to < 0)
 
62
        to = dataSize() - 1;
 
63
 
 
64
    painter->save();
 
65
    painter->setPen(QwtPlotCurve::pen());
 
66
    painter->setBrush(QwtPlotCurve::brush());
 
67
 
 
68
    int dx, dy, ref;
 
69
        double bar_width = 0;
 
70
 
 
71
    if (bar_style == Vertical)
 
72
       ref= yMap.transform(1e-100); //smalest positive value for log scales
 
73
    else
 
74
       ref= xMap.transform(1e-100);
 
75
 
 
76
        if (bar_style == Vertical)
 
77
        {
 
78
                dx = abs(xMap.transform(x(from+1))-xMap.transform(x(from)));
 
79
                for (int i=from+2; i<to; i++)
 
80
                {
 
81
                        int min = abs(xMap.transform(x(i+1))-xMap.transform(x(i)));
 
82
                        if (min <= dx)
 
83
                                dx=min;
 
84
                }
 
85
                bar_width = dx*(1-bar_gap*0.01);
 
86
        }
 
87
        else
 
88
        {
 
89
                dy = abs(yMap.transform(y(from+1))-yMap.transform(y(from)));
 
90
                for (int i=from+2; i<to; i++)
 
91
                {
 
92
                        int min = abs(yMap.transform(y(i+1))-yMap.transform(y(i)));
 
93
                        if (min <= dy)
 
94
                                dy=min;
 
95
                }
 
96
                bar_width = dy*(1-bar_gap*0.01);
 
97
        }
 
98
 
 
99
        const int half_width = int((0.5-bar_offset*0.01)*bar_width);
 
100
        int bw1 = int(bar_width) + 1;
 
101
        for (int i=from; i<=to; i++)
 
102
        {
 
103
                const int px = xMap.transform(x(i));
 
104
        const int py = yMap.transform(y(i));
 
105
 
 
106
                if (bar_style == Vertical)
 
107
                {
 
108
                        if (y(i) < 0)
 
109
                                painter->drawRect(px-half_width, ref, bw1, (py-ref));
 
110
                        else
 
111
                                painter->drawRect(px-half_width, py, bw1, (ref-py+1));
 
112
                }
 
113
                else
 
114
                {
 
115
                        if (x(i) < 0)
 
116
                                painter->drawRect(px, py-half_width, (ref-px), bw1);
 
117
                        else
 
118
                                painter->drawRect(ref, py-half_width, (px-ref), bw1);
 
119
                }
 
120
        }
 
121
        painter->restore();
 
122
}
 
123
 
 
124
QwtDoubleRect QwtBarCurve::boundingRect() const
 
125
{
 
126
QwtDoubleRect rect = QwtPlotCurve::boundingRect();
 
127
double n= (double)dataSize();
 
128
 
 
129
if (bar_style == Vertical)
 
130
        {
 
131
        double dx=(rect.right()-rect.left())/n;
 
132
        rect.setLeft(rect.left()-dx);
 
133
        rect.setRight(rect.right()+dx);
 
134
        }
 
135
else
 
136
        {
 
137
        double dy=(rect.bottom()-rect.top())/n;
 
138
        rect.setTop(rect.top()-dy);
 
139
        rect.setBottom(rect.bottom()+dy);
 
140
        }
 
141
 
 
142
return rect;
 
143
}
 
144
 
 
145
void QwtBarCurve::setGap (int gap)
 
146
{
 
147
if (bar_gap == gap)
 
148
        return;
 
149
 
 
150
bar_gap =gap;
 
151
}
 
152
 
 
153
void QwtBarCurve::setOffset(int offset)
 
154
{
 
155
if (bar_offset == offset)
 
156
        return;
 
157
 
 
158
bar_offset = offset;
 
159
}
 
160
 
 
161
double QwtBarCurve::dataOffset()
 
162
{
 
163
        if (bar_style == Vertical)
 
164
        {
 
165
                const QwtScaleMap &xMap = plot()->canvasMap(xAxis());
 
166
                int dx = abs(xMap.transform(x(1))-xMap.transform(x(0)));
 
167
                double bar_width = dx*(1-bar_gap*0.01);
 
168
                if (plot()->isVisible())
 
169
                {
 
170
                        for (int i = 2; i<dataSize(); i++)
 
171
                        {
 
172
                                int min = abs(xMap.transform(x(i))-xMap.transform(x(i-1)));
 
173
                                if (min <= dx)
 
174
                                        dx=min;
 
175
                        }
 
176
                        int x1 = xMap.transform(minXValue()) + int(bar_offset*0.01*bar_width);
 
177
                        return xMap.invTransform(x1) - minXValue();
 
178
                }
 
179
                else
 
180
                        return 0.5*bar_offset*0.01*bar_width;
 
181
        }
 
182
        else
 
183
        {
 
184
                const QwtScaleMap &yMap = plot()->canvasMap(yAxis());
 
185
                int dy = abs(yMap.transform(y(1))-yMap.transform(y(0)));
 
186
                double bar_width = dy*(1-bar_gap*0.01);
 
187
                if (plot()->isVisible())
 
188
                {
 
189
                        for (int i=2; i<dataSize(); i++)
 
190
                        {
 
191
                                int min = abs(yMap.transform(y(i))-yMap.transform(y(i-1)));
 
192
                                if (min <= dy)
 
193
                                        dy=min;
 
194
                        }
 
195
                        int y1 = yMap.transform(minYValue()) + int(bar_offset*0.01*bar_width);
 
196
                        return yMap.invTransform(y1) - minYValue();
 
197
                }
 
198
                else
 
199
                        return 0.5*bar_offset*0.01*bar_width;
 
200
        }
 
201
return 0;
 
202
}