qwt_double_rect.h 11.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

/*! \file */
#ifndef QWT_DOUBLE_RECT_H
#define QWT_DOUBLE_RECT_H 1

#include "qwt_global.h"
#include "qwt_array.h"

#if QT_VERSION >= 0x040000

#include <QPointF>
#include <QSizeF>
#include <QRectF>

23
/*!
pixhawk's avatar
pixhawk committed
24 25 26 27 28 29 30
  \typedef QPointF QwtDoublePoint
  \brief This is a typedef, see Trolltech Documentation for QPointF
         in QT assistant 4.x. As soon as Qt3 compatibility is dropped
         this typedef will disappear.
*/
typedef QPointF QwtDoublePoint;

31
/*!
pixhawk's avatar
pixhawk committed
32 33 34 35 36 37 38
   \typedef QSizeF QwtDoubleSize
   \brief This is a typedef, see Trolltech Documentation for QSizeF
          in QT assistant 4.x. As soon as Qt3 compatibility is dropped
         this typedef will disappear.
*/
typedef QSizeF QwtDoubleSize;

39
/*!
pixhawk's avatar
pixhawk committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
   \typedef QRectF QwtDoubleRect
   \brief This is a typedef, see Trolltech Documentation for QRectF
          in QT assistant 4.x. As soon as Qt3 compatibility is dropped
         this typedef will disappear.
*/
typedef QRectF QwtDoubleRect;

#else

#include <qpoint.h>
#include <qsize.h>
#include <qrect.h>

/*!
  \brief The QwtDoublePoint class defines a point in double coordinates
*/

class QWT_EXPORT QwtDoublePoint
{
public:
    QwtDoublePoint();
    QwtDoublePoint(double x, double y);
    QwtDoublePoint(const QPoint &);

    QPoint toPoint() const;

    bool isNull()    const;

    double x() const;
    double y() const;

    double &rx();
    double &ry();

    void setX(double x);
    void setY(double y);

    bool operator==(const QwtDoublePoint &) const;
    bool operator!=(const QwtDoublePoint &) const;

    const QwtDoublePoint operator-() const;
    const QwtDoublePoint operator+(const QwtDoublePoint &) const;
    const QwtDoublePoint operator-(const QwtDoublePoint &) const;
    const QwtDoublePoint operator*(double) const;
    const QwtDoublePoint operator/(double) const;

    QwtDoublePoint &operator+=(const QwtDoublePoint &);
    QwtDoublePoint &operator-=(const QwtDoublePoint &);
    QwtDoublePoint &operator*=(double);
    QwtDoublePoint &operator/=(double);

private:
    double d_x;
    double d_y;
};

/*!
  The QwtDoubleSize class defines a size in double coordinates
*/

class QWT_EXPORT QwtDoubleSize
{
public:
    QwtDoubleSize();
    QwtDoubleSize(double width, double height);
    QwtDoubleSize(const QSize &);

    bool isNull() const;
    bool isEmpty() const;
    bool isValid() const;

    double width() const;
    double height() const;
    void setWidth( double w );
    void setHeight( double h );
    void transpose();

    QwtDoubleSize expandedTo(const QwtDoubleSize &) const;
    QwtDoubleSize boundedTo(const QwtDoubleSize &) const;

    bool operator==(const QwtDoubleSize &) const;
    bool operator!=(const QwtDoubleSize &) const;

    const QwtDoubleSize operator+(const QwtDoubleSize &) const;
    const QwtDoubleSize operator-(const QwtDoubleSize &) const;
    const QwtDoubleSize operator*(double) const;
    const QwtDoubleSize operator/(double) const;

    QwtDoubleSize &operator+=(const QwtDoubleSize &);
    QwtDoubleSize &operator-=(const QwtDoubleSize &);
    QwtDoubleSize &operator*=(double c);
    QwtDoubleSize &operator/=(double c);

private:
    double d_width;
    double d_height;
};

/*!
  The QwtDoubleRect class defines a size in double coordinates.
*/

