Merge branch 'master' into QJson_Experiments
[buliscores] / src / src / mainwidget.cpp
1 #include <QSettings>
2 #include <QMouseEvent>
3 #include <QApplication>
4 #include <QProcess>
5
6 #include "mainwidget.h"
7 #include "backendkicker.h"
8 #include "matchdaymodel.h"
9 #include "settingsdialog.h"
10
11 //QJSON
12 #include <qjson/serializer.h>
13
14 MainWidget::MainWidget(QWidget *parent) :
15     QWidget(parent),
16     m_mediaObject_tor(new Phonon::MediaObject(this)),
17     m_mediaObject_pfeife(new Phonon::MediaObject(this)),
18     m_audioOutput_tor(new Phonon::AudioOutput(Phonon::MusicCategory, this)),
19     m_audioOutput_pfeife(new Phonon::AudioOutput(Phonon::MusicCategory, this)),
20     m_backend(new BackendKicker(this)),
21     m_datamodel(new MatchDayModel(this, m_backend)),
22     m_scoretbl(new ScoreTable(m_datamodel)),
23     m_settingsdlg(new SettingsDialog()),
24     m_settings(qApp->organizationName(), qApp->applicationName())
25 {
26     QFont f;
27     QPalette palette;
28
29     this->hide();
30     this->setAttribute(Qt::WA_TranslucentBackground);
31
32     // label
33     m_statuslbl.hide();
34     m_statuslbl.setText(tr("BuLi Scores!"));
35     f.setPixelSize(40);
36     palette.setColor(QPalette::Background, QColor(0, 0, 0, 127));
37     palette.setColor(QPalette::Foreground, QColor(255, 255, 255, 127));
38     m_statuslbl.setPalette(palette);
39     m_statuslbl.setAutoFillBackground(true);
40     m_statuslbl.setBackgroundRole(QPalette::Background);
41     m_statuslbl.setAlignment(Qt::AlignCenter);
42
43     m_statuslbl.setGeometry(0, 0, 400, 200);
44     m_statuslbl.setFont(f);
45
46     // table
47     m_layout.addWidget(&m_statuslbl);
48     m_layout.addWidget(m_scoretbl);
49     m_layout.setSizeConstraint(QLayout::SetFixedSize);
50     this->setLayout(&m_layout);
51     this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
52
53     m_statuslbl.show();
54
55     connect(m_settingsdlg, SIGNAL(accepted()),
56             this, SLOT(onSettingsAccepted()));
57
58     connect(m_backend, SIGNAL(updateFinished(int)),
59             this, SLOT(onBackendUpdateFinished(int)));
60
61     connect(m_backend, SIGNAL(updateStarted()),
62             this, SLOT(onBackendUpdateStarted()));
63
64     connect(m_backend, SIGNAL(matchAdded(Match*)),
65            this, SLOT(onMatchAdded(Match*)));
66
67     m_mediaObject_tor->setCurrentSource(Phonon::MediaSource("/usr/share/buliscores/tor.mp3"));
68     m_mediaObject_pfeife->setCurrentSource(Phonon::MediaSource("/usr/share/buliscores/trillerpfeife.mp3"));
69
70     Phonon::createPath(m_mediaObject_tor,     m_audioOutput_tor);
71     Phonon::createPath(m_mediaObject_pfeife,  m_audioOutput_pfeife);
72 }
73
74 void MainWidget::mousePressEvent(QMouseEvent* event)
75 {
76     QJson::Serializer serializer;
77     QVariantMap data;
78
79     qDebug() << "widget tapped";
80
81     // only needed for testing on desktop
82     if (event->button() == Qt::RightButton) {
83         this->showSettingsDialog();
84     } else {
85         data.insert("BackendData", m_backend->serializableData());
86         qWarning() << serializer.serialize(m_backend->serializableData());
87
88 //        m_mediaObject_pfeife->seek(0);
89 //        if (m_settings.value("Sounds", false).toBool()) {
90 //            m_mediaObject_pfeife->play();
91 //        }
92 //        m_mediaObject_tor->seek(0);
93 //        if (m_settings.value("Sounds", false).toBool()) {
94 //            m_mediaObject_tor->play();
95 //        }
96 //        qDebug() << "pfeife state 2: " << m_mediaObject_pfeife->state();
97 //        qDebug() << "tor state 2: " << m_mediaObject_pfeife->state();
98     }
99 }
100
101 void MainWidget::onSettingsAccepted()
102 {
103     m_backend->selectLeague(m_settings.value("League", "1. Bundesliga").toString());
104 }
105
106 void MainWidget::onBackendUpdateStarted()
107 {
108
109 }
110
111 void MainWidget::onBackendUpdateFinished(int)
112 {
113     if (m_statuslbl.isHidden() == false) {
114         this->hide();
115         m_statuslbl.hide();
116         m_scoretbl->show();
117         this->show();
118     }
119 }
120
121 void MainWidget::onMatchAdded(Match* match) {
122     connect(match, SIGNAL(scoreChanged(int,int,int,int)),
123             this, SLOT(onScoreChange()));
124
125     connect(match, SIGNAL(stateChanged(Match::MatchState)),
126             this, SLOT(onMatchStateChanged(Match::MatchState)));
127
128     qDebug() << "Match (" << match->homeTeam() << " vs "
129              << match->awayTeam() << ") : registering signals";
130 }
131
132 void MainWidget::onScoreChange()
133 {
134     if (m_settings.value("Sounds", false).toBool()) {
135         m_mediaObject_tor->seek(0);
136         m_mediaObject_tor->play();
137         qDebug() << "Playing sound: tor";
138     }
139 }
140
141 void MainWidget::onMatchStateChanged(Match::MatchState)
142 {
143     if (m_settings.value("Sounds", false).toBool()) {
144         m_mediaObject_pfeife->seek(0);
145         m_mediaObject_pfeife->play();
146         qDebug() << "Playing sound: trillerpfeife";
147     }
148 }
149
150 void MainWidget::showSettingsDialog()
151 {
152     m_settingsdlg->show();
153 }
154