slideraction.cpp 7.07 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 36 37 38 39 40
/**
 * 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.
 */

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

46
protected:
47 48 49 50 51
    virtual void mousePressEvent(QMouseEvent *e) {
        if(e->button() == LeftButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), MidButton, e->state());
            QSlider::mousePressEvent(&reverse); 
            emit sliderPressed();
52
	}
53 54 55 56
        else if(e->button() == MidButton) {
            QMouseEvent reverse(QEvent::MouseButtonPress, e->pos(), LeftButton, e->state());
            QSlider::mousePressEvent(&reverse); 
        }
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 70
    virtual void wheelEvent(QWheelEvent *e) {
        QWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());

        QSlider::wheelEvent(&transposed);
71
    }
72
    virtual void focusInEvent(QFocusEvent *) { clearFocus(); }
73 74
};

75 76 77 78 79
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
80
    : CustomAction(text, parent, name)
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 133 134 135
        QLabel *trackPositionLabel = new QLabel(base);
        trackPositionLabel->setName("kde toolbar widget");
        trackPositionLabel->setPixmap(SmallIcon("juk_time"));
        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 143 144 145 146 147 148 149 150
        m_layout->addItem(new QSpacerItem(10, 1));

        QLabel *volumeLabel = new QLabel(base);
        volumeLabel->setName("kde toolbar widget");
        volumeLabel->setPixmap(SmallIcon("juk_volume"));
        QToolTip::add(volumeLabel, i18n("Volume"));
	m_layout->addWidget(volumeLabel);

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

155
        m_volumeSlider->setName("kde toolbar widget");
156
        m_trackPositionSlider->setName("kde toolbar widget");
157

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

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

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

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

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

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

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

}
195

196
#include "slideraction.moc"