OSDN Git Service

[denncoCreator] prepare for supporting external editors for the code editor.
authortkawata <tkawata@users.sourceforge.jp>
Sat, 8 Dec 2012 14:54:46 +0000 (23:54 +0900)
committertkawata <tkawata@users.sourceforge.jp>
Sat, 8 Dec 2012 14:54:46 +0000 (23:54 +0900)
Source/codeeditor/dccellscriptseditorpagewidget.cpp
Source/codeeditor/dccellscriptseditorpagewidget.h
Source/codeeditor/dccellscriptseditortabwidget.cpp
Source/codeeditor/dccellscriptseditortabwidget.h
Source/codeeditor/dceditscriptfolder.cpp [new file with mode: 0644]
Source/codeeditor/dceditscriptfolder.h [new file with mode: 0644]
Source/denncoCreator.pro

index 31e7e23..8a40b0b 100644 (file)
@@ -65,33 +65,75 @@ DCCellScriptsEditorPageWidget::DCCellScriptsEditorPageWidget(DCCell *targetCell,
 
     setLayout(rootLayout);
 
-    loadScriptsFormFile();
+    d_customScriptFolder.attach(d_cell);
+    d_cellCodeScriptFolder.attach(d_cellCodeOrg);
 
     connect(targetCell, SIGNAL(destroyed()), this, SLOT(cellDestroyed()));
     connect(targetCell, SIGNAL(cellCodeChanged()), this, SLOT(assignedCellCodeChanged()));
+    connect(&d_customScriptFolder, SIGNAL(scriptChanged()), this, SLOT(folderCustomScriptChanged()));
+    connect(&d_cellCodeScriptFolder, SIGNAL(scriptChanged()), this, SLOT(folderCellCodeScriptChanged()));
+    connect(&d_customScriptFolder, SIGNAL(modificationStatusChanged(bool)), this, SLOT(folderCustomScriptModifiedStatusChanged(bool)));
+    connect(&d_cellCodeScriptFolder, SIGNAL(modificationStatusChanged(bool)), this, SLOT(folderCellCodeScriptModifiedStatusChanged(bool)));
+
+    loadScripts();
+}
+
+bool DCCellScriptsEditorPageWidget::loadScripts()
+{
+    d_customScriptEditor->disconnect(this);
+    d_cellCodeScriptEditor->disconnect(this);
+
+    d_customScriptEditor->setPlainText(d_customScriptFolder.getCurrentScript());
+    if (d_cell->getIsCellCodeAssgined())
+    {
+        d_cellCodeScriptEditor->setPlainText(d_cellCodeScriptFolder.getCurrentScript());
+        d_cellCodeScriptEditor->setReadOnly(false);
+        d_cellCodeScriptEditor->document()->setModified(false);
+        d_splitter->widget(1)->show();
+    }
+    else
+    {
+        d_cellCodeScriptEditor->setPlainText("");
+        d_cellCodeScriptEditor->setReadOnly(true);
+        d_splitter->widget(1)->hide();
+    }
+
+    updateModifiedStatus();
+
+    connect(d_customScriptEditor, SIGNAL(textChanged()), this, SLOT(editorCustomScriptChanged()));
+    connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(editorCellCodeScriptChanged()));
+
+    return true;
 }
 
 DCCellScriptsEditorPageWidget::~DCCellScriptsEditorPageWidget()
 {
-    disconnect(d_cell, 0, this, 0);
+    if (d_cell)
+    {
+        disconnect(d_cell, 0, this, 0);
+    }
     this->disconnect();
 }
 
 //slot
 void DCCellScriptsEditorPageWidget::cellDestroyed()
 {
-    emit editingCellDestroyed(d_cell);
+    DCCell *cell = d_cell;
+    d_cell = NULL;
+    emit editingCellDestroyed(cell);
 }
 
 //slot
 void DCCellScriptsEditorPageWidget::assignedCellCodeChanged()
 {
     d_cellCodeScriptEditor->disconnect(this);
+    d_cellCodeScriptFolder.deattach();
 
     if (d_cell->getIsCellCodeAssgined())
     {
         DCCellCode *cellCode = d_cell->getCellCode();
-        d_cellCodeScriptEditor->setPlainText(cellCode->getOwnScript());
+        d_cellCodeScriptFolder.attach(cellCode);
+        d_cellCodeScriptEditor->setPlainText(d_cellCodeScriptFolder.getCurrentScript());
         d_cellCodeScriptEditor->setReadOnly(false);
         d_splitter->widget(1)->show();
         d_splitter->setStretchFactor(0,1);
@@ -105,85 +147,78 @@ void DCCellScriptsEditorPageWidget::assignedCellCodeChanged()
         d_splitter->widget(1)->hide();
     }
 
-    if (d_modified != getIsModified())
-    {
-        d_modified = getIsModified();
-        emit cellScriptsModifiedStatusChanged(this,d_modified);
-    }
-    connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(cellCodeScriptChanged()));
+    updateModifiedStatus();
+    connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(editorCellCodeScriptChanged()));
 }
 
 //slot
