2 * Copyright (c) 2010 Willem Liu
\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
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\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
23 #include "mainwindow.h"
\r
24 #include "ui_mainwindow.h"
\r
25 #include "ui_listwindow.h"
\r
27 #define DBUS_KEYBOARD_SLIDE "/org/freedesktop/Hal/devices/platform_slide"
\r
31 * Settings are initialised here.
\r
32 * Landscape mode is set to true.
\r
33 * List window is loaded.
\r
35 * @param parent - The parent widget.
\r
37 MainWindow::MainWindow(QWidget *parent) :
\r
38 QMainWindow(parent),
\r
39 editUi(new Ui::EditWindow),
\r
40 listUi(new Ui::ListWindow)
\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
54 landscape = settings->value("Landscape").toBool();
\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
61 setLandscapeMode(true);
\r
65 setLandscapeMode(landscape);
\r
67 // Auto-detect portrait/landscape mode. Only works on top widget.
\r
68 // setAttribute(Qt::WA_Maemo5AutoOrientation, true);
\r
74 * User interface pointers are deleted here.
\r
78 MainWindow::~MainWindow()
\r
85 * Check the DBUS property to see if the keyboard is closed or opened.
\r
87 bool MainWindow::isKeyboardClosed()
\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
99 * Slot is called when a DBUS event has been received caused by opening/closing the keyboard.
\r
101 void MainWindow::slotKeyboardSlide()
\r
103 // When keyboard is opened.
\r
104 if(isKeyboardClosed() == false)
\r
106 setLandscapeMode(true);
\r
110 setLandscapeMode(landscape);
\r
115 * Slot for action from Edit list button in de the list window.
\r
117 * @fn slotListWindowEdit
\r
119 void MainWindow::slotListWindowEdit()
\r
121 qDebug() << "Edit list";
\r
126 * Slot for action from Cancel button in de the edit window.
\r
128 * @fn slotEditWindowCancel
\r
130 void MainWindow::slotEditWindowCancel()
\r
132 qDebug() << "Cancel";
\r
137 * Slot for action from Save button in de the edit window.
\r
139 * @fn slotEditWindowSave
\r
141 void MainWindow::slotEditWindowSave()
\r
143 qDebug() << "Save";
\r
144 settings->setValue("ListText", editUi->textEdit->toPlainText());
\r
149 * Slot for action from Clear selected button in de the list window.
\r
151 * @fn slotListWindowClearSelected
\r
153 void MainWindow::slotListWindowClearSelected()
\r
155 qDebug() << "Clear selected";
\r
156 QString listText("");
\r
157 foreach(QCheckBox * cb, checkBoxes)
\r
160 if(cb->isChecked() == false)
\r
162 listText.append(cb->text());
\r
163 listText.append("\n");
\r
167 settings->setValue("ListText", listText);
\r
172 * Show the edit window.
\r
174 * @fn showEditWindow
\r
176 void MainWindow::showEditWindow()
\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
185 * Show the list window.
\r
187 * @fn showListWindow
\r
189 void MainWindow::showListWindow()
\r
191 listUi->setupUi(this);
\r
192 listUi->listVerticalLayout->setAlignment(Qt::AlignTop);
\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
201 * Generate the list of checkboxes. A pointer to such a checkbox will be stored
\r
202 * in a container for later use.
\r
206 void MainWindow::generateList()
\r
208 qDebug() << "Generate List";
\r
210 checkBoxes.clear();
\r
211 QString text = settings->value("ListText").toString();
\r
212 QStringList list = text.split("\n");
\r
214 foreach(QString item, list)
\r
216 if(item.length() > 0)
\r
218 QCheckBox * cb = new QCheckBox(item);
\r
219 checkBoxes.append(cb);
\r
220 listUi->listVerticalLayout->addWidget(cb);
\r
226 * Is called when the application terminates.
\r
229 * @param event - the QCloseEvent.
\r
231 void MainWindow::closeEvent(QCloseEvent *event)
\r
233 qDebug() << "Closed";
\r
238 * Is called when the Rotate menu item is triggered. Display orientation is saved in the QSettings.
\r
240 * @fn slotActionRotate
\r
241 * @param QAction* action - the action.
\r
243 void MainWindow::slotActionRotate(QAction* action)
\r
245 qDebug() << "Rotate" << action->text();
\r
246 landscape = !landscape;
\r
247 settings->setValue("Landscape", landscape);
\r
248 setLandscapeMode(landscape);
\r
252 * Set landscape/portrait mode.
\r
254 void MainWindow::setLandscapeMode(bool landscape)
\r
258 qDebug() << "Landscape";
\r
259 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
\r
260 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
\r
264 qDebug() << "Portrait";
\r
265 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
\r
266 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
\r
271 * Is called when the About menu item is triggered.
\r
273 * @fn slotActionAbout
\r
274 * @param QAction* action - the action.
\r
276 void MainWindow::slotActionAbout(QAction* action)
\r
278 qDebug() << "About" << action->text();
\r
280 aboutText.append("EasyList (c) 2010\n\n");
\r
281 aboutText.append("Created by Willem Liu.\n");
\r
282 aboutText.append("Created with QtCreator.\n");
\r
283 QMessageBox::about(this, "EasyList", aboutText);
\r