Change back to real tabbed window, updates keysets
[pierogi] / mainwindow.cpp
index 692b64c..78744b4 100644 (file)
@@ -4,19 +4,27 @@
 #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 "pirkeysetmanager.h"
 #include "pirpanelmanager.h"
 
 //#define DEBUGGING
+//#include <iostream>
 
 // Some ugly globals used for thread communications:
 
@@ -38,10 +46,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 +62,25 @@ MainWindow::MainWindow(QWidget *parent)
   myKeysets = new PIRKeysetManager();
   myPanels = new PIRPanelManager(this);
 
-  // Set up the keyset selection window:
+  // Display the panels:
+  myPanels->updateTabSet();
+
+  // Add the corner button:
+  insertCornerButton();
+
+  // Construct the rest of the forms:
   selectKeysetForm = new PIRSelectKeysetForm(this);
+  myKeysets->populateSelectionWidget(selectKeysetForm);
 
-  // Set up the device selection window:
   selectDeviceForm = new PIRSelectDeviceForm(this);
+  PIRKeysetMetaData::populateDevices(selectDeviceForm);
 
-  // Set up the panel selection window:
-  panelSelectionForm = new PIRPanelSelectionForm(this);
-
-  myKeysets->populateSelectionWidgets(selectKeysetForm, selectDeviceForm);
-//  myPanels->setupPanels(panelSelectionForm);
+  preferencesForm = new PIRPreferencesForm(this, myKeysets);
 
   // 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,7 +88,6 @@ MainWindow::MainWindow(QWidget *parent)
       settings.value("currentKeysetMake").toString(),
       settings.value("currentKeysetName").toString(),
       currentKeyset);
-//    currentKeyset = settings.value("currentKeyset").toInt();
   }
 
   enableButtons();
@@ -163,7 +175,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 +242,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 +308,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 +354,6 @@ void MainWindow::keysetSelectionChanged(
   currentKeyset = kwi->getID();
 
   QSettings settings("pietrzak.org", "Pierogi");
-//  settings.setValue("currentKeyset", currentKeyset);
 
   settings.setValue(
     "currentKeysetMake",
@@ -362,7 +413,6 @@ void MainWindow::addCurrentKeyset(
 
   settings.beginWriteArray("favorites");
   settings.setArrayIndex(favSettingsSize);
-//  settings.setValue("keysetID", currentKeyset);
 
   settings.setValue(
     "keysetMake",
@@ -405,7 +455,6 @@ void MainWindow::removeFavoriteKeyset(
     kwi = dynamic_cast<PIRKeysetWidgetItem *>(qlw->item(index));
 
     settings.setArrayIndex(index);
-//    settings.setValue("keysetID", kwi->getID());
     id = kwi->getID();
 
     settings.setValue(
@@ -464,6 +513,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);
@@ -483,76 +545,55 @@ void MainWindow::selectNextFavKeyset()
 }
 
 
-void MainWindow::managePanel(
-  PIRPanelName name,
-  int state)
+void MainWindow::insertCornerButton()
 {
-  myPanels->managePanel(name, state);
-}
+  // Set up the dialog box:
+  PIRTabsChoiceDialog *tcd = new PIRTabsChoiceDialog(this);
 
+  // Next, set up the corner button itself:
+  QPushButton *button =
+    new QPushButton(QIcon(":/icons/align_just_icon&32.png"), "");
 
-void MainWindow::insertPanel(
-  int index,
-  QWidget *panel,
-  const QString &displayName)
-{
-  ui->stackedButtonsWidget->insertWidget(index, panel);
-  ui->selectPanelComboBox->insertItem(index, displayName);
-}
+  button->setFlat(true);
 
+  connect(
+    button,
+    SIGNAL(clicked()),
+    tcd,
+    SLOT(exec()),
+    Qt::QueuedConnection);
 
-void MainWindow::removePanel(
-  int index,
-  QWidget *panel)
-{
-  ui->stackedButtonsWidget->removeWidget(panel);
-  ui->selectPanelComboBox->removeItem(index);
+  ui->mainTabWidget->setCornerWidget(button);
 }
 
 
-void MainWindow::on_prevPanelButton_clicked()
+void MainWindow::disableUpdates()
 {
-  int count = ui->selectPanelComboBox->count();
-
-  // No need to do anything if there are less than two panels available:
-  if (count < 2) return;
+  ui->mainTabWidget->setUpdatesEnabled(false);
+}
 
-  int index = ui->selectPanelComboBox->currentIndex();
-  if (index == 0)
-  {
-    index = count - 1;
-  }
-  else
-  {
-    --index;
-  }
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+void MainWindow::enableUpdates()
+{
+  ui->mainTabWidget->setUpdatesEnabled(true);
 }
 
 
-void MainWindow::on_nextPanelButton_clicked()
+void MainWindow::clearTabs()
 {
-  int count = ui->selectPanelComboBox->count();
-
-  // No need to do anything if there are less than two panels available:
-  if (count < 2) return;
+  ui->mainTabWidget->clear();
+}
 
-  int index = ui->selectPanelComboBox->currentIndex();
-  if (index == count - 1)
-  {
-    index = 0;
-  }
-  else
-  {
-    ++index;
-  }
 
-  ui->selectPanelComboBox->setCurrentIndex(index);
+void MainWindow::addTab(
+  QWidget *page,
+  QString label)
+{
+  ui->mainTabWidget->addTab(page, label);
 }
 
-
-void MainWindow::on_selectPanelComboBox_currentIndexChanged(int index)
+void MainWindow::setupTabs(
+  PIRTabBarName name)
 {
-  ui->stackedButtonsWidget->setCurrentIndex(index);
+  myPanels->setupTabs(name);
 }