~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to kmidi/table.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** $Id: table.cpp,v 1.8 2001/04/12 09:08:31 garbanzo Exp $
 
3
**
 
4
** Copyright (C) 1992-1999 Troll Tech AS.  All rights reserved.
 
5
**
 
6
** This file is part of an example program for Qt.  This example
 
7
** program may be used, distributed and modified without limitation.
 
8
**
 
9
*****************************************************************************/
 
10
 
 
11
#include <sys/types.h>
 
12
 
 
13
#include "config.h"
 
14
#include "table.h"
 
15
#include "instrum.h"
 
16
#include "playmidi.h"
 
17
#include "ctl.h"
 
18
 
 
19
#include <qpainter.h>
 
20
 
 
21
static const int NUMCOLS = 9;
 
22
static const int NUMROWS = 17;
 
23
 
 
24
/*
 
25
  Constructs a Table widget.
 
26
*/
 
27
 
 
28
Table::Table( int width, int height, QWidget *parent, const char *name, WFlags f)
 
29
    : QTableView(parent,name,f)
 
30
{
 
31
    int n;
 
32
 
 
33
    setAutoUpdate(true);
 
34
    setFocusPolicy( NoFocus );
 
35
    setBackgroundMode( PaletteBase );           // set widgets background
 
36
    setNumCols( NUMCOLS );                      // set number of col's in table
 
37
    setNumRows( NUMROWS );                      // set number of rows in table
 
38
    setCellHeight( height/NUMROWS );                    // set height of cell in pixels
 
39
 
 
40
    cell_width[0] = 20;
 
41
    cell_width[1] = 100;
 
42
    cell_width[2] = 27;
 
43
    cell_width[8] = 27;
 
44
    for (n=3; n<8; n++) cell_width[n] = (width - (20+100+27+27)) / 5;
 
45
 
 
46
    setTableFlags( Tbl_clipCellPainting );      // avoid drawing outside cell
 
47
    resize( width, height );                            // set default size in pixels
 
48
 
 
49
    contents = new QString[ NUMROWS * NUMCOLS ];        // make room for contents
 
50
 
 
51
    for (n=1; n<NUMROWS; n++) contents[indexOf( n, 0 )].setNum(n);
 
52
 
 
53
    contents[indexOf(0,0)] = QString("C");
 
54
    contents[indexOf(0,1)] = QString("Patch");
 
55
    contents[indexOf(0,2)] = QString("Pr");
 
56
    contents[indexOf(0,3)] = QString("Exp");
 
57
    contents[indexOf(0,4)] = QString("Pan");
 
58
    contents[indexOf(0,5)] = QString("Rev");
 
59
    contents[indexOf(0,6)] = QString("Chor");
 
60
    contents[indexOf(0,7)] = QString("Vol");
 
61
    contents[indexOf(0,8)] = QString("Bank");
 
62
 
 
63
    for (n=0; n<16; n++) {
 
64
        c_flags[n] = 0;
 
65
        t_expression[n] = 0;
 
66
        t_panning[n] = 0;
 
67
        t_reverberation[n] = 0;
 
68
        t_chorusdepth[n] = 0;
 
69
        t_volume[n] = 0;
 
70
    }
 
71
}
 
72
 
 
73
 
 
74
/*
 
75
  Destructor: deallocates memory for contents
 
76
*/
 
77
 
 
78
Table::~Table()
 
79
{
 
80
    delete[] contents;                          // deallocation
 
81
}
 
82
 
 
83
int Table::cellWidth( int col )
 
84
{
 
85
   return cell_width[col];
 
86
}
 
87
 
 
88
void Table::clearChannels( void )
 
