summaryrefslogtreecommitdiff
path: root/core/settings/light-and-power/light.cpp
blob: 2ea035677c035c208540eff832f540c6e485df1f (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
                             This file is part of the OPIE Project
               =.            Copyright (c)  2002 Maximilian Reiss <harlekin@handhelds.org>
             .=l.            Copyright (c)  2002 Robert Griebl <sandman@handhelds.org>
           .>+-=
 _;:,     .>    :=|.         This file is free software; you can
.> <`_,   >  .   <=          redistribute it and/or modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This file 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 file;
  -_. . .   )=.  =           see the file COPYING. If not, write to the
    --        :-=`           Free Software Foundation, Inc.,
                             59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

#include "settings.h"

#include <qpe/global.h>
#include <qpe/fontmanager.h>
#include <qpe/config.h>
#include <qpe/applnk.h>
#include <qpe/qpeapplication.h>
#include <qpe/power.h>
#if defined(Q_WS_QWS) && !defined(QT_NO_COP)
#include <qpe/qcopenvelope_qws.h>
#endif

#include <qlabel.h>
#include <qcheckbox.h>
#include <qradiobutton.h>
#include <qtabwidget.h>
#include <qslider.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qdatastream.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qgroupbox.h>
#include <qspinbox.h>
#include <qpushbutton.h>
#include <qlistbox.h>
#include <qdir.h>
#if QT_VERSION >= 300
#include <qstylefactory.h>
#endif

#include <opie/odevice.h>

#include "sensor.h"

using namespace Opie;

LightSettings::LightSettings( QWidget* parent,  const char* name, WFlags )
	: LightSettingsBase( parent, name, true, WStyle_ContextHelp )
{
	m_res = ODevice::inst ( )-> displayBrightnessResolution ( );

	if ( !ODevice::inst ( )-> hasLightSensor ( )) {
		auto_brightness-> hide ( );
		CalibrateLightSensor-> hide ( );
		auto_brightness_ac_3-> hide ( );
		CalibrateLightSensorAC-> hide ( );
	}

	Config config ( "apm" );
	config. setGroup ( "Battery" );

	// battery spinboxes
	interval_dim->      setValue ( config. readNumEntry ( "Dim", 20 ));
	interval_lightoff-> setValue ( config. readNumEntry ( "LightOff", 30 ));
	interval_suspend->  setValue ( config. readNumEntry ( "Suspend", 60 ));

	// battery check and slider
	LcdOffOnly-> setChecked ( config. readBoolEntry ( "LcdOffOnly", false ));

	int bright = config. readNumEntry ( "Brightness", 255 );
	brightness-> setMaxValue ( m_res - 1 );
	brightness-> setTickInterval ( QMAX( 1, m_res / 16 ));
	brightness-> setLineStep ( QMAX( 1, m_res / 16 ));
	brightness-> setPageStep ( QMAX( 1, m_res / 16 ));
	brightness-> setValue (( bright * ( m_res - 1 ) + 127 ) / 255 );

	// light sensor
	auto_brightness-> setChecked ( config. readBoolEntry ( "LightSensor", false ));
	m_sensordata = config. readListEntry ( "LightSensorData", ';' );

	config. setGroup ( "AC" );
	
	// ac spinboxes
	interval_dim_ac_3->      setValue ( config. readNumEntry ( "Dim", 20 ));
	interval_lightoff_ac_3-> setValue ( config. readNumEntry ( "LightOff", 30 ));
	interval_suspend_ac_3->  setValue ( config. readNumEntry ( "Suspend", 60 ));

	// ac check and slider
	LcdOffOnly_2_3-> setChecked ( config. readBoolEntry ( "LcdOffOnly", false ));

	bright = config. readNumEntry ( "Brightness", 255 );
	brightness_ac_3-> setMaxValue ( m_res - 1 );
	brightness_ac_3-> setTickInterval ( QMAX( 1, m_res / 16 ));
	brightness_ac_3-> setLineStep ( QMAX( 1, m_res / 16 ));
	brightness_ac_3-> setPageStep ( QMAX( 1, m_res / 16 ));
	brightness_ac_3-> setValue (( bright * ( m_res - 1 ) + 127 ) / 255 );

	// light sensor
	auto_brightness_ac_3-> setChecked ( config. readBoolEntry ( "LightSensor", false ));
	m_sensordata_ac = config. readListEntry ( "LightSensorData", ';' );
	
	// advanced settings
	config. setGroup ( "Warnings" );
	warnintervalBox-> setValue ( config. readNumEntry ( "checkinterval", 10000 ) / 1000 );
	lowSpinBox->      setValue ( config. readNumEntry ( "powerverylow", 10 ) );
	criticalSpinBox-> setValue ( config. readNumEntry ( "powercritical", 5 ) );

	if ( PowerStatusManager::readStatus ( ). acStatus ( ) != PowerStatus::Online ) 
		connect ( brightness, SIGNAL( valueChanged ( int )), this, SLOT( setBacklight ( int )));
	else
		connect ( brightness_ac_3, SIGNAL( valueChanged ( int )), this, SLOT( setBacklight ( int )));
}

LightSettings::~LightSettings ( ) 
{
}


void LightSettings::calibrateSensor ( )
{
	Sensor *s = new Sensor ( m_sensordata, this );
	s-> showMaximized ( );
	s-> exec ( );
	delete s;
}

void LightSettings::calibrateSensorAC ( )
{
	Sensor *s = new Sensor ( m_sensordata_ac, this );
	s-> showMaximized ( );
	s-> exec ( );
	delete s;
}

void LightSettings::setBacklight ( int bright )
{
	bright = bright * 255 / ( m_res - 1 );
	QCopEnvelope e ( "QPE/System", "setBacklight(int)" );
	e << bright;
}

void LightSettings::reject ( )
{
	{
		QCopEnvelope e ( "QPE/System", "setBacklight(int)" );
		e << -1;
	}
	QDialog::reject ( );
}

void LightSettings::accept ( )
{
	Config config ( "apm" );

	// bat
	config. setGroup ( "Battery" );
	config. writeEntry ( "LcdOffOnly", LcdOffOnly-> isChecked ( ));
	config. writeEntry ( "Dim",        interval_dim-> value ( ));
	config. writeEntry ( "LightOff",   interval_lightoff-> value ( ));
	config. writeEntry ( "Suspend",    interval_suspend-> value ( ));
	config. writeEntry ( "Brightness", brightness-> value ( ) * 255 / ( m_res - 1 ) );

	// ac
	config. setGroup ( "AC" );
	config. writeEntry ( "LcdOffOnly", LcdOffOnly_2_3-> isChecked ( ));
	config. writeEntry ( "Dim",        interval_dim_ac_3-> value ( ));
	config. writeEntry ( "LightOff",   interval_lightoff_ac_3-> value ( ));
	config. writeEntry ( "Suspend",    interval_suspend_ac_3-> value ( ));
	config. writeEntry ( "Brightness", brightness_ac_3-> value ( ) * 255 / ( m_res - 1 ));

	// only make light sensor stuff appear if the unit has a sensor	
	if ( ODevice::inst ( )-> hasLightSensor ( )) {
		config. setGroup ( "Battery" );
		config. writeEntry ( "LightSensor", auto_brightness->isChecked() );
		config. writeEntry ( "LightSensorData", m_sensordata, ';' );
		config. setGroup ( "AC" );
		config. writeEntry ( "LightSensor", auto_brightness_ac_3->isChecked() );
		config. writeEntry ( "LightSensorData", m_sensordata_ac, ';' );
	}

	// advanced
	config. setGroup ( "Warnings" );
	config. writeEntry ( "check_interval", warnintervalBox-> value ( ) * 1000 );
	config. writeEntry ( "power_verylow",  lowSpinBox-> value ( ));
	config. writeEntry ( "power_critical", criticalSpinBox-> value ( ));
	config. write ( );

	// notify the launcher
	{
		QCopEnvelope e ( "QPE/System", "reloadPowerWarnSettings()" );
	}
	{
		QCopEnvelope e ( "QPE/System", "setScreenSaverInterval(int)" );
		e << -1;
	}
	{
		QCopEnvelope e ( "QPE/System", "setBacklight(int)" );
		e << -1;
	}
	
	QDialog::accept ( );
}

void LightSettings::done ( int r )
{
	QDialog::done ( r );
	close ( );
}