Code:
#include "headlinewidget.h"
#include "ui_headlinewidget.h"
#include <QLCDNumber>
#include "mylabel.h"
#ifdef Q_OS_SYMBIAN
#include <AknAppUi.h>
#include <e32std.h> // Link against euser.lib
#endif
void HeadlineWidget::updateHeapStats() {
#ifdef Q_OS_SYMBIAN
TInt mem, size, limit;
User::Heap().AllocSize(mem);
size = User::Heap().Size();
limit = User::Heap().MaxLength();
QMessageBox msgBox1;
QString tmp = "Mem = " + QString::number(mem) + " Size = " + QString::number(size)+ " Limit = " + QString::number(limit);
msgBox1.setText(tmp);
msgBox1.setDefaultButton(QMessageBox::Ok);
int ret = msgBox1.exec();
#endif
}
HeadlineWidget::HeadlineWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::HeadlineWidget)
{
Q_INIT_RESOURCE(resources);
#ifdef Q_OS_SYMBIAN
//lock orientation
CAknAppUi* appUi = dynamic_cast<CAknAppUi*>(CEikonEnv::Static()->AppUi());
if(appUi){
QT_TRAP_THROWING(appUi ->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape));
}
#endif
ui->setupUi(this);
_swipeGesture=new MySwipeGesture(this);
this->setStyleSheet("background-color: rgb(52, 195, 76);");
ui->lcdNumber->display("00:00:00");
ui->label->setText(sharedData::currentHeadline.name);
connect(ui->lcdNumber,SIGNAL(beYellow()),this,SLOT(setYellow()));
connect(ui->lcdNumber,SIGNAL(beRed()),this,SLOT(setRed()));
connect(ui->lcdNumber,SIGNAL(jstBeGreen()),this,SLOT(jstSetGreen()));
connect(ui->lcdNumber,SIGNAL(closeNote()),this,SLOT(isClosed()));
connect(ui->lcdNumber,SIGNAL(setLabelText()),this,SLOT(setText()));
connect(_swipeGesture,SIGNAL(handleSwipe(MySwipeGesture::SwipeDirection)),this,SLOT(swipe(MySwipeGesture::SwipeDirection)));
connect(ui->lcdNumber,SIGNAL(headlineTimeChanged()),this,SLOT(setLCD_2()));
connect(ui->exit,SIGNAL(isClicked()),SLOT(isClosed()));
}
void HeadlineWidget::setText()
{
ui->label->setText(sharedData::currentHeadline.name);
ui->label->repaint();
}
void HeadlineWidget::setLCD_2()
{
ui->lcdNumber_2->display(sharedData::headlineTimeString);
}
void HeadlineWidget::showButton()
{
}
void HeadlineWidget::jstSetGreen()
{
ui->label->setText(sharedData::currentHeadline.name);
this->setStyleSheet("background-color: rgb(52, 195, 76);");
this->repaint();
}
void HeadlineWidget::setYellow()
{
ui->label->setText(sharedData::currentHeadline.name);
this->setStyleSheet("background-color: rgb(255, 217, 0);");
this->repaint();
}
void HeadlineWidget::setRed()
{
ui->label->setText(sharedData::currentHeadline.name);
this->setStyleSheet("background-color: rgb(255, 0, 0);");
this->repaint();
}
void HeadlineWidget::goNext()
{
this->setStyleSheet("background-color: rgb(52, 195, 76);");
this->repaint();
sharedData::currentHeadline.time=sharedData::remainingList.at(sharedData::headlineIndex);
sharedData::currentNote.headlines.takeAt(sharedData::headlineIndex);
sharedData::currentNote.headlines.insert(sharedData::headlineIndex,sharedData::currentHeadline);
//Auto calculation
if(sharedData::autoCal && sharedData::currentHeadline.time<0)
{
int totalRemaining=0;
for(int i=sharedData::headlineIndex+1;i<sharedData::remainingList.count();i++)
{
totalRemaining+= sharedData::remainingList.at(i);
}
for(int i=sharedData::headlineIndex+1;i<sharedData::remainingList.count();i++)
{
int newTime= sharedData::remainingList.at(i)- (float)(((float)sharedData::remainingList.at(i)/(float)totalRemaining)*(float)abs(sharedData::currentHeadline.time));
sharedData::remainingList.takeAt(i);
sharedData::remainingList.insert(i,newTime);
headline newHead=sharedData::currentNote.headlines.at(i);
newHead.time=newTime;
sharedData::currentNote.headlines.takeAt(i);
sharedData::currentNote.headlines.insert(i,newHead);
}
}
if(sharedData::headlineIndex<sharedData::currentNote.headlines.count()-1)
sharedData::headlineIndex++;
sharedData::currentHeadline=sharedData::currentNote.headlines.at(sharedData::headlineIndex);
sharedData::headlineStartTime=QTime::currentTime();
ui->label->setText(sharedData::currentHeadline.name);
ui->label->repaint();
}
void HeadlineWidget::isClosed()
{
QMessageBox msgBox;
msgBox.setText("Are you sure you want to close this talk and go to talks page?");
msgBox.setIcon(QMessageBox::Question);
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
if(ret==QMessageBox::Ok)
{
#ifdef Q_OS_SYMBIAN
// lock orientation
CAknAppUi* appUi = dynamic_cast<CAknAppUi*>(CEikonEnv::Static()->AppUi());
if(appUi){
QT_TRAP_THROWING(appUi ->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait));
}
#endif
this->close();
updateHeapStats();
}
}
HeadlineWidget::~HeadlineWidget()
{
delete ui;
}
MySwipeGesture::MySwipeGesture(QObject *parent)
:QObject(parent),_startPoint(0,0),_endPoint(0,0)
{}
void MySwipeGesture::handleEvent( QEvent *event)
{
if( event->type() == QEvent::MouseButtonPress ) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
_startPoint = mouseEvent->pos();
} else if( event->type() == QEvent::MouseButtonRelease ) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
_endPoint = mouseEvent->pos();
//process distance and direction
int xDiff = _startPoint.x() - _endPoint.x();
int yDiff = _startPoint.y() - _endPoint.y();
if( qAbs(xDiff) > qAbs(yDiff) && qAbs(xDiff)>120 ) {
// horizontal swipe detected, now find direction
if( _startPoint.x() > _endPoint.x() ) {
emit handleSwipe( Left);
} else {
emit handleSwipe( Right);
}
} else {
// vertical swipe detected, now find direction
if( _startPoint.y() > _endPoint.y() && qAbs(yDiff)<10 ) {
emit handleSwipe( Up);
} else {
emit handleSwipe( Down);
}
}
} else if( event->type() == QEvent::MouseMove ) {
//ignore event
}
}
bool HeadlineWidget::event(QEvent *event)
{
_swipeGesture->handleEvent(event);
return QWidget::event(event);
}
void HeadlineWidget::swipe(MySwipeGesture::SwipeDirection direction)
{
if(direction==MySwipeGesture::Left)
{
this->goNext();
}
else if(direction==MySwipeGesture::Right)
{
this->goPrev();
}
else
{
//click
//showButton();
}
}
void HeadlineWidget::goPrev()
{
sharedData::currentHeadline.time=sharedData::remainingList.at(sharedData::headlineIndex);
sharedData::currentNote.headlines.takeAt(sharedData::headlineIndex);
sharedData::currentNote.headlines.insert(sharedData::headlineIndex,sharedData::currentHeadline);
if(sharedData::headlineIndex>0)
sharedData::headlineIndex--;
sharedData::currentHeadline=sharedData::currentNote.headlines.at(sharedData::headlineIndex);
sharedData::headlineStartTime=QTime::currentTime();
ui->label->setText(sharedData::currentHeadline.name);
ui->label->repaint();
}