/****************************************************************************
** Form implementation generated from reading ui file 'form1.ui'
**
** Created by: The User Interface Compiler ($Id: qt/main.cpp 3.3.6 edited Aug 31 2005 $)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "form1.h"
#include <qvariant.h>
#include <qpushbutton.h>
#include <qcombobox.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qdatetime.h>
#include <qcolor.h>
#include <qpointarray.h>
/*
* Constructs a Form1 as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*
* The dialog will by default be modeless, unless you set 'modal' to
* TRUE to construct a modal dialog.
*/
Mytime *mytime=new Mytime();
Mytime::Mytime()
{
hour=0;
minute=0;
second=0;
}
QColor hcolor=QColor(255,0,0);
QColor mcolor=QColor(0,255,0);
QColor scolor=QColor(255,255,255);
QColor hcellcolor=QColor(255,0,0);
QColor mcellcolor=QColor(0,0,255);
/*void Form1::timerEvent(QTimerEvent*)
{
update();//重绘
}
*/
void Form1::paintEvent(QPaintEvent*)
{
QCOORD hourHand[8]={2,0,0,2,-2,0,0,-20};
QCOORD minuteHand[8]={1,0,0,1,-1,0,0,-30};
if(mytime->second>59)
mytime->second=mytime->second%60;
if(mytime->second==0)
{
mytime->minute++;
}
if(mytime->minute>59)
mytime->minute=mytime->minute%60;
if(mytime->minute==0)
{
mytime->hour++;
}
if(mytime->hour>11)
mytime->hour=mytime->hour%12;
mytime->second++;
QPainter painter(this);
painter.setWindow(-50,-50,100,100);
painter.save();
painter.rotate(30*(mytime->hour%12)+mytime->minute/2);
painter.setBrush(hcolor);
QPointArray a;
a.putPoints(0,4,hourHand);
painter.drawConvexPolygon(a);
painter.restore();
painter.save();
painter.rotate(mytime->minute*6+0.1*mytime->second);
painter.setBrush(mcolor);
QPointArray b;
b.putPoints(0,4,minuteHand);
painter.drawConvexPolygon(b);
painter.restore();
painter.save();
painter.rotate(6*(mytime->second-15));
painter.setPen(scolor);
painter.drawLine(0,0,30,0);
painter.restore();
painter.save();
painter.setPen(hcellcolor);
for(int i=0;i<12;i++)
{
painter.rotate(30);
painter.drawLine(32,0,36,0);
}
painter.restore();
painter.save();
painter.setPen(mcellcolor);
for(int j=0;j<60;j++)
{
if((j%5)!=0)
painter.drawLine(32,0,34,0);
painter.rotate(6.0);
}
painter.restore();
}
Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )
: QDialog( parent, name, modal, fl )
{
if ( !name )
setName( "Form1" );
h_Button = new QPushButton( this, "h_Button" );
h_Button->setGeometry( QRect( 210, 10, 41, 31 ) );
m_Button = new QPushButton( this, "m_Button" );
m_Button->setGeometry( QRect( 290, 10, 41, 31 ) );
s_Button = new QPushButton( this, "s_Button" );
s_Button->setGeometry( QRect( 370, 10, 41, 31 ) );
setArea = new QComboBox( FALSE, this, "setArea" );
setArea->setGeometry( QRect( 10, 10, 111, 30 ) );
Q_CHECK_PTR(setArea); //检查setArea指针是否为空
setArea->insertItem(" Beijing ",0);
setArea->insertItem(" Pairs",1);//-7
setArea->insertItem(" Athens",2);//-6
setArea->insertItem(" Moscow",3);//-5
setArea->insertItem(" Tokyo",4);//+1
setArea->insertItem(" Sydny",5);//+1.5
setArea->insertItem(" Hawaii",6);//+6
setArea->insertItem(" Los Angeles",7);//+8
QTime time=QTime::currentTime();
// QDate date=QDate::currentDate();
// startTimer(1000);
//resize(200,200);
// setMinimumSize(475,320);
QTimer*timer=new QTimer(this);
timer->start(1000);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
// setUpdatesEnabled(true);
mytime->setHour(time.hour());
mytime->setMinute(time.minute());
mytime->setSecond(time.second());
languageChange();
resize( QSize(450, 450).expandedTo(minimumSizeHint()) );
clearWState( WState_Polished );
//信号连接
connect( setArea, SIGNAL( activated ( int )),this,SLOT( changeArea(int)));
connect( h_Button, SIGNAL( clicked()),this,SLOT( changeHourColor()));
connect( m_Button, SIGNAL( clicked()),this,SLOT( changeMinuteColor()));
connect( s_Button, SIGNAL( clicked()),this,SLOT( changeSecondColor()));
}
/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Form1::languageChange()
{
setCaption( tr( "Form1" ) );
h_Button->setText( tr( "H" ) );
m_Button->setText( tr( "M" ) );
s_Button->setText( tr( "S" ) );
}
void Form1::changeArea(int areachoice)
{
QTime curtime = QTime::currentTime();
switch(areachoice)
{
case 0:
mytime->setHour(curtime.hour());
mytime->setSecond(curtime.second());
mytime->setMinute(curtime.minute());
break;
case 1:
mytime->setHour(curtime.hour()-7);
mytime->setMinute(curtime.minute());
mytime->setSecond(curtime.second());
break;
case 2:
mytime->setHour(curtime.hour()-6);
mytime->setMinute(curtime.minute());
mytime->setSecond(curtime.second());
break;
case 3:
mytime->setHour(curtime.hour()-5);
mytime->setMinute(curtime.minute());
mytime->setSecond(curtime.second());
break;
case 4:
mytime->setHour(curtime.hour()+1);
mytime->setMinute(curtime.minute());
mytime->setSecond(curtime.second());
break;
case 5:
mytime->setHour(curtime.hour()+1);
mytime->setMinute(curtime.minute()+30);
mytime->setSecond(curtime.second());
break;
case 6:
mytime->setHour(curtime.hour()+6);
mytime->setMinute(curtime.minute());
mytime->setSecond(curtime.second());
break;
case 7:
mytime->setHour(curtime.hour()+8);
mytime->setMinute(curtime.minute());
mytime->setSecond(curtime.second());
break;
}
}
//颜色变化处理
void Form1::changeHourColor()
{
hcolor = QColorDialog::getColor();
}
void Form1::changeMinuteColor()
{
mcolor = QColorDialog::getColor();
}
void Form1::changeSecondColor()
{
scolor = QColorDialog::getColor();
}