timer.cpp
资源名称:timer_scr.rar [点击查看]
上传用户:karlzgj
上传日期:2020-03-01
资源大小:68k
文件大小:12k
源码类别:
TAPI编程
开发平台:
QT
- /* 时间助手(timer) 2010-01-28 by feng.Z.Z Email : feng7553@163.com thanks yan0 */
- #include "timer.h"
- //#include "iostream.h"
- #include <stdlib.h>
- #include <unistd.h> #include <ZSeparator.h> #include <sys/types.h> #include <sys/time.h> #include <sys/resource.h>
- //using namespace std;
- QTextCodec* gbk_codec = QTextCodec::codecForName("UTF-8"); QString ctr(const char*ChineseString) { return gbk_codec->toUnicode(ChineseString); }
- Timer::Timer(QWidget* parent, char* name)
- :ZKbMainWidget((ZHeader::HEADER_TYPE)3 , parent , name , WType_TopLevel)
- { channelStart = new QCopChannel("myapplication/timer/",this,"timer_feng");//注册
- CHECK_PTR(channelStart);
- connect( channelStart, SIGNAL(received(const QCString&, const QByteArray&)), this, SLOT( siganalReceived(const QCString&, const QByteArray&) ) );
- scrollPanel = new ZScrollPanel(this); scrollPanel->resize ( 240, 320 ); //scrollPanel->setVScrollBarMode ( ZScrollPanel::AlwaysOff ); //ZConfig是读取/写入配置文件相当方便的一个函数 ZConfig skincfg("/ezxlocal/download/appwrite/setup/ezx_theme.cfg", false);//读取当前使用的主题 QString skincurrent = skincfg.readEntry(QString("THEME_GROUP_LOG"), QString("CurrentSkin"), ""); skincfg.flush(); qDebug("CurrentSkin:"+skincurrent); ZConfig colorcfg(QString(skincurrent+"/common.ini"), false);//读取Font_DetailItemStandardStd(查看内存)的字体颜色 color = colorcfg.readNumEntry("Font_DetailItemStandardStd","Color",0); colorcfg.flush(); qDebug("color:"+QString::number(color)); baoshiing=false;autopowerdown=false; autopower=0; systemcfg = new ZConfig(QString("%1/timer.ini").arg(getProgramDir()),true);//读取配置文件 timezoom = systemcfg->readBoolEntry("SYSTEM","timezoom",0); setbaoshi = systemcfg->readBoolEntry("SYSTEM","baoshi",0); automodel = systemcfg->readBoolEntry("SYSTEM","automodel",0);
- setMainWidgetTitle(ctr("时间助手")); //标题 //**************** QFont font ( qApp->font() ); font.setPointSize ( 30 );
- label = new ZLabel(ctr(""), this, "ZLabel",0, (ZSkinService::WidgetClsID)4);//标签 tiempo = new QTime();//构造一个时间
- label->setPreferredWidth(240); label->setFontColor(color);//设置颜色 label->setFont(font); label->setAlignment(ZLabel::AlignTop);
- scrollPanel->addChild(label,30,0);//布局 ZSeparator *sep = new ZSeparator();//分割线 sep->setLength(230);
- scrollPanel->addChild(sep,5,45); label1 = new ZLabel(QString(""), this, "ZLabel",0, (ZSkinService::WidgetClsID)4);//标签
- label1->setPreferredWidth(240); label1->setFontColor(color); label1->setFont(font); label1->setAlignment(ZLabel::AlignTop); //label->setGeometry(20,0,240,100);
- scrollPanel->addChild(label1,30,50); label1->setText(ctr("<feng.Z.Z>")); ZGroupBox *appForGBox = new ZGroupBox(ctr("设置"), scrollPanel, "AppFor",ZSkinService::clsZGroupBox); baoshilabel = new ZNewCheckBox(ctr("整点报时"),appForGBox); baoshilabel->setChecked(setbaoshi); baoshilabel->resize(220,baoshilabel->heightForWidth(220)); auto1label = new ZNewCheckBox(ctr("定时关机"),appForGBox); auto1label->setChecked(false); auto1label->resize(220,auto1label->heightForWidth(220)); autolabel = new ZNewCheckBox(ctr("自动关机"),appForGBox); autolabel->setChecked(false); autolabel->resize(220,autolabel->heightForWidth(220)); appForGBox->resize(230,150); scrollPanel->addChild(appForGBox,5,95);
- QVBoxLayout* layout = getVBoxLayout();
- layout->addWidget(scrollPanel); softKey = new ZSoftKey(tr("CST_2A") , this , this); //创建选项菜单
- QRect rect;
- operate = new ZOptionsMenu( rect , this); operate->insertItem(ctr("关机时刻") ,NULL, this, SLOT(setpowertime()), true, false, false, 0); operate->insertItem(ctr("倒计时关机"), NULL, this, SLOT(autopowertime()), true, false, false, 1); operate->insertItem(ctr("设置时区") , NULL, this, NULL, true, true, false, 2); operate->insertItem(ctr("自启模式") , NULL, this, NULL, true, true, false, 3); if(timezoom) operate->setItemChecked(2, true); if(automodel) operate->setItemChecked(3, true); menu_Main = new ZOptionsMenu(rect, this);
- //添加菜单 menu_Main->insertItem(ctr("开始") , NULL, this, SLOT(start()), true, false, false, 0); menu_Main->insertItem(ctr("设置") , operate, NULL, true, 1, 1); menu_Main->insertItem(ctr("隐藏") , NULL, this, SLOT(hide()), true, false, false, 2); menu_Main->insertSeparator(3, 3); menu_Main->insertItem(ctr("关于") , NULL, this, SLOT(about()), true, false, false, 4); menu_Main->insertItem(ctr("退出") , NULL, qApp, SLOT(quit()), true, false, false, 5); //左右软键 softKey->setTextForOptMenuHide(ctr("选项")); softKey->setTextForOptMenuShow(ctr("选择"),ctr("关闭")); softKey->setText(ZSoftKey::LEFT, ctr("选项")); softKey->setOptMenu(ZSoftKey::LEFT, menu_Main); softKey->setClickedSlot(ZSoftKey::RIGHT, this, SLOT(stop())); softKey->setText(ZSoftKey::RIGHT, ctr("暂停")); setSoftKey(softKey); timer = new QTimer(this); connect( timer, SIGNAL(timeout()), SLOT(updatetimer()) );//关联消息和槽:SIGNAL消息;SLOT槽 //消息和槽机制是QT的特色,具体上网查一下!这个很重要 timer->start( 10 ); if(!automodel) this->show();//显示UI,自启模式就是设置这里,如果开了自启模式,当程序运行时就不显示界面,只在后台工作,然后再点击时才显示
- } void Timer::siganalReceived( const QCString &msg, const QByteArray & )//接收消息时的动作
- {
- qDebug("timer_feng QCop Mes: "+msg);
- if ( msg == "show()" )
- {
- this->raise(); //this->show();
- this->setActiveWindow();
- } else if ( msg == "return()" )
- { this->show(); this->raise(); this->setActiveWindow();
- }
- }
- Timer::~Timer()//析构函数,也就是退出程序时执行
- { systemcfg->writeEntry("SYSTEM","timezoom",operate->isItemChecked(2));//退出时记录当前报时的状态 systemcfg->writeEntry("SYSTEM","baoshi",baoshilabel->isChecked()); systemcfg->writeEntry("SYSTEM","automodel",operate->isItemChecked(3));
- killTimers(); delete tiempo; }
- void Timer::baoshi(int zhengdian)//整点报时,这里用了SHELL,system()系统调用,会SHELL的人都应该明白这里的意思了 { label->setText(QString::number(zhengdian%24)+ctr("点整")); system(QString("echo "/usr/SYSqtapp/phone/alertprocess -playMode 2 -playvol 7 -playfile %1/baoshi/%2.mp3" >>%3/zhengdianbaoshi.sh").arg(getProgramDir()).arg(QString::number(zhengdian%24)).arg(getProgramDir())); system(QString("exec %1/zhengdianbaoshi.sh &").arg(getProgramDir())); system(QString("rm -f %2/zhengdianbaoshi.sh").arg(getProgramDir())); sleep(1); label->setText(""); sleep(1); label->setText(QString::number(zhengdian%24)+ctr("点整")); sleep(1); label->setText(""); sleep(1); label->setText(QString::number(zhengdian%24)+ctr("点整")); baoshiing=false; timer->start( 00 ); }
- void Timer::about()
- {
- QString b = ctr("时间助手 ver. 0.1 nby feng.Z.Z ne-mail: feng7553@163.comn黑色武装手机论坛n<http://moto.blackarm.net>");
- dlg = new ZMessageDlg(ctr("关于") , b , (ZMessageDlg::MessageDlgType)2 , 0 , this , "about" , true , 0);
- dlg->show();
- }
- void Timer::stop()//暂停 { softKey->setClickedSlot(ZSoftKey::RIGHT, this, SLOT(start()));//暂停时,改变右键的显示和槽SLOT联接 softKey->setText(ZSoftKey::RIGHT, ctr("继续")); if(autolabel->isChecked()) primaryTime -= tiempo->elapsed(); else{ autopowerdown=false; primaryTime = 0; } timer->stop(); } void Timer::updatetimer()//这个就是程序的中心:界面的更新,这个函数是不断被调用的,由timer调控 { h2=tiempo->currentTime().hour();//获得当前时间的小时 m2=tiempo->currentTime().minute();//分钟 s2=tiempo->currentTime().second();//秒 ms2=tiempo->currentTime().msec();//毫秒 if(operate->isItemChecked(2))//这个是设置时区,如果选项菜单那勾选了,变会在当前时间上加上8再取24的模 h2=(h2+8)%24; label->setText(QString::number(h2)+":"+QString::number(m2)+":"+QString::number(s2)+"."+QString::number(ms2)); //改变label(也就是时间)的显示 if (autolabel->isChecked()&&autopowerdown){//判断自动关机是否被勾选,和是否处于倒计时状态 int elapsedTime = primaryTime - tiempo->elapsed();//primaryTime就是倒计时关机的秒数.elapsed()是QTime的一个函数,统计从start()开始再现在过去的秒数. if(elapsedTime < 0) { ms1 = 0; s1 = 0; m1 = 0; h1 = 0; }else{ ms1 = (elapsedTime / 10) % 100;//把秒换算成小时/分/秒/毫秒 s1 = (elapsedTime / 1000) % 60; m1 = (elapsedTime / 60000) % 60; h1 = (elapsedTime / 3600000) % 60; } if(h1==0&&m1==0&&s1<3)//小倒计时小于3秒时,关机 system(QString("exec %1/shutdown &").arg(getProgramDir()));//关机 label1->setText(QString::number(h1)+":"+QString::number(m1)+":"+QString::number(s1)+"."+QString::number(ms1)); } if(!autolabel->isChecked()&&autopower>0) { label1->setText(QString::number(autopower/60)+":"+QString::number(autopower%60)+":00.00"); } if(!autolabel->isChecked()&&autopower==0) { label1->setText(ctr("<feng.Z.Z>")); } if (auto1label->isChecked()){//判断定时关机是否被勾先 if(h2==powertimer.hour()&&m2==powertimer.minute())//小时和分钟符合就关机 system(QString("exec %1/shutdown &").arg(getProgramDir())); } if(baoshilabel->isChecked()&&!baoshiing){//报时 if(m2==59 && s2>55){//还没到整点,分钟等于59,秒为55以上就开始报时,所以这个报时是提前了5秒的 baoshiing=true; baoshi(h2+1); } } } void Timer::start()//开始和继续都是这个函数 {
- if(autolabel->isChecked()){//当自动关机被勾先时 if(!autopowerdown&&autopower>0){//当autopowerdown为假并且倒计时的时间不为零时开始倒计时 autopowerdown=true; //primaryTime=0; primaryTime =autopower* 60000;//把autopower换算成毫秒 tiempo->start();//开始计时 } if(autopowerdown){//当autopowerdown为真,也就是在计时状态时 tiempo->restart(); } } softKey->setClickedSlot(ZSoftKey::RIGHT, this, SLOT(stop())); softKey->setText(ZSoftKey::RIGHT, ctr("暂停")); timer->start(00); } QString Timer::getProgramDir()//获得当前目录路径
- {
- QString ProgDir = QString ( qApp->argv() [0] ) ;
- int i = ProgDir.findRev ( "/" );
- ProgDir.remove ( i+1, ProgDir.length() - i );
- return ProgDir;
- } void Timer::setpowertime()//设置定时关机的时刻 { timer->stop(); softKey->setClickedSlot(ZSoftKey::RIGHT, this, SLOT(start())); softKey->setText(ZSoftKey::RIGHT, ctr("继续")); settime = new ZTimePickerDlg(NULL, this, "a", NULL); settime->setTitle(ctr("定时关机")); settime->setInstructText(ctr("选择自动关机的时刻")); settime->setInsTruncPolicy(false); settime->show(); connect(settime, SIGNAL(leftSoftkeyClicked()), SLOT(getpowertime()) ); connect(settime, SIGNAL(signalCenterSelKeyPressed()), SLOT(getpowertime()) ); } void Timer::getpowertime() { timer->start(00); powertimer=settime->getTime(); auto1label->setTitlePosition(ZCheckBox::TitleLeft); auto1label->setTitle("<"+QString::number(powertimer.hour())+":"+QString::number(powertimer.minute())+">"); } void Timer::autopowertime() { timer->stop(); ZNumPickerDlg* znpd = new ZNumPickerDlg(10, NULL, "ZNumPickerDlg", 0); znpd->setTitle(ctr("自动关机")); znpd->setInstructText(ctr("选择倒计时关机的时间(分钟)")); ZNumModule* znpm = znpd->getNumModule(); znpm->setMaxValue(999); znpm->setMinValue(0); znpm->setValue(0); if (znpd->exec()){ autopower = znpd->getNumValue(); if(autopower==0){ delete znpm; delete znpd; return; } autolabel->setTitlePosition(ZCheckBox::TitleLeft); autolabel->setTitle("<"+QString::number(autopower)+ctr("分钟>")); } timer->start(00); delete znpm; delete znpd; }