4eb5ed3f3c0272d58105eb7c63be187ebc575da5
[pierogi] / pirselectdeviceform.cpp
1 #include "pirselectdeviceform.h"
2 #include "ui_pirselectdeviceform.h"
3 #include "pirkeysetwidgetitem.h"
4
5 PIRDeviceTypeMgr deviceTypeManager;
6
7 extern PIRMakeMgr makeManager;
8
9 PIRSelectDeviceForm::PIRSelectDeviceForm(
10   QWidget *parent)
11   : QWidget(parent),
12     ui(new Ui::PIRSelectDeviceForm),
13     currentMake(Any_Make),
14     currentDevice(Any_Device)
15 {
16   ui->setupUi(this);
17
18   setAttribute(Qt::WA_Maemo5StackedWindow);
19   setWindowFlags(windowFlags() | Qt::Window);
20
21   // push the list of makers into the make combo box:
22   makeManager.populateComboBox(ui->makeComboBox);
23   deviceTypeManager.populateComboBox(ui->deviceComboBox);
24
25   // Connection telling main window that keyset has been selected:
26   connect(
27     ui->deviceListWidget,
28     SIGNAL(itemActivated(QListWidgetItem *)),
29     parent,
30     SLOT(keysetSelectionChanged(QListWidgetItem *)),
31     Qt::QueuedConnection);
32
33   // Connection used to filter keyset list:
34   connect(
35     ui->makeComboBox,
36     SIGNAL(currentIndexChanged(int)),
37     this,
38     SLOT(filterListByMake(int)),
39     Qt::QueuedConnection);
40
41   connect(
42     ui->deviceComboBox,
43     SIGNAL(currentIndexChanged(int)),
44     this,
45     SLOT(filterListByDeviceType(int)),
46     Qt::QueuedConnection);
47 }
48
49 PIRSelectDeviceForm::~PIRSelectDeviceForm()
50 {
51   delete ui;
52 }
53
54 /*
55 void PIRSelectDeviceForm::addNameToList(
56   QString name,
57   unsigned int index,
58   PIRMakeName make)
59 {
60   ui->deviceListWidget->addItem(new PIRKeysetWidgetItem(name, index, make));
61 }
62 */
63
64 void PIRSelectDeviceForm::addWidgetItem(
65   PIRKeysetWidgetItem *kwi)
66 {
67   ui->deviceListWidget->addItem(kwi);
68 }
69
70 QListWidget *PIRSelectDeviceForm::getDeviceListWidget()
71 {
72   return ui->deviceListWidget;
73 }
74
75 void PIRSelectDeviceForm::filterListByMake(
76   int make)
77 {
78   currentMake = (PIRMakeName) make;
79   refilterList();
80 }
81
82 void PIRSelectDeviceForm::filterListByDeviceType(
83   int deviceType)
84 {
85   currentDevice = (PIRDeviceTypeName) deviceType;
86   refilterList();
87 }
88
89 void PIRSelectDeviceForm::refilterList()
90 {
91   int index = 0;
92   int count = ui->deviceListWidget->count();
93   PIRKeysetWidgetItem *item;
94   while (index < count)
95   {
96     item = dynamic_cast<PIRKeysetWidgetItem *>(
97       ui->deviceListWidget->item(index));
98
99     // Does the keylist have the required make?
100     if ((currentMake == Any_Make) || (item->getMake() == currentMake))
101     {
102       // And does it have the required type?
103       if ( (currentDevice == Any_Device)
104         || (item->getDeviceType() == currentDevice))
105      {
106         // Yes, we can show this keylist:
107         item->setHidden(false);
108       }
109       else
110       {
111         item->setHidden(true);
112       }
113     }
114     else
115     {
116       item->setHidden(true);
117     }
118
119     ++index;
120   }
121 }