summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/notation/NotationHLayout.h
blob: 878dc17ba62228b32f847f20c490a1862a29f6c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
142
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>

    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _RG_NOTATIONHLAYOUT_H_
#define _RG_NOTATIONHLAYOUT_H_

#include "base/LayoutEngine.h"
#include "base/NotationTypes.h"
#include "NotationElement.h"
#include "gui/general/ProgressReporter.h"
#include <map>
#include <vector>
#include "base/Event.h"


class TieMap;
class TQObject;


namespace Rosegarden
{

class ViewElement;
class Staff;
class Quantizer;
class NotePixmapFactory;
class NotationProperties;
class NotationGroup;
class NotationChord;
class Key;
class Composition;
class Clef;
class AccidentalTable;


/**
 * Horizontal notation layout
 *
 * computes the X coordinates of notation elements
 */

class NotationHLayout : public ProgressReporter,
                        public HorizontalLayoutEngine
{
public:
    NotationHLayout(Composition *c,
                    NotePixmapFactory *npf,
                    const NotationProperties &properties,
                    TQObject* parent, const char* name = 0);

    virtual ~NotationHLayout();

    void setNotePixmapFactory(NotePixmapFactory *npf) {
        m_npf = npf;
    }

    /**
     * Precomputes layout data for a single staff.  The resulting data
     * is stored in the BarDataMap, keyed from the staff reference;
     * the entire map is then used by reconcileBars() and layout().
     * The map should be cleared (by calling reset()) before a full
     * set of staffs is preparsed.
     */
    virtual void scanStaff(Staff &staff,
                           timeT startTime = 0,
                           timeT endTime = 0);

    /**
     * Resets internal data stores, notably the BarDataMap that is
     * used to retain the data computed by scanStaff().
     */
    virtual void reset();

    /**
     * Resets internal data stores, notably the given staff's entry
     * in the BarDataMap used to retain the data computed by scanStaff().
     */
    virtual void resetStaff(Staff &staff,
                            timeT startTime = 0,
                            timeT endTime = 0);

    /**
     * Lays out all staffs that have been scanned
     */
    virtual void finishLayout(timeT startTime = 0,
                              timeT endTime = 0);

    /**
     * Set page mode
     */
    virtual void setPageMode(bool pageMode) { m_pageMode = pageMode; }

    /**
     * Get the page mode setting
     */
    virtual bool isPageMode() { return m_pageMode; }

    /**
     * Set a page width
     */
    virtual void setPageWidth(double pageWidth) { m_pageWidth = pageWidth; }

    /**
     * Get the page width
     */
    virtual double getPageWidth() { return m_pageWidth; }

    /**
     * Gets the current spacing factor (100 == "normal" spacing)
     */
    int getSpacing() const { return m_spacing; }

    /**
     * Sets the current spacing factor (100 == "normal" spacing)
     */
    void setSpacing(int spacing) { m_spacing = spacing; }

    /**
     * Gets the range of "standard" spacing factors (you can
     * setSpacing() to anything you want, but it makes sense to
     * have a standard list for GUI use).  The only guaranteed
     * property of the returned list is that 100 will be in it.
     */
    static std::vector<int> getAvailableSpacings();

    /**
     * Gets the current proportion (100 == spaces proportional to
     * durations, 0 == equal spacings)
     */
    int getProportion() const { return m_proportion; }

    /**
     * Sets the current proportion (100 == spaces proportional to
     * durations, 0 == equal spacings)
     */
    void setProportion(int proportion) { m_proportion = proportion; }

    /**
     * Gets the range of "standard" proportion factors (you can
     * setProportion() to anything you want, but it makes sense to
     * have a standard list for GUI use).  The only guaranteed
     * property of the returned list is that 0, 100, and whatever the
     * default proportion is will be in it.
     */
    static std::vector<int> getAvailableProportions();

    /**
     * Returns the total length of all elements once layout is done
     * This is the x-coord of the end of the last element on the longest
     * staff, plus the space allocated to that element
     */
    virtual double getTotalWidth() const { return m_totalWidth; }

    /**
     * Returns the number of the first visible bar line on the given
     * staff
     */
    virtual int getFirstVisibleBarOnStaff(Staff &staff);

    /**
     * Returns the number of the first visible bar line on any
     * staff
     */
    virtual int getFirstVisibleBar() const;

    /**
     * Returns the number of the last visible bar line on the given
     * staff
     */
    virtual int getLastVisibleBarOnStaff(Staff &staff) const;

    /**
     * Returns the number of the first visible bar line on any
     * staff
     */
    virtual int getLastVisibleBar() const;

    /**
     * Returns the x-coordinate of the given bar number
     */
    virtual double getBarPosition(int barNo) const;

    /**
     * Returns the nearest time value to the given X coord.
     */
    virtual timeT getTimeForX(double x) const;

    /**
     * Returns the X coord corresponding to the given time value.
     * This RulerScale method works by interpolating between bar lines
     * (the inverse of the way getTimeForX works), and should be used
     * for any rulers associated with the layout.
     */
    virtual double getXForTime(timeT time) const;

    /**
     * Returns the X coord corresponding to the given time value.
     * This method works by interpolating between event positions, and
     * should be used for position pointer tracking during playback.
     */
    virtual double getXForTimeByEvent(timeT time) const;

    /**
     * Returns true if the specified bar has the correct length
     */
    virtual bool isBarCorrectOnStaff(Staff &staff, int barNo);

    /**
     * Returns true if there is a new time signature in the given bar,
     * setting timeSignature appropriately and setting timeSigX to its
     * x-coord
     */
    virtual bool getTimeSignaturePosition
    (Staff &staff, int barNo,
     TimeSignature &timeSig, double &timeSigX);

    /// purely optional, used only for progress reporting
    void setStaffCount(int staffCount) {
        m_staffCount = staffCount;
    }

protected:

    struct Chunk {
        timeT duration;
        short subordering;
        float fixed;
        float stretchy;
        float x;

        Chunk(timeT d, short sub, float f, float s) :
            duration(d), subordering(sub), fixed(f), stretchy(s), x(0) { }
        Chunk(short sub, float f) :
            duration(0), subordering(sub), fixed(f), stretchy(0), x(0) { }
    };
    typedef std::vector<Chunk> ChunkList;

    /**
     * Inner class for bar data, used by scanStaff()
     */
    struct BarData
    {
        ChunkList chunks;
        
        struct BasicData
        {   // slots that can be filled at construction time

            NotationElementList::iterator start; // i.e. event following barline
            bool correct; // bar preceding barline has correct duration
            TimeSignature timeSignature;
            bool newTimeSig;

        } basicData;

        struct SizeData
        {   // slots that can be filled when the following bar has been scanned

            float idealWidth;    // theoretical width of bar following barline
            float reconciledWidth;
            float fixedWidth;       // width of non-chunk items in bar
            int clefKeyWidth;
            timeT actualDuration; // may exceed nominal duration

        } sizeData;

        struct LayoutData
        {   // slots either assumed, or only known at layout time
            bool needsLayout;
            double x;             // coordinate for display of barline
            int timeSigX;

        } layoutData;

        BarData(NotationElementList::iterator i,
                bool correct, TimeSignature timeSig, bool newTimeSig) {
            basicData.start = i;
            basicData.correct = correct;
            basicData.timeSignature = timeSig;
            basicData.newTimeSig = newTimeSig;
            sizeData.idealWidth = 0;
            sizeData.reconciledWidth = 0;
            sizeData.fixedWidth = 0;
            sizeData.clefKeyWidth = 0;
            sizeData.actualDuration = 0;
            layoutData.needsLayout = true;
            layoutData.x = -1;
            layoutData.timeSigX = -1;
        }
    };

    typedef std::map<int, BarData> BarDataList;
    typedef BarDataList::value_type BarDataPair;
    typedef std::map<Staff *, BarDataList> BarDataMap;
    typedef std::map<int, double> BarPositionList;

    typedef std::map<Staff *, int> StaffIntMap;
    typedef std::map<long, NotationGroup *> NotationGroupMap;

    void clearBarList(Staff &);


    /**
     * Set the basic data for the given barNo.  If barNo is
     * beyond the end of the existing bar data list, create new
     * records and/or fill with empty ones as appropriate.
     */
    void setBarBasicData(Staff &staff, int barNo,
                         NotationElementList::iterator start, bool correct,
                         TimeSignature timeSig, bool newTimeSig);

    /**
     * Set the size data for the given barNo.  If barNo is
     * beyond the end of the existing bar data list, create new
     * records and/or fill with empty ones as appropriate.
     */
    void setBarSizeData(Staff &staff, int barNo,
                        float fixedWidth, timeT actualDuration);

    /**
     * Returns the bar positions for a given staff, provided that
     * staff has been preparsed since the last reset
     */
    BarDataList& getBarData(Staff &staff);
    const BarDataList& getBarData(Staff &staff) const;

    /// Find the staff in which bar "barNo" is widest
    Staff *getStaffWithWidestBar(int barNo);

    /// Find width of clef+key in the staff in which they're widest in this bar
    int getMaxRepeatedClefAndKeyWidth(int barNo);

    /// For a single bar, makes sure synchronisation points align in all staves
    void preSquishBar(int barNo);

    /// Tries to harmonize the bar positions for all the staves (linear mode)
    void reconcileBarsLinear();

    /// Tries to harmonize the bar positions for all the staves (page mode)
    void reconcileBarsPage();

    void layout(BarDataMap::iterator,
                timeT startTime,
                timeT endTime);
    
    /// Find earliest element with quantized time of t or greater
    NotationElementList::iterator getStartOfQuantizedSlice 
    (NotationElementList *, timeT t) const;

    void scanChord
    (NotationElementList *notes, NotationElementList::iterator &i,
     const Clef &, const ::Rosegarden::Key &,
     AccidentalTable &, float &lyricWidth,
     ChunkList &chunks, NotePixmapFactory *, int ottavaShift,
     NotationElementList::iterator &to);

    typedef std::map<int, NotationElementList::iterator> TieMap;

    // This modifies the NotationElementList::iterator passed to it,
    // moving it on to the last note in the chord; updates the TieMap;
    // and may modify the to-iterator if it turns out to point at a
    // note within the chord
    void positionChord
    (Staff &staff, 
     NotationElementList::iterator &, const Clef &clef,
     const ::Rosegarden::Key &key, TieMap &, NotationElementList::iterator &to);

    void sampleGroupElement
    (Staff &staff, const Clef &clef,
     const ::Rosegarden::Key &key, const NotationElementList::iterator &);

    /// Difference between absolute time of next event and of this
    timeT getSpacingDuration
    (Staff &staff, const NotationElementList::iterator &);

    /// Difference between absolute time of chord and of first event not in it
    timeT getSpacingDuration
    (Staff &staff, const NotationChord &);

    float getLayoutWidth(ViewElement &,
                         NotePixmapFactory *,
                         const ::Rosegarden::Key &) const;

    int getBarMargin() const;
    int getPreBarMargin() const;
    int getPostBarMargin() const;
    int getFixedItemSpacing() const;

    NotePixmapFactory *getNotePixmapFactory(Staff &);
    NotePixmapFactory *getGraceNotePixmapFactory(Staff &);

    //--------------- Data members ---------------------------------

    BarDataMap m_barData;
    StaffIntMap m_staffNameWidths;
    BarPositionList m_barPositions;
    NotationGroupMap m_groupsExtant;

    double m_totalWidth;
    bool m_pageMode;
    double m_pageWidth;
    int m_spacing;
    int m_proportion;
    int m_keySigCancelMode;

    //!!! This should not be here -- different staffs may have
    //different sizes in principle, so we should always be referring
    //to the npf of a particular staff
    NotePixmapFactory *m_npf;

    static std::vector<int> m_availableSpacings;
    static std::vector<int> m_availableProportions;

    const Quantizer *m_notationQuantizer;
    const NotationProperties &m_properties;

    int m_timePerProgressIncrement;
    std::map<Staff *, bool> m_haveOttavaSomewhere;
    int m_staffCount; // purely for progress reporting
};


}

#endif