Canorus  0.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
/home/iurt/rpm/BUILD/canorus-0.7.R1002/src/widgets/scoreviewport.h
Go to the documentation of this file.
00001 
00008 #ifndef SCOREVIEWPORT_H_
00009 #define SCOREVIEWPORT_H_
00010 
00011 #include <QList>
00012 #include <QPen>
00013 #include <QBrush>
00014 #include <QRect>
00015 #include <QLineEdit>
00016 #include <QTimer>
00017 
00018 #include "widgets/viewport.h"
00019 #include "core/kdtree.h"
00020 #include "core/note.h"
00021 
00022 class QScrollBar;
00023 class QMouseEvent;
00024 class QWheelEvent;
00025 class QTimer;
00026 class QGridLayout;
00027 
00028 class CADrawable;
00029 class CADrawableMusElement;
00030 class CADrawableContext;
00031 class CADrawableNote;
00032 class CAMusElement;
00033 class CAContext;
00034 class CASheet;
00035 class CAStaff;
00036 class CALyricsContext;
00037 
00038 class CATextEdit : public QLineEdit {
00039 Q_OBJECT
00040 
00041 public:
00042         CATextEdit( QWidget *parent=0 );
00043         ~CATextEdit();
00044 
00045 signals:
00046         void CAKeyPressEvent( QKeyEvent * );
00047 
00048 public slots:
00049         void keyPressEvent( QKeyEvent * );
00050 };
00051 
00052 class CAScoreViewPort : public CAViewPort {
00053 Q_OBJECT
00054 
00055 public:
00056         enum CAScrollBarVisibility {
00057                 ScrollBarAlwaysVisible,
00058                 ScrollBarAlwaysHidden,
00059                 ScrollBarShowIfNeeded
00060         };
00061 
00063         // Basic methods //
00065         CAScoreViewPort(QWidget *parent=0);
00066         CAScoreViewPort(CASheet *sheet, QWidget *parent=0);
00067         virtual ~CAScoreViewPort();
00068         CAScoreViewPort *clone();
00069         CAScoreViewPort *clone(QWidget *parent);
00070         inline CASheet  *sheet() { return _sheet; }
00071         inline void setSheet( CASheet *sheet ) { _sheet = sheet; }
00072 
00074         // Addition, removal of drawable elements //
00076         void addMElement(CADrawableMusElement *elt, bool select=false);
00077         void addCElement(CADrawableContext *elt, bool select=false);
00078         CAMusElement *removeMElement(double x, double y);
00079 
00080         void importElements(CAKDTree<CADrawableMusElement*> *drawableMList, CAKDTree<CADrawableContext*> *drawableCList);
00081         void importMElements(CAKDTree<CADrawableMusElement*> *elts);
00082         void importCElements(CAKDTree<CADrawableContext*> *elts);
00083 
00085         // Selection //
00087         inline QList<CADrawableMusElement*>& selection() { return _selection; };
00088         QList<CAMusElement*>                 musElementSelection();
00089         QList<CADrawableMusElement*>         musElementsAt(double x, double y);
00090         CADrawableContext                   *selectCElement(double x, double y);
00091         CADrawableMusElement                *selectMElement(CAMusElement *elt);
00092         CADrawableContext                   *selectContext(CAContext *context);
00093         inline QPoint                        lastMousePressCoords() { return _lastMousePressCoords; }
00094         void                                 setLastMousePressCoordsAfter(const QList<CAMusElement*> list);
00095 
00096         inline CADrawableContext *currentContext() { return _currentContext; }
00097         inline void setCurrentContext(CADrawableContext *c) { _currentContext = c; }
00098 
00099         void selectAll();
00100         void selectAllCurBar();
00101         void selectAllCurContext();
00102         void invertSelection();
00103         inline void clearSelection() { _selection.clear(); emit selectionChanged(); }
00104         inline bool removeFromSelection(CADrawableMusElement *elt) { return _selection.removeAll(elt); emit selectionChanged(); }
00105 
00106         void                  addToSelection(CADrawableMusElement *elt, bool triggerSignal=true );
00107         void                  addToSelection( const QList<CADrawableMusElement*> list, bool selectableOnly=true );
00108         CADrawableMusElement *addToSelection(CAMusElement *elt);
00109         void                  addToSelection(const QList<CAMusElement *> elts);
00110 
00111         CADrawableMusElement* selectNextMusElement( bool append=false );
00112         CADrawableMusElement* selectPrevMusElement( bool append=false );
00113         CADrawableMusElement* selectUpMusElement();
00114         CADrawableMusElement* selectDownMusElement();
00115 
00116         inline const QList<QRect> &selectionRegionList() const { return _selectionRegionList; }
00117         inline void addSelectionRegion(QRect r) { _selectionRegionList << r; }
00118         inline void removeSelectionRegion(QRect r) { _selectionRegionList.removeAll(r); }
00119         inline void clearSelectionRegionList() { _selectionRegionList.clear(); }
00120         inline CADrawable::CADirection resizeDirection() { return _resizeDirection; }
00121 
00123         // Music elements and contexts query, space calculation and access //
00125         CADrawableMusElement     *findMElement(CAMusElement*);
00126         CADrawableContext        *findCElement(CAContext*);
00127         QList<CADrawableContext*> findContextsInRegion(QRect &reg);
00128         CADrawableMusElement     *nearestLeftElement(double x, double y, CADrawableContext* context=0);
00129         CADrawableMusElement     *nearestLeftElement(double x, double y, CAVoice *voice);
00130         CADrawableMusElement     *nearestRightElement(double x, double y, CADrawableContext* context=0);
00131         CADrawableMusElement     *nearestRightElement(double x, double y, CAVoice *voice);
00132         int coordsToTime( double x );
00133         double timeToCoords( int time );
00134         double timeToCoordsSimpleVersion( int time );
00135 
00136         CADrawableContext *nearestUpContext(double x, double y);
00137         CADrawableContext *nearestDownContext(double x, double y);
00138 
00139         int calculateTime(double x, double y);
00140 
00141         CAContext *contextCollision(double x, double y);
00142 
00144         // Scrollbars //
00146         inline void setManualScroll(bool scroll) { _allowManualScroll = scroll; }
00147         inline bool manualScroll() { return _allowManualScroll; }
00148 
00149         void checkScrollBars();
00150 
00151         inline CAScrollBarVisibility isScrollBarVisible() { return _scrollBarVisible;}
00152         void setScrollBarVisible(CAScrollBarVisibility status);
00153 
00155         // Scene appearance, properties and actions //
00157         void rebuild();
00158         inline const int drawableWidth() { return _canvas->width(); }
00159         inline const int drawableHeight() { return _canvas->height(); }
00160 
00161         void setWorldX(int x, bool animate=false, bool force=false);
00162         void setWorldY(int y, bool animate=false, bool force=false);
00163         void setWorldWidth(int w, bool force=false);
00164         void setWorldHeight(int h, bool force=false);
00165 
00166         inline const int worldX() { return _worldX; }
00167         inline const int worldY() { return _worldY; }
00168         inline const int worldWidth() { return _worldW; }
00169         inline const int worldHeight() { return _worldH; }
00170         inline const QRect worldCoords() { return QRect(worldX(), worldY(), worldWidth(), worldHeight()); }
00171 
00172         inline const float zoom() { return _zoom; }
00173 
00174         void setWorldCoords(const QRect r, bool animate=false, bool force=false);
00175         void setWorldCoords(double x, double y, double w, double h, bool animate=false, bool force=false)  { setWorldCoords( QRect(x,y,w,h), animate, force); }
00176 
00177         void setCenterCoords(double x, double y, bool animate=false, bool force=false);
00178 
00179         void setZoom(float z, double x=0, double y=0, bool animate=false, bool force = false);
00180         void setZoom(float z, QPoint p,  bool animate=false, bool force = false) { setZoom(z, p.x(), p.y(), animate, force); }
00181 
00182         void zoomToSelection(bool animate=false, bool force=false);
00183         void zoomToWidth(bool animate=false, bool force=false);
00184         void zoomToHeight(bool animate=false, bool force=false);
00185         void zoomToFit(bool animate=false, bool force=false);
00186 
00187         void setBorder(const QPen pen);
00188         void unsetBorder();
00189         inline QPen border() { return _borderPen; }
00190         inline QColor backgroundColor() { return _backgroundColor; }
00191         inline void setBackgroundColor( const QColor c ) { _backgroundColor = c; }
00192         inline QColor foregroundColor() { return _foregroundColor; }
00193         inline void setForegroundColor( const QColor c ) { _foregroundColor = c; }
00194         inline QColor selectionColor() { return _selectionColor; }
00195         inline void setSelectionColor( const QColor c ) { _selectionColor = c; }
00196         inline QColor selectionAreaColor() { return _selectionAreaColor; }
00197         inline void setSelectionAreaColor( const QColor c ) { _selectionAreaColor = c; }
00198         inline QColor selectedContextColor() { return _selectedContextColor; }
00199         inline void setSelectedContextColor( const QColor c ) { _selectedContextColor = c; }
00200         inline QColor hiddenElementsColor() { return _hiddenElementsColor; }
00201         inline void setHiddenElementsColor( const QColor c ) { _hiddenElementsColor = c; }
00202         inline QColor disabledElementsColor() { return _disabledElementsColor; }
00203         inline void setDisabledElementsColor( const QColor c ) { _disabledElementsColor = c; }
00204 
00205         inline bool playing() { return _playing; }
00206         inline void setPlaying(bool playing) { _playing = playing; }
00207 
00208         inline void setRepaintArea(QRect *area) { _repaintArea = area; }
00209         inline void clearRepaintArea() { if (_repaintArea) delete _repaintArea; _repaintArea=0; }
00210 
00211         inline CAVoice *selectedVoice() { return _selectedVoice; }
00212         inline void setSelectedVoice( CAVoice *selectedVoice ) { _selectedVoice = selectedVoice; }
00213 
00214         inline bool shadowNoteVisible() { return _shadowNoteVisible; }
00215         inline void setShadowNoteVisible(bool visible) { _shadowNoteVisible = visible; setShadowNoteVisibleOnLeave(visible); }
00216 
00217         inline bool drawShadowNoteAccs() { return _drawShadowNoteAccs; }
00218         inline void setDrawShadowNoteAccs(bool draw) { _drawShadowNoteAccs = draw; }
00219 
00220         inline int shadowNoteAccs() { return _shadowNoteAccs; }
00221         inline void setShadowNoteAccs(int accs) { _shadowNoteAccs = accs; }
00222 
00223         void setShadowNoteLength( CAPlayableLength );
00224 
00225         CATextEdit *createTextEdit( CADrawableMusElement *elt );
00226         inline CATextEdit *textEdit() { return _textEdit; }
00227         void removeTextEdit();
00228         inline bool textEditVisible() { return _textEditVisible; }
00229 
00230         bool noteNameVisible() { return _noteNameVisible; }
00231         void setNoteNameVisible( bool v ) { _noteNameVisible = v; }
00232 
00233         QString noteName() { return _noteName; }
00234         void setNoteName( QString n ) { _noteName = n; }
00235 
00236         void updateHelpers(); // method for updating shadow notes, syllable edits and other post-engrave elements coordinates and sizes when zoom level is changed etc.
00237 
00238 private slots:
00239         void mousePressEvent(QMouseEvent *e);
00240         void mouseMoveEvent(QMouseEvent *e);
00241         void mouseReleaseEvent(QMouseEvent *e);
00242         void wheelEvent(QWheelEvent *e);
00243         void keyPressEvent(QKeyEvent *e);
00244 
00245         void HScrollBarEvent(int val);
00246         void VScrollBarEvent(int val);
00247 
00248         void resizeEvent(QResizeEvent *e);
00249         void paintEvent(QPaintEvent *p);
00250         void leaveEvent(QEvent *e);
00251         void enterEvent(QEvent *e);
00252         void on_animationTimer_timeout();
00253         void on_clickTimer_timeout();
00254 
00255 signals:
00256         void CATripleClickEvent( QMouseEvent *e, QPoint p );
00257         void CADoubleClickEvent( QMouseEvent *e, QPoint p );
00258         void CAMousePressEvent( QMouseEvent *e, QPoint p );
00259         void CAMouseReleaseEvent( QMouseEvent *e, QPoint p );
00260         void CAMouseMoveEvent( QMouseEvent *e, QPoint p );
00261         void CAWheelEvent( QWheelEvent *e, QPoint p );
00262         void CAKeyPressEvent( QKeyEvent *e );
00263         void selectionChanged();
00264 
00265 private:
00266         void initScoreViewPort( CASheet *s );
00267         inline void clearMElements() { _drawableMList.clear(true); }
00268         inline void clearCElements() { _drawableCList.clear(true); }
00269         inline bool isSelected(CADrawableMusElement *elt) { return (_selection.contains(elt)); }
00270 
00272         // Core Widgets //
00274         QGridLayout *_layout;    // Grid layout for placing the scrollbars at the right and the bottom.
00275         QWidget *_canvas;        // Virtual canvas which represents the size of the drawable area. All its signals are forwarded to CAViewPort.
00276         QScrollBar *_hScrollBar, *_vScrollBar; // Horizontal/vertical scrollbars
00277 
00279         // General properties //
00281         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!
00282         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!
00283         CASheet                        *_sheet;          // Pointer to the CASheet which the viewport represents.
00284 
00285         QList<CADrawableMusElement *>   _selection;      // The set of elements being selected.
00286         CADrawableContext              *_currentContext; // The pointer to the currently active context (staff, lyrics).
00287 
00288         static const int RIGHT_EXTRA_SPACE;       // Extra space at the right end to insert new music
00289         static const int BOTTOM_EXTRA_SPACE;  // Extra space at the bottom end to insert new music
00290         template <typename T> int getMaxXExtended(CAKDTree<T> &v);  // Make the viewable World a little bigger (stuffed) to make inserting at the end easier
00291         template <typename T> int getMaxYExtended(CAKDTree<T> &v);  // Make the viewable World a little bigger (stuffed) to make inserting below easies
00292 
00293         double _worldX, _worldY, _worldW, _worldH;      // Absolute world coordinates of the area the viewport is currently showing.
00294         QPoint _lastMousePressCoords;           // Used in multiple selection - coordinates of the upper-left point of the rectangle the user drags in world coordinates
00295         inline void setLastMousePressCoords( QPoint p ) { _lastMousePressCoords = p; }
00296         float _zoom;                            // Zoom level of the viewport (1.0 = 100%, 1.5 = 150% etc.).
00297 
00298         CAVoice *_selectedVoice;        // Voice to be drawn normal colors, others are shaded
00299 
00301         // Helpers //
00303         // Shadow note
00304         bool _shadowNoteVisible;        // Should the shadow notes be rendered or not
00305 
00306         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.
00307         inline bool shadowNoteVisibleOnLeave() { return _shadowNoteVisibleOnLeave; }
00308         inline void setShadowNoteVisibleOnLeave( bool v ) { _shadowNoteVisibleOnLeave = v; }
00309 
00310         int _shadowNoteAccs;            // Number of accidentals - 0 - natural, 1 - sharp, -1 flat
00311         bool _drawShadowNoteAccs;       // Draw shadow note accs?
00312         QList<CANote*> _shadowNote;     // List of all shadow notes - one shadow note per drawable staff
00313         QList<CADrawableNote*> _shadowDrawableNote;     // List of drawable shadow notes
00314 
00315         // QLineEdit for editing or creating a lyrics syllable
00316         CATextEdit *_textEdit;
00317         inline void setTextEdit( CATextEdit *e ) { _textEdit = e; }
00318         QRect _textEditGeometry;
00319         inline QRect textEditGeometry() { return _textEditGeometry; }
00320         inline void setTextEditGeometry( const QRect r ) { _textEditGeometry = r; }
00321         bool _textEditVisible;
00322         inline void setTextEditVisible(bool v) { _textEditVisible = v; }
00323         inline void setResizeDirection( CADrawable::CADirection r ) { _resizeDirection = r; }
00324         CADrawable::CADirection _resizeDirection; // Is the current scalable music element in drag-drop resizing mode?
00325 
00326         // Selection regions
00327         QList<QRect> _selectionRegionList;
00328         void drawSelectionRegion( QPainter *p, CADrawSettings s );
00329 
00331         // Appearance //
00333         bool _drawBorder;              // Should the border be drawn or not.
00334         QRect *_repaintArea;           // Area to be repainted on paintEvent().
00335         QPen _borderPen;               // Pen which the border is drawn by.
00336         QColor _backgroundColor;       // Color which the background is filled.
00337         QColor _foregroundColor;       // Color which the music elements are painted.
00338         QColor _selectionColor;        // Color which the selected music elements are painted.
00339         QColor _selectionAreaColor;    // Color which the selection area background is filled.
00340         QColor _selectedContextColor;  // Color which the current context is painted.
00341         QColor _disabledElementsColor; // Color which the elements in non-selected voice are painted.
00342         QColor _hiddenElementsColor;   // Color which the invisible elements are painted in current-voice-only mode.
00343         bool    _noteNameVisible;      // Is the written note name visible
00344         QString _noteName;             // Name of the note to be inserted. eg. c', Des,
00345 
00347         // Animation //
00349         QTimer *_animationTimer;          // Timer used to animate scroll/zoom behaviour.
00350         static const int ANIMATION_STEPS; // Number of steps used in animation
00351         int _animationStep;               // Current step in the animation
00352         double _targetWorldX, _targetWorldY, _targetWorldW, _targetWorldH;      // Absolute world coordinates of the area the viewport is currently showing.
00353         float _targetZoom;                // Zoom level of the viewport (1.0 = 100%, 1.5 = 150% etc.).
00354 
00355         void startAnimationTimer();
00356 
00358         // Widgets behaviour //
00360         CAScrollBarVisibility _scrollBarVisible;  // Are the scrollbars always visible/never/if needed. Use CAViewPort::ScrollBarAlwaysVisible, CAViewPort::ScrollBarAlwaysHidden or CAViewPort::ScrollBarShowIfNeeded.
00361         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
00362 
00364         // Internal properties //
00366         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.
00367         bool _playing;                                      // Set to on, when in Playback mode
00368         QTimer *_clickTimer;                                // Used for measuring doubleClick and tripleClick
00369         int     _numberOfClicks;                            // Used for measuring doubleClick and tripleClick
00370 
00371         double _xCursor, _yCursor;                             // Mouse cursor position in absolute world coords.
00372         bool _holdRepaint;                                  // Flag to prevent multiple repaintings.
00373         bool _checkScrollBarsDeadLock;                      // Flag to prevent recursive checkScrollBars() calls.
00374         bool _hScrollBarDeadLock;                           // Flag to prevent recursive scrollbar calls when its value is manually changed.
00375         bool _vScrollBarDeadLock;                           // Flag to prevent recursive scrollbar calls when its value is manually changed.
00376 };
00377 
00378 #endif /*SCOREVIEWPORT_H_*/