Changed changelog, copyright. Support for sorting checked items to bottom.
[easylist] / src / mainwindow.cpp
index ebec627..8bab599 100755 (executable)
@@ -24,8 +24,6 @@
 #include "ui_mainwindow.h"\r
 #include "ui_listwindow.h"\r
 \r
-#define DBUS_KEYBOARD_SLIDE "/org/freedesktop/Hal/devices/platform_slide"\r
-\r
 /**\r
  * Constructor.\r
  * Settings are initialised here.\r
@@ -46,22 +44,34 @@ MainWindow::MainWindow(QWidget *parent) :
                                          QString("PropertyModified"),\r
                                          this, SLOT(slotKeyboardSlide()));\r
     // Initialise the settings.\r
-    settings = new QSettings("WillemLiu", "easylist");\r
+    settings = new QSettings(WILLEM_LIU, EASY_LIST);\r
+\r
+    // Set a default value for CHECKED_ITEMS_TO_BOTTOM\r
+    if(settings->contains(CHECKED_ITEMS_TO_BOTTOM) == false)\r
+    {\r
+        settings->setValue(CHECKED_ITEMS_TO_BOTTOM, false);\r
+    }\r
+\r
     // We always start in landscape mode.\r
-    landscape = settings->value("Landscape").toBool();\r
-    if(settings->contains("Landscape"))\r
+    landscape = settings->value(LANDSCAPE).toBool();\r
+    if(settings->contains(LANDSCAPE))\r
     {\r
-        landscape = settings->value("Landscape").toBool();\r
+        landscape = settings->value(LANDSCAPE).toBool();\r
     }\r
-    settings->setValue("Landscape", landscape);\r
-    // If keyboard is opened at start.\r
+    settings->setValue(LANDSCAPE, landscape);\r
+    // If keyboard is opened at start. We do landscape mode.\r
+    // Otherwise we do what's read from the QSettings.\r
     if(isKeyboardClosed() == false)\r
     {\r
-        landscape = true;\r
+        setLandscapeMode(true);\r
+    }\r
+    else\r
+    {\r
+        setLandscapeMode(landscape);\r
     }\r
-    setLandscapeMode(landscape);\r
     // Auto-detect portrait/landscape mode. Only works on top widget.\r
 //    setAttribute(Qt::WA_Maemo5AutoOrientation, true);\r
+\r
     showListWindow();\r
 }\r
 \r
@@ -97,7 +107,7 @@ bool MainWindow::isKeyboardClosed()
 void MainWindow::slotKeyboardSlide()\r
 {\r
     // When keyboard is opened.\r
-    if(false == isKeyboardClosed())\r
+    if(isKeyboardClosed() == false)\r
     {\r
         setLandscapeMode(true);\r
     }\r
@@ -137,12 +147,14 @@ void MainWindow::slotEditWindowCancel()
 void MainWindow::slotEditWindowSave()\r
 {\r
     qDebug() << "Save";\r
-    settings->setValue("ListText", editUi->textEdit->toPlainText());\r
+    settings->setValue(LIST_TEXT, editUi->textEdit->toPlainText());\r
     showListWindow();\r
 }\r
 \r
 /**\r
  * Slot for action from Clear selected button in de the list window.\r
+ * It clears all QCheckBoxes from the layout, removes all checked items from\r
+ * the list text and finally calls generateList().\r
  *\r
  * @fn slotListWindowClearSelected\r
  */\r
@@ -160,19 +172,117 @@ void MainWindow::slotListWindowClearSelected()
         }\r
     }\r
 \r
-    settings->setValue("ListText", listText);\r
+    settings->setValue(LIST_TEXT, listText);\r
     generateList();\r
 }\r
 \r
 /**\r
+ * Prefix all checked items with ! in the list text and save it to QSettings.\r
+ */\r
+void MainWindow::slotListWindowSaveChecked()\r
+{\r
+    qDebug() << "Save selected";\r
+    QString listText("");\r
+    foreach(QCheckBox * cb, checkBoxes)\r
+    {\r
+        QString item(cb->text());\r
+        if(cb->isChecked() == true)\r
+        {\r
+            if(item.startsWith("!") == false)\r
+            {\r
+                item.push_front("!");\r
+            }\r
+            listText.append(item);\r
+        }\r
+        else\r
+        {\r
+            if(item.startsWith("!") == true)\r
+            {\r
+                item = item.replace("!", "");\r
+            }\r
+            listText.append(item);\r
+        }\r
+        listText.append("\n");\r
+    }\r
+\r
+    settings->setValue(LIST_TEXT, listText);\r
+}\r
+\r
+/**\r
+ * Is called when a checkbox has been clicked.\r
+ *\r
+ * @fn slotActionCheckBox\r
+ * @param bool checked - true if checkbox is checked.\r
+ */\r
+void MainWindow::slotActionCheckBox(bool checked)\r
+{\r
+    qDebug() << "CheckBox checked=" << checked;\r
+    slotListWindowSaveChecked();\r
+    if(settings->value(CHECKED_ITEMS_TO_BOTTOM).toBool())\r
+    {\r
+        slotSortCheckedBottom();\r
+        // Remove all the checkboxes from the screen.\r
+        foreach(QCheckBox * cb, checkBoxes)\r
+        {\r
+            cb->deleteLater();\r
+        }\r
+        generateList();\r
+    }\r
+    else\r
+    {\r
+        qDebug() << "No need to sort items to bottom";\r
+    }\r
+}\r
+\r
+/**\r
+ * Sort checked items to bottom of the list text and returns the new list text.\r
+ *\r
+ * @fn slotSortCheckedBottom\r
+ *\r
+ * @return QString - the new list text.\r
+ */\r
+QString MainWindow::slotSortCheckedBottom()\r
+{\r
+    QString result(settings->value(LIST_TEXT).toString());\r
+    if(settings->value(CHECKED_ITEMS_TO_BOTTOM).toBool())\r
+    {\r
+        QStringList list = result.split("\n");\r
+        QString listText("");\r
+        QString checkedListText("");\r
+        foreach(QString item, list)\r
+        {\r
+            if(item.length() > 0)\r
+            {\r
+                if(item.startsWith("!"))\r
+                {\r
+                    checkedListText.append(item);\r
+                    checkedListText.append("\n");\r
+                }\r
+                else\r
+                {\r
+                    listText.append(item);\r
+                    listText.append("\n");\r
+                }\r
+            }\r
+        }\r
+        listText.append(checkedListText);\r
+        qDebug() << "Sort checked items to bottom";\r
+        settings->setValue(LIST_TEXT, listText);\r
+        result = listText;\r
+    }\r
+    return result;\r
+}\r
+\r
+/**\r
  * Show the edit window.\r
  *\r
  * @fn showEditWindow\r
  */\r
 void MainWindow::showEditWindow()\r
 {\r
+    slotListWindowSaveChecked();\r
     editUi->setupUi(this);\r
-    editUi->textEdit->setText(settings->value("ListText").toString());\r
+    editUi->textEdit->setText(settings->value(LIST_TEXT).toString());\r
     connect(editUi->savePushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowSave()));\r
     connect(editUi->cancelPushButton, SIGNAL(clicked()), this, SLOT(slotEditWindowCancel()));\r
 }\r
