slider.cpp 2.42 KB
Newer Older
1 2 3
/*
 *  slider.cpp  -  slider control with read-only option
 *  Program:  kalarm
4
 *  Copyright © 2004-2006 by David Jarvie <djarvie@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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
David Jarvie's avatar
David Jarvie committed
16 17 18
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 20
 */

Laurent Montel's avatar
Laurent Montel committed
21
#include "slider.h"
22
#include <QMouseEvent>
23 24


David Jarvie's avatar
David Jarvie committed
25
Slider::Slider(QWidget* parent)
26 27
    : QSlider(parent),
      mReadOnly(false)
28 29
{ }

David Jarvie's avatar
David Jarvie committed
30
Slider::Slider(Qt::Orientation o, QWidget* parent)
31 32
    : QSlider(o, parent),
      mReadOnly(false)
33 34
{ }

35
Slider::Slider(int minval, int maxval, int pageStep, Qt::Orientation o, QWidget* parent)
36 37
    : QSlider(o, parent),
      mReadOnly(false)
38
{ 
39 40
    setRange(minval, maxval);
    setPageStep(pageStep);
41
}
42 43 44 45 46 47 48

/******************************************************************************
*  Set the read-only status. If read-only, the slider can be moved by the
*  application, but not by the user.
*/
void Slider::setReadOnly(bool ro)
{
49
    mReadOnly = ro;
50 51 52 53 54 55 56 57
}

/******************************************************************************
*  Event handlers to intercept events if in read-only mode.
*  Any events which could change the slider value are discarded.
*/
void Slider::mousePressEvent(QMouseEvent* e)
{
58 59 60 61 62 63 64
    if (mReadOnly)
    {
        // Swallow up the event if it's the left button
        if (e->button() == Qt::LeftButton)
            return;
    }
    QSlider::mousePressEvent(e);
65 66 67 68
}

void Slider::mouseReleaseEvent(QMouseEvent* e)
{
69 70
    if (!mReadOnly)
        QSlider::mouseReleaseEvent(e);
71 72 73 74
}

void Slider::mouseMoveEvent(QMouseEvent* e)
{
75 76
    if (!mReadOnly)
        QSlider::mouseMoveEvent(e);
77 78 79 80
}

void Slider::keyPressEvent(QKeyEvent* e)
{
81 82
    if (!mReadOnly  ||  e->key() == Qt::Key_Escape)
        QSlider::keyPressEvent(e);
83 84 85 86
}

void Slider::keyReleaseEvent(QKeyEvent* e)
{
87 88
    if (!mReadOnly)
        QSlider::keyReleaseEvent(e);
89
}
90

91
// vim: et sw=4: