Bugfixes and Keysets
[pierogi] / pirselectkeysetform.cpp
1 #include "pirselectkeysetform.h"
2 #include "ui_pirselectkeysetform.h"
3
4 //#include <QListWidget>
5 //#include <QListWidgetItem>
6 #include <QKeyEvent>
7
8 #include "mainwindow.h"
9 #include "pirkeysetwidgetitem.h"
10 #include "dialogs/pireditkeysetdialog.h"
11
12 // Debugging include:
13 //#include <iostream>
14
15 extern PIRMakeMgr makeManager;
16
17 PIRSelectKeysetForm::PIRSelectKeysetForm(
18   MainWindow *mw)
19   : QWidget(mw), // is this right?
20     ui(new Ui::PIRSelectKeysetForm),
21     mainWindow(mw),
22     editDialog(0),
23     showOnlyFavorites(false),
24     currentMake(Any_Make)
25 {
26   ui->setupUi(this);
27
28   // Make sure the user can only select one keyset at a time:
29   ui->keysetListWidget->setSelectionMode(
30     QAbstractItemView::SingleSelection);
31
32   // Don't want to start with the line editor visible:
33   ui->searchStringLineEdit->hide();
34   ui->searchStringLineEdit->lower();
35   ui->ssClosePushButton->hide();
36
37   // Set some initial flags:
38   setAttribute(Qt::WA_Maemo5StackedWindow);
39   setWindowFlags(windowFlags() | Qt::Window);
40
41   // push the list of makers into the make combo box:
42   makeManager.populateComboBox(ui->makeComboBox);
43
44   // Connection telling main window that keyset has been selected:
45   connect(
46     ui->keysetListWidget,
47     SIGNAL(itemActivated(QListWidgetItem *)),
48     mainWindow,
49     SLOT(keysetSelectionChanged(QListWidgetItem *)),
50     Qt::QueuedConnection);
51
52   // Connection used to filter keyset list:
53   connect(
54     ui->makeComboBox,
55     SIGNAL(currentIndexChanged(int)),
56     this,
57     SLOT(filterListByMake(int)),
58     Qt::QueuedConnection);
59
60   // Open editor dialog for indivual keysets:
61   connect(
62     ui->keysetListWidget,
63     SIGNAL(itemClicked(QListWidgetItem *)),
64     this,
65     SLOT(openKeysetDialog(QListWidgetItem *)),
66     Qt::QueuedConnection);
67
68   // Go ahead and construct the dialog window right now:
69   editDialog = new PIREditKeysetDialog(mainWindow);
70 }
71
72
73 PIRSelectKeysetForm::~PIRSelectKeysetForm()
74 {
75   delete ui;
76 }
77
78
79 /*
80 void PIRSelectKeysetForm::addNameToList(
81   QString name,
82   unsigned int index,
83   PIRMakeName make)
84 {
85   ui->keysetListWidget->addItem(new PIRKeysetWidgetItem(name, index, make));
86 }
87 */
88
89
90 void PIRSelectKeysetForm::addWidgetItem(
91   PIRKeysetWidgetItem *kwi)
92 {
93   ui->keysetListWidget->addItem(kwi);
94 }
95
96
97 QListWidget *PIRSelectKeysetForm::getKeysetListWidget()
98 {
99   return ui->keysetListWidget;
100 }
101
102
103 bool PIRSelectKeysetForm::selectNextKeyset()
104 {
105   int currentRow = ui->keysetListWidget->currentRow();
106
107   // If we're at the end of the list, give up:
108   if (currentRow >= (ui->keysetListWidget->count() -1))
109   {
110     return false;
111   }
112
113   ui->keysetListWidget->setCurrentRow(
114     currentRow + 1,
115     QItemSelectionModel::ClearAndSelect);
116
117   mainWindow->keysetSelectionChanged(
118     ui->keysetListWidget->currentItem());
119
120   return true;
121 }
122
123
124 bool PIRSelectKeysetForm::selectPrevKeyset()
125 {
126   int currentRow = ui->keysetListWidget->currentRow();
127
128   // If we're at the beginning of the list, give up:
129   if (currentRow <= 0)
130   {
131     return false;
132   }
133
134   ui->keysetListWidget->setCurrentRow(
135     currentRow - 1,
136     QItemSelectionModel::ClearAndSelect);
137
138   mainWindow->keysetSelectionChanged(
139     ui->keysetListWidget->currentItem());
140
141   return true;
142 }
143
144
145 bool PIRSelectKeysetForm::selectFirstKeyset()
146 {
147   if (ui->keysetListWidget->count() == 0)
148   {
149     return false;
150   }
151
152   if (ui->keysetListWidget->currentRow() != 0)
153   {
154     ui->keysetListWidget->setCurrentRow(
155       0, QItemSelectionModel::ClearAndSelect);
156
157     mainWindow->keysetSelectionChanged(
158       ui->keysetListWidget->currentItem());
159   }
160
161   return true;
162 }
163
164
165 QString PIRSelectKeysetForm::getKeysetName()
166 {
167   QListWidgetItem *item = ui->keysetListWidget->currentItem();
168
169   if (item)
170   {
171     return item->text();
172   }
173   else
174   {
175     return "";
176   }
177 }
178
179
180 void PIRSelectKeysetForm::keyPressEvent(
181   QKeyEvent *event)
182 {
183   ui->searchStringLineEdit->show();
184   ui->searchStringLineEdit->raise();
185   ui->ssClosePushButton->show();
186
187   ui->searchStringLineEdit->setText(event->text());
188   ui->searchStringLineEdit->setFocus();
189 }
190
191
192 void PIRSelectKeysetForm::on_searchStringLineEdit_textChanged(
193   const QString &arg1)
194 {
195   filterListByString(arg1);
196 }
197
198
199 void PIRSelectKeysetForm::on_ssClosePushButton_clicked()
200 {
201   ui->searchStringLineEdit->hide();
202   ui->searchStringLineEdit->lower();
203   ui->ssClosePushButton->hide();
204   ui->searchStringLineEdit->clear();
205 }
206
207
208 void PIRSelectKeysetForm::filterListByMake(
209   int make)
210 {
211   currentMake = (PIRMakeName) make;
212   refilterList();
213 }
214
215
216 void PIRSelectKeysetForm::filterListByString(
217   QString string)
218 {
219   searchString = string;
220   refilterList();
221 }
222
223
224 void PIRSelectKeysetForm::refilterList()
225 {
226   int index = 0;
227   int count = ui->keysetListWidget->count();
228   PIRKeysetWidgetItem *item;
229   while (index < count)
230   {
231     item = dynamic_cast<PIRKeysetWidgetItem *>(
232       ui->keysetListWidget->item(index));
233
234     // Does the keylist have the required make?
235     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
236     {
237       // If required, is the keyset a favorite?
238       if (!showOnlyFavorites || (item->isFavorite()))
239       {
240         // Does this keylist match the search string?
241         if ( searchString.isEmpty()
242           || item->text().contains(searchString, Qt::CaseInsensitive))
243         {
244           // Yes, we can show this keylist:
245           item->setHidden(false);
246         }
247         else
248         {
249           item->setHidden(true);
250         }
251       }
252       else
253       {
254         item->setHidden(true);
255       }
256     }
257     else
258     {
259       item->setHidden(true);
260     }
261
262     ++index;
263   }
264 }
265
266
267 void PIRSelectKeysetForm::openKeysetDialog(
268   QListWidgetItem *item)
269 {
270   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
271
272   editDialog->setupDialog(kwi);
273
274   editDialog->exec();
275 }
276
277
278 void PIRSelectKeysetForm::openCurrentKeysetDialog()
279 {
280   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *> (
281     ui->keysetListWidget->currentItem());
282
283   editDialog->setupDialog(kwi);
284
285   editDialog->exec();
286 }
287
288
289 void PIRSelectKeysetForm::on_showFavoritesCheckBox_toggled(bool checked)
290 {
291   showOnlyFavorites = checked;
292   refilterList();
293 }
294
295
296 void PIRSelectKeysetForm::selectKeyset(
297   unsigned int targetID)
298 {
299   int count = ui->keysetListWidget->count();
300
301   if (count == 0)
302   {
303     return;
304   }
305
306   QListWidgetItem *localItem;
307   PIRKeysetWidgetItem *kwi;
308   int row = 0;
309
310   while (row < count)
311   {
312     localItem = ui->keysetListWidget->item(row);
313
314     if (localItem)
315     {
316       kwi = dynamic_cast<PIRKeysetWidgetItem *> (localItem);
317
318       if (kwi->getID() == targetID)
319       {
320         ui->keysetListWidget->setCurrentRow(
321           row, QItemSelectionModel::ClearAndSelect);
322
323         mainWindow->keysetSelectionChanged(
324           ui->keysetListWidget->currentItem());
325
326         return;
327       }
328     }
329
330     ++row;
331   }
332 }