-void DCCellScriptsEditorPageWidget::customScriptChanged()
+void DCCellScriptsEditorPageWidget::editorCustomScriptChanged()
 {
-    if (d_modified != d_customScriptEditor->document()->isModified())
-    {
-        d_modified = getIsModified();
-        emit cellScriptsModifiedStatusChanged(this, d_modified);
-    }
+    d_customScriptFolder.setScript(d_customScriptEditor->toPlainText());
 }
 
 //slot
-void DCCellScriptsEditorPageWidget::cellCodeScriptChanged()
+void DCCellScriptsEditorPageWidget::editorCellCodeScriptChanged()
 {
-    if (d_modified != d_cellCodeScriptEditor->document()->isModified())
-    {
-        d_modified = getIsModified();
-        emit cellScriptsModifiedStatusChanged(this, d_modified);
-    }
+    d_cellCodeScriptFolder.setScript(d_cellCodeScriptEditor->toPlainText());
 }
 
-bool DCCellScriptsEditorPageWidget::getIsModified() const
+//slot
+void DCCellScriptsEditorPageWidget::folderCustomScriptChanged()
 {
-    if (d_customScriptEditor->document()->isModified())
-        return true;
+    d_customScriptEditor->disconnect(this);
+    d_customScriptEditor->setPlainText(d_customScriptFolder.getCurrentScript());
+    connect(d_customScriptEditor, SIGNAL(textChanged()), this, SLOT(editorCustomScriptChanged()));
+}
 
-    if (!d_cell->getIsCellCodeAssgined())
-    {
-        return d_cellCodeOrg != NULL;
-    }
+//slot
+void DCCellScriptsEditorPageWidget::folderCellCodeScriptChanged()
+{
+    d_cellCodeScriptEditor->disconnect(this);
+    d_cellCodeScriptEditor->setPlainText(d_cellCodeScriptFolder.getCurrentScript());
+    connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(editorCellCodeScriptChanged()));
+}
 
-    if (d_cellCodeOrg != d_cell->getCellCode() || d_cellCodeScriptEditor->document()->isModified())
-        return true;
+//slot
+void DCCellScriptsEditorPageWidget::folderCustomScriptModifiedStatusChanged(bool modified)
+{
+    (void)modified;
+    updateModifiedStatus();
+}
 
-    return false;
+//slot
+void DCCellScriptsEditorPageWidget::folderCellCodeScriptModifiedStatusChanged(bool modified)
+{
+    (void)modified;
+    updateModifiedStatus();
 }
 
-bool DCCellScriptsEditorPageWidget::loadScriptsFormFile()
+void DCCellScriptsEditorPageWidget::updateModifiedStatus()
 {
-    d_customScriptEditor->disconnect(this);
-    d_cellCodeScriptEditor->disconnect(this);
+    bool modified = d_customScriptFolder.getIsModified();
+    if (!modified)
+        modified = d_cellCodeScriptFolder.getIsModified();
 
-    d_customScriptEditor->setPlainText(d_cell->getCustomScript());
-    if (d_cell->getIsCellCodeAssgined())
+    if (modified != d_modified)
     {
-        DCCellCode *cellCode = d_cell->getCellCode();
-        d_cellCodeScriptEditor->setPlainText(cellCode->getOwnScript());
-        d_cellCodeScriptEditor->setReadOnly(false);
-        d_cellCodeScriptEditor->document()->setModified(false);
-        d_splitter->widget(1)->show();
-    }
-    else
-    {
-        d_cellCodeScriptEditor->setPlainText("");
-        d_cellCodeScriptEditor->setReadOnly(true);
-        d_splitter->widget(1)->hide();
+        d_modified = modified;
+        emit cellScriptsModifiedStatusChanged(this, modified);
     }
+}
 
