1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
4 #include <QtCore/QCoreApplication>
6 #include <QtGui/QMessageBox>
10 #include "pirkeysetwidgetitem.h"
11 #include "pirselectkeysetform.h"
12 #include "pirselectdeviceform.h"
13 #include "pirpanelselectionform.h"
14 #include "pirdocumentationform.h"
15 #include "piraboutform.h"
16 #include "pirkeysetmanager.h"
17 #include "pirpanelmanager.h"
21 // Some ugly globals used for thread communications:
23 // A global to show that a command is being processed:
24 bool commandInFlight = false;
25 QMutex commandIFMutex;
27 // The stopRepeatingFlag boolean is the method used to tell running commands
28 // in the worker thread to stop:
29 bool stopRepeatingFlag = false;
30 QMutex stopRepeatingMutex;
33 extern PIRMakeMgr makeManager;
36 MainWindow::MainWindow(QWidget *parent)
37 : QMainWindow(parent),
38 ui(new Ui::MainWindow),
41 panelSelectionForm(0),
48 // Make this a Maemo 5 stacked widget:
49 setAttribute(Qt::WA_Maemo5StackedWindow);
51 // Create the managers:
52 myKeysets = new PIRKeysetManager();
53 myPanels = new PIRPanelManager(this);
55 // Set up the keyset selection window:
56 selectKeysetForm = new PIRSelectKeysetForm(this);
58 // Set up the device selection window:
59 selectDeviceForm = new PIRSelectDeviceForm(this);
61 // Set up the panel selection window:
62 panelSelectionForm = new PIRPanelSelectionForm(this);
64 myKeysets->populateSelectionWidgets(selectKeysetForm, selectDeviceForm);
65 // myPanels->setupPanels(panelSelectionForm);
67 // Remember any favorites the user has already set:
70 // Retrieve the user's most recent keyset (if any):
71 QSettings settings("pietrzak.org", "Pierogi");
72 if (settings.contains("currentKeysetName"))
74 myKeysets->findKeysetID(
75 settings.value("currentKeysetMake").toString(),
76 settings.value("currentKeysetName").toString(),
78 // currentKeyset = settings.value("currentKeyset").toInt();
83 QListWidget *fkw = myPanels->getFavoritesListWidget();
87 SIGNAL(itemActivated(QListWidgetItem *)),
89 SLOT(keysetSelectionChanged(QListWidgetItem *)),
90 Qt::QueuedConnection);
92 // Make sure the three selection lists don't show different selections:
93 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
94 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
96 // favorites -> keyset name
99 SIGNAL(itemActivated(QListWidgetItem *)),
101 SLOT(clearSelection()),
102 Qt::QueuedConnection);
104 // favorites -> device name
107 SIGNAL(itemActivated(QListWidgetItem *)),
109 SLOT(clearSelection()),
110 Qt::QueuedConnection);
112 // keyset name -> favorites
115 SIGNAL(itemActivated(QListWidgetItem *)),
117 SLOT(clearSelection()),
118 Qt::QueuedConnection);
120 // device name -> favorites
123 SIGNAL(itemActivated(QListWidgetItem *)),
125 SLOT(clearSelection()),
126 Qt::QueuedConnection);
128 // keyset name -> device name
131 SIGNAL(itemActivated(QListWidgetItem *)),
133 SLOT(clearSelection()),
134 Qt::QueuedConnection);
136 // device name -> keyset name
139 SIGNAL(itemActivated(QListWidgetItem *)),
141 SLOT(clearSelection()),
142 Qt::QueuedConnection);
145 // The PIRModprobe object should take care of setting up and shutting down
146 // the lirc_rx51 kernel module, if necessary:
148 if (modprobeObj.loadRX51Module() != 0)
150 // Couldn't load module, quit:
152 errBox.setText("Couldn't load lirc_rx51 kernel module!");
153 errBox.setIcon(QMessageBox::Warning);
155 // throw; // Need a clean way to exit here!!!
161 MainWindow::~MainWindow()
164 if (selectKeysetForm) delete selectKeysetForm;
165 if (selectDeviceForm) delete selectDeviceForm;
166 if (panelSelectionForm) delete panelSelectionForm;
167 if (documentationForm) delete documentationForm;
168 if (aboutForm) delete aboutForm;
173 void MainWindow::setOrientation(ScreenOrientation orientation)
175 #if defined(Q_OS_SYMBIAN)
176 // If the version of Qt on the device is < 4.7.2, that attribute won't work
177 if (orientation != ScreenOrientationAuto) {
178 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
179 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
180 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
184 #endif // Q_OS_SYMBIAN
186 Qt::WidgetAttribute attribute;
187 switch (orientation) {
188 #if QT_VERSION < 0x040702
189 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
190 case ScreenOrientationLockPortrait:
191 attribute = static_cast<Qt::WidgetAttribute>(128);
193 case ScreenOrientationLockLandscape:
194 attribute = static_cast<Qt::WidgetAttribute>(129);
197 case ScreenOrientationAuto:
198 attribute = static_cast<Qt::WidgetAttribute>(130);
200 #else // QT_VERSION < 0x040702
201 case ScreenOrientationLockPortrait:
202 attribute = Qt::WA_LockPortraitOrientation;
204 case ScreenOrientationLockLandscape:
205 attribute = Qt::WA_LockLandscapeOrientation;
208 case ScreenOrientationAuto:
209 attribute = Qt::WA_AutoOrientation;
211 #endif // QT_VERSION < 0x040702
213 setAttribute(attribute, true);
216 void MainWindow::showExpanded()
218 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
220 #elif defined(Q_WS_MAEMO_5)
228 void MainWindow::enableButtons()
230 // Just to be sure, check to see if the keyset has been populated:
231 myKeysets->populateKeyset(this, currentKeyset);
233 myPanels->enableButtons(myKeysets, currentKeyset);
237 void MainWindow::receivedExternalWarning(
241 errBox.setText(warning);
242 errBox.setIcon(QMessageBox::Warning);
249 void MainWindow::on_actionSelectKeyset_triggered()
251 selectKeysetForm->show();
254 void MainWindow::on_actionSelect_Device_By_Name_triggered()
256 selectDeviceForm->show();
259 void MainWindow::on_actionArrange_Button_Panels_triggered()
261 panelSelectionForm->show();
264 void MainWindow::on_actionAbout_triggered()
268 aboutForm = new PIRAboutForm(this);
274 void MainWindow::on_actionDocumentation_triggered()
276 if (!documentationForm)
278 documentationForm = new PIRDocumentationForm(this);
281 documentationForm->show();
287 void MainWindow::keysetSelectionChanged(
288 QListWidgetItem *item)
290 if (!item) return; // Should probably display error message here!
292 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
294 if (!kwi) return; // Also need to say something here
296 if (currentKeyset == kwi->getID())
298 // We're already on that keyset, so nothing to do:
302 currentKeyset = kwi->getID();
304 QSettings settings("pietrzak.org", "Pierogi");
305 // settings.setValue("currentKeyset", currentKeyset);
309 makeManager.getMakeString(kwi->getMake()));
313 myKeysets->getDisplayName(currentKeyset));
319 void MainWindow::finalCleanup()
321 // Perform any necessary cleanup work here.
323 // Make certain that the thread stops repeating:
328 void MainWindow::addCurrentKeyset(
331 // Is the current keyset already a favorite?
332 int count = qlw->count();
334 PIRKeysetWidgetItem *kwi = NULL;
335 while (index < count)
337 kwi = dynamic_cast<PIRKeysetWidgetItem *>(
340 if (kwi && (kwi->getID() == currentKeyset))
342 // Current keyset already in list! No need to continue.
348 // Ok, add the current keyset to the favorites:
349 PIRMakeName make = myKeysets->getMake(currentKeyset);
351 QString name = makeManager.getMakeString(make);
353 name.append(myKeysets->getDisplayName(currentKeyset));
355 qlw->addItem(new PIRKeysetWidgetItem(name, currentKeyset, make));
357 // And, add the keyset id to the persistent list:
358 QSettings settings("pietrzak.org", "Pierogi");
360 int favSettingsSize = settings.beginReadArray("favorites");
363 settings.beginWriteArray("favorites");
364 settings.setArrayIndex(favSettingsSize);
365 // settings.setValue("keysetID", currentKeyset);
369 makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
371 settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
377 void MainWindow::removeFavoriteKeyset(
380 // Deleting an item removes it from the list, so just grab the currently
381 // selected item and delete it:
382 QListWidgetItem *item = qlw->currentItem();
384 if (item) delete item;
386 // Remove this item from the persistent list. Well, actually, it seems a
387 // little more convenient to just blow away the existing list of favorites
388 // and rewrite it, as modifying an existing QSettings array in the middle
389 // seems a bit hard...
390 QSettings settings("pietrzak.org", "Pierogi");
392 settings.remove("favorites");
394 int count = qlw->count();
396 // If the count is empty, we can stop right here:
397 if (count == 0) return;
401 PIRKeysetWidgetItem *kwi = NULL;
402 settings.beginWriteArray("favorites");
403 while (index < count)
405 kwi = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
407 settings.setArrayIndex(index);
408 // settings.setValue("keysetID", kwi->getID());
413 makeManager.getMakeString(myKeysets->getMake(id)));
415 settings.setValue("keysetName", myKeysets->getDisplayName(id));
423 void MainWindow::populateFavorites()
425 QSettings settings("pietrzak.org", "Pierogi");
427 int size = settings.beginReadArray("favorites");
431 PIRKeysetWidgetItem *kwi;
435 settings.setArrayIndex(index);
436 make = settings.value("keysetMake").toString();
437 name = settings.value("keysetName").toString();
439 kwi = myKeysets->makeKeysetItem(make, name);
441 // Did the item creation work?
444 // Keyset does exist, so continue:
445 myPanels->addFavoritesItem(kwi);
455 void MainWindow::startRepeating(
458 QMutexLocker locker(&commandIFMutex);
459 if (!commandInFlight)
461 commandInFlight = true;
462 emit buttonPressed(currentKeyset, name);
467 void MainWindow::stopRepeating()
469 QMutexLocker locker(&stopRepeatingMutex);
470 stopRepeatingFlag = true;
474 void MainWindow::selectPrevFavKeyset()
476 myPanels->selectPrevFavKeyset();
480 void MainWindow::selectNextFavKeyset()
482 myPanels->selectNextFavKeyset();
486 void MainWindow::managePanel(
490 myPanels->managePanel(name, state);
494 void MainWindow::insertPanel(
497 const QString &displayName)
499 ui->stackedButtonsWidget->insertWidget(index, panel);
500 ui->selectPanelComboBox->insertItem(index, displayName);
504 void MainWindow::removePanel(
508 ui->stackedButtonsWidget->removeWidget(panel);
509 ui->selectPanelComboBox->removeItem(index);
513 void MainWindow::on_prevPanelButton_clicked()
515 int count = ui->selectPanelComboBox->count();
517 // No need to do anything if there are less than two panels available:
518 if (count < 2) return;
520 int index = ui->selectPanelComboBox->currentIndex();
530 ui->selectPanelComboBox->setCurrentIndex(index);
534 void MainWindow::on_nextPanelButton_clicked()
536 int count = ui->selectPanelComboBox->count();
538 // No need to do anything if there are less than two panels available:
539 if (count < 2) return;
541 int index = ui->selectPanelComboBox->currentIndex();
542 if (index == count - 1)
551 ui->selectPanelComboBox->setCurrentIndex(index);
555 void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
557 ui->stackedButtonsWidget->setCurrentIndex(index);