slideraction.cpp 13.5 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
#include <kdebug.h>

21 22 23
#include <QToolTip>
#include <QLayout>
#include <QLabel>
24
#include <QTimer>
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
    TrackPositionSlider(QWidget *parent) : QSlider(parent)
49
    {
50
        setFocusPolicy(Qt::NoFocus);
51
    }
52

53
protected:
54 55
    virtual void mousePressEvent(QMouseEvent *e)
    {
56
        if(e->button() == Qt::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() == Qt::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
////////////////////////////////////////////////////////////////////////////////

72 73
VolumeSlider::VolumeSlider(Qt::Orientation o, QWidget *parent) :
    QSlider(o, parent)
74
{
75 76
    connect(this, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
}
77

78 79
void VolumeSlider::wheelEvent(QWheelEvent *e)
{
80
    if(orientation() == Qt::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
    else
        QSlider::wheelEvent(e);
}

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

int VolumeSlider::volume() const
{
95
    if(orientation() == Qt::Horizontal)
96 97
        return value();
    else
98
        return maximum() - value();
99 100 101 102
}

void VolumeSlider::setVolume(int value)
{
103
    if(orientation() == Qt::Horizontal)
104 105
        setValue(value);
    else
106
        setValue(maximum() - value);
107 108
}

Laurent Montel's avatar
Laurent Montel committed
109
void VolumeSlider::setOrientation(Qt::Orientation o)
110 111 112 113 114
{
    if(o == orientation())
        return;

    blockSignals(true);
115
    setValue(maximum() - value());
116 117 118 119 120 121
    blockSignals(false);
    QSlider::setOrientation(o);
}

void VolumeSlider::slotValueChanged(int value)
{
Laurent Montel's avatar
Laurent Montel committed
122
    if(orientation() == Qt::Horizontal)
123 124
        emit signalVolumeChanged(value);
    else
125
        emit signalVolumeChanged(maximum() - value);
126
}
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, KActionCollection* collection, const char *name)
Stephan Kulow's avatar
Stephan Kulow committed
136
    : KAction(text, collection, name),
137
      m_toolBar(0),
138
      m_widget(0),
139 140 141
      m_layout(0),
      m_trackPositionSlider(0),
      m_volumeSlider(0),
142 143
      m_dragging(false),
      m_volumeDragging(false)
144 145 146 147 148
{
}

SliderAction::~SliderAction()
{
149

150 151
}

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

156
    if(!m_widget)
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 166
//        m_toolBar->insertWidget(id, m_widget->width(), m_widget, index);
        m_toolBar->addWidget(m_widget);
167

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

174 175
        slotUpdateOrientation();
        return (containerCount() - 1);
176
    }
177 178
    else
        slotUpdateOrientation();
179 180 181 182 183 184 185 186

    return -1;
}


void SliderAction::unplug(QWidget *parent)
{
    if (parent->inherits("KToolBar")) {
187 188 189
        delete m_widget;
        m_widget = 0;
        m_toolBar = 0;
190 191 192
    }
}

193 194 195 196 197
QWidget *SliderAction::createToolBarWidget( QToolBar * parent )
{
    if(parent) {
        QWidget *base = new QWidget(parent);
        base->setBackgroundRole(parent->backgroundRole());
198
        base->setObjectName("kde toolbar widget");
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

        KToolBar *toolBar = dynamic_cast<KToolBar *>(parent);

        Qt::Orientation orientation = Qt::Horizontal;

        if(toolBar) {
//            toolBar->setStretchableWidget(base);
            orientation = toolBar->orientation();
        }

        m_layout = new QHBoxLayout(base);
        //m_layout->setDirection(QBoxLayout::TopToBottom);
	m_layout->setMargin(5);
        m_layout->setSpacing(5);

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

        QLabel *trackPositionLabel = new QLabel(base);
217
        trackPositionLabel->setObjectName("kde toolbar widget");
218 219 220 221
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
        trackPositionLabel->setToolTip( i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);

222 223
        m_trackPositionSlider = new TrackPositionSlider(base);
        m_trackPositionSlider->setObjectName("trackPositionSlider");
224
        m_trackPositionSlider->setOrientation(orientation);
225
        m_trackPositionSlider->setMaximum(maxPosition);
226 227 228 229 230 231 232 233
        m_trackPositionSlider->setToolTip( i18n("Track position"));
        m_layout->addWidget(m_trackPositionSlider);
        connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
        connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));

        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
234
        volumeLabel->setObjectName("kde toolbar widget");
235 236 237 238
        volumeLabel->setPixmap(SmallIcon("player_volume"));
        volumeLabel->setToolTip( i18n("Volume"));
        m_layout->addWidget(volumeLabel);

239 240 241
        m_volumeSlider = new VolumeSlider(orientation, base);
        m_volumeSlider->setObjectName("volumeSlider");
        m_volumeSlider->setMaximum(100);
242 243 244 245 246 247
        m_volumeSlider->setToolTip( i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
        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()));

248 249
        m_volumeSlider->setObjectName("kde toolbar widget");
        m_trackPositionSlider->setObjectName("kde toolbar widget");
250 251 252 253 254 255 256 257 258 259 260 261

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

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

        return base;
    }
    else
        return 0;
}

