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

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

16
#include <ktoolbar.h>
17
#include <klocale.h>
18
#include <kiconloader.h>
19 20 21
#include <kdebug.h>

#include <qtooltip.h>
22
#include <qlayout.h>
23
#include <qlabel.h>
24
#include <qtimer.h>
25 26 27

#include "slideraction.h"

28 29 30 31
////////////////////////////////////////////////////////////////////////////////
// convenience class
////////////////////////////////////////////////////////////////////////////////

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

39 40 41
class TrackPositionSlider : public QSlider
{
public:
42 43 44 45
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name)
    {
        setFocusPolicy(NoFocus);
    }
46

47
protected:
48 49
    virtual void mousePressEvent(QMouseEvent *e)
    {
50 51
        if(e->button() == LeftButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), MidButton, e->state());
52
            QSlider::mousePressEvent(&reverse);
53
            emit sliderPressed();
54
        }
55 56
        else if(e->button() == MidButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), LeftButton, e->state());
57
            QSlider::mousePressEvent(&reverse);
58
        }
59 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
    : KAction(text, 0, parent, name),
      m_toolBar(0), m_dragging(false)
129
{
130

131 132 133 134
}

SliderAction::~SliderAction()
{
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
int SliderAction::plug(QWidget *parent, int index)
{
    QWidget *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, SIGNAL(destroyed()), this, SLOT(slotDestroyed()));

	return (containerCount() - 1);
    }

    return -1;
}


void SliderAction::unplug(QWidget *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;
        }
    }
}

180
////////////////////////////////////////////////////////////////////////////////
181 182 183
// public slots
////////////////////////////////////////////////////////////////////////////////

184
void SliderAction::slotUpdateOrientation(QDockWindow *dockWindow)
185
{
186
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
187 188
    if((!dockWindow || dockWindow == dynamic_cast<QDockWindow *>(m_toolBar))) {
        if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
189 190
            m_trackPositionSlider->setOrientation(Vertical);
            m_volumeSlider->setOrientation(Vertical);
191
            m_layout->setDirection(QBoxLayout::TopToBottom);
192 193
        }
        else {
194 195
            m_trackPositionSlider->setOrientation(Horizontal);
            m_volumeSlider->setOrientation(Horizontal);
196
            m_layout->setDirection(QBoxLayout::LeftToRight);
197
        }
198
    }
199
    slotUpdateSize();
200 201 202 203
}

////////////////////////////////////////////////////////////////////////////////
// private members
204 205 206 207
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
208 209
    if(parent) {
        QWidget *base = new QWidget(parent);
210 211 212
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

213 214 215 216 217 218 219 220 221 222 223
        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;
224 225 226 227

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

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

229 230
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
231
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
232 233
        QToolTip::add(trackPositionLabel, i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);
234

235 236
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
        m_trackPositionSlider->setMaxValue(1000);
Stephan Binner's avatar
Stephan Binner committed
237
        QToolTip::add(m_trackPositionSlider, i18n("Track position"));
238
        m_layout->addWidget(m_trackPositionSlider);
239 240
	connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
	connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
241

242 243 244 245
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
246
        volumeLabel->setPixmap(SmallIcon("player_volume"));
247
        QToolTip::add(volumeLabel, i18n("Volume"));
248
        m_layout->addWidget(volumeLabel);
249

250
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
251 252 253
        m_volumeSlider->setMaxValue(100);
        QToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
254

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

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

261
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
262

263
        return base;
264
    }
265
    else
266
        return 0;
267 268 269 270 271 272
}

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

273
void SliderAction::slotUpdateSize()
274
{
275 276 277
    static const int offset = 3;
    static const int absoluteMax = 10000;

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

282
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
283 284 285
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
286
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
287
        m_volumeSlider->setMaximumWidth(volumeMax);
288

289
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
290
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
291
    }
292
}
293

294 295 296 297 298 299 300 301 302
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

305
#include "slideraction.moc"