slideraction.cpp 6.19 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 23 24
#include <klocale.h>
#include <kdebug.h>

#include <qslider.h>
#include <qlabel.h>
#include <qtooltip.h>
25 26
#include <qlayout.h>
#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 45 46 47 48 49 50 51 52 53 54
class TrackPositionSlider : public QSlider
{
public:
    TrackPositionSlider(QWidget *parent, const char *name) : QSlider(parent, name) {}
    
protected:
    void mousePressEvent(QMouseEvent *e) {
	if(e->button() == LeftButton)
	    QSlider::mousePressEvent(new QMouseEvent(QEvent::MouseButtonPress, e->pos(), MidButton, e->state()));
	else if(e->button() == MidButton)
	    QSlider::mousePressEvent(new QMouseEvent(QEvent::MouseButtonPress, e->pos(), LeftButton, e->state()));
    }
};

55 56 57 58 59
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
60
    : CustomAction(text, parent, name)
61
{
62

63 64 65 66
}

SliderAction::~SliderAction()
{
67

68 69
}

70
QSlider *SliderAction::getTrackPositionSlider() const
71
{
72 73 74 75
    if(trackPositionSlider)
        return (trackPositionSlider);
    else
        return(0);
76 77
}

78
QSlider *SliderAction::getVolumeSlider() const
79
{
80 81 82 83
    if(volumeSlider)
        return (volumeSlider);
    else
        return(0);
84 85 86
}

////////////////////////////////////////////////////////////////////////////////
87 88 89 90 91
// public slots
////////////////////////////////////////////////////////////////////////////////

void SliderAction::updateOrientation(QDockWindow *dockWindow)
{
92 93 94 95 96 97 98 99 100 101 102 103
    // 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) {
            trackPositionSlider->setOrientation(Qt::Vertical);
            volumeSlider->setOrientation(Qt::Vertical);
            layout->setDirection(QBoxLayout::TopToBottom);
        }
        else {
            trackPositionSlider->setOrientation(Qt::Horizontal);
            volumeSlider->setOrientation(Qt::Horizontal);
            layout->setDirection(QBoxLayout::LeftToRight);
        }
104
    }
105
    updateSize();
106 107 108 109
}

////////////////////////////////////////////////////////////////////////////////
// private members
110 111 112 113
////////////////////////////////////////////////////////////////////////////////

QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base class
{
114 115
    if(parent) {
        QWidget *base = new QWidget(parent);
116 117
	base->setName("kde toolbar widget");
//	base->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
118

119
	layout = new QBoxLayout(base, QBoxLayout::TopToBottom, 5, 5);
120

121
        trackPositionSlider = new TrackPositionSlider(base, "trackPositionSlider");
122 123 124 125
        trackPositionSlider->setMaxValue(1000);
        QToolTip::add(trackPositionSlider, i18n("Track Position"));
        layout->addWidget(trackPositionSlider);

126
	volumeSlider = new QSlider(base, "volumeSlider" );
127 128 129 130
        volumeSlider->setMaxValue(100);
        QToolTip::add(volumeSlider, i18n("Volume"));
        layout->addWidget(volumeSlider);

131 132 133
	volumeSlider->setName("kde toolbar widget");
        trackPositionSlider->setName("kde toolbar widget");

134 135 136
        layout->setStretchFactor(trackPositionSlider, 4);
        layout->setStretchFactor(volumeSlider, 1);

137
//        setWidget(base);
138 139 140 141 142

        connect(parent, SIGNAL(modechange()), this, SLOT(updateLabels()));
        connect(parent, SIGNAL(modechange()), this, SLOT(updateSize()));
        return(base);
    }
143
    else
144
        return(0);
145 146 147 148 149 150 151 152 153 154 155 156 157
}

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

void SliderAction::updateLabels()
{

}

void SliderAction::updateSize()
{
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    static const int offset = 3;
    static const int absoluteMax = 10000;

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

            trackPositionSlider->setMaximumWidth(toolbar->iconSize() - offset);
            trackPositionSlider->setMaximumHeight(absoluteMax);
        }
        else {
            volumeSlider->setMaximumHeight(toolbar->iconSize() - offset);
            volumeSlider->setMaximumWidth(volumeMax);

            trackPositionSlider->setMaximumHeight(toolbar->iconSize() - offset);
            trackPositionSlider->setMaximumWidth(absoluteMax);
        }
        //    kdDebug() << "SliderAction::updateLabels()" << endl;
        //    kdDebug() << toolbar->iconSize() << endl;
178 179 180
    }

}
181
#include "slideraction.moc"