Advanced Settings Panel
[pierogi] / mainwindow.cpp
index d55e661..bd556f3 100644 (file)
@@ -1,3 +1,11 @@
+// mainwindow.cpp
+//
+// For the Pierogi IR remote control app.
+//
+// Copyright (C) 2012 by John Pietrzak  (john@pietrzak.org)
+//
+// Licensed under the GNU GPL version 2.0 or later.
+
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 
@@ -9,6 +17,9 @@
 #include <QDialogButtonBox>
 #include <QScrollArea>
 #include <QSettings>
+#include <QKeyEvent>
+
+//#include "pirtabwidget.h"
 
 #include "pirkeysetmetadata.h"
 
 
 #include "pirkeysetmanager.h"
 #include "pirpanelmanager.h"
+#include "macros/pirmacromanager.h"
 
 //#define DEBUGGING
-//#include <iostream>
+#include <iostream>
 
 // Some ugly globals used for thread communications:
 
@@ -50,10 +62,15 @@ MainWindow::MainWindow(QWidget *parent)
     preferencesForm(0),
     documentationForm(0),
     aboutForm(0),
+    favoritesDialog(0),
     myKeysets(0),
     myPanels(0),
+    myMacros(0),
     currentKeyset(1) // Zero is not a valid keyset any more
 {
+  // Create the tab widget:
+//  myTabWidget = new PIRTabWidget(ui->centralWidget, this);
+
   ui->setupUi(this);
 
   // Make this a Maemo 5 stacked widget:
@@ -61,6 +78,7 @@ MainWindow::MainWindow(QWidget *parent)
 
   // Create the managers:
   myKeysets = new PIRKeysetManager();
+  myMacros = new PIRMacroManager(this);
   myPanels = new PIRPanelManager(this);
 
   // Display the panels:
@@ -70,6 +88,7 @@ MainWindow::MainWindow(QWidget *parent)
   selectKeysetForm = new PIRSelectKeysetForm(this);
   favoritesDialog = new PIRFavoritesDialog(this);
   myKeysets->populateListWidgets(selectKeysetForm, favoritesDialog);
+  selectKeysetForm->populateKeysetComboBox(myPanels->getKeysetComboBox());
 
   selectDeviceForm = new PIRSelectDeviceForm(this);
   PIRKeysetMetaData::populateDevices(selectDeviceForm);
@@ -86,56 +105,19 @@ MainWindow::MainWindow(QWidget *parent)
       currentKeyset);
   }
 
+  selectKeysetForm->selectKeyset(currentKeyset);
+
   // Add the corner buttons:
   insertCornerButtons();
 
+  // Set up all the buttons:
   enableButtons();
-
-  QListWidget *fkw = favoritesDialog->getFavoritesListWidget();
-
-  connect(
-    fkw,
-    SIGNAL(itemActivated(QListWidgetItem *)),
-    this,
-    SLOT(keysetSelectionChanged(QListWidgetItem *)),
-    Qt::QueuedConnection);
+  myPanels->updateUserButtons();
 
   // Make sure the three selection lists don't show different selections:
   QListWidget *klw = selectKeysetForm->getKeysetListWidget();
   QListWidget *dlw = selectDeviceForm->getDeviceListWidget();
 
-  // favorites -> keyset name
-  connect(
-    fkw,
-    SIGNAL(itemActivated(QListWidgetItem *)),
-    klw,
-    SLOT(clearSelection()),
-    Qt::QueuedConnection);
-
-  // favorites -> device name
-  connect(
-    fkw,
-    SIGNAL(itemActivated(QListWidgetItem *)),
-    dlw,
-    SLOT(clearSelection()),
-    Qt::QueuedConnection);
-
-  // keyset name -> favorites
-  connect(
-    klw,
-    SIGNAL(itemActivated(QListWidgetItem *)),
-    fkw,
-    SLOT(clearSelection()),
-    Qt::QueuedConnection);
-
-  // device name -> favorites
-  connect(
-    dlw,
-    SIGNAL(itemActivated(QListWidgetItem *)),
-    fkw,
-    SLOT(clearSelection()),
-    Qt::QueuedConnection);
-
   // keyset name -> device name
   connect(
     klw,
@@ -171,12 +153,16 @@ MainWindow::MainWindow(QWidget *parent)
 
 MainWindow::~MainWindow()
 {
-  delete myKeysets;
-  if (selectKeysetForm) delete selectKeysetForm;
-  if (selectDeviceForm) delete selectDeviceForm;
-//  if (panelSelectionForm) delete panelSelectionForm;
-  if (documentationForm) delete documentationForm;
   if (aboutForm) delete aboutForm;
+  if (documentationForm) delete documentationForm;
+  if (preferencesForm) delete preferencesForm;
+  if (selectDeviceForm) delete selectDeviceForm;
+  if (favoritesDialog) delete favoritesDialog;
+  if (selectKeysetForm) delete selectKeysetForm;
+
+  if (myPanels) delete myPanels;
+  if (myMacros) delete myMacros;
+  if (myKeysets) delete myKeysets;
   delete ui;
 }
 
@@ -285,6 +271,42 @@ QString MainWindow::getCurrentName()
 }
 
 
