1 // Copyright (c) 2012 Dennco Project
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.
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.
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/>.
17 // Created by tkawata on Nov-3, 2012.
19 #include "dccellscriptseditorpagewidget.h"
21 #include <QVBoxLayout>
24 #include "dccreator.h"
25 #include "dccontainer.h"
26 #include "dccodeeditor.h"
28 #include "dccellcode.h"
30 DCCellScriptsEditorPageWidget::DCCellScriptsEditorPageWidget(DCCell *targetCell, QWidget *parent) :
31 QWidget(parent), d_cell(targetCell), d_modified(false)
33 if (d_cell->getIsCellCodeAssgined())
35 d_cellCodeOrg = d_cell->getCellCode();
42 d_splitter = new QSplitter(Qt::Vertical, this);
44 d_customScriptEditor = new DCCodeEditor(this);
45 d_cellCodeScriptEditor = new DCCodeEditor(this);
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);
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);
59 d_splitter->addWidget(customScriptWidget);
60 d_splitter->addWidget(cellCodeScriptWidget);
62 QVBoxLayout *rootLayout = new QVBoxLayout;
63 rootLayout->setMargin(0);
64 rootLayout->addWidget(d_splitter);
66 setLayout(rootLayout);
68 loadScriptsFormFile();
70 connect(targetCell, SIGNAL(destroyed()), this, SLOT(cellDestroyed()));
71 connect(targetCell, SIGNAL(cellCodeChanged()), this, SLOT(assignedCellCodeChanged()));
74 DCCellScriptsEditorPageWidget::~DCCellScriptsEditorPageWidget()
80 void DCCellScriptsEditorPageWidget::cellDestroyed()
82 emit editingCellDestroyed(d_cell);
86 void DCCellScriptsEditorPageWidget::assignedCellCodeChanged()
88 d_cellCodeScriptEditor->disconnect(this);
90 if (d_cell->getIsCellCodeAssgined())
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();
102 d_cellCodeScriptEditor->setPlainText("");
103 d_cellCodeScriptEditor->setReadOnly(true);
104 d_splitter->widget(1)->hide();
107 if (d_modified != getIsModified())
109 d_modified = getIsModified();
110 emit cellScriptsModifiedStatusChanged(this,d_modified);
112 connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(cellCodeScriptChanged()));
116 void DCCellScriptsEditorPageWidget::customScriptChanged()
118 if (d_modified != d_customScriptEditor->document()->isModified())
120 d_modified = getIsModified();
121 emit cellScriptsModifiedStatusChanged(this, d_modified);
126 void DCCellScriptsEditorPageWidget::cellCodeScriptChanged()
128 if (d_modified != d_cellCodeScriptEditor->document()->isModified())
130 d_modified = getIsModified();
131 emit cellScriptsModifiedStatusChanged(this, d_modified);
135 bool DCCellScriptsEditorPageWidget::getIsModified() const
137 if (d_customScriptEditor->document()->isModified())
140 if (!d_cell->getIsCellCodeAssgined())
142 return d_cellCodeOrg != NULL;
145 if (d_cellCodeOrg != d_cell->getCellCode() || d_cellCodeScriptEditor->document()->isModified())
151 bool DCCellScriptsEditorPageWidget::loadScriptsFormFile()
153 d_customScriptEditor->disconnect(this);
154 d_cellCodeScriptEditor->disconnect(this);
156 d_customScriptEditor->setPlainText(d_cell->getCustomScript());
157 if (d_cell->getIsCellCodeAssgined())
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();
167 d_cellCodeScriptEditor->setPlainText("");
168 d_cellCodeScriptEditor->setReadOnly(true);
169 d_splitter->widget(1)->hide();
174 connect(d_customScriptEditor, SIGNAL(textChanged()), this, SLOT(customScriptChanged()));
175 connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(cellCodeScriptChanged()));
180 void DCCellScriptsEditorPageWidget::setReadOnly(bool readOnly)
182 d_customScriptEditor->setReadOnly(readOnly);
183 d_cellCodeScriptEditor->setReadOnly(readOnly);
186 QSet<DCVCPage*> DCCellScriptsEditorPageWidget::saveScriptsToFile(bool saveModifiedOnly)
191 QSet<DCVCPage*> modifiedPages;
193 if (!saveModifiedOnly || d_customScriptEditor->document()->isModified())
196 result = saveCustomScriptToFilePrivate();
199 modifiedPages.insert(d_cell->getPageBelonging());
205 if (d_cell->getIsCellCodeAssgined())
207 if (!saveModifiedOnly || d_cellCodeScriptEditor->document()->isModified())
209 DCCellCode *cellCode = d_cell->getCellCode();
210 result = saveCellCodeScriptToFilePrivate(cellCode);
213 modifiedPages.insert(cellCode->getPageBelonging());
215 d_cellCodeOrg = cellCode;
220 d_cellCodeOrg = NULL;
227 emit cellScriptsSaved(this);
231 QMessageBox::warning(this, tr("Failed to save"), tr("The change couldn't be saved"));
233 return modifiedPages;
236 bool DCCellScriptsEditorPageWidget::saveCustomScriptOnlyToFile(bool saveModifiedOnly)
240 if (!saveModifiedOnly || d_customScriptEditor->document()->isModified())
243 result = saveCustomScriptToFilePrivate();
250 d_modified = getIsModified();
251 emit cellScriptsSaved(this);
256 QMessageBox::warning(this, tr("Failed to save"), tr("The change couldn't be saved"));
261 bool DCCellScriptsEditorPageWidget::saveCellCodeScriptOnlyToFile(bool saveModifiedOnly)
265 DCCellCode *cellCode = NULL;
266 if (d_cell->getIsCellCodeAssgined())
267 cellCode = d_cell->getCellCode();
269 if (cellCode && (!saveModifiedOnly || d_cellCodeScriptEditor->document()->isModified()))
272 result = saveCellCodeScriptToFilePrivate(cellCode);
279 d_modified = getIsModified();
280 emit cellScriptsSaved(this);
285 QMessageBox::warning(this, tr("Failed to save"), tr("The change couldn't be saved"));
290 bool DCCellScriptsEditorPageWidget::saveCustomScriptToFilePrivate()
292 if (d_cell->saveCustomScript(d_customScriptEditor->toPlainText()))
294 d_customScriptEditor->document()->setModified(false);
300 bool DCCellScriptsEditorPageWidget::saveCellCodeScriptToFilePrivate(DCCellCode *cellCode)
302 if (cellCode->saveScript(d_cellCodeScriptEditor->toPlainText()))
304 d_cellCodeScriptEditor->document()->setModified(false);
310 void DCCellScriptsEditorPageWidget::focusCustomScript()
312 d_splitter->setStretchFactor(0,1);
313 d_splitter->setStretchFactor(1,4);
314 d_customScriptEditor->setFocus();
317 void DCCellScriptsEditorPageWidget::focusCellCodeScript()
319 d_splitter->setStretchFactor(0,1);
320 d_splitter->setStretchFactor(1,4);
321 d_cellCodeScriptEditor->setFocus();