89
{
 
90
    int chan;
 
91
    for (chan = 1; chan < NUMROWS; chan++) {
 
92
        contents[indexOf( chan, 1 )] = QString::null;
 
93
        contents[indexOf( chan, 2 )] = QString::null;
 
94
        contents[indexOf( chan, 3 )] = QString::null;
 
95
        contents[indexOf( chan, 4 )] = QString::null;
 
96
        contents[indexOf( chan, 5 )] = QString::null;
 
97
        contents[indexOf( chan, 6 )] = QString::null;
 
98
        contents[indexOf( chan, 7 )] = QString::null;
 
99
        contents[indexOf( chan, 8 )] = QString::null;
 
100
        t_expression[chan-1] = 0;
 
101
        c_flags[chan-1] = 0;
 
102
        t_panning[chan-1] = 0;
 
103
        t_reverberation[chan-1] = 0;
 
104
        t_chorusdepth[chan-1] = 0;
 
105
        t_volume[chan-1] = 0;
 
106
        updateCell( chan, 1 );
 
107
        updateCell( chan, 2 );
 
108
        updateCell( chan, 3 );
 
109
        updateCell( chan, 4 );
 
110
        updateCell( chan, 5 );
 
111
        updateCell( chan, 6 );
 
112
        updateCell( chan, 7 );
 
113
        updateCell( chan, 8 );
 
114
    }
 
115
}
 
116
 
 
117
void Table::setProgram( int chan, int val, const char *inst, int bank, int variationbank )
 
118
{
 
119
    if (chan < 0 || chan > 15) return;
 
120
    chan++;
 
121
    contents[indexOf( chan, 1 )] = QString(inst);
 
122
    contents[indexOf( chan, 2 )].setNum(val);
 
123
        updateCell( chan, 1 );
 
124
        updateCell( chan, 2 );
 
125
    if (variationbank) contents[indexOf( chan, 8 )].setNum(variationbank);
 
126
    else contents[indexOf( chan, 8 )].setNum(bank);
 
127
        updateCell( chan, 8 );
 
128
}
 
129
 
 
130
 
 
131
void Table::setExpression( int chan, int val )
 
132
{
 
133
    if (chan < 0 || chan > 15) return;
 
134
    int wid = (cell_width[3]-8)*val/128;
 
135
 
 
136
    contents[indexOf( chan+1, 3 )].setNum(wid);
 
137
    t_expression[chan] = wid;
 
138
        updateCell( chan+1, 3, false );
 
139
}
 
140
 
 
141
void Table::setPanning( int chan, int val )
 
142
{
 
143
    if (chan < 0 || chan > 15) return;
 
144
    int wid = (cell_width[4]/2 - 4)*(val - 64)/64;
 
145
    contents[indexOf( chan+1, 4 )].setNum(wid);
 
146
    t_panning[chan] = wid;
 
147
        updateCell( chan+1, 4, false );
 
148
}
 
149
 
 
150
void Table::setReverberation( int chan, int val )
 
151
{
 
152
    if (chan < 0 || chan > 15) return;
 
153
    int wid = (cell_width[5]-8)*val/128;
 
154
    contents[indexOf( chan+1, 5 )].setNum(wid);
 
155
    t_reverberation[chan] = wid;
 
156
        updateCell( chan+1, 5, false );
 
157
}
 
158
 
 
159
void Table::setChorusDepth( int chan, int val )
 
160
{
 
161
    if (chan < 0 || chan > 15) return;
 
162
    int wid = (cell_width[6]-8)*val/128;
 
163
    contents[indexOf( chan+1, 6 )].setNum(wid);
 
164
    t_chorusdepth[chan] = wid;
 
165
        updateCell( chan+1, 6, false );
 
166
}
 
167
 
 
168
void Table::setVolume( int chan, int val )
 
169
{
 
170
    if (chan < 0 || chan > 15) return;
 
171
    int wid = (cell_width[7]-8)*val/128;
 
172
    contents[indexOf( chan+1, 7 )].setNum(wid);
 
173
    t_volume[chan] = wid;
 
174
        updateCell( chan+1, 7, false );
 
175
}
 
176
 
 
177
/*
 
178
  Return content of cell
 
179
*/
 
180
 
 
181
QString Table::cellContent( int row, int col ) const
 
182
{
 
183
    return contents[indexOf( row, col )];       // contents array lookup
 
184
}
 
185
 
 
186
 
 
187
/*
 
188
  Set content of cell
 
189
*/
 
190
 
 
191
void Table::setCellContent( int row, int col, const char* c )
 
192
{
 
193
    contents[indexOf( row, col )] = c;          // contents lookup and assign
 
194
    updateCell( row, col );                     // show new content
 
195
}
 
196
 
 
197
 
 
198
/*
 
199
  Handles cell painting for the Table widget.
 
200
*/
 
