~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/editors/tab/layout/PrinterViewLayout.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on 04-ene-2006
3
 
 *
4
 
 * TODO To change the template for this generated file go to
5
 
 * Window - Preferences - Java - Code Style - Code Templates
6
 
 */
7
 
package org.herac.tuxguitar.gui.editors.tab.layout;
8
 
 
9
 
import java.util.ArrayList;
10
 
import java.util.List;
11
 
 
12
 
import org.eclipse.swt.SWT;
13
 
import org.eclipse.swt.graphics.Font;
14
 
import org.eclipse.swt.graphics.GC;
15
 
import org.eclipse.swt.graphics.Image;
16
 
import org.eclipse.swt.graphics.Rectangle;
17
 
import org.herac.tuxguitar.gui.TuxGuitar;
18
 
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
19
 
import org.herac.tuxguitar.gui.editors.tab.SongTrackCoords;
20
 
import org.herac.tuxguitar.gui.editors.tab.Tablature;
21
 
import org.herac.tuxguitar.gui.printer.PrintDocument;
22
 
import org.herac.tuxguitar.gui.printer.PrintDocumentData;
23
 
import org.herac.tuxguitar.song.managers.SongManager;
24
 
import org.herac.tuxguitar.song.models.Duration;
25
 
import org.herac.tuxguitar.song.models.Measure;
26
 
 
27
 
/**
28
 
 * @author julian
29
 
 *
30
 
 * TODO To change the template for this generated type comment go to
31
 
 * Window - Preferences - Java - Code Style - Code Templates
32
 
 */
33
 
public class PrinterViewLayout extends ViewLayout{
34
 
        
35
 
        public static final boolean AUTO_SPACING_ENABLED = true;        
36
 
        protected static final int DEFAULT_SCORE_LINE_SPAN = 7;
37
 
        protected static final int DEFAULT_STRING_SPAN = 8;
38
 
        protected static final int MIN_SCORE_TAB_SPAN =  20;
39
 
        protected static final int DEFAULT_SCORE_SPAN = ((DEFAULT_SCORE_LINE_SPAN * 5) + MIN_SCORE_TAB_SPAN);   
40
 
        protected static final int DEFAULT_TRACK_SPAN = 8;
41
 
        protected static final int DEFAULT_FIRST_TRACK_SPAN = DEFAULT_TRACK_SPAN;
42
 
                
43
 
        private PrintDocument document;
44
 
        private PrintDocumentData documentData;
45
 
    private Image image;
46
 
    private int divisionCount;        
47
 
    
48
 
    public PrinterViewLayout(Tablature tablature,SongManager songManager,PrintDocumentData data){
49
 
        super(tablature,songManager,false,data.isScoreEnabled(),data.isTablatureEnabled());
50
 
        this.documentData = data;
51
 
    }
52
 
    
53
 
    public int getMode(){
54
 
        return 0;
55
 
    }
56
 
    
57
 
    public PrintDocument makeDocument(Rectangle size){          
58
 
        //-------init document-------------------------------------------------------
59
 
        this.document = new PrintDocument();
60
 
        this.document.setPageSize(size);
61
 
        //-------draw the image------------------------------------------------------
62
 
        this.image = new Image(getTablature().getDisplay(),this.document.getPageSize());
63
 
        GC gc = new GC(this.image);
64
 
        paintHeader(gc);
65
 
        paintSong(gc,0,80);
66
 
        gc.dispose();
67
 
        this.image.dispose();
68
 
        
69
 
        return this.document;
70
 
    }
71
 
    
72
 
    private void makePage(GC gc){
73
 
        String pageNumber = Integer.toString(this.document.count() + 1);
74
 
        
75
 
        setDefaultStyle(gc);
76
 
        gc.setForeground(getTablature().getDisplay().getSystemColor(SWT.COLOR_BLACK));
77
 
        gc.drawString(pageNumber,getRight(gc,pageNumber),this.document.getPageSize().height - (gc.stringExtent(pageNumber).y + 5));
78
 
        
79
 
        this.document.makePage(this.image.getImageData());
80
 
        gc.fillRectangle(this.document.getPageSize());
81
 
    }
82
 
    
83
 
