9584bc068773c6987abe5dee64a1d0338da87e9f
[easylist] / src / 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     newIndex = 0;\r
12 \r
13     connect(SystemSettings::getInstance(), SIGNAL(signalKeyboardClosed(bool)), this, SLOT(keyboardClosed(bool)));\r
14 \r
15     ui->actionAuto_Orientation->setChecked(settings->value(AUTO_ORIENTATION).toBool());\r
16     on_actionAuto_Orientation_triggered();\r
17 \r
18     ui->actionSort_A_Z->setChecked(settings->value(SORT_A_Z).toBool());\r
19     on_actionSort_A_Z_triggered();\r
20 \r
21     // Set a default value for CHECKED_ITEMS_TO_BOTTOM\r
22     if(settings->contains(CHECKED_ITEMS_TO_BOTTOM) == false)\r
23     {\r
24         settings->setValue(CHECKED_ITEMS_TO_BOTTOM, false);\r
25     }\r
26     ui->actionChecked_bottom->setChecked(settings->value(CHECKED_ITEMS_TO_BOTTOM).toBool());\r
27     on_actionChecked_bottom_triggered();\r
28 \r
29     // Create a default for landscape mode.\r
30     landscape = settings->value(LANDSCAPE).toBool();\r
31     // If LANDSCAPE exists in QSettings.\r
32     if(settings->contains(LANDSCAPE))\r
33     {\r
34         // We use the LANDSCAPE value in the QSettings.\r
35         landscape = settings->value(LANDSCAPE).toBool();\r
36     }\r
37     else\r
38     {\r
39         // Otherwise we set our default into the QSettings.\r
40         settings->setValue(LANDSCAPE, landscape);\r
41     }\r
42     // If keyboard is opened at start. We do landscape mode.\r
43     // Otherwise we do what's read from the QSettings.\r
44     if(ui->actionAuto_Orientation->isChecked() == false)\r
45     {\r
46         if(SystemSettings::getInstance()->getKeyboardClosed() == false)\r
47         {\r
48             setLandscapeMode(true);\r
49         }\r
50         else\r
51         {\r
52             setLandscapeMode(landscape);\r
53         }\r
54     }\r
55 \r
56     // Populate the QStackedWidget. ListForm is set as the current widget.\r
57     listForm = new ListForm(this);\r
58     editForm = new EditForm(this);\r
59 \r
60     connect(listForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
61     connect(listForm, SIGNAL(signalEditListPushButtonTriggered(SlideWidget*)), this, SLOT(changeWidget(SlideWidget*)));\r
62 \r
63     connect(editForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
64     connect(editForm, SIGNAL(signalCancelPushButtonClicked(SlideWidget*)), this, SLOT(changeWidget(SlideWidget*)));\r
65     connect(editForm, SIGNAL(signalSavePushButtonClicked(SlideWidget*)), this, SLOT(changeWidget(SlideWidget*)));\r
66 \r
67     ui->stackedWidget->addWidget(listForm);\r
68     ui->stackedWidget->addWidget(editForm);\r
69     ui->stackedWidget->setCurrentWidget(listForm);\r
70 }\r
71 \r
72 MainForm::~MainForm()\r
73 {\r
74     delete ui;\r
75 }\r
76 \r
77 void MainForm::stateOutFinished()\r
78 {\r
79     qDebug() << "Show new widget" << newIndex;\r
80     ui->stackedWidget->setCurrentIndex(newIndex);\r
81     SlideWidget * newWidget = dynamic_cast<SlideWidget * >(ui->stackedWidget->currentWidget());\r
82     newWidget->move(0, -newWidget->height());\r
83     newWidget->setStateIn();\r
84     newWidget->shown();\r
85 }\r
86 \r
87 void MainForm::changeWidget(SlideWidget * currentWidget)\r
88 {\r
89     currentWidget->initStates();\r
90     int currentIndex = ui->stackedWidget->indexOf(currentWidget);\r
91     // Because all widgets are started with StateOut as initial state, we\r
92     // need to reset the current widget to StateIn. The view is showing the\r
93     // current widget at the place of its StateIn position. But the state\r
94     // is never set to StateIn.\r
95     currentWidget->setStateIn();\r
96     qDebug() << "Current widget index" << currentIndex;\r
97     if(currentIndex < ui->stackedWidget->count()-1)\r
98     {\r
99         newIndex = currentIndex+1;\r
100         currentWidget->setStateOut();\r
101     }\r
102     else\r
103     {\r
104         if(ui->stackedWidget->count() > 0)\r
105         {\r
106             newIndex = 0;\r
107             currentWidget->setStateOut();\r
108         }\r
109         else\r
110         {\r
111             qDebug() << "StackedWidget does not have any widgets";\r
112         }\r
113     }\r
114     qDebug() << "New widget index" << newIndex;\r
115 }\r
116 \r
117 void MainForm::keyboardClosed(bool closed)\r
118 {\r
119     // When keyboard is opened.\r
120     if(ui->actionAuto_Orientation->isChecked() == false)\r
121     {\r
122         if(closed == false)\r
123         {\r
124             setLandscapeMode(true);\r
125         }\r
126         else\r
127         {\r
128             setLandscapeMode(landscape);\r
129         }\r
130     }\r
131 }\r
132 \r
133 void MainForm::setLandscapeMode(bool landscape)\r
134 {\r
135     if(landscape)\r
136     {\r
137         tempLandscapeMode = true;\r
138         qDebug() << LANDSCAPE;\r
139 #ifdef Q_WS_MAEMO_5\r
140         setAttribute(Qt::WA_Maemo5AutoOrientation, false);\r
141         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
142         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
143 #endif\r
144     }\r
145     else\r
146     {\r
147         tempLandscapeMode = false;\r
148         qDebug() << PORTRAIT;\r
149 #ifdef Q_WS_MAEMO_5\r
150         setAttribute(Qt::WA_Maemo5AutoOrientation, false);\r
151         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
152         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
153 #endif\r
154     }\r
155 }\r
156 \r
157 void MainForm::on_actionRotate_triggered()\r
158 {\r
159     qDebug() << "Rotate";\r
160 \r
161     landscape = (width() < height());\r
162     settings->setValue(LANDSCAPE, landscape);\r
163     ui->actionAuto_Orientation->setChecked(false);\r
164     settings->setValue(AUTO_ORIENTATION, ui->actionAuto_Orientation->isChecked());\r
165     setLandscapeMode(landscape);\r
166 }\r
167 \r
168 void MainForm::on_actionAbout_triggered()\r
169 {\r
170     qDebug() << "About";\r
171     QString aboutText;\r
172     aboutText.append("EasyList (c) 2010\n\n");\r
173     aboutText.append("Created by Willem Liu.\n");\r
174     aboutText.append("Created with QtCreator.\n");\r
175     QMessageBox::about(this, "EasyList", aboutText);\r
176 }\r
177 \r
178 void MainForm::on_actionChecked_bottom_triggered()\r
179 {\r
180     bool sortToBottom = ui->actionChecked_bottom->isChecked();\r
181     qDebug() << "Checked Bottom" << sortToBottom;\r
182     settings->setValue(CHECKED_ITEMS_TO_BOTTOM, sortToBottom);\r
183     MyCheckBoxContainer::getInstance()->setSortCheckedToBottom(sortToBottom);\r
184 }\r
185 \r
186 void MainForm::closeEvent(QCloseEvent *event)\r
187 {\r
188     settings->setValue(LIST_TEXT, MyCheckBoxContainer::getInstance()->getListText());\r
189     event->accept();\r
190 }\r
191 \r
192 void MainForm::on_actionAuto_Orientation_triggered()\r
193 {\r
194     settings->setValue(AUTO_ORIENTATION, ui->actionAuto_Orientation->isChecked());\r
195     qDebug() << "Auto orientation" << ui->actionAuto_Orientation->isChecked();\r
196     if(ui->actionAuto_Orientation->isChecked())\r
197     {\r
198 #ifdef Q_WS_MAEMO_5\r
199         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
200         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
201         setAttribute(Qt::WA_Maemo5AutoOrientation, true);\r
202 #endif\r
203     }\r
204     else\r
205     {\r
206         setLandscapeMode(landscape);\r
207     }\r
208 }\r
209 \r
210 void MainForm::on_actionSort_A_Z_triggered()\r
211 {\r
212     settings->setValue(SORT_A_Z, ui->actionSort_A_Z->isChecked());\r
213     MyCheckBoxContainer::getInstance()->setSortAlphabetically(ui->actionSort_A_Z->isChecked());\r
214 }\r