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 void MainWindow::receivedExternalWarning(
292 errBox.setText(warning);
293 errBox.setIcon(QMessageBox::Warning);
300 void MainWindow::on_actionSelectKeyset_triggered()
302 selectKeysetForm->show();
305 void MainWindow::on_actionSelect_Device_By_Name_triggered()
307 selectDeviceForm->show();
310 void MainWindow::on_actionPreferences_triggered()
312 preferencesForm->show();
315 void MainWindow::on_actionAbout_triggered()
319 aboutForm = new PIRAboutForm(this);
325 void MainWindow::on_actionDocumentation_triggered()
327 if (!documentationForm)
329 documentationForm = new PIRDocumentationForm(this);
332 documentationForm->show();
338 void MainWindow::keysetSelectionChanged(
339 QListWidgetItem *item)
341 if (!item) return; // Should probably display error message here!
343 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
345 if (!kwi) return; // Also need to say something here
347 if (currentKeyset == kwi->getID())
349 // We're already on that keyset, so nothing to do:
353 currentKeyset = kwi->getID();
355 QSettings settings("pietrzak.org", "Pierogi");
359 makeManager.getMakeString(kwi->getMake()));
363 myKeysets->getDisplayName(currentKeyset));
369 void MainWindow::finalCleanup()
371 // Perform any necessary cleanup work here.
373 // Make certain that the thread stops repeating:
378 void MainWindow::addToFavorites(
379 PIRKeysetWidgetItem *kwi)
381 //Add keyset to the favorites:
382 favoritesDialog->addItem(new PIRKeysetWidgetItem(kwi));
384 // And, add the keyset id to the persistent list:
385 QSettings settings("pietrzak.org", "Pierogi");
387 int favSettingsSize = settings.beginReadArray("favorites");
390 settings.beginWriteArray("favorites");
391 settings.setArrayIndex(favSettingsSize);
395 makeManager.getMakeString(kwi->getMake()));
397 settings.setValue("keysetName", kwi->getInternalName());
403 void MainWindow::removeFromFavorites(
404 unsigned int keysetID)
406 favoritesDialog->removeItem(keysetID);
408 // Remove this item from the persistent list. Well, actually, it seems a
409 // little more convenient to just blow away the existing list of favorites
410 // and rewrite it, as modifying an existing QSettings array in the middle
411 // seems a bit hard...
412 QSettings settings("pietrzak.org", "Pierogi");
414 settings.remove("favorites");
416 int count = favoritesDialog->getCount();
418 // If the count is empty, we can stop right here:
419 if (count == 0) return;
423 PIRKeysetWidgetItem *kwi = NULL;
424 settings.beginWriteArray("favorites");
425 while (index < count)
427 kwi = favoritesDialog->getItem(index);
429 settings.setArrayIndex(index);
434 makeManager.getMakeString(myKeysets->getMake(id)));
436 settings.setValue("keysetName", myKeysets->getDisplayName(id));
445 void MainWindow::populateFavorites()
447 QSettings settings("pietrzak.org", "Pierogi");
449 int size = settings.beginReadArray("favorites");
453 PIRKeysetWidgetItem *kwi;
457 settings.setArrayIndex(index);
458 make = settings.value("keysetMake").toString();
459 name = settings.value("keysetName").toString();
461 kwi = myKeysets->makeKeysetItem(make, name);
463 // Did the item creation work?
466 // Keyset does exist, so continue:
467 favoritesDialog->addItem(kwi);
478 void MainWindow::startRepeating(
481 QMutexLocker locker(&commandIFMutex);
482 if (!commandInFlight)
484 commandInFlight = true;
485 emit buttonPressed(currentKeyset, name);
490 void MainWindow::startRepeating(
492 unsigned int keysetID)
494 QMutexLocker locker(&commandIFMutex);
495 if (!commandInFlight)
497 commandInFlight = true;
498 emit buttonPressed(keysetID, name);
503 void MainWindow::stopRepeating()
505 QMutexLocker locker(&stopRepeatingMutex);
506 stopRepeatingFlag = true;
510 void MainWindow::selectPrevFavKeyset()
512 favoritesDialog->selectPrevFavKeyset();
516 void MainWindow::selectNextFavKeyset()
518 favoritesDialog->selectNextFavKeyset();
522 void MainWindow::insertCornerButtons()
524 // Set up the dialog boxes:
525 PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
526 // favoritesDialog = new PIRFavoritesDialog(this);
528 // Next, set up the corner buttons:
529 QPushButton *button =
530 new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
532 button->setFlat(true);
539 Qt::QueuedConnection);
541 ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
544 new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
546 button->setFlat(true);
553 Qt::QueuedConnection);
555 ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
559 void MainWindow::disableUpdates()
561 ui->mainTabWidget->setUpdatesEnabled(false);
565 void MainWindow::enableUpdates()
567 ui->mainTabWidget->setUpdatesEnabled(true);
571 void MainWindow::clearTabs()
573 ui->mainTabWidget->clear();
577 void MainWindow::addTab(
581 ui->mainTabWidget->addTab(page, label);
584 void MainWindow::setupTabs(
587 myPanels->setupTabs(name);