1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
4 #include <QtCore/QCoreApplication>
6 #include <QtGui/QMessageBox>
9 #include <QDialogButtonBox>
10 #include <QScrollArea>
14 #include "pirkeysetmetadata.h"
16 #include "pirkeysetwidgetitem.h"
17 #include "pirselectkeysetform.h"
18 #include "pirselectdeviceform.h"
19 #include "pirpreferencesform.h"
20 #include "pirdocumentationform.h"
21 #include "piraboutform.h"
22 #include "dialogs/pirtabschoicedialog.h"
23 #include "dialogs/pirfavoritesdialog.h"
25 #include "pirkeysetmanager.h"
26 #include "pirpanelmanager.h"
27 #include "macros/pirmacromanager.h"
32 // Some ugly globals used for thread communications:
34 // A global to show that a command is being processed:
35 bool commandInFlight = false;
36 QMutex commandIFMutex;
38 // The stopRepeatingFlag boolean is the method used to tell running commands
39 // in the worker thread to stop:
40 bool stopRepeatingFlag = false;
41 QMutex stopRepeatingMutex;
44 extern PIRMakeMgr makeManager;
47 MainWindow::MainWindow(QWidget *parent)
48 : QMainWindow(parent),
49 ui(new Ui::MainWindow),
59 currentKeyset(1) // Zero is not a valid keyset any more
63 // Make this a Maemo 5 stacked widget:
64 setAttribute(Qt::WA_Maemo5StackedWindow);
66 // Create the managers:
67 myKeysets = new PIRKeysetManager();
68 myMacros = new PIRMacroManager(this);
69 myPanels = new PIRPanelManager(this);
71 // Display the panels:
72 myPanels->updateTabSet();
74 // Construct the rest of the forms:
75 selectKeysetForm = new PIRSelectKeysetForm(this);
76 favoritesDialog = new PIRFavoritesDialog(this);
77 myKeysets->populateListWidgets(selectKeysetForm, favoritesDialog);
78 selectKeysetForm->populateKeysetComboBox(myPanels->getKeysetComboBox());
80 selectDeviceForm = new PIRSelectDeviceForm(this);
81 PIRKeysetMetaData::populateDevices(selectDeviceForm);
83 preferencesForm = new PIRPreferencesForm(this, myKeysets);
85 // Retrieve the user's preferences:
86 QSettings settings("pietrzak.org", "Pierogi");
87 if (settings.contains("currentKeysetName"))
89 myKeysets->findKeysetID(
90 settings.value("currentKeysetMake").toString(),
91 settings.value("currentKeysetName").toString(),
95 selectKeysetForm->selectKeyset(currentKeyset);
97 // Add the corner buttons:
98 insertCornerButtons();
100 // Set up all the buttons:
102 myPanels->updateUserButtons();
104 // Make sure the three selection lists don't show different selections:
105 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
106 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
108 // keyset name -> device name
111 SIGNAL(itemActivated(QListWidgetItem *)),
113 SLOT(clearSelection()),
114 Qt::QueuedConnection);
116 // device name -> keyset name
119 SIGNAL(itemActivated(QListWidgetItem *)),
121 SLOT(clearSelection()),
122 Qt::QueuedConnection);
125 // The PIRModprobe object should take care of setting up and shutting down
126 // the lirc_rx51 kernel module, if necessary:
128 if (modprobeObj.loadRX51Module() != 0)
130 // Couldn't load module, quit:
132 errBox.setText("Couldn't load lirc_rx51 kernel module!");
133 errBox.setIcon(QMessageBox::Warning);
135 // throw; // Need a clean way to exit here!!!
141 MainWindow::~MainWindow()
143 if (aboutForm) delete aboutForm;
144 if (documentationForm) delete documentationForm;
145 if (preferencesForm) delete preferencesForm;
146 if (selectDeviceForm) delete selectDeviceForm;
147 if (favoritesDialog) delete favoritesDialog;
148 if (selectKeysetForm) delete selectKeysetForm;
150 if (myPanels) delete myPanels;
151 if (myMacros) delete myMacros;
152 if (myKeysets) delete myKeysets;
157 void MainWindow::setOrientation(ScreenOrientation orientation)
159 #if defined(Q_OS_SYMBIAN)
160 // If the version of Qt on the device is < 4.7.2, that attribute won't work
161 if (orientation != ScreenOrientationAuto) {
162 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
163 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
164 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
168 #endif // Q_OS_SYMBIAN
170 Qt::WidgetAttribute attribute;
171 switch (orientation) {
172 #if QT_VERSION < 0x040702
173 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
174 case ScreenOrientationLockPortrait:
175 attribute = static_cast<Qt::WidgetAttribute>(128);
177 case ScreenOrientationLockLandscape:
178 attribute = static_cast<Qt::WidgetAttribute>(129);
181 case ScreenOrientationAuto:
182 attribute = static_cast<Qt::WidgetAttribute>(130);
184 #else // QT_VERSION < 0x040702
185 case ScreenOrientationLockPortrait:
186 attribute = Qt::WA_LockPortraitOrientation;
188 case ScreenOrientationLockLandscape:
189 attribute = Qt::WA_LockLandscapeOrientation;
192 case ScreenOrientationAuto:
193 attribute = Qt::WA_AutoOrientation;
195 #endif // QT_VERSION < 0x040702
197 setAttribute(attribute, true);
200 void MainWindow::showExpanded()
202 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
204 #elif defined(Q_WS_MAEMO_5)
212 void MainWindow::enableButtons()
214 // Just to be sure, check to see if the keyset has been populated:
215 myKeysets->populateKeyset(this, currentKeyset);
219 unsigned int dk = preferencesForm->getDefaultKeyset();
220 if (preferencesForm->defaultControlsVolume() && dk)
222 myKeysets->populateKeyset(this, dk);
223 myPanels->enableButtons(myKeysets, currentKeyset, dk);
227 myPanels->enableButtons(myKeysets, currentKeyset);
232 myPanels->enableButtons(myKeysets, currentKeyset);
237 void MainWindow::useMainPanel()
239 myPanels->useMainPanel();
243 void MainWindow::useAltMainPanel()
245 myPanels->useAltMainPanel();
249 QString MainWindow::getCurrentMake()
251 return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
255 QString MainWindow::getCurrentName()
257 return myKeysets->getDisplayName(currentKeyset);
261 QString MainWindow::getCurrentFullName()
263 return selectKeysetForm->getCurrentKeysetName();
267 QString MainWindow::getKeysetMake(
270 return makeManager.getMakeString(myKeysets->getMake(id));
274 QString MainWindow::getKeysetName(
277 return myKeysets->getDisplayName(id);
281 QString MainWindow::getFullKeysetName(
284 return selectKeysetForm->getKeysetName(id);
288 bool MainWindow::findKeysetID(
293 return myKeysets->findKeysetID(make, name, id);
297 void MainWindow::receivedExternalWarning(
301 errBox.setText(warning);
302 errBox.setIcon(QMessageBox::Warning);
309 void MainWindow::on_actionSelectKeyset_triggered()
311 selectKeysetForm->show();
314 void MainWindow::on_actionBrowse_Device_List_triggered()
316 selectDeviceForm->show();
319 void MainWindow::on_actionPreferences_triggered()
321 preferencesForm->show();
324 void MainWindow::on_actionAbout_triggered()
328 aboutForm = new PIRAboutForm(this);
334 void MainWindow::on_actionDocumentation_triggered()
336 if (!documentationForm)
338 documentationForm = new PIRDocumentationForm(this);
341 documentationForm->show();
347 void MainWindow::keysetSelectionChanged(
348 QListWidgetItem *item)
350 if (!item) return; // Should probably display error message here!
352 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
354 if (!kwi) return; // Also need to say something here
356 if (currentKeyset == kwi->getID())
358 // We're already on that keyset, so nothing to do:
362 // Clean up and remove the current keyset:
363 myKeysets->clearKeyset(currentKeyset);
365 currentKeyset = kwi->getID();
367 QSettings settings("pietrzak.org", "Pierogi");
371 makeManager.getMakeString(kwi->getMake()));
375 myKeysets->getDisplayName(currentKeyset));
381 void MainWindow::finalCleanup()
383 // Perform any necessary cleanup work here.
385 // Make certain that the thread stops repeating:
390 void MainWindow::addToFavorites(
391 PIRKeysetWidgetItem *kwi)
393 //Add keyset to the favorites:
394 favoritesDialog->addItem(new PIRKeysetWidgetItem(kwi));
396 // And, add the keyset id to the persistent list:
397 QSettings settings("pietrzak.org", "Pierogi");
399 int favSettingsSize = settings.beginReadArray("favorites");
402 settings.beginWriteArray("favorites");
403 settings.setArrayIndex(favSettingsSize);
407 makeManager.getMakeString(kwi->getMake()));
409 settings.setValue("keysetName", kwi->getInternalName());
415 void MainWindow::removeFromFavorites(
416 unsigned int keysetID)
418 favoritesDialog->removeItem(keysetID);
420 // Remove this item from the persistent list. Well, actually, it seems a
421 // little more convenient to just blow away the existing list of favorites
422 // and rewrite it, as modifying an existing QSettings array in the middle
423 // seems a bit hard...
424 QSettings settings("pietrzak.org", "Pierogi");
426 settings.remove("favorites");
428 int count = favoritesDialog->getCount();
430 // If the count is empty, we can stop right here:
431 if (count == 0) return;
435 PIRKeysetWidgetItem *kwi = NULL;
436 settings.beginWriteArray("favorites");
437 while (index < count)
439 kwi = favoritesDialog->getItem(index);
441 settings.setArrayIndex(index);
446 makeManager.getMakeString(myKeysets->getMake(id)));
448 settings.setValue("keysetName", myKeysets->getDisplayName(id));
457 void MainWindow::populateFavorites()
459 QSettings settings("pietrzak.org", "Pierogi");
461 int size = settings.beginReadArray("favorites");
465 PIRKeysetWidgetItem *kwi;
469 settings.setArrayIndex(index);
470 make = settings.value("keysetMake").toString();
471 name = settings.value("keysetName").toString();
473 kwi = myKeysets->makeKeysetItem(make, name);
475 // Did the item creation work?
478 // Keyset does exist, so continue:
479 favoritesDialog->addItem(kwi);
490 bool MainWindow::startRepeating(
493 QMutexLocker locker(&commandIFMutex);
494 if (!commandInFlight)
496 commandInFlight = true;
497 emit buttonPressed(currentKeyset, name);
507 bool MainWindow::startRepeating(
509 unsigned int keysetID)
511 QMutexLocker locker(&commandIFMutex);
512 if (!commandInFlight)
514 commandInFlight = true;
515 emit buttonPressed(keysetID, name);
525 void MainWindow::stopRepeating()
527 QMutexLocker locker(&stopRepeatingMutex);
528 stopRepeatingFlag = true;
532 void MainWindow::selectPrevFavKeyset()
534 favoritesDialog->selectPrevFavKeyset();
538 void MainWindow::selectNextFavKeyset()
540 favoritesDialog->selectNextFavKeyset();
544 void MainWindow::insertCornerButtons()
546 // Set up the dialog boxes:
547 PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
548 // favoritesDialog = new PIRFavoritesDialog(this);
550 // Next, set up the corner buttons:
551 QPushButton *button =
552 new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
554 button->setFlat(true);
561 Qt::QueuedConnection);
563 ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
566 new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
568 button->setFlat(true);
575 Qt::QueuedConnection);
577 ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
581 void MainWindow::disableUpdates()
583 ui->mainTabWidget->setUpdatesEnabled(false);
587 void MainWindow::enableUpdates()
589 ui->mainTabWidget->setUpdatesEnabled(true);
593 void MainWindow::clearTabs()
595 ui->mainTabWidget->clear();
599 void MainWindow::addTab(
603 ui->mainTabWidget->addTab(page, label);
606 void MainWindow::setupTabs(
609 myPanels->setupTabs(name);
613 bool MainWindow::selectNextKeyset()
615 return selectKeysetForm->selectNextKeyset();
619 bool MainWindow::selectPrevKeyset()
621 return selectKeysetForm->selectPrevKeyset();
625 bool MainWindow::selectFirstKeyset()
627 return selectKeysetForm->selectFirstKeyset();
631 void MainWindow::openCurrentKeysetDialog()
633 selectKeysetForm->openCurrentKeysetDialog();
637 void MainWindow::updateKeysetSelection(
638 unsigned int targetID)
640 selectKeysetForm->selectKeyset(targetID);
644 PIRMacroPack *MainWindow::getUserPack()
646 return myMacros->getUserPack();
650 PIRMacroPack *MainWindow::getMultitapPack()
652 return myMacros->getMultitapPack();
656 void MainWindow::keyPressEvent(
662 myMacros->handleKeypress('A');
665 myMacros->handleKeypress('B');
668 myMacros->handleKeypress('C');
671 myMacros->handleKeypress('D');
674 myMacros->handleKeypress('E');
677 myMacros->handleKeypress('F');
680 myMacros->handleKeypress('G');
683 myMacros->handleKeypress('H');
686 myMacros->handleKeypress('I');
689 myMacros->handleKeypress('J');
692 myMacros->handleKeypress('K');
695 myMacros->handleKeypress('L');
698 myMacros->handleKeypress('M');
701 myMacros->handleKeypress('N');
704 myMacros->handleKeypress('O');
707 myMacros->handleKeypress('P');
710 myMacros->handleKeypress('Q');
713 myMacros->handleKeypress('R');
716 myMacros->handleKeypress('S');
719 myMacros->handleKeypress('T');
722 myMacros->handleKeypress('U');
725 myMacros->handleKeypress('V');
728 myMacros->handleKeypress('W');
731 myMacros->handleKeypress('X');
734 myMacros->handleKeypress('Y');
737 myMacros->handleKeypress('Z');
740 myMacros->handleKeypress(' ');
743 QMainWindow::keyPressEvent(event);
749 bool MainWindow::hasMacroButton(
750 unsigned int buttonID)
752 return myMacros->hasMacroButton(buttonID);
756 QString MainWindow::getMacroButtonText(
757 unsigned int buttonID)
759 return myMacros->getMacroButtonText(buttonID);
763 void MainWindow::executeMacroButton(
764 unsigned int buttonID)
766 myMacros->executeMacroButton(buttonID);
770 void MainWindow::updateUserButtons()
772 myPanels->updateUserButtons();
776 void MainWindow::storeMacros()
778 myMacros->storeSettings();
782 void MainWindow::setMacroKbdFocus(
785 myMacros->setKbdFocus(index);
789 void MainWindow::setMacroBtnFocus(
792 myMacros->setBtnFocus(index);