New Keysets
[pierogi] / mainwindow.cpp
index 692b64c..f57cde7 100644 (file)
@@ -4,19 +4,30 @@
 #include <QtCore/QCoreApplication>
 #include <QMutex>
 #include <QtGui/QMessageBox>
-//#include <QtGui>
+#include <QPushButton>
+#include <QDialog>
+#include <QDialogButtonBox>
+#include <QScrollArea>
 #include <QSettings>
+#include <QKeyEvent>
+
+#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:
 
@@ -38,10 +49,14 @@ 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
 {
   ui->setupUi(this);
 
@@ -50,24 +65,24 @@ 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);
+  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);
+  preferencesForm = new PIRPreferencesForm(this, myKeysets);
 
-  myKeysets->populateSelectionWidgets(selectKeysetForm, selectDeviceForm);
-//  myPanels->setupPanels(panelSelectionForm);
-
-  // Remember any favorites the user has already set:
-  populateFavorites();
-
-  // Retrieve the user's most recent keyset (if any):
+  // Retrieve the user's preferences:
   QSettings settings("pietrzak.org", "Pierogi");
   if (settings.contains("currentKeysetName"))
   {
@@ -75,56 +90,21 @@ MainWindow::MainWindow(QWidget *parent)
       settings.value("currentKeysetMake").toString(),
       settings.value("currentKeysetName").toString(),
       currentKeyset);
-//    currentKeyset = settings.value("currentKeyset").toInt();
   }
 
-  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,
@@ -160,12 +140,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;
 }
 
@@ -230,7 +214,83 @@ 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);
+  }
+}
+
+
+void MainWindow::useMainPanel()
+{
+  myPanels->useMainPanel();
+}
+
+
+void MainWindow::useAltMainPanel()
+{
+  myPanels->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);
 }
 
 
@@ -251,14 +311,14 @@ 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()
+void MainWindow::on_actionPreferences_triggered()
 {
-  panelSelectionForm->show();
+  preferencesForm->show();
 }
 
 void MainWindow::on_actionAbout_triggered()
@@ -298,11 +358,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",
@@ -325,34 +387,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");
@@ -362,26 +401,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
@@ -391,7 +425,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;
@@ -402,10 +436,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(
@@ -420,6 +453,7 @@ void MainWindow::removeFavoriteKeyset(
 }
 
 
+/*
 void MainWindow::populateFavorites()
 {
   QSettings settings("pietrzak.org", "Pierogi");
@@ -442,7 +476,7 @@ void MainWindow::populateFavorites()
     if (kwi)
     {
       // Keyset does exist, so continue:
-      myPanels->addFavoritesItem(kwi);
+      favoritesDialog->addItem(kwi);
     }
 
     ++index;
@@ -450,9 +484,10 @@ void MainWindow::populateFavorites()
 
   settings.endArray();
 }
+*/
 
 
-void MainWindow::startRepeating(
+bool MainWindow::startRepeating(
   PIRKeyName name)
 {
   QMutexLocker locker(&commandIFMutex);
@@ -460,6 +495,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;
   }
 }
 
@@ -473,86 +531,263 @@ void MainWindow::stopRepeating()
 
 void MainWindow::selectPrevFavKeyset()
 {
-  myPanels->selectPrevFavKeyset();
+  favoritesDialog->selectPrevFavKeyset();
 }
 
 
 void MainWindow::selectNextFavKeyset()
 {
-  myPanels->selectNextFavKeyset();
+  favoritesDialog->selectNextFavKeyset();
 }
 
 
-void MainWindow::managePanel(
-  PIRPanelName name,
-  int state)
+void MainWindow::insertCornerButtons()
 {
-  myPanels->managePanel(name, state);
+  // 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);
+
+  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);
 }
 
 
-void MainWindow::insertPanel(
-  int index,
-  QWidget *panel,
-  const QString &displayName)
+void MainWindow::disableUpdates()
 {
-  ui->stackedButtonsWidget->insertWidget(index, panel);
-  ui->selectPanelComboBox->insertItem(index, displayName);
+  ui->mainTabWidget->setUpdatesEnabled(false);
 }
 
 
-void MainWindow::removePanel(
-  int index,
-  QWidget *panel)
+void MainWindow::enableUpdates()
 {
-  ui->stackedButtonsWidget->removeWidget(panel);
-  ui->selectPanelComboBox->removeItem(index);
+  ui->mainTabWidget->setUpdatesEnabled(true);
 }
 
 
-void MainWindow::on_prevPanelButton_clicked()
+void MainWindow::clearTabs()
 {
-  int count = ui->selectPanelComboBox->count();
+  ui->mainTabWidget->clear();
+}
 
-  // No need to do anything if there are less than two panels available:
-  if (count < 2) return;
 
-  int index = ui->selectPanelComboBox->currentIndex();
-  if (index == 0)
-  {
-    index = count - 1;
-  }
-  else
-  {
-    --index;
-  }
+void MainWindow::addTab(
+  QWidget *page,
+  QString label)
+{
+  ui->mainTabWidget->addTab(page, label);
+}
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+void MainWindow::setupTabs(
+  PIRTabBarName name)
+{
+  myPanels->setupTabs(name);
 }
 
 
-void MainWindow::on_nextPanelButton_clicked()
+bool MainWindow::selectNextKeyset()
 {
-  int count = ui->selectPanelComboBox->count();
+  return selectKeysetForm->selectNextKeyset();
+}
 
-  // 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)
-  {
-    index = 0;
-  }
-  else
+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::keyPressEvent(
+  QKeyEvent *event)
+{
+  switch(event->key())
   {
-    ++index;
+  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;
+  default:
+    QMainWindow::keyPressEvent(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::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::on_selectPanelComboBox_currentIndexChanged(int index)
+void MainWindow::setMacroBtnFocus(
+  int index)
 {
-  ui->stackedButtonsWidget->setCurrentIndex(index);
+  myMacros->setBtnFocus(index);
 }