slideraction.cpp 9.43 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
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);
150

151 152 153 154 155 156 157
	int id = KAction::getToolButtonID();

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

	addContainer(m_toolBar, id);

	connect(m_toolBar, SIGNAL(destroyed()), this, SLOT(slotDestroyed()));
158 159 160 161
	connect(m_toolBar, SIGNAL(orientationChanged(Orientation)),
		this, SLOT(slotUpdateOrientation()));
	connect(m_toolBar, SIGNAL(placeChanged(QDockWindow::Place)),
		this, SLOT(slotUpdateOrientation()));
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

	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;
        }
    }
}

185
////////////////////////////////////////////////////////////////////////////////
186 187 188
// public slots
////////////////////////////////////////////////////////////////////////////////

189
void SliderAction::slotUpdateOrientation()
190
{
191
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
192 193 194 195 196 197 198 199 200 201

    if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
	m_trackPositionSlider->setOrientation(Vertical);
	m_volumeSlider->setOrientation(Vertical);
	m_layout->setDirection(QBoxLayout::TopToBottom);
    }
    else {
	m_trackPositionSlider->setOrientation(Horizontal);
	m_volumeSlider->setOrientation(Horizontal);
	m_layout->setDirection(QBoxLayout::LeftToRight);
202
    }
203
    slotUpdateSize();
204 205 206 207
}

////////////////////////////////////////////////////////////////////////////////
// private members
208 209 210 211
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
212 213
    if(parent) {
        QWidget *base = new QWidget(parent);
214 215 216
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

217 218 219 220 221 222 223 224 225 226 227
        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;
228 229 230 231

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

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

233 234
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
235
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
236 237
        QToolTip::add(trackPositionLabel, i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);
238

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

246 247 248 249
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
250
        volumeLabel->setPixmap(SmallIcon("player_volume"));
251
        QToolTip::add(volumeLabel, i18n("Volume"));
252
        m_layout->addWidget(volumeLabel);
253

254
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
255 256 257
        m_volumeSlider->setMaxValue(100);
        QToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
258

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

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

265
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
266

267
        return base;
268
    }
269
    else
270
        return 0;
271 272 273 274 275 276
}

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

277
void SliderAction::slotUpdateSize()
278
{
279 280 281
    static const int offset = 3;
    static const int absoluteMax = 10000;

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

286
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
287 288 289
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
290
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
291
        m_volumeSlider->setMaximumWidth(volumeMax);
292

293
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
294
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
295
    }
296
}
297

298 299 300 301 302 303 304 305 306
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

309
#include "slideraction.moc"