-    d_modified = false;
-
-    connect(d_customScriptEditor, SIGNAL(textChanged()), this, SLOT(customScriptChanged()));
-    connect(d_cellCodeScriptEditor, SIGNAL(textChanged()), this, SLOT(cellCodeScriptChanged()));
-
-    return true;
+bool DCCellScriptsEditorPageWidget::getIsModified() const
+{
+    return d_modified;
 }
 
+
 void DCCellScriptsEditorPageWidget::setReadOnly(bool readOnly)
 {
     d_customScriptEditor->setReadOnly(readOnly);
     d_cellCodeScriptEditor->setReadOnly(readOnly);
 }
 
+
 QSet<DCVCPage*> DCCellScriptsEditorPageWidget::saveScriptsToFile(bool saveModifiedOnly)
 {
     bool save = false;
@@ -191,10 +226,10 @@ QSet<DCVCPage*> DCCellScriptsEditorPageWidget::saveScriptsToFile(bool saveModifi
 
     QSet<DCVCPage*> modifiedPages;
 
-    if (!saveModifiedOnly || d_customScriptEditor->document()->isModified())
+    if (!saveModifiedOnly || d_customScriptFolder.getIsModified())
     {
         save = true;
-        result = saveCustomScriptToFilePrivate();
+        result = d_customScriptFolder.saveScript();
         if (result)
         {
             modifiedPages.insert(d_cell->getPageBelonging());
@@ -205,10 +240,10 @@ QSet<DCVCPage*> DCCellScriptsEditorPageWidget::saveScriptsToFile(bool saveModifi
     {
         if (d_cell->getIsCellCodeAssgined())
         {
-            if (!saveModifiedOnly || d_cellCodeScriptEditor->document()->isModified())
+            if (!saveModifiedOnly || d_cellCodeScriptFolder.getIsModified())
             {
+                result = d_cellCodeScriptFolder.saveScript();
                 DCCellCode *cellCode = d_cell->getCellCode();
-                result = saveCellCodeScriptToFilePrivate(cellCode);
                 if (result)
                 {
                     modifiedPages.insert(cellCode->getPageBelonging());
@@ -238,10 +273,10 @@ bool DCCellScriptsEditorPageWidget::saveCustomScriptOnlyToFile(bool saveModified
 {
     bool result = true;
     bool saved = false;
-    if (!saveModifiedOnly || d_customScriptEditor->document()->isModified())
+    if (!saveModifiedOnly || d_customScriptFolder.getIsModified())
     {
         saved = true;
-        result = saveCustomScriptToFilePrivate();
+        result = d_customScriptFolder.saveScript();
     }
 
     if (result)
@@ -267,10 +302,10 @@ bool DCCellScriptsEditorPageWidget::saveCellCodeScriptOnlyToFile(bool saveModifi
     if (d_cell->getIsCellCodeAssgined())
         cellCode = d_cell->getCellCode();
 
-    if (cellCode && (!saveModifiedOnly || d_cellCodeScriptEditor->document()->isModified()))
+    if (cellCode && (!saveModifiedOnly || d_cellCodeScriptFolder.getIsModified()))
     {
         saved = true;
-        result = saveCellCodeScriptToFilePrivate(cellCode);
+        result = d_cellCodeScriptFolder.saveScript();
     }
 
     if (result)
@@ -288,25 +323,6 @@ bool DCCellScriptsEditorPageWidget::saveCellCodeScriptOnlyToFile(bool saveModifi
     return result;
 }
 
-bool DCCellScriptsEditorPageWidget::saveCustomScriptToFilePrivate()
-{
-    if (d_cell->saveCustomScript(d_customScriptEditor->toPlainText()))
-    {
-        d_customScriptEditor->document()->setModified(false);
-        return true;
-    }
-    return false;
-}
-
-bool DCCellScriptsEditorPageWidget::saveCellCodeScriptToFilePrivate(DCCellCode *cellCode)
-{
-    if (cellCode->saveScript(d_cellCodeScriptEditor->toPlainText()))
-    {
-        d_cellCodeScriptEditor->document()->setModified(false);
-        return true;
-    }
-    return false;
-}
 
 void DCCellScriptsEditorPageWidget::focusCustomScript()
 {
index d5e458a..e82ecd3 100644 (file)
@@ -22,6 +22,8 @@
 #include <QWidget>
 #include <QSplitter>
 
+#include "dceditscriptfolder.h"
+
 class DCCell;
 class DCCellCode;
 class DCCodeEditor;
@@ -37,10 +39,13 @@ class DCCellScriptsEditorPageWidget : public QWidget
     DCCodeEditor    *d_cellCodeScriptEditor;
 
     QSplitter       *d_splitter;
-    bool            d_modified;
 
-    bool            saveCustomScriptToFilePrivate();
-    bool            saveCellCodeScriptToFilePrivate(DCCellCode *cellCode);
+    bool                d_modified;
+    DCEditScriptFolder  d_customScriptFolder;
+    DCEditScriptFolder  d_cellCodeScriptFolder;
+
+    bool            loadScripts();
+    void            updateModifiedStatus();
 
 public:
     DCCellScriptsEditorPageWidget(DCCell *targetCell, QWidget *parent = 0);
@@ -48,7 +53,6 @@ public:
 
     bool            getIsModified() const;
 
-    bool            loadScriptsFormFile();
     void            setReadOnly(bool readOnly);
     QSet<DCVCPage*> saveScriptsToFile(bool saveModifiedOnly);
     bool            saveCustomScriptOnlyToFile(bool saveModifiedOnly);
@@ -65,8 +69,12 @@ signals:
 private slots:
     void assignedCellCodeChanged();
     void cellDestroyed();
-    void customScriptChanged();
-    void cellCodeScriptChanged();
+    void editorCustomScriptChanged();
+    void editorCellCodeScriptChanged();
+    void folderCustomScriptChanged();
+    void folderCellCodeScriptChanged();
+    void folderCustomScriptModifiedStatusChanged(bool modified);
+    void folderCellCodeScriptModifiedStatusChanged(bool modified);
 };
 
 #endif // DCCELLCODEEDITORPAGEWIDGET_H
index 54cc057..09ea1a9 100644 (file)
@@ -151,10 +151,7 @@ void DCCellScriptsEditorTabWidget::slotTabPageContentModifiedStatusChanged(QWidg
     QMap<QWidget*, DCCell*>::iterator it = d_targets.find(tabPage);
     if (it != d_targets.end())
     {
-        QString tabTitle = QString::fromStdString(it.value()->getName());
-        if (modified)
-            tabTitle.append(" * ");
-        setTabText(index, tabTitle);
+        setTabText(index, getTabTitle(it.value(), tabPage));
         emit editTabContentModifiedStatusChanged(it.value(), modified);
     }
 }
@@ -174,7 +171,7 @@ void DCCellScriptsEditorTabWidget::slotTabPageContentSaved(QWidget *tabPage)
     QMap<QWidget*, DCCell*>::iterator it = d_targets.find(tabPage);
     if (it != d_targets.end())
     {
-        QString tabTitle = QString::fromStdString(it.value()->getName());
+        QString tabTitle = getTabTitle(it.value(), tabPage);
         setTabText(index, tabTitle);
 
         emit editTabContentModifiedStatusChanged(it.value(), false);
@@ -193,6 +190,16 @@ void DCCellScriptsEditorTabWidget::setCurrentTabReadOnly(bool readOnly)
     }
 }
 
+QString DCCellScriptsEditorTabWidget::getTabTitle(DCCell *cell, QWidget *tabPageWidget)
+{
+    QString tabTitle = QString::fromStdString(cell->getName());
+    DCCellScriptsEditorPageWidget *w = dynamic_cast<DCCellScriptsEditorPageWidget*>(tabPageWidget);
+    if (w && w->getIsModified())
+        tabTitle.append(" * ");
+
+    return tabTitle;
+}
+
 void DCCellScriptsEditorTabWidget::openTab(DCCell *target, bool focusInCellCodeScriptFirst)
 {
     DCCellScriptsEditorPageWidget *targetWidget = NULL;
@@ -219,7 +226,8 @@ void DCCellScriptsEditorTabWidget::openTab(DCCell *target, bool focusInCellCodeS
     {
         targetWidget = new DCCellScriptsEditorPageWidget(target, this);
         d_targets.insert(targetWidget, target);
-        setCurrentIndex(addTab(targetWidget, QString::fromStdString(target->getName())));
+
+        setCurrentIndex(addTab(targetWidget, getTabTitle(target, targetWidget)));
         connect(targetWidget, SIGNAL(editingCellDestroyed(DCCell*)), this, SLOT(slotCellDestroyed(DCCell*)));
         connect(targetWidget, SIGNAL(cellScriptsModifiedStatusChanged(QWidget*, bool)), this, SLOT(slotTabPageContentModifiedStatusChanged(QWidget*, bool)));
         connect(targetWidget, SIGNAL(cellScriptsSaved(QWidget*)), this, SLOT(slotTabPageContentSaved(QWidget*)));
index 5356875..c69d84f 100644 (file)
@@ -34,6 +34,7 @@ class DCCellScriptsEditorTabWidget : public QTabWidget
     DCCellScriptsEditorWindow  *d_parent;
     QMap<QWidget*, DCCell*> d_targets;
 
+    QString getTabTitle(DCCell *cell, QWidget *tabPageWidget);
 
 public:
     explicit DCCellScriptsEditorTabWidget(DCCellScriptsEditorWindow *parent);
diff --git a/Source/codeeditor/dceditscriptfolder.cpp b/Source/codeeditor/dceditscriptfolder.cpp
new file mode 100644 (file)
index 0000000..1a3c783
--- /dev/null
@@ -0,0 +1,493 @@
+//  Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+//
+//  Created by tkawata on Dec-8, 2012.
+//
+#include "dceditscriptfolder.h"
+
+#include <QMap>
+#include <QCryptographicHash>
+
+#include "dccell.h"
+#include "dccellcode.h"
+
+class ScriptAttacher;
+class ScriptWatcher;
+
+class ScriptManager
+{
+    static ScriptManager* getInstance();
+    ScriptManager();
+    ~ScriptManager();
+    ScriptAttacher* createCustomScriptAttacherPrivate(DCEditScriptFolder *folder, DCCell *cell);
+    ScriptAttacher* createCellCodeScriptAttacherPrivate(DCEditScriptFolder *folder, DCCellCode *cellCode);
+
+    static ScriptManager *instance;
+    QMap<DCCell*, ScriptWatcher*>       d_customScriptWatchers;
+    QMap<DCCellCode*, ScriptWatcher*>   d_cellCodeScriptWatchers;
+
+public:
+    static ScriptAttacher* createCustomScriptAttacher(DCEditScriptFolder *folder, DCCell *cell);
+    static ScriptAttacher* createCellCodeScriptAttacher(DCEditScriptFolder *folder, DCCellCode *cell);
+    QString getCurrentScript(ScriptAttacher *attacher);
+    bool getIsModified(const ScriptAttacher *attacher) const;
+    void setScript(ScriptAttacher *attacher, const QString& newScript);
+    bool saveScript(ScriptAttacher *attacher);
+    void attacherDeleting(ScriptAttacher *attacher);
+
+    void callbackScriptChanged(ScriptWatcher *watcher, const ScriptAttacher *modifier);
+    void callbackModificationStatusChanged(ScriptWatcher *watcher, ScriptAttacher* modifier, bool modified);
+
+};
+
+class ScriptWatcher
+{
+    bool d_scriptLoadedFromFile;
+    void setModified(ScriptAttacher *modifier, bool modified)
+    {
+        if (d_modified != modified)
+        {
+            d_modified = modified;
+            d_manager->callbackModificationStatusChanged(this, modifier, modified);
+        }
+    }
+
+public:
+    enum E_TYPE { E_CUSTOMSCRIPT, E_CELLCODESCRIPT};
+
+protected:
+    ScriptManager           *d_manager;
+    E_TYPE                   d_type;
+    QList<ScriptAttacher*>  d_attachers;
+    QString                 d_script;
+    QByteArray              d_hashForOriginalScript;
+    bool                    d_modified;
+
+    ScriptWatcher(ScriptManager *manager, E_TYPE type) :
+        d_scriptLoadedFromFile(false), d_manager(manager), d_type(type), d_modified(false)
+    {
+    }
+
+    virtual bool loadScriptFromFile() = 0;
+    virtual bool saveScriptToFile() = 0;
+
+public:
+    virtual ~ScriptWatcher() {}
+    E_TYPE getType() const { return d_type; }
+    void attach(ScriptAttacher *attacher)
+    {
+        d_attachers.append(attacher);
+    }
+
+    void deattach(ScriptAttacher *attacher)
+    {
+        d_attachers.removeOne(attacher);
+    }
+
+    int getRefCount() const
+    {
+        return d_attachers.count();
+    }
+
+    const QList<ScriptAttacher*>* getAttachers() { return &d_attachers; }
+
+    virtual QString getCurrentScript(ScriptAttacher *attacher)
+    {
+        if (!d_scriptLoadedFromFile)
+        {
+            d_hashForOriginalScript.clear();
+            d_scriptLoadedFromFile = loadScriptFromFile();
+            if (d_scriptLoadedFromFile)
+            {
+                d_hashForOriginalScript = QCryptographicHash::hash(d_script.toLocal8Bit(), QCryptographicHash::Md5);
+                setModified(attacher,false);
+            }
+        }
+        return d_script;
+    }
+
+    virtual bool getIsModified(const ScriptAttacher *attacher) const
+    {
+        return d_modified;
+    }
+
+    virtual void setScript(ScriptAttacher *attacher, const QString& newScript)
+    {
+        d_script = newScript;
+        d_manager->callbackScriptChanged(this, attacher);
+        setModified(attacher, d_hashForOriginalScript != QCryptographicHash::hash(newScript.toLocal8Bit(), QCryptographicHash::Md5));
+    }
+
+    virtual bool saveScript(ScriptAttacher *attacher)
+    {
+        bool succeeded = saveScriptToFile();
+        if (succeeded)
+        {
+            d_hashForOriginalScript = QCryptographicHash::hash(d_script.toLocal8Bit(), QCryptographicHash::Md5);
+            setModified(attacher,false);
+        }
+        return succeeded;
+    }
+
+};
+
+class CustomScriptWatcher : public ScriptWatcher
+{
+    DCCell *d_cell;
+
+
+protected:
+    virtual bool loadScriptFromFile()
+    {
+        d_script = d_cell->getCustomScript();
+        return true;
+    }
+
+    virtual bool saveScriptToFile()
+    {
+        return d_cell->saveCustomScript(d_script);
+    }
+
+public:
+    CustomScriptWatcher(ScriptManager *manager, DCCell *cell)
+        : ScriptWatcher(manager, E_CUSTOMSCRIPT), d_cell(cell)
+    {
+    }
+};
+
+class CellCodeScriptWatcher : public ScriptWatcher
+{
+    DCCellCode *d_cellCode;
+
+protected:
+    virtual bool loadScriptFromFile()
+    {
+        d_script = d_cellCode->getOwnScript();
+        return true;
+    }
+
+    virtual bool saveScriptToFile()
+    {
+        return d_cellCode->saveScript(d_script);
+    }
+
+public:
+    CellCodeScriptWatcher(ScriptManager *manager, DCCellCode *cellCode)
+        : ScriptWatcher(manager, E_CELLCODESCRIPT), d_cellCode(cellCode)
+    {
+    }
+
+};
+
+class ScriptAttacher
+{
+    DCEditScriptFolder  *d_folder;
+    ScriptManager       *d_manager;
+    ScriptWatcher       *d_watcher;
+
+public:
+    ScriptAttacher(DCEditScriptFolder *folder, ScriptManager *manager, ScriptWatcher *watcher)
+        : d_folder(folder), d_manager(manager), d_watcher(watcher)
+    {
+    }
+
+    ~ScriptAttacher()
+    {
+        d_manager->attacherDeleting(this);
+    }
+
+    ScriptWatcher* getWatcher() const
+    {
+        return d_watcher;
+    }
+
+    QString getCurrentScript()
+    {
+        return d_manager->getCurrentScript(this);
+    }
+
+    bool getIsModified() const
+    {
+        return d_manager->getIsModified(this);
+    }
+
+    void setScript(const QString& newScript)
+    {
+        d_manager->setScript(this, newScript);
+    }
+
+    bool saveScript()
+    {
+        return d_manager->saveScript(this);
+    }
+
+    void callbackScriptChanged()
+    {
+        d_folder->callbackScriptChanged();
+    }
+
+    void callbackModificationStatusChanged(bool modified)
+    {
+        d_folder->callbackModificationStatusChanged(modified);
+    }
+
+};
+
+ScriptManager* ScriptManager::instance = NULL;
+
+//static
+ScriptAttacher* ScriptManager::createCustomScriptAttacher(DCEditScriptFolder *folder, DCCell *cell)
+{
+    ScriptManager *instance = getInstance();
+    return instance->createCustomScriptAttacherPrivate(folder, cell);
+}
+
+//static
+ScriptAttacher* ScriptManager::createCellCodeScriptAttacher(DCEditScriptFolder *folder, DCCellCode *cell)
+{
+    ScriptManager *instance = getInstance();
+    return instance->createCellCodeScriptAttacherPrivate(folder, cell);
+}
+
+QString ScriptManager::getCurrentScript(ScriptAttacher *attacher)
+{
+    return attacher->getWatcher()->getCurrentScript(attacher);
+}
+
+bool ScriptManager::getIsModified(const ScriptAttacher *attacher) const
+{
+    return attacher->getWatcher()->getIsModified(attacher);
+}
+
+void ScriptManager::setScript(ScriptAttacher *attacher, const QString& newScript)
+{
+    attacher->getWatcher()->setScript(attacher, newScript);
+}
+
+bool ScriptManager::saveScript(ScriptAttacher *attacher)
+{
+    return attacher->getWatcher()->saveScript(attacher);
+}
+
+
+//static
+ScriptManager* ScriptManager::getInstance()
+{
+    if (!instance)
+    {
+        instance = new ScriptManager();
+    }
+    return instance;
+}
+
+ScriptManager::ScriptManager()
+{
+}
+
+ScriptManager::~ScriptManager()
+{
+}
+
+ScriptAttacher* ScriptManager::createCustomScriptAttacherPrivate(DCEditScriptFolder *folder, DCCell *cell)
+{
+    QMap<DCCell*, ScriptWatcher*>::const_iterator it = d_customScriptWatchers.find(cell);
+
+    ScriptWatcher* watcher = NULL;
+    if (it != d_customScriptWatchers.end())
+    {
+        watcher = it.value();
+    }
+    else
+    {
+        watcher = new CustomScriptWatcher(this, cell);
+        d_customScriptWatchers.insert(cell, watcher);
+    }
+
+    ScriptAttacher *attacher = new ScriptAttacher(folder, this, watcher);
+    watcher->attach(attacher);
+
+    return attacher;
+}
+
+ScriptAttacher* ScriptManager::createCellCodeScriptAttacherPrivate(DCEditScriptFolder *folder, DCCellCode *cellCode)
+{
+    QMap<DCCellCode*, ScriptWatcher*>::const_iterator it = d_cellCodeScriptWatchers.find(cellCode);
+
+    ScriptWatcher* watcher = NULL;
+    if (it != d_cellCodeScriptWatchers.end())
+    {
+        watcher = it.value();
+    }
+    else
+    {
+        watcher = new CellCodeScriptWatcher(this, cellCode);
+        d_cellCodeScriptWatchers.insert(cellCode, watcher);
+    }
+
+    ScriptAttacher *attacher = new ScriptAttacher(folder, this, watcher);
+    watcher->attach(attacher);
+
+    return attacher;
+}
+
+void ScriptManager::attacherDeleting(ScriptAttacher *attacher)
+{
+    ScriptWatcher *watcher = attacher->getWatcher();
+    if (watcher)
+    {
+        watcher->deattach(attacher);
+        if (watcher->getRefCount() <= 0)
+        {
+            if (watcher->getType() == ScriptWatcher::E_CUSTOMSCRIPT)
+            {
+                QMap<DCCell*, ScriptWatcher*>::iterator i = d_customScriptWatchers.begin();
+                while (i != d_customScriptWatchers.end())
+                {
+                    if (i.value() == watcher)
+                    {
+                        d_customScriptWatchers.erase(i);
+                        delete watcher;
+                        break;
+                    }
+                    ++i;
+                }
+            }
+            else
+            {
+                QMap<DCCellCode*, ScriptWatcher*>::iterator i = d_cellCodeScriptWatchers.begin();
+                while (i != d_cellCodeScriptWatchers.end())
+                {
+                    if (i.value() == watcher)
+                    {
+                        d_cellCodeScriptWatchers.erase(i);
+                        delete watcher;
+                        break;
+                    }
+                    ++i;
+                }
+            }
+        }
+    }
+}
+
+void ScriptManager::callbackScriptChanged(ScriptWatcher *watcher, const ScriptAttacher *modifier)
+{
+    const QList<ScriptAttacher*> *attachers = watcher->getAttachers();
+
+    QList<ScriptAttacher*>::const_iterator it = attachers->constBegin();
+    while (it != attachers->constEnd())
+    {
+        if (*it != modifier)
+        {
+            (*it)->callbackScriptChanged();
+        }
+        ++it;
+    }
+}
+
+void ScriptManager::callbackModificationStatusChanged(ScriptWatcher *watcher, ScriptAttacher *modifier, bool modified)
+{
+    (void)modifier;
+
+    const QList<ScriptAttacher*> *attachers = watcher->getAttachers();
+
+    QList<ScriptAttacher*>::const_iterator it = attachers->constBegin();
+    while (it != attachers->constEnd())
+    {
+        (*it)->callbackModificationStatusChanged(modified);
+        ++it;
+    }
+}
+
+
+/*--------------------
+ * DCEditScriptFolder
+ *-------------------*/
+DCEditScriptFolder::DCEditScriptFolder(QObject *parent) :
+    QObject(parent), d_attacher(NULL)
+{
+}
+
+DCEditScriptFolder::~DCEditScriptFolder()
+{
+    deattach();
+}
+
+void DCEditScriptFolder::attach(DCCell *cell)
+{
+    deattach();
+    d_attacher = ScriptManager::createCustomScriptAttacher(this, cell);
+}
+
+void DCEditScriptFolder::attach(DCCellCode *cellCode)
+{
+    deattach();
+    d_attacher = ScriptManager::createCellCodeScriptAttacher(this, cellCode);
+}
+
+void DCEditScriptFolder::deattach()
+{
+    if (d_attacher)
+    {
+        delete d_attacher;
+        d_attacher = NULL;
+    }
+}
+
+QString DCEditScriptFolder::getCurrentScript()
+{
+    if (d_attacher)
+    {
+        return d_attacher->getCurrentScript();
+    }
+    return "";
+}
+bool DCEditScriptFolder::getIsModified() const
+{
+    if (d_attacher)
+    {
+        return d_attacher->getIsModified();
+    }
+    return false;
+}
+
+void DCEditScriptFolder::setScript(const QString& newScript)
+{
+    if (d_attacher)
+    {
+        d_attacher->setScript(newScript);
+    }
+}
+
+bool DCEditScriptFolder::saveScript()
+{
+    if (d_attacher)
+    {
+        return d_attacher->saveScript();
+    }
+    return false;
+}
+
+void DCEditScriptFolder::callbackScriptChanged()
+{
+    emit scriptChanged();
+}
+
+void DCEditScriptFolder::callbackModificationStatusChanged(bool modified)
+{
+    emit modificationStatusChanged(modified);
+}
+
diff --git a/Source/codeeditor/dceditscriptfolder.h b/Source/codeeditor/dceditscriptfolder.h
new file mode 100644 (file)
index 0000000..205ac1f
--- /dev/null
@@ -0,0 +1,60 @@
+//  Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+//
+//  Created by tkawata on Dec-8, 2012.
+//
+#ifndef DCEDITSCRIPTFOLDER_H
+#define DCEDITSCRIPTFOLDER_H
+
+#include <QObject>
+
+class DCCell;
+class DCCellCode;
+
+class ScriptAttacher;
+
+class DCEditScriptFolder : public QObject
+{
+    Q_OBJECT
+
+    ScriptAttacher   *d_attacher;
+
+public:
+    explicit DCEditScriptFolder(QObject *parent = 0);
+    virtual ~DCEditScriptFolder();
+
+    void    attach(DCCell *cell);
+    void    attach(DCCellCode *cellCode);
+    void    deattach();
+
+    QString getCurrentScript();
+    bool    getIsModified() const;
+
+    void    setScript(const QString& newScript);
+    bool    saveScript();
+
+    void    callbackScriptChanged();
+    void    callbackModificationStatusChanged(bool modified);
+
+signals:
+    void    scriptChanged();
+    void    modificationStatusChanged(bool modified);
+    
+public slots:
+    
+};
+
+#endif // DCEDITSCRIPTFOLDER_H
index 032eddb..e9a414d 100644 (file)
@@ -99,7 +99,8 @@ HEADERS       = mainwindow.h \
     codeeditor/dccellscriptseditortabwidget.h \
     codeeditor/dccellscriptseditorpagewidget.h \
     dialog/dcmanagecellcodedialog.h \
-    engine/versioninfo.h
+    engine/versioninfo.h \
+    codeeditor/dceditscriptfolder.h
 
 SOURCES       = main.cpp \
                 mainwindow.cpp \
@@ -192,7 +193,8 @@ SOURCES       = main.cpp \
     codeeditor/dccellscriptseditorwindow.cpp \
     codeeditor/dccellscriptseditortabwidget.cpp \
     codeeditor/dccellscriptseditorpagewidget.cpp \
-    dialog/dcmanagecellcodedialog.cpp
+    dialog/dcmanagecellcodedialog.cpp \
+    codeeditor/dceditscriptfolder.cpp
 
 RESOURCES     = denncoCreator.qrc