262
////////////////////////////////////////////////////////////////////////////////
263 264 265
// public slots
////////////////////////////////////////////////////////////////////////////////

266
void SliderAction::slotUpdateOrientation()
267
{
268
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
269

270 271 272
    if(!m_toolBar)
        return;

273
    if(m_toolBar->orientation() == Qt::Vertical) {
274 275
        m_trackPositionSlider->setOrientation(Qt::Vertical);
        m_volumeSlider->setOrientation(Qt::Vertical);
276
        m_layout->setDirection(QBoxLayout::TopToBottom);
277 278
    }
    else {
279 280
        m_trackPositionSlider->setOrientation(Qt::Horizontal);
        m_volumeSlider->setOrientation(Qt::Horizontal);
281
        m_layout->setDirection(QBoxLayout::LeftToRight);
282
    }
283
    slotUpdateSize();
284 285 286 287
}

////////////////////////////////////////////////////////////////////////////////
// private members
288 289 290 291
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
292 293
    if(parent) {
        QWidget *base = new QWidget(parent);
Stephan Kulow's avatar
Stephan Kulow committed
294
        base->setBackgroundRole(parent->backgroundRole());
295
        base->setObjectName("kde toolbar widget");
296

297 298
        KToolBar *toolBar = dynamic_cast<KToolBar *>(parent);

299
        Qt::Orientation orientation = Qt::Horizontal;
300

301 302 303 304
        if(toolBar) {
//            toolBar->setStretchableWidget(base);
            orientation = toolBar->orientation();
        }
305

Stephan Kulow's avatar
Stephan Kulow committed
306 307 308
        m_layout = new QBoxLayout(QBoxLayout::TopToBottom, base );
	m_layout->setMargin(5);
        m_layout->setSpacing(5);
309 310

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

312
        QLabel *trackPositionLabel = new QLabel(base);
313
        trackPositionLabel->setObjectName("kde toolbar widget");
314
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
Laurent Montel's avatar
Laurent Montel committed
315
        trackPositionLabel->setToolTip( i18n("Track position"));
316
        m_layout->addWidget(trackPositionLabel);
317

318 319 320
        m_trackPositionSlider = new TrackPositionSlider(base);
        m_trackPositionSlider->setObjectName("trackPositionSlider");
        m_trackPositionSlider->setMaximum(maxPosition);
Laurent Montel's avatar
Laurent Montel committed
321
        m_trackPositionSlider->setToolTip( i18n("Track position"));
322
        m_layout->addWidget(m_trackPositionSlider);
323 324
        connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
        connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
325

326 327 328
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
329
        volumeLabel->setObjectName("kde toolbar widget");
330
        volumeLabel->setPixmap(SmallIcon("player_volume"));
Laurent Montel's avatar
Laurent Montel committed
331
        volumeLabel->setToolTip( i18n("Volume"));
332
        m_layout->addWidget(volumeLabel);
333

334 335 336
        m_volumeSlider = new VolumeSlider(orientation, base);
        m_volumeSlider->setObjectName("volumeSlider");
        m_volumeSlider->setMaximum(100);
Laurent Montel's avatar
Laurent Montel committed
337
        m_volumeSlider->setToolTip( i18n("Volume"));
338
        m_layout->addWidget(m_volumeSlider);
339 340 341
        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()));
342

343 344
        m_volumeSlider->setObjectName("kde toolbar widget");
        m_trackPositionSlider->setObjectName("kde toolbar widget");
345

346 347
        m_layout->setStretchFactor(m_trackPositionSlider, 4);
        m_layout->setStretchFactor(m_volumeSlider, 1);
348

349
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
350

351
        return base;
352
    }
353
    else
354
        return 0;
355 356 357 358 359 360
}

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

361
void SliderAction::slotUpdateSize()
362
{
363 364 365
    static const int offset = 3;
    static const int absoluteMax = 10000;

366 367 368
    if(!m_toolBar)
        return;

369 370
    if(m_toolBar->orientation() == Qt::Vertical) {
        m_volumeSlider->setMaximumWidth(m_toolBar->iconSize().width() - offset);
371
        m_volumeSlider->setMaximumHeight(volumeMax);
372

373
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize().width() - offset);
374 375 376
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
377
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize().height() - offset);
378
        m_volumeSlider->setMaximumWidth(volumeMax);
379

380
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize().height() - offset);
381
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
382
    }
383
}
384

385 386 387 388 389 390 391 392 393
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

396 397 398 399 400 401 402 403 404 405 406 407 408
void SliderAction::slotVolumeSliderPressed()
{
    m_volumeDragging = true;
}

void SliderAction::slotVolumeSliderReleased()
{
    m_volumeDragging = false;
    emit signalVolumeChanged(m_volumeSlider->value());
}

void SliderAction::slotToolbarDestroyed()
{
409
#if 0 // what's going on here?
410 411 412 413
    int index = findContainer(m_toolBar);
    if(index != -1)
        removeContainer(index);

414
#endif
415 416 417 418 419 420 421 422 423
    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;
}

424
#include "slideraction.moc"
425

426
// vim: set et sw=4 tw=0 sta: