/*
    KSysGuard, the KDE System Guard
   
	Copyright (c) 1999, 2000, 2001 Chris Schlaeger <cs@kde.org>
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    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.

	KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
	Please do not commit any changes without consulting me first. Thanks!

*/

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

#include <tqdom.h>
#include <tqlcdnumber.h>
#include <tqtooltip.h>

#include <kdebug.h>

#include <ksgrd/SensorManager.h>
#include <ksgrd/StyleEngine.h>

#include "MultiMeter.moc"
#include "MultiMeterSettings.h"

MultiMeter::MultiMeter(TQWidget* parent, const char* name,
				   const TQString& title, double, double, bool nf, bool isApplet)
	: KSGRD::SensorDisplay(parent, name, title, nf, isApplet)
{
	setShowUnit( true );
	lowerLimit = upperLimit = 0;
	lowerLimitActive = upperLimitActive = false;

	normalDigitColor = KSGRD::Style->firstForegroundColor();
	alarmDigitColor = KSGRD::Style->alarmColor();
	if (noFrame())
		lcd = new TQLCDNumber(this, "meterLCD");
	else
		lcd = new TQLCDNumber(frame(), "meterLCD");
	TQ_CHECK_PTR(lcd);
	lcd->setSegmentStyle(TQLCDNumber::Filled);
	setDigitColor(KSGRD::Style->backgroundColor());
	lcd->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding,
					   TQSizePolicy::Expanding, false));

	setBackgroundColor(KSGRD::Style->backgroundColor());
	/* All RMB clicks to the lcd widget will be handled by 
	 * SensorDisplay::eventFilter. */
	lcd->installEventFilter(this);

	setPlotterWidget(lcd);

	setMinimumSize(5, 5);
	setModified(false);
}

bool
MultiMeter::addSensor(const TQString& hostName, const TQString& sensorName,
					const TQString& sensorType, const TQString& title)
{
	if (sensorType != "integer" && sensorType != "float")
		return (false);

	registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));

	/* To differentiate between answers from value requests and info
	 * requests we use 100 for info requests. */
	sendRequest(hostName, sensorName + "?", 100);

	TQToolTip::remove(lcd);
	TQToolTip::add(lcd, TQString("%1:%2").arg(hostName).arg(sensorName));

	setModified(true);
	return (true);
}

void
MultiMeter::answerReceived(int id, const TQString& answer)
{
	/* We received something, so the sensor is probably ok. */
	sensorError(id, false);

	if (id == 100)
	{
		KSGRD::SensorIntegerInfo info(answer);
		setUnit(KSGRD::SensorMgr->translateUnit(info.unit()));
	}
	else
	{
		double val = answer.toDouble();
		int digits = (int) log10(val) + 1;

		if (noFrame())
			if (digits > 4)
				lcd->setNumDigits(4);
			else
				lcd->setNumDigits(digits);	    
		else
		{
			if (digits > 5)
				lcd->setNumDigits(digits);
			else
				lcd->setNumDigits(5);
		}

		lcd->display(val);
		if (lowerLimitActive && val < lowerLimit)
		{
			setDigitColor(alarmDigitColor);	
		}
		else if (upperLimitActive && val > upperLimit)
		{
			setDigitColor(alarmDigitColor);
		}
		else
			setDigitColor(normalDigitColor);
	}
}

void
MultiMeter::resizeEvent(TQResizeEvent*)
{
	if (noFrame())
		lcd->setGeometry(0, 0, width(), height());
	else
		frame()->setGeometry(0, 0, width(), height());
}

bool
MultiMeter::restoreSettings(TQDomElement& element)
{
	lowerLimitActive = element.attribute("lowerLimitActive").toInt();
	lowerLimit = element.attribute("lowerLimit").toLong();
	upperLimitActive = element.attribute("upperLimitActive").toInt();
	upperLimit = element.attribute("upperLimit").toLong();

	normalDigitColor = restoreColor(element, "normalDigitColor",
						KSGRD::Style->firstForegroundColor());
	alarmDigitColor = restoreColor(element, "alarmDigitColor",
						KSGRD::Style->alarmColor());
	setBackgroundColor(restoreColor(element, "backgroundColor",
						KSGRD::Style->backgroundColor()));

	addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "integer" : element.attribute("sensorType")), "");

	SensorDisplay::restoreSettings(element);

	setModified(false);

	return (true);
}

bool
MultiMeter::saveSettings(TQDomDocument& doc, TQDomElement& element, bool save)
{
	element.setAttribute("hostName", sensors().at(0)->hostName());
	element.setAttribute("sensorName", sensors().at(0)->name());
	element.setAttribute("sensorType", sensors().at(0)->type());
	element.setAttribute("showUnit", showUnit());
	element.setAttribute("lowerLimitActive", (int) lowerLimitActive);
	element.setAttribute("lowerLimit", (int) lowerLimit);
	element.setAttribute("upperLimitActive", (int) upperLimitActive);
	element.setAttribute("upperLimit", (int) upperLimit);

	saveColor(element, "normalDigitColor", normalDigitColor);
	saveColor(element, "alarmDigitColor", alarmDigitColor);
	saveColor(element, "backgroundColor", lcd->backgroundColor());

	SensorDisplay::saveSettings(doc, element);

	if (save)
		setModified(false);

	return (true);
}

void
MultiMeter::configureSettings()
{
	mms = new MultiMeterSettings(this, "MultiMeterSettings");
	TQ_CHECK_PTR(mms);
	mms->setTitle(title());
	mms->setShowUnit(showUnit());
	mms->setLowerLimitActive(lowerLimitActive);
	mms->setLowerLimit(lowerLimit);
	mms->setUpperLimitActive(upperLimitActive);
	mms->setUpperLimit(upperLimit);
	mms->setNormalDigitColor(normalDigitColor);
	mms->setAlarmDigitColor(alarmDigitColor);
	mms->setMeterBackgroundColor(lcd->backgroundColor());

	connect(mms, TQ_SIGNAL(applyClicked()), TQ_SLOT(applySettings()));

	if (mms->exec())
		applySettings();

	delete mms;
	mms = 0;
}

void
MultiMeter::applySettings()
{
	setShowUnit( mms->showUnit() );
	setTitle(mms->title());
	lowerLimitActive = mms->lowerLimitActive();
	lowerLimit = mms->lowerLimit();
	upperLimitActive = mms->upperLimitActive();
	upperLimit = mms->upperLimit();

	normalDigitColor = mms->normalDigitColor();
	alarmDigitColor = mms->alarmDigitColor();
	setBackgroundColor(mms->meterBackgroundColor());

	repaint();
	setModified(true);
}

void
MultiMeter::applyStyle()
{
	normalDigitColor = KSGRD::Style->firstForegroundColor();
	setBackgroundColor(KSGRD::Style->backgroundColor());
	repaint();
	setModified(true);
}

void
MultiMeter::setDigitColor(const TQColor& col)
{
	TQPalette p = lcd->palette();
	p.setColor(TQColorGroup::Foreground, col);
	lcd->setPalette(p);
}

void
MultiMeter::setBackgroundColor(const TQColor& col)
{
	lcd->setBackgroundColor(col);

	TQPalette p = lcd->palette();
	p.setColor(TQColorGroup::Light, col);
	p.setColor(TQColorGroup::Dark, col);
	lcd->setPalette(p);
}
