You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Demon tarkoitus

Demon tarkoituksena on näyttää Qt:n mahdollisuuksia visualisointiin. Demo on ohjelmoitu Qt SDK for Windows alustalla.

Screenshot:

BatteryDemo.pro
QT += core gui
TARGET = BatteryDemo
TEMPLATE = app
INCLUDEPATH += C:\analogwidgets\include
LIBS += C:\analogwidgets\include\analogwidgets\libanalogwidgets.a
SOURCES += main.cpp\
        mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
mainwindow.h
 #ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QColorGroup>
#include <QPalette>
#include <QColor>
#include <QTimer>
#include <QPainter>
#include <QPixmap>
#include <QGraphicsScene>
#include <QPropertyAnimation>
#include <QThread>
namespace Ui {
    class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QTimer *timer;
    QTimer *timer2;
    QTimer *timer3;
    QTimer *AnimTimer;
private slots:
    void CalculateAmpers();
    void CalculateCharge();
    void RotateGear();
    void CalculateRotation();
    void CalculateTemp();
    void InitGV();
    void Anim1();
    void Anim2();
    void AnimatePanel();
private:
    Ui::MainWindow *ui;
    float Ampers;
    int Charge;
    float i;
    int p;
    float ChargeP;
    float Temp;
    float BatteryMultiplier;
    float AmperMultiplier;
    bool Flag;
    QString Text;
    QGraphicsScene *S;
};
#endif // MAINWINDOW_H

