slideraction.cpp 6.4 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 20 21 22
#include <klocale.h>
#include <kdebug.h>

#include <qtooltip.h>
23 24
#include <qlayout.h>
#include <qslider.h>
25 26 27

#include "slideraction.h"

28 29 30 31
////////////////////////////////////////////////////////////////////////////////
// convenience class
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

protected:
    void wheelEvent(QWheelEvent *e) {
	QWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());
	
	QSlider::wheelEvent(&transposed);
//	QSlider::wheelEvent(new QWheelEvent(e->pos(), -(e->delta()), e->state(), e->orientation()));
69 70 71
    }
};

72 73 74 75 76
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
77
    : CustomAction(text, parent, name)
78
{
79

80 81 82 83
}

SliderAction::~SliderAction()
{
84

85 86
}

87
QSlider *SliderAction::getTrackPositionSlider() const
88
{
89
    return m_trackPositionSlider;
90 91
}

92
QSlider *SliderAction::getVolumeSlider() const
93
{
94
    return m_volumeSlider;
95 96 97
}

////////////////////////////////////////////////////////////////////////////////
98 99 100
// public slots
////////////////////////////////////////////////////////////////////////////////

101
void SliderAction::slotUpdateOrientation(QDockWindow *dockWindow)
102
{
103 104 105
    // 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) {
106 107 108
            m_trackPositionSlider->setOrientation(Qt::Vertical);
            m_volumeSlider->setOrientation(Qt::Vertical);
            m_layout->setDirection(QBoxLayout::TopToBottom);
109 110
        }
        else {
111 112 113
            m_trackPositionSlider->setOrientation(Qt::Horizontal);
            m_volumeSlider->setOrientation(Qt::Horizontal);
            m_layout->setDirection(QBoxLayout::LeftToRight);
114
        }
115
    }
116
    slotUpdateSize();
117 118 119 120
}

////////////////////////////////////////////////////////////////////////////////
// private members
121 122 123 124
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
125 126
    if(parent) {
        QWidget *base = new QWidget(parent);
127
	base->setBackgroundMode( parent->backgroundMode() );
128 129
	base->setName("kde toolbar widget");
//	base->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
130

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

133 134
        m_trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
        m_trackPositionSlider->setMaxValue(1000);
Stephan Binner's avatar
Stephan Binner committed
135
        QToolTip::add(m_trackPositionSlider, i18n("Track position"));
136
        m_layout->addWidget(m_trackPositionSlider);
137

138
	m_volumeSlider = new VolumeSlider(base, "volumeSlider");
139 140 141
        m_volumeSlider->setMaxValue(100);
        QToolTip::add(m_volumeSlider, i18n("Volume"));
        m_layout->addWidget(m_volumeSlider);
142

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

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

149
//        setWidget(base);
150

151
        connect(parent, SIGNAL(modechange()), this, SLOT(slotUpdateSize()));
152
        return base;
153
    }
154
    else
155
        return 0;
156 157 158 159 160 161
}

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

162
void SliderAction::slotUpdateSize()
163
{
164 165 166 167 168
    static const int offset = 3;
    static const int absoluteMax = 10000;

    if(customWidget && toolbar) {
        if(toolbar->barPos() == KToolBar::Right || toolbar->barPos() == KToolBar::Left) {
169 170
            m_volumeSlider->setMaximumWidth(toolbar->iconSize() - offset);
            m_volumeSlider->setMaximumHeight(volumeMax);
171

172 173
            m_trackPositionSlider->setMaximumWidth(toolbar->iconSize() - offset);
            m_trackPositionSlider->setMaximumHeight(absoluteMax);
174 175
        }
        else {
176 177
            m_volumeSlider->setMaximumHeight(toolbar->iconSize() - offset);
            m_volumeSlider->setMaximumWidth(volumeMax);
178

179 180
            m_trackPositionSlider->setMaximumHeight(toolbar->iconSize() - offset);
            m_trackPositionSlider->setMaximumWidth(absoluteMax);
181
        }
182 183 184
    }

}
185

186
#include "slideraction.moc"