slideraction.cpp 7.48 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          slideraction.cpp  -  description
                             -------------------
    begin                : Wed Feb 6 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
***************************************************************************/
8 9 10 11 12 13 14 15 16 17

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

18
#include <ktoolbar.h>
19
#include <klocale.h>
20
#include <kiconloader.h>
21 22 23
#include <kdebug.h>

#include <qtooltip.h>
24
#include <qlayout.h>
25
#include <qlabel.h>
26
#include <qslider.h>
27 28 29

#include "slideraction.h"

30 31 32 33
////////////////////////////////////////////////////////////////////////////////
// convenience class
////////////////////////////////////////////////////////////////////////////////

34
/**
35
 * This "custom" slider reverses the left and middle buttons.  Typically the
36 37 38 39 40
 * 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.
 */

41 42 43 44
class TrackPositionSlider : public QSlider
{
public:
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name) {}
45

46
protected:
47 48 49
    virtual void mousePressEvent(QMouseEvent *e) {
        if(e->button() == LeftButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), MidButton, e->state());
50
            QSlider::mousePressEvent(&reverse);
51
            emit sliderPressed();
52
        }
53 54
        else if(e->button() == MidButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), LeftButton, e->state());
55
            QSlider::mousePressEvent(&reverse);
56
        }
57
    }
58
    virtual void focusInEvent(QFocusEvent *) { clearFocus(); }
59 60 61 62 63 64 65 66
};

class VolumeSlider : public QSlider
{
public:
    VolumeSlider(QWidget *parent, const char *name) : QSlider(parent, name) {}

protected:
67 68 69
    virtual void wheelEvent(QWheelEvent *e) {
        QWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());
        QSlider::wheelEvent(&transposed);
70
    }
71
    virtual void focusInEvent(QFocusEvent *) { clearFocus(); }
72 73
};

74 75 76 77 78
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
79 80
    : CustomAction(text, parent, name),
      m_dragging(false)
81
{
82

83 84 85 86
}

SliderAction::~SliderAction()
{
87

88 89 90
}

////////////////////////////////////////////////////////////////////////////////
91 92 93
// public slots
////////////////////////////////////////////////////////////////////////////////

94
void SliderAction::slotUpdateOrientation(QDockWindow *dockWindow)
95
{
96 97 98
    // if the toolbar is not null and either the dockWindow not defined or is the toolbar
    if(customWidget && toolbar && (!dockWindow || dockWindow == dynamic_cast<QDockWindow *>(toolbar))) {
        if(toolbar->barPos() == KToolBar::Right || toolbar->barPos() == KToolBar::Left) {
99 100 101
            m_trackPositionSlider->setOrientation(Qt::Vertical);
            m_volumeSlider->setOrientation(Qt::Vertical);
            m_layout->setDirection(QBoxLayout::TopToBottom);
102 103
        }
        else {
104 105 106
            m_trackPositionSlider->setOrientation(Qt::Horizontal);
            m_volumeSlider->setOrientation(Qt::Horizontal);
            m_layout->setDirection(QBoxLayout::LeftToRight);
107
        }
108
    }
109
    slotUpdateSize();
110 111 112 113
}

////////////////////////////////////////////////////////////////////////////////
// private members
114 115 116 117
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
118 119
    if(parent) {
        QWidget *base = new QWidget(parent);
120 121 122 123 124 125 126 127 128 129
        base->setBackgroundMode(parent->backgroundMode());
        base->setName("kde toolbar widget");

        QToolBar *toolbar = dynamic_cast<QToolBar *>(parent);
        if(toolbar)
            toolbar->setStretchableWidget(base);

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

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

131 132
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
133
        trackPositionLabel->setPixmap(SmallIcon("player_time"));
134 135
        QToolTip::add(trackPositionLabel, i18n("Track position"));
        m_layout->addWidget(trackPositionLabel);
136

137 138
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
        m_trackPositionSlider->setMaxValue(1000);
Stephan Binner's avatar
Stephan Binner committed
139
        QToolTip::add(m_trackPositionSlider, i18n("Track position"));
140
        m_layout->addWidget(m_trackPositionSlider);
141 142
	connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
	connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
143

144 145 146 147
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
148
        volumeLabel->setPixmap(SmallIcon("player_volume"));
149
        QToolTip::add(volumeLabel, i18n("Volume"));
150
        m_layout->addWidget(volumeLabel);
151 152

        m_volumeSlider = new VolumeSlider(base, "volumeSlider");
153 154 155
        m_volumeSlider->setMaxValue(100);
        QToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
156

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

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

163
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
164
        return base;
165
    }
166
    else
167
        return 0;
168 169 170 171 172 173
}

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

174
void SliderAction::slotUpdateSize()
175
{
176 177 178 179 180
    static const int offset = 3;
    static const int absoluteMax = 10000;

    if(customWidget && toolbar) {
        if(toolbar->barPos() == KToolBar::Right || toolbar->barPos() == KToolBar::Left) {
181 182
            m_volumeSlider->setMaximumWidth(toolbar->iconSize() - offset);
            m_volumeSlider->setMaximumHeight(volumeMax);
183

184 185
            m_trackPositionSlider->setMaximumWidth(toolbar->iconSize() - offset);
            m_trackPositionSlider->setMaximumHeight(absoluteMax);
186 187
        }
        else {
188 189
            m_volumeSlider->setMaximumHeight(toolbar->iconSize() - offset);
            m_volumeSlider->setMaximumWidth(volumeMax);
190

191 192
            m_trackPositionSlider->setMaximumHeight(toolbar->iconSize() - offset);
            m_trackPositionSlider->setMaximumWidth(absoluteMax);
193
        }
194
    }
195
}
196

197 198 199 200 201 202 203 204 205
void SliderAction::slotSliderPressed()
{
    m_dragging = true;
}

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

208
#include "slideraction.moc"