b21cbcc210af4bfb08491208a0c23c4fdd3d1527
[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     chooseListForm = new ChooseListForm(this);\r
60 \r
61     connect(listForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
62     connect(listForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
63 \r
64     connect(editForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
65     connect(editForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
66 \r
67     connect(chooseListForm, SIGNAL(signalTransitionOutFinished()), this, SLOT(stateOutFinished()));\r
68     connect(chooseListForm, SIGNAL(signalNavigate(int)), this, SLOT(changeWidget(int)));\r
69 \r
70     ui->stackedWidget->addWidget(listForm);\r
71     ui->stackedWidget->addWidget(editForm);\r
72     ui->stackedWidget->addWidget(chooseListForm);\r
73     ui->stackedWidget->setCurrentWidget(listForm);\r
74 }\r
75 \r
76 MainForm::~MainForm()\r
77 {\r
78     delete ui;\r
79 }\r
80 \r
81 void MainForm::stateOutFinished()\r
82 {\r
83     qDebug() << "Show new widget" << newIndex;\r
84     ui->stackedWidget->setCurrentIndex(newIndex);\r
85     SlideWidget * newWidget = dynamic_cast<SlideWidget * >(ui->stackedWidget->currentWidget());\r
86     newWidget->move(0, -newWidget->height());\r
87     newWidget->setStateIn();\r
88     newWidget->shown();\r
89 }\r
90 \r
91 void MainForm::changeWidget(int step)\r
92 {\r
93     SlideWidget * currentWidget = dynamic_cast<SlideWidget * >(ui->stackedWidget->currentWidget());\r
94     currentWidget->initStates();\r
95     int currentIndex = ui->stackedWidget->indexOf(currentWidget);\r
96     // Because all widgets are started with StateOut as initial state, we\r
97     // need to reset the current widget to StateIn. The view is showing the\r
98     // current widget at the place of its StateIn position. But the state\r
99     // is never set to StateIn.\r
100     currentWidget->setStateIn();\r
101     qDebug() << "Current widget index" << currentIndex;\r
102     newIndex = step;\r
103     currentWidget->setStateOut();\r
104     qDebug() << "New widget index" << newIndex;\r
105 }\r
106 \r
107 void MainForm::keyboardClosed(bool closed)\r
108 {\r
109     // When keyboard is opened.\r
110     if(ui->actionAuto_Orientation->isChecked() == false)\r
111     {\r
112         if(closed == false)\r
113         {\r
114             setLandscapeMode(true);\r
115         }\r
116         else\r
117         {\r
118             setLandscapeMode(landscape);\r
119         }\r
120     }\r
121 }\r
122 \r
123 void MainForm::setLandscapeMode(bool landscape)\r
124 {\r
125     if(landscape)\r
126     {\r
127         tempLandscapeMode = true;\r
128         qDebug() << LANDSCAPE;\r
129 #ifdef Q_WS_MAEMO_5\r
130         setAttribute(Qt::WA_Maemo5AutoOrientation, false);\r
131         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);\r
132         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
133 #endif\r
134     }\r
135     else\r
136     {\r
137         tempLandscapeMode = false;\r
138         qDebug() << PORTRAIT;\r
139 #ifdef Q_WS_MAEMO_5\r
140         setAttribute(Qt::WA_Maemo5AutoOrientation, false);\r
141         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);\r
142         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
143 #endif\r
144     }\r
145 }\r
146 \r
147 void MainForm::on_actionRotate_triggered()\r
148 {\r
149     qDebug() << "Rotate";\r
150 \r
151     landscape = (width() < height());\r
152     settings->setValue(LANDSCAPE, landscape);\r
153     ui->actionAuto_Orientation->setChecked(false);\r
154     settings->setValue(AUTO_ORIENTATION, ui->actionAuto_Orientation->isChecked());\r
155     setLandscapeMode(landscape);\r
156 }\r
157 \r
158 void MainForm::on_actionAbout_triggered()\r
159 {\r
160     qDebug() << "About";\r
161     QString aboutText;\r
162     aboutText.append("EasyList (c) 2010\n\n");\r
163     aboutText.append("Created by Willem Liu.\n");\r
164     aboutText.append("Created with QtCreator.\n");\r
165     QMessageBox::about(this, "EasyList", aboutText);\r
166 }\r
167 \r
168 void MainForm::on_actionChecked_bottom_triggered()\r
169 {\r
170     bool sortToBottom = ui->actionChecked_bottom->isChecked();\r
171     qDebug() << "Checked Bottom" << sortToBottom;\r
172     settings->setValue(CHECKED_ITEMS_TO_BOTTOM, sortToBottom);\r
173     MyCheckBoxContainer::getInstance()->setSortCheckedToBottom(sortToBottom);\r
174 }\r
175 \r
176 void MainForm::closeEvent(QCloseEvent *event)\r
177 {\r
178     settings->setValue(LIST_TEXT, MyCheckBoxContainer::getInstance()->getListText());\r
179     event->accept();\r
180 }\r
181 \r
182 void MainForm::on_actionAuto_Orientation_triggered()\r
183 {\r
184     settings->setValue(AUTO_ORIENTATION, ui->actionAuto_Orientation->isChecked());\r
185     qDebug() << "Auto orientation" << ui->actionAuto_Orientation->isChecked();\r
186     if(ui->actionAuto_Orientation->isChecked())\r
187     {\r
188 #ifdef Q_WS_MAEMO_5\r
189         setAttribute(Qt::WA_Maemo5PortraitOrientation, false);\r
190         setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);\r
191         setAttribute(Qt::WA_Maemo5AutoOrientation, true);\r
192 #endif\r
193     }\r
194     else\r
195     {\r
196         setLandscapeMode(landscape);\r
197     }\r
198 }\r
199 \r
200 void MainForm::on_actionSort_A_Z_triggered()\r
201 {\r
202     settings->setValue(SORT_A_Z, ui->actionSort_A_Z->isChecked());\r
203     MyCheckBoxContainer::getInstance()->setSortAlphabetically(ui->actionSort_A_Z->isChecked());\r
204 }\r
205 \r
206 void MainForm::on_actionLists_triggered()\r
207 {\r
208     changeWidget(2);\r
209 }\r