OSDN Git Service

[denncoCreator] Implemented cell code manager dialog. The implementation is incomplet...
[dennco/denncoCreator.git] / Source / codeeditor / dccellscriptseditorpagewidget.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 //
17 //  Created by tkawata on Nov-3, 2012.
18 //
19 #include "dccellscriptseditorpagewidget.h"
20
21 #include <QVBoxLayout>
22 #include <QLabel>
23
24 #include "dccreator.h"
25 #include "dccontainer.h"
26 #include "dccodeeditor.h"
27 #include "dccell.h"
28 #include "dccellcode.h"
29
30 DCCellScriptsEditorPageWidget::DCCellScriptsEditorPageWidget(DCCell *targetCell, QWidget *parent) :
31     QWidget(parent), d_cell(targetCell), d_modified(false)
32 {
33     if (d_cell->getIsCellCodeAssgined())
34     {
35         d_cellCodeOrg = d_cell->getCellCode();
36     }
37     else
38     {
39         d_cellCodeOrg = NULL;
40     }
41
42     d_splitter = new QSplitter(Qt::Vertical, this);
43
44     d_customScriptEditor = new DCCodeEditor(this);
45     d_cellCodeScriptEditor = new DCCodeEditor(this);
46
47     QVBoxLayout *customScriptEditorLayout = new QVBoxLayout;
48     customScriptEditorLayout->addWidget(new QLabel(tr("custom script")));
49     customScriptEditorLayout->addWidget(d_customScriptEditor);
50     QWidget *customScriptWidget = new QWidget;
51     customScriptWidget->setLayout(customScriptEditorLayout);
52
53     QVBoxLayout *cellCodeScriptEditorLayout = new QVBoxLayout;
54     cellCodeScriptEditorLayout->addWidget(new QLabel(tr("cell code script")));
55     cellCodeScriptEditorLayout->addWidget(d_cellCodeScriptEditor);
56     QWidget *cellCodeScriptWidget = new QWidget;
57     cellCodeScriptWidget->setLayout(cellCodeScriptEditorLayout);
58
59     d_splitter->addWidget(customScriptWidget);
60     d_splitter->addWidget(cellCodeScriptWidget);
61
62     QVBoxLayout *rootLayout = new QVBoxLayout;
63     rootLayout->setMargin(0);
64     rootLayout->addWidget(d_splitter);
65
66     setLayout(rootLayout);
67
68     loadScriptsFormFile();
69
70     connect(targetCell, SIGNAL(destroyed()), this, SLOT(cellDestroyed()));
71     connect(targetCell, SIGNAL(cellCodeChanged()), this, SLOT(assignedCellCodeChanged()));
72 }
73
74 DCCellScriptsEditorPageWidget::~DCCellScriptsEditorPageWidget()
75 {
76     this->disconnect();
77 }
78
79 //slot
80 void DCCellScriptsEditorPageWidget::cellDestroyed()
81 {
82     emit editingCellDestroyed(d_cell);
83 }
84
85 //slot
86 void DCCellScriptsEditorPageWidget::assignedCellCodeChanged()
87 {
88     d_cellCodeScriptEditor->disconnect(this);
89
90     if (d_cell->getIsCellCodeAssgined())
91     {
92         DCCellCode *cellCode = d_cell->getCellCode();
93         d_cellCodeScriptEditor->setPlainText(cellCode->getOwnScript());
94         d_cellCodeScriptEditor->setReadOnly(false);
95         d_splitter->widget(1)->show();
96         d_splitter->setStretchFactor(0,1);
97         d_splitter->setStretchFactor(1,4);
98         d_cellCodeScriptEditor->setFocus();
99     }
100     else
101     {
102         d_cellCodeScriptEditor->setPlainText("");
103         d_cellCodeScriptEditor->setReadOnly(true);
104         d_splitter->widget(1)->hide();
105     }
106
107     if (d_modified != getIsModified())
108     {
109         d_modified = getIsModified();
110         emit cellScriptsModifiedStatusChanged(this,d_modified);
111     }
112     connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(cellCodeScriptChanged()));
113 }
114
115 //slot
116 void DCCellScriptsEditorPageWidget::customScriptChanged()
117 {
118     if (d_modified != d_customScriptEditor->document()->isModified())
119     {
120         d_modified = getIsModified();
121         emit cellScriptsModifiedStatusChanged(this, d_modified);
122     }
123 }
124
125 //slot
126 void DCCellScriptsEditorPageWidget::cellCodeScriptChanged()
127 {
128     if (d_modified != d_cellCodeScriptEditor->document()->isModified())
129     {
130         d_modified = getIsModified();
131         emit cellScriptsModifiedStatusChanged(this, d_modified);
132     }
133 }
134
135 bool DCCellScriptsEditorPageWidget::getIsModified() const
136 {
137     if (d_customScriptEditor->document()->isModified())
138         return true;
139
140     if (!d_cell->getIsCellCodeAssgined())
141     {
142         return d_cellCodeOrg != NULL;
143     }
144
145     if (d_cellCodeOrg != d_cell->getCellCode() || d_cellCodeScriptEditor->document()->isModified())
146         return true;
147
148     return false;
149 }
150
151 bool DCCellScriptsEditorPageWidget::loadScriptsFormFile()
152 {
153     d_customScriptEditor->disconnect(this);
154     d_cellCodeScriptEditor->disconnect(this);
155
156     d_customScriptEditor->setPlainText(d_cell->getCustomScript());
157     if (d_cell->getIsCellCodeAssgined())
158     {
159         DCCellCode *cellCode = d_cell->getCellCode();
160         d_cellCodeScriptEditor->setPlainText(cellCode->getOwnScript());
161         d_cellCodeScriptEditor->setReadOnly(false);
162         d_cellCodeScriptEditor->document()->setModified(false);
163         d_splitter->widget(1)->show();
164     }
165     else
166     {
167         d_cellCodeScriptEditor->setPlainText("");
168         d_cellCodeScriptEditor->setReadOnly(true);
169         d_splitter->widget(1)->hide();
170     }
171
172     d_modified = false;
173
174     connect(d_customScriptEditor, SIGNAL(textChanged()), this, SLOT(customScriptChanged()));
175     connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(cellCodeScriptChanged()));
176
177     return true;
178 }
179
180 void DCCellScriptsEditorPageWidget::setReadOnly(bool readOnly)
181 {
182     d_customScriptEditor->setReadOnly(readOnly);
183     d_cellCodeScriptEditor->setReadOnly(readOnly);
184 }
185
186 QSet<DCVCPage*> DCCellScriptsEditorPageWidget::saveScriptsToFile(bool saveModifiedOnly)
187 {
188     bool save = false;
189     bool result = true;
190
191     QSet<DCVCPage*> modifiedPages;
192
193     if (!saveModifiedOnly || d_customScriptEditor->document()->isModified())
194     {
195         save = true;
196         result = saveCustomScriptToFilePrivate();
197         if (result)
198         {
199             modifiedPages.insert(d_cell->getPageBelonging());
200         }
201     }
202
203     if (result)
204     {
205         if (d_cell->getIsCellCodeAssgined())
206         {
207             if (!saveModifiedOnly || d_cellCodeScriptEditor->document()->isModified())
208             {
209                 DCCellCode *cellCode = d_cell->getCellCode();
210                 result = saveCellCodeScriptToFilePrivate(cellCode);
211                 if (result)
212                 {
213                     modifiedPages.insert(cellCode->getPageBelonging());
214                 }
215                 d_cellCodeOrg = cellCode;
216             }
217         }
218         else
219         {
220             d_cellCodeOrg = NULL;
221         }
222     }
223
224     if (result)
225     {
226         d_modified = false;
227         emit cellScriptsSaved(this);
228     }
229     else
230     {
231         QMessageBox::warning(this, tr("Failed to save"), tr("The change couldn't be saved"));
232     }
233     return modifiedPages;
234 }
235
236 bool DCCellScriptsEditorPageWidget::saveCustomScriptOnlyToFile(bool saveModifiedOnly)
237 {
238     bool result = true;
239     bool saved = false;
240     if (!saveModifiedOnly || d_customScriptEditor->document()->isModified())
241     {
242         saved = true;
243         result = saveCustomScriptToFilePrivate();
244     }
245
246     if (result)
247     {
248         if (saved)
249         {
250             d_modified = getIsModified();
251             emit cellScriptsSaved(this);
252         }
253     }
254     else
255     {
256         QMessageBox::warning(this, tr("Failed to save"), tr("The change couldn't be saved"));
257     }
258     return result;
259 }
260
261 bool DCCellScriptsEditorPageWidget::saveCellCodeScriptOnlyToFile(bool saveModifiedOnly)
262 {
263     bool result = true;
264     bool saved = false;
265     DCCellCode *cellCode = NULL;
266     if (d_cell->getIsCellCodeAssgined())
267         cellCode = d_cell->getCellCode();
268
269     if (cellCode && (!saveModifiedOnly || d_cellCodeScriptEditor->document()->isModified()))
270     {
271         saved = true;
272         result = saveCellCodeScriptToFilePrivate(cellCode);
273     }
274
275     if (result)
276     {
277         if (saved)
278         {
279             d_modified = getIsModified();
280             emit cellScriptsSaved(this);
281         }
282     }
283     else
284     {
285         QMessageBox::warning(this, tr("Failed to save"), tr("The change couldn't be saved"));
286     }
287     return result;
288 }
289
290 bool DCCellScriptsEditorPageWidget::saveCustomScriptToFilePrivate()
291 {
292     if (d_cell->saveCustomScript(d_customScriptEditor->toPlainText()))
293     {
294         d_customScriptEditor->document()->setModified(false);
295         return true;
296     }
297     return false;
298 }
299
300 bool DCCellScriptsEditorPageWidget::saveCellCodeScriptToFilePrivate(DCCellCode *cellCode)
301 {
302     if (cellCode->saveScript(d_cellCodeScriptEditor->toPlainText()))
303     {
304         d_cellCodeScriptEditor->document()->setModified(false);
305         return true;
306     }
307     return false;
308 }
309
310 void DCCellScriptsEditorPageWidget::focusCustomScript()
311 {
312     d_splitter->setStretchFactor(0,1);
313     d_splitter->setStretchFactor(1,4);
314     d_customScriptEditor->setFocus();
315 }
316
317 void DCCellScriptsEditorPageWidget::focusCellCodeScript()
318 {
319     d_splitter->setStretchFactor(0,1);
320     d_splitter->setStretchFactor(1,4);
321     d_cellCodeScriptEditor->setFocus();
322
323 }