Advanced Settings Panel
[pierogi] / dialogs / pirfavoritesdialog.cpp
1 #include "pirfavoritesdialog.h"
2 #include "ui_pirfavoritesdialog.h"
3
4 //#include <QListWidget>
5
6 #include "mainwindow.h"
7 #include "pirkeysetwidgetitem.h"
8 #include <QMaemo5InformationBox>
9
10 /*
11 PIRFavoritesDialog::PIRFavoritesDialog(QWidget *parent) :
12   QDialog(parent),
13   ui(new Ui::PIRFavoritesDialog)
14 {
15   ui->setupUi(this);
16 }
17 */
18
19
20 PIRFavoritesDialog::PIRFavoritesDialog(
21   MainWindow *mw)
22   : QDialog(0),
23     ui(new Ui::PIRFavoritesDialog),
24     mainWindow(mw)
25 {
26   ui->setupUi(this);
27 }
28
29
30 PIRFavoritesDialog::~PIRFavoritesDialog()
31 {
32   delete ui;
33 }
34
35
36 void PIRFavoritesDialog::selectPrevFavKeyset()
37 {
38   int size = ui->favoritesListWidget->count();
39
40   if (size == 0)
41   {
42     // No favorites, so nothing to do!
43     return;
44   }
45
46   int position = ui->favoritesListWidget->currentRow();
47
48   --position;
49   if (position < 0)
50   {
51     position = size - 1;
52   }
53
54   ui->favoritesListWidget->setCurrentRow(
55     position,
56     QItemSelectionModel::ClearAndSelect);
57
58   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *> (
59       ui->favoritesListWidget->currentItem());
60
61   mainWindow->updateKeysetSelection(kwi->getID());
62
63   // Tell the user about the change:
64   QMaemo5InformationBox::information(0, kwi->text());
65 }
66
67
68 void PIRFavoritesDialog::selectNextFavKeyset()
69 {
70   int size = ui->favoritesListWidget->count();
71
72   if (size == 0)
73   {
74     // No favorites, so just return:
75     return;
76   }
77
78   int position = ui->favoritesListWidget->currentRow();
79
80   ++position;
81   if (position == size)
82   {
83     position = 0;
84   }
85
86   ui->favoritesListWidget->setCurrentRow(
87     position,
88     QItemSelectionModel::ClearAndSelect);
89
90   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *> (
91       ui->favoritesListWidget->currentItem());
92
93   mainWindow->updateKeysetSelection(kwi->getID());
94
95   // Tell the user about the change:
96   QMaemo5InformationBox::information(0, kwi->text());
97 }
98
99
100 void PIRFavoritesDialog::addItem(
101   PIRKeysetWidgetItem *item)
102 {
103   PIRKeysetWidgetItem *itemCopy = new PIRKeysetWidgetItem(item);
104   ui->favoritesListWidget->addItem(itemCopy);
105   ui->favoritesListWidget->sortItems();
106 }
107
108
109 int PIRFavoritesDialog::getCount()
110 {
111   return ui->favoritesListWidget->count();
112 }
113
114
115 PIRKeysetWidgetItem *PIRFavoritesDialog::getItem(
116   int index)
117 {
118   return dynamic_cast<PIRKeysetWidgetItem *>(
119     ui->favoritesListWidget->item(index));
120 }
121
122
123 void PIRFavoritesDialog::removeItem(
124   unsigned int keysetID)
125 {
126   // Find the keyset in the list (if it is there):
127   int count = ui->favoritesListWidget->count();
128   int index = 0;
129   PIRKeysetWidgetItem *kwi;
130
131   while (index < count)
132   {
133     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
134          ui->favoritesListWidget->item(index));
135
136     if (kwi->getID() == keysetID)
137     {
138       // Deleting the item removes it from the list:
139       delete kwi;
140       return;
141     }
142
143     ++index;
144   }
145 }
146
147
148 QListWidget *PIRFavoritesDialog::getFavoritesListWidget()
149 {
150   return ui->favoritesListWidget;
151 }
152
153
154 void PIRFavoritesDialog::on_favoritesListWidget_itemClicked(
155   QListWidgetItem *item)
156 {
157   if (item)
158   {
159     PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *> (item);
160
161     mainWindow->updateKeysetSelection(kwi->getID());
162   }
163
164   // Exit from the dialog:
165   accept();
166 }