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

0001 #include "charactercontroller.h"
0002 
0003 #include <QAction>
0004 #include <QHeaderView>
0005 #include <QMenu>
0006 #include <QTreeView>
0007 
0008 // undo
0009 #include "charactersheet/controllers/section.h"
0010 #include "charactersheet/csitem.h"
0011 #include "undo/addcharactercommand.h"
0012 #include "undo/deletecharactercommand.h"
0013 #include "undo/setpropertyonallcharacters.h"
0014 
0015 CharacterController::CharacterController(QObject* parent)
0016     : QObject(parent)
0017     , m_characterModel(new CharacterSheetModel)
0018     , m_characters(new CharacterList(m_characterModel.get()))
0019 {
0020 
0021     // m_characters->setSourceModel(m_characterModel.get());
0022 
0023     connect(m_characterModel.get(), &CharacterSheetModel::characterSheetHasBeenAdded, this,
0024             [this](CharacterSheet* sheet) {
0025                 connect(sheet, &CharacterSheet::updateField, m_characterModel.get(),
0026                         &CharacterSheetModel::fieldHasBeenChanged);
0027                 connect(sheet, &CharacterSheet::addLineToTableField, m_characterModel.get(),
0028                         &CharacterSheetModel::addSubChild);
0029                 connect(sheet, &CharacterSheet::updateField, this, &CharacterController::dataChanged);
0030                 connect(sheet, &CharacterSheet::addLineToTableField, this, &CharacterController::dataChanged);
0031                 emit dataChanged();
0032             });
0033 }
0034 
0035 CharacterController::~CharacterController()= default;
0036 
0037 void CharacterController::sendAddCharacterCommand()
0038 {
0039     emit performCommand(new AddCharacterCommand(this));
0040 }
0041 
0042 CharacterList* CharacterController::characters() const
0043 {
0044     return m_characters.get();
0045 }
0046 
0047 void CharacterController::sendRemoveCharacterCommand(const QModelIndex& index)
0048 {
0049     if(index.column() > 0)
0050         emit performCommand(new DeleteCharacterCommand(index.column() - 1, this));
0051 }
0052 
0053 CharacterSheet* CharacterController::characterSheetFromIndex(int index) const
0054 {
0055     return m_characterModel->getCharacterSheet(index);
0056 }
0057 
0058 CharacterSheet* CharacterController::characterSheetFromUuid(const QString& uuid) const
0059 {
0060     return m_characterModel->getCharacterSheetById(uuid);
0061 }
0062 
0063 void CharacterController::addCharacter()
0064 {
0065     insertCharacter(characterCount(), new CharacterSheet);
0066     emit dataChanged();
0067 }
0068 void CharacterController::removeCharacter(int index)
0069 {
0070     m_characterModel->removeCharacterSheet(index);
0071     emit dataChanged();
0072 }
0073 
0074 void CharacterController::insertCharacter(int pos, CharacterSheet* sheet)
0075 {
0076     auto root= m_characterModel->getRootSection();
0077     if(!root || !sheet)
0078         return;
0079     root->buildDataInto(sheet);
0080     m_characterModel->addCharacterSheet(sheet, pos);
0081     emit dataChanged();
0082 }
0083 
0084 void CharacterController::applyOnSelection(const QModelIndex& index, const QModelIndexList& list)
0085 {
0086     QVariant var= index.data(Qt::DisplayRole);
0087     QVariant editvar= index.data(Qt::EditRole);
0088     if(editvar != var)
0089     {
0090         var= editvar;
0091     }
0092     int col= index.column();
0093     for(QModelIndex modelIndex : list)
0094     {
0095         if(modelIndex.column() == col)
0096         {
0097             m_characterModel->setData(modelIndex, var, Qt::EditRole);
0098         }
0099     }
0100     if(!list.isEmpty())
0101         emit dataChanged();
0102 }
0103 void CharacterController::applyOnAllCharacter(const QModelIndex& index)
0104 {
0105 
0106     QString value= index.data().toString();
0107     QString formula= index.data(CharacterSheetModel::FormulaRole).toString();
0108     auto characterItem= m_characterModel->indexToSection(index);
0109     if((!value.isEmpty()) && (nullptr != characterItem))
0110     {
0111         QString key= characterItem->id();
0112         emit performCommand(new SetPropertyOnCharactersCommand(key, value, formula, m_characterModel.get()));
0113         emit dataChanged();
0114     }
0115 }
0116 
0117 void CharacterController::setRootSection(Section* section)
0118 {
0119     m_characterModel->setRootSection(section);
0120 }
0121 
0122 void CharacterController::save(QJsonObject& obj)
0123 {
0124     m_characterModel->writeModel(obj);
0125 }
0126 
0127 void CharacterController::load(const QJsonObject& obj, bool complete)
0128 {
0129     m_characterModel->readModel(obj, complete);
0130 }
0131 void CharacterController::clear()
0132 {
0133     m_characterModel->clearModel();
0134 }
0135 
0136 CharacterSheetModel* CharacterController::model() const
0137 {
0138     return m_characterModel.get();
0139 }
0140 int CharacterController::characterCount() const
0141 {
0142     return m_characterModel->getCharacterSheetCount();
0143 }
0144 void CharacterController::checkCharacter(Section* sec)
0145 {
0146     m_characterModel->checkCharacter(sec);
0147 }