slideraction.cpp 10.8 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
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

126 127 128
const int SliderAction::minPosition = 0;
const int SliderAction::maxPosition = 1000;

129
SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
130
    : KAction(text, 0, parent, name),
131 132 133 134
      m_toolBar(0),
      m_layout(0),
      m_trackPositionSlider(0),
      m_volumeSlider(0),
135 136
      m_dragging(false),
      m_volumeDragging(false)
137
{
138

139 140 141 142
}

SliderAction::~SliderAction()
{
143

144 145
}

146 147 148 149 150
int SliderAction::plug(QWidget *parent, int index)
{
    QWidget *w = createWidget(parent);

    if(!w)
151
        return -1;
152 153 154 155 156

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

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

159
        int id = KAction::getToolButtonID();
160

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

163
        addContainer(m_toolBar, id);
164

165 166 167 168 169
        connect(m_toolBar, SIGNAL(destroyed()), this, SLOT(slotToolbarDestroyed()));
        connect(m_toolBar, SIGNAL(orientationChanged(Orientation)),
                this, SLOT(slotUpdateOrientation()));
        connect(m_toolBar, SIGNAL(placeChanged(QDockWindow::Place)),
                this, SLOT(slotUpdateOrientation()));
170

171 172
        slotUpdateOrientation();
        return (containerCount() - 1);
173
    }
174 175
    else
        slotUpdateOrientation();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

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

196
////////////////////////////////////////////////////////////////////////////////
197 198 199
// public slots
////////////////////////////////////////////////////////////////////////////////

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

204 205 206
    if(!m_toolBar)
        return;

207
    if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
208 209 210
        m_trackPositionSlider->setOrientation(Vertical);
        m_volumeSlider->setOrientation(Vertical);
        m_layout->setDirection(QBoxLayout::TopToBottom);
211 212
    }
    else {
213 214 215
        m_trackPositionSlider->setOrientation(Horizontal);
        m_volumeSlider->setOrientation(Horizontal);
        m_layout->setDirection(QBoxLayout::LeftToRight);
216
    }
217
    slotUpdateSize();
218 219 220 221
}

////////////////////////////////////////////////////////////////////////////////
// private members
222 223 224 225
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
226 227
    if(parent) {
        QWidget *base = new QWidget(parent);
228 229 230
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

231 232 233 234 235 236 237 238 239 240 241
        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;
242 243 244 245

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

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

247 248
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
249
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
250 251
        QToolTip::add(trackPositionLabel, i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);
252

253
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
254
        m_trackPositionSlider->setMaxValue(maxPosition);
Stephan Binner's avatar
Stephan Binner committed
255
        QToolTip::add(m_trackPositionSlider, i18n("Track position"));
256
        m_layout->addWidget(m_trackPositionSlider);
257 258
        connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
        connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
259

260 261 262 263
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
264
        volumeLabel->setPixmap(SmallIcon("player_volume"));
265
        QToolTip::add(volumeLabel, i18n("Volume"));
266
        m_layout->addWidget(volumeLabel);
267

268
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
269 270 271
        m_volumeSlider->setMaxValue(100);
        QToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
272 273 274
        connect(m_volumeSlider, SIGNAL(signalVolumeChanged(int)), SIGNAL(signalVolumeChanged(int)));
        connect(m_volumeSlider, SIGNAL(sliderPressed()), this, SLOT(slotVolumeSliderPressed()));
        connect(m_volumeSlider, SIGNAL(sliderReleased()), this, SLOT(slotVolumeSliderReleased()));
275

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

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

282
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
283

284
        return base;
285
    }
286
    else
287
        return 0;
288 289 290 291 292 293
}

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

294
void SliderAction::slotUpdateSize()
295
{
296 297 298
    static const int offset = 3;
    static const int absoluteMax = 10000;

299 300 301
    if(!m_toolBar)
        return;

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

306
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
307 308 309
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
310
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
311
        m_volumeSlider->setMaximumWidth(volumeMax);
312

313
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
314
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
315
    }
316
}
317

318 319 320 321 322 323 324 325 326
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

void SliderAction::slotSliderReleased()
{
    m_dragging = false;
    emit signalPositionChanged(m_trackPositionSlider->value());
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
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;
}

355
#include "slideraction.moc"
356 357

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