/[qspeakers]/mainwindow.cpp
ViewVC logotype

Contents of /mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations)
Mon Oct 6 20:36:42 2014 UTC (7 years, 1 month ago) by ben
File size: 12063 byte(s)
add custom plot class
1 #include <QtCore>
2 #include <QDebug>
3 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
4 #include <QStandardPaths>
5 #endif
6 #include <QDesktopServices>
7
8 #include "mainwindow.h"
9 #include "ui_mainwindow.h"
10
11 #include "speakerdb.h"
12 #include "importexport.h"
13 #include "plot.h"
14
15 MainWindow::MainWindow(QWidget *parent) :
16 QMainWindow(parent),
17 ui(new Ui::MainWindow),
18 projectSaved(false),
19 projectModified(false),
20 spkDialog(NULL),
21 alertDialog(NULL),
22 sealedPlot(NULL),
23 portedPlot(NULL),
24 bandpassPlot(NULL),
25 notInDbSpeaker(NULL)
26 {
27 ui->setupUi(this);
28
29 /* insert Qwt plotters in ui */
30
31 QHBoxLayout *ly1 = new QHBoxLayout(ui->sealedVolumePlotWidget);
32 sealedPlot = new Plot(tr("Sealed volume frequency response"), ui->sealedVolumePlotWidget);
33 ly1->insertWidget(0, sealedPlot);
34
35 QHBoxLayout *ly2 = new QHBoxLayout(ui->portedPlotWidget);
36 portedPlot = new Plot(tr("Ported volume frequency response"), ui->portedPlotWidget);
37 ly2->insertWidget(0, portedPlot);
38
39 QHBoxLayout *ly3 = new QHBoxLayout(ui->bandpassPlotWidget);
40 bandpassPlot = new Plot(tr("Bandpass volumes frequency response"), ui->bandpassPlotWidget);
41 ly3->insertWidget(0, bandpassPlot);
42
43 /* fill speaker combos */
44 ui->vendorComboBox->addItems(SpeakerDb::getVendors());
45 ui->modelComboBox->addItems(SpeakerDb::getModelsByVendor(ui->vendorComboBox->currentText()));
46
47 /* restore last saved project... */
48 ImportExport::restoreProject(currentSpeaker, currentSealedBox, currentPortedBox, currentBandPassBox);
49 projectSaved = true;
50 syncUiFromCurrentSpeaker();
51
52 /* display speaker's values */
53 ui->fsValueLabel->setNum(currentSpeaker.getFs());
54 ui->qtsValueLabel->setNum(currentSpeaker.getQts());
55 ui->vasValueLabel->setNum(currentSpeaker.getVas());
56 ui->sdValueLabel->setNum(currentSpeaker.getSd());
57 ui->reValueLabel->setNum(currentSpeaker.getRe());
58
59 /*
60 * connections
61 */
62
63 /* combos action */
64 connect(ui->vendorComboBox, SIGNAL(activated(QString)), this, SLOT(onVendorChanged(QString)));
65 connect(ui->modelComboBox, SIGNAL(activated(QString)), this, SLOT(onModelChanged(QString)));
66
67 /* menu actions */
68 connect(ui->actionProjectSave, SIGNAL(triggered()), this, SLOT(onProjectSave()));
69 connect(ui->actionProjectQuit, SIGNAL(triggered()), this, SLOT(onProjectQuit()));
70 connect(ui->actionSpeakerNew, SIGNAL(triggered()), this, SLOT(onSpeakerNew()));
71 connect(ui->actionSpeakerModify, SIGNAL(triggered()), this, SLOT(onSpeakerModify()));
72 connect(ui->actionSpeakerRemove, SIGNAL(triggered()), this, SLOT(onSpeakerRemove()));
73 connect(ui->actionProjectExport, SIGNAL(triggered()), this, SLOT(onProjectExport()));
74 connect(ui->actionProjectImport, SIGNAL(triggered()), this, SLOT(onProjectImport()));
75
76 /* app state */
77 connect(this, SIGNAL(currentSpeakerChanged(Speaker)), this, SLOT(onCurrentSpeakerChanged(const Speaker&)));
78 connect(this, SIGNAL(currentSealedBoxChanged(SealedBox)), this, SLOT(onCurrentSealedBoxChanged(const SealedBox&)));
79 connect(this, SIGNAL(currentPortedBoxChanged(PortedBox)), this, SLOT(onCurrentPortedBoxChanged(const PortedBox&)));
80 connect(this, SIGNAL(currentBandPassBoxChanged(BandPassBox)), this, SLOT(onCurrentBandPassBoxChanged(const BandPassBox&)));
81
82 }
83
84 MainWindow::~MainWindow()
85 {
86 delete ui;
87 }
88
89 void MainWindow::onProjectSave()
90 {
91 projectSaved = true;
92
93 ImportExport::saveProject(currentSpeaker, currentSealedBox, currentPortedBox, currentBandPassBox);
94 }
95
96 void MainWindow::onProjectExport()
97 {
98 QString home = MainWindow::getPath();
99 QString fileName = QFileDialog::getSaveFileName(this, tr("Export File"), home + tr("/untitled.qsp"), tr("QSpeaker project (*.qsp)"));
100
101 QFile file(fileName);
102 ImportExport::exportProject(file, currentSpeaker, currentSealedBox, currentPortedBox, currentBandPassBox);
103 }
104
105 void MainWindow::onProjectImport()
106 {
107 QString home = MainWindow::getPath();
108 QString fileName = QFileDialog::getOpenFileName(this, tr("Import File"), home, tr("QSpeaker project (*.qsp)"));
109 QFile file(fileName);
110 ImportExport::importProject(currentSpeaker, currentSealedBox, currentPortedBox, currentBandPassBox, file);
111 syncUiFromCurrentSpeaker();
112 emit currentSpeakerChanged(currentSpeaker);
113 emit currentSealedBoxChanged(currentSealedBox);
114 emit currentPortedBoxChanged(currentPortedBox);
115 emit currentBandPassBoxChanged(currentBandPassBox);
116 }
117
118 void MainWindow::onProjectQuit()
119 {
120 if (projectSaved) {
121 this->close();
122 return;
123 }
124
125 alertDialog = new AlertDialog(this);
126 connect(alertDialog, SIGNAL(accepted()), this, SLOT(onProjectQuitAccepted()));
127 connect(alertDialog, SIGNAL(rejected()), this, SLOT(onProjectQuitRejected()));
128 alertDialog->show();
129 }
130
131 void MainWindow::onProjectQuitAccepted()
132 {
133 onProjectQuitResponse(true);
134 }
135
136 void MainWindow::onProjectQuitRejected()
137 {
138 onProjectQuitResponse(false);
139 }
140
141 void MainWindow::onProjectQuitResponse(bool accept)
142 {
143 alertDialog->close();
144 delete alertDialog;
145 alertDialog = NULL;
146
147 if (accept)
148 this->close();
149 }
150
151 void MainWindow::syncUiFromCurrentSpeaker()
152 {
153 /* look if vendor is in the db */
154 QList<Speaker> speakers = SpeakerDb::getByVendor(currentSpeaker.getVendor());
155
156 if (speakers.count() == 0) {
157 /* if this is an unknown vendor, insert in combo */
158 ui->vendorComboBox->insertItem(0, currentSpeaker.getVendor());
159 ui->vendorComboBox->setCurrentIndex(0);
160 ui->modelComboBox->clear();
161 /* this means also a new model */
162 ui->modelComboBox->insertItem(0, currentSpeaker.getModel());
163 ui->modelComboBox->setCurrentIndex(0);
164 notInDbSpeaker = &currentSpeaker;
165 } else {
166 /* select correct vendor */
167 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
168 ui->vendorComboBox->setCurrentText(currentSpeaker.getVendor());
169 #else
170 ui->vendorComboBox->setCurrentIndex(ui->vendorComboBox->findText(currentSpeaker.getVendor()));
171 #endif
172 /* fill speakers of this vendor */
173 ui->modelComboBox->clear();
174 ui->modelComboBox->addItems(SpeakerDb::getModelsByVendor(currentSpeaker.getVendor()));
175
176 for (int i = 0; i < speakers.count(); i++) {
177 Speaker speaker = speakers[i];
178 if (speaker.getModel() == currentSpeaker.getModel()) {
179 if (speaker != currentSpeaker) {
180 /* speaker conflict, renaming */
181 currentSpeaker.setModel(speaker.getModel() + "*");
182 ui->modelComboBox->addItem(currentSpeaker.getModel());
183 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
184 ui->modelComboBox->setCurrentText(currentSpeaker.getModel());
185 #else
186 ui->modelComboBox->setCurrentIndex(ui->modelComboBox->findText(currentSpeaker.getModel()));
187 #endif
188 notInDbSpeaker = &currentSpeaker;
189 } else {
190 /* okay, select correct combo */
191 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
192 ui->modelComboBox->setCurrentText(currentSpeaker.getModel());
193 #else
194 ui->modelComboBox->setCurrentIndex(ui->modelComboBox->findText(currentSpeaker.getModel()));
195 #endif
196 notInDbSpeaker = NULL;
197 }
198 }
199 }
200 }
201 }
202
203 void MainWindow::syncUiFromCurrentSealedBox()
204 {
205 /* FIXME */
206 }
207
208 void MainWindow::syncUiFromCurrentPortedBox()
209 {
210 /* FIXME */
211 }
212
213 void MainWindow::syncUiFromCurrentBandPassBox()
214 {
215 /* FIXME */
216 }
217
218 void MainWindow::setCurrentSpeaker(const Speaker &spk)
219 {
220 if ((currentSpeaker.getVendor() == spk.getVendor()) &&
221 (currentSpeaker.getModel() == spk.getModel()))
222 return;
223
224 currentSpeaker = spk;
225 emit currentSpeakerChanged(spk);
226 }
227
228 QString MainWindow::getPath()
229 {
230 QString home;
231 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
232 home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
233 #else
234 home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
235 #endif
236 return home;
237 }
238
239 void MainWindow::onSpeakerNew()
240 {
241 spkDialog = new SpeakerDialog(this);
242 connect(spkDialog, SIGNAL(speakerInserted()), this, SLOT(onSpeakerInserted()));
243 connect(spkDialog, SIGNAL(speakerCancelled()), this, SLOT(onSpeakerCancelled()));
244 spkDialog->show();
245 }
246
247 void MainWindow::onSpeakerRemove()
248 {
249 SpeakerDb::removeByVendorAndModel(currentSpeaker.getVendor(), currentSpeaker.getModel());
250 qDebug() << __func__ << "FIXME: re-fetch speakers db";
251 }
252
253 void MainWindow::onSpeakerModify()
254 {
255 spkDialog = new SpeakerDialog(currentSpeaker, this);
256 connect(spkDialog, SIGNAL(speakerInserted()), this, SLOT(onSpeakerInserted()));
257 connect(spkDialog, SIGNAL(speakerCancelled()), this, SLOT(onSpeakerCancelled()));
258 spkDialog->show();
259 }
260
261 void MainWindow::onSpeakerInserted()
262 {
263 qDebug() << __func__ << "FIXME: re-fetch speakers db";
264 disconnect(spkDialog, SIGNAL(speakerInserted()), this, SLOT(onSpeakerInserted()));
265 disconnect(spkDialog, SIGNAL(speakerCancelled()), this, SLOT(onSpeakerCancelled()));
266 spkDialog->close();
267 delete spkDialog;
268 spkDialog = NULL;
269 }
270
271 void MainWindow::onSpeakerCancelled()
272 {
273 disconnect(spkDialog, SIGNAL(speakerInserted()), this, SLOT(onSpeakerInserted()));
274 disconnect(spkDialog, SIGNAL(speakerCancelled()), this, SLOT(onSpeakerCancelled()));
275 spkDialog->close();
276 delete spkDialog;
277 spkDialog = NULL;
278 }
279
280 void MainWindow::onVendorChanged(QString vendor)
281 {
282
283 if (notInDbSpeaker) {
284 /* FIXME: popup a "would you like to..." */
285 SpeakerDb::insertOrReplace(notInDbSpeaker->getVendor(), notInDbSpeaker->getModel(), *notInDbSpeaker);
286 notInDbSpeaker = NULL;
287 }
288
289 /* fill speakers of this vendor */
290 ui->modelComboBox->clear();
291 ui->modelComboBox->addItems(SpeakerDb::getModelsByVendor(vendor));
292 /* insert a fake speaker to let the choice to the user */
293 ui->modelComboBox->insertItem(0, "?");
294 ui->modelComboBox->setCurrentIndex(0);
295 emit currentSpeakerChanged(currentSpeaker);
296 }
297
298 void MainWindow::onModelChanged(QString model)
299 {
300 if (model.isNull() || model.isEmpty())
301 return;
302
303 if (notInDbSpeaker) {
304 /* FIXME: popup a "would you like to..." */
305 SpeakerDb::insertOrReplace(notInDbSpeaker->getVendor(), notInDbSpeaker->getModel(), *notInDbSpeaker);
306 notInDbSpeaker = NULL;
307 }
308
309 if ((ui->modelComboBox->findText("?") == 0) && (ui->modelComboBox->currentIndex() != 0)) {
310 /* user choose a speaker; safely remove fake speaker */
311 ui->modelComboBox->removeItem(0);
312 }
313
314 qDebug() << __func__ << "setting current speaker to" << model;
315 QString vendor = ui->vendorComboBox->currentText();
316 setCurrentSpeaker(SpeakerDb::getByVendorAndModel(vendor, model));
317 }
318
319 void MainWindow::onCurrentSpeakerChanged(const Speaker &spk)
320 {
321 ui->fsValueLabel->setNum(spk.getFs());
322 ui->qtsValueLabel->setNum(spk.getQts());
323 ui->vasValueLabel->setNum(spk.getVas());
324 ui->sdValueLabel->setNum(spk.getSd());
325 ui->reValueLabel->setNum(spk.getRe());
326
327 /* the user may have activated the "?" fake speaker */
328 if (spk.getModel().isNull() || spk.getModel().isEmpty()) {
329 qDebug() << "invalid speaker, not computing.";
330 return;
331 }
332
333 projectSaved = false;
334 if (ui->sealedTab->isActiveWindow())
335 syncUiFromCurrentSealedBox();
336 else if (ui->portedTab->isActiveWindow())
337 syncUiFromCurrentPortedBox();
338 else
339 syncUiFromCurrentBandPassBox();
340 }
341
342 void MainWindow::onCurrentSealedBoxChanged(const SealedBox &box)
343 {
344 projectSaved = false;
345 if (ui->sealedTab->isActiveWindow())
346 syncUiFromCurrentSealedBox();
347 }
348
349 void MainWindow::onCurrentPortedBoxChanged(const PortedBox &box)
350 {
351 projectSaved = false;
352 if (ui->portedTab->isActiveWindow())
353 syncUiFromCurrentPortedBox();
354 }
355
356 void MainWindow::onCurrentBandPassBoxChanged(const BandPassBox &box)
357 {
358 projectSaved = false;
359 if (ui->bandpassTab->isActiveWindow())
360 syncUiFromCurrentBandPassBox();
361 }

  ViewVC Help
Powered by ViewVC 1.1.26