slideraction.cpp 9.52 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
class TrackPositionSlider : public QSlider
{
public:
44 45 46 47
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name)
    {
        setFocusPolicy(NoFocus);
    }
48

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

64 65 66 67 68 69
////////////////////////////////////////////////////////////////////////////////
// VolumeSlider implementation
////////////////////////////////////////////////////////////////////////////////

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

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

124 125 126 127 128
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
129 130
    : KAction(text, 0, parent, name),
      m_toolBar(0), m_dragging(false)
131
{
132

133 134 135 136
}

SliderAction::~SliderAction()
{
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
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;
        }
    }
}

182
////////////////////////////////////////////////////////////////////////////////
183 184 185
// public slots
////////////////////////////////////////////////////////////////////////////////

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

////////////////////////////////////////////////////////////////////////////////
// private members
206 207 208 209
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

244 245 246 247
        m_layout->addItem(new QSpacerItem(10, 1));

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

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

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

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

263
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
264

265
        return base;
266
    }
267
    else
268
        return 0;
269 270 271 272 273 274
}

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

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

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

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

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

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

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

307
#include "slideraction.moc"