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()
145 if (aboutForm) delete aboutForm;
146 if (documentationForm) delete documentationForm;
147 if (preferencesForm) delete preferencesForm;
148 if (selectDeviceForm) delete selectDeviceForm;
149 if (favoritesDialog) delete favoritesDialog;
150 if (selectKeysetForm) delete selectKeysetForm;
152 if (myPanels) delete myPanels;
153 if (myMacros) delete myMacros;
154 // if (myKeysets) delete myKeysets;
159 void MainWindow::setOrientation(ScreenOrientation orientation)
161 #if defined(Q_OS_SYMBIAN)
162 // If the version of Qt on the device is < 4.7.2, that attribute won't work
163 if (orientation != ScreenOrientationAuto) {
164 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
165 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
166 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
170 #endif // Q_OS_SYMBIAN
172 Qt::WidgetAttribute attribute;
173 switch (orientation) {
174 #if QT_VERSION < 0x040702
175 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
176 case ScreenOrientationLockPortrait:
177 attribute = static_cast<Qt::WidgetAttribute>(128);
179 case ScreenOrientationLockLandscape:
180 attribute = static_cast<Qt::WidgetAttribute>(129);
183 case ScreenOrientationAuto:
184 attribute = static_cast<Qt::WidgetAttribute>(130);
186 #else // QT_VERSION < 0x040702
187 case ScreenOrientationLockPortrait:
188 attribute = Qt::WA_LockPortraitOrientation;
190 case ScreenOrientationLockLandscape:
191 attribute = Qt::WA_LockLandscapeOrientation;
194 case ScreenOrientationAuto:
195 attribute = Qt::WA_AutoOrientation;
197 #endif // QT_VERSION < 0x040702
199 setAttribute(attribute, true);
202 void MainWindow::showExpanded()
204 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
206 #elif defined(Q_WS_MAEMO_5)
214 void MainWindow::enableButtons()
216 // Just to be sure, check to see if the keyset has been populated:
217 myKeysets->populateKeyset(this, currentKeyset);
221 unsigned int dk = preferencesForm->getDefaultKeyset();
222 if (preferencesForm->defaultControlsVolume() && dk)
224 myKeysets->populateKeyset(this, dk);
225 myPanels->enableButtons(myKeysets, currentKeyset, dk);
229 myPanels->enableButtons(myKeysets, currentKeyset);
234 myPanels->enableButtons(myKeysets, currentKeyset);
239 void MainWindow::useMainPanel()
241 myPanels->useMainPanel();
245 void MainWindow::useAltMainPanel()
247 myPanels->useAltMainPanel();
251 QString MainWindow::getCurrentMake()
253 return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
257 QString MainWindow::getCurrentName()
259 return myKeysets->getDisplayName(currentKeyset);
263 QString MainWindow::getCurrentFullName()
265 return selectKeysetForm->getCurrentKeysetName();
269 QString MainWindow::getKeysetMake(
272 return makeManager.getMakeString(myKeysets->getMake(id));
276 QString MainWindow::getKeysetName(
279 return myKeysets->getDisplayName(id);
283 QString MainWindow::getFullKeysetName(
286 return selectKeysetForm->getKeysetName(id);
290 bool MainWindow::findKeysetID(
295 return myKeysets->findKeysetID(make, name, id);
299 void MainWindow::receivedExternalWarning(
303 errBox.setText(warning);
304 errBox.setIcon(QMessageBox::Warning);
311 void MainWindow::on_actionSelectKeyset_triggered()
313 selectKeysetForm->show();
316 void MainWindow::on_actionBrowse_Device_List_triggered()
318 selectDeviceForm->show();
321 void MainWindow::on_actionPreferences_triggered()
323 preferencesForm->show();
326 void MainWindow::on_actionAbout_triggered()
330 aboutForm = new PIRAboutForm(this);
336 void MainWindow::on_actionDocumentation_triggered()
338 if (!documentationForm)
340 documentationForm = new PIRDocumentationForm(this);
343 documentationForm->show();
349 void MainWindow::keysetSelectionChanged(
350 QListWidgetItem *item)
352 if (!item) return; // Should probably display error message here!
354 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
356 if (!kwi) return; // Also need to say something here
358 if (currentKeyset == kwi->getID())
360 // We're already on that keyset, so nothing to do:
364 // Clean up and remove the current keyset:
365 myKeysets->clearKeyset(currentKeyset);
367 currentKeyset = kwi->getID();
369 QSettings settings("pietrzak.org", "Pierogi");
373 makeManager.getMakeString(kwi->getMake()));
377 myKeysets->getDisplayName(currentKeyset));
383 void MainWindow::finalCleanup()
385 // Perform any necessary cleanup work here.
387 // Make certain that the thread stops repeating:
392 void MainWindow::addToFavorites(
393 PIRKeysetWidgetItem *kwi)
395 //Add keyset to the favorites:
396 favoritesDialog->addItem(new PIRKeysetWidgetItem(kwi));
398 // And, add the keyset id to the persistent list:
399 QSettings settings("pietrzak.org", "Pierogi");
401 int favSettingsSize = settings.beginReadArray("favorites");
404 settings.beginWriteArray("favorites");
405 settings.setArrayIndex(favSettingsSize);
409 makeManager.getMakeString(kwi->getMake()));
411 settings.setValue("keysetName", kwi->getInternalName());
417 void MainWindow::removeFromFavorites(
418 unsigned int keysetID)
420 favoritesDialog->removeItem(keysetID);
422 // Remove this item from the persistent list. Well, actually, it seems a
423 // little more convenient to just blow away the existing list of favorites
424 // and rewrite it, as modifying an existing QSettings array in the middle
425 // seems a bit hard...
426 QSettings settings("pietrzak.org", "Pierogi");
428 settings.remove("favorites");
430 int count = favoritesDialog->getCount();
432 // If the count is empty, we can stop right here:
433 if (count == 0) return;
437 PIRKeysetWidgetItem *kwi = NULL;
438 settings.beginWriteArray("favorites");
439 while (index < count)
441 kwi = favoritesDialog->getItem(index);
443 settings.setArrayIndex(index);
448 makeManager.getMakeString(myKeysets->getMake(id)));
450 settings.setValue("keysetName", myKeysets->getDisplayName(id));
459 void MainWindow::populateFavorites()
461 QSettings settings("pietrzak.org", "Pierogi");
463 int size = settings.beginReadArray("favorites");
467 PIRKeysetWidgetItem *kwi;
471 settings.setArrayIndex(index);
472 make = settings.value("keysetMake").toString();
473 name = settings.value("keysetName").toString();
475 kwi = myKeysets->makeKeysetItem(make, name);
477 // Did the item creation work?
480 // Keyset does exist, so continue:
481 favoritesDialog->addItem(kwi);
492 bool MainWindow::startRepeating(
495 QMutexLocker locker(&commandIFMutex);
496 if (!commandInFlight)
498 commandInFlight = true;
499 emit buttonPressed(currentKeyset, name);
509 bool MainWindow::startRepeating(
511 unsigned int keysetID)
513 QMutexLocker locker(&commandIFMutex);
514 if (!commandInFlight)
516 commandInFlight = true;
517 emit buttonPressed(keysetID, name);
527 void MainWindow::stopRepeating()
529 QMutexLocker locker(&stopRepeatingMutex);
530 stopRepeatingFlag = true;
534 void MainWindow::selectPrevFavKeyset()
536 favoritesDialog->selectPrevFavKeyset();
540 void MainWindow::selectNextFavKeyset()
542 favoritesDialog->selectNextFavKeyset();
546 void MainWindow::insertCornerButtons()
548 // Set up the dialog boxes:
549 PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
550 // favoritesDialog = new PIRFavoritesDialog(this);
552 // Next, set up the corner buttons:
553 QPushButton *button =
554 new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
556 button->setFlat(true);
563 Qt::QueuedConnection);
565 ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
568 new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
570 button->setFlat(true);
577 Qt::QueuedConnection);
579 ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
583 void MainWindow::disableUpdates()
585 ui->mainTabWidget->setUpdatesEnabled(false);
589 void MainWindow::enableUpdates()
591 ui->mainTabWidget->setUpdatesEnabled(true);
595 void MainWindow::clearTabs()
597 ui->mainTabWidget->clear();
601 void MainWindow::addTab(
605 ui->mainTabWidget->addTab(page, label);
608 void MainWindow::setupTabs(
611 myPanels->setupTabs(name);
615 bool MainWindow::selectNextKeyset()
617 return selectKeysetForm->selectNextKeyset();
621 bool MainWindow::selectPrevKeyset()
623 return selectKeysetForm->selectPrevKeyset();
627 bool MainWindow::selectFirstKeyset()
629 return selectKeysetForm->selectFirstKeyset();
633 void MainWindow::openCurrentKeysetDialog()
635 selectKeysetForm->openCurrentKeysetDialog();
639 void MainWindow::updateKeysetSelection(
640 unsigned int targetID)
642 selectKeysetForm->selectKeyset(targetID);
646 PIRMacroPack *MainWindow::getUserPack()
648 return myMacros->getUserPack();
652 PIRMacroPack *MainWindow::getMultitapPack()
654 return myMacros->getMultitapPack();
658 void MainWindow::keyPressEvent(
664 myMacros->handleKeypress('A');
667 myMacros->handleKeypress('B');
670 myMacros->handleKeypress('C');
673 myMacros->handleKeypress('D');
676 myMacros->handleKeypress('E');
679 myMacros->handleKeypress('F');
682 myMacros->handleKeypress('G');
685 myMacros->handleKeypress('H');
688 myMacros->handleKeypress('I');
691 myMacros->handleKeypress('J');
694 myMacros->handleKeypress('K');
697 myMacros->handleKeypress('L');
700 myMacros->handleKeypress('M');
703 myMacros->handleKeypress('N');
706 myMacros->handleKeypress('O');
709 myMacros->handleKeypress('P');
712 myMacros->handleKeypress('Q');
715 myMacros->handleKeypress('R');
718 myMacros->handleKeypress('S');
721 myMacros->handleKeypress('T');
724 myMacros->handleKeypress('U');
727 myMacros->handleKeypress('V');
730 myMacros->handleKeypress('W');
733 myMacros->handleKeypress('X');
736 myMacros->handleKeypress('Y');
739 myMacros->handleKeypress('Z');
742 myMacros->handleKeypress(' ');
745 QMainWindow::keyPressEvent(event);
751 bool MainWindow::hasMacroButton(
752 unsigned int buttonID)
754 return myMacros->hasMacroButton(buttonID);
758 QString MainWindow::getMacroButtonText(
759 unsigned int buttonID)
761 return myMacros->getMacroButtonText(buttonID);
765 void MainWindow::executeMacroButton(
766 unsigned int buttonID)
768 myMacros->executeMacroButton(buttonID);
772 void MainWindow::updateUserButtons()
774 myPanels->updateUserButtons();
778 void MainWindow::storeMacros()
780 myMacros->storeSettings();
784 void MainWindow::setMacroKbdFocus(
787 myMacros->setKbdFocus(index);
791 void MainWindow::setMacroBtnFocus(
794 myMacros->setBtnFocus(index);