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.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
    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 136
SliderAction::SliderAction(const QString &text, KActionCollection* collection, const char *name)
    : KAction(text, 0, 0, 0, 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
    setToolBarWidgetFactory(this);
146 147 148 149
}

SliderAction::~SliderAction()
{
150

151 152
}

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

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

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

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

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

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

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

    return -1;
}


void SliderAction::unplug(QWidget *parent)
{
    if (parent->inherits("KToolBar")) {
188 189 190
        delete m_widget;
        m_widget = 0;
        m_toolBar = 0;
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 260
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;
}

261
////////////////////////////////////////////////////////////////////////////////
262 263 264
// public slots
////////////////////////////////////////////////////////////////////////////////

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

269 270 271
    if(!m_toolBar)
        return;

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

////////////////////////////////////////////////////////////////////////////////
// private members
287 288 289 290
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

363 364 365
    if(!m_toolBar)
        return;

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

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

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

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

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

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

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

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

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

421
#include "slideraction.moc"
422

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