@@ -186,9 +296,12 @@ void MainWindow::showListWindow()
 {\r
     listUi->setupUi(this);\r
     listUi->listVerticalLayout->setAlignment(Qt::AlignTop);\r
+    listUi->actionChecked_Bottom->setChecked(settings->value(CHECKED_ITEMS_TO_BOTTOM).toBool());\r
+    slotSortCheckedBottom();\r
     generateList();\r
     connect(listUi->editListPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowEdit()));\r
     connect(listUi->clearSelectedPushButton, SIGNAL(clicked()), this, SLOT(slotListWindowClearSelected()));\r
+    connect(listUi->menuChecked_Bottom, SIGNAL(triggered(QAction*)), this, SLOT(slotActionCheckedBottom(QAction*)));\r
     connect(listUi->menuAbout, SIGNAL(triggered(QAction*)), this, SLOT(slotActionAbout(QAction*)));\r
     connect(listUi->menuRotate, SIGNAL(triggered(QAction*)), this, SLOT(slotActionRotate(QAction*)));\r
 }\r
@@ -204,7 +317,7 @@ void MainWindow::generateList()
     qDebug() << "Generate List";\r
 \r
     checkBoxes.clear();\r
-    QString text = settings->value("ListText").toString();\r
+    QString text = settings->value(LIST_TEXT).toString();\r
     QStringList list = text.split("\n");\r
 \r
     foreach(QString item, list)\r
@@ -212,6 +325,13 @@ void MainWindow::generateList()
         if(item.length() > 0)\r
         {\r
             QCheckBox * cb = new QCheckBox(item);\r
+            connect(cb, SIGNAL(clicked(bool)), this, SLOT(slotActionCheckBox(bool)));\r
+            if(item.startsWith("!"))\r
+            {\r
+                QString itemName(item.right(item.length()-1));\r
+                cb->setText(itemName);\r
+                cb->setChecked(true);\r
+            }\r
             checkBoxes.append(cb);\r
             listUi->listVerticalLayout->addWidget(cb);\r
         }\r
@@ -227,6 +347,7 @@ void MainWindow::generateList()
 void MainWindow::closeEvent(QCloseEvent *event)\r
 {\r
     qDebug() << "Closed";\r
+    slotListWindowSaveChecked();\r
     event->accept();\r
 }\r
 \r
@@ -239,8 +360,9 @@ void MainWindow::closeEvent(QCloseEvent *event)
 void MainWindow::slotActionRotate(QAction* action)\r
 {\r
     qDebug() << "Rotate" << action->text();\r
-    landscape = !landscape;\r
-    settings->setValue("Landscape", landscape);\r
+\r
+    landscape = !tempLandscapeMode;\r
+    settings->setValue(LANDSCAPE, landscape);\r
     setLandscapeMode(landscape);\r
 }\r
 \r
@@ -251,11 +373,15 @@ void MainWindow::setLandscapeMode(bool landscape)
 {\r
     if(landscape)\r
     {\r
+        tempLandscapeMode = true;\r
+        qDebug() << LANDSCAPE;\r
         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
     }\r
     else\r
     {\r
+        tempLandscapeMode = false;\r
+        qDebug() << PORTRAIT;\r
         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
     }\r
@@ -276,3 +402,16 @@ void MainWindow::slotActionAbout(QAction* action)
     aboutText.append("Created with QtCreator.\n");\r
     QMessageBox::about(this, "EasyList", aboutText);\r
 }\r
+\r
+/**\r
+ * Is called when the Checked Bottom menu item is triggered.\r
+ * The menu item is a checkable item. So we need to check if it's checked or not.\r
+ *\r
+ * @fn slotActionCheckedBottom\r
+ * @param QAction* action - the action.\r
+ */\r
+void MainWindow::slotActionCheckedBottom(QAction* action)\r
+{\r
+    qDebug() << "Checked Bottom" << action->text() << listUi->actionChecked_Bottom->isChecked();\r
+    settings->setValue(CHECKED_ITEMS_TO_BOTTOM, listUi->actionChecked_Bottom->isChecked());\r
+}\r