summaryrefslogtreecommitdiffstats
path: root/juk/slideraction.cpp
blob: cf68043c7e70367bd5f4a15c7f33f48a75fbfdb3 (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
/***************************************************************************
    begin                : Wed Feb 6 2002
    copyright            : (C) 2002 - 2004 by Scott Wheeler
    email                : wheeler@kde.org
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <ktoolbar.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kdebug.h>

#include <tqtooltip.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqtimer.h>

#include "slideraction.h"

////////////////////////////////////////////////////////////////////////////////
// convenience class
////////////////////////////////////////////////////////////////////////////////

/**
 * This "custom" slider reverses the left and middle buttons.  Typically the
 * middle button "instantly" seeks rather than moving the slider towards the
 * click position in fixed intervals.  This behavior has now been mapped on
 * to the left mouse button.
 */

class TrackPositionSlider : public TQSlider
{
public:
    TrackPositionSlider(TQWidget *parent, const char *name) : TQSlider(parent, name)
    {
        setFocusPolicy(TQ_NoFocus);
    }

protected:
    virtual void mousePressEvent(TQMouseEvent *e)
    {
        if(e->button() == Qt::LeftButton) {
            TQMouseEvent reverse(TQEvent::MouseButtonPress, e->pos(), Qt::MidButton, e->state());
            TQSlider::mousePressEvent(&reverse);
            emit sliderPressed();
        }
        else if(e->button() == Qt::MidButton) {
            TQMouseEvent reverse(TQEvent::MouseButtonPress, e->pos(), Qt::LeftButton, e->state());
            TQSlider::mousePressEvent(&reverse);
        }
    }
};

////////////////////////////////////////////////////////////////////////////////
// VolumeSlider implementation
////////////////////////////////////////////////////////////////////////////////

VolumeSlider::VolumeSlider(Qt::Orientation o, TQWidget *parent, const char *name) :
    TQSlider(o, parent, name)
{
    connect(this, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotValueChanged(int)));
}

void VolumeSlider::wheelEvent(TQWheelEvent *e)
{
    if(orientation() ==Qt::Horizontal) {
        TQWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());
        TQSlider::wheelEvent(&transposed);
    }
    else
        TQSlider::wheelEvent(e);
}

void VolumeSlider::focusInEvent(TQFocusEvent *)
{
    clearFocus();
}

int VolumeSlider::volume() const
{
    if(orientation() ==Qt::Horizontal)
        return value();
    else
        return maxValue() - value();    
}

void VolumeSlider::setVolume(int value)
{
    if(orientation() ==Qt::Horizontal)
        setValue(value);
    else
        setValue(maxValue() - value); 
}

void VolumeSlider::setOrientation(Qt::Orientation o)
{
    if(o == orientation())
        return;

    blockSignals(true);
    setValue(maxValue() - value());
    blockSignals(false);
    TQSlider::setOrientation(o);
}

void VolumeSlider::slotValueChanged(int value)
{
    if(orientation() ==Qt::Horizontal)
        emit signalVolumeChanged(value);
    else
        emit signalVolumeChanged(maxValue() - value);
}

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

const int SliderAction::minPosition = 0;
const int SliderAction::maxPosition = 1000;

SliderAction::SliderAction(const TQString &text, TQObject *parent, const char *name)
    : KAction(text, 0, parent, name),
      m_toolBar(0),
      m_layout(0),
      m_trackPositionSlider(0),
      m_volumeSlider(0),
      m_dragging(false),
      m_volumeDragging(false)
{

}

SliderAction::~SliderAction()
{

}

int SliderAction::plug(TQWidget *parent, int index)
{
    TQWidget *w = createWidget(parent);

    if(!w)
        return -1;

    // the check for null makes sure that there is only one toolbar that this is
    // "plugged" in to

    if(parent->inherits("KToolBar") && !m_toolBar) {
        m_toolBar = static_cast<KToolBar *>(parent);

        int id = KAction::getToolButtonID();

        m_toolBar->insertWidget(id, w->width(), w, index);

        addContainer(m_toolBar, id);

        connect(m_toolBar, TQT_SIGNAL(destroyed()), this, TQT_SLOT(slotToolbarDestroyed()));
        connect(m_toolBar, TQT_SIGNAL(orientationChanged(Qt::Orientation)),
                this, TQT_SLOT(slotUpdateOrientation()));
        connect(m_toolBar, TQT_SIGNAL(placeChanged(TQDockWindow::Place)),
                this, TQT_SLOT(slotUpdateOrientation()));

        slotUpdateOrientation();
        return (containerCount() - 1);
    }
    else
        slotUpdateOrientation();

    return -1;
}