    public void paintHeader(GC gc){
84
 
        String songName = getSongManager().getSong().getName();
85
 
        String songAuthor = getSongManager().getSong().getAuthor();
86
 
        String trackName = "(" + getSongManager().getTrack(documentData.getTrackNumber()).getName() + ")";
87
 
        
88
 
        if(songName == null || songName.length() == 0){
89
 
                songName = TuxGuitar.getProperty("print-header.default-song-name");
90
 
        }
91
 
        if(songAuthor == null || songAuthor.length() == 0){
92
 
                songAuthor = TuxGuitar.getProperty("print-header.default-song-author");
93
 
        }
94
 
        Font songNameFont = new Font(getTablature().getDisplay(),"Sans",16, SWT.BOLD | SWT.CENTER);
95
 
        Font trackNameFont = new Font(getTablature().getDisplay(),"Sans",8, SWT.BOLD | SWT.CENTER);
96
 
        Font songAuthorFont = new Font(getTablature().getDisplay(),"Sans",8, SWT.BOLD | SWT.CENTER);
97
 
                
98
 
        gc.setFont(songNameFont);  
99
 
        gc.drawString(songName,getCenter(gc,songName),0);
100
 
        gc.setFont(trackNameFont);
101
 
        gc.drawString(trackName,getCenter(gc,trackName),30);
102
 
        gc.setFont(songAuthorFont);
103
 
        gc.drawString(songAuthor,getRight(gc,songAuthor),50);
104
 
        
105
 
        songNameFont.dispose();
106
 
        trackNameFont.dispose();
107
 
        songAuthorFont.dispose();
108
 
        
109
 
        setDefaultStyle(gc);
110
 
    }
111
 
    
112
 
    private int getCenter(GC gc,String text){
113
 
        int textWidth = gc.stringExtent(text).x;
114
 
        return ((getMaxWidth() - textWidth) / 2);
115
 
    }
116
 
 
117
 
    private int getRight(GC gc,String text){
118
 
        int textWidth = gc.stringExtent(text).x;
119
 
        return ((getMaxWidth() - textWidth));
120
 
    }
121
 
    
122
 
    public void paintSong(GC gc,Rectangle clientArea,int fromX,int fromY){
123
 
        
124
 
    }
125
 
    
126
 
    public void paintSong(GC gc,int fromX,int fromY){ 
127
 
        List tracksCoords = new ArrayList();        
128
 
        tracksCoords.add(getTablature().getCaret().getSongCoords().getTrack(documentData.getTrackNumber()));
129
 
        paintTracks(tracksCoords,gc,null,fromX,fromY);        
130
 
    }
131
 
    
132
 
    
133
 
    public void paintTracks(List tracksCoords,GC gc,Rectangle clientArea,int fromX,int fromY) {         
134
 
        int posY = fromY + getDefaultFirstTrackSpan();
135
 
        int height = getDefaultFirstTrackSpan();
136
 
        int lineHeight = 0;     
137
 
        
138
 
        SongTrackCoords trackCoords = (SongTrackCoords)tracksCoords.get(0);             
139
 
                trackCoords.getLyricPainter().start(getSkippedBeats(trackCoords.getMeasuresCoords()));
140
 
                TrackSpacing ts = new TrackSpacing(this) ;
141
 
                TempLine line = getTempLines(fromX,trackCoords.getMeasuresCoords(),( documentData.getFromMeasure() - 1 ),ts);
142
 
                while(!line.measures.isEmpty()){
143
 
                            
144
 
            ts.setSize(TrackSpacing.POSITION_SCORE_MIDDLE_LINES, ((isScoreEnabled())?( (DEFAULT_SCORE_LINE_SPAN * 5) ):0));
145
 
                        if(AUTO_SPACING_ENABLED && isScoreEnabled()){                           
146
 
                ts.setSize(TrackSpacing.POSITION_SCORE_UP_LINES, Math.abs(line.minY));                              
147
 
                if(isScoreEnabled() && line.maxY + MIN_SCORE_TAB_SPAN > DEFAULT_SCORE_SPAN){
148
 
                        ts.setSize(TrackSpacing.POSITION_SCORE_DOWN_LINES, (line.maxY - (getScoreLineSpan() * 4)) );
149
 
                }       
150
 
                        }       
151
 
            ts.setSize(TrackSpacing.POSITION_TABLATURE_TOP_SEPARATOR,(isScoreEnabled()?getMinScoreTabSpan():getStringSpan()));
152
 
            ts.setSize(TrackSpacing.POSITION_TABLATURE, ( isTablatureEnabled()?(isScoreEnabled()?trackCoords.getTabHeight() + getStringSpan() +1:trackCoords.getTabHeight() + ((getStringSpan() / 2) * 5) +1 ) :0) );
153
 
            ts.setSize(TrackSpacing.POSITION_LYRIC,10);            
154
 
            checkTopSpan(ts);
155
 
            
156
 
                        lineHeight = ts.getSize();                      
157
 
                //Verifico si entra en la pagina actual
158
 
                if((posY + lineHeight + DEFAULT_TRACK_SPAN) > getMaxHeight()){
159
 
                makePage(gc);
160
 
                posY = getDefaultFirstTrackSpan();
161
 
            }                           
162
 
                //pinto la linea
163
 
                paintLine(trackCoords,line,gc,fromX,posY,ts,clientArea);
164
 
                
165
 
            posY += lineHeight + DEFAULT_TRACK_SPAN;    
166
 
                height += lineHeight + DEFAULT_TRACK_SPAN;                             
167
 
                
168
 
                ts = new TrackSpacing(this) ;
169
 
                line = getTempLines(fromX,trackCoords.getMeasuresCoords(),( line.lastIndex + 1 ),ts);
170
 
        }       
171
 
 
172
 
        //Creo la ultima pagina
173
 
        makePage(gc);
174
 
        
175
 
        setHeight(height);
176
 
    }
177
 
    
178
 
    
179
 
    
180
 
