File indexing completed on 2024-05-19 05:40:36

0001 /***************************************************************************
0002  *  Copyright (C) 2019 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 "controller/view_controller/mindmapcontroller.h"
0021 
0022 #include <QDebug>
0023 #include <QFile>
0024 #include <QJsonArray>
0025 #include <QJsonDocument>
0026 #include <QJsonObject>
0027 #include <QQmlEngine>
0028 #include <QThread>
0029 #include <QUrl>
0030 #include <random>
0031 
0032 #include "mindmap/controller/spacingcontroller.h"
0033 #include "mindmap/model/minditemmodel.h"
0034 #include "updater/media/mindmapupdater.h"
0035 
0036 QPointer<RemotePlayerModel> MindMapController::m_remotePlayerModel;
0037 QPointer<MindMapUpdater> MindMapController::m_updater;
0038 
0039 MindMapController::MindMapController(const QString& id, QObject* parent) : MindMapControllerBase(true, id, parent)
0040 {
0041     m_selectionController->setUndoStack(&m_stack);
0042 
0043     MediaControllerBase::connect(m_itemModel.get(), &mindmap::MindItemModel::rowsInserted,
0044                                  static_cast<MediaControllerBase*>(this),
0045                                  [this]
0046                                  {
0047                                      // qDebug() << "Row inserted";
0048                                      setModified();
0049                                  });
0050     MediaControllerBase::connect(m_itemModel.get(), &mindmap::MindItemModel::dataChanged,
0051                                  static_cast<MediaControllerBase*>(this),
0052                                  [this](const QModelIndex&, const QModelIndex&, QVector<int> roles)
0053                                  {
0054                                      if(!roles.contains(mindmap::MindItemModel::LinkPositionFromSpacing))
0055                                      {
0056                                          // qDebug() << "dataChanged" << roles;
0057                                          setModified();
0058                                      }
0059                                  });
0060     MediaControllerBase::connect(m_itemModel.get(), &mindmap::MindItemModel::rowsRemoved,
0061                                  static_cast<MediaControllerBase*>(this),
0062                                  [this]
0063                                  {
0064                                      // qDebug() << "Row rowsRemoved";
0065                                      setModified();
0066                                  });
0067 
0068     MediaControllerBase::connect(this, &MindMapController::defaultStyleIndexChanged,
0069                                  static_cast<MediaControllerBase*>(this),
0070                                  [this]
0071                                  {
0072                                      // qDebug() << "Default style changed";
0073                                      setModified();
0074                                  });
0075 
0076     MediaControllerBase::connect(this, &MindMapController::nameChanged, static_cast<MediaControllerBase*>(this),
0077                                  [this]
0078                                  {
0079                                      // qDebug() << "name Changed";
0080                                      setModified();
0081                                  });
0082 
0083     /*MediaControllerBase::connect(this, &MindMapController::titleChanged, static_cast<MediaControllerBase*>(this),
0084                                  [this] {
0085                                      //qDebug() << "title changed";
0086                                      setModified();
0087                                  });*/
0088     MediaControllerBase::connect(this, &MindMapController::urlChanged, static_cast<MediaControllerBase*>(this),
0089                                  [this]
0090                                  {
0091                                      qDebug() << "url changed";
0092                                      setModified();
0093                                  });
0094 
0095     clearData();
0096 }
0097 
0098 MindMapController::~MindMapController()
0099 {
0100     if(m_spacing->isRunning())
0101     {
0102         m_spacingController->setRunning(false);
0103         m_spacing->quit();
0104         m_spacing->wait();
0105         delete m_spacing.release();
0106     }
0107 
0108     auto model= m_itemModel.release();
0109     auto spacingCtrl= m_spacingController.release();
0110 
0111     delete spacingCtrl;
0112     delete model;
0113 }
0114 
0115 RemotePlayerModel* MindMapController::remotePlayerModel() const
0116 {
0117     return m_remotePlayerModel;
0118 }
0119 
0120 PlayerModel* MindMapController::playerModel() const
0121 {
0122     return m_remotePlayerModel->sourceModel();
0123 }
0124 
0125 bool MindMapController::readWrite() const
0126 {
0127     bool hasDedicatedPermission= false;
0128     if(m_permissions.contains(m_localId))
0129         hasDedicatedPermission= (m_permissions[m_localId] == Core::SharingPermission::ReadWrite);
0130 
0131     return (localIsOwner() || m_sharingToAll == Core::SharingPermission::ReadWrite || hasDedicatedPermission);
0132 }
0133 
0134 void MindMapController::setSharingToAll(int b)
0135 {
0136     auto realPerm= static_cast<Core::SharingPermission>(b);
0137     if(m_sharingToAll == realPerm)
0138         return;
0139     auto old= m_sharingToAll;
0140     m_sharingToAll= realPerm;
0141     emit sharingToAllChanged(m_sharingToAll, old);
0142 }
0143 
0144 Core::SharingPermission MindMapController::sharingToAll() const
0145 {
0146     return m_sharingToAll;
0147 }
0148 
0149 void MindMapController::setPermissionForUser(const QString& userId, int perm)
0150 {
0151     auto realPerm= static_cast<Core::SharingPermission>(perm);
0152     switch(realPerm)
0153     {
0154     case Core::SharingPermission::None:
0155         m_permissions.remove(userId);
0156         emit closeMindMapTo(userId);
0157         break;
0158     case Core::SharingPermission::ReadOnly:
0159     {
0160         if(m_permissions.contains(userId))
0161         {
0162             auto p= m_permissions.find(userId);
0163             Q_ASSERT(p.value() == Core::SharingPermission::ReadWrite);
0164             emit permissionChangedForUser(userId, realPerm);
0165         }
0166         else
0167         {
0168             m_permissions.insert(userId, realPerm);
0169             emit openMindMapTo(userId);
0170         }
0171     }
0172     break;
0173     case Core::SharingPermission::ReadWrite:
0174         m_permissions.insert(userId, realPerm);
0175         emit permissionChangedForUser(userId, realPerm);
0176         break;
0177     }
0178 }
0179 
0180 void MindMapController::setRemotePlayerModel(RemotePlayerModel* model)
0181 {
0182     m_remotePlayerModel= model;
0183 }
0184 
0185 void MindMapController::setMindMapUpdater(MindMapUpdater* updater)
0186 {
0187     m_updater= updater;
0188 }
0189 
0190 void MindMapController::generateTree()
0191 {
0192     return;
0193     /* QString res;
0194      {
0195          QTextStream output(&res);
0196 
0197          output << QString("digraph G {") << "\n";
0198 
0199          auto const& nodes= m_itemModel->items(mindmap::MindItem::NodeType);
0200          output << QString("    subgraph cluster_0 {") << "\n";
0201          for(auto const& i : nodes)
0202          {
0203              auto node= dynamic_cast<mindmap::MindNode*>(i);
0204              if(!node)
0205                  continue;
0206              output << "0_" << node->toString(true) << ";\n";
0207              auto links= node->subLinks();
0208              output << "# sublinks of :" << node->id();
0209              for(auto link : links)
0210              {
0211                  output << "0_" << link->toString(true) << ";\n";
0212                  output << "0_" << link->toString(false) << "->"
0213                         << "0_" << node->toString(false) << ";\n";
0214              }
0215          }
0216          output << "    }\n";
0217          output << "# from link model point of view";
0218          auto const& links= m_itemModel->items(mindmap::MindItem::LinkType);
0219          output << QString("    subgraph cluster_1 {") << "\n";
0220          for(auto const& i : links)
0221          {
0222              auto link= dynamic_cast<mindmap::LinkController*>(i);
0223              auto end= link->end();
0224              auto start= link->start();
0225 
0226              if(!end)
0227                  continue;
0228 
0229              output << end->toString(true) << ";\n";
0230 
0231              if(!start)
0232                  continue;
0233 
0234              output << start->toString(true) << ";\n";
0235              output << start->toString(false) << "->" << end->toString(false)
0236                     << QString("[Label=\"Link-id:%1 text:%2\"]").arg(link->id(), link->text());
0237          }
0238          output << "    }\n";
0239          output.flush();
0240      }
0241 
0242      // qDebug() << res;*/
0243 }