142
class QWT_EXPORT QwtDoubleRect
pixhawk's avatar
pixhawk committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
public:
    QwtDoubleRect();
    QwtDoubleRect(double left, double top, double width, double height);
    QwtDoubleRect(const QwtDoublePoint&, const QwtDoubleSize &);

    QwtDoubleRect(const QRect &);
    QRect toRect() const;

    bool isNull()    const;
    bool isEmpty()   const;
    bool isValid()   const;

    QwtDoubleRect normalized() const;

    double x()  const;
    double y()  const;

    double left()  const;
    double right()  const;
    double top()  const;
    double bottom()  const;

    void setX(double);
    void setY(double);

    void setLeft(double);
    void setRight(double);
    void setTop(double);
    void setBottom(double);

    QwtDoublePoint center()  const;

    void moveLeft(double x);
    void moveRight(double x);
    void moveTop(double y );
    void moveBottom(double y );
    void moveTo(double x, double y);
    void moveTo(const QwtDoublePoint &);
    void moveBy(double dx, double dy);
    void moveCenter(const QwtDoublePoint &);
    void moveCenter(double dx, double dy);

    void setRect(double x1, double x2, double width, double height);

    double width()   const;
    double height()  const;
    QwtDoubleSize size() const;

    void setWidth(double w );
    void setHeight(double h );
    void setSize(const QwtDoubleSize &);

    QwtDoubleRect  operator|(const QwtDoubleRect &r) const;
    QwtDoubleRect  operator&(const QwtDoubleRect &r) const;
    QwtDoubleRect &operator|=(const QwtDoubleRect &r);
    QwtDoubleRect &operator&=(const QwtDoubleRect &r);
    bool operator==( const QwtDoubleRect &) const;
    bool operator!=( const QwtDoubleRect &) const;

    bool contains(const QwtDoublePoint &p, bool proper = false) const;
204
    bool contains(double x, double y, bool proper = false) const;
pixhawk's avatar
pixhawk committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    bool contains(const QwtDoubleRect &r, bool proper=false) const;

    QwtDoubleRect unite(const QwtDoubleRect &) const;
    QwtDoubleRect intersect(const QwtDoubleRect &) const;
    bool intersects(const QwtDoubleRect &) const;

    QwtDoublePoint bottomRight() const;
    QwtDoublePoint topRight() const;
    QwtDoublePoint topLeft() const;
    QwtDoublePoint bottomLeft() const;

private:
    double d_left;
    double d_right;
    double d_top;
    double d_bottom;
};

223
/*!
pixhawk's avatar
pixhawk committed
224 225
    Returns true if the point is null; otherwise returns false.

226
    A point is considered to be null if both the x- and y-coordinates
pixhawk's avatar
pixhawk committed
227 228 229
    are equal to zero.
*/
inline bool QwtDoublePoint::isNull() const
230 231
{
    return d_x == 0.0 && d_y == 0.0;
pixhawk's avatar
pixhawk committed
232 233 234 235
}

//! Returns the x-coordinate of the point.
inline double QwtDoublePoint::x() const
236 237
{
    return d_x;
pixhawk's avatar
pixhawk committed
238 239 240 241
}

