Changed project files
[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.\r
58     if(isKeyboardClosed() == false)\r
59     {\r
60         landscape = true;\r
61     }\r
62     setLandscapeMode(landscape);\r
63     // Auto-detect portrait/landscape mode. Only works on top widget.\r
64 //    setAttribute(Qt::WA_Maemo5AutoOrientation, true);\r
65     showListWindow();\r
66 }\r
67 \r
68 /**\r
69  * Destructor.\r
70  * User interface pointers are deleted here.\r
71  *\r
72  * @fn ~MainWindow\r
73  */\r
74 MainWindow::~MainWindow()\r
75 {\r
76     delete editUi;\r
77     delete listUi;\r
78 }\r
79 \r
80 /**\r
81  * Check the DBUS property to see if the keyboard is closed or opened.\r
82  */\r
83 bool MainWindow::isKeyboardClosed()\r
84 {\r
85     QDBusInterface propertyInterface("org.freedesktop.Hal",\r
86                     DBUS_KEYBOARD_SLIDE,\r
87                     "org.freedesktop.Hal.Device",\r
88                     QDBusConnection::systemBus());\r
89     bool result = propertyInterface.call("GetProperty", "button.state.value").arguments().at(0).toBool();\r
90     qDebug() << "Keyboard is closed:" << result;\r
91     return result;\r
92 }\r
93 \r
94 /**\r
95  * Slot is called when a DBUS event has been received caused by opening/closing the keyboard.\r
96  */\r
97 void MainWindow::slotKeyboardSlide()\r
98 {\r
99     // When keyboard is opened.\r
100     if(false == isKeyboardClosed())\r
101     {\r
102         setLandscapeMode(true);\r
103     }\r
104     else\r
105     {\r
106         setLandscapeMode(landscape);\r
107     }\r
108 }\r
109 \r
110 /**\r
111  * Slot for action from Edit list button in de the list window.\r
112  *\r
113  * @fn slotListWindowEdit\r
114  */\r
115 void MainWindow::slotListWindowEdit()\r
116 {\r
117     qDebug() << "Edit list";\r
118     showEditWindow();\r
119 }\r
120 \r
121 /**\r
122  * Slot for action from Cancel button in de the edit window.\r
123  *\r
124  * @fn slotEditWindowCancel\r
125  */\r
126 void MainWindow::slotEditWindowCancel()\r
127 {\r
128     qDebug() << "Cancel";\r
129     showListWindow();\r
130 }\r
131 \r
132 /**\r
133  * Slot for action from Save button in de the edit window.\r
134  *\r
135  * @fn slotEditWindowSave\r
136  */\r
137 void MainWindow::slotEditWindowSave()\r
138 {\r
139     qDebug() << "Save";\r
140     settings->setValue("ListText", editUi->textEdit->toPlainText());\r
141     showListWindow();\r
142 }\r
143 \r
144 /**\r
145  * Slot for action from Clear selected button in de the list window.\r
146  *\r
147  * @fn slotListWindowClearSelected\r
148  */\r
149 void MainWindow::slotListWindowClearSelected()\r
150 {\r
151     qDebug() << "Clear selected";\r
152     QString listText("");\r
153     foreach(QCheckBox * cb, checkBoxes)\r
154     {\r
155         cb->deleteLater();\r
156         if(cb->isChecked() == false)\r
157         {\r
158             listText.append(cb->text());\r
159             listText.append("\n");\r
160         }\r
161     }\r
162 \r
163     settings->setValue("ListText", listText);\r
164     generateList();\r
165 }\r
166 \r
167 /**\r
168  * Show the edit window.\r
169  *\r
170  * @fn showEditWindow\r
171  */\r
172 void MainWindow::showEditWindow()\r
173 {\r
174     editUi->setupUi(this);\r
175     editUi->textEdit->setText(settings->value("ListText").toString());\r
176     connect(editUi->savePushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowSave()));\r
177     connect(editUi->cancelPushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowCancel()));\r
178 }\r
179 \r
180 /**\r
181  * Show the list window.\r
182  *\r
183  * @fn showListWindow\r
184  */\r
185 void MainWindow::showListWindow()\r
186 {\r
187     listUi->setupUi(this);\r
188     listUi->listVerticalLayout->setAlignment(Qt::AlignTop);\r
189     generateList();\r
190     connect(listUi->editListPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowEdit()));\r
191     connect(listUi->clearSelectedPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowClearSelected()));\r
192     connect(listUi->menuAbout, SIGNAL(triggered(QAction*)), this, SLOT(slotActionAbout(QAction*)));\r
193     connect(listUi->menuRotate, SIGNAL(triggered(QAction*)), this, SLOT(slotActionRotate(QAction*)));\r
194 }\r
195 \r
196 /**\r
197  * Generate the list of checkboxes. A pointer to such a checkbox will be stored\r
198  * in a container for later use.\r
199  *\r
200  * @fn generateList\r
201  */\r
202 void MainWindow::generateList()\r
203 {\r
204     qDebug() << "Generate List";\r
205 \r
206     checkBoxes.clear();\r
207     QString text = settings->value("ListText").toString();\r
208     QStringList list = text.split("\n");\r
209 \r
210     foreach(QString item, list)\r
211     {\r
212         if(item.length() > 0)\r
213         {\r
214             QCheckBox * cb = new QCheckBox(item);\r
215             checkBoxes.append(cb);\r
216             listUi->listVerticalLayout->addWidget(cb);\r
217         }\r
218     }\r
219 }\r
220 \r
221 /**\r
222  * Is called when the application terminates.\r
223  *\r
224  * @fn closeEvent\r
225  * @param event - the QCloseEvent.\r
226  */\r
227 void MainWindow::closeEvent(QCloseEvent *event)\r
228 {\r
229     qDebug() << "Closed";\r
230     event->accept();\r
231 }\r
232 \r
233 /**\r
234  * Is called when the Rotate menu item is triggered. Display orientation is saved in the QSettings.\r
235  *\r
236  * @fn slotActionRotate\r
237  * @param QAction* action - the action.\r
238  */\r
239 void MainWindow::slotActionRotate(QAction* action)\r
240 {\r
241     qDebug() << "Rotate" << action->text();\r
242     landscape = !landscape;\r
243     settings->setValue("Landscape", landscape);\r
244     setLandscapeMode(landscape);\r
245 }\r
246 \r
247 /**\r
248  * Set landscape/portrait mode.\r
249  */\r
250 void MainWindow::setLandscapeMode(bool landscape)\r
251 {\r
252     if(landscape)\r
253     {\r
254         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
255         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
256     }\r
257     else\r
258     {\r
259         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
260         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
261     }\r
262 }\r
263 \r
264 /**\r
265  * Is called when the About menu item is triggered.\r
266  *\r
267  * @fn slotActionAbout\r
268  * @param QAction* action - the action.\r
269  */\r
270 void MainWindow::slotActionAbout(QAction* action)\r
271 {\r
272     qDebug() << "About" << action->text();\r
273     QString aboutText;\r
274     aboutText.append("EasyList (c) 2010\n\n");\r
275     aboutText.append("Created by Willem Liu.\n");\r
276     aboutText.append("Created with QtCreator.\n");\r
277     QMessageBox::about(this, "EasyList", aboutText);\r
278 }\r