1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
4 #include <QtCore/QCoreApplication>
6 #include <QtGui/QMessageBox>
10 #include "pirkeysetmetadata.h"
11 #include "pirkeysetwidgetitem.h"
12 #include "pirselectkeysetform.h"
13 #include "pirselectdeviceform.h"
14 #include "pirpanelselectionform.h"
15 #include "pirpreferencesform.h"
16 #include "pirdocumentationform.h"
17 #include "piraboutform.h"
18 #include "pirkeysetmanager.h"
19 #include "pirpanelmanager.h"
24 // Some ugly globals used for thread communications:
26 // A global to show that a command is being processed:
27 bool commandInFlight = false;
28 QMutex commandIFMutex;
30 // The stopRepeatingFlag boolean is the method used to tell running commands
31 // in the worker thread to stop:
32 bool stopRepeatingFlag = false;
33 QMutex stopRepeatingMutex;
36 extern PIRMakeMgr makeManager;
39 MainWindow::MainWindow(QWidget *parent)
40 : QMainWindow(parent),
41 ui(new Ui::MainWindow),
44 panelSelectionForm(0),
48 currentKeyset(1) // Zero is not a valid keyset any more
52 // Make this a Maemo 5 stacked widget:
53 setAttribute(Qt::WA_Maemo5StackedWindow);
55 // Create the managers:
56 myKeysets = new PIRKeysetManager();
57 myPanels = new PIRPanelManager(this);
59 // Construct the forms:
60 panelSelectionForm = new PIRPanelSelectionForm(this);
61 myPanels->setupPanels(panelSelectionForm);
63 selectKeysetForm = new PIRSelectKeysetForm(this);
64 myKeysets->populateSelectionWidget(selectKeysetForm);
66 selectDeviceForm = new PIRSelectDeviceForm(this);
67 PIRKeysetMetaData::populateDevices(selectDeviceForm);
69 preferencesForm = new PIRPreferencesForm(this, myKeysets);
71 // Remember any favorites the user has already set:
74 // Retrieve the user's preferences:
75 QSettings settings("pietrzak.org", "Pierogi");
76 if (settings.contains("currentKeysetName"))
78 myKeysets->findKeysetID(
79 settings.value("currentKeysetMake").toString(),
80 settings.value("currentKeysetName").toString(),
86 QListWidget *fkw = myPanels->getFavoritesListWidget();
90 SIGNAL(itemActivated(QListWidgetItem *)),
92 SLOT(keysetSelectionChanged(QListWidgetItem *)),
93 Qt::QueuedConnection);
95 // Make sure the three selection lists don't show different selections:
96 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
97 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
99 // favorites -> keyset name
102 SIGNAL(itemActivated(QListWidgetItem *)),
104 SLOT(clearSelection()),
105 Qt::QueuedConnection);
107 // favorites -> device name
110 SIGNAL(itemActivated(QListWidgetItem *)),
112 SLOT(clearSelection()),
113 Qt::QueuedConnection);
115 // keyset name -> favorites
118 SIGNAL(itemActivated(QListWidgetItem *)),
120 SLOT(clearSelection()),
121 Qt::QueuedConnection);
123 // device name -> favorites
126 SIGNAL(itemActivated(QListWidgetItem *)),
128 SLOT(clearSelection()),
129 Qt::QueuedConnection);
131 // keyset name -> device name
134 SIGNAL(itemActivated(QListWidgetItem *)),
136 SLOT(clearSelection()),
137 Qt::QueuedConnection);
139 // device name -> keyset name
142 SIGNAL(itemActivated(QListWidgetItem *)),
144 SLOT(clearSelection()),
145 Qt::QueuedConnection);
148 // The PIRModprobe object should take care of setting up and shutting down
149 // the lirc_rx51 kernel module, if necessary:
151 if (modprobeObj.loadRX51Module() != 0)
153 // Couldn't load module, quit:
155 errBox.setText("Couldn't load lirc_rx51 kernel module!");
156 errBox.setIcon(QMessageBox::Warning);
158 // throw; // Need a clean way to exit here!!!
164 MainWindow::~MainWindow()
167 if (selectKeysetForm) delete selectKeysetForm;
168 if (selectDeviceForm) delete selectDeviceForm;
169 if (panelSelectionForm) delete panelSelectionForm;
170 if (documentationForm) delete documentationForm;
171 if (aboutForm) delete aboutForm;
176 void MainWindow::setOrientation(ScreenOrientation orientation)
178 #if defined(Q_OS_SYMBIAN)
179 // If the version of Qt on the device is < 4.7.2, that attribute won't work
180 if (orientation != ScreenOrientationAuto) {
181 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
182 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
183 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
187 #endif // Q_OS_SYMBIAN
189 Qt::WidgetAttribute attribute;
190 switch (orientation) {
191 #if QT_VERSION < 0x040702
192 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
193 case ScreenOrientationLockPortrait:
194 attribute = static_cast<Qt::WidgetAttribute>(128);
196 case ScreenOrientationLockLandscape:
197 attribute = static_cast<Qt::WidgetAttribute>(129);
200 case ScreenOrientationAuto:
201 attribute = static_cast<Qt::WidgetAttribute>(130);
203 #else // QT_VERSION < 0x040702
204 case ScreenOrientationLockPortrait:
205 attribute = Qt::WA_LockPortraitOrientation;
207 case ScreenOrientationLockLandscape:
208 attribute = Qt::WA_LockLandscapeOrientation;
211 case ScreenOrientationAuto:
212 attribute = Qt::WA_AutoOrientation;
214 #endif // QT_VERSION < 0x040702
216 setAttribute(attribute, true);
219 void MainWindow::showExpanded()
221 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
223 #elif defined(Q_WS_MAEMO_5)
231 void MainWindow::enableButtons()
233 // Just to be sure, check to see if the keyset has been populated:
234 myKeysets->populateKeyset(this, currentKeyset);
238 unsigned int dk = preferencesForm->getDefaultKeyset();
239 if (preferencesForm->defaultControlsVolume() && dk)
241 myKeysets->populateKeyset(this, dk);
242 myPanels->enableButtons(myKeysets, currentKeyset, dk);
246 myPanels->enableButtons(myKeysets, currentKeyset);
251 myPanels->enableButtons(myKeysets, currentKeyset);
256 void MainWindow::useMainPanel()
258 myPanels->useMainPanel();
262 void MainWindow::useAltMainPanel()
264 myPanels->useAltMainPanel();
268 QString MainWindow::getCurrentMake()
270 return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
274 QString MainWindow::getCurrentName()
276 return myKeysets->getDisplayName(currentKeyset);
280 void MainWindow::receivedExternalWarning(
284 errBox.setText(warning);
285 errBox.setIcon(QMessageBox::Warning);
292 void MainWindow::on_actionSelectKeyset_triggered()
294 selectKeysetForm->show();
297 void MainWindow::on_actionSelect_Device_By_Name_triggered()
299 selectDeviceForm->show();
302 void MainWindow::on_actionArrange_Button_Panels_triggered()
304 panelSelectionForm->show();
307 void MainWindow::on_actionPreferences_triggered()
309 preferencesForm->show();
312 void MainWindow::on_actionAbout_triggered()
316 aboutForm = new PIRAboutForm(this);
322 void MainWindow::on_actionDocumentation_triggered()
324 if (!documentationForm)
326 documentationForm = new PIRDocumentationForm(this);
329 documentationForm->show();
335 void MainWindow::keysetSelectionChanged(
336 QListWidgetItem *item)
338 if (!item) return; // Should probably display error message here!
340 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
342 if (!kwi) return; // Also need to say something here
344 if (currentKeyset == kwi->getID())
346 // We're already on that keyset, so nothing to do:
350 currentKeyset = kwi->getID();
352 QSettings settings("pietrzak.org", "Pierogi");
353 // settings.setValue("currentKeyset", currentKeyset);
357 makeManager.getMakeString(kwi->getMake()));
361 myKeysets->getDisplayName(currentKeyset));
367 void MainWindow::finalCleanup()
369 // Perform any necessary cleanup work here.
371 // Make certain that the thread stops repeating:
376 void MainWindow::addCurrentKeyset(
379 // Is the current keyset already a favorite?
380 int count = qlw->count();
382 PIRKeysetWidgetItem *kwi = NULL;
383 while (index < count)
385 kwi = dynamic_cast<PIRKeysetWidgetItem *>(
388 if (kwi && (kwi->getID() == currentKeyset))
390 // Current keyset already in list! No need to continue.
396 // Ok, add the current keyset to the favorites:
397 PIRMakeName make = myKeysets->getMake(currentKeyset);
399 QString name = makeManager.getMakeString(make);
401 name.append(myKeysets->getDisplayName(currentKeyset));
403 qlw->addItem(new PIRKeysetWidgetItem(name, currentKeyset, make));
405 // And, add the keyset id to the persistent list:
406 QSettings settings("pietrzak.org", "Pierogi");
408 int favSettingsSize = settings.beginReadArray("favorites");
411 settings.beginWriteArray("favorites");
412 settings.setArrayIndex(favSettingsSize);
413 // settings.setValue("keysetID", currentKeyset);
417 makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
419 settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
425 void MainWindow::removeFavoriteKeyset(
428 // Deleting an item removes it from the list, so just grab the currently
429 // selected item and delete it:
430 QListWidgetItem *item = qlw->currentItem();
432 if (item) delete item;
434 // Remove this item from the persistent list. Well, actually, it seems a
435 // little more convenient to just blow away the existing list of favorites
436 // and rewrite it, as modifying an existing QSettings array in the middle
437 // seems a bit hard...
438 QSettings settings("pietrzak.org", "Pierogi");
440 settings.remove("favorites");
442 int count = qlw->count();
444 // If the count is empty, we can stop right here:
445 if (count == 0) return;
449 PIRKeysetWidgetItem *kwi = NULL;
450 settings.beginWriteArray("favorites");
451 while (index < count)
453 kwi = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
455 settings.setArrayIndex(index);
456 // settings.setValue("keysetID", kwi->getID());
461 makeManager.getMakeString(myKeysets->getMake(id)));
463 settings.setValue("keysetName", myKeysets->getDisplayName(id));
471 void MainWindow::populateFavorites()
473 QSettings settings("pietrzak.org", "Pierogi");
475 int size = settings.beginReadArray("favorites");
479 PIRKeysetWidgetItem *kwi;
483 settings.setArrayIndex(index);
484 make = settings.value("keysetMake").toString();
485 name = settings.value("keysetName").toString();
487 kwi = myKeysets->makeKeysetItem(make, name);
489 // Did the item creation work?
492 // Keyset does exist, so continue:
493 myPanels->addFavoritesItem(kwi);
503 void MainWindow::startRepeating(
506 QMutexLocker locker(&commandIFMutex);
507 if (!commandInFlight)
509 commandInFlight = true;
510 emit buttonPressed(currentKeyset, name);
515 void MainWindow::startRepeating(
517 unsigned int keysetID)
519 QMutexLocker locker(&commandIFMutex);
520 if (!commandInFlight)
522 commandInFlight = true;
523 emit buttonPressed(keysetID, name);
528 void MainWindow::stopRepeating()
530 QMutexLocker locker(&stopRepeatingMutex);
531 stopRepeatingFlag = true;
535 void MainWindow::selectPrevFavKeyset()
537 myPanels->selectPrevFavKeyset();
541 void MainWindow::selectNextFavKeyset()
543 myPanels->selectNextFavKeyset();
547 void MainWindow::selectPanel(
550 ui->selectPanelComboBox->setCurrentIndex(index);
554 void MainWindow::managePanel(
558 myPanels->managePanel(name, state);
562 void MainWindow::insertPanel(
565 const QString &displayName)
567 ui->selectPanelComboBox->insertItem(index, displayName);
568 ui->stackedButtonsWidget->insertWidget(index, panel);
572 void MainWindow::removePanel(
576 ui->stackedButtonsWidget->removeWidget(panel);
577 ui->selectPanelComboBox->removeItem(index);
581 void MainWindow::on_prevPanelButton_clicked()
583 int count = ui->selectPanelComboBox->count();
585 // No need to do anything if there are less than two panels available:
586 if (count < 2) return;
588 int index = ui->selectPanelComboBox->currentIndex();
598 ui->selectPanelComboBox->setCurrentIndex(index);
602 void MainWindow::on_nextPanelButton_clicked()
604 int count = ui->selectPanelComboBox->count();
606 // No need to do anything if there are less than two panels available:
607 if (count < 2) return;
609 int index = ui->selectPanelComboBox->currentIndex();
610 if (index == count - 1)
619 ui->selectPanelComboBox->setCurrentIndex(index);
623 void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
625 ui->stackedButtonsWidget->setCurrentIndex(index);