slideraction.cpp 10.7 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
    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

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
// public slots
////////////////////////////////////////////////////////////////////////////////

198
void SliderAction::slotUpdateOrientation()
199
{
200
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
201

202 203 204
    if(!m_toolBar)
        return;

205
    if(m_toolBar->orientation() == Qt::Vertical) {
206 207
        m_trackPositionSlider->setOrientation(Qt::Vertical);
        m_volumeSlider->setOrientation(Qt::Vertical);
208
        m_layout->setDirection(QBoxLayout::TopToBottom);
209 210
    }
    else {
211 212
        m_trackPositionSlider->setOrientation(Qt::Horizontal);
        m_volumeSlider->setOrientation(Qt::Horizontal);
213
        m_layout->setDirection(QBoxLayout::LeftToRight);
214
    }
215
    slotUpdateSize();
216 217 218 219
}

////////////////////////////////////////////////////////////////////////////////
// private members
220 221 222 223
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
224 225
    if(parent) {
        QWidget *base = new QWidget(parent);
Stephan Kulow's avatar
Stephan Kulow committed
226
        base->setBackgroundRole(parent->backgroundRole());
227 228
        base->setName("kde toolbar widget");

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

231
        Qt::Orientation orientation = Qt::Horizontal;
232

233 234 235 236
        if(toolBar) {
//            toolBar->setStretchableWidget(base);
            orientation = toolBar->orientation();
        }
237 238 239 240

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

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

242 243
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
244
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
Laurent Montel's avatar
Laurent Montel committed
245
        trackPositionLabel->setToolTip( i18n("Track position"));
246
        m_layout->addWidget(trackPositionLabel);
247

248
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
249
        m_trackPositionSlider->setMaxValue(maxPosition);
Laurent Montel's avatar
Laurent Montel committed
250
        m_trackPositionSlider->setToolTip( i18n("Track position"));
251
        m_layout->addWidget(m_trackPositionSlider);
252 253
        connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
        connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
254

255 256 257 258
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
259
        volumeLabel->setPixmap(SmallIcon("player_volume"));
Laurent Montel's avatar
Laurent Montel committed
260
        volumeLabel->setToolTip( i18n("Volume"));
261
        m_layout->addWidget(volumeLabel);
262

263
        m_volumeSlider = new VolumeSlider(orientation, base, "volumeSlider");
264
        m_volumeSlider->setMaxValue(100);
Laurent Montel's avatar
Laurent Montel committed
265
        m_volumeSlider->setToolTip( i18n("Volume"));
266
        m_layout->addWidget(m_volumeSlider);
267 268 269
        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()));
270

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

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

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

279
        return base;
280
    }
281
    else
282
        return 0;
283 284 285 286 287 288
}

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

289
void SliderAction::slotUpdateSize()
290
{
291 292 293
    static const int offset = 3;
    static const int absoluteMax = 10000;

294 295 296
    if(!m_toolBar)
        return;

297 298
    if(m_toolBar->orientation() == Qt::Vertical) {
        m_volumeSlider->setMaximumWidth(m_toolBar->iconSize().width() - offset);
299
        m_volumeSlider->setMaximumHeight(volumeMax);
300

301
        m_trackPositionSlider->setMaximumWidth(m_toolBar->iconSize().width() - offset);
302 303 304
        m_trackPositionSlider->setMaximumHeight(absoluteMax);
    }
    else {
305
        m_volumeSlider->setMaximumHeight(m_toolBar->iconSize().height() - offset);
306
        m_volumeSlider->setMaximumWidth(volumeMax);
307

308
        m_trackPositionSlider->setMaximumHeight(m_toolBar->iconSize().height() - offset);
309
        m_trackPositionSlider->setMaximumWidth(absoluteMax);
310
    }
311
}
312

313 314 315 316 317 318 319 320 321
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

324 325 326 327 328 329 330 331 332 333 334 335 336
void SliderAction::slotVolumeSliderPressed()
{
    m_volumeDragging = true;
}

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

void SliderAction::slotToolbarDestroyed()
{
337
#if 0 // what's going on here?
338 339 340 341
    int index = findContainer(m_toolBar);
    if(index != -1)
        removeContainer(index);

342
#endif
343 344 345 346 347 348 349 350 351
    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;
}

352
#include "slideraction.moc"
353

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