MuseScore/libmscore/shape.h

109 lines
3.1 KiB
C
Raw Normal View History

2016-01-04 14:48:58 +01:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2016 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#ifndef __SHAPE_H__
#define __SHAPE_H__
namespace Ms {
#ifndef NDEBUG
// #define DEBUG_SHAPES // enable shape debugging
#endif
2016-01-04 14:48:58 +01:00
class Segment;
//---------------------------------------------------------
// ShapeElement
//---------------------------------------------------------
struct ShapeElement : public QRectF {
#ifndef NDEBUG
const char* text;
void dump() const;
ShapeElement(const QRectF& f, const char* t = 0) : QRectF(f), text(t) {}
#else
ShapeElement(const QRectF& f) : QRectF(f) {}
#endif
};
2016-01-04 14:48:58 +01:00
//---------------------------------------------------------
// Shape
//---------------------------------------------------------
class Shape : public std::vector<ShapeElement> {
// class Shape : std::vector<ShapeElement> {
2016-01-04 14:48:58 +01:00
public:
Shape() {}
#ifndef NDEBUG
Shape(const QRectF& r, const char* s = 0) { add(r, s); }
#else
2016-07-20 12:45:34 +02:00
Shape(const QRectF& r) { add(r); }
#endif
2016-02-06 22:03:43 +01:00
void add(const Shape& s) { insert(end(), s.begin(), s.end()); }
#ifndef NDEBUG
2018-01-16 13:38:17 +01:00
void add(const QRectF& r, const char* t = 0);
#else
2016-02-06 22:03:43 +01:00
void add(const QRectF& r) { push_back(r); }
#endif
2016-03-18 14:35:15 +01:00
void remove(const QRectF&);
void remove(const Shape&);
2018-02-01 10:37:12 +01:00
2016-01-04 14:48:58 +01:00
void translate(const QPointF&);
2018-02-01 10:37:12 +01:00
void translateX(qreal);
void translateY(qreal);
2016-01-04 14:48:58 +01:00
Shape translated(const QPointF&) const;
2018-02-01 10:37:12 +01:00
2016-01-04 14:48:58 +01:00
qreal minHorizontalDistance(const Shape&) const;
qreal minVerticalDistance(const Shape&) const;
qreal topDistance(const QPointF&) const;
qreal bottomDistance(const QPointF&) const;
2016-01-04 14:48:58 +01:00
qreal left() const;
qreal right() const;
qreal top() const;
qreal bottom() const;
size_t size() const { return std::vector<ShapeElement>::size(); }
bool empty() const { return std::vector<ShapeElement>::empty(); }
void clear() { std::vector<ShapeElement>::clear(); }
2016-01-04 14:48:58 +01:00
2016-12-29 13:42:55 +01:00
bool contains(const QPointF&) const;
bool intersects(const QRectF& rr) const;
void paint(QPainter&) const;
2016-12-29 13:42:55 +01:00
2017-08-02 18:19:08 +02:00
#ifndef NDEBUG
2016-01-04 14:48:58 +01:00
void dump(const char*) const;
#endif
};
2016-06-09 13:56:30 +02:00
//---------------------------------------------------------
// intersects
//---------------------------------------------------------
inline static bool intersects(qreal a, qreal b, qreal c, qreal d)
{
// return (a >= c && a < d) || (b >= c && b < d) || (a < c && b >= b);
// return (std::max(a,b) > std::min(c,d)) && (std::min(a,b) < std::max(c,d));
2016-08-17 12:52:35 +02:00
// if we can assume a <= b and c <= d
if (a == b || c == d) // zero height
return false;
2016-06-09 13:56:30 +02:00
return (b > c) && (a < d);
}
2016-01-04 14:48:58 +01:00
#ifdef DEBUG_SHAPES
extern void testShapes();
#endif
} // namespace Ms
#endif