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"
23 #include "pirkeysetmanager.h"
24 #include "pirpanelmanager.h"
29 // Some ugly globals used for thread communications:
31 // A global to show that a command is being processed:
32 bool commandInFlight = false;
33 QMutex commandIFMutex;
35 // The stopRepeatingFlag boolean is the method used to tell running commands
36 // in the worker thread to stop:
37 bool stopRepeatingFlag = false;
38 QMutex stopRepeatingMutex;
41 extern PIRMakeMgr makeManager;
44 MainWindow::MainWindow(QWidget *parent)
45 : QMainWindow(parent),
46 ui(new Ui::MainWindow),
54 currentKeyset(1) // Zero is not a valid keyset any more
58 // Make this a Maemo 5 stacked widget:
59 setAttribute(Qt::WA_Maemo5StackedWindow);
61 // Create the managers:
62 myKeysets = new PIRKeysetManager();
63 myPanels = new PIRPanelManager(this);
65 // Display the panels:
66 myPanels->updateTabSet();
68 // Add the corner button:
71 // Construct the rest of the forms:
72 selectKeysetForm = new PIRSelectKeysetForm(this);
73 myKeysets->populateSelectionWidget(selectKeysetForm);
75 selectDeviceForm = new PIRSelectDeviceForm(this);
76 PIRKeysetMetaData::populateDevices(selectDeviceForm);
78 preferencesForm = new PIRPreferencesForm(this, myKeysets);
80 // Remember any favorites the user has already set:
83 // Retrieve the user's preferences:
84 QSettings settings("pietrzak.org", "Pierogi");
85 if (settings.contains("currentKeysetName"))
87 myKeysets->findKeysetID(
88 settings.value("currentKeysetMake").toString(),
89 settings.value("currentKeysetName").toString(),
95 QListWidget *fkw = myPanels->getFavoritesListWidget();
99 SIGNAL(itemActivated(QListWidgetItem *)),
101 SLOT(keysetSelectionChanged(QListWidgetItem *)),
102 Qt::QueuedConnection);
104 // Make sure the three selection lists don't show different selections:
105 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
106 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
108 // favorites -> keyset name
111 SIGNAL(itemActivated(QListWidgetItem *)),
113 SLOT(clearSelection()),
114 Qt::QueuedConnection);
116 // favorites -> device name
119 SIGNAL(itemActivated(QListWidgetItem *)),
121 SLOT(clearSelection()),
122 Qt::QueuedConnection);
124 // keyset name -> favorites
127 SIGNAL(itemActivated(QListWidgetItem *)),
129 SLOT(clearSelection()),
130 Qt::QueuedConnection);
132 // device name -> favorites
135 SIGNAL(itemActivated(QListWidgetItem *)),
137 SLOT(clearSelection()),
138 Qt::QueuedConnection);
140 // keyset name -> device name
143 SIGNAL(itemActivated(QListWidgetItem *)),
145 SLOT(clearSelection()),
146 Qt::QueuedConnection);
148 // device name -> keyset name
151 SIGNAL(itemActivated(QListWidgetItem *)),
153 SLOT(clearSelection()),
154 Qt::QueuedConnection);
157 // The PIRModprobe object should take care of setting up and shutting down
158 // the lirc_rx51 kernel module, if necessary:
160 if (modprobeObj.loadRX51Module() != 0)
162 // Couldn't load module, quit:
164 errBox.setText("Couldn't load lirc_rx51 kernel module!");
165 errBox.setIcon(QMessageBox::Warning);
167 // throw; // Need a clean way to exit here!!!
173 MainWindow::~MainWindow()
176 if (selectKeysetForm) delete selectKeysetForm;
177 if (selectDeviceForm) delete selectDeviceForm;
178 // if (panelSelectionForm) delete panelSelectionForm;
179 if (documentationForm) delete documentationForm;
180 if (aboutForm) delete aboutForm;
185 void MainWindow::setOrientation(ScreenOrientation orientation)
187 #if defined(Q_OS_SYMBIAN)
188 // If the version of Qt on the device is < 4.7.2, that attribute won't work
189 if (orientation != ScreenOrientationAuto) {
190 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
191 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
192 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
196 #endif // Q_OS_SYMBIAN
198 Qt::WidgetAttribute attribute;
199 switch (orientation) {
200 #if QT_VERSION < 0x040702
201 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
202 case ScreenOrientationLockPortrait:
203 attribute = static_cast<Qt::WidgetAttribute>(128);
205 case ScreenOrientationLockLandscape:
206 attribute = static_cast<Qt::WidgetAttribute>(129);
209 case ScreenOrientationAuto:
210 attribute = static_cast<Qt::WidgetAttribute>(130);
212 #else // QT_VERSION < 0x040702
213 case ScreenOrientationLockPortrait:
214 attribute = Qt::WA_LockPortraitOrientation;
216 case ScreenOrientationLockLandscape:
217 attribute = Qt::WA_LockLandscapeOrientation;
220 case ScreenOrientationAuto:
221 attribute = Qt::WA_AutoOrientation;
223 #endif // QT_VERSION < 0x040702
225 setAttribute(attribute, true);
228 void MainWindow::showExpanded()
230 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
232 #elif defined(Q_WS_MAEMO_5)
240 void MainWindow::enableButtons()
242 // Just to be sure, check to see if the keyset has been populated:
243 myKeysets->populateKeyset(this, currentKeyset);
247 unsigned int dk = preferencesForm->getDefaultKeyset();
248 if (preferencesForm->defaultControlsVolume() && dk)
250 myKeysets->populateKeyset(this, dk);
251 myPanels->enableButtons(myKeysets, currentKeyset, dk);
255 myPanels->enableButtons(myKeysets, currentKeyset);
260 myPanels->enableButtons(myKeysets, currentKeyset);
265 void MainWindow::useMainPanel()
267 myPanels->useMainPanel();
271 void MainWindow::useAltMainPanel()
273 myPanels->useAltMainPanel();
277 QString MainWindow::getCurrentMake()
279 return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
283 QString MainWindow::getCurrentName()
285 return myKeysets->getDisplayName(currentKeyset);
289 void MainWindow::receivedExternalWarning(
293 errBox.setText(warning);
294 errBox.setIcon(QMessageBox::Warning);
301 void MainWindow::on_actionSelectKeyset_triggered()
303 selectKeysetForm->show();
306 void MainWindow::on_actionSelect_Device_By_Name_triggered()
308 selectDeviceForm->show();
311 void MainWindow::on_actionPreferences_triggered()
313 preferencesForm->show();
316 void MainWindow::on_actionAbout_triggered()
320 aboutForm = new PIRAboutForm(this);
326 void MainWindow::on_actionDocumentation_triggered()
328 if (!documentationForm)
330 documentationForm = new PIRDocumentationForm(this);
333 documentationForm->show();
339 void MainWindow::keysetSelectionChanged(
340 QListWidgetItem *item)
342 if (!item) return; // Should probably display error message here!
344 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
346 if (!kwi) return; // Also need to say something here
348 if (currentKeyset == kwi->getID())
350 // We're already on that keyset, so nothing to do:
354 currentKeyset = kwi->getID();
356 QSettings settings("pietrzak.org", "Pierogi");
360 makeManager.getMakeString(kwi->getMake()));
364 myKeysets->getDisplayName(currentKeyset));
370 void MainWindow::finalCleanup()
372 // Perform any necessary cleanup work here.
374 // Make certain that the thread stops repeating:
379 void MainWindow::addCurrentKeyset(
382 // Is the current keyset already a favorite?
383 int count = qlw->count();
385 PIRKeysetWidgetItem *kwi = NULL;
386 while (index < count)
388 kwi = dynamic_cast<PIRKeysetWidgetItem *>(
391 if (kwi && (kwi->getID() == currentKeyset))
393 // Current keyset already in list! No need to continue.
399 // Ok, add the current keyset to the favorites:
400 PIRMakeName make = myKeysets->getMake(currentKeyset);
402 QString name = makeManager.getMakeString(make);
404 name.append(myKeysets->getDisplayName(currentKeyset));
406 qlw->addItem(new PIRKeysetWidgetItem(name, currentKeyset, make));
408 // And, add the keyset id to the persistent list:
409 QSettings settings("pietrzak.org", "Pierogi");
411 int favSettingsSize = settings.beginReadArray("favorites");
414 settings.beginWriteArray("favorites");
415 settings.setArrayIndex(favSettingsSize);
419 makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
421 settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
427 void MainWindow::removeFavoriteKeyset(
430 // Deleting an item removes it from the list, so just grab the currently
431 // selected item and delete it:
432 QListWidgetItem *item = qlw->currentItem();
434 if (item) delete item;
436 // Remove this item from the persistent list. Well, actually, it seems a
437 // little more convenient to just blow away the existing list of favorites
438 // and rewrite it, as modifying an existing QSettings array in the middle
439 // seems a bit hard...
440 QSettings settings("pietrzak.org", "Pierogi");
442 settings.remove("favorites");
444 int count = qlw->count();
446 // If the count is empty, we can stop right here:
447 if (count == 0) return;
451 PIRKeysetWidgetItem *kwi = NULL;
452 settings.beginWriteArray("favorites");
453 while (index < count)
455 kwi = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
457 settings.setArrayIndex(index);
462 makeManager.getMakeString(myKeysets->getMake(id)));
464 settings.setValue("keysetName", myKeysets->getDisplayName(id));
472 void MainWindow::populateFavorites()
474 QSettings settings("pietrzak.org", "Pierogi");
476 int size = settings.beginReadArray("favorites");
480 PIRKeysetWidgetItem *kwi;
484 settings.setArrayIndex(index);
485 make = settings.value("keysetMake").toString();
486 name = settings.value("keysetName").toString();
488 kwi = myKeysets->makeKeysetItem(make, name);
490 // Did the item creation work?
493 // Keyset does exist, so continue:
494 myPanels->addFavoritesItem(kwi);
504 void MainWindow::startRepeating(
507 QMutexLocker locker(&commandIFMutex);
508 if (!commandInFlight)
510 commandInFlight = true;
511 emit buttonPressed(currentKeyset, name);
516 void MainWindow::startRepeating(
518 unsigned int keysetID)
520 QMutexLocker locker(&commandIFMutex);
521 if (!commandInFlight)
523 commandInFlight = true;
524 emit buttonPressed(keysetID, name);
529 void MainWindow::stopRepeating()
531 QMutexLocker locker(&stopRepeatingMutex);
532 stopRepeatingFlag = true;
536 void MainWindow::selectPrevFavKeyset()
538 myPanels->selectPrevFavKeyset();
542 void MainWindow::selectNextFavKeyset()
544 myPanels->selectNextFavKeyset();
548 void MainWindow::insertCornerButton()
550 // Set up the dialog box:
551 PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
553 // Next, set up the corner button itself:
554 QPushButton *button =
555 new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
557 button->setFlat(true);
564 Qt::QueuedConnection);
566 ui->mainTabWidget->setCornerWidget(button);
570 void MainWindow::disableUpdates()
572 ui->mainTabWidget->setUpdatesEnabled(false);
576 void MainWindow::enableUpdates()
578 ui->mainTabWidget->setUpdatesEnabled(true);
582 void MainWindow::clearTabs()
584 ui->mainTabWidget->clear();
588 void MainWindow::addTab(
592 ui->mainTabWidget->addTab(page, label);
595 void MainWindow::setupTabs(
598 myPanels->setupTabs(name);