slideraction.cpp 8.63 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          slideraction.cpp  -  description
                             -------------------
    begin                : Wed Feb 6 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
***************************************************************************/
8 9 10 11 12 13 14 15 16 17

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

18
#include <ktoolbar.h>
19
#include <klocale.h>
20
#include <kiconloader.h>
21 22 23
#include <kdebug.h>

#include <qtooltip.h>
24
#include <qlayout.h>
25
#include <qlabel.h>
26
#include <qtimer.h>
27 28 29

#include "slideraction.h"

30 31 32 33
////////////////////////////////////////////////////////////////////////////////
// convenience class
////////////////////////////////////////////////////////////////////////////////

34
/**
35
 * This "custom" slider reverses the left and middle buttons.  Typically the
36 37 38 39 40
 * 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.
 */

41 42 43 44
class TrackPositionSlider : public QSlider
{
public:
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name) {}
45

46
protected:
47 48
    virtual void mousePressEvent(QMouseEvent *e)
    {
49 50
        if(e->button() == LeftButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), MidButton, e->state());
51
            QSlider::mousePressEvent(&reverse);
52
            emit sliderPressed();
53
        }
54 55
        else if(e->button() == MidButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), LeftButton, e->state());
56
            QSlider::mousePressEvent(&reverse);
57
        }
58
    }
59
    virtual void focusInEvent(QFocusEvent *) { clearFocus(); }
60 61
};

62 63 64 65 66 67
////////////////////////////////////////////////////////////////////////////////
// VolumeSlider implementation
////////////////////////////////////////////////////////////////////////////////

VolumeSlider::VolumeSlider(Orientation o, QWidget *parent, const char *name) :
    QSlider(o, parent, name)
68
{
69 70
    connect(this, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
}
71

72 73 74
void VolumeSlider::wheelEvent(QWheelEvent *e)
{
    if(orientation() == Horizontal) {
75 76
        QWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());
        QSlider::wheelEvent(&transposed);
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
    else
        QSlider::wheelEvent(e);
}

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

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

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

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

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

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

122 123 124 125 126
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
127 128
    : CustomAction(text, parent, name),
      m_dragging(false)
129
{
130

131 132 133 134
}

SliderAction::~SliderAction()
{
135

136 137 138
}

////////////////////////////////////////////////////////////////////////////////
139 140 141
// public slots
////////////////////////////////////////////////////////////////////////////////

142
void SliderAction::slotUpdateOrientation(QDockWindow *dockWindow)
143
{
144
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
145 146 147 148
    if((!dockWindow || dockWindow == dynamic_cast<QDockWindow *>(toolBar()))) {
        if(toolBar()->barPos() == KToolBar::Right || toolBar()->barPos() == KToolBar::Left) {
            m_trackPositionSlider->setOrientation(Vertical);
            m_volumeSlider->setOrientation(Vertical);
149
            m_layout->setDirection(QBoxLayout::TopToBottom);
150 151
        }
        else {
152 153
            m_trackPositionSlider->setOrientation(Horizontal);
            m_volumeSlider->setOrientation(Horizontal);
154
            m_layout->setDirection(QBoxLayout::LeftToRight);
155
        }
156
    }
157
    slotUpdateSize();
158 159 160 161
}

////////////////////////////////////////////////////////////////////////////////
// private members
162 163 164 165
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
166 167
    if(parent) {
        QWidget *base = new QWidget(parent);
168 169 170
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

171 172 173 174 175 176 177 178 179 180 181
        KToolBar *toolBar = dynamic_cast<KToolBar *>(parent);

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

        Orientation orientation;

        if(toolBar && toolBar->barPos() == KToolBar::Right || toolBar->barPos() == KToolBar::Left)
            orientation = Vertical;
        else
            orientation = Horizontal;
182 183 184 185

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

        m_layout->addItem(new QSpacerItem(20, 1));
186

187 188
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
189
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
190 191
        QToolTip::add(trackPositionLabel, i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);
192

193 194
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
        m_trackPositionSlider->setMaxValue(1000);
Stephan Binner's avatar
Stephan Binner committed
195
        QToolTip::add(m_trackPositionSlider, i18n("Track position"));
196
        m_layout->addWidget(m_trackPositionSlider);
197 198
	connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
	connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
199

200 201 202 203
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
204
        volumeLabel->setPixmap(SmallIcon("player_volume"));
205
        QToolTip::add(volumeLabel, i18n("Volume"));
206
        m_layout->addWidget(volumeLabel);
207

208
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
209 210 211
        m_volumeSlider->setMaxValue(100);
        QToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
212

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

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

219
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
220

221
        return base;
222
    }
223
    else
224
        return 0;
225 226 227 228 229 230
}

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

231
void SliderAction::slotUpdateSize()
232
{
233 234 235
    static const int offset = 3;
    static const int absoluteMax = 10000;

236 237 238
    if(toolBar()->barPos() == KToolBar::Right || toolBar()->barPos() == KToolBar::Left) {
        m_volumeSlider->setMaximumWidth(toolBar()->iconSize() - offset);
        m_volumeSlider->setMaximumHeight(volumeMax);
239

240 241 242 243 244 245
        m_trackPositionSlider->setMaximumWidth(toolBar()->iconSize() - offset);
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
        m_volumeSlider->setMaximumHeight(toolBar()->iconSize() - offset);
        m_volumeSlider->setMaximumWidth(volumeMax);
246

247 248
        m_trackPositionSlider->setMaximumHeight(toolBar()->iconSize() - offset);
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
249
    }
250
}
251

252 253 254 255 256 257 258 259 260
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

263
#include "slideraction.moc"