Advanced Settings Panel
[pierogi] / mainwindow.cpp
index ade853a..bd556f3 100644 (file)
@@ -1,24 +1,43 @@
+// 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"
 
 #include <QtCore/QCoreApplication>
 #include <QMutex>
 #include <QtGui/QMessageBox>
-//#include <QtGui>
+#include <QPushButton>
+#include <QDialog>
+#include <QDialogButtonBox>
+#include <QScrollArea>
 #include <QSettings>
+#include <QKeyEvent>
+
+//#include "pirtabwidget.h"
 
 #include "pirkeysetmetadata.h"
+
 #include "pirkeysetwidgetitem.h"
 #include "pirselectkeysetform.h"
 #include "pirselectdeviceform.h"
-#include "pirpanelselectionform.h"
 #include "pirpreferencesform.h"
 #include "pirdocumentationform.h"
 #include "piraboutform.h"
+#include "dialogs/pirtabschoicedialog.h"
+#include "dialogs/pirfavoritesdialog.h"
+
 #include "pirkeysetmanager.h"
 #include "pirpanelmanager.h"
+#include "macros/pirmacromanager.h"
 
 //#define DEBUGGING
+#include <iostream>
 
 // Some ugly globals used for thread communications:
 
@@ -40,12 +59,18 @@ MainWindow::MainWindow(QWidget *parent)
     ui(new Ui::MainWindow),
     selectKeysetForm(0),
     selectDeviceForm(0),
-    panelSelectionForm(0),
     preferencesForm(0),
     documentationForm(0),
     aboutForm(0),
-    currentKeyset(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:
@@ -53,25 +78,22 @@ MainWindow::MainWindow(QWidget *parent)
 
   // Create the managers:
   myKeysets = new PIRKeysetManager();
+  myMacros = new PIRMacroManager(this);
   myPanels = new PIRPanelManager(this);
 
-  // Set up the keyset selection window:
+  // Display the panels:
+  myPanels->updateTabSet();
+
+  // Construct the rest of the forms:
   selectKeysetForm = new PIRSelectKeysetForm(this);
-  myKeysets->populateSelectionWidget(selectKeysetForm);
+  favoritesDialog = new PIRFavoritesDialog(this);
+  myKeysets->populateListWidgets(selectKeysetForm, favoritesDialog);
+  selectKeysetForm->populateKeysetComboBox(myPanels->getKeysetComboBox());
 
-  // Set up the device selection window:
   selectDeviceForm = new PIRSelectDeviceForm(this);
   PIRKeysetMetaData::populateDevices(selectDeviceForm);
 
-  // Set up the panel selection window:
-  panelSelectionForm = new PIRPanelSelectionForm(this);
-  myPanels->setupPanels(panelSelectionForm);
-
-  // Set up the preferences window:
-  preferencesForm = new PIRPreferencesForm(this);
-
-  // Remember any favorites the user has already set:
-  populateFavorites();
+  preferencesForm = new PIRPreferencesForm(this, myKeysets);
 
   // Retrieve the user's preferences:
   QSettings settings("pietrzak.org", "Pierogi");
@@ -83,53 +105,19 @@ MainWindow::MainWindow(QWidget *parent)
       currentKeyset);
   }
 
-  enableButtons();
+  selectKeysetForm->selectKeyset(currentKeyset);
 
-  QListWidget *fkw = myPanels->getFavoritesListWidget();
+  // Add the corner buttons:
+  insertCornerButtons();
 
-  connect(
-    fkw,
-    SIGNAL(itemActivated(QListWidgetItem *)),
-    this,
-    SLOT(keysetSelectionChanged(QListWidgetItem *)),
-    Qt::QueuedConnection);
+  // Set up all the buttons:
+  enableButtons();
+  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,
@@ -165,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;
 }
 
@@ -235,7 +227,23 @@ void MainWindow::enableButtons()
   // Just to be sure, check to see if the keyset has been populated:
   myKeysets->populateKeyset(this, currentKeyset);
 
-  myPanels->enableButtons(myKeysets, currentKeyset);
+  if (preferencesForm)
+  {
+    unsigned int dk = preferencesForm->getDefaultKeyset();
+    if (preferencesForm->defaultControlsVolume() && dk)
+    {
+      myKeysets->populateKeyset(this, dk);
+      myPanels->enableButtons(myKeysets, currentKeyset, dk);
+    }
+    else
+    {
+      myPanels->enableButtons(myKeysets, currentKeyset);
+    }
+  }
+  else
+  {
+    myPanels->enableButtons(myKeysets, currentKeyset);
+  }
 }
 
 
@@ -251,6 +259,54 @@ void MainWindow::useAltMainPanel()
 }
 
 
+QString MainWindow::getCurrentMake()
+{
+  return makeManager.getMakeString(myKeysets->getMake(currentKeyset));
+}
+
+
+QString MainWindow::getCurrentName()
+{
+  return myKeysets->getDisplayName(currentKeyset);
+}
+
+
+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)
 {
@@ -268,16 +324,11 @@ void MainWindow::on_actionSelectKeyset_triggered()
   selectKeysetForm->show();
 }
 
