39ac64527d92760a1154df91208e2de1959fcf23
[pierogi] / pirselectkeysetform.cpp
1 #include "pirselectkeysetform.h"
2 #include "ui_pirselectkeysetform.h"
3 #include "pirkeysetwidgetitem.h"
4 //#include <QListWidget>
5 #include <QKeyEvent>
6
7 extern PIRMakeMgr makeManager;
8
9 PIRSelectKeysetForm::PIRSelectKeysetForm(
10   QWidget *parent)
11   : QWidget(parent),
12     ui(new Ui::PIRSelectKeysetForm),
13     currentMake(Any_Make)
14 {
15   ui->setupUi(this);
16
17   // Don't want to start with the line editor visible:
18   ui->searchStringLineEdit->hide();
19   ui->searchStringLineEdit->lower();
20   ui->ssClosePushButton->hide();
21
22   // Set some initial flags:
23   setAttribute(Qt::WA_Maemo5StackedWindow);
24   setWindowFlags(windowFlags() | Qt::Window);
25
26   // push the list of makers into the make combo box:
27   makeManager.populateComboBox(ui->makeComboBox);
28
29   // Connection telling main window that keyset has been selected:
30   connect(
31     ui->keysetListWidget,
32     SIGNAL(itemActivated(QListWidgetItem *)),
33     parent,
34     SLOT(keysetSelectionChanged(QListWidgetItem *)),
35     Qt::QueuedConnection);
36
37   // Connection used to filter keyset list:
38   connect(
39     ui->makeComboBox,
40     SIGNAL(currentIndexChanged(int)),
41     this,
42     SLOT(filterListByMake(int)),
43     Qt::QueuedConnection);
44 }
45
46
47 PIRSelectKeysetForm::~PIRSelectKeysetForm()
48 {
49   delete ui;
50 }
51
52
53 /*
54 void PIRSelectKeysetForm::addNameToList(
55   QString name,
56   unsigned int index,
57   PIRMakeName make)
58 {
59   ui->keysetListWidget->addItem(new PIRKeysetWidgetItem(name, index, make));
60 }
61 */
62
63
64 void PIRSelectKeysetForm::addWidgetItem(
65   PIRKeysetWidgetItem *kwi)
66 {
67   ui->keysetListWidget->addItem(kwi);
68 }
69
70
71 QListWidget *PIRSelectKeysetForm::getKeysetListWidget()
72 {
73   return ui->keysetListWidget;
74 }
75
76
77 void PIRSelectKeysetForm::keyPressEvent(
78   QKeyEvent *event)
79 {
80   ui->searchStringLineEdit->show();
81   ui->searchStringLineEdit->raise();
82   ui->ssClosePushButton->show();
83
84   ui->searchStringLineEdit->setText(event->text());
85   ui->searchStringLineEdit->setFocus();
86 }
87
88
89 void PIRSelectKeysetForm::on_searchStringLineEdit_textChanged(
90   const QString &arg1)
91 {
92   filterListByString(arg1);
93 }
94
95
96 void PIRSelectKeysetForm::on_ssClosePushButton_clicked()
97 {
98   ui->searchStringLineEdit->hide();
99   ui->searchStringLineEdit->lower();
100   ui->ssClosePushButton->hide();
101   ui->searchStringLineEdit->clear();
102 }
103
104
105 void PIRSelectKeysetForm::filterListByMake(
106   int make)
107 {
108   currentMake = (PIRMakeName) make;
109   refilterList();
110 }
111
112
113 void PIRSelectKeysetForm::filterListByString(
114   QString string)
115 {
116   searchString = string;
117   refilterList();
118 }
119
120
121 void PIRSelectKeysetForm::refilterList()
122 {
123   int index = 0;
124   int count = ui->keysetListWidget->count();
125   PIRKeysetWidgetItem *item;
126   while (index < count)
127   {
128     item = dynamic_cast<PIRKeysetWidgetItem *>(
129       ui->keysetListWidget->item(index));
130
131     // Does the keylist have the required make?
132     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
133     {
134       // Does this keylist match the search string?
135       if ( searchString.isEmpty()
136         || item->text().contains(searchString, Qt::CaseInsensitive))
137       {
138         // Yes, we can show this keylist:
139         item->setHidden(false);
140       }
141       else
142       {
143         item->setHidden(true);
144       }
145     }
146     else
147     {
148       item->setHidden(true);
149     }
150
151     ++index;
152   }
153 }