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 "pirpreferencesform.h"
16 #include "pirdocumentationform.h"
17 #include "piraboutform.h"
18 #include "pirkeysetmanager.h"
19 #include "pirpanelmanager.h"
23 // Some ugly globals used for thread communications:
25 // A global to show that a command is being processed:
26 bool commandInFlight = false;
27 QMutex commandIFMutex;
29 // The stopRepeatingFlag boolean is the method used to tell running commands
30 // in the worker thread to stop:
31 bool stopRepeatingFlag = false;
32 QMutex stopRepeatingMutex;
35 extern PIRMakeMgr makeManager;
38 MainWindow::MainWindow(QWidget *parent)
39 : QMainWindow(parent),
40 ui(new Ui::MainWindow),
43 panelSelectionForm(0),
51 // Make this a Maemo 5 stacked widget:
52 setAttribute(Qt::WA_Maemo5StackedWindow);
54 // Create the managers:
55 myKeysets = new PIRKeysetManager();
56 myPanels = new PIRPanelManager(this);
58 // Set up the keyset selection window:
59 selectKeysetForm = new PIRSelectKeysetForm(this);
60 myKeysets->populateSelectionWidget(selectKeysetForm);
62 // Set up the device selection window:
63 selectDeviceForm = new PIRSelectDeviceForm(this);
64 PIRKeysetMetaData::populateDevices(selectDeviceForm);
66 // Set up the panel selection window:
67 panelSelectionForm = new PIRPanelSelectionForm(this);
68 myPanels->setupPanels(panelSelectionForm);
70 // Set up the preferences window:
71 preferencesForm = new PIRPreferencesForm(this);
73 // Remember any favorites the user has already set:
76 // Retrieve the user's preferences:
77 QSettings settings("pietrzak.org", "Pierogi");
78 if (settings.contains("currentKeysetName"))
80 myKeysets->findKeysetID(
81 settings.value("currentKeysetMake").toString(),
82 settings.value("currentKeysetName").toString(),
88 QListWidget *fkw = myPanels->getFavoritesListWidget();
92 SIGNAL(itemActivated(QListWidgetItem *)),
94 SLOT(keysetSelectionChanged(QListWidgetItem *)),
95 Qt::QueuedConnection);
97 // Make sure the three selection lists don't show different selections:
98 QListWidget *klw = selectKeysetForm->getKeysetListWidget();
99 QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
101 // favorites -> keyset name
104 SIGNAL(itemActivated(QListWidgetItem *)),
106 SLOT(clearSelection()),
107 Qt::QueuedConnection);
109 // favorites -> device name
112 SIGNAL(itemActivated(QListWidgetItem *)),
114 SLOT(clearSelection()),
115 Qt::QueuedConnection);
117 // keyset name -> favorites
120 SIGNAL(itemActivated(QListWidgetItem *)),
122 SLOT(clearSelection()),
123 Qt::QueuedConnection);
125 // device name -> favorites
128 SIGNAL(itemActivated(QListWidgetItem *)),
130 SLOT(clearSelection()),
131 Qt::QueuedConnection);
133 // keyset name -> device name
136 SIGNAL(itemActivated(QListWidgetItem *)),
138 SLOT(clearSelection()),
139 Qt::QueuedConnection);
141 // device name -> keyset name
144 SIGNAL(itemActivated(QListWidgetItem *)),
146 SLOT(clearSelection()),
147 Qt::QueuedConnection);
150 // The PIRModprobe object should take care of setting up and shutting down
151 // the lirc_rx51 kernel module, if necessary:
153 if (modprobeObj.loadRX51Module() != 0)
155 // Couldn't load module, quit:
157 errBox.setText("Couldn't load lirc_rx51 kernel module!");
158 errBox.setIcon(QMessageBox::Warning);
160 // throw; // Need a clean way to exit here!!!
166 MainWindow::~MainWindow()
169 if (selectKeysetForm) delete selectKeysetForm;
170 if (selectDeviceForm) delete selectDeviceForm;
171 if (panelSelectionForm) delete panelSelectionForm;
172 if (documentationForm) delete documentationForm;
173 if (aboutForm) delete aboutForm;
178 void MainWindow::setOrientation(ScreenOrientation orientation)
180 #if defined(Q_OS_SYMBIAN)
181 // If the version of Qt on the device is < 4.7.2, that attribute won't work
182 if (orientation != ScreenOrientationAuto) {
183 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
184 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
185 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
189 #endif // Q_OS_SYMBIAN
191 Qt::WidgetAttribute attribute;
192 switch (orientation) {
193 #if QT_VERSION < 0x040702
194 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
195 case ScreenOrientationLockPortrait:
196 attribute = static_cast<Qt::WidgetAttribute>(128);
198 case ScreenOrientationLockLandscape:
199 attribute = static_cast<Qt::WidgetAttribute>(129);
202 case ScreenOrientationAuto:
203 attribute = static_cast<Qt::WidgetAttribute>(130);
205 #else // QT_VERSION < 0x040702
206 case ScreenOrientationLockPortrait:
207 attribute = Qt::WA_LockPortraitOrientation;
209 case ScreenOrientationLockLandscape:
210 attribute = Qt::WA_LockLandscapeOrientation;
213 case ScreenOrientationAuto:
214 attribute = Qt::WA_AutoOrientation;
216 #endif // QT_VERSION < 0x040702
218 setAttribute(attribute, true);
221 void MainWindow::showExpanded()
223 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
225 #elif defined(Q_WS_MAEMO_5)
233 void MainWindow::enableButtons()
235 // Just to be sure, check to see if the keyset has been populated:
236 myKeysets->populateKeyset(this, currentKeyset);
238 myPanels->enableButtons(myKeysets, currentKeyset);
242 void MainWindow::useMainPanel()
244 myPanels->useMainPanel();
248 void MainWindow::useAltMainPanel()
250 myPanels->useAltMainPanel();
254 void MainWindow::receivedExternalWarning(
258 errBox.setText(warning);
259 errBox.setIcon(QMessageBox::Warning);
266 void MainWindow::on_actionSelectKeyset_triggered()
268 selectKeysetForm->show();
271 void MainWindow::on_actionSelect_Device_By_Name_triggered()
273 selectDeviceForm->show();
276 void MainWindow::on_actionArrange_Button_Panels_triggered()
278 panelSelectionForm->show();
281 void MainWindow::on_actionPreferences_triggered()
283 preferencesForm->show();
286 void MainWindow::on_actionAbout_triggered()
290 aboutForm = new PIRAboutForm(this);
296 void MainWindow::on_actionDocumentation_triggered()
298 if (!documentationForm)
300 documentationForm = new PIRDocumentationForm(this);
303 documentationForm->show();
309 void MainWindow::keysetSelectionChanged(
310 QListWidgetItem *item)
312 if (!item) return; // Should probably display error message here!
314 PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
316 if (!kwi) return; // Also need to say something here
318 if (currentKeyset == kwi->getID())
320 // We're already on that keyset, so nothing to do:
324 currentKeyset = kwi->getID();
326 QSettings settings("pietrzak.org", "Pierogi");
327 // settings.setValue("currentKeyset", currentKeyset);
331 makeManager.getMakeString(kwi->getMake()));
335 myKeysets->getDisplayName(currentKeyset));
341 void MainWindow::finalCleanup()
343 // Perform any necessary cleanup work here.
345 // Make certain that the thread stops repeating:
350 void MainWindow::addCurrentKeyset(
353 // Is the current keyset already a favorite?
354 int count = qlw->count();
356 PIRKeysetWidgetItem *kwi = NULL;
357 while (index < count)
359 kwi = dynamic_cast<PIRKeysetWidgetItem *>(
362 if (kwi && (kwi->getID() == currentKeyset))
364 // Current keyset already in list! No need to continue.
370 // Ok, add the current keyset to the favorites:
371 PIRMakeName make = myKeysets->getMake(currentKeyset);
373 QString name = makeManager.getMakeString(make);
375 name.append(myKeysets->getDisplayName(currentKeyset));
377 qlw->addItem(new PIRKeysetWidgetItem(name, currentKeyset, make));
379 // And, add the keyset id to the persistent list:
380 QSettings settings("pietrzak.org", "Pierogi");
382 int favSettingsSize = settings.beginReadArray("favorites");
385 settings.beginWriteArray("favorites");
386 settings.setArrayIndex(favSettingsSize);
387 // settings.setValue("keysetID", currentKeyset);
391 makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
393 settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
399 void MainWindow::removeFavoriteKeyset(
402 // Deleting an item removes it from the list, so just grab the currently
403 // selected item and delete it:
404 QListWidgetItem *item = qlw->currentItem();
406 if (item) delete item;
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 = qlw->count();
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 = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
429 settings.setArrayIndex(index);
430 // settings.setValue("keysetID", kwi->getID());
435 makeManager.getMakeString(myKeysets->getMake(id)));
437 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 myPanels->addFavoritesItem(kwi);
477 void MainWindow::startRepeating(
480 QMutexLocker locker(&commandIFMutex);
481 if (!commandInFlight)
483 commandInFlight = true;
484 emit buttonPressed(currentKeyset, name);
489 void MainWindow::stopRepeating()
491 QMutexLocker locker(&stopRepeatingMutex);
492 stopRepeatingFlag = true;
496 void MainWindow::selectPrevFavKeyset()
498 myPanels->selectPrevFavKeyset();
502 void MainWindow::selectNextFavKeyset()
504 myPanels->selectNextFavKeyset();
508 void MainWindow::selectPanel(
511 ui->selectPanelComboBox->setCurrentIndex(index);
515 void MainWindow::managePanel(
519 myPanels->managePanel(name, state);
523 void MainWindow::insertPanel(
526 const QString &displayName)
528 ui->selectPanelComboBox->insertItem(index, displayName);
529 ui->stackedButtonsWidget->insertWidget(index, panel);
533 void MainWindow::removePanel(
537 ui->stackedButtonsWidget->removeWidget(panel);
538 ui->selectPanelComboBox->removeItem(index);
542 void MainWindow::on_prevPanelButton_clicked()
544 int count = ui->selectPanelComboBox->count();
546 // No need to do anything if there are less than two panels available:
547 if (count < 2) return;
549 int index = ui->selectPanelComboBox->currentIndex();
559 ui->selectPanelComboBox->setCurrentIndex(index);
563 void MainWindow::on_nextPanelButton_clicked()
565 int count = ui->selectPanelComboBox->count();
567 // No need to do anything if there are less than two panels available:
568 if (count < 2) return;
570 int index = ui->selectPanelComboBox->currentIndex();
571 if (index == count - 1)
580 ui->selectPanelComboBox->setCurrentIndex(index);
584 void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
586 ui->stackedButtonsWidget->setCurrentIndex(index);