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 selectKeysetForm->selectKeyset(currentKeyset);
91 // Add the corner buttons:
92 insertCornerButtons();
96 // Make sure the three selection lists don't show different selections:
97 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
98 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
100 // keyset name -> device name
103 SIGNAL(itemActivated(QListWidgetItem *)),
105 SLOT(clearSelection()),
106 Qt::QueuedConnection);
108 // device name -> keyset name
111 SIGNAL(itemActivated(QListWidgetItem *)),
113 SLOT(clearSelection()),
114 Qt::QueuedConnection);
117 // The PIRModprobe object should take care of setting up and shutting down
118 // the lirc_rx51 kernel module, if necessary:
120 if (modprobeObj.loadRX51Module() != 0)
122 // Couldn't load module, quit:
124 errBox.setText("Couldn't load lirc_rx51 kernel module!");
125 errBox.setIcon(QMessageBox::Warning);
127 // throw; // Need a clean way to exit here!!!
133 MainWindow::~MainWindow()
136 if (selectKeysetForm) delete selectKeysetForm;
137 if (selectDeviceForm) delete selectDeviceForm;
138 // if (panelSelectionForm) delete panelSelectionForm;
139 if (documentationForm) delete documentationForm;
140 if (aboutForm) delete aboutForm;
145 void MainWindow::setOrientation(ScreenOrientation orientation)
147 #if defined(Q_OS_SYMBIAN)
148 // If the version of Qt on the device is < 4.7.2, that attribute won't work
149 if (orientation != ScreenOrientationAuto) {
150 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
151 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
152 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
156 #endif // Q_OS_SYMBIAN
158 Qt::WidgetAttribute attribute;
159 switch (orientation) {
160 #if QT_VERSION < 0x040702
161 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
162 case ScreenOrientationLockPortrait:
163 attribute = static_cast<Qt::WidgetAttribute>(128);
165 case ScreenOrientationLockLandscape:
166 attribute = static_cast<Qt::WidgetAttribute>(129);
169 case ScreenOrientationAuto:
170 attribute = static_cast<Qt::WidgetAttribute>(130);
172 #else // QT_VERSION < 0x040702
173 case ScreenOrientationLockPortrait:
174 attribute = Qt::WA_LockPortraitOrientation;
176 case ScreenOrientationLockLandscape:
177 attribute = Qt::WA_LockLandscapeOrientation;
180 case ScreenOrientationAuto:
181 attribute = Qt::WA_AutoOrientation;
183 #endif // QT_VERSION < 0x040702
185 setAttribute(attribute, true);
188 void MainWindow::showExpanded()
190 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
192 #elif defined(Q_WS_MAEMO_5)
200 void MainWindow::enableButtons()
202 // Just to be sure, check to see if the keyset has been populated:
203 myKeysets->populateKeyset(this, currentKeyset);
207 unsigned int dk = preferencesForm->getDefaultKeyset();
208 if (preferencesForm->defaultControlsVolume() && dk)
210 myKeysets->populateKeyset(this, dk);
211 myPanels->enableButtons(myKeysets, currentKeyset, dk);
215 myPanels->enableButtons(myKeysets, currentKeyset);
220 myPanels->enableButtons(myKeysets, currentKeyset);
225 void MainWindow::useMainPanel()
227 myPanels->useMainPanel();
231 void MainWindow::useAltMainPanel()
233 myPanels->useAltMainPanel();
237 QString MainWindow::getCurrentMake()
239 return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
243 QString MainWindow::getCurrentName()
245 return myKeysets->getDisplayName(currentKeyset);
249 QString MainWindow::getCurrentFullName()
251 return selectKeysetForm->getKeysetName();
255 void MainWindow::receivedExternalWarning(
259 errBox.setText(warning);
260 errBox.setIcon(QMessageBox::Warning);
267 void MainWindow::on_actionSelectKeyset_triggered()
269 selectKeysetForm->show();
272 void MainWindow::on_actionBrowse_Device_List_triggered()
274 selectDeviceForm->show();
277 void MainWindow::on_actionPreferences_triggered()
279 preferencesForm->show();
282 void MainWindow::on_actionAbout_triggered()
286 aboutForm = new PIRAboutForm(this);
292 void MainWindow::on_actionDocumentation_triggered()
294 if (!documentationForm)
296 documentationForm = new PIRDocumentationForm(this);
299 documentationForm->show();
305 void MainWindow::keysetSelectionChanged(
306 QListWidgetItem *item)
308 if (!item) return; // Should probably display error message here!
310 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
312 if (!kwi) return; // Also need to say something here
314 if (currentKeyset == kwi->getID())
316 // We're already on that keyset, so nothing to do:
320 // Clean up and remove the current keyset:
321 myKeysets->clearKeyset(currentKeyset);
323 currentKeyset = kwi->getID();
325 QSettings settings("pietrzak.org", "Pierogi");
329 makeManager.getMakeString(kwi->getMake()));
333 myKeysets->getDisplayName(currentKeyset));
339 void MainWindow::finalCleanup()
341 // Perform any necessary cleanup work here.
343 // Make certain that the thread stops repeating:
348 void MainWindow::addToFavorites(
349 PIRKeysetWidgetItem *kwi)
351 //Add keyset to the favorites:
352 favoritesDialog->addItem(new PIRKeysetWidgetItem(kwi));
354 // And, add the keyset id to the persistent list:
355 QSettings settings("pietrzak.org", "Pierogi");
357 int favSettingsSize = settings.beginReadArray("favorites");
360 settings.beginWriteArray("favorites");
361 settings.setArrayIndex(favSettingsSize);
365 makeManager.getMakeString(kwi->getMake()));
367 settings.setValue("keysetName", kwi->getInternalName());
373 void MainWindow::removeFromFavorites(
374 unsigned int keysetID)
376 favoritesDialog->removeItem(keysetID);
378 // Remove this item from the persistent list. Well, actually, it seems a
379 // little more convenient to just blow away the existing list of favorites
380 // and rewrite it, as modifying an existing QSettings array in the middle
381 // seems a bit hard...
382 QSettings settings("pietrzak.org", "Pierogi");
384 settings.remove("favorites");
386 int count = favoritesDialog->getCount();
388 // If the count is empty, we can stop right here:
389 if (count == 0) return;
393 PIRKeysetWidgetItem *kwi = NULL;
394 settings.beginWriteArray("favorites");
395 while (index < count)
397 kwi = favoritesDialog->getItem(index);
399 settings.setArrayIndex(index);
404 makeManager.getMakeString(myKeysets->getMake(id)));
406 settings.setValue("keysetName", myKeysets->getDisplayName(id));
415 void MainWindow::populateFavorites()
417 QSettings settings("pietrzak.org", "Pierogi");
419 int size = settings.beginReadArray("favorites");
423 PIRKeysetWidgetItem *kwi;
427 settings.setArrayIndex(index);
428 make = settings.value("keysetMake").toString();
429 name = settings.value("keysetName").toString();
431 kwi = myKeysets->makeKeysetItem(make, name);
433 // Did the item creation work?
436 // Keyset does exist, so continue:
437 favoritesDialog->addItem(kwi);
448 bool MainWindow::startRepeating(
451 QMutexLocker locker(&commandIFMutex);
452 if (!commandInFlight)
454 commandInFlight = true;
455 emit buttonPressed(currentKeyset, name);
465 bool MainWindow::startRepeating(
467 unsigned int keysetID)
469 QMutexLocker locker(&commandIFMutex);
470 if (!commandInFlight)
472 commandInFlight = true;
473 emit buttonPressed(keysetID, name);
483 void MainWindow::stopRepeating()
485 QMutexLocker locker(&stopRepeatingMutex);
486 stopRepeatingFlag = true;
490 void MainWindow::selectPrevFavKeyset()
492 favoritesDialog->selectPrevFavKeyset();
496 void MainWindow::selectNextFavKeyset()
498 favoritesDialog->selectNextFavKeyset();
502 void MainWindow::insertCornerButtons()
504 // Set up the dialog boxes:
505 PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
506 // favoritesDialog = new PIRFavoritesDialog(this);
508 // Next, set up the corner buttons:
509 QPushButton *button =
510 new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
512 button->setFlat(true);
519 Qt::QueuedConnection);
521 ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
524 new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
526 button->setFlat(true);
533 Qt::QueuedConnection);
535 ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
539 void MainWindow::disableUpdates()
541 ui->mainTabWidget->setUpdatesEnabled(false);
545 void MainWindow::enableUpdates()
547 ui->mainTabWidget->setUpdatesEnabled(true);
551 void MainWindow::clearTabs()
553 ui->mainTabWidget->clear();
557 void MainWindow::addTab(
561 ui->mainTabWidget->addTab(page, label);
564 void MainWindow::setupTabs(
567 myPanels->setupTabs(name);
571 bool MainWindow::selectNextKeyset()
573 return selectKeysetForm->selectNextKeyset();
577 bool MainWindow::selectPrevKeyset()
579 return selectKeysetForm->selectPrevKeyset();
583 bool MainWindow::selectFirstKeyset()
585 return selectKeysetForm->selectFirstKeyset();
589 void MainWindow::openCurrentKeysetDialog()
591 selectKeysetForm->openCurrentKeysetDialog();
595 void MainWindow::updateKeysetSelection(
596 unsigned int targetID)
598 selectKeysetForm->selectKeyset(targetID);