File indexing completed on 2024-06-23 05:33:15

0001 /***************************************************************************
0002  *  Copyright (C) 2022 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "maincontroller.h"
0021 
0022 namespace rcse
0023 {
0024 MainController::MainController(QObject* parent)
0025     : QObject{parent}
0026     , m_logCtrl{new LogController(true)}
0027     , m_imageCtrl{new ImageController}
0028     , m_characterCtrl{new CharacterController}
0029     , m_editCtrl{new EditorController(m_imageCtrl.get())}
0030     , m_generatorCtrl{new QmlGeneratorController()}
0031 {
0032     connect(m_generatorCtrl.get(), &QmlGeneratorController::reportLog, m_logCtrl.get(), [this](const QString& msg,  LogController::LogLevel type){
0033         m_logCtrl->manageMessage(msg, QStringLiteral("QMLEngine"), type);
0034     });
0035     m_logCtrl->setCurrentModes(LogController::Gui);
0036 
0037     connect(m_characterCtrl.get(), &CharacterController::performCommand, this, &MainController::processCommand);
0038     connect(m_editCtrl.get(), &EditorController::performCommand, this, &MainController::processCommand);
0039 
0040     m_editCtrl->setCurrentTool(Canvas::MOVE);
0041 
0042     auto f= [this]() { setModified(true); };
0043     connect(m_editCtrl.get(), &EditorController::dataChanged, this, f);
0044     connect(m_characterCtrl.get(), &CharacterController::dataChanged, this, f);
0045     connect(m_generatorCtrl.get(), &QmlGeneratorController::dataChanged, this, f);
0046     connect(m_imageCtrl.get(), &ImageController::dataChanged, this, f);
0047 
0048     connect(m_editCtrl.get(), &EditorController::canvasBackgroundChanged, m_imageCtrl.get(),
0049             &ImageController::addBackgroundImage);
0050 
0051     connect(m_imageCtrl.get(), &ImageController::errorOccurs, this,
0052             [this](const QString& msg) { m_logCtrl->manageMessage(msg, QStringLiteral("ImageCtrl"), LogController::Error); });
0053 
0054     connect(m_generatorCtrl.get(), &QmlGeneratorController::errors, this,
0055             &MainController::displayQmlError); //[this](const QList<QQmlError>& errors) {
0056 
0057     connect(m_generatorCtrl.get(), &QmlGeneratorController::sectionChanged, m_characterCtrl.get(),
0058             &CharacterController::setRootSection);
0059 
0060     connect(m_generatorCtrl->fieldModel(), &FieldModel::fieldAdded, m_editCtrl.get(), &EditorController::addFieldItem);
0061 
0062     m_characterCtrl->setRootSection(m_generatorCtrl->fieldModel()->getRootSection());
0063 
0064     connect(m_editCtrl.get(), &EditorController::pageCountChanged, this,
0065             [this]() { m_generatorCtrl->setLastPageId(static_cast<unsigned int>(m_editCtrl->pageCount() - 1)); });
0066 }
0067 
0068 CharacterController* MainController::characterCtrl() const
0069 {
0070     return m_characterCtrl.get();
0071 }
0072 EditorController* MainController::editCtrl() const
0073 {
0074     return m_editCtrl.get();
0075 }
0076 ImageController* MainController::imageCtrl() const
0077 {
0078     return m_imageCtrl.get();
0079 }
0080 QmlGeneratorController* MainController::generatorCtrl() const
0081 {
0082     return m_generatorCtrl.get();
0083 }
0084 LogController* MainController::logCtrl() const
0085 {
0086     return m_logCtrl.get();
0087 }
0088 
0089 const QUndoStack& MainController::undoStack() const
0090 {
0091     return m_undoStack;
0092 }
0093 
0094 void MainController::processCommand(QUndoCommand* cmd)
0095 {
0096     m_undoStack.push(cmd);
0097 }
0098 
0099 bool MainController::modified() const
0100 {
0101     return m_modified;
0102 }
0103 
0104 void MainController::setModified(bool newModified)
0105 {
0106     if(m_modified == newModified)
0107         return;
0108     m_modified= newModified;
0109     emit modifiedChanged(m_modified);
0110 }
0111 
0112 void MainController::cleanUpData(bool addPage)
0113 {
0114     m_generatorCtrl->clearData();
0115     CSItem::resetCount();
0116     m_editCtrl->clearData();
0117     m_imageCtrl->clearData();
0118     m_characterCtrl->clear();
0119     m_undoStack.clear();
0120     setCurrentFile(QString());
0121     setModified(false);
0122 
0123     if(addPage)
0124         m_editCtrl->addPage();
0125 }
0126 
0127 void MainController::displayQmlError(const QList<QQmlError>& errors)
0128 {
0129     if(!m_logCtrl)
0130         return;
0131 
0132     for(const auto& er : errors)
0133     {
0134         LogController::LogLevel lvl= LogController::Info;
0135         switch(er.messageType())
0136         {
0137         case QtDebugMsg:
0138             lvl= LogController::Debug;
0139             break;
0140         case QtWarningMsg:
0141             lvl= LogController::Warning;
0142             break;
0143         case QtCriticalMsg:
0144             lvl= LogController::Error;
0145             break;
0146         case QtFatalMsg:
0147             lvl= LogController::Error;
0148             break;
0149         case QtInfoMsg:
0150             lvl= LogController::Info;
0151             break;
0152         }
0153         if(m_logCtrl)
0154             m_logCtrl->manageMessage(er.toString(), QStringLiteral("QMLEngine"), lvl);
0155     }
0156 }
0157 
0158 const QString& MainController::currentFile() const
0159 {
0160     return m_currentFile;
0161 }
0162 
0163 void MainController::setCurrentFile(const QString& newCurrentFile)
0164 {
0165     if(m_currentFile == newCurrentFile)
0166         return;
0167     m_currentFile= newCurrentFile;
0168     emit currentFileChanged();
0169 }
0170 }