Change back to real tabbed window, updates keysets
[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(
98   const QString &arg1)
99 {
100   filterListByString(arg1);
101 }
102
103
104 void PIRSelectDeviceForm::on_ssClosePushButton_clicked()
105 {
106   ui->searchStringLineEdit->hide();
107   ui->searchStringLineEdit->lower();
108   ui->ssClosePushButton->hide();
109   ui->searchStringLineEdit->clear();
110 }
111
112
113 void PIRSelectDeviceForm::filterListByMake(
114   int make)
115 {
116   currentMake = (PIRMakeName) make;
117   refilterList();
118 }
119
120
121 void PIRSelectDeviceForm::filterListByDeviceType(
122   int deviceType)
123 {
124   currentDevice = (PIRDeviceTypeName) deviceType;
125   refilterList();
126 }
127
128
129 void PIRSelectDeviceForm::filterListByString(
130   QString string)
131 {
132   searchString = string;
133   refilterList();
134 }
135
136
137 void PIRSelectDeviceForm::refilterList()
138 {
139   int index = 0;
140   int count = ui->deviceListWidget->count();
141   PIRKeysetWidgetItem *item;
142   while (index < count)
143   {
144     item = dynamic_cast<PIRKeysetWidgetItem *>(
145       ui->deviceListWidget->item(index));
146
147     // Does the keylist have the required make?
148     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
149     {
150       // And does it have the required type?
151       if ( (currentDevice == Any_Device)
152         || (item->getDeviceType() == currentDevice))
153      {
154         // Does it match the search string?
155         if ( searchString.isEmpty()
156           || item->text().contains(searchString, Qt::CaseInsensitive))
157         {
158           // Yes, we can show this keylist:
159           item->setHidden(false);
160         }
161         else
162         {
163           item->setHidden(true);
164         }
165       }
166       else
167       {
168         item->setHidden(true);
169       }
170     }
171     else
172     {
173       item->setHidden(true);
174     }
175
176     ++index;
177   }
178 }