+QString MainWindow::getCurrentFullName()
+{
+  return selectKeysetForm->getCurrentKeysetName();
+}
+
+
+QString MainWindow::getKeysetMake(
+  unsigned int id)
+{
+  return makeManager.getMakeString(myKeysets->getMake(id));
+}
+
+
+QString MainWindow::getKeysetName(
+  unsigned int id)
+{
+  return myKeysets->getDisplayName(id);
+}
+
+
+QString MainWindow::getFullKeysetName(
+  unsigned int id)
+{
+  return selectKeysetForm->getKeysetName(id);
+}
+
+
+bool MainWindow::findKeysetID(
+  QString make,
+  QString name,
+  unsigned int &id)
+{
+  return myKeysets->findKeysetID(make, name, id);
+}
+
+
 void MainWindow::receivedExternalWarning(
   const char *warning)
 {
@@ -349,6 +371,9 @@ void MainWindow::keysetSelectionChanged(
     // We're already on that keyset, so nothing to do:
     return;
   }
+
+  // Clean up and remove the current keyset:
+  myKeysets->clearKeyset(currentKeyset);
   
   currentKeyset = kwi->getID();
 
@@ -475,7 +500,7 @@ void MainWindow::populateFavorites()
 */
 
 
-void MainWindow::startRepeating(
+bool MainWindow::startRepeating(
   PIRKeyName name)
 {
   QMutexLocker locker(&commandIFMutex);
@@ -483,11 +508,16 @@ void MainWindow::startRepeating(
   {
     commandInFlight = true;
     emit buttonPressed(currentKeyset, name);
+    return true;
+  }
+  else
+  {
+    return false;
   }
 }
 
 
-void MainWindow::startRepeating(
+bool MainWindow::startRepeating(
   PIRKeyName name,
   unsigned int keysetID)
 {
@@ -496,6 +526,11 @@ void MainWindow::startRepeating(
   {
     commandInFlight = true;
     emit buttonPressed(keysetID, name);
+    return true;
+  }
+  else
+  {
+    return false;
   }
 }
 
@@ -539,6 +574,7 @@ void MainWindow::insertCornerButtons()
     Qt::QueuedConnection);
 
   ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
+//  myTabWidget->setCornerWidget(button, Qt::TopRightCorner);
 
   button =
     new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
@@ -553,24 +589,28 @@ void MainWindow::insertCornerButtons()
     Qt::QueuedConnection);
 
   ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
+//  myTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
 }
 
 
 void MainWindow::disableUpdates()
 {
   ui->mainTabWidget->setUpdatesEnabled(false);
+//  myTabWidget->setUpdatesEnabled(false);
 }
 
 
 void MainWindow::enableUpdates()
 {
   ui->mainTabWidget->setUpdatesEnabled(true);
+//  myTabWidget->setUpdatesEnabled(true);
 }
 
 
 void MainWindow::clearTabs()
 {
   ui->mainTabWidget->clear();
+//  myTabWidget->clear();
 }
 
 
@@ -579,6 +619,7 @@ void MainWindow::addTab(
   QString label)
 {
   ui->mainTabWidget->addTab(page, label);
+//  myTabWidget->addTab(page, label);
 }
 
 void MainWindow::setupTabs(
@@ -586,3 +627,283 @@ void MainWindow::setupTabs(
 {
   myPanels->setupTabs(name);
 }
+
+
+bool MainWindow::selectNextKeyset()
+{
+  return selectKeysetForm->selectNextKeyset();
+}
+
+
+bool MainWindow::selectPrevKeyset()
+{
+  return selectKeysetForm->selectPrevKeyset();
+}
+
+
+bool MainWindow::selectFirstKeyset()
+{
+  return selectKeysetForm->selectFirstKeyset();
+}
+
+
+void MainWindow::openCurrentKeysetDialog()
+{
+  selectKeysetForm->openCurrentKeysetDialog();
+}
+
+
+void MainWindow::updateKeysetSelection(
+  unsigned int targetID)
+{
+  selectKeysetForm->selectKeyset(targetID);
+}
+
+
+PIRMacroPack *MainWindow::getUserPack()
+{
+  return myMacros->getUserPack();
+}
+
+
+PIRMacroPack *MainWindow::getMultitapPack()
+{
+  return myMacros->getMultitapPack();
+}
+
+
+void MainWindow::handleKeypress(
+  char key)
+{
+  myMacros->handleKeypress(key);
+}
+
+
+/*
+void MainWindow::handleKeyRelease(
+  char key)
+{
+}
+*/
+
+
+void MainWindow::gotoPreviousTabs()
+{
+  myPanels->gotoPreviousTabs();
+}
+
+
+void MainWindow::gotoNextTabs()
+{
+  myPanels->gotoNextTabs();
+}
+
+/*
+void MainWindow::keyPressEvent(
+  QKeyEvent *event)
+{
+  switch(event->key())
+  {
+  case Qt::Key_A:
+    myMacros->handleKeypress('A');
+    break;
+  case Qt::Key_B:
+    myMacros->handleKeypress('B');
+    break;
+  case Qt::Key_C:
+    myMacros->handleKeypress('C');
+    break;
+  case Qt::Key_D:
+    myMacros->handleKeypress('D');
+    break;
+  case Qt::Key_E:
+    myMacros->handleKeypress('E');
+    break;
+  case Qt::Key_F:
+    myMacros->handleKeypress('F');
+    break;
+  case Qt::Key_G:
+    myMacros->handleKeypress('G');
+    break;
+  case Qt::Key_H:
+    myMacros->handleKeypress('H');
+    break;
+  case Qt::Key_I:
+    myMacros->handleKeypress('I');
+    break;
+  case Qt::Key_J:
+    myMacros->handleKeypress('J');
+    break;
+  case Qt::Key_K:
+    myMacros->handleKeypress('K');
+    break;
+  case Qt::Key_L:
+    myMacros->handleKeypress('L');
+    break;
+  case Qt::Key_M:
+    myMacros->handleKeypress('M');
+    break;
+  case Qt::Key_N:
+    myMacros->handleKeypress('N');
+    break;
+  case Qt::Key_O:
+    myMacros->handleKeypress('O');
+    break;
+  case Qt::Key_P:
+    myMacros->handleKeypress('P');
+    break;
+  case Qt::Key_Q:
+    myMacros->handleKeypress('Q');
+    break;
+  case Qt::Key_R:
+    myMacros->handleKeypress('R');
+    break;
+  case Qt::Key_S:
+    myMacros->handleKeypress('S');
+    break;
+  case Qt::Key_T:
+    myMacros->handleKeypress('T');
+    break;
+  case Qt::Key_U:
+    myMacros->handleKeypress('U');
+    break;
+  case Qt::Key_V:
+    myMacros->handleKeypress('V');
+    break;
+  case Qt::Key_W:
+    myMacros->handleKeypress('W');
+    break;
+  case Qt::Key_X:
+    myMacros->handleKeypress('X');
+    break;
+  case Qt::Key_Y:
+    myMacros->handleKeypress('Y');
+    break;
+  case Qt::Key_Z:
+    myMacros->handleKeypress('Z');
+    break;
+  case Qt::Key_Space:
+    myMacros->handleKeypress(' ');
+    break;
+
+  case Qt::Key_Up:
+    myPanels->gotoPreviousTabs();
+    break;
+  case Qt::Key_Down:
+    myPanels->gotoNextTabs();
+    break;
+
+
+  case Qt::Key_Left:
+    startRepeating(VolumeDown_Key);
+    break;
+  case Qt::Key_Right:
+    startRepeating(VolumeUp_Key);
+    break;
+
+  case Qt::Key_Backspace:
+    startRepeating(Power_Key);
+    break;
+  case Qt::Key_Return:
+    startRepeating(Mute_Key);
+    break;
+
+  default:
+    QMainWindow::keyPressEvent(event);
+    break;
+  }
+}
+*/
+
+
+/*
+void MainWindow::keyReleaseEvent(
+  QKeyEvent *event)
+{
+  switch(event->key())
+  {
+
+  case Qt::Key_Up:
+  case Qt::Key_Down:
+  case Qt::Key_Left:
+  case Qt::Key_Right:
+
+  case Qt::Key_Backspace:
+  case Qt::Key_Return:
+    stopRepeating();
+    break;
+
+  default:
+    QMainWindow::keyReleaseEvent(event);
+    break;
+  }
+}
+*/
+
+
+bool MainWindow::hasMacroButton(
+  unsigned int buttonID)
+{
+  return myMacros->hasMacroButton(buttonID);
+}
+
+
+QString MainWindow::getMacroButtonText(
+  unsigned int buttonID)
+{
+  return myMacros->getMacroButtonText(buttonID);
+}
+
+
+void MainWindow::executeMacroButton(
+  unsigned int buttonID)
+{
+  myMacros->executeMacroButton(buttonID);
+}
+
+
+void MainWindow::updateUserButtons()
+{
+  myPanels->updateUserButtons();
+}
+
+
+void MainWindow::storeMacros()
+{
+  myMacros->storeSettings();
+}
+
+
+void MainWindow::setMacroKbdFocus(
+  int index)
+{
+  myMacros->setKbdFocus(index);
+}
+
+
+void MainWindow::setMacroBtnFocus(
+  int index)
+{
+  myMacros->setBtnFocus(index);
+}
+
+
+void MainWindow::switchToTab(
+  int tabNumber)
+{
+  int count = ui->mainTabWidget->count();
+
+  if (tabNumber < 0)
+  {
+    ui->mainTabWidget->setCurrentIndex(0);
+  }
+  else if (tabNumber >= count)
+  {
+    ui->mainTabWidget->setCurrentIndex(count-1);
+  }
+  else
+  {
+    ui->mainTabWidget->setCurrentIndex(tabNumber);
+  }
+}