Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
188 lines (140 loc) · 5.63 KB
/
Copy pathmainwindow.cpp
File metadata and controls
188 lines (140 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <QSettings>
#include <QMenu>
#include "dataprovider.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
qDebug() << "MainWindow::MainWindow";
QCoreApplication::setOrganizationName("somafm-qt");
//QCoreApplication::setOrganizationDomain("somafm.com");
QCoreApplication::setApplicationName("somafm-qt");
setWindowTitle(tr("SomaFM Radio Player"));
setWindowIcon(QIcon(":/icon.png"));
readSettings();
setFont(QFont(fontInfo().family(), -1));
m_topView = new QLabel(this);
//m_topView->setFixedSize(QSize(468, 60));
//m_topView->setWidth(m_topView->maximumWidth());
m_topView->setScaledContents(true);
QPixmap banner = QPixmap::fromImage(QImage(":/banner.gif"));
m_topView->setPixmap(banner);
m_tabWidget = new QTabWidget(this);
m_channelsView = new ChannelsView(this);
m_channelsView->setFocus();
m_tabWidget->addTab(m_channelsView, tr("Channels"));
m_historyView = new QWebEngineView();
m_historyView->setUrl(QUrl(QStringLiteral("http://somafm.com/groovesalad/songhistory.html")));
m_tabWidget->addTab(m_historyView, tr("History"));
m_newsView = new QWebEngineView();
m_newsView->setUrl(QUrl(QStringLiteral("http://somafm.com/news/")));
m_tabWidget->addTab(m_newsView, tr("News"));
m_settingsView = new SettingsView(this);
m_tabWidget->addTab(m_settingsView, tr("Settings"));
m_playerView = new PlayerView(this);
m_mainLayout = new QVBoxLayout;
m_mainLayout->addWidget(m_topView);
m_mainLayout->addWidget(m_tabWidget);
m_mainLayout->addWidget(m_playerView);
m_mainWidget = new QFrame(this);
m_mainWidget->setLayout(m_mainLayout);
setCentralWidget(m_mainWidget);
m_dataProvider = new DataProvider;
connect(m_channelsView, SIGNAL(requestChannelList()),
m_dataProvider, SLOT(provideChannelList()));
connect(m_dataProvider, SIGNAL(channelListProvided(ChannelList)),
m_channelsView, SLOT(updateChannelList(ChannelList)));
connect(m_channelsView, SIGNAL(requestChannelIcon(QString)),
m_dataProvider, SLOT(provideChannelIcon(QString)));
connect(m_dataProvider, SIGNAL(channelIconProvided(QString,QImage)),
m_channelsView, SLOT(updateChannelIcon(QString,QImage)));
connect(m_dataProvider, SIGNAL(channelImageProvided(QString,QImage)),
m_playerView, SLOT(updateChannelImage(QString,QImage)));
connect(m_channelsView, SIGNAL(channelActivated(QString)),
this, SLOT(startPlayer(QString)));
connect(m_playerView, SIGNAL(requestChannelImage(QString)),
m_dataProvider, SLOT(provideChannelImage(QString)));
connect(m_playerView, SIGNAL(playerStarted(QString)),
m_channelsView, SLOT(playingState(QString)));
connect(m_playerView, SIGNAL(playerStopped()),
m_channelsView, SLOT(stoppedState()));
connect(m_playerView, SIGNAL(titleUpdated(QString)),
this, SLOT(updateTitle(QString)));
connect(m_settingsView, SIGNAL(showTrayIcon(bool)),
this, SLOT(showTrayIcon(bool)));
m_dataProvider->provideChannelList();
createTrayIcon();
m_settingsView->applySettings();
}
void MainWindow::createTrayIcon() {
qDebug() << "MainWindow::createTrayIcon()";
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(QIcon(":icon.png"));
m_trayIcon->setToolTip(tr("Not playing"));
connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
auto *menu = new QMenu();
menu->setTitle(QStringLiteral("somafm-qt"));
// add menu entry to quit the app
QAction *quitAction = menu->addAction(tr("Quit"));
quitAction->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
connect(quitAction, &QAction::triggered, this,
&QApplication::quit);
m_trayIcon->setContextMenu(menu);
}
void MainWindow::showTrayIcon(bool show) {
qDebug() << "MainWindow::showTrayIcon(" << show << ")";
m_trayIcon->setVisible(show);
}
void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason) {
qDebug() << "MainWindow::trayIconActivated";
switch(reason) {
case QSystemTrayIcon::Trigger:
setVisible(!isVisible());
break;
default:
break;
}
}
void MainWindow::updateTitle(QString title) {
qDebug() << "MainWindow::updateTitle(" << title << ")";
m_trayIcon->setToolTip(title);
}
void MainWindow::startPlayer(QString id) {
qDebug() << "MainWindow::startPlayer : " << id;
QUrl history_url = "http://somafm.com/" + id + "/songhistory.html";
m_historyView->load(history_url);
QUrl url = m_dataProvider->getPlaylistUrl(id);
m_playerView->play(id, url);
}
void MainWindow::closeEvent(QCloseEvent *event) {
qDebug() << "MainWindow::closeEvent";
const bool isJustHide = QSettings().value("showTrayIcon", true).toBool();
// don't ignore close event when the app is hidden to tray
// this can occur when the OS issues close events on shutdown
if (isJustHide && !isHidden()) {
hide();
event->ignore();
} else {
m_trayIcon->hide();
delete m_trayIcon;
event->accept();
}
}
MainWindow::~MainWindow()
{
qDebug() << "MainWindow::~MainWindow()";
writeSettings();
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("size", size());
settings.setValue("pos", pos());
}
void MainWindow::readSettings()
{
QSettings settings;
resize(settings.value("size", QSize(520, 800)).toSize());
move(settings.value("pos", QPoint(0, 0)).toPoint());
}