-void MainWindow::on_actionSelect_Device_By_Name_triggered()
+void MainWindow::on_actionBrowse_Device_List_triggered()
 {
   selectDeviceForm->show();
 }
 
-void MainWindow::on_actionArrange_Button_Panels_triggered()
-{
-  panelSelectionForm->show();
-}
-
 void MainWindow::on_actionPreferences_triggered()
 {
   preferencesForm->show();
@@ -320,11 +371,13 @@ 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();
 
   QSettings settings("pietrzak.org", "Pierogi");
-//  settings.setValue("currentKeyset", currentKeyset);
 
   settings.setValue(
     "currentKeysetMake",
@@ -347,34 +400,11 @@ void MainWindow::finalCleanup()
 }
 
 
-void MainWindow::addCurrentKeyset(
-  QListWidget *qlw)
+void MainWindow::addToFavorites(
+  PIRKeysetWidgetItem *kwi)
 {
-  // Is the current keyset already a favorite?
-  int count = qlw->count();
-  int index = 0;
-  PIRKeysetWidgetItem *kwi = NULL;
-  while (index < count)
-  {
-    kwi = dynamic_cast<PIRKeysetWidgetItem *>(
-      qlw->item(index));
-
-    if (kwi && (kwi->getID() == currentKeyset))
-    {
-      // Current keyset already in list!  No need to continue.
-      return;
-    }
-    ++index;
-  }
-
-  // Ok, add the current keyset to the favorites:
-  PIRMakeName make = myKeysets->getMake(currentKeyset);
-
-  QString name = makeManager.getMakeString(make);
-  name.append(" ");
-  name.append(myKeysets->getDisplayName(currentKeyset));
-
-  qlw->addItem(new PIRKeysetWidgetItem(name, currentKeyset, make));
+  //Add keyset to the favorites:
+  favoritesDialog->addItem(new PIRKeysetWidgetItem(kwi));
 
   // And, add the keyset id to the persistent list:
   QSettings settings("pietrzak.org", "Pierogi");
@@ -384,26 +414,21 @@ void MainWindow::addCurrentKeyset(
 
   settings.beginWriteArray("favorites");
   settings.setArrayIndex(favSettingsSize);
-//  settings.setValue("keysetID", currentKeyset);
 
   settings.setValue(
     "keysetMake",
-    makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
+    makeManager.getMakeString(kwi->getMake()));
 
-  settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
+  settings.setValue("keysetName", kwi->getInternalName());
 
   settings.endArray();
 }
 
 
-void MainWindow::removeFavoriteKeyset(
-  QListWidget *qlw)
+void MainWindow::removeFromFavorites(
+  unsigned int keysetID)
 {
-  // Deleting an item removes it from the list, so just grab the currently
-  // selected item and delete it:
-  QListWidgetItem *item = qlw->currentItem();
-
-  if (item) delete item;
+  favoritesDialog->removeItem(keysetID);
 
   // Remove this item from the persistent list.  Well, actually, it seems a
   // little more convenient to just blow away the existing list of favorites
@@ -413,7 +438,7 @@ void MainWindow::removeFavoriteKeyset(
 
   settings.remove("favorites");
 
-  int count = qlw->count();
+  int count = favoritesDialog->getCount();
 
   // If the count is empty, we can stop right here:
   if (count == 0) return;
@@ -424,10 +449,9 @@ void MainWindow::removeFavoriteKeyset(
   settings.beginWriteArray("favorites");
   while (index < count)
   {
-    kwi = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
+    kwi = favoritesDialog->getItem(index);
 
     settings.setArrayIndex(index);
-//    settings.setValue("keysetID", kwi->getID());
     id = kwi->getID();
 
     settings.setValue(
@@ -442,6 +466,7 @@ void MainWindow::removeFavoriteKeyset(
 }
 
 
+/*
 void MainWindow::populateFavorites()
 {
   QSettings settings("pietrzak.org", "Pierogi");
@@ -464,7 +489,7 @@ void MainWindow::populateFavorites()
     if (kwi)
     {
       // Keyset does exist, so continue:
-      myPanels->addFavoritesItem(kwi);
+      favoritesDialog->addItem(kwi);
     }
 
     ++index;
@@ -472,9 +497,10 @@ void MainWindow::populateFavorites()
 
   settings.endArray();
 }
+*/
 
 
-void MainWindow::startRepeating(
+bool MainWindow::startRepeating(
   PIRKeyName name)
 {
   QMutexLocker locker(&commandIFMutex);
@@ -482,6 +508,29 @@ void MainWindow::startRepeating(
   {
     commandInFlight = true;
     emit buttonPressed(currentKeyset, name);
+    return true;
+  }
+  else
+  {
+    return false;
+  }
+}
+
+
+bool MainWindow::startRepeating(
+  PIRKeyName name,
+  unsigned int keysetID)
+{
+  QMutexLocker locker(&commandIFMutex);
+  if (!commandInFlight)
+  {
+    commandInFlight = true;
+    emit buttonPressed(keysetID, name);
+    return true;
+  }
+  else
+  {
+    return false;
   }
 }
 
@@ -495,93 +544,366 @@ void MainWindow::stopRepeating()
 
 void MainWindow::selectPrevFavKeyset()
 {
-  myPanels->selectPrevFavKeyset();
+  favoritesDialog->selectPrevFavKeyset();
 }
 
 
 void MainWindow::selectNextFavKeyset()
 {
-  myPanels->selectNextFavKeyset();
+  favoritesDialog->selectNextFavKeyset();
 }
 
 
-void MainWindow::selectPanel(
-  int index)
+void MainWindow::insertCornerButtons()
 {
-  ui->selectPanelComboBox->setCurrentIndex(index);
+  // Set up the dialog boxes:
+  PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
+//  favoritesDialog = new PIRFavoritesDialog(this);
+
+  // Next, set up the corner buttons:
+  QPushButton *button =
+    new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
+
+  button->setFlat(true);
+
+  connect(
+    button,
+    SIGNAL(clicked()),
+    tcd,
+    SLOT(exec()),
+    Qt::QueuedConnection);
+
+  ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
+//  myTabWidget->setCornerWidget(button, Qt::TopRightCorner);
+
+  button =
+    new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
+
+  button->setFlat(true);
+
+  connect(
+    button,
+    SIGNAL(clicked()),
+    favoritesDialog,
+    SLOT(exec()),
+    Qt::QueuedConnection);
+
+  ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
+//  myTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
 }
 
 
-void MainWindow::managePanel(
-  PIRPanelName name,
-  int state)
+void MainWindow::disableUpdates()
 {
-  myPanels->managePanel(name, state);
+  ui->mainTabWidget->setUpdatesEnabled(false);
+//  myTabWidget->setUpdatesEnabled(false);
 }
 
 
-void MainWindow::insertPanel(
-  int index,
-  QWidget *panel,
-  const QString &displayName)
+void MainWindow::enableUpdates()
 {
-  ui->selectPanelComboBox->insertItem(index, displayName);
-  ui->stackedButtonsWidget->insertWidget(index, panel);
+  ui->mainTabWidget->setUpdatesEnabled(true);
+//  myTabWidget->setUpdatesEnabled(true);
 }
 
 
-void MainWindow::removePanel(
-  int index,
-  QWidget *panel)
+void MainWindow::clearTabs()
 {
-  ui->stackedButtonsWidget->removeWidget(panel);
-  ui->selectPanelComboBox->removeItem(index);
+  ui->mainTabWidget->clear();
+//  myTabWidget->clear();
 }
 
 
-void MainWindow::on_prevPanelButton_clicked()
+void MainWindow::addTab(
+  QWidget *page,
+  QString label)
 {
-  int count = ui->selectPanelComboBox->count();
+  ui->mainTabWidget->addTab(page, label);
+//  myTabWidget->addTab(page, label);
+}
 
-  // No need to do anything if there are less than two panels available:
-  if (count < 2) return;
+void MainWindow::setupTabs(
+  PIRTabBarName name)
+{
+  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);
+}
 
-  int index = ui->selectPanelComboBox->currentIndex();
-  if (index == 0)
-  {
-    index = count - 1;
-  }
-  else
-  {
-    --index;
-  }
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+/*
+void MainWindow::handleKeyRelease(
+  char key)
+{
 }
+*/
 
 
-void MainWindow::on_nextPanelButton_clicked()
+void MainWindow::gotoPreviousTabs()
 {
-  int count = ui->selectPanelComboBox->count();
+  myPanels->gotoPreviousTabs();
+}
 
-  // No need to do anything if there are less than two panels available:
-  if (count < 2) return;
 
-  int index = ui->selectPanelComboBox->currentIndex();
-  if (index == count - 1)
+void MainWindow::gotoNextTabs()
+{
+  myPanels->gotoNextTabs();
+}
+
+/*
+void MainWindow::keyPressEvent(
+  QKeyEvent *event)
+{
+  switch(event->key())
   {
-    index = 0;
+  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;
   }
-  else
+}
+*/
+
+
+/*
+void MainWindow::keyReleaseEvent(
+  QKeyEvent *event)
+{
+  switch(event->key())
   {
-    ++index;
+
+  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);
+}
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+
+QString MainWindow::getMacroButtonText(
+  unsigned int buttonID)
+{
+  return myMacros->getMacroButtonText(buttonID);
 }
 
 
-void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
+void MainWindow::executeMacroButton(
+  unsigned int buttonID)
 {
-  ui->stackedButtonsWidget->setCurrentIndex(index);
+  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);
+  }
 }