Added some minor improvements to about box.
[easylist] / mainform.cpp
1 #include "mainform.h"\r
2 #include "ui_mainform.h"\r
3 \r
4 MainForm::MainForm(QWidget *parent) :\r
5     QMainWindow(parent),\r
6     ui(new Ui::MainForm)\r
7 {\r
8     ui->setupUi(this);\r
9     settings = new QSettings(WILLEM_LIU, EASY_LIST);\r
10 \r
11     requestWebpage = new RequestWebpage(this);\r
12     connect(requestWebpage, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotSyncList(QNetworkReply*)));\r
13 \r
14     newIndex = 0;\r
15     connect(SystemSettings::getInstance(), SIGNAL(signalKeyboardClosed(bool)), this, SLOT(keyboardClosed(bool)));\r
16 \r
17     ui->actionAuto_Orientation->setChecked(settings->value(AUTO_ORIENTATION).toBool());\r
18     on_actionAuto_Orientation_triggered();\r
19 \r
20     ui->actionSort_A_Z->setChecked(settings->value(SORT_A_Z).toBool());\r
21     on_actionSort_A_Z_triggered();\r
22 \r
23     // Set a default value for CHECKED_ITEMS_TO_BOTTOM\r
24     if(settings->contains(CHECKED_ITEMS_TO_BOTTOM) == false)\r
25     {\r
26         settings->setValue(CHECKED_ITEMS_TO_BOTTOM, false);\r
27     }\r
28     ui->actionChecked_bottom->setChecked(settings->value(CHECKED_ITEMS_TO_BOTTOM).toBool());\r
29     on_actionChecked_bottom_triggered();\r
30 \r
31     // Create a default for landscape mode.\r
32     landscape = settings->value(LANDSCAPE).toBool();\r
33     // If LANDSCAPE exists in QSettings.\r
34     if(settings->contains(LANDSCAPE))\r
35     {\r
36         // We use the LANDSCAPE value in the QSettings.\r
37         landscape = settings->value(LANDSCAPE).toBool();\r
38     }\r
39     else\r
40     {\r
41         // Otherwise we set our default into the QSettings.\r
42         settings->setValue(LANDSCAPE, landscape);\r
43     }\r
44     // If keyboard is opened at start. We do landscape mode.\r
45     // Otherwise we do what's read from the QSettings.\r
46     if(ui->actionAuto_Orientation->isChecked() == false)\r
47     {\r
48         if(SystemSettings::getInstance()->getKeyboardClosed() == false)\r
49         {\r
50             setLandscapeMode(true);\r
51         }\r
52         else\r
53         {\r
54             setLandscapeMode(landscape);\r
55         }\r
56     }\r
57 \r
58     // Populate the QStackedWidget. ListForm is set as the current widget.\r
59     listForm = new ListForm(this);\r
60     editForm = new EditForm(this);\r
61     chooseListForm = new ChooseListForm(this);\r
62     settingsForm = new SettingsForm(this);\r
63 \r
64     connect(listForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
65     connect(listForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
66 \r
67     connect(editForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
68     connect(editForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
69 \r
70     connect(chooseListForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
71     connect(chooseListForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
72 \r
73     connect(settingsForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
74     connect(settingsForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
75 \r
76     ui->stackedWidget->addWidget(listForm);\r
77     ui->stackedWidget->addWidget(editForm);\r
78     ui->stackedWidget->addWidget(chooseListForm);\r
79     ui->stackedWidget->addWidget(settingsForm);\r
80     ui->stackedWidget->setCurrentWidget(listForm);\r
81 }\r
82 \r
83 MainForm::~MainForm()\r
84 {\r
85     delete ui;\r
86 }\r
87 \r
88 void MainForm::stateOutFinished()\r
89 {\r
90     qDebug() << "Show new widget" << newIndex;\r
91     ui->stackedWidget->setCurrentIndex(newIndex);\r
92     SlideWidget * newWidget = dynamic_cast<SlideWidget * >(ui->stackedWidget->currentWidget());\r
93     newWidget->move(0, -newWidget->height());\r
94     newWidget->setStateIn();\r
95     newWidget->shown();\r
96 }\r
97 \r
98 void MainForm::changeWidget(int step)\r
99 {\r
100     SlideWidget * currentWidget = dynamic_cast<SlideWidget * >(ui->stackedWidget->currentWidget());\r
101     currentWidget->initStates();\r
102     int currentIndex = ui->stackedWidget->indexOf(currentWidget);\r
103     // Because all widgets are started with StateOut as initial state, we\r
104     // need to reset the current widget to StateIn. The view is showing the\r
105     // current widget at the place of its StateIn position. But the state\r
106     // is never set to StateIn.\r
107     currentWidget->setStateIn();\r
108     qDebug() << "Current widget index" << currentIndex;\r
109     newIndex = step;\r
110     currentWidget->setStateOut();\r
111     qDebug() << "New widget index" << newIndex;\r
112 }\r
113 \r
114 void MainForm::keyboardClosed(bool closed)\r
115 {\r
116     // When keyboard is opened.\r
117     if(ui->actionAuto_Orientation->isChecked() == false)\r
118     {\r
119         if(closed == false)\r
120         {\r
121             setLandscapeMode(true);\r
122         }\r
123         else\r
124         {\r
125             setLandscapeMode(landscape);\r
126         }\r
127     }\r
128 }\r
129 \r
130 void MainForm::setLandscapeMode(bool landscape)\r
131 {\r
132     if(landscape)\r
133     {\r
134         tempLandscapeMode = true;\r
135         qDebug() << LANDSCAPE;\r
136 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)\r
137         setAttribute(Qt::WA_Maemo5AutoOrientation, false);\r
138         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
139         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
140 #endif\r
141     }\r
142     else\r
143     {\r
144         tempLandscapeMode = false;\r
145         qDebug() << PORTRAIT;\r
146 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)\r
147         setAttribute(Qt::WA_Maemo5AutoOrientation, false);\r
148         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
149         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
150 #endif\r
151     }\r
152 }\r
153 \r
154 void MainForm::on_actionRotate_triggered()\r
155 {\r
156     qDebug() << "Rotate";\r
157 \r
158     landscape = (width() < height());\r
159     settings->setValue(LANDSCAPE, landscape);\r
160     ui->actionAuto_Orientation->setChecked(false);\r
161     settings->setValue(AUTO_ORIENTATION, ui->actionAuto_Orientation->isChecked());\r
162     setLandscapeMode(landscape);\r
163 }\r
164 \r
165 void MainForm::on_actionAbout_triggered()\r
166 {\r
167     qDebug() << "About";\r
168     QString aboutText;\r
169     aboutText.append("<html><body>");\r
170     aboutText.append("EasyList (c) 2010-");\r
171     aboutText.append(QDate::currentDate().toString("yyyy"));\r
172     aboutText.append("<br><br>");\r
173     aboutText.append("Created by Willem Liu.<br>");\r
174     aboutText.append("Created with QtCreator.<br><br>");\r
175     aboutText.append("Please <a href='http://www.willemliu.nl/donate'>donate</a> any amount you deem this app is worthy to keep me going on.<br><br>");\r
176     aboutText.append("</body></html>");\r
177     QMessageBox::about(this, "EasyList", aboutText);\r
178 }\r
179 \r
180 void MainForm::on_actionChecked_bottom_triggered()\r
181 {\r
182     bool sortToBottom = ui->actionChecked_bottom->isChecked();\r
183     qDebug() << "Checked Bottom" << sortToBottom;\r
184     settings->setValue(CHECKED_ITEMS_TO_BOTTOM, sortToBottom);\r
185     MyCheckBoxContainer::getInstance()->setSortCheckedToBottom(sortToBottom);\r
186 }\r
187 \r
188 void MainForm::closeEvent(QCloseEvent *event)\r
189 {\r
190     settings->setValue(LIST_TEXT, MyCheckBoxContainer::getInstance()->getListText());\r
191     SystemSettings::getInstance()->saveCurrentList();\r
192     event->accept();\r
193 }\r
194 \r
195 void MainForm::on_actionAuto_Orientation_triggered()\r
196 {\r
197     settings->setValue(AUTO_ORIENTATION, ui->actionAuto_Orientation->isChecked());\r
198     qDebug() << "Auto orientation" << ui->actionAuto_Orientation->isChecked();\r
199     if(ui->actionAuto_Orientation->isChecked())\r
200     {\r
201 #ifdef Q_WS_MAEMO_5\r
202         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
203         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
204         setAttribute(Qt::WA_Maemo5AutoOrientation, true);\r
205 #endif\r
206     }\r
207     else\r
208     {\r
209         setLandscapeMode(landscape);\r
210     }\r
211 }\r
212 \r
213 void MainForm::on_actionSort_A_Z_triggered()\r
214 {\r
215     settings->setValue(SORT_A_Z, ui->actionSort_A_Z->isChecked());\r
216     MyCheckBoxContainer::getInstance()->setSortAlphabetically(ui->actionSort_A_Z->isChecked());\r
217 }\r
218 \r
219 void MainForm::on_actionLists_triggered()\r
220 {\r
221     listForm->saveList();\r
222     changeWidget(2);\r
223 }\r
224 \r
225 void MainForm::on_actionSync_triggered()\r
226 {\r
227     QString username = settings->value(USERNAME, "").toString();\r
228     QString password = settings->value(PASSWORD, "").toString();\r
229     QString url = settings->value(SYNC_URL, DEFAULT_SYNC_URL).toString();\r
230     url.append("?username=" + username);\r
231     url.append("&password=" + password);\r
232     qDebug() << url;\r
233     requestWebpage->post(url,settings->value(LIST_TEXT,"").toString().toUtf8());\r
234     //requestWebpage->fetch(url);\r
235 }\r
236 \r
237 void MainForm::slotSyncList(QNetworkReply* pReply)\r
238 {\r
239     QByteArray data=pReply->readAll();\r
240     QString list = QString::fromUtf8(data);\r
241     settings->setValue(LIST_TEXT, list);\r
242     settings->setValue(SELECTED_LIST_NAME, SYNC_LIST_NAME);\r
243     settings->setValue(SYNC_LIST_NAME, settings->value(LIST_TEXT, ""));\r
244     QStringList listNames = settings->value(LIST_NAMES, "").toStringList();\r
245     if(listNames.contains(SYNC_LIST_NAME) == false)\r
246     {\r
247         listNames.append(SYNC_LIST_NAME);\r
248     }\r
249     settings->setValue(LIST_NAMES, QVariant(listNames));\r
250     changeWidget(0);\r
251     pReply->deleteLater();\r
252 }\r
253 \r
254 void MainForm::on_actionSetting_triggered()\r
255 {\r
256     changeWidget(3);\r
257 }\r