/*
 *  Copyright (C) 2005 Bucote Software LLC
 *  tbradley@bucote.com
 *
 *  file: BDesktopClock.cpp
 *
 *  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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <qapplication.h>
#include <qpopupmenu.h>
#include <qdatetime.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qbitmap.h>
#include <qlabel.h>
#include <qpoint.h>
#include <qfont.h>

#include "desktopClockPreferences.h"
#include "BDesktopClock.h"

static const double degreesPerMinute  = 360.0 / 60.0;
static const double degreesPerSecond  = 360.0 / 60.0;
static const double degreesPerHour    = 360.0 / 12.0;
static const double degreesPerMinHour = 360.0 / 720.0;

static QPen B2  (Qt::black,    2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
static QPen B3  (Qt::black,    3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
static QPen R2  (Qt::red,      2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);

static QPoint clockCenter(101, 101);

//=====================================================
//=====================================================
BDesktopClock::BDesktopClock(WFlags f, BSettings  set) : QWidget(0, 0, f), _settings(set)
{ 
	face = new QPixmap(QPixmap::fromMimeSource("face.png"));

	if(!_settings.showWM)
		setMask(QPixmap::fromMimeSource("mask.png").createHeuristicMask());       

	glare = new QPixmap(QPixmap::fromMimeSource("glare.png"));

	setFixedSize(face->size());

	if(_settings.showSeconds)
		startTimer(1000);
	else
		startTimer(60000);

	setBackgroundMode(Qt::NoBackground);

	setFont(QFont(_settings.fontFormat));
	hHand = QPen(QColor(_settings.hourColor),   3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
	mHand = QPen(QColor(_settings.hourColor),   2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
	sHand = QPen(QColor(_settings.secondColor), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
}

//=====================================================
//=====================================================
void BDesktopClock::paintEvent(QPaintEvent *)
{
	QPixmap pm(*face);
	QPainter p;
	QRect textRect;
	int x, y;

	int h = QTime::currentTime().hour();
	int m = QTime::currentTime().minute();
	int s = QTime::currentTime().second();

	p.begin(&pm, this);

	p.translate(101, 101);	
	
	double deg;

	// Draw the hour hand
	if(_settings.showAH)
		deg = degreesPerMinHour * ((h * 60) + m);
	else
		deg = degreesPerHour * h;
	p.rotate(deg);
	p.setPen(hHand);
	p.drawLine(0, 0, 0, -33);
	p.rotate(-deg);

	// Draw the minute hand
	deg = degreesPerMinute * m;
	p.rotate(deg);
	p.setPen(mHand);
	p.drawLine(0, 0, 0, -48);
	p.rotate(-deg);

	// Draw the second hand
	if(_settings.showSeconds) {
		deg = degreesPerSecond * s;
		p.rotate(deg);
		p.setPen(sHand);
		p.drawLine(0, 0, 0, -50);
		p.rotate(-deg);
	}

	p.translate(-101, -101);

	// Draw center circle
	if(_settings.showGlare)
		p.drawPixmap(0, 0, *glare);

	// Draw the text time
	if(_settings.showTime) {
		QString time = QTime::currentTime().toString(_settings.timeFormat);
		textRect     = fontMetrics().boundingRect(time);
		x            = (width() - textRect.width()) / 2;
		y            = height() - 20;

		p.setPen(black);
		p.drawText(x + 2, y + 2, time);
		p.drawText(x + 1, y + 1, time);
		p.setPen(white);
		p.drawText(x, y, time);
	}

	// Draw the text date
	if(_settings.showDate) {
		QString date = QDate::currentDate().toString(_settings.dateFormat);
		textRect     = fontMetrics().boundingRect(date);
		x            = (width() - textRect.width()) / 2;
		y            = height() - 42;

		p.setPen(black);
		p.drawText(x + 2, y + 2, date);
		p.drawText(x + 1, y + 1, date);
		p.setPen(white);
		p.drawText(x, y, date);
	}

	p.end();

	bitBlt(this, 0, 0, &pm);
}

//=====================================================
//=====================================================
void BDesktopClock::timerEvent(QTimerEvent *)
{
	repaint();
}
    
//=====================================================
//=====================================================
void BDesktopClock::mousePressEvent(QMouseEvent * event) 
{
	last = event->pos();
}
        
//=====================================================
//=====================================================
void BDesktopClock::mouseMoveEvent(QMouseEvent * event) 
{
	move(pos() + (event->pos() - last)); 
}
        
//=====================================================
//=====================================================
void BDesktopClock::contextMenuEvent (QContextMenuEvent * event)
{
	QPopupMenu menu(this);

	menu.insertItem("Properties",  this, SLOT(showProperties()));
	menu.insertItem("Exit",        this, SLOT(close()));

	menu.exec(event->globalPos());
}

//=====================================================
//=====================================================
void BDesktopClock::showProperties() 
{
	bool prevWM(_settings.showWM), prevTop(_settings.stayOnTop);
	DesktopClockPreferences pref;
	pref.setSettings(&_settings);

	if(pref.exec()) {
		bool doReparent = false;
		pref.getSettings(&_settings);
		_settings.saveSettings();
		
		Qt::WFlags flags = WNoAutoErase;

		if(prevWM != _settings.showWM || prevTop != _settings.stayOnTop) {
			if(!_settings.showWM)
				flags |= Qt::WStyle_Customize | Qt::WStyle_NoBorder;
			
			if(_settings.stayOnTop)
				flags |= Qt::WStyle_StaysOnTop;

			doReparent = true;
		}

		if(doReparent)
			reparent(parentWidget(), flags, mapToGlobal( QPoint(0, 0)), true);

		if(!_settings.showWM)
			setMask(QPixmap::fromMimeSource("mask.png").createHeuristicMask ());       
		
		killTimers();
		
		if(_settings.showSeconds)
			startTimer(1000);
		else
			startTimer(60000);

		setFont(QFont(_settings.fontFormat));
		hHand = QPen(QColor(_settings.hourColor), 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
		mHand = QPen(QColor(_settings.hourColor), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
		sHand = QPen(QColor(_settings.secondColor), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);

	}  

	repaint();
}