//! Returns the y-coordinate of the point.
inline double QwtDoublePoint::y() const
242 243
{
    return d_y;
pixhawk's avatar
pixhawk committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

//! Returns a reference to the x-coordinate of the point.
inline double &QwtDoublePoint::rx()
{
    return d_x;
}

//! Returns a reference to the y-coordinate of the point.
inline double &QwtDoublePoint::ry()
{
    return d_y;
}

//! Sets the x-coordinate of the point to the value specified by x.
inline void QwtDoublePoint::setX(double x)
260 261
{
    d_x = x;
pixhawk's avatar
pixhawk committed
262 263 264 265
}

//! Sets the y-coordinate of the point to the value specified by y.
inline void QwtDoublePoint::setY(double y)
266 267
{
    d_y = y;
pixhawk's avatar
pixhawk committed
268 269 270
}

/*!
271
   Rounds the coordinates of this point to the nearest integer and
pixhawk's avatar
pixhawk committed
272 273 274 275 276 277 278 279
   returns a QPoint with these rounded coordinates.
*/
inline QPoint QwtDoublePoint::toPoint() const
{
    return QPoint(qRound(d_x), qRound(d_y));
}

/*!
280
  Returns true if the width is 0 and the height is 0;
pixhawk's avatar
pixhawk committed
281 282 283
  otherwise returns false.
*/
inline bool QwtDoubleSize::isNull() const
284 285
{
    return d_width == 0.0 && d_height == 0.0;
pixhawk's avatar
pixhawk committed
286 287
}

288 289 290
/*!
  Returns true if the width is <= 0.0 or the height is <= 0.0,
  otherwise false.
pixhawk's avatar
pixhawk committed
291 292
*/
inline bool QwtDoubleSize::isEmpty() const
293 294
{
    return d_width <= 0.0 || d_height <= 0.0;
pixhawk's avatar
pixhawk committed
295 296 297
}

/*!
298
  Returns true if the width is equal to or greater than 0.0 and the height
pixhawk's avatar
pixhawk committed
299 300 301
  is equal to or greater than 0.0; otherwise returns false.
*/
inline bool QwtDoubleSize::isValid() const
302 303
{
    return d_width >= 0.0 && d_height >= 0.0;
pixhawk's avatar
pixhawk committed
304 305
}

306
//! Returns the width.
pixhawk's avatar
pixhawk committed
307
inline double QwtDoubleSize::width() const
308 309
{
    return d_width;
pixhawk's avatar
pixhawk committed
310 311
}

312
//! Returns the height.
pixhawk's avatar
pixhawk committed
313
inline double QwtDoubleSize::height() const
314 315
{
    return d_height;
pixhawk's avatar
pixhawk committed
316 317
}

318
//! Sets the width to width.
pixhawk's avatar
pixhawk committed
319
inline void QwtDoubleSize::setWidth(double width)
320 321
{
    d_width = width;
pixhawk's avatar
pixhawk committed
322 323
}

324
//! Sets the height to height.
pixhawk's avatar
pixhawk committed
325
inline void QwtDoubleSize::setHeight(double height)
326 327
{
    d_height = height;
pixhawk's avatar
pixhawk committed
328 329 330 331 332 333 334 335 336 337
}

/*!
    Returns true if the rectangle is a null rectangle; otherwise returns false.
    A null rectangle has both the width and the height set to 0.
    A null rectangle is also empty and invalid.

    \sa QwtDoubleRect::isEmpty, QwtDoubleRect::isValid
*/
inline bool QwtDoubleRect::isNull() const
338
{
pixhawk's avatar
pixhawk committed
339 340 341 342 343 344 345 346 347 348 349
    return d_right == d_left && d_bottom == d_top;
}

/*!
    Returns true if the rectangle is empty; otherwise returns false.
    An empty rectangle has a width() <= 0 or height() <= 0.
    An empty rectangle is not valid. isEmpty() == !isValid()

    \sa QwtDoubleRect::isNull, QwtDoubleRect::isValid
*/
inline bool QwtDoubleRect::isEmpty() const
350 351
{
    return d_left >= d_right || d_top >= d_bottom;
pixhawk's avatar
pixhawk committed
352 353 354 355 356
}

/*!
    Returns true if the rectangle is valid; otherwise returns false.
    A valid rectangle has a width() > 0 and height() > 0.
357
    Note that non-trivial operations like intersections are not defined
pixhawk's avatar
pixhawk committed
358 359 360 361 362
    for invalid rectangles.  isValid() == !isEmpty()

    \sa isNull(), isEmpty(), and normalized().
*/
inline bool QwtDoubleRect::isValid() const
363 364
{
    return d_left < d_right && d_top < d_bottom;
pixhawk's avatar
pixhawk committed
365 366 367 368
}

//! Returns x
inline double QwtDoubleRect::x() const
369 370
{
    return d_left;
pixhawk's avatar
pixhawk committed
371 372 373 374
}

//! Returns y
inline double QwtDoubleRect::y() const
375 376
{
    return d_top;
pixhawk's avatar
pixhawk committed
377 378 379 380
}

//! Returns left
inline double QwtDoubleRect::left() const
381 382
{
    return d_left;
pixhawk's avatar
pixhawk committed
383 384 385 386
}

//! Returns right
inline double QwtDoubleRect::right() const
387 388
{
    return d_right;
pixhawk's avatar
pixhawk committed
389 390 391 392
}

//! Returns top
inline double QwtDoubleRect::top() const
393 394
{
    return d_top;
pixhawk's avatar
pixhawk committed
395 396 397 398
}

//! Returns bottom
inline double QwtDoubleRect::bottom() const
399 400
{
    return d_bottom;
pixhawk's avatar
pixhawk committed
401 402
}

403
//! Set left
pixhawk's avatar
pixhawk committed
404
inline void QwtDoubleRect::setX(double x)
405
{
pixhawk's avatar
pixhawk committed
406 407 408
    d_left = x;
}

409
//! Set left
pixhawk's avatar
pixhawk committed
410
inline void QwtDoubleRect::setY(double y)
411
{
pixhawk's avatar
pixhawk committed
412 413 414
    d_top = y;
}

415
//! Set left
pixhawk's avatar
pixhawk committed
416
inline void QwtDoubleRect::setLeft(double x)
417
{
pixhawk's avatar
pixhawk committed
418 419 420
    d_left = x;
}

421
//! Set right
pixhawk's avatar
pixhawk committed
422
inline void QwtDoubleRect::setRight(double x)
423
{
pixhawk's avatar
pixhawk committed
424 425 426
    d_right = x;
}

427
//! Set top
pixhawk's avatar
pixhawk committed
428
inline void QwtDoubleRect::setTop(double y)
429
{
pixhawk's avatar
pixhawk committed
430 431 432
    d_top = y;
}

433
//! Set bottom
pixhawk's avatar
pixhawk committed
434
inline void QwtDoubleRect::setBottom(double y)
435
{
pixhawk's avatar
pixhawk committed
436 437 438 439 440
    d_bottom = y;
}

//! Returns the width
inline double QwtDoubleRect::width() const
441 442
{
    return  d_right - d_left;
pixhawk's avatar
pixhawk committed
443 444 445 446
}

//! Returns the height
inline double QwtDoubleRect::height() const
447 448
{
    return  d_bottom - d_top;
pixhawk's avatar
pixhawk committed
449 450 451 452
}

//! Returns the size
inline QwtDoubleSize QwtDoubleRect::size() const
453
{
pixhawk's avatar
pixhawk committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    return QwtDoubleSize(width(), height());
}

//! Set the width, by right = left + w;
inline void QwtDoubleRect::setWidth(double w)
{
    d_right = d_left + w;
}

//! Set the height, by bottom = top + h;
inline void QwtDoubleRect::setHeight(double h)
{
    d_bottom = d_top + h;
}

469 470
/*!
    Moves the top left corner of the rectangle to p,
pixhawk's avatar
pixhawk committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    without changing the rectangles size.
*/
inline void QwtDoubleRect::moveTo(const QwtDoublePoint &p)
{
    moveTo(p.x(), p.y());
}

inline QwtDoublePoint QwtDoubleRect::bottomRight() const
{
    return QwtDoublePoint(bottom(), right());
}

inline QwtDoublePoint QwtDoubleRect::topRight() const
{
    return QwtDoublePoint(top(), right());
}

inline QwtDoublePoint QwtDoubleRect::topLeft() const
{
    return QwtDoublePoint(top(), left());
}

inline QwtDoublePoint QwtDoubleRect::bottomLeft() const
{
    return QwtDoublePoint(bottom(), left());
}


#endif // QT_VERSION < 0x040000

#endif // QWT_DOUBLE_RECT_H