mainwindow.cpp
 #include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    timer2 = new QTimer(this);
    timer3 = new QTimer(this);
    AnimTimer = new QTimer(this);
    S = new QGraphicsScene(this);
    //Alustuksia
    InitGV();
    Flag = false;
    p=0;
    i=0;
    Ampers=0;
    Charge=1000;
    BatteryMultiplier=0.5;
    AmperMultiplier=0.8;
    CalculateCharge();
    Temp=20;
    ui->panelView->setScene(new QGraphicsScene(this));
    //Labelit piiloon
    ui->redArrowLabel->hide();
    ui->greenArrowLabel->hide();
    ui->TempMeter->setValue(Temp);
    //Labeleihin kuvat
    ui->BatteryLabel->setPixmap(QPixmap("Pics/batteryGreen.png"));
    ui->redArrowLabel->setPixmap(QPixmap("Pics/ArrowRed.png"));
    ui->greenArrowLabel->setPixmap(QPixmap("Pics/ArrowGreen.png"));
    ui->PanelLabel->setPixmap(QPixmap("Pics/panel.png"));
    ui->panelView->setBackgroundBrush(Qt::darkBlue);
    //Yhdistetaan timerit funktioihin
    connect(timer,SIGNAL(timeout()),this,SLOT(CalculateCharge()));
    connect(timer,SIGNAL(timeout()),this,SLOT(CalculateAmpers()));
    connect(timer3,SIGNAL(timeout()),this,SLOT(CalculateTemp()));
    connect(AnimTimer,SIGNAL(timeout()),this,SLOT(Anim1()));
    connect(AnimTimer,SIGNAL(timeout()),this,SLOT(Anim2()));
    connect(AnimTimer,SIGNAL(timeout()),this,SLOT(AnimatePanel()));
    connect(timer2,SIGNAL(timeout()),this,SLOT(RotateGear()));
    //Yhdistetaan slideri rattaan animointiin
    connect(ui->UsageSlider,SIGNAL(valueChanged(int)),this,SLOT(CalculateRotation()));
    //Kaynnistetaan timerit
    timer->start(500);
    timer3->start(2000);
    AnimTimer->start(1100);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::CalculateAmpers() //"Lasketaan" rattaan kayttama virta ja naytetaan se mittarissa
{
    if(Charge |= 0)Ampers = ui->UsageSlider->value()*AmperMultiplier;
    else Ampers = 0;
    ui->AmperMeter->setValue(Ampers);
}
void MainWindow::CalculateCharge() //"Lasketaan" akun lataus, naytetaan se progressbarissa ja prosentteina
                                   //(kuvat vaihtuu akun kohdalla latauksen mukaan, vihrea, keltainen, punainen)
{
    if(ui->UsageSlider->value()>0)
    {
        Charge=Charge-ui->UsageSlider->value()*BatteryMultiplier;
        if(Charge<0) Charge = 0;
    }
    else
    {
        Charge = Charge+10;
        if(Charge>1000) Charge=1000;
    }
    ChargeP=(float)Charge/(float)1000*(float)100;
    Text.setNum(ChargeP);
    ui->BatteryBar->setValue(Charge);
    ui->ChargeP->setText(Text);
    if(Charge>=300) ui->BatteryLabel->setPixmap(QPixmap("Pics/batteryGreen.png"));
    if(Charge<300 && Charge>=100) ui->BatteryLabel->setPixmap(QPixmap("Pics/batteryYellow.png"));
    if(Charge<100) ui->BatteryLabel->setPixmap(QPixmap("Pics/batteryRed.png"));
}
void MainWindow::RotateGear() //Rotate animaation toteutus
{
    if(Charge|=0)ui->graphicsView->rotate(2);
}
void MainWindow::InitGV()  //Graphicsviewin initialisointi
{
    S->addPixmap(QPixmap("Pics/gear.png")); //laitetan pixmap sceneen S
    ui->graphicsView->setScene(S); //Asetetaan graphicsviewiin scene S (kuva rattaasta)
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //Scrollbarit pois
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void MainWindow::CalculateRotation() //Maaritellaan rattaan pyorimisnopeus sliderista (timerin aikajakso, joka kutsuu qgraphicsviewin rotatea)
{
    int value;
    if(Charge==0) value=0;
    else value = ui->UsageSlider->value();
    switch(value){
    case 0: timer2->stop(); break;
    case 1: timer2->start(200); break;
    case 2: timer2->start(150); break;
    case 3: timer2->start(100); break;
    case 4: timer2->start(80); break;
    case 5: timer2->start(60); break;
    case 6: timer2->start(50); break;
    case 7: timer2->start(40); break;
    case 8: timer2->start(30); break;
    case 9: timer2->start(20); break;
    case 10: timer2->start(10); break;
    }
}
void MainWindow::CalculateTemp()  //"Lasketaan" akun lampotila
{
    i=0.1;
    if(ui->UsageSlider->value()>0) Temp= Temp + ui->UsageSlider->value()*i;
    else Temp = Temp - 1*i;
    if(Temp>66) Temp=66;
    if(Temp<20) Temp=20;
    ui->TempMeter->setValue(Temp);
}
void MainWindow::Anim1()  // KS. anim2()
{
    QPropertyAnimation* anim1 = new QPropertyAnimation(ui->redArrowLabel,"geometry");
    anim1->setDuration(1100);
    anim1->setStartValue(QRect(630,400,91,41));
    anim1->setEndValue(ui->redArrowGoal->geometry());
    if(ui->UsageSlider->value()>0 && Charge > 0)
    {
        ui->redArrowLabel->show();
        anim1->start();
    }
    else ui->redArrowLabel->hide();
}
void MainWindow::Anim2()
{
    QPropertyAnimation* anim2 = new QPropertyAnimation(ui->greenArrowLabel,"geometry");  //Luodaan animaatio, otetaan sille geometria vihreän nuolen labelista
    anim2->setDuration(1100); //Maaritetan animaation kesto
    anim2->setStartValue(QRect(810,110,122,144));   //Maaritellaan animaation alkuarvo (x,y,leveys,korkeus)
    anim2->setEndValue(ui->greenArrowGoal->geometry());  //Maaritellaan animaation loppuarvot ( tassa tapauksessa ui:in laitettu label, jonka paikka on loppuarvo)
    if(ui->UsageSlider->value()==0 && Charge<1000)  //jos kayttosliderin arvo on isompi kuin 0 = ratas pyorii ja lataus on alle taydet
    {
        ui->greenArrowLabel->show();                //nayta nuoli
        anim2->start();                             //aloita animaatio
    }
    else ui->greenArrowLabel->hide();               //muuten nuoli piiloon
}
void MainWindow::AnimatePanel()  //"animaatio", jolla aurinkopaneelin vari vaihtuu latauksessa
{
    if(ui->UsageSlider->value()==0 && Charge<1000)
    {
       p++;
       ui->panelView->setBackgroundBrush(Qt::blue);
    }
    if(ui->UsageSlider->value()==0 && Charge<1000 && p>1)
    {
        ui->panelView->setBackgroundBrush(Qt::darkBlue);
        p=0;
    }
    if(Charge==1000) ui->panelView->setBackgroundBrush(Qt::darkBlue);
}

main.cpp
 #include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
  • No labels
You must log in to comment.