slideraction.cpp 13.6 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(Qt::Horizontal, parent)
49
    {
50
        setFocusPolicy(Qt::NoFocus);
51
    }
52

53
protected:
54 55
    virtual void mousePressEvent(QMouseEvent *e)
    {
56
        if(e->button() == Qt::LeftButton) {
Tim Beaulen's avatar
Tim Beaulen committed
57
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), Qt::MidButton, e->buttons(), e->modifiers());
58
            QSlider::mousePressEvent(&reverse);
59
            emit sliderPressed();
60
        }
61
        else if(e->button() == Qt::MidButton) {
Tim Beaulen's avatar
Tim Beaulen committed
62
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), Qt::LeftButton, e->buttons(), e->modifiers());
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) {
Tim Beaulen's avatar
Tim Beaulen committed
81
        QWheelEvent transposed(e->pos(), -(e->delta()), e->buttons(), e->modifiers(), e->orientation());
82
        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)
{
Tim Beaulen's avatar
Tim Beaulen committed
154 155
    Q_UNUSED(index)

156
    m_widget = createWidget(parent);
157

158
    if(!m_widget)
159
        return -1;
160 161 162 163 164

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

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

167 168
//        m_toolBar->insertWidget(id, m_widget->width(), m_widget, index);
        m_toolBar->addWidget(m_widget);
169

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

176
        slotUpdateOrientation();
Tim Beaulen's avatar
Tim Beaulen committed
177
        return (associatedWidgets().count() - 1);
178
    }
179 180
    else
        slotUpdateOrientation();
181 182 183 184 185 186 187 188

    return -1;
}


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

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

        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);
219
        trackPositionLabel->setObjectName("kde toolbar widget");
220 221 222 223
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
        trackPositionLabel->setToolTip( i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);

224 225
        m_trackPositionSlider = new TrackPositionSlider(base);
        m_trackPositionSlider->setObjectName("trackPositionSlider");
226
        m_trackPositionSlider->setOrientation(orientation);
227
        m_trackPositionSlider->setMaximum(maxPosition);
228 229 230 231 232 233 234 235
        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);
236
        volumeLabel->setObjectName("kde toolbar widget");
237 238 239 240
        volumeLabel->setPixmap(SmallIcon("player_volume"));
        volumeLabel->setToolTip( i18n("Volume"));
        m_layout->addWidget(volumeLabel);

241 242 243
        m_volumeSlider = new VolumeSlider(orientation, base);
        m_volumeSlider->setObjectName("volumeSlider");
        m_volumeSlider->setMaximum(100);
244 245 246 247 248 249
        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()));

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

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

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

        return base;
    }
    else
        return 0;
}

264
////////////////////////////////////////////////////////////////////////////////
265 266 267
// public slots
////////////////////////////////////////////////////////////////////////////////

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

272 273 274
    if(!m_toolBar)
        return;

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

////////////////////////////////////////////////////////////////////////////////
// private members
290 291 292 293
////////////////////////////////////////////////////////////////////////////////

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

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

301
        Qt::Orientation orientation = Qt::Horizontal;
302

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

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

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

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

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

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

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

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

345 346
        m_volumeSlider->setObjectName("kde toolbar widget");
        m_trackPositionSlider->setObjectName("kde toolbar widget");
347

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

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

353
        return base;
354
    }
355
    else
356
        return 0;
357 358 359 360 361 362
}

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

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

368 369 370
    if(!m_toolBar)
        return;

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

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

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

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

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

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

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

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

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

426
#include "slideraction.moc"
427

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