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