Now remembers the display orientation so pressing Rotate button will change the orien...
[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  * Show the edit window.\r
173  *\r
174  * @fn showEditWindow\r
175  */\r
176 void MainWindow::showEditWindow()\r
177 {\r
178     editUi->setupUi(this);\r
179     editUi->textEdit->setText(settings->value("ListText").toString());\r
180     connect(editUi->savePushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowSave()));\r
181     connect(editUi->cancelPushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowCancel()));\r
182 }\r
183 \r
184 /**\r
185  * Show the list window.\r
186  *\r
187  * @fn showListWindow\r
188  */\r
189 void MainWindow::showListWindow()\r
190 {\r
191     listUi->setupUi(this);\r
192     listUi->listVerticalLayout->setAlignment(Qt::AlignTop);\r
193     generateList();\r
194     connect(listUi->editListPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowEdit()));\r
195     connect(listUi->clearSelectedPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowClearSelected()));\r
196     connect(listUi->menuAbout, SIGNAL(triggered(QAction*)), this, SLOT(slotActionAbout(QAction*)));\r
197     connect(listUi->menuRotate, SIGNAL(triggered(QAction*)), this, SLOT(slotActionRotate(QAction*)));\r
198 }\r
199 \r
200 /**\r
201  * Generate the list of checkboxes. A pointer to such a checkbox will be stored\r
202  * in a container for later use.\r
203  *\r
204  * @fn generateList\r
205  */\r
206 void MainWindow::generateList()\r
207 {\r
208     qDebug() << "Generate List";\r
209 \r
210     checkBoxes.clear();\r
211     QString text = settings->value("ListText").toString();\r
212     QStringList list = text.split("\n");\r
213 \r
214     foreach(QString item, list)\r
215     {\r
216         if(item.length() > 0)\r
217         {\r
218             QCheckBox * cb = new QCheckBox(item);\r
219             checkBoxes.append(cb);\r
220             listUi->listVerticalLayout->addWidget(cb);\r
221         }\r
222     }\r
223 }\r
224 \r
225 /**\r
226  * Is called when the application terminates.\r
227  *\r
228  * @fn closeEvent\r
229  * @param event - the QCloseEvent.\r
230  */\r
231 void MainWindow::closeEvent(QCloseEvent *event)\r
232 {\r
233     qDebug() << "Closed";\r
234     event->accept();\r
235 }\r
236 \r
237 /**\r
238  * Is called when the Rotate menu item is triggered. Display orientation is saved in the QSettings.\r
239  *\r
240  * @fn slotActionRotate\r
241  * @param QAction* action - the action.\r
242  */\r
243 void MainWindow::slotActionRotate(QAction* action)\r
244 {\r
245     qDebug() << "Rotate" << action->text();\r
246 \r
247     landscape = !tempLandscapeMode;\r
248     settings->setValue("Landscape", landscape);\r
249     setLandscapeMode(landscape);\r
250 }\r
251 \r
252 /**\r
253  * Set landscape/portrait mode.\r
254  */\r
255 void MainWindow::setLandscapeMode(bool landscape)\r
256 {\r
257     if(landscape)\r
258     {\r
259         tempLandscapeMode = true;\r
260         qDebug() << "Landscape";\r
261         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
262         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
263     }\r
264     else\r
265     {\r
266         tempLandscapeMode = false;\r
267         qDebug() << "Portrait";\r
268         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
269         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
270     }\r
271 }\r
272 \r
273 /**\r
274  * Is called when the About menu item is triggered.\r
275  *\r
276  * @fn slotActionAbout\r
277  * @param QAction* action - the action.\r
278  */\r
279 void MainWindow::slotActionAbout(QAction* action)\r
280 {\r
281     qDebug() << "About" << action->text();\r
282     QString aboutText;\r
283     aboutText.append("EasyList (c) 2010\n\n");\r
284     aboutText.append("Created by Willem Liu.\n");\r
285     aboutText.append("Created with QtCreator.\n");\r
286     QMessageBox::about(this, "EasyList", aboutText);\r
287 }\r