8d6f403efa63b6b234bae66ec47d0cceb73d5c43
[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   // Don't want to start with the line editor visible:
29   ui->searchStringLineEdit->hide();
30   ui->searchStringLineEdit->lower();
31   ui->ssClosePushButton->hide();
32
33   // Set some initial flags:
34   setAttribute(Qt::WA_Maemo5StackedWindow);
35   setWindowFlags(windowFlags() | Qt::Window);
36
37   // push the list of makers into the make combo box:
38   makeManager.populateComboBox(ui->makeComboBox);
39
40   // Connection telling main window that keyset has been selected:
41   connect(
42     ui->keysetListWidget,
43     SIGNAL(itemActivated(QListWidgetItem *)),
44     mainWindow,
45     SLOT(keysetSelectionChanged(QListWidgetItem *)),
46     Qt::QueuedConnection);
47
48   // Connection used to filter keyset list:
49   connect(
50     ui->makeComboBox,
51     SIGNAL(currentIndexChanged(int)),
52     this,
53     SLOT(filterListByMake(int)),
54     Qt::QueuedConnection);
55
56   // Open editor dialog for indivual keysets:
57   connect(
58     ui->keysetListWidget,
59     SIGNAL(itemClicked(QListWidgetItem *)),
60     this,
61     SLOT(openKeysetDialog(QListWidgetItem *)),
62     Qt::QueuedConnection);
63
64   // Go ahead and construct the dialog window right now:
65   editDialog = new PIREditKeysetDialog(mainWindow);
66 }
67
68
69 PIRSelectKeysetForm::~PIRSelectKeysetForm()
70 {
71   delete ui;
72 }
73
74
75 /*
76 void PIRSelectKeysetForm::addNameToList(
77   QString name,
78   unsigned int index,
79   PIRMakeName make)
80 {
81   ui->keysetListWidget->addItem(new PIRKeysetWidgetItem(name, index, make));
82 }
83 */
84
85
86 void PIRSelectKeysetForm::addWidgetItem(
87   PIRKeysetWidgetItem *kwi)
88 {
89   ui->keysetListWidget->addItem(kwi);
90 }
91
92
93 QListWidget *PIRSelectKeysetForm::getKeysetListWidget()
94 {
95   return ui->keysetListWidget;
96 }
97
98
99 void PIRSelectKeysetForm::keyPressEvent(
100   QKeyEvent *event)
101 {
102   ui->searchStringLineEdit->show();
103   ui->searchStringLineEdit->raise();
104   ui->ssClosePushButton->show();
105
106   ui->searchStringLineEdit->setText(event->text());
107   ui->searchStringLineEdit->setFocus();
108 }
109
110
111 void PIRSelectKeysetForm::on_searchStringLineEdit_textChanged(
112   const QString &arg1)
113 {
114   filterListByString(arg1);
115 }
116
117
118 void PIRSelectKeysetForm::on_ssClosePushButton_clicked()
119 {
120   ui->searchStringLineEdit->hide();
121   ui->searchStringLineEdit->lower();
122   ui->ssClosePushButton->hide();
123   ui->searchStringLineEdit->clear();
124 }
125
126
127 void PIRSelectKeysetForm::filterListByMake(
128   int make)
129 {
130   currentMake = (PIRMakeName) make;
131   refilterList();
132 }
133
134
135 void PIRSelectKeysetForm::filterListByString(
136   QString string)
137 {
138   searchString = string;
139   refilterList();
140 }
141
142
143 void PIRSelectKeysetForm::refilterList()
144 {
145   int index = 0;
146   int count = ui->keysetListWidget->count();
147   PIRKeysetWidgetItem *item;
148   while (index < count)
149   {
150     item = dynamic_cast<PIRKeysetWidgetItem *>(
151       ui->keysetListWidget->item(index));
152
153     // Does the keylist have the required make?
154     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
155     {
156       // If required, is the keyset a favorite?
157       if (!showOnlyFavorites || (item->isFavorite()))
158       {
159         // Does this keylist match the search string?
160         if ( searchString.isEmpty()
161           || item->text().contains(searchString, Qt::CaseInsensitive))
162         {
163           // Yes, we can show this keylist:
164           item->setHidden(false);
165         }
166         else
167         {
168           item->setHidden(true);
169         }
170       }
171       else
172       {
173         item->setHidden(true);
174       }
175     }
176     else
177     {
178       item->setHidden(true);
179     }
180
181     ++index;
182   }
183 }
184
185
186 void PIRSelectKeysetForm::openKeysetDialog(
187   QListWidgetItem *item)
188 {
189   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
190
191   editDialog->setupDialog(kwi);
192
193   editDialog->exec();
194 }
195
196
197 void PIRSelectKeysetForm::on_showFavoritesCheckBox_toggled(bool checked)
198 {
199   showOnlyFavorites = checked;
200   refilterList();
201 }