Advanced Settings Panel
[pierogi] / pirselectkeysetform.cpp
index e678686..69501c1 100644 (file)
@@ -1,31 +1,52 @@
 #include "pirselectkeysetform.h"
 #include "ui_pirselectkeysetform.h"
+
+//#include <QListWidget>
+//#include <QListWidgetItem>
+#include <QKeyEvent>
+#include <QComboBox>
+
+#include "mainwindow.h"
 #include "pirkeysetwidgetitem.h"
+#include "dialogs/pireditkeysetdialog.h"
+
+// Debugging include:
+//#include <iostream>
 
 extern PIRMakeMgr makeManager;
-extern PIRDeviceTypeMgr deviceManager;
 
 PIRSelectKeysetForm::PIRSelectKeysetForm(
-  QWidget *parent)
-  : QWidget(parent),
+  MainWindow *mw)
+  : QWidget(mw), // is this right?
     ui(new Ui::PIRSelectKeysetForm),
-    currentMake(Any_Make),
-    currentDevice(Any_Device)
+    mainWindow(mw),
+    editDialog(0),
+    showOnlyFavorites(false),
+    currentMake(Any_Make)
 {
   ui->setupUi(this);
 
+  // Make sure the user can only select one keyset at a time:
+  ui->keysetListWidget->setSelectionMode(
+    QAbstractItemView::SingleSelection);
+
+  // Don't want to start with the line editor visible:
+  ui->searchStringLineEdit->hide();
+  ui->searchStringLineEdit->lower();
+  ui->ssClosePushButton->hide();
+
+  // Set some initial flags:
   setAttribute(Qt::WA_Maemo5StackedWindow);
   setWindowFlags(windowFlags() | Qt::Window);
 
   // push the list of makers into the make combo box:
   makeManager.populateComboBox(ui->makeComboBox);
-  deviceManager.populateComboBox(ui->deviceComboBox);
 
   // Connection telling main window that keyset has been selected:
   connect(
     ui->keysetListWidget,
     SIGNAL(itemActivated(QListWidgetItem *)),
-    parent,
+    mainWindow,
     SLOT(keysetSelectionChanged(QListWidgetItem *)),
     Qt::QueuedConnection);
 
@@ -37,19 +58,26 @@ PIRSelectKeysetForm::PIRSelectKeysetForm(
     SLOT(filterListByMake(int)),
     Qt::QueuedConnection);
 
+  // Open editor dialog for indivual keysets:
   connect(
-    ui->deviceComboBox,
-    SIGNAL(currentIndexChanged(int)),
+    ui->keysetListWidget,
+    SIGNAL(itemClicked(QListWidgetItem *)),
     this,
-    SLOT(filterListByDeviceType(int)),
+    SLOT(openKeysetDialog(QListWidgetItem *)),
     Qt::QueuedConnection);
+
+  // Go ahead and construct the dialog window right now:
+  editDialog = new PIREditKeysetDialog(mainWindow);
 }
 
+
 PIRSelectKeysetForm::~PIRSelectKeysetForm()
 {
   delete ui;
 }
 
+
+/*
 void PIRSelectKeysetForm::addNameToList(
   QString name,
   unsigned int index,
@@ -57,6 +85,8 @@ void PIRSelectKeysetForm::addNameToList(
 {
   ui->keysetListWidget->addItem(new PIRKeysetWidgetItem(name, index, make));
 }
+*/
+
 
 void PIRSelectKeysetForm::addWidgetItem(
   PIRKeysetWidgetItem *kwi)
@@ -64,11 +94,150 @@ void PIRSelectKeysetForm::addWidgetItem(
   ui->keysetListWidget->addItem(kwi);
 }
 
+
 QListWidget *PIRSelectKeysetForm::getKeysetListWidget()
 {
   return ui->keysetListWidget;
 }
 
