MuseScore  3.4
Music composition and notation
shape.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2016 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __SHAPE_H__
14 #define __SHAPE_H__
15 
16 namespace Ms {
17 
18 #ifndef NDEBUG
19 // #define DEBUG_SHAPES // enable shape debugging
20 #endif
21 
22 class Segment;
23 
24 //---------------------------------------------------------
25 // ShapeElement
26 //---------------------------------------------------------
27 
28 struct ShapeElement : public QRectF {
29 #ifndef NDEBUG
30  const char* text;
31  void dump() const;
32  ShapeElement(const QRectF& f, const char* t = 0) : QRectF(f), text(t) {}
33 #else
34  ShapeElement(const QRectF& f) : QRectF(f) {}
35 #endif
36  };
37 
38 //---------------------------------------------------------
39 // Shape
40 //---------------------------------------------------------
41 
42 class Shape : public std::vector<ShapeElement> {
43 // class Shape : std::vector<ShapeElement> {
44  public:
46  SPACING_GENERAL = 0,
49  };
50 
51  Shape() {}
52 #ifndef NDEBUG
53  Shape(const QRectF& r, const char* s = 0) { add(r, s); }
54 #else
55  Shape(const QRectF& r) { add(r); }
56 #endif
57  void add(const Shape& s) { insert(end(), s.begin(), s.end()); }
58 #ifndef NDEBUG
59  void add(const QRectF& r, const char* t = 0);
60 #else
61  void add(const QRectF& r) { push_back(r); }
62 #endif
63  void remove(const QRectF&);
64  void remove(const Shape&);
65 
66  void addHorizontalSpacing(HorizontalSpacingType type, qreal left, qreal right);
67 
68  void translate(const QPointF&);
69  void translateX(qreal);
70  void translateY(qreal);
71  Shape translated(const QPointF&) const;
72 
73  qreal minHorizontalDistance(const Shape&) const;
74  qreal minVerticalDistance(const Shape&) const;
75  qreal topDistance(const QPointF&) const;
76  qreal bottomDistance(const QPointF&) const;
77  qreal left() const;
78  qreal right() const;
79  qreal top() const;
80  qreal bottom() const;
81 
82  size_t size() const { return std::vector<ShapeElement>::size(); }
83  bool empty() const { return std::vector<ShapeElement>::empty(); }
84  void clear() { std::vector<ShapeElement>::clear(); }
85 
86  bool contains(const QPointF&) const;
87  bool intersects(const QRectF& rr) const;
88  bool intersects(const Shape&) const;
89  void paint(QPainter&) const;
90 
91 #ifndef NDEBUG
92  void dump(const char*) const;
93 #endif
94  };
95 
96 //---------------------------------------------------------
97 // intersects
98 //---------------------------------------------------------
99 
100 inline static bool intersects(qreal a, qreal b, qreal c, qreal d)
101  {
102  // return (a >= c && a < d) || (b >= c && b < d) || (a < c && b >= b);
103  // return (std::max(a,b) > std::min(c,d)) && (std::min(a,b) < std::max(c,d));
104  // if we can assume a <= b and c <= d
105  if (a == b || c == d) // zero height
106  return false;
107  return (b > c) && (a < d);
108  }
109 
110 #ifdef DEBUG_SHAPES
111 extern void testShapes();
112 #endif
113 
114 } // namespace Ms
115 
116 #endif
117 
bool empty() const
Definition: shape.h:83
void clear()
Definition: shape.h:84
void add(const Shape &s)
Definition: shape.h:57
Shape()
Definition: shape.h:51
void dump() const
Definition: shape.cpp:301
Definition: shape.h:48
Definition: aeolus.cpp:26
ShapeElement(const QRectF &f, const char *t=0)
Definition: shape.h:32
Shape(const QRectF &r, const char *s=0)
Definition: shape.h:53
const char * text
Definition: shape.h:30
size_t size() const
Definition: shape.h:82
HorizontalSpacingType
Definition: shape.h:45
Definition: shape.h:28
Definition: shape.h:42
Definition: shape.h:47