Changed project file.
[easylist] / src / mainwindow.cpp
1 /*\r
2  *  Copyright (c) 2010 Willem Liu\r
3  *\r
4  *  Permission is hereby granted, free of charge, to any person obtaining a copy\r
5  *  of this software and associated documentation files (the "Software"), to deal\r
6  *  in the Software without restriction, including without limitation the rights\r
7  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8  *  copies of the Software, and to permit persons to whom the Software is\r
9  *  furnished to do so, subject to the following conditions:\r
10  *\r
11  *  The above copyright notice and this permission notice shall be included in\r
12  *  all copies or substantial portions of the Software.\r
13  *\r
14  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
19  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
20  *  THE SOFTWARE.\r
21  */\r
22 \r
23 #include "mainwindow.h"\r
24 #include "ui_mainwindow.h"\r
25 #include "ui_listwindow.h"\r
26 \r
27 #define DBUS_KEYBOARD_SLIDE "/org/freedesktop/Hal/devices/platform_slide"\r
28 \r
29 /**\r
30  * Constructor.\r
31  * Settings are initialised here.\r
32  * Landscape mode is set to true.\r
33  * List window is loaded.\r
34  * @fn MainWindow\r
35  * @param parent - The parent widget.\r
36  */\r
37 MainWindow::MainWindow(QWidget *parent) :\r
38     QMainWindow(parent),\r
39     editUi(new Ui::EditWindow),\r
40     listUi(new Ui::ListWindow)\r
41 {\r
42     // Connect to DBUS of keyboard slider.\r
43     QDBusConnection::systemBus().connect(QString("org.freedesktop.Hal"),\r
44                                          DBUS_KEYBOARD_SLIDE,\r
45                                          QString("org.freedesktop.Hal.Device"),\r
46                                          QString("PropertyModified"),\r
47                                          this, SLOT(slotKeyboardSlide()));\r
48     // Initialise the settings.\r
49     settings = new QSettings("WillemLiu", "easylist");\r
50     // We always start in landscape mode.\r
51     landscape = settings->value("Landscape").toBool();\r
52     if(settings->contains("Landscape"))\r
53     {\r
54         landscape = settings->value("Landscape").toBool();\r
55     }\r
56     settings->setValue("Landscape", landscape);\r
57     // If keyboard is opened at start. We do landscape mode.\r
58     // Otherwise we do what's read from the QSettings.\r
59     if(isKeyboardClosed() == false)\r
60     {\r
61         setLandscapeMode(true);\r
62     }\r
63     else\r
64     {\r
65         setLandscapeMode(landscape);\r
66     }\r
67     // Auto-detect portrait/landscape mode. Only works on top widget.\r
68 //    setAttribute(Qt::WA_Maemo5AutoOrientation, true);\r
69     showListWindow();\r
70 }\r
71 \r
72 /**\r
73  * Destructor.\r
74  * User interface pointers are deleted here.\r
75  *\r
76  * @fn ~MainWindow\r
77  */\r
78 MainWindow::~MainWindow()\r
79 {\r
80     delete editUi;\r
81     delete listUi;\r
82 }\r
83 \r
84 /**\r
85  * Check the DBUS property to see if the keyboard is closed or opened.\r
86  */\r
87 bool MainWindow::isKeyboardClosed()\r
88 {\r
89     QDBusInterface propertyInterface("org.freedesktop.Hal",\r
90                     DBUS_KEYBOARD_SLIDE,\r
91                     "org.freedesktop.Hal.Device",\r
92                     QDBusConnection::systemBus());\r
93     bool result = propertyInterface.call("GetProperty", "button.state.value").arguments().at(0).toBool();\r
94     qDebug() << "Keyboard is closed:" << result;\r
95     return result;\r
96 }\r
97 \r
98 /**\r
99  * Slot is called when a DBUS event has been received caused by opening/closing the keyboard.\r
100  */\r
101 void MainWindow::slotKeyboardSlide()\r
102 {\r
103     // When keyboard is opened.\r
104     if(isKeyboardClosed() == false)\r
105     {\r
106         setLandscapeMode(true);\r
107     }\r
108     else\r
109     {\r
110         setLandscapeMode(landscape);\r
111     }\r
112 }\r
113 \r
114 /**\r
115  * Slot for action from Edit list button in de the list window.\r
116  *\r
117  * @fn slotListWindowEdit\r
118  */\r
119 void MainWindow::slotListWindowEdit()\r
120 {\r
121     qDebug() << "Edit list";\r
122     showEditWindow();\r
123 }\r
124 \r
125 /**\r
126  * Slot for action from Cancel button in de the edit window.\r
127  *\r
128  * @fn slotEditWindowCancel\r
129  */\r
130 void MainWindow::slotEditWindowCancel()\r
131 {\r
132     qDebug() << "Cancel";\r
133     showListWindow();\r
134 }\r
135 \r
136 /**\r
137  * Slot for action from Save button in de the edit window.\r
138  *\r
139  * @fn slotEditWindowSave\r
140  */\r
141 void MainWindow::slotEditWindowSave()\r
142 {\r
143     qDebug() << "Save";\r
144     settings->setValue("ListText", editUi->textEdit->toPlainText());\r
145     showListWindow();\r
146 }\r
147 \r
148 /**\r
149  * Slot for action from Clear selected button in de the list window.\r
150  *\r
151  * @fn slotListWindowClearSelected\r
152  */\r
153 void MainWindow::slotListWindowClearSelected()\r
154 {\r
155     qDebug() << "Clear selected";\r
156     QString listText("");\r
157     foreach(QCheckBox * cb, checkBoxes)\r
158     {\r
159         cb->deleteLater();\r
160         if(cb->isChecked() == false)\r
161         {\r
162             listText.append(cb->text());\r
163             listText.append("\n");\r
164         }\r
165     }\r
166 \r
167     settings->setValue("ListText", listText);\r
168     generateList();\r
169 }\r
170 \r
171 /**\r
172  * Prefix all checked items with ! in the list text and save it to QSettings.\r
173  */\r
174 void MainWindow::slotListWindowSaveChecked()\r
175 {\r
176     qDebug() << "Save selected";\r
177     QString listText("");\r
178     foreach(QCheckBox * cb, checkBoxes)\r
179     {\r
180         QString item(cb->text());\r
181         if(cb->isChecked() == true)\r
182         {\r
183             if(item.startsWith("!") == false)\r
184             {\r
185                 item.push_front("!");\r
186             }\r
187             listText.append(item);\r
188         }\r
189         else\r
190         {\r
191             if(item.startsWith("!") == true)\r
192             {\r
193                 item = item.replace("!", "");\r
194             }\r
195             listText.append(item);\r
196         }\r
197         listText.append("\n");\r
198     }\r
199 \r
200     settings->setValue("ListText", listText);\r
201 }\r
202 \r
203 /**\r
204  * Show the edit window.\r
205  *\r
206  * @fn showEditWindow\r
207  */\r
208 void MainWindow::showEditWindow()\r
209 {\r
210     slotListWindowSaveChecked();\r
211     editUi->setupUi(this);\r
212     editUi->textEdit->setText(settings->value("ListText").toString());\r
213     connect(editUi->savePushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowSave()));\r
214     connect(editUi->cancelPushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowCancel()));\r
215 }\r
216 \r
217 /**\r
218  * Show the list window.\r
219  *\r
220  * @fn showListWindow\r
221  */\r
222 void MainWindow::showListWindow()\r
223 {\r
224     listUi->setupUi(this);\r
225     listUi->listVerticalLayout->setAlignment(Qt::AlignTop);\r
226     generateList();\r
227     connect(listUi->editListPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowEdit()));\r
228     connect(listUi->clearSelectedPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowClearSelected()));\r
229     connect(listUi->menuAbout, SIGNAL(triggered(QAction*)), this, SLOT(slotActionAbout(QAction*)));\r
230     connect(listUi->menuRotate, SIGNAL(triggered(QAction*)), this, SLOT(slotActionRotate(QAction*)));\r
231 }\r
232 \r
233 /**\r
234  * Generate the list of checkboxes. A pointer to such a checkbox will be stored\r
235  * in a container for later use.\r
236  *\r
237  * @fn generateList\r
238  */\r
239 void MainWindow::generateList()\r
240 {\r
241     qDebug() << "Generate List";\r
242 \r
243     checkBoxes.clear();\r
244     QString text = settings->value("ListText").toString();\r
245     QStringList list = text.split("\n");\r
246 \r
247     foreach(QString item, list)\r
248     {\r
249         if(item.length() > 0)\r
250         {\r
251             QCheckBox * cb = new QCheckBox(item);\r
252             if(item.startsWith("!"))\r
253             {\r
254                 QString itemName(item.right(item.length()-1));\r
255                 cb->setText(itemName);\r
256                 cb->setChecked(true);\r
257             }\r
258             checkBoxes.append(cb);\r
259             listUi->listVerticalLayout->addWidget(cb);\r
260         }\r
261     }\r
262 }\r
263 \r
264 /**\r
265  * Is called when the application terminates.\r
266  *\r
267  * @fn closeEvent\r
268  * @param event - the QCloseEvent.\r
269  */\r
270 void MainWindow::closeEvent(QCloseEvent *event)\r
271 {\r
272     qDebug() << "Closed";\r
273     slotListWindowSaveChecked();\r
274     event->accept();\r
275 }\r
276 \r
277 /**\r
278  * Is called when the Rotate menu item is triggered. Display orientation is saved in the QSettings.\r
279  *\r
280  * @fn slotActionRotate\r
281  * @param QAction* action - the action.\r
282  */\r
283 void MainWindow::slotActionRotate(QAction* action)\r
284 {\r
285     qDebug() << "Rotate" << action->text();\r
286 \r
287     landscape = !tempLandscapeMode;\r
288     settings->setValue("Landscape", landscape);\r
289     setLandscapeMode(landscape);\r
290 }\r
291 \r
292 /**\r
293  * Set landscape/portrait mode.\r
294  */\r
295 void MainWindow::setLandscapeMode(bool landscape)\r
296 {\r
297     if(landscape)\r
298     {\r
299         tempLandscapeMode = true;\r
300         qDebug() << "Landscape";\r
301         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
302         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
303     }\r
304     else\r
305     {\r
306         tempLandscapeMode = false;\r
307         qDebug() << "Portrait";\r
308         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
309         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
310     }\r
311 }\r
312 \r
313 /**\r
314  * Is called when the About menu item is triggered.\r
315  *\r
316  * @fn slotActionAbout\r
317  * @param QAction* action - the action.\r
318  */\r
319 void MainWindow::slotActionAbout(QAction* action)\r
320 {\r
321     qDebug() << "About" << action->text();\r
322     QString aboutText;\r
323     aboutText.append("EasyList (c) 2010\n\n");\r
324     aboutText.append("Created by Willem Liu.\n");\r
325     aboutText.append("Created with QtCreator.\n");\r
326     QMessageBox::about(this, "EasyList", aboutText);\r
327 }\r