+
+bool PIRSelectKeysetForm::selectNextKeyset()
+{
+  int currentRow = ui->keysetListWidget->currentRow();
+
+  // If we're at the end of the list, give up:
+  if (currentRow >= (ui->keysetListWidget->count() -1))
+  {
+    return false;
+  }
+
+  ui->keysetListWidget->setCurrentRow(
+    currentRow + 1,
+    QItemSelectionModel::ClearAndSelect);
+
+  mainWindow->keysetSelectionChanged(
+    ui->keysetListWidget->currentItem());
+
+  return true;
+}
+
+
+bool PIRSelectKeysetForm::selectPrevKeyset()
+{
+  int currentRow = ui->keysetListWidget->currentRow();
+
+  // If we're at the beginning of the list, give up:
+  if (currentRow <= 0)
+  {
+    return false;
+  }
+
+  ui->keysetListWidget->setCurrentRow(
+    currentRow - 1,
+    QItemSelectionModel::ClearAndSelect);
+
+  mainWindow->keysetSelectionChanged(
+    ui->keysetListWidget->currentItem());
+
+  return true;
+}
+
+
+bool PIRSelectKeysetForm::selectFirstKeyset()
+{
+  if (ui->keysetListWidget->count() == 0)
+  {
+    return false;
+  }
+
+  if (ui->keysetListWidget->currentRow() != 0)
+  {
+    ui->keysetListWidget->setCurrentRow(
+      0, QItemSelectionModel::ClearAndSelect);
+
+    mainWindow->keysetSelectionChanged(
+      ui->keysetListWidget->currentItem());
+  }
+
+  return true;
+}
+
+
+QString PIRSelectKeysetForm::getCurrentKeysetName()
+{
+  QListWidgetItem *item = ui->keysetListWidget->currentItem();
+
+  if (item)
+  {
+    return item->text();
+  }
+  else
+  {
+    return "";
+  }
+}
+
+
+QString PIRSelectKeysetForm::getKeysetName(
+  unsigned int id)
+{
+  int count = ui->keysetListWidget->count();
+
+  if (count == 0) return "";
+
+  QListWidgetItem *localItem;
+  PIRKeysetWidgetItem *kwi;
+  int row = 0;
+
+  while (row < count)
+  {
+    localItem = ui->keysetListWidget->item(row);
+
+    if (localItem)
+    {
+      kwi = dynamic_cast<PIRKeysetWidgetItem *> (localItem);
+
+      if (kwi->getID() == id)
+      {
+        return kwi->text();
+      }
+    }
+
+    ++row;
+  }
+
+  return "";
+}
+
+
+void PIRSelectKeysetForm::keyPressEvent(
+  QKeyEvent *event)
+{
+  ui->searchStringLineEdit->show();
+  ui->searchStringLineEdit->raise();
+  ui->ssClosePushButton->show();
+
+  ui->searchStringLineEdit->setText(event->text());
+  ui->searchStringLineEdit->setFocus();
+}
+
+
+void PIRSelectKeysetForm::on_searchStringLineEdit_textChanged(
+  const QString &arg1)
+{
+  filterListByString(arg1);
+}
+
+
+void PIRSelectKeysetForm::on_ssClosePushButton_clicked()
+{
+  ui->searchStringLineEdit->hide();
+  ui->searchStringLineEdit->lower();
+  ui->ssClosePushButton->hide();
+  ui->searchStringLineEdit->clear();
+}
+
+
 void PIRSelectKeysetForm::filterListByMake(
   int make)
 {
@@ -76,13 +245,15 @@ void PIRSelectKeysetForm::filterListByMake(
   refilterList();
 }
 
-void PIRSelectKeysetForm::filterListByDeviceType(
-  int deviceType)
+
+void PIRSelectKeysetForm::filterListByString(
+  QString string)
 {
-  currentDevice = (PIRDeviceTypeName) deviceType;
+  searchString = string;
   refilterList();
 }
 
+
 void PIRSelectKeysetForm::refilterList()
 {
   int index = 0;
@@ -96,12 +267,20 @@ void PIRSelectKeysetForm::refilterList()
     // Does the keylist have the required make?
     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
     {
-      // And, does the keylist have the required device type?
-      if ((currentDevice == Any_Device)
-          || (item->supportsDeviceType(currentDevice)))
+      // If required, is the keyset a favorite?
+      if (!showOnlyFavorites || (item->isFavorite()))
       {
-        // Yes, we can show this keylist:
-        item->setHidden(false);
+        // Does this keylist match the search string?
+        if ( searchString.isEmpty()
+          || item->text().contains(searchString, Qt::CaseInsensitive))
+        {
+          // Yes, we can show this keylist:
+          item->setHidden(false);
+        }
+        else
+        {
+          item->setHidden(true);
+        }
       }
       else
       {
@@ -116,3 +295,97 @@ void PIRSelectKeysetForm::refilterList()
     ++index;
   }
 }
+
+
+void PIRSelectKeysetForm::openKeysetDialog(
+  QListWidgetItem *item)
+{
+  PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
+
+  editDialog->setupDialog(kwi);
+
+  editDialog->exec();
+}
+
+
+void PIRSelectKeysetForm::openCurrentKeysetDialog()
+{
+  PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *> (
+    ui->keysetListWidget->currentItem());
+
+  editDialog->setupDialog(kwi);
+
+  editDialog->exec();
+}
+
+
+void PIRSelectKeysetForm::on_showFavoritesCheckBox_toggled(bool checked)
+{
+  showOnlyFavorites = checked;
+  refilterList();
+}
+
+
+void PIRSelectKeysetForm::selectKeyset(
+  unsigned int targetID)
+{
+  int count = ui->keysetListWidget->count();
+
+  if (count == 0) return;
+
+  QListWidgetItem *localItem;
+  PIRKeysetWidgetItem *kwi;
+  int row = 0;
+
+  while (row < count)
+  {
+    localItem = ui->keysetListWidget->item(row);
+
+    if (localItem)
+    {
+      kwi = dynamic_cast<PIRKeysetWidgetItem *> (localItem);
+
+      if (kwi->getID() == targetID)
+      {
+        ui->keysetListWidget->setCurrentRow(
+          row, QItemSelectionModel::ClearAndSelect);
+
+        mainWindow->keysetSelectionChanged(
+          ui->keysetListWidget->currentItem());
+
+        return;
+      }
+    }
+
+    ++row;
+  }
+}
+
+
+void PIRSelectKeysetForm::populateKeysetComboBox(
+  QComboBox *comboBox)
+{
+  int count = ui->keysetListWidget->count();
+
+  if (count == 0) return;
+
+  QListWidgetItem *localItem;
+  PIRKeysetWidgetItem *kwi;
+  int row = 0;
+
+  while (row < count)
+  {
+    localItem = ui->keysetListWidget->item(row);
+
+    if (localItem)
+    {
+      kwi = dynamic_cast<PIRKeysetWidgetItem *> (localItem);
+
+      comboBox->addItem(
+        kwi->text(),
+        QVariant(kwi->getID()));
+    }
+
+    ++row;
+  }
+}