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