void SliderAction::unplug(TQWidget *parent)
{
    if (parent->inherits("KToolBar")) {
        m_toolBar = static_cast<KToolBar *>(parent);

        int index = findContainer(m_toolBar);
        if (index != -1) {
            m_toolBar->removeItem(itemId(index));
            removeContainer(index);

            m_toolBar = 0;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void SliderAction::slotUpdateOrientation()
{
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar

    if(!m_toolBar)
        return;

    if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
        m_trackPositionSlider->setOrientation(Qt::Vertical);
        m_volumeSlider->setOrientation(Qt::Vertical);
        m_layout->setDirection(TQBoxLayout::TopToBottom);
    }
    else {
        m_trackPositionSlider->setOrientation(Qt::Horizontal);
        m_volumeSlider->setOrientation(Qt::Horizontal);
        m_layout->setDirection(TQBoxLayout::LeftToRight);
    }
    slotUpdateSize();
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

TQWidget *SliderAction::createWidget(TQWidget *parent) // virtual -- used by base class
{
    if(parent) {
        TQWidget *base = new TQWidget(parent);
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

        KToolBar *toolBar = dynamic_cast<KToolBar *>(parent);

        if(toolBar)
            toolBar->setStretchableWidget(base);

        Qt::Orientation orientation;

        if(toolBar && toolBar->barPos() == KToolBar::Right || toolBar->barPos() == KToolBar::Left)
            orientation =Qt::Vertical;
        else
            orientation =Qt::Horizontal;

        m_layout = new TQBoxLayout(base, TQBoxLayout::TopToBottom, 5, 5);

        m_layout->addItem(new TQSpacerItem(20, 1));

        TQLabel *trackPositionLabel = new TQLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
        TQToolTip::add(trackPositionLabel, i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);

        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
        m_trackPositionSlider->setMaxValue(maxPosition);
        TQToolTip::add(m_trackPositionSlider, i18n("Track position"));
        m_layout->addWidget(m_trackPositionSlider);
        connect(m_trackPositionSlider, TQT_SIGNAL(sliderPressed()), this, TQT_SLOT(slotSliderPressed()));
        connect(m_trackPositionSlider, TQT_SIGNAL(sliderReleased()), this, TQT_SLOT(slotSliderReleased()));

        m_layout->addItem(new TQSpacerItem(10, 1));

        TQLabel *volumeLabel = new TQLabel(base);
        volumeLabel->setName("kde toolbar widget");
        volumeLabel->setPixmap(SmallIcon("player_volume"));
        TQToolTip::add(volumeLabel, i18n("Volume"));
        m_layout->addWidget(volumeLabel);

        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
        m_volumeSlider->setMaxValue(100);
        TQToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
        connect(m_volumeSlider, TQT_SIGNAL(signalVolumeChanged(int)), TQT_SIGNAL(signalVolumeChanged(int)));
        connect(m_volumeSlider, TQT_SIGNAL(sliderPressed()), this, TQT_SLOT(slotVolumeSliderPressed()));
        connect(m_volumeSlider, TQT_SIGNAL(sliderReleased()), this, TQT_SLOT(slotVolumeSliderReleased()));

        m_volumeSlider->setName("kde toolbar widget");
        m_trackPositionSlider->setName("kde toolbar widget");

        m_layout->setStretchFactor(m_trackPositionSlider, 4);
        m_layout->setStretchFactor(m_volumeSlider, 1);

        connect(parent, TQT_SIGNAL(modechange()), this, TQT_SLOT(slotUpdateSize()));

        return base;
    }
    else
        return 0;
}

////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void SliderAction::slotUpdateSize()
{
    static const int offset = 3;
    static const int absoluteMax = 10000;

    if(!m_toolBar)
        return;

    if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
        m_volumeSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
        m_volumeSlider->setMaximumHeight(volumeMax);

        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
        m_volumeSlider->setMaximumWidth(volumeMax);

        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
    }
}

void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

void SliderAction::slotSliderReleased()
{
    m_dragging = false;
    emit signalPositionChanged(m_trackPositionSlider->value());
}

void SliderAction::slotVolumeSliderPressed()
{
    m_volumeDragging = true;
}

void SliderAction::slotVolumeSliderReleased()
{
    m_volumeDragging = false;
    emit signalVolumeChanged(m_volumeSlider->value());
}

void SliderAction::slotToolbarDestroyed()
{
    int index = findContainer(m_toolBar);
    if(index != -1)
        removeContainer(index);

    m_toolBar = 0;

    // This is probably a leak, but this code path hardly ever occurs, and it's
    // too hard to debug correctly.

    m_trackPositionSlider = 0;
    m_volumeSlider = 0;
}

#include "slideraction.moc"

// vim: set et sw=4 ts=4: