1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
4 #include <QtCore/QCoreApplication>
6 #include <QtGui/QMessageBox>
10 #include "pirkeysetmetadata.h"
11 #include "pirkeysetwidgetitem.h"
12 #include "pirselectkeysetform.h"
13 #include "pirselectdeviceform.h"
14 #include "pirpanelselectionform.h"
15 #include "pirdocumentationform.h"
16 #include "piraboutform.h"
17 #include "pirkeysetmanager.h"
18 #include "pirpanelmanager.h"
22 // Some ugly globals used for thread communications:
24 // A global to show that a command is being processed:
25 bool commandInFlight = false;
26 QMutex commandIFMutex;
28 // The stopRepeatingFlag boolean is the method used to tell running commands
29 // in the worker thread to stop:
30 bool stopRepeatingFlag = false;
31 QMutex stopRepeatingMutex;
34 extern PIRMakeMgr makeManager;
37 MainWindow::MainWindow(QWidget *parent)
38 : QMainWindow(parent),
39 ui(new Ui::MainWindow),
42 panelSelectionForm(0),
49 // Make this a Maemo 5 stacked widget:
50 setAttribute(Qt::WA_Maemo5StackedWindow);
52 // Create the managers:
53 myKeysets = new PIRKeysetManager();
54 myPanels = new PIRPanelManager(this);
56 // Set up the keyset selection window:
57 selectKeysetForm = new PIRSelectKeysetForm(this);
58 myKeysets->populateSelectionWidget(selectKeysetForm);
60 // Set up the device selection window:
61 selectDeviceForm = new PIRSelectDeviceForm(this);
62 PIRKeysetMetaData::populateDevices(selectDeviceForm);
64 // Set up the panel selection window:
65 panelSelectionForm = new PIRPanelSelectionForm(this);
67 // myPanels->setupPanels(panelSelectionForm);
69 // Remember any favorites the user has already set:
72 // Retrieve the user's most recent keyset (if any):
73 QSettings settings("pietrzak.org", "Pierogi");
74 if (settings.contains("currentKeysetName"))
76 myKeysets->findKeysetID(
77 settings.value("currentKeysetMake").toString(),
78 settings.value("currentKeysetName").toString(),
80 // currentKeyset = settings.value("currentKeyset").toInt();
85 QListWidget *fkw = myPanels->getFavoritesListWidget();
89 SIGNAL(itemActivated(QListWidgetItem *)),
91 SLOT(keysetSelectionChanged(QListWidgetItem *)),
92 Qt::QueuedConnection);
94 // Make sure the three selection lists don't show different selections:
95 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
96 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
98 // favorites -> keyset name
101 SIGNAL(itemActivated(QListWidgetItem *)),
103 SLOT(clearSelection()),
104 Qt::QueuedConnection);
106 // favorites -> device name
109 SIGNAL(itemActivated(QListWidgetItem *)),
111 SLOT(clearSelection()),
112 Qt::QueuedConnection);
114 // keyset name -> favorites
117 SIGNAL(itemActivated(QListWidgetItem *)),
119 SLOT(clearSelection()),
120 Qt::QueuedConnection);
122 // device name -> favorites
125 SIGNAL(itemActivated(QListWidgetItem *)),
127 SLOT(clearSelection()),
128 Qt::QueuedConnection);
130 // keyset name -> device name
133 SIGNAL(itemActivated(QListWidgetItem *)),
135 SLOT(clearSelection()),
136 Qt::QueuedConnection);
138 // device name -> keyset name
141 SIGNAL(itemActivated(QListWidgetItem *)),
143 SLOT(clearSelection()),
144 Qt::QueuedConnection);
147 // The PIRModprobe object should take care of setting up and shutting down
148 // the lirc_rx51 kernel module, if necessary:
150 if (modprobeObj.loadRX51Module() != 0)
152 // Couldn't load module, quit:
154 errBox.setText("Couldn't load lirc_rx51 kernel module!");
155 errBox.setIcon(QMessageBox::Warning);
157 // throw; // Need a clean way to exit here!!!
163 MainWindow::~MainWindow()
166 if (selectKeysetForm) delete selectKeysetForm;
167 if (selectDeviceForm) delete selectDeviceForm;
168 if (panelSelectionForm) delete panelSelectionForm;
169 if (documentationForm) delete documentationForm;
170 if (aboutForm) delete aboutForm;
175 void MainWindow::setOrientation(ScreenOrientation orientation)
177 #if defined(Q_OS_SYMBIAN)
178 // If the version of Qt on the device is < 4.7.2, that attribute won't work
179 if (orientation != ScreenOrientationAuto) {
180 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
181 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
182 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
186 #endif // Q_OS_SYMBIAN
188 Qt::WidgetAttribute attribute;
189 switch (orientation) {
190 #if QT_VERSION < 0x040702
191 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
192 case ScreenOrientationLockPortrait:
193 attribute = static_cast<Qt::WidgetAttribute>(128);
195 case ScreenOrientationLockLandscape:
196 attribute = static_cast<Qt::WidgetAttribute>(129);
199 case ScreenOrientationAuto:
200 attribute = static_cast<Qt::WidgetAttribute>(130);
202 #else // QT_VERSION < 0x040702
203 case ScreenOrientationLockPortrait:
204 attribute = Qt::WA_LockPortraitOrientation;
206 case ScreenOrientationLockLandscape:
207 attribute = Qt::WA_LockLandscapeOrientation;
210 case ScreenOrientationAuto:
211 attribute = Qt::WA_AutoOrientation;
213 #endif // QT_VERSION < 0x040702
215 setAttribute(attribute, true);
218 void MainWindow::showExpanded()
220 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
222 #elif defined(Q_WS_MAEMO_5)
230 void MainWindow::enableButtons()
232 // Just to be sure, check to see if the keyset has been populated:
233 myKeysets->populateKeyset(this, currentKeyset);
235 myPanels->enableButtons(myKeysets, currentKeyset);
239 void MainWindow::receivedExternalWarning(
243 errBox.setText(warning);
244 errBox.setIcon(QMessageBox::Warning);
251 void MainWindow::on_actionSelectKeyset_triggered()
253 selectKeysetForm->show();
256 void MainWindow::on_actionSelect_Device_By_Name_triggered()
258 selectDeviceForm->show();
261 void MainWindow::on_actionArrange_Button_Panels_triggered()
263 panelSelectionForm->show();
266 void MainWindow::on_actionAbout_triggered()
270 aboutForm = new PIRAboutForm(this);
276 void MainWindow::on_actionDocumentation_triggered()
278 if (!documentationForm)
280 documentationForm = new PIRDocumentationForm(this);
283 documentationForm->show();
289 void MainWindow::keysetSelectionChanged(
290 QListWidgetItem *item)
292 if (!item) return; // Should probably display error message here!
294 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
296 if (!kwi) return; // Also need to say something here
298 if (currentKeyset == kwi->getID())
300 // We're already on that keyset, so nothing to do:
304 currentKeyset = kwi->getID();
306 QSettings settings("pietrzak.org", "Pierogi");
307 // settings.setValue("currentKeyset", currentKeyset);
311 makeManager.getMakeString(kwi->getMake()));
315 myKeysets->getDisplayName(currentKeyset));
321 void MainWindow::finalCleanup()
323 // Perform any necessary cleanup work here.
325 // Make certain that the thread stops repeating:
330 void MainWindow::addCurrentKeyset(
333 // Is the current keyset already a favorite?
334 int count = qlw->count();
336 PIRKeysetWidgetItem *kwi = NULL;
337 while (index < count)
339 kwi = dynamic_cast<PIRKeysetWidgetItem *>(
342 if (kwi && (kwi->getID() == currentKeyset))
344 // Current keyset already in list! No need to continue.
350 // Ok, add the current keyset to the favorites:
351 PIRMakeName make = myKeysets->getMake(currentKeyset);
353 QString name = makeManager.getMakeString(make);
355 name.append(myKeysets->getDisplayName(currentKeyset));
357 qlw->addItem(new PIRKeysetWidgetItem(name, currentKeyset, make));
359 // And, add the keyset id to the persistent list:
360 QSettings settings("pietrzak.org", "Pierogi");
362 int favSettingsSize = settings.beginReadArray("favorites");
365 settings.beginWriteArray("favorites");
366 settings.setArrayIndex(favSettingsSize);
367 // settings.setValue("keysetID", currentKeyset);
371 makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
373 settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
379 void MainWindow::removeFavoriteKeyset(
382 // Deleting an item removes it from the list, so just grab the currently
383 // selected item and delete it:
384 QListWidgetItem *item = qlw->currentItem();
386 if (item) delete item;
388 // Remove this item from the persistent list. Well, actually, it seems a
389 // little more convenient to just blow away the existing list of favorites
390 // and rewrite it, as modifying an existing QSettings array in the middle
391 // seems a bit hard...
392 QSettings settings("pietrzak.org", "Pierogi");
394 settings.remove("favorites");
396 int count = qlw->count();
398 // If the count is empty, we can stop right here:
399 if (count == 0) return;
403 PIRKeysetWidgetItem *kwi = NULL;
404 settings.beginWriteArray("favorites");
405 while (index < count)
407 kwi = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
409 settings.setArrayIndex(index);
410 // settings.setValue("keysetID", kwi->getID());
415 makeManager.getMakeString(myKeysets->getMake(id)));
417 settings.setValue("keysetName", myKeysets->getDisplayName(id));
425 void MainWindow::populateFavorites()
427 QSettings settings("pietrzak.org", "Pierogi");
429 int size = settings.beginReadArray("favorites");
433 PIRKeysetWidgetItem *kwi;
437 settings.setArrayIndex(index);
438 make = settings.value("keysetMake").toString();
439 name = settings.value("keysetName").toString();
441 kwi = myKeysets->makeKeysetItem(make, name);
443 // Did the item creation work?
446 // Keyset does exist, so continue:
447 myPanels->addFavoritesItem(kwi);
457 void MainWindow::startRepeating(
460 QMutexLocker locker(&commandIFMutex);
461 if (!commandInFlight)
463 commandInFlight = true;
464 emit buttonPressed(currentKeyset, name);
469 void MainWindow::stopRepeating()
471 QMutexLocker locker(&stopRepeatingMutex);
472 stopRepeatingFlag = true;
476 void MainWindow::selectPrevFavKeyset()
478 myPanels->selectPrevFavKeyset();
482 void MainWindow::selectNextFavKeyset()
484 myPanels->selectNextFavKeyset();
488 void MainWindow::managePanel(
492 myPanels->managePanel(name, state);
496 void MainWindow::insertPanel(
499 const QString &displayName)
501 ui->stackedButtonsWidget->insertWidget(index, panel);
502 ui->selectPanelComboBox->insertItem(index, displayName);
506 void MainWindow::removePanel(
510 ui->stackedButtonsWidget->removeWidget(panel);
511 ui->selectPanelComboBox->removeItem(index);
515 void MainWindow::on_prevPanelButton_clicked()
517 int count = ui->selectPanelComboBox->count();
519 // No need to do anything if there are less than two panels available:
520 if (count < 2) return;
522 int index = ui->selectPanelComboBox->currentIndex();
532 ui->selectPanelComboBox->setCurrentIndex(index);
536 void MainWindow::on_nextPanelButton_clicked()
538 int count = ui->selectPanelComboBox->count();
540 // No need to do anything if there are less than two panels available:
541 if (count < 2) return;
543 int index = ui->selectPanelComboBox->currentIndex();
544 if (index == count - 1)
553 ui->selectPanelComboBox->setCurrentIndex(index);
557 void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
559 ui->stackedButtonsWidget->setCurrentIndex(index);