~showard314/ubuntu/utopic/qtiplot/utopic_1311721

« back to all changes in this revision

Viewing changes to qtiplot/src/plot2D/VectorCurve.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2011-05-07 17:00:26 UTC
  • mfrom: (1.1.9 upstream) (2.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110507170026-77wvcv8uc6955fff
* moved debian/build.conf into debian/patches/03_build_conf.patch
  to track changes to build.conf between releases. Updated
  debian/rules accordingly.
* New upstream release. (Closes: #599450)
* Refreshed patches.
* debian/control B-D on libalglib-dev, libtamuanova-dev,
  libqtexengine-dev
* debian/rules allows for parallel builds
* Byte compile python modules with dh_python2, removed debian/*.post{rm,inst}
  dropped dependency on depricated python-central (Closes: #587669)
* Added shared-mime-info xml data in debian/qtiplot.sharedmimeinfo
  (LP: #184307)
* 04_qwtplot3d_static.patch added to build a modified static
  library of qwt3dplot
* 05_link_gl2ps.patch added to use Debian gl2ps library.
* Policy 3.9.2, no changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    File                 : VectorCurve.cpp
3
3
    Project              : QtiPlot
4
4
    --------------------------------------------------------------------
5
 
    Copyright            : (C) 2006 by Ion Vasilief
 
5
        Copyright            : (C) 2006 - 2010 by Ion Vasilief
6
6
    Email (use @ for *)  : ion_vasilief*yahoo.fr
7
7
    Description          : Vector curve class
8
8
 
27
27
 *                                                                         *
28
28
 ***************************************************************************/
29
29
#include "VectorCurve.h"
 
30
#include "ErrorBarsCurve.h"
30
31
#include "Graph.h"
31
32
#include "MultiLayer.h"
32
33
 
33
34
#include <qwt_painter.h>
34
35
#include <qwt_double_rect.h>
35
36
#include <QPainter>
 
37
#include <stdio.h>
36
38
 
37
39
VectorCurve::VectorCurve(VectorStyle style, Table *t, const QString& xColName, const char *name,
38
40
                                const QString& endCol1, const QString& endCol2, int startRow, int endRow):
44
46
        d_headAngle (45),
45
47
        d_position (Tail),
46
48
        d_end_x_a (endCol1),
47
 
        d_end_y_m (endCol2)
 
49
        d_end_y_m (endCol2),
 
50
        vectorEnd(0)
48
51
{
49
52
        d_pen.setCosmetic(true);
50
53
        if (style == XYXY){
87
90
void VectorCurve::drawVector(QPainter *painter,
88
91
    const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
89
92
{
90
 
if (d_style == XYAM)
91
 
{
92
 
 for (int i = from; i <= to; i++)
93
 
        {
94
 
        const double x0 = x(i);
95
 
        const double y0 = y(i);
96
 
        const double angle = vectorEnd->x(i);
97
 
        const double mag = vectorEnd->y(i);
98
 
 
99
 
        int xs = 0, ys = 0, xe = 0, ye = 0;
100
 
        switch(d_position)
101
 
                {
102
 
                case Tail:
103
 
                        xs = xMap.transform(x0);
104
 
                        ys = yMap.transform(y0);
105
 
                        xe = xMap.transform(x0 + mag*cos(angle));
106
 
                        ye = yMap.transform(y0 + mag*sin(angle));
107
 
                break;
108
 
 
109
 
                case Middle:
110
 
                        {
111
 
                        double dxh = 0.5*mag*cos(angle);
112
 
                        double dyh = 0.5*mag*sin(angle);
113
 
                        xs = xMap.transform(x0 - dxh);
114
 
                        ys = yMap.transform(y0 - dyh);
115
 
                        xe = xMap.transform(x0 + dxh);
116
 
                        ye = yMap.transform(y0 + dyh);
 
93
        if (d_style == XYAM){
 
94
                for (int i = from; i <= to; i++){
 
95
                        const double x0 = x(i);
 
96
                        const double y0 = y(i);
 
97
                        const double angle = vectorEnd->x(i);
 
98
                        const double mag = vectorEnd->y(i);
 
99
 
 
100
                        int xs = 0, ys = 0, xe = 0, ye = 0;
 
101
                        switch(d_position){
 
102
                                case Tail:
 
103
                                        xs = xMap.transform(x0);
 
104
                                        ys = yMap.transform(y0);
 
105
                                        xe = xMap.transform(x0 + mag*cos(angle));
 
106
                                        ye = yMap.transform(y0 + mag*sin(angle));
 
107
                                break;
 
108
 
 
109
                                case Middle:
 
110
                                        {
 
111
                                        double dxh = 0.5*mag*cos(angle);
 
112
                                        double dyh = 0.5*mag*sin(angle);
 
113
                                        xs = xMap.transform(x0 - dxh);
 
114
                                        ys = yMap.transform(y0 - dyh);
 
115
                                        xe = xMap.transform(x0 + dxh);
 
116
                                        ye = yMap.transform(y0 + dyh);
 
117
                                        }
 
118
                                break;
 
119
 
 
120
                                case Head:
 
121
                                        xs = xMap.transform(x0 - mag*cos(angle));
 
122
                                        ys = yMap.transform(y0 - mag*sin(angle));
 
123
                                        xe = xMap.transform(x0);
 
124
                                        ye = yMap.transform(y0);
 
125
                                break;
117
126
                        }
118
 
                break;
119
 
 
120
 
                case Head:
121
 
                        xs = xMap.transform(x0 - mag*cos(angle));
122
 
                        ys = yMap.transform(y0 - mag*sin(angle));
123
 
                        xe = xMap.transform(x0);
124
 
                        ye = yMap.transform(y0);
125
 
                break;
 
127
                        QwtPainter::drawLine(painter, xs, ys, xe, ye);
 
128
                        drawArrowHead(painter, xs, ys, xe, ye);
126
129
                }
127
 
        QwtPainter::drawLine(painter, xs, ys, xe, ye);
128
 
        drawArrowHead(painter, xs, ys, xe, ye);
129
 
        }
130
 
}
131
 
else
132
 
        {
133
 
        for (int i = from; i <= to; i++)
134
 
                {
135
 
                const int xs = xMap.transform(x(i));
136
 
                const int ys = yMap.transform(y(i));
137
 
                const int xe = xMap.transform(vectorEnd->x(i));
138
 
                const int ye = yMap.transform(vectorEnd->y(i));
139
 
                QwtPainter::drawLine(painter, xs, ys, xe, ye);
140
 
                drawArrowHead(painter, xs, ys, xe, ye);
 
130
        } else {
 
131
                for (int i = from; i <= to; i++){
 
132
                        const int xs = xMap.transform(x(i));
 
133
                        const int ys = yMap.transform(y(i));
 
134
                        const int xe = xMap.transform(vectorEnd->x(i));
 
135
                        const int ye = yMap.transform(vectorEnd->y(i));
 
136
                        QwtPainter::drawLine(painter, xs, ys, xe, ye);
 
137
                        drawArrowHead(painter, xs, ys, xe, ye);
141
138
                }
142
139
        }
143
140
}
167
164
 
168
165
double VectorCurve::theta(int x0, int y0, int x1, int y1) const
169
166
{
170
 
double t,pi=4*atan(-1.0);
171
 
if (x1==x0)
172
 
        {
173
 
        if (y0>y1)
174
 
                t=90;
175
 
        else
176
 
                t=270;
177
 
        }
178
 
else
179
 
        {
180
 
        t=atan2((y1-y0)*1.0,(x1-x0)*1.0)*180/pi;
181
 
        if (t<0)
182
 
                t=360+t;
183
 
        }
184
 
return t;
 
167
        double t = 0.0, pi = 4*atan(-1.0);
 
168
        if (x1 == x0){
 
169
                if (y0 > y1)
 
170
                        t = 90;
 
171
                else
 
172
                        t = 270;
 
173
        } else {
 
174
                t = atan2((y1 - y0)*1.0, (x1 - x0)*1.0)*180/pi;
 
175
                if (t < 0)
 
176
                        t = 360 + t;
 
177
        }
 
178
        return t;
185
179
}
186
180
 
187
181
void VectorCurve::setVectorEnd(const QString& xColName, const QString& yColName)
197
191
 
198
192
void VectorCurve::setVectorEnd(const QwtArray<double>&x, const QwtArray<double>&y)
199
193
{
200
 
    vectorEnd = new QwtArrayData(x, y);
 
194
        vectorEnd = new QwtArrayData(x, y);
201
195
}
202
196
 
203
197
double VectorCurve::width()
217
211
 
218
212
void VectorCurve::setColor(const QColor& c)
219
213
{
220
 
if (d_pen.color() != c)
221
 
        d_pen.setColor(c);
 
214
        if (d_pen.color() != c)
 
215
                d_pen.setColor(c);
222
216
}
223
217
 
224
218
void VectorCurve::setHeadLength(int l)
225
219
{
226
 
if (d_headLength != l)
227
 
        d_headLength = l;
 
220
        if (d_headLength != l)
 
221
                d_headLength = l;
228
222
}
229
223
 
230
224
void VectorCurve::setHeadAngle(int a)
231
225
{
232
 
if (d_headAngle != a)
233
 
        d_headAngle = a;
 
226
        if (d_headAngle != a)
 
227
                d_headAngle = a;
234
228
}
235
229
 
236
230
void VectorCurve::fillArrowHead(bool fill)
237
231
{
238
 
if (filledArrow != fill)
239
 
        filledArrow = fill;
 
232
        if (filledArrow != fill)
 
233
                filledArrow = fill;
240
234
}
241
235
 
242
236
QwtDoubleRect VectorCurve::boundingRect() const
243
237
{
244
 
QwtDoubleRect rect = QwtPlotCurve::boundingRect();
245
 
QwtDoubleRect vrect = vectorEnd->boundingRect();
246
 
 
247
 
if (d_style == XYXY){
248
 
        rect.setTop(QMIN((double)rect.top(), (double)vrect.top()));
249
 
        rect.setBottom(QMAX((double)rect.bottom(), (double)vrect.bottom()));
250
 
        rect.setLeft(QMIN((double)rect.left(), (double)vrect.left()));
251
 
        rect.setRight(QMAX((double)rect.right(), (double)vrect.right()));
252
 
} else {
253
 
        const double angle = vectorEnd->x(0);
254
 
        double mag = vectorEnd->y(0);
255
 
        switch(d_position)
256
 
                {
257
 
                case Tail:
258
 
                        rect.setTop(QMIN((double)rect.top(), (double)(rect.top()+mag*sin(angle))));
259
 
                        rect.setBottom(QMAX((double)rect.bottom(), (double)(rect.bottom()+mag*sin(angle))));
260
 
                        rect.setLeft(QMIN((double)rect.left(), (double)(rect.left()+mag*cos(angle))));
261
 
                        rect.setRight(QMAX((double)rect.right(), (double)(rect.right()+mag*cos(angle))));
262
 
                break;
263
 
 
264
 
                case Middle:
 
238
        QwtDoubleRect rect = QwtPlotCurve::boundingRect();
 
239
        if (!vectorEnd)
 
240
                return rect;
 
241
 
 
242
        QwtDoubleRect vrect = vectorEnd->boundingRect();
 
243
 
 
244
        if (d_style == XYXY){
 
245
                rect.setTop(QMIN((double)rect.top(), (double)vrect.top()));
 
246
                rect.setBottom(QMAX((double)rect.bottom(), (double)vrect.bottom()));
 
247
                rect.setLeft(QMIN((double)rect.left(), (double)vrect.left()));
 
248
                rect.setRight(QMAX((double)rect.right(), (double)vrect.right()));
 
249
        } else {
 
250
                const double angle = vectorEnd->x(0);
 
251
                double mag = vectorEnd->y(0);
 
252
                switch(d_position)
265
253
                        {
266
 
                        mag *= 0.5;
267
 
                        rect.setTop(QMIN((double)rect.top(), (double)(rect.top() - fabs(mag*sin(angle)))));
268
 
                        rect.setBottom(QMAX((double)rect.bottom(), (double)(rect.bottom() + fabs(mag*sin(angle)))));
269
 
                        rect.setLeft(QMIN((double)rect.left(), (double)(rect.left() - fabs(mag*cos(angle)))));
270
 
                        rect.setRight(QMAX((double)rect.right(), (double)(rect.right() + fabs(mag*cos(angle)))));
 
254
                        case Tail:
 
255
                                rect.setTop(QMIN((double)rect.top(), (double)(rect.top()+mag*sin(angle))));
 
256
                                rect.setBottom(QMAX((double)rect.bottom(), (double)(rect.bottom()+mag*sin(angle))));
 
257
                                rect.setLeft(QMIN((double)rect.left(), (double)(rect.left()+mag*cos(angle))));
 
258
                                rect.setRight(QMAX((double)rect.right(), (double)(rect.right()+mag*cos(angle))));
 
259
                        break;
 
260
 
 
261
                        case Middle:
 
262
                                {
 
263
                                mag *= 0.5;
 
264
                                rect.setTop(QMIN((double)rect.top(), (double)(rect.top() - fabs(mag*sin(angle)))));
 
265
                                rect.setBottom(QMAX((double)rect.bottom(), (double)(rect.bottom() + fabs(mag*sin(angle)))));
 
266
                                rect.setLeft(QMIN((double)rect.left(), (double)(rect.left() - fabs(mag*cos(angle)))));
 
267
                                rect.setRight(QMAX((double)rect.right(), (double)(rect.right() + fabs(mag*cos(angle)))));
 
268
                                }
 
269
                        break;
 
270
 
 
271
                        case Head:
 
272
                                rect.setTop(QMIN((double)rect.top(), (double)(rect.top() - mag*sin(angle))));
 
273
                                rect.setBottom(QMAX((double)rect.bottom(), (double)(rect.bottom() - mag*sin(angle))));
 
274
                                rect.setLeft(QMIN((double)rect.left(), (double)(rect.left() - mag*cos(angle))));
 
275
                                rect.setRight(QMAX((double)rect.right(), (double)(rect.right() - mag*cos(angle))));
 
276
                        break;
271
277
                        }
272
 
                break;
273
 
 
274
 
                case Head:
275
 
                        rect.setTop(QMIN((double)rect.top(), (double)(rect.top() - mag*sin(angle))));
276
 
                        rect.setBottom(QMAX((double)rect.bottom(), (double)(rect.bottom() - mag*sin(angle))));
277
 
                        rect.setLeft(QMIN((double)rect.left(), (double)(rect.left() - mag*cos(angle))));
278
 
                        rect.setRight(QMAX((double)rect.right(), (double)(rect.right() - mag*cos(angle))));
279
 
                break;
280
278
                }
281
 
        }
282
 
return rect;
 
279
        return rect;
283
280
}
284
281
 
285
282
void VectorCurve::updateColumnNames(const QString& oldName, const QString& newName, bool updateTableName)
313
310
    }
314
311
}
315
312
 
316
 
QString VectorCurve::plotAssociation()
 
313
QStringList VectorCurve::plotAssociation()
317
314
{
318
 
    QString base = d_x_column + "(X)," + title().text() + "(Y)," + d_end_x_a;
319
 
    if (d_style == XYAM)
320
 
        base += "(A)," + d_end_y_m + "(M)";
321
 
    else
322
 
        base += "(X)," + d_end_y_m + "(Y)";
323
 
 
324
 
    if (!d_labels_column.isEmpty())
325
 
        base += "," + d_labels_column + "(L)";
326
 
 
327
 
        return base;
 
315
        QStringList lst = QStringList() << d_x_column + "(X)" << title().text() + "(Y)";
 
316
        if (d_style == XYAM)
 
317
                lst << d_end_x_a + "(A)" << d_end_y_m + "(M)";
 
318
        else
 
319
                lst << d_end_x_a + "(X)" << d_end_y_m + "(Y)";
 
320
 
 
321
        if (!d_labels_column.isEmpty())
 
322
                lst << d_labels_column + "(L)";
 
323
 
 
324
        return lst;
328
325
}
329
326
 
330
327
bool VectorCurve::updateData(Table *t, const QString& colName)
339
336
 
340
337
void VectorCurve::loadData()
341
338
{
342
 
    if (!plot())
343
 
        return;
 
339
        if (!plot())
 
340
                return;
344
341
 
345
342
        int xcol = d_table->colIndex(d_x_column);
346
343
        int ycol = d_table->colIndex(title().text());
347
 
        int endXCol = d_table->colIndex(d_end_x_a);
348
 
        int endYCol = d_table->colIndex(d_end_y_m);
 
344
        int endXCol = d_table->colIndex(d_end_x_a.replace("_ ", "_"));
 
345
        int endYCol = d_table->colIndex(d_end_y_m.replace("_ ", "_"));
349
346
 
350
347
        int rows = abs(d_end_row - d_start_row) + 1;
351
 
    QVector<double> X(rows), Y(rows), X2(rows), Y2(rows);
352
 
    int size = 0;
353
 
    QLocale locale = ((Graph *)plot())->multiLayer()->locale();
 
348
        QVector<double> X(rows), Y(rows), X2(rows), Y2(rows);
 
349
        int size = 0;
 
350
        QLocale locale = ((Graph *)plot())->multiLayer()->locale();
354
351
        for (int i = d_start_row; i <= d_end_row; i++){
355
352
                QString xval = d_table->text(i, xcol);
356
353
                QString yval = d_table->text(i, ycol);
357
354
                QString xend = d_table->text(i, endXCol);
358
355
                QString yend = d_table->text(i, endYCol);
359
356
                if (!xval.isEmpty() && !yval.isEmpty() && !xend.isEmpty() && !yend.isEmpty()){
360
 
                    bool valid_data = true;
 
357
                        bool valid_data = true;
361
358
                        X[size] = locale.toDouble(xval, &valid_data);
362
359
                        if (!valid_data)
363
 
                continue;
364
 
            Y[size] = locale.toDouble(yval, &valid_data);
365
 
            if (!valid_data)
366
 
                continue;
 
360
                                continue;
 
361
                        Y[size] = locale.toDouble(yval, &valid_data);
 
362
                        if (!valid_data)
 
363
                                continue;
367
364
                        X2[size] = locale.toDouble(xend, &valid_data);
368
365
                        if (!valid_data)
369
 
                continue;
370
 
            Y2[size] = locale.toDouble(yend, &valid_data);
 
366
                                continue;
 
367
                        Y2[size] = locale.toDouble(yend, &valid_data);
371
368
                        if (valid_data)
372
 
                size++;
 
369
                                size++;
373
370
                }
374
371
        }
375
372
 
376
373
        if (!size)
377
374
                return;
378
375
 
379
 
    X.resize(size); Y.resize(size); X2.resize(size); Y2.resize(size);
 
376
        X.resize(size); Y.resize(size); X2.resize(size); Y2.resize(size);
380
377
        setData(X.data(), Y.data(), size);
381
 
        foreach(DataCurve *c, d_error_bars)
382
 
        c->setData(X.data(), Y.data(), size);
 
378
        foreach(ErrorBarsCurve *c, d_error_bars)
 
379
                c->setData(X.data(), Y.data(), size);
383
380
        setVectorEnd(X2, Y2);
384
381
}
385
382
 
386
383
VectorCurve::~VectorCurve()
387
384
{
388
 
delete vectorEnd;
 
385
        if (vectorEnd)
 
386
                delete vectorEnd;
389
387
}