Adding per-keyset editable data
[pierogi] / mainwindow.cpp
index 692b64c..bdce733 100644 (file)
@@ -4,19 +4,28 @@
 #include <QtCore/QCoreApplication>
 #include <QMutex>
 #include <QtGui/QMessageBox>
-//#include <QtGui>
+#include <QPushButton>
+#include <QDialog>
+#include <QDialogButtonBox>
+#include <QScrollArea>
 #include <QSettings>
 
+#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"
 
 //#define DEBUGGING
+//#include <iostream>
 
 // Some ugly globals used for thread communications:
 
@@ -38,10 +47,12 @@ MainWindow::MainWindow(QWidget *parent)
     ui(new Ui::MainWindow),
     selectKeysetForm(0),
     selectDeviceForm(0),
-    panelSelectionForm(0),
+    preferencesForm(0),
     documentationForm(0),
     aboutForm(0),
-    currentKeyset(0)
+    myKeysets(0),
+    myPanels(0),
+    currentKeyset(1) // Zero is not a valid keyset any more
 {
   ui->setupUi(this);
 
@@ -52,22 +63,20 @@ MainWindow::MainWindow(QWidget *parent)
   myKeysets = new PIRKeysetManager();
   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);
 
-  // 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,12 +84,14 @@ MainWindow::MainWindow(QWidget *parent)
       settings.value("currentKeysetMake").toString(),
       settings.value("currentKeysetName").toString(),
       currentKeyset);
-//    currentKeyset = settings.value("currentKeyset").toInt();
   }
 
+  // Add the corner buttons:
+  insertCornerButtons();
+
   enableButtons();
 
-  QListWidget *fkw = myPanels->getFavoritesListWidget();
+  QListWidget *fkw = favoritesDialog->getFavoritesListWidget();
 
   connect(
     fkw,
@@ -163,7 +174,7 @@ MainWindow::~MainWindow()
   delete myKeysets;
   if (selectKeysetForm) delete selectKeysetForm;
   if (selectDeviceForm) delete selectDeviceForm;
-  if (panelSelectionForm) delete panelSelectionForm;
+//  if (panelSelectionForm) delete panelSelectionForm;
   if (documentationForm) delete documentationForm;
   if (aboutForm) delete aboutForm;
   delete ui;
@@ -230,7 +241,47 @@ 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);
 }
 
 
@@ -256,9 +307,9 @@ void MainWindow::on_actionSelect_Device_By_Name_triggered()
   selectDeviceForm->show();
 }
 
-void MainWindow::on_actionArrange_Button_Panels_triggered()
+void MainWindow::on_actionPreferences_triggered()
 {
-  panelSelectionForm->show();
+  preferencesForm->show();
 }
 
 void MainWindow::on_actionAbout_triggered()
@@ -302,7 +353,6 @@ void MainWindow::keysetSelectionChanged(
   currentKeyset = kwi->getID();
 
   QSettings settings("pietrzak.org", "Pierogi");
-//  settings.setValue("currentKeyset", currentKeyset);
 
   settings.setValue(
     "currentKeysetMake",
@@ -325,34 +375,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 +389,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 +413,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 +424,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 +441,7 @@ void MainWindow::removeFavoriteKeyset(
 }
 
 
+/*
 void MainWindow::populateFavorites()
 {
   QSettings settings("pietrzak.org", "Pierogi");
@@ -442,7 +464,7 @@ void MainWindow::populateFavorites()
     if (kwi)
     {
       // Keyset does exist, so continue:
-      myPanels->addFavoritesItem(kwi);
+      favoritesDialog->addItem(kwi);
     }
 
     ++index;
@@ -450,6 +472,7 @@ void MainWindow::populateFavorites()
 
   settings.endArray();
 }
+*/
 
 
 void MainWindow::startRepeating(
@@ -464,6 +487,19 @@ void MainWindow::startRepeating(
 }
 
 
+void MainWindow::startRepeating(
+  PIRKeyName name,
+  unsigned int keysetID)
+{
+  QMutexLocker locker(&commandIFMutex);
+  if (!commandInFlight)
+  {
+    commandInFlight = true;
+    emit buttonPressed(keysetID, name);
+  }
+}
+
+
 void MainWindow::stopRepeating()
 {
   QMutexLocker locker(&stopRepeatingMutex);
@@ -473,86 +509,80 @@ 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);
 
-void MainWindow::insertPanel(
-  int index,
-  QWidget *panel,
-  const QString &displayName)
-{
-  ui->stackedButtonsWidget->insertWidget(index, panel);
-  ui->selectPanelComboBox->insertItem(index, displayName);
-}
+  // Next, set up the corner buttons:
+  QPushButton *button =
+    new QPushButton(QIcon(":/icons/folder_plus_icon&32.png"), "");
 
+  button->setFlat(true);
 
-void MainWindow::removePanel(
-  int index,
-  QWidget *panel)
-{
-  ui->stackedButtonsWidget->removeWidget(panel);
-  ui->selectPanelComboBox->removeItem(index);
-}
+  connect(
+    button,
+    SIGNAL(clicked()),
+    tcd,
+    SLOT(exec()),
+    Qt::QueuedConnection);
 
+  ui->mainTabWidget->setCornerWidget(button, Qt::TopRightCorner);
 
-void MainWindow::on_prevPanelButton_clicked()
-{
-  int count = ui->selectPanelComboBox->count();
+  button =
+    new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
 
-  // No need to do anything if there are less than two panels available:
-  if (count < 2) return;
+  button->setFlat(true);
 
-  int index = ui->selectPanelComboBox->currentIndex();
-  if (index == 0)
-  {
-    index = count - 1;
-  }
-  else
-  {
-    --index;
-  }
+  connect(
+    button,
+    SIGNAL(clicked()),
+    favoritesDialog,
+    SLOT(exec()),
+    Qt::QueuedConnection);
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+  ui->mainTabWidget->setCornerWidget(button, Qt::TopLeftCorner);
 }
 
 
-void MainWindow::on_nextPanelButton_clicked()
+void MainWindow::disableUpdates()
 {
-  int count = ui->selectPanelComboBox->count();
+  ui->mainTabWidget->setUpdatesEnabled(false);
+}
 
-  // 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
-  {
-    ++index;
-  }
+void MainWindow::enableUpdates()
+{
+  ui->mainTabWidget->setUpdatesEnabled(true);
+}
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+
+void MainWindow::clearTabs()
+{
+  ui->mainTabWidget->clear();
 }
 
 
-void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
+void MainWindow::addTab(
+  QWidget *page,
+  QString label)
+{
+  ui->mainTabWidget->addTab(page, label);
+}
+
+void MainWindow::setupTabs(
+  PIRTabBarName name)
 {
-  ui->stackedButtonsWidget->setCurrentIndex(index);
+  myPanels->setupTabs(name);
 }