summaryrefslogtreecommitdiffstats
path: root/kmplot/kmplot/ksliderwindow.cpp
blob: 004a08db34f82ac1a51fb1c3ea00b7d152369cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 2005  Fredrik Edemar
*                     f_edemar@linux.se
*
* This file is part of the KDE Project.
* KmPlot is part of the KDE-EDU Project.
*
* 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.
*
* 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.
*
*/
// TQt includes
#include <tqcursor.h>
#include <tqslider.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>

// KDE includes
#include <kaction.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kinputdialog.h>
#include <klocale.h>

#include <limits.h>
#include <math.h>
#include <stdlib.h>

// local includes
#include "ksliderwindow.h"

KSliderWindow::KSliderWindow(TQWidget* parent, int num ) :
	SliderWindow( parent, "", false, TQt::WStyle_Tool-TQt::WStyle_Maximize ), m_num(num)
{
	setCaption(i18n( "Slider %1" ).arg( num+1 ) );
	TQToolTip::add( slider, i18n( "Slider no. %1" ).arg( num+1 ));
	TQWhatsThis::add( this, i18n( "Move slider to change the parameter of the function plot connected to this slider." ) );
	
	// load the min and max value + the current value
	TDEConfig config( "kmplotrc" );
	config.setGroup( "slider" + TQString::number(num) );
	slider->setMinValue( config.readNumEntry( "min", 0) );
	slider->setMaxValue( config.readNumEntry( "max", 100) );
	slider->setValue( config.readNumEntry( "value", 50) );
	slider->setPageStep( (int)ceil((abs(slider->minValue()) + abs(slider->maxValue()))/10.) );
	
	slider->installEventFilter(this);
	installEventFilter(this);
	
	m_popupmenu = new KPopupMenu(this);
	KAction *mnuMinValue = new KAction(i18n("&Change Minimum Value") ,0,TQT_TQOBJECT(this), TQT_SLOT( mnuMinValue_clicked() ),0);
	mnuMinValue->plug(m_popupmenu);
	KAction *mnuMaxValue = new KAction(i18n("&Change Maximum Value") ,0,TQT_TQOBJECT(this), TQT_SLOT( mnuMaxValue_clicked() ),0 );
	mnuMaxValue->plug(m_popupmenu);
}

KSliderWindow::~KSliderWindow()
{
	// save the min and max value + the current value
	TDEConfig config( "kmplotrc" );
	config.setGroup( "slider" + TQString::number(m_num) );
	config.writeEntry( "min", slider->minValue() );
	config.writeEntry( "max", slider->maxValue() );
	config.writeEntry( "value", slider->value() );
}

bool KSliderWindow::eventFilter( TQObject *obj, TQEvent *ev )
{
	if (ev->type() == TQEvent::MouseButtonPress)
	{
		TQMouseEvent *e = (TQMouseEvent *)ev;
 		if (e->button() != Qt::RightButton)
			return SliderWindow::eventFilter( obj, ev );
		m_popupmenu->exec(TQCursor::pos());
		return true;
	}
	return SliderWindow::eventFilter( obj, ev );
}

void KSliderWindow::closeEvent( TQCloseEvent * e)
{
	emit windowClosed(m_num);
	e->accept();
}

void KSliderWindow::mnuMinValue_clicked()
{
	bool ok;
	int const result = KInputDialog::getInteger(i18n("Change Minimum Value"), i18n("Type a new minimum value for the slider:"), slider->minValue(), INT_MIN, INT_MAX, 1, 10, &ok);
	if (!ok)
		return;
	slider->setMinValue(result);
	slider->setPageStep( (int)ceil((abs(slider->maxValue()) + abs(result))/10.) );
	setFocus();
}

void KSliderWindow::mnuMaxValue_clicked()
{
	bool ok;
	int const result = KInputDialog::getInteger(i18n("Change Maximum Value"), i18n("Type a new maximum value for the slider:"), slider->maxValue(), INT_MIN, INT_MAX, 1, 10, &ok);
	if (!ok)
		return;
	slider->setMaxValue(result);
	slider->setPageStep( (int)ceil((abs(slider->minValue()) + abs(result))/10.) );
	setFocus();
}

#include "ksliderwindow.moc"