Parse skillInTraining element
[evehomescreen] / src / widget.cpp
1 // Copyright (C) 2010 Jaakko Kyro <jkyro@korjaussarja.net>
2 // This file is licenced under GPL, see COPYING
3 // for full licence information
4 #include "widget.h"
5 #include <QPainter>
6 #include "eveaccount.h"
7 #include "evesettingsdialog.h"
8 #include "eveskilltraining.h"
9 #include "skilltree.h"
10 #include <QtDebug>
11 #include <QNetworkConfigurationManager>
12 #include <QSettings>
13 #include <QTimer>
14 #include <QDateTime>
15
16 QTM_USE_NAMESPACE
17
18 Widget::Widget(QWidget *parent)
19     : QLabel(parent),
20     m_settings(new EveSettingsDialog(this)),
21     m_training(NULL),
22     m_net(new QNetworkConfigurationManager(this)),
23     m_skills(new SkillTree(this)),
24     m_model(new EveModel),
25     m_timer(new QTimer(this))
26 {
27     setAlignment(Qt::AlignCenter);
28     setAttribute(Qt::WA_TranslucentBackground);
29     setGeometry(0,0,150,180);
30     connect(m_net,SIGNAL(onlineStateChanged(bool)),this,SLOT(onlineStateChanged(bool)));
31     connect(m_skills,SIGNAL(skillsLoaded()),this,SLOT(update()));
32     m_skills->loadSkills();
33     m_model->loadSettings();
34     m_settings->setModel(m_model);
35     EveCharacter *c = m_model->selectedCharacter();
36     if (c != NULL) {
37         loadTraining();
38     }
39     connect(m_timer,SIGNAL(timeout()),this,SLOT(loadTraining()));
40 }
41
42 Widget::~Widget()
43 {
44
45 }
46
47
48 QSize Widget::sizeHint() const
49 {
50     return 2 * QLabel::sizeHint();
51 }
52
53 void Widget::paintEvent(QPaintEvent *event)
54 {
55     QPainter p(this);
56     p.setBrush(QColor(0, 0, 0, 128));
57     p.setPen(Qt::NoPen);
58     p.drawRoundedRect(rect(), 25, 25);
59     p.setPen(Qt::white);
60     QFont dFont(p.font());
61     dFont.setPixelSize(15);
62     p.setFont(dFont);
63     EveCharacter *character = m_model->selectedCharacter();
64     if (character != NULL) {
65         QPoint iconLoc((150-64)/2,(150-64)/2);
66         if (character->characterIcon) {
67             p.drawPixmap(iconLoc,*(character->characterIcon));
68         }
69         QRect nameLoc(0,10,150,20);
70         p.drawText(nameLoc,Qt::AlignCenter,character->name);
71         if (m_training) {
72             QRect skillTitle(0,110,150,50);
73             p.drawText(skillTitle,Qt::AlignCenter|Qt::TextWordWrap,
74                        QString("%1 %2").arg(m_skills->skillName(m_training->typeId))
75                                        .arg(m_training->level));
76             QRect skillLoc(0,155,150,20);
77             p.drawText(skillLoc,Qt::AlignCenter,m_training->endTime.toString(Qt::SystemLocaleShortDate));
78         }
79
80     }
81     p.end();
82
83     QLabel::paintEvent(event);
84 }
85
86 void Widget::showSettingsDialog()
87 {
88
89     int result = m_settings->exec();
90     if (result == QDialog::Accepted) {
91
92         m_model->setSelected(m_settings->selectedCharacter());
93         EveCharacter *character = m_model->selectedCharacter();
94         if (character != NULL) {
95             qDebug() << "Got character " << character->name;
96             connect(character,SIGNAL(imageLoaded()),this,SLOT(skillReady()));
97             character->fetchImage();
98
99             loadTraining();
100         }
101     }
102 }
103
104 void Widget::loadTraining()
105 {
106     EveCharacter *character = m_model->selectedCharacter();
107     if (character != NULL) {
108
109         qDebug() << "Fetch skills";
110         if (m_training == NULL)
111             m_training = new EveSkillTraining(this);
112         m_training->setAccount(m_settings->model().data());
113         m_training->setCharacter(character);
114         connect(m_training,SIGNAL(finished()),this,SLOT(trainingLoaded()));
115         m_training->fetchInfo();
116     }
117 }
118
119 void Widget::skillReady()
120 {
121     update();
122     m_training->fetchInfo();
123     m_model->saveSettings();
124 }
125
126 void Widget::onlineStateChanged(bool online)
127 {
128     qDebug() << "Online status changed, reloading info";
129     if (online) {
130         m_training->fetchInfo();
131     }
132 }
133
134 void Widget::trainingLoaded()
135 {
136     QDateTime now = QDateTime::currentDateTime();
137     if (m_training != NULL) {
138         int interval = 1000 * (now.secsTo(m_training->endTime) + 60);
139         qDebug() << "Timer interval" << interval;
140         m_timer->setInterval( interval );
141         m_timer->start();
142     }
143 }