Missed one file
[pierogi] / pirselectkeysetform.cpp
index 67c9e61..e191da9 100644 (file)
@@ -1,18 +1,40 @@
 #include "pirselectkeysetform.h"
 #include "ui_pirselectkeysetform.h"
+
+//#include <QListWidget>
+//#include <QListWidgetItem>
+#include <QKeyEvent>
+
+#include "mainwindow.h"
 #include "pirkeysetwidgetitem.h"
-#include <QListWidget>
+#include "dialogs/pireditkeysetdialog.h"
+
+// Debugging include:
+//#include <iostream>
 
 extern PIRMakeMgr makeManager;
 
 PIRSelectKeysetForm::PIRSelectKeysetForm(
-  QWidget *parent)
-  : QWidget(parent),
+  MainWindow *mw)
+  : QWidget(mw), // is this right?
     ui(new Ui::PIRSelectKeysetForm),
+    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);
 
@@ -23,7 +45,7 @@ PIRSelectKeysetForm::PIRSelectKeysetForm(
   connect(
     ui->keysetListWidget,
     SIGNAL(itemActivated(QListWidgetItem *)),
-    parent,
+    mainWindow,
     SLOT(keysetSelectionChanged(QListWidgetItem *)),
     Qt::QueuedConnection);
 
@@ -34,13 +56,26 @@ PIRSelectKeysetForm::PIRSelectKeysetForm(
     this,
     SLOT(filterListByMake(int)),
     Qt::QueuedConnection);
+
+  // Open editor dialog for indivual keysets:
+  connect(
+    ui->keysetListWidget,
+    SIGNAL(itemClicked(QListWidgetItem *)),
+    this,
+    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,
@@ -51,17 +86,105 @@ void PIRSelectKeysetForm::addNameToList(
 }
 */
 
+
 void PIRSelectKeysetForm::addWidgetItem(
   PIRKeysetWidgetItem *kwi)
 {
   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;
+}
+
+
+QString PIRSelectKeysetForm::getKeysetName()
+{
+  QListWidgetItem *item = ui->keysetListWidget->currentItem();
+
+  if (item)
+  {
+    return item->text();
+  }
+  else
+  {
+    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)
 {
@@ -69,6 +192,15 @@ void PIRSelectKeysetForm::filterListByMake(
   refilterList();
 }
 
+
+void PIRSelectKeysetForm::filterListByString(
+  QString string)
+{
+  searchString = string;
+  refilterList();
+}
+
+
 void PIRSelectKeysetForm::refilterList()
 {
   int index = 0;
@@ -82,8 +214,25 @@ void PIRSelectKeysetForm::refilterList()
     // Does the keylist have the required make?
     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
     {
-      // Yes, we can show this keylist:
-      item->setHidden(false);
+      // If required, is the keyset a favorite?
+      if (!showOnlyFavorites || (item->isFavorite()))
+      {
+        // 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
+      {
+        item->setHidden(true);
+      }
     }
     else
     {
@@ -93,3 +242,21 @@ void PIRSelectKeysetForm::refilterList()
     ++index;
   }
 }
+
+
+void PIRSelectKeysetForm::openKeysetDialog(
+  QListWidgetItem *item)
+{
+  PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
+
+  editDialog->setupDialog(kwi);
+
+  editDialog->exec();
+}
+
+
+void PIRSelectKeysetForm::on_showFavoritesCheckBox_toggled(bool checked)
+{
+  showOnlyFavorites = checked;
+  refilterList();
+}