    public void paintLine(SongTrackCoords trackCoords,TempLine line,GC gc,int fromX, int fromY,TrackSpacing ts,Rectangle clientArea) {  
181
 
        int posX = fromX;//(fromX + DEFAULT_HORIZONTAL_SPAN);
182
 
        int posY = fromY;
183
 
        int width = 0;
184
 
        int height = 0;        
185
 
 
186
 
        int measureSpan = 0;
187
 
        if(line.fullLine){
188
 
                int diff = ( getMaxWidth() - line.tempWith);
189
 
                if(diff != 0 && line.measures.size() > 0){
190
 
                        measureSpan = diff / line.measures.size();                    
191
 
                }
192
 
        }
193
 
            
194
 
        for(int i = 0;i < line.measures.size();i ++){
195
 
                int index = ((Integer)line.measures.get(i)).intValue();
196
 
            MeasureCoords currMeasureCoords = (MeasureCoords)trackCoords.getMeasuresCoords().get(index);
197
 
                    
198
 
            //asigno la posicion dentro del compas
199
 
            currMeasureCoords.setPosX(posX);
200
 
            currMeasureCoords.setPosY(posY);              
201
 
            currMeasureCoords.setTs(ts);
202
 
            
203
 
            trackCoords.getLyricPainter().setCurrentMeasure(currMeasureCoords);
204
 
            
205
 
            currMeasureCoords.setFirstOfLine(i == 0);
206
 
 
207
 
            paintMeasure(currMeasureCoords,gc,posX, posY,measureSpan,clientArea);
208
 
            trackCoords.getLyricPainter().paintCurrentNoteBeats(gc,this,currMeasureCoords,posX, posY);
209
 
                    
210
 
            posX += currMeasureCoords.getWidth() + currMeasureCoords.getSpan();
211
 
            
212
 
            if(posX > width){
213
 
                width = posX;
214
 
                
215
 
            }                                                        
216
 
        }                
217
 
 
218
 
        setWidth((width > getWidth())?width:getWidth());
219
 
    }                     
220
 
    
221
 
    public TempLine getTempLines(int posX,List measuresCoords,int fromIndex,TrackSpacing ts) {          
222
 
        TempLine line = new TempLine();
223
 
        this.divisionCount = 1;        
224
 
        for (int measureIdx = fromIndex; measureIdx < measuresCoords.size(); measureIdx++) {            
225
 
            MeasureCoords measure= (MeasureCoords) measuresCoords.get(measureIdx);                
226
 
            if(measure.getMeasure().getNumber() >= documentData.getFromMeasure() && measure.getMeasure().getNumber() <= documentData.getToMeasure() ){
227
 
         
228
 
                //verifico si tengo que bajar de linea
229
 
                if((line.tempWith + measure.getWidth()) >=  getMaxWidth() && !line.measures.isEmpty()){
230
 
                        line.fullLine = true;
231
 
                        return line;
232
 
                }
233
 
                line.tempWith +=  measure.getWidth();
234
 
                line.maxY = (measure.getMaxY() > line.maxY)?measure.getMaxY():line.maxY;
235
 
                line.minY = (measure.getMinY() < line.minY)?measure.getMinY():line.minY;
236
 
                
237
 
                line.addMeasure(measureIdx);
238
 
                measure.registerEffects(ts);
239
 
            }
240
 
        }        
241
 
       
242
 
        return line;
243
 
    }    
244
 
    
245
 
