Missed one file
[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 QString PIRSelectKeysetForm::getKeysetName()
146 {
147   QListWidgetItem *item = ui->keysetListWidget->currentItem();
148
149   if (item)
150   {
151     return item->text();
152   }
153   else
154   {
155     return "";
156   }
157 }
158
159
160 void PIRSelectKeysetForm::keyPressEvent(
161   QKeyEvent *event)
162 {
163   ui->searchStringLineEdit->show();
164   ui->searchStringLineEdit->raise();
165   ui->ssClosePushButton->show();
166
167   ui->searchStringLineEdit->setText(event->text());
168   ui->searchStringLineEdit->setFocus();
169 }
170
171
172 void PIRSelectKeysetForm::on_searchStringLineEdit_textChanged(
173   const QString &arg1)
174 {
175   filterListByString(arg1);
176 }
177
178
179 void PIRSelectKeysetForm::on_ssClosePushButton_clicked()
180 {
181   ui->searchStringLineEdit->hide();
182   ui->searchStringLineEdit->lower();
183   ui->ssClosePushButton->hide();
184   ui->searchStringLineEdit->clear();
185 }
186
187
188 void PIRSelectKeysetForm::filterListByMake(
189   int make)
190 {
191   currentMake = (PIRMakeName) make;
192   refilterList();
193 }
194
195
196 void PIRSelectKeysetForm::filterListByString(
197   QString string)
198 {
199   searchString = string;
200   refilterList();
201 }
202
203
204 void PIRSelectKeysetForm::refilterList()
205 {
206   int index = 0;
207   int count = ui->keysetListWidget->count();
208   PIRKeysetWidgetItem *item;
209   while (index < count)
210   {
211     item = dynamic_cast<PIRKeysetWidgetItem *>(
212       ui->keysetListWidget->item(index));
213
214     // Does the keylist have the required make?
215     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
216     {
217       // If required, is the keyset a favorite?
218       if (!showOnlyFavorites || (item->isFavorite()))
219       {
220         // Does this keylist match the search string?
221         if ( searchString.isEmpty()
222           || item->text().contains(searchString, Qt::CaseInsensitive))
223         {
224           // Yes, we can show this keylist:
225           item->setHidden(false);
226         }
227         else
228         {
229           item->setHidden(true);
230         }
231       }
232       else
233       {
234         item->setHidden(true);
235       }
236     }
237     else
238     {
239       item->setHidden(true);
240     }
241
242     ++index;
243   }
244 }
245
246
247 void PIRSelectKeysetForm::openKeysetDialog(
248   QListWidgetItem *item)
249 {
250   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
251
252   editDialog->setupDialog(kwi);
253
254   editDialog->exec();
255 }
256
257
258 void PIRSelectKeysetForm::on_showFavoritesCheckBox_toggled(bool checked)
259 {
260   showOnlyFavorites = checked;
261   refilterList();
262 }