201
 
 
202
void Table::paintCell( QPainter* p, int row, int col )
 
203
{
 
204
    int w = cellWidth( col );                   // width of cell in pixels
 
205
    int h = cellHeight( row );                  // height of cell in pixels
 
206
    int x2 = w - 1;
 
207
    int y2 = h - 1;
 
208
    int chan = row-1;
 
209
 
 
210
    /*
 
211
      Draw our part of cell frame.
 
212
    */
 
213
    p->drawLine( x2, 0, x2, y2 );               // draw vertical line on right
 
214
    p->drawLine( 0, y2, x2, y2 );               // draw horiz. line at bottom
 
215
 
 
216
    if (!row) p->drawLine( 0,  0, x2,  0 );             // horiz. at top
 
217
    if (!col) p->drawLine( 0,  0,  0, y2 );             // vert. on left
 
218
 
 
219
    /*
 
220
      Draw cell content (text)
 
221
    */
 
222
 
 
223
    if (row==0 || col==0 || col==2 || col==8)
 
224
        p->drawText( 0, 0, w, h, AlignCenter, contents[indexOf(row,col)] );
 
225
 
 
226
    else if (col==1)
 
227
        p->drawText( 3, 0, w, h, AlignLeft, contents[indexOf(row,col)] );
 
228
 
 
229
    else if (col==3) {
 
230
        int wid = t_expression[chan];
 
231
        if (wid < w-8) p->fillRect(4+wid,4,w-8-wid,h-8, backgroundColor());
 
232
        if (wid > 0) {
 
233
            if (c_flags[chan] & FLAG_PERCUSSION)
 
234
                p->fillRect(4,4,wid,h-8, QColor("yellow"));
 
235
            else p->fillRect(4,4,wid,h-8, QColor("orange"));
 
236
        }
 
237
    }
 
238
    else if (col==4) {
 
239
        int wid = t_panning[chan];
 
240
        if (wid > 0) {
 
241
            p->fillRect(4,4,(w-8)/2,h-8, backgroundColor());
 
242
            if (wid < w/2-4) p->fillRect(w/2+wid,4,w/2-4-wid,h-8, backgroundColor());
 
243
            p->fillRect(w/2,4,wid,h-8, QColor("green"));
 
244
        }
 
245
        else if (wid < 0) {
 
246
            p->fillRect(w/2,4,(w-8)/2,h-8, backgroundColor());
 
247
            if (-wid < w/2-4) p->fillRect(4,4,w/2-4+wid,h-8, backgroundColor());
 
248
            p->fillRect(w/2 + wid,4,-wid,h-8, QColor("blue"));
 
249
        }
 
250
        else p->fillRect(4,4,w-8,h-8, backgroundColor());
 
251
    }
 
252
    else if (col==5) {
 
253
        int wid = t_reverberation[chan];
 
254
        if (wid < w-8) p->fillRect(4+wid,4,w-8-wid,h-8, backgroundColor());
 
255
        if (wid > 0) {
 
256
            p->fillRect(4,4,wid,h-8, QColor("SlateBlue2"));
 
257
        }
 
258
    }
 
259
    else if (col==6) {
 
260
        int wid = t_chorusdepth[chan];
 
261
        if (wid < w-8) p->fillRect(4+wid,4,w-8-wid,h-8, backgroundColor());
 
262
        if (wid > 0) {
 
263
            p->fillRect(4,4,wid,h-8, QColor("coral2"));
 
264
        }
 
265
    }
 
266
    else if (col==7) {
 
267
        int wid = t_volume[chan];
 
268
        if (wid < w-8) p->fillRect(4+wid,4,w-8-wid,h-8, backgroundColor());
 
269
        if (wid > 0) {
 
270
            if (c_flags[chan] & FLAG_PERCUSSION)
 
271
                p->fillRect(4,4,wid,h-8, QColor("yellow"));
 
272
            else p->fillRect(4,4,wid,h-8, QColor("white"));
 
273
        }
 
274
    }
 
275
}
 
276
 
 
277
/*
 
278
  Utility function for mapping from 2D table to 1D array
 
279
*/
 
280
 
 
281
int Table::indexOf( int row, int col ) const
 
282
{
 
283
    return (row * numCols()) + col;
 
284
}
 
285
#include "table.moc"
 
286