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