slideraction.cpp 10.9 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>
Laurent Montel's avatar
Laurent Montel committed
25 26 27 28 29 30
//Added by qt3to4:
#include <QMouseEvent>
#include <QWheelEvent>
#include <QFocusEvent>
#include <QEvent>
#include <QBoxLayout>
31 32 33

#include "slideraction.h"

34 35 36 37
////////////////////////////////////////////////////////////////////////////////
// convenience class
////////////////////////////////////////////////////////////////////////////////

38
/**
39
 * This "custom" slider reverses the left and middle buttons.  Typically the
40 41 42 43 44
 * 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.
 */

45 46 47
class TrackPositionSlider : public QSlider
{
public:
48 49 50 51
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name)
    {
        setFocusPolicy(NoFocus);
    }
52

53
protected:
54 55
    virtual void mousePressEvent(QMouseEvent *e)
    {
56
        if(e->button() == LeftButton) {
Laurent Montel's avatar
Laurent Montel committed
57
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), Qt::MidButton, e->state());
58
            QSlider::mousePressEvent(&reverse);
59
            emit sliderPressed();
60
        }
61
        else if(e->button() == MidButton) {
Laurent Montel's avatar
Laurent Montel committed
62
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), Qt::LeftButton, e->state());
63
            QSlider::mousePressEvent(&reverse);
64
        }
65 66 67
    }
};

68 69 70 71
////////////////////////////////////////////////////////////////////////////////
// VolumeSlider implementation
////////////////////////////////////////////////////////////////////////////////

Laurent Montel's avatar
Laurent Montel committed
72
VolumeSlider::VolumeSlider(Qt::Orientation o, QWidget *parent, const char *name) :
73
    QSlider(o, parent, name)
74
{
75 76
    connect(this, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
}
77

78 79 80
void VolumeSlider::wheelEvent(QWheelEvent *e)
{
    if(orientation() == Horizontal) {
81 82
        QWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());
        QSlider::wheelEvent(&transposed);
83
    }
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    else
        QSlider::wheelEvent(e);
}

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

int VolumeSlider::volume() const
{
    if(orientation() == Horizontal)
        return value();
    else
98
        return maxValue() - value();
99 100 101 102 103 104 105
}

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

Laurent Montel's avatar
Laurent Montel committed
109
void VolumeSlider::setOrientation(Qt::Orientation o)
110 111 112 113 114 115 116 117 118 119 120 121
{
    if(o == orientation())
        return;

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

void VolumeSlider::slotValueChanged(int value)
{
Laurent Montel's avatar
Laurent Montel committed
122
    if(orientation() == Qt::Horizontal)
123 124 125 126
        emit signalVolumeChanged(value);
    else
        emit signalVolumeChanged(maxValue() - value);
}
127

128 129 130 131
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

132 133 134
const int SliderAction::minPosition = 0;
const int SliderAction::maxPosition = 1000;

135
SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
136
    : KAction(text, 0, parent, name),
137 138 139 140
      m_toolBar(0),
      m_layout(0),
      m_trackPositionSlider(0),
      m_volumeSlider(0),
141 142
      m_dragging(false),
      m_volumeDragging(false)
143
{
144

145 146 147 148
}

SliderAction::~SliderAction()
{
149

150 151
}

152 153 154 155 156
int SliderAction::plug(QWidget *parent, int index)
{
    QWidget *w = createWidget(parent);

    if(!w)
157
        return -1;
158 159 160 161 162

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

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

165
        int id = KAction::getToolButtonID();
166

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

169
        addContainer(m_toolBar, id);
170

171 172 173
        connect(m_toolBar, SIGNAL(destroyed()), this, SLOT(slotToolbarDestroyed()));
        connect(m_toolBar, SIGNAL(orientationChanged(Orientation)),
                this, SLOT(slotUpdateOrientation()));
Laurent Montel's avatar
Laurent Montel committed
174
        connect(m_toolBar, SIGNAL(placeChanged(Q3DockWindow::Place)),
175
                this, SLOT(slotUpdateOrientation()));
176

177 178
        slotUpdateOrientation();
        return (containerCount() - 1);
179
    }
180 181
    else
        slotUpdateOrientation();
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

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

202
////////////////////////////////////////////////////////////////////////////////
203 204 205
// public slots
////////////////////////////////////////////////////////////////////////////////

206
void SliderAction::slotUpdateOrientation()
207
{
208
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
209

210 211 212
    if(!m_toolBar)
        return;

213
    if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
214 215 216
        m_trackPositionSlider->setOrientation(Vertical);
        m_volumeSlider->setOrientation(Vertical);
        m_layout->setDirection(QBoxLayout::TopToBottom);
217 218
    }
    else {
219 220 221
        m_trackPositionSlider->setOrientation(Horizontal);
        m_volumeSlider->setOrientation(Horizontal);
        m_layout->setDirection(QBoxLayout::LeftToRight);
222
    }
223
    slotUpdateSize();
224 225 226 227
}

////////////////////////////////////////////////////////////////////////////////
// private members
228 229 230 231
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
232 233
    if(parent) {
        QWidget *base = new QWidget(parent);
234 235 236
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

237 238 239 240 241 242 243 244 245 246 247
        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;
248 249 250 251

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

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

253 254
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
255
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
Laurent Montel's avatar
Laurent Montel committed
256
        trackPositionLabel->setToolTip( i18n("Track position"));
257
        m_layout->addWidget(trackPositionLabel);
258

259
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
260
        m_trackPositionSlider->setMaxValue(maxPosition);
Laurent Montel's avatar
Laurent Montel committed
261
        m_trackPositionSlider->setToolTip( i18n("Track position"));
262
        m_layout->addWidget(m_trackPositionSlider);
263 264
        connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
        connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
265

266 267 268 269
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
270
        volumeLabel->setPixmap(SmallIcon("player_volume"));
Laurent Montel's avatar
Laurent Montel committed
271
        volumeLabel->setToolTip( i18n("Volume"));
272
        m_layout->addWidget(volumeLabel);
273

274
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
275
        m_volumeSlider->setMaxValue(100);
Laurent Montel's avatar
Laurent Montel committed
276
        m_volumeSlider->setToolTip( i18n("Volume"));
277
        m_layout->addWidget(m_volumeSlider);
278 279 280
        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()));
281

282
        m_volumeSlider->setName("kde toolbar widget");
283
        m_trackPositionSlider->setName("kde toolbar widget");
284

285 286
        m_layout->setStretchFactor(m_trackPositionSlider, 4);
        m_layout->setStretchFactor(m_volumeSlider, 1);
287

288
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
289

290
        return base;
291
    }
292
    else
293
        return 0;
294 295 296 297 298 299
}

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

300
void SliderAction::slotUpdateSize()
301
{
302 303 304
    static const int offset = 3;
    static const int absoluteMax = 10000;

305 306 307
    if(!m_toolBar)
        return;

308 309
    if(m_toolBar->barPos() == KToolBar::Right || m_toolBar->barPos() == KToolBar::Left) {
        m_volumeSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
310
        m_volumeSlider->setMaximumHeight(volumeMax);
311

312
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize() - offset);
313 314 315
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
316
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
317
        m_volumeSlider->setMaximumWidth(volumeMax);
318

319
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize() - offset);
320
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
321
    }
322
}
323

324 325 326 327 328 329 330 331 332
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
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;
}

361
#include "slideraction.moc"
362 363

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