1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
4 #include <QtCore/QCoreApplication>
6 #include <QtGui/QMessageBox>
9 #include <QDialogButtonBox>
10 #include <QScrollArea>
13 #include "pirkeysetmetadata.h"
15 #include "pirkeysetwidgetitem.h"
16 #include "pirselectkeysetform.h"
17 #include "pirselectdeviceform.h"
18 #include "pirpreferencesform.h"
19 #include "pirdocumentationform.h"
20 #include "piraboutform.h"
21 #include "dialogs/pirtabschoicedialog.h"
22 #include "dialogs/pirfavoritesdialog.h"
24 #include "pirkeysetmanager.h"
25 #include "pirpanelmanager.h"
30 // Some ugly globals used for thread communications:
32 // A global to show that a command is being processed:
33 bool commandInFlight = false;
34 QMutex commandIFMutex;
36 // The stopRepeatingFlag boolean is the method used to tell running commands
37 // in the worker thread to stop:
38 bool stopRepeatingFlag = false;
39 QMutex stopRepeatingMutex;
42 extern PIRMakeMgr makeManager;
45 MainWindow::MainWindow(QWidget *parent)
46 : QMainWindow(parent),
47 ui(new Ui::MainWindow),
55 currentKeyset(1) // Zero is not a valid keyset any more
59 // Make this a Maemo 5 stacked widget:
60 setAttribute(Qt::WA_Maemo5StackedWindow);
62 // Create the managers:
63 myKeysets = new PIRKeysetManager();
64 myPanels = new PIRPanelManager(this);
66 // Display the panels:
67 myPanels->updateTabSet();
69 // Construct the rest of the forms:
70 selectKeysetForm = new PIRSelectKeysetForm(this);
71 favoritesDialog = new PIRFavoritesDialog(this);
72 myKeysets->populateListWidgets(selectKeysetForm, favoritesDialog);
74 selectDeviceForm = new PIRSelectDeviceForm(this);
75 PIRKeysetMetaData::populateDevices(selectDeviceForm);
77 preferencesForm = new PIRPreferencesForm(this, myKeysets);
79 // Retrieve the user's preferences:
80 QSettings settings("pietrzak.org", "Pierogi");
81 if (settings.contains("currentKeysetName"))
83 myKeysets->findKeysetID(
84 settings.value("currentKeysetMake").toString(),
85 settings.value("currentKeysetName").toString(),
89 // Add the corner buttons:
90 insertCornerButtons();
94 QListWidget *fkw = favoritesDialog->getFavoritesListWidget();
98 SIGNAL(itemActivated(QListWidgetItem *)),
100 SLOT(keysetSelectionChanged(QListWidgetItem *)),
101 Qt::QueuedConnection);
103 // Make sure the three selection lists don't show different selections:
104 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
105 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
107 // favorites -> keyset name
110 SIGNAL(itemActivated(QListWidgetItem *)),
112 SLOT(clearSelection()),
113 Qt::QueuedConnection);
115 // favorites -> device name
118 SIGNAL(itemActivated(QListWidgetItem *)),
120 SLOT(clearSelection()),
121 Qt::QueuedConnection);
123 // keyset name -> favorites
126 SIGNAL(itemActivated(QListWidgetItem *)),
128 SLOT(clearSelection()),
129 Qt::QueuedConnection);
131 // device name -> favorites
134 SIGNAL(itemActivated(QListWidgetItem *)),
136 SLOT(clearSelection()),
137 Qt::QueuedConnection);
139 // keyset name -> device name
142 SIGNAL(itemActivated(QListWidgetItem *)),
144 SLOT(clearSelection()),
145 Qt::QueuedConnection);
147 // device name -> keyset name
150 SIGNAL(itemActivated(QListWidgetItem *)),
152 SLOT(clearSelection()),
153 Qt::QueuedConnection);
156 // The PIRModprobe object should take care of setting up and shutting down
157 // the lirc_rx51 kernel module, if necessary:
159 if (modprobeObj.loadRX51Module() != 0)
161 // Couldn't load module, quit:
163 errBox.setText("Couldn't load lirc_rx51 kernel module!");
164 errBox.setIcon(QMessageBox::Warning);
166 // throw; // Need a clean way to exit here!!!
172 MainWindow::~MainWindow()
175 if (selectKeysetForm) delete selectKeysetForm;
176 if (selectDeviceForm) delete selectDeviceForm;
177 // if (panelSelectionForm) delete panelSelectionForm;
178 if (documentationForm) delete documentationForm;
179 if (aboutForm) delete aboutForm;
184 void MainWindow::setOrientation(ScreenOrientation orientation)
186 #if defined(Q_OS_SYMBIAN)
187 // If the version of Qt on the device is < 4.7.2, that attribute won't work
188 if (orientation != ScreenOrientationAuto) {
189 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
190 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
191 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
195 #endif // Q_OS_SYMBIAN
197 Qt::WidgetAttribute attribute;
198 switch (orientation) {
199 #if QT_VERSION < 0x040702
200 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
201 case ScreenOrientationLockPortrait:
202 attribute = static_cast<Qt::WidgetAttribute>(128);
204 case ScreenOrientationLockLandscape:
205 attribute = static_cast<Qt::WidgetAttribute>(129);
208 case ScreenOrientationAuto:
209 attribute = static_cast<Qt::WidgetAttribute>(130);
211 #else // QT_VERSION < 0x040702
212 case ScreenOrientationLockPortrait:
213 attribute = Qt::WA_LockPortraitOrientation;
215 case ScreenOrientationLockLandscape:
216 attribute = Qt::WA_LockLandscapeOrientation;
219 case ScreenOrientationAuto:
220 attribute = Qt::WA_AutoOrientation;
222 #endif // QT_VERSION < 0x040702
224 setAttribute(attribute, true);
227 void MainWindow::showExpanded()
229 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
231 #elif defined(Q_WS_MAEMO_5)
239 void MainWindow::enableButtons()
241 // Just to be sure, check to see if the keyset has been populated:
242 myKeysets->populateKeyset(this, currentKeyset);
246 unsigned int dk = preferencesForm->getDefaultKeyset();
247 if (preferencesForm->defaultControlsVolume() && dk)
249 myKeysets->populateKeyset(this, dk);
250 myPanels->enableButtons(myKeysets, currentKeyset, dk);
254 myPanels->enableButtons(myKeysets, currentKeyset);
259 myPanels->enableButtons(myKeysets, currentKeyset);
264 void MainWindow::useMainPanel()
266 myPanels->useMainPanel();
270 void MainWindow::useAltMainPanel()
272 myPanels->useAltMainPanel();
276 QString MainWindow::getCurrentMake()
278 return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
282 QString MainWindow::getCurrentName()
284 return myKeysets->getDisplayName(currentKeyset);
288 QString MainWindow::getCurrentFullName()
290 return selectKeysetForm->getKeysetName();
294 void MainWindow::receivedExternalWarning(
298 errBox.setText(warning);
299 errBox.setIcon(QMessageBox::Warning);
306 void MainWindow::on_actionSelectKeyset_triggered()
308 selectKeysetForm->show();
311 void MainWindow::on_actionBrowse_Device_List_triggered()
313 selectDeviceForm->show();
316 void MainWindow::on_actionPreferences_triggered()
318 preferencesForm->show();
321 void MainWindow::on_actionAbout_triggered()
325 aboutForm = new PIRAboutForm(this);
331 void MainWindow::on_actionDocumentation_triggered()
333 if (!documentationForm)
335 documentationForm = new PIRDocumentationForm(this);
338 documentationForm->show();
344 void MainWindow::keysetSelectionChanged(
345 QListWidgetItem *item)
347 if (!item) return; // Should probably display error message here!
349 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
351 if (!kwi) return; // Also need to say something here
353 if (currentKeyset == kwi->getID())
355 // We're already on that keyset, so nothing to do:
359 // Clean up and remove the current keyset:
360 myKeysets->clearKeyset(currentKeyset);
362 currentKeyset = kwi->getID();
364 QSettings settings("pietrzak.org", "Pierogi");
368 makeManager.getMakeString(kwi->getMake()));
372 myKeysets->getDisplayName(currentKeyset));
378 void MainWindow::finalCleanup()
380 // Perform any necessary cleanup work here.
382 // Make certain that the thread stops repeating:
387 void MainWindow::addToFavorites(
388 PIRKeysetWidgetItem *kwi)
390 //Add keyset to the favorites:
391 favoritesDialog->addItem(new PIRKeysetWidgetItem(kwi));
393 // And, add the keyset id to the persistent list:
394 QSettings settings("pietrzak.org", "Pierogi");
396 int favSettingsSize = settings.beginReadArray("favorites");
399 settings.beginWriteArray("favorites");
400 settings.setArrayIndex(favSettingsSize);
404 makeManager.getMakeString(kwi->getMake()));
406 settings.setValue("keysetName", kwi->getInternalName());
412 void MainWindow::removeFromFavorites(
413 unsigned int keysetID)
415 favoritesDialog->removeItem(keysetID);
417 // Remove this item from the persistent list. Well, actually, it seems a
418 // little more convenient to just blow away the existing list of favorites
419 // and rewrite it, as modifying an existing QSettings array in the middle
420 // seems a bit hard...
421 QSettings settings("pietrzak.org", "Pierogi");
423 settings.remove("favorites");
425 int count = favoritesDialog->getCount();
427 // If the count is empty, we can stop right here:
428 if (count == 0) return;
432 PIRKeysetWidgetItem *kwi = NULL;
433 settings.beginWriteArray("favorites");
434 while (index < count)
436 kwi = favoritesDialog->getItem(index);
438 settings.setArrayIndex(index);
443 makeManager.getMakeString(myKeysets->getMake(id)));
445 settings.setValue("keysetName", myKeysets->getDisplayName(id));
454 void MainWindow::populateFavorites()
456 QSettings settings("pietrzak.org", "Pierogi");
458 int size = settings.beginReadArray("favorites");
462 PIRKeysetWidgetItem *kwi;
466 settings.setArrayIndex(index);
467 make = settings.value("keysetMake").toString();
468 name = settings.value("keysetName").toString();
470 kwi = myKeysets->makeKeysetItem(make, name);
472 // Did the item creation work?
475 // Keyset does exist, so continue:
476 favoritesDialog->addItem(kwi);
487 void MainWindow::startRepeating(
490 QMutexLocker locker(&commandIFMutex);
491 if (!commandInFlight)
493 commandInFlight = true;
494 emit buttonPressed(currentKeyset, name);
499 void MainWindow::startRepeating(
501 unsigned int keysetID)
503 QMutexLocker locker(&commandIFMutex);
504 if (!commandInFlight)
506 commandInFlight = true;
507 emit buttonPressed(keysetID, name);
512 void MainWindow::stopRepeating()
514 QMutexLocker locker(&stopRepeatingMutex);
515 stopRepeatingFlag = true;
519 void MainWindow::selectPrevFavKeyset()
521 favoritesDialog->selectPrevFavKeyset();
525 void MainWindow::selectNextFavKeyset()
527 favoritesDialog->selectNextFavKeyset();
531 void MainWindow::insertCornerButtons()
533 // Set up the dialog boxes:
534 PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
535 // favoritesDialog = new PIRFavoritesDialog(this);
537 // Next, set up the corner buttons:
538 QPushButton *button =
539 new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
541 button->setFlat(true);
548 Qt::QueuedConnection);
550 ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
553 new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
555 button->setFlat(true);
562 Qt::QueuedConnection);
564 ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
568 void MainWindow::disableUpdates()
570 ui->mainTabWidget->setUpdatesEnabled(false);
574 void MainWindow::enableUpdates()
576 ui->mainTabWidget->setUpdatesEnabled(true);
580 void MainWindow::clearTabs()
582 ui->mainTabWidget->clear();
586 void MainWindow::addTab(
590 ui->mainTabWidget->addTab(page, label);
593 void MainWindow::setupTabs(
596 myPanels->setupTabs(name);
600 bool MainWindow::selectNextKeyset()
602 return selectKeysetForm->selectNextKeyset();
606 bool MainWindow::selectPrevKeyset()
608 return selectKeysetForm->selectPrevKeyset();