summaryrefslogtreecommitdiffstats
path: root/amarok/src/scripts/graphequalizer/equalizercanvasview.cpp
blob: aaea599a24c2e0c79606b4e8c3c82a4def98944c (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
/*
 *   Copyright (C) 2005 by Ian Monroe
 *   Released under GPL 2 or later, see COPYING
 */


#include "equalizercanvasview.h"

#include <tqlabel.h>
#include <tqcanvas.h>
#include <tqpen.h>

#include <dcopclient.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>

EqualizerCanvasView::EqualizerCanvasView(TQWidget *tqparent = 0, const char *name = 0)
    : TQCanvasView(tqparent, name)
{
    m_pen.setWidth(5);
    m_circleList = new TQPtrList<EqualizerCircle>();
}

//called after setCanvas
void
EqualizerCanvasView::init()
{

//     TQCanvasLine* line = new TQCanvasLine(this->canvas());
//     line->setPoints(0,100,400,100);
//     line->setPen(TQPen(m_pen));
//     line->setBrush(TQBrush(TQt::black));
    TQCanvasLine* lineLeft = makeLine(TQPoint(0,canvas()->height()/2)
                ,TQPoint(canvas()->width()/2,canvas()->height()/2));
    TQCanvasLine* lineRight= makeLine(TQPoint(canvas()->width()/2,canvas()->height()/2)
                ,TQPoint(canvas()->width(),canvas()->height()/2));
    EqualizerCircle* circleMid = new EqualizerCircle(canvas()->width()/2
              ,canvas()->height()/2
              ,canvas()
              ,lineLeft
              ,lineRight
              ,m_circleList);
    EqualizerCircle* circleLeft = new EqualizerCircle(0,100,this->canvas(),NULL,lineLeft,m_circleList);
    EqualizerCircle* circleRight =  new EqualizerCircle(400,100,this->canvas(),lineRight,NULL,m_circleList);
    circleLeft->show();
    circleRight->show();
    circleMid->show();
//    line->show();
    this->canvas()->update();
}

void
EqualizerCanvasView::contentsMousePressEvent(TQMouseEvent *event)
{
    TQCanvasItemList items = canvas()->collisions(event->pos());
    if (items.empty())
        m_selectedItem = 0;
    else
        m_selectedItem = *(items.begin());
}


void
EqualizerCanvasView::contentsMouseDoubleClickEvent(TQMouseEvent *event)
{
    if (event->button() == LeftButton && m_selectedItem
            && m_selectedItem->rtti() == TQCanvasLine::RTTI)
    {
        TQCanvasLine* line = (TQCanvasLine*) m_selectedItem;
        int y = getY(event->x());

        EqualizerCircle* circle = new EqualizerCircle(event->x(), y
              ,canvas()
              ,makeLine(line->startPoint(),TQPoint(event->x(),event->y()))
              ,makeLine(TQPoint(event->x(),event->y()),line->endPoint())
              ,m_circleList);
        //kdDebug() << event->x() << 'x' << y << " cursor at " << event->x() << 'x' << event->y() << endl;
        circle->show();
        canvas()->update();
        m_selectedItem=0;
        delete line;
        canvas()->update();
    }
}
void
EqualizerCanvasView::contentsMouseMoveEvent(TQMouseEvent *event)
{
    if ((event->state() & LeftButton) && m_selectedItem ) {
      //  kdDebug() << "dragging " << m_selectedItem->rtti() << endl;
        if (m_selectedItem->rtti() == TQCanvasEllipse::RTTI)
        {
            EqualizerCircle* circle = (EqualizerCircle*) m_selectedItem;
            circle->setLocation(event->pos());
      //      kdDebug() << "dragged" <<endl;
        }
    }
}
void
EqualizerCanvasView::contentsMouseReleaseEvent(TQMouseEvent */*event*/)
{
    if (m_selectedItem && m_selectedItem->rtti() == TQCanvasEllipse::RTTI)
    {
        emit eqChanged();
    }
}

/**
 * m =(y1-y2)/(x1-x2)
 * b = m*x1 - y1
 * y = m*xCoord -b
 */
int
EqualizerCanvasView::getY(int xCoord)
{
//     int y1 = line->startPoint().y();
//     int y2 = line->endPoint().y();
//     int x1 = line->startPoint().x();
//     int x2 = line->endPoint().x();
//     double slope = double(y1-y2) / double(x1-x2);
//     kdDebug() << slope << "= (" << y1 << " - " << y2 << ")/(" << x1  << " - " << x2 << ')' << endl;
//     double b = slope*double(x1 - y1);
//     kdDebug() << b << " = " << slope <<" * "<< x1 << " - " << y1 << endl;
//     double y = double(xCoord)*slope - b;
//     kdDebug() << y << " = " << xCoord << '*' << slope << " - " << b << endl;
    //TQPointArray* yAxis = new TQPointArray(canvas->height());
    //yAxis->makeEllipse(xCoord, canvas()->height()/2,1,canvas->height());
    //canvas()->collisions(yAxis,0,false);
    //delete yAxis;
    TQMemArray<int> collidedPoints(20);
    unsigned int collisionNum = 0;
    for(int i=0; i<canvas()->height();i++)
    {
        TQCanvasItemList list = canvas()->collisions(TQPoint(xCoord,i));
        if( ! list.isEmpty())
        {
            if(collidedPoints.size() <= collisionNum)
                collidedPoints.resize(collidedPoints.size()+100);//for the steep slops
            collidedPoints[collisionNum] = i;
            collisionNum++;
        }
    }
    collisionNum--;
    return collidedPoints[collisionNum] - collisionNum/2;
}

TQCanvasLine*
EqualizerCanvasView::makeLine(TQPoint startPoint, TQPoint endPoint)
{
    TQCanvasLine* line = new TQCanvasLine(canvas());
    line->setPoints(startPoint.x(),
                   startPoint.y(),
                   endPoint.x(),
                   endPoint.y());
    line->setZ(-50);
    line->setPen(TQPen(m_pen));
    line->show();
    return line;
}
TQValueList<int>
EqualizerCanvasView::currentSettings()
{
    int step = canvas()->width()/10;
    int i;
    TQValueList<int> ret;
    ret << (int)(m_circleList->first()->y() - 100)*-1;
    for(i=1; i<9; i++)
        ret << (getY(i*step) - 100)*-1;
    ret << (int)(m_circleList->last()->y() - 100)*-1;
//    kdDebug() << "sending " << ret << endl;
    return ret;
}

//////////////////////
//EqualizerCircle
//////////////////////
EqualizerCircle::EqualizerCircle(int x, int y, TQCanvas *canvas, TQCanvasLine* line1, TQCanvasLine* line2,
TQPtrList<EqualizerCircle>* circleList )
: TQCanvasEllipse(15, 15, canvas)
{
    m_line1 = line1;
    m_line2 = line2;
    setBrush(TQBrush(TQt::blue));
    move(x,y);
    m_circleList = circleList;

    EqualizerCircle* it;
    int index = -1;
    bool inserted = false;
    for ( it = m_circleList->first(); it; it = m_circleList->next() )
    {
        if(it->x() > x)
        {
            index = m_circleList->tqfind(it);
            //if(index != 0)
            //{
                m_circleList->insert(index,this);
                inserted = true;
            //}
            break;
        }
    }
    if( !inserted )
        m_circleList->append(this);

    //clean up the loose pointer for the line that was split
    unsigned int circleIndex = m_circleList->tqfind(this);
    if(circleIndex > 0)
        m_circleList->at(circleIndex-1)->setLine(RIGHT,m_line1);
    if(circleIndex < m_circleList->count()-1)
         m_circleList->at(circleIndex+1)->setLine(LEFT,m_line2);
}

void EqualizerCircle::setLocation(const TQPoint &newLocation)
{
    unsigned int circleIndex = m_circleList->tqfind(this);
    double xMin, xMax;
    TQPoint correctedLoc(newLocation);
    if(m_line1 == 0 || m_line2 == 0)
    {//if the line is on the edges of the board, make it only move vertically
        correctedLoc.setX( (int) x());
    }
    else
    {
        xMin = 0; xMax = canvas()->width();
        if(circleIndex > 0)
        {
        // kdDebug() << "circleIndex " << circleIndex << endl;
            xMin = m_circleList->at(circleIndex - 1)->x();
        }
        if(circleIndex < m_circleList->count()-1)
        {
            //kdDebug() << "circleIndex " << circleIndex << " count " << m_circleList->count() << endl;
            xMax = m_circleList->at(circleIndex + 1)->x();
        }
        if(newLocation.x() < xMin)
        {
            //kdDebug() << "set " << newLocation.x() << " to xMin " << xMin << endl;
            correctedLoc.setX( (int) (xMin + 1.0) );
        }
        else if (newLocation.x() > xMax)
        {
        //kdDebug() << "set " << newLocation.x() << " to xMax " << xMax << endl;
            correctedLoc.setX( (int) (xMax - 1.0) );
        }
    }
    if(newLocation.y() > canvas()->height())
        correctedLoc.setY(canvas()->height());
    else if (newLocation.y() < 0)
        correctedLoc.setY(0);
    move(correctedLoc.x(), correctedLoc.y());
    if (m_line1) //account for circles on the edge
        m_line1->setPoints( m_line1->startPoint().x()
                       ,m_line1->startPoint().y()
                       ,correctedLoc.x()
                       ,correctedLoc.y());
    if(m_line2)
        m_line2->setPoints(correctedLoc.x()
                       , correctedLoc.y()
                       , m_line2->endPoint().x()
                       , m_line2->endPoint().y());
    canvas()->update();
}

void EqualizerCircle::setLine(WhichLine lineNum, TQCanvasLine* line)
{
    if(lineNum == LEFT)
        m_line1 = line;
    else
        m_line2 = line;
}

void CallAmarok::updateEq()
{
        TQByteArray data;
        TQDataStream arg(data, IO_WriteOnly);
        arg << m_preampSlider->value() *-1;
        TQValueList<int> cs = m_canvasView->currentSettings();
        TQValueList<int>::iterator it;
        for ( it = cs.begin(); it != cs.end(); ++it )
            arg << (*it);
        KApplication::dcopClient()->send("amarok", "player",
            "setEqualizer(int,int,int,int,int,int,int,int,int,int,int)"
            , data);
}


#include "equalizercanvasview.moc"