    public int getSkippedBeats(List measuresCoords) {
246
 
        int beats = 0;
247
 
        
248
 
        for (int i = 0; i < (documentData.getFromMeasure() - 1); i++) {                 
249
 
            MeasureCoords measure = (MeasureCoords) measuresCoords.get(i);    
250
 
            beats += measure.getBeatPositions().size();
251
 
        }               
252
 
        return beats;
253
 
    }    
254
 
    
255
 
    
256
 
    /**
257
 
     * Calcula el espacio minimo entre negras, dependiendo de la duracion de la nota 
258
 
     */
259
 
    public int getSpanForQuarter(Duration duration){        
260
 
        double span = ((double)Duration.QUARTER_TIME / (double)duration.getTime()) * getMinSpan(duration);
261
 
        return  (int)span;
262
 
    }       
263
 
    
264
 
    /**
265
 
     * Calcula el Espacio minimo que quedara entre nota y nota
266
 
     */
267
 
    private int getMinSpan(Duration duration){
268
 
        int minSpan = 0;
269
 
        switch(duration.getValue()){
270
 
                case Duration.WHOLE:
271
 
                    minSpan = 18;
272
 
                    break;
273
 
                case Duration.HALF:
274
 
                    minSpan = 16;
275
 
                    break;
276
 
                case Duration.QUARTER:
277
 
                    minSpan = 14;
278
 
                    break;
279
 
                case Duration.EIGHTH:
280
 
                    minSpan = 12;
281
 
                    break;       
282
 
                default:
283
 
                    minSpan = 12;
284
 
                    break;
285
 
        }        
286
 
        return minSpan;        
287
 
    } 
288
 
        
289
 
    public boolean followMeasure(MeasureCoords measure,boolean redraw){
290
 
        return false;
291
 
    }       
292
 
    
293
 
    public boolean isCaretVisible(){
294
 
        return false;
295
 
    }
296
 
 
297
 
    public boolean isPlayModeEnabled(){
298
 
        return false;
299
 
    }
300
 
 
301
 
    public void setDefaultStyle(GC gc){
302
 
        gc.setFont(getTablature().getPrinterDefaultFont());
303
 
    }    
304
 
    
305
 
    public void setNoteStyle(GC gc){
306
 
        gc.setFont(getTablature().getPrinterNoteFont());        
307
 
    }
308
 
    
309
 
    public void setTimeSignatureStyle(GC gc){
310
 
        gc.setFont(getTablature().getPrinterTimeSignatureFont());       
311
 
    }
312
 
    
313
 
    public int getMaxWidth(){
314
 
        return (this.document.getPageSize().width - 10);
315
 
    }
316
 
    
317
 
    public int getMaxHeight(){
318
 
        return this.document.getPageSize().height;
319
 
    }    
320
 
 
321
 
    public boolean isFirstMeasure(Measure measure){
322
 
        return (measure.getNumber() == documentData.getFromMeasure());
323
 
    }
324
 
 
325
 
    public boolean isLastMeasure(Measure measure){
326
 
        return (measure.getNumber() == documentData.getToMeasure());
327
 
    }
328
 
 
329
 
    public int getStringSpan(){
330
 
        return DEFAULT_STRING_SPAN;
331
 
    }
332
 
        
333
 
    public int getScoreLineSpan(){
334
 
        return DEFAULT_SCORE_LINE_SPAN;
335
 
    }       
336
 
    
337
 
    public int getDefaultTrackSpan(){
338
 
        return DEFAULT_TRACK_SPAN;
339
 
    }
340
 
 
341
 
    public int getMinScoreTabSpan(){
342
 
        return MIN_SCORE_TAB_SPAN;
343
 
    }    
344
 
    
345
 
    public int getDefaultFirstTrackSpan(){
346
 
        return DEFAULT_FIRST_TRACK_SPAN;
347
 
    }
348
 
    
349
 
    public int getDefaultEffectSpan(){
350
 
        return super.getDefaultEffectSpan();
351
 
    }     
352
 
    
353
 
    private class TempLine{     
354
 
        private int tempWith;           
355
 
        private int lastIndex;
356
 
        private int startBeat;
357
 
        private boolean fullLine;
358
 
        private List measures = new ArrayList();
359
 
        
360
 
        private int maxY = 0;
361
 
        private int minY = 50;
362
 
        
363
 
        private void addMeasure(int index){
364
 
                this.measures.add(new Integer(index));
365
 
                this.lastIndex = index;
366
 
        }
367
 
    }
368
 
}