Canorus  0.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scoreviewport.h
Go to the documentation of this file.
1 
8 #ifndef SCOREVIEWPORT_H_
9 #define SCOREVIEWPORT_H_
10 
11 #include <QList>
12 #include <QPen>
13 #include <QBrush>
14 #include <QRect>
15 #include <QLineEdit>
16 #include <QTimer>
17 
18 #include "widgets/viewport.h"
19 #include "core/kdtree.h"
20 #include "core/note.h"
21 
22 class QScrollBar;
23 class QMouseEvent;
24 class QWheelEvent;
25 class QTimer;
26 class QGridLayout;
27 
28 class CADrawable;
30 class CADrawableContext;
31 class CADrawableNote;
32 class CAMusElement;
33 class CAContext;
34 class CASheet;
35 class CAStaff;
36 class CALyricsContext;
37 
38 class CATextEdit : public QLineEdit {
39 Q_OBJECT
40 
41 public:
42  CATextEdit( QWidget *parent=0 );
43  ~CATextEdit();
44 
45 signals:
46  void CAKeyPressEvent( QKeyEvent * );
47 
48 public slots:
49  void keyPressEvent( QKeyEvent * );
50 };
51 
52 class CAScoreViewPort : public CAViewPort {
53 Q_OBJECT
54 
55 public:
60  };
61 
63  // Basic methods //
65  CAScoreViewPort(QWidget *parent=0);
66  CAScoreViewPort(CASheet *sheet, QWidget *parent=0);
67  virtual ~CAScoreViewPort();
69  CAScoreViewPort *clone(QWidget *parent);
70  inline CASheet *sheet() { return _sheet; }
71  inline void setSheet( CASheet *sheet ) { _sheet = sheet; }
72 
74  // Addition, removal of drawable elements //
76  void addMElement(CADrawableMusElement *elt, bool select=false);
77  void addCElement(CADrawableContext *elt, bool select=false);
78  CAMusElement *removeMElement(double x, double y);
79 
83 
85  // Selection //
87  inline QList<CADrawableMusElement*>& selection() { return _selection; };
88  QList<CAMusElement*> musElementSelection();
89  QList<CADrawableMusElement*> musElementsAt(double x, double y);
90  CADrawableContext *selectCElement(double x, double y);
93  inline QPoint lastMousePressCoords() { return _lastMousePressCoords; }
94  void setLastMousePressCoordsAfter(const QList<CAMusElement*> list);
95 
98 
99  void selectAll();
100  void selectAllCurBar();
101  void selectAllCurContext();
102  void invertSelection();
103  inline void clearSelection() { _selection.clear(); emit selectionChanged(); }
104  inline bool removeFromSelection(CADrawableMusElement *elt) { return _selection.removeAll(elt); emit selectionChanged(); }
105 
106  void addToSelection(CADrawableMusElement *elt, bool triggerSignal=true );
107  void addToSelection( const QList<CADrawableMusElement*> list, bool selectableOnly=true );
109  void addToSelection(const QList<CAMusElement *> elts);
110 
111  CADrawableMusElement* selectNextMusElement( bool append=false );
112  CADrawableMusElement* selectPrevMusElement( bool append=false );
115 
116  inline const QList<QRect> &selectionRegionList() const { return _selectionRegionList; }
117  inline void addSelectionRegion(QRect r) { _selectionRegionList << r; }
118  inline void removeSelectionRegion(QRect r) { _selectionRegionList.removeAll(r); }
121 
123  // Music elements and contexts query, space calculation and access //
127  QList<CADrawableContext*> findContextsInRegion(QRect &reg);
128  CADrawableMusElement *nearestLeftElement(double x, double y, CADrawableContext* context=0);
129  CADrawableMusElement *nearestLeftElement(double x, double y, CAVoice *voice);
130  CADrawableMusElement *nearestRightElement(double x, double y, CADrawableContext* context=0);
131  CADrawableMusElement *nearestRightElement(double x, double y, CAVoice *voice);
132  int coordsToTime( double x );
133  double timeToCoords( int time );
134  double timeToCoordsSimpleVersion( int time );
135 
136  CADrawableContext *nearestUpContext(double x, double y);
137  CADrawableContext *nearestDownContext(double x, double y);
138 
139  int calculateTime(double x, double y);
140 
141  CAContext *contextCollision(double x, double y);
142 
144  // Scrollbars //
146  inline void setManualScroll(bool scroll) { _allowManualScroll = scroll; }
147  inline bool manualScroll() { return _allowManualScroll; }
148 
149  void checkScrollBars();
150 
153 
155  // Scene appearance, properties and actions //
157  void rebuild();
158  inline const int drawableWidth() { return _canvas->width(); }
159  inline const int drawableHeight() { return _canvas->height(); }
160 
161  void setWorldX(int x, bool animate=false, bool force=false);
162  void setWorldY(int y, bool animate=false, bool force=false);
163  void setWorldWidth(int w, bool force=false);
164  void setWorldHeight(int h, bool force=false);
165 
166  inline const int worldX() { return _worldX; }
167  inline const int worldY() { return _worldY; }
168  inline const int worldWidth() { return _worldW; }
169  inline const int worldHeight() { return _worldH; }
170  inline const QRect worldCoords() { return QRect(worldX(), worldY(), worldWidth(), worldHeight()); }
171 
172  inline const float zoom() { return _zoom; }
173 
174  void setWorldCoords(const QRect r, bool animate=false, bool force=false);
175  void setWorldCoords(double x, double y, double w, double h, bool animate=false, bool force=false) { setWorldCoords( QRect(x,y,w,h), animate, force); }
176 
177  void setCenterCoords(double x, double y, bool animate=false, bool force=false);
178 
179  void setZoom(float z, double x=0, double y=0, bool animate=false, bool force = false);
180  void setZoom(float z, QPoint p, bool animate=false, bool force = false) { setZoom(z, p.x(), p.y(), animate, force); }
181 
182  void zoomToSelection(bool animate=false, bool force=false);
183  void zoomToWidth(bool animate=false, bool force=false);
184  void zoomToHeight(bool animate=false, bool force=false);
185  void zoomToFit(bool animate=false, bool force=false);
186 
187  void setBorder(const QPen pen);
188  void unsetBorder();
189  inline QPen border() { return _borderPen; }
190  inline QColor backgroundColor() { return _backgroundColor; }
191  inline void setBackgroundColor( const QColor c ) { _backgroundColor = c; }
192  inline QColor foregroundColor() { return _foregroundColor; }
193  inline void setForegroundColor( const QColor c ) { _foregroundColor = c; }
194  inline QColor selectionColor() { return _selectionColor; }
195  inline void setSelectionColor( const QColor c ) { _selectionColor = c; }
196  inline QColor selectionAreaColor() { return _selectionAreaColor; }
197  inline void setSelectionAreaColor( const QColor c ) { _selectionAreaColor = c; }
198  inline QColor selectedContextColor() { return _selectedContextColor; }
199  inline void setSelectedContextColor( const QColor c ) { _selectedContextColor = c; }
200  inline QColor hiddenElementsColor() { return _hiddenElementsColor; }
201  inline void setHiddenElementsColor( const QColor c ) { _hiddenElementsColor = c; }
202  inline QColor disabledElementsColor() { return _disabledElementsColor; }
203  inline void setDisabledElementsColor( const QColor c ) { _disabledElementsColor = c; }
204 
205  inline bool playing() { return _playing; }
206  inline void setPlaying(bool playing) { _playing = playing; }
207 
208  inline void setRepaintArea(QRect *area) { _repaintArea = area; }
209  inline void clearRepaintArea() { if (_repaintArea) delete _repaintArea; _repaintArea=0; }
210 
211  inline CAVoice *selectedVoice() { return _selectedVoice; }
213 
214  inline bool shadowNoteVisible() { return _shadowNoteVisible; }
215  inline void setShadowNoteVisible(bool visible) { _shadowNoteVisible = visible; setShadowNoteVisibleOnLeave(visible); }
216 
217  inline bool drawShadowNoteAccs() { return _drawShadowNoteAccs; }
218  inline void setDrawShadowNoteAccs(bool draw) { _drawShadowNoteAccs = draw; }
219 
220  inline int shadowNoteAccs() { return _shadowNoteAccs; }
221  inline void setShadowNoteAccs(int accs) { _shadowNoteAccs = accs; }
222 
224 
226  inline CATextEdit *textEdit() { return _textEdit; }
227  void removeTextEdit();
228  inline bool textEditVisible() { return _textEditVisible; }
229 
231  void setNoteNameVisible( bool v ) { _noteNameVisible = v; }
232 
233  QString noteName() { return _noteName; }
234  void setNoteName( QString n ) { _noteName = n; }
235 
236  void updateHelpers(); // method for updating shadow notes, syllable edits and other post-engrave elements coordinates and sizes when zoom level is changed etc.
237 
238 private slots:
239  void mousePressEvent(QMouseEvent *e);
240  void mouseMoveEvent(QMouseEvent *e);
241  void mouseReleaseEvent(QMouseEvent *e);
242  void wheelEvent(QWheelEvent *e);
243  void keyPressEvent(QKeyEvent *e);
244 
245  void HScrollBarEvent(int val);
246  void VScrollBarEvent(int val);
247 
248  void resizeEvent(QResizeEvent *e);
249  void paintEvent(QPaintEvent *p);
250  void leaveEvent(QEvent *e);
251  void enterEvent(QEvent *e);
253  void on_clickTimer_timeout();
254 
255 signals:
256  void CATripleClickEvent( QMouseEvent *e, QPoint p );
257  void CADoubleClickEvent( QMouseEvent *e, QPoint p );
258  void CAMousePressEvent( QMouseEvent *e, QPoint p );
259  void CAMouseReleaseEvent( QMouseEvent *e, QPoint p );
260  void CAMouseMoveEvent( QMouseEvent *e, QPoint p );
261  void CAWheelEvent( QWheelEvent *e, QPoint p );
262  void CAKeyPressEvent( QKeyEvent *e );
263  void selectionChanged();
264 
265 private:
266  void initScoreViewPort( CASheet *s );
267  inline void clearMElements() { _drawableMList.clear(true); }
268  inline void clearCElements() { _drawableCList.clear(true); }
269  inline bool isSelected(CADrawableMusElement *elt) { return (_selection.contains(elt)); }
270 
272  // Core Widgets //
274  QGridLayout *_layout; // Grid layout for placing the scrollbars at the right and the bottom.
275  QWidget *_canvas; // Virtual canvas which represents the size of the drawable area. All its signals are forwarded to CAViewPort.
276  QScrollBar *_hScrollBar, *_vScrollBar; // Horizontal/vertical scrollbars
277 
279  // General properties //
281  CAKDTree<CADrawableMusElement*> _drawableMList; // The list of music elements stored in a tree for faster lookup and other operations. Every viewport has its own list of drawable elements and drawable objects themselves!
282  CAKDTree<CADrawableContext*> _drawableCList; // The list of context drawable elements (staffs, lyrics etc.). Every viewport has its own list of drawable elements and drawable objects themselves!
283  CASheet *_sheet; // Pointer to the CASheet which the viewport represents.
284 
285  QList<CADrawableMusElement *> _selection; // The set of elements being selected.
286  CADrawableContext *_currentContext; // The pointer to the currently active context (staff, lyrics).
287 
288  static const int RIGHT_EXTRA_SPACE; // Extra space at the right end to insert new music
289  static const int BOTTOM_EXTRA_SPACE; // Extra space at the bottom end to insert new music
290  template <typename T> int getMaxXExtended(CAKDTree<T> &v); // Make the viewable World a little bigger (stuffed) to make inserting at the end easier
291  template <typename T> int getMaxYExtended(CAKDTree<T> &v); // Make the viewable World a little bigger (stuffed) to make inserting below easies
292 
293  double _worldX, _worldY, _worldW, _worldH; // Absolute world coordinates of the area the viewport is currently showing.
294  QPoint _lastMousePressCoords; // Used in multiple selection - coordinates of the upper-left point of the rectangle the user drags in world coordinates
295  inline void setLastMousePressCoords( QPoint p ) { _lastMousePressCoords = p; }
296  float _zoom; // Zoom level of the viewport (1.0 = 100%, 1.5 = 150% etc.).
297 
298  CAVoice *_selectedVoice; // Voice to be drawn normal colors, others are shaded
299 
301  // Helpers //
303  // Shadow note
304  bool _shadowNoteVisible; // Should the shadow notes be rendered or not
305 
306  bool _shadowNoteVisibleOnLeave; // When you leave the viewport, shadow note is always turned off. This property holds the value, if shadow note was enabled before you left the viewport.
309 
310  int _shadowNoteAccs; // Number of accidentals - 0 - natural, 1 - sharp, -1 flat
311  bool _drawShadowNoteAccs; // Draw shadow note accs?
312  QList<CANote*> _shadowNote; // List of all shadow notes - one shadow note per drawable staff
313  QList<CADrawableNote*> _shadowDrawableNote; // List of drawable shadow notes
314 
315  // QLineEdit for editing or creating a lyrics syllable
317  inline void setTextEdit( CATextEdit *e ) { _textEdit = e; }
319  inline QRect textEditGeometry() { return _textEditGeometry; }
320  inline void setTextEditGeometry( const QRect r ) { _textEditGeometry = r; }
322  inline void setTextEditVisible(bool v) { _textEditVisible = v; }
324  CADrawable::CADirection _resizeDirection; // Is the current scalable music element in drag-drop resizing mode?
325 
326  // Selection regions
327  QList<QRect> _selectionRegionList;
328  void drawSelectionRegion( QPainter *p, CADrawSettings s );
329 
331  // Appearance //
333  bool _drawBorder; // Should the border be drawn or not.
334  QRect *_repaintArea; // Area to be repainted on paintEvent().
335  QPen _borderPen; // Pen which the border is drawn by.
336  QColor _backgroundColor; // Color which the background is filled.
337  QColor _foregroundColor; // Color which the music elements are painted.
338  QColor _selectionColor; // Color which the selected music elements are painted.
339  QColor _selectionAreaColor; // Color which the selection area background is filled.
340  QColor _selectedContextColor; // Color which the current context is painted.
341  QColor _disabledElementsColor; // Color which the elements in non-selected voice are painted.
342  QColor _hiddenElementsColor; // Color which the invisible elements are painted in current-voice-only mode.
343  bool _noteNameVisible; // Is the written note name visible
344  QString _noteName; // Name of the note to be inserted. eg. c', Des,
345 
347  // Animation //
349  QTimer *_animationTimer; // Timer used to animate scroll/zoom behaviour.
350  static const int ANIMATION_STEPS; // Number of steps used in animation
351  int _animationStep; // Current step in the animation
352  double _targetWorldX, _targetWorldY, _targetWorldW, _targetWorldH; // Absolute world coordinates of the area the viewport is currently showing.
353  float _targetZoom; // Zoom level of the viewport (1.0 = 100%, 1.5 = 150% etc.).
354 
355  void startAnimationTimer();
356 
358  // Widgets behaviour //
360  CAScrollBarVisibility _scrollBarVisible; // Are the scrollbars always visible/never/if needed. Use CAViewPort::ScrollBarAlwaysVisible, CAViewPort::ScrollBarAlwaysHidden or CAViewPort::ScrollBarShowIfNeeded.
361  bool _allowManualScroll; // Does the scrollbars actually react on user actions - sometimes we only want the scrollbars to show the current location of the score and don't do anything
362 
364  // Internal properties //
366  double _oldWorldX, _oldWorldY, _oldWorldW, _oldWorldH; // Old coordinates used before the repaint. This is needed so only the new part of the viewport gets repainted when panning.
367  bool _playing; // Set to on, when in Playback mode
368  QTimer *_clickTimer; // Used for measuring doubleClick and tripleClick
369  int _numberOfClicks; // Used for measuring doubleClick and tripleClick
370 
371  double _xCursor, _yCursor; // Mouse cursor position in absolute world coords.
372  bool _holdRepaint; // Flag to prevent multiple repaintings.
373  bool _checkScrollBarsDeadLock; // Flag to prevent recursive checkScrollBars() calls.
374  bool _hScrollBarDeadLock; // Flag to prevent recursive scrollbar calls when its value is manually changed.
375  bool _vScrollBarDeadLock; // Flag to prevent recursive scrollbar calls when its value is manually changed.
376 };
377 
378 #endif /*SCOREVIEWPORT_H_*/