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