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