slideraction.cpp 13.4 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 49
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name)
    {
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
////////////////////////////////////////////////////////////////////////////////

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
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 maxValue() - value();
99 100 101 102
}

void VolumeSlider::setVolume(int value)
{
103
    if(orientation() == Qt::Horizontal)
104 105
        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, 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
QWidget *SliderAction::createToolBarWidget( QToolBar * parent )
{
    if(parent) {
        QWidget *base = new QWidget(parent);
        base->setBackgroundRole(parent->backgroundRole());
        base->setName("kde toolbar widget");

        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);
        trackPositionLabel->setName("kde toolbar widget");
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
        trackPositionLabel->setToolTip( i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);

        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
        m_trackPositionSlider->setOrientation(orientation);
        m_trackPositionSlider->setMaxValue(maxPosition);
        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);
        volumeLabel->setName("kde toolbar widget");
        volumeLabel->setPixmap(SmallIcon("player_volume"));
        volumeLabel->setToolTip( i18n("Volume"));
        m_layout->addWidget(volumeLabel);

        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
        m_volumeSlider->setMaxValue(100);
        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()));

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

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

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

        return base;
    }
    else
        return 0;
}

260
////////////////////////////////////////////////////////////////////////////////
261 262 263
// public slots
////////////////////////////////////////////////////////////////////////////////

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

268 269 270
    if(!m_toolBar)
        return;

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

////////////////////////////////////////////////////////////////////////////////
// private members
286 287 288 289
////////////////////////////////////////////////////////////////////////////////

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

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

297
        Qt::Orientation orientation = Qt::Horizontal;
298

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

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

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

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

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

323 324 325 326
        m_layout->addItem(new QSpacerItem(10, 1));

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

331
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
332
        m_volumeSlider->setMaxValue(100);
Laurent Montel's avatar
Laurent Montel committed
333
        m_volumeSlider->setToolTip( i18n("Volume"));
334
        m_layout->addWidget(m_volumeSlider);
335 336 337
        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()));
338

339
        m_volumeSlider->setName("kde toolbar widget");
340
        m_trackPositionSlider->setName("kde toolbar widget");
341

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

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

347
        return base;
348
    }
349
    else
350
        return 0;
351 352 353 354 355 356
}

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

357
void SliderAction::slotUpdateSize()
358
{
359 360 361
    static const int offset = 3;
    static const int absoluteMax = 10000;

362 363 364
    if(!m_toolBar)
        return;

365 366
    if(m_toolBar->orientation() == Qt::Vertical) {
        m_volumeSlider->setMaximumWidth(m_toolBar->iconSize().width() - offset);
367
        m_volumeSlider->setMaximumHeight(volumeMax);
368

369
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize().width() - offset);
370 371 372
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
373
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize().height() - offset);
374
        m_volumeSlider->setMaximumWidth(volumeMax);
375

376
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize().height() - offset);
377
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
378
    }
379
}
380

381 382 383 384 385 386 387 388 389
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

392 393 394 395 396 397 398 399 400 401 402 403 404
void SliderAction::slotVolumeSliderPressed()
{
    m_volumeDragging = true;
}

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

void SliderAction::slotToolbarDestroyed()
{
405
#if 0 // what's going on here?
406 407 408 409
    int index = findContainer(m_toolBar);
    if(index != -1)
        removeContainer(index);

410
#endif
411 412 413 414 415 416 417 418 419
    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;
}

420
#include "slideraction.moc"
421

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