File indexing completed on 2024-05-05 05:40:34

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Renaud Guezennec                                *
0003  *   http://www.rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   This program 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 "addfieldcommand.h"
0021 #include "field.h"
0022 #include "tablecanvasfield.h"
0023 
0024 AddFieldCommand::AddFieldCommand(
0025     Canvas::Tool tool, Canvas* canvas, FieldModel* model, int currentPage, QPointF pos, QUndoCommand* parent)
0026     : QUndoCommand(parent), m_canvas(canvas), m_model(model), m_currentPage(currentPage)
0027 {
0028     m_field= new Field(pos);
0029     m_field->setPage(m_currentPage);
0030     if(Canvas::ADDTABLE == tool)
0031     {
0032         m_field->setCanvasField(new TableCanvasField(m_field));
0033     }
0034 
0035     // m_canvas->addItem(m_field->getCanvasField());
0036 
0037     m_field->setValueFrom(CharacterSheetItem::X, pos.x());
0038     m_field->setValueFrom(CharacterSheetItem::Y, pos.y());
0039 
0040     QString type;
0041 
0042     // m_currentItem->setFocus();
0043     switch(tool) // TEXTINPUT,TEXTFIELD,TEXTAREA,SELECT,CHECKBOX,IMAGE,BUTTON
0044     {
0045     case Canvas::ADDCHECKBOX:
0046         m_field->setCurrentType(Field::CHECKBOX);
0047         type= QObject::tr("checkbox");
0048         break;
0049     case Canvas::ADDINPUT:
0050         m_field->setCurrentType(Field::TEXTINPUT);
0051         type= QObject::tr("TextInput");
0052         break;
0053     case Canvas::ADDTEXTAREA:
0054         m_field->setCurrentType(Field::TEXTAREA);
0055         type= QObject::tr("TextArea");
0056         break;
0057     case Canvas::ADDTEXTFIELD:
0058         m_field->setCurrentType(Field::TEXTFIELD);
0059         type= QObject::tr("TextField");
0060         break;
0061     case Canvas::ADDTABLE:
0062     {
0063         m_field->setCurrentType(Field::TABLE);
0064         type= QObject::tr("Table");
0065     }
0066     break;
0067     case Canvas::ADDIMAGE:
0068         m_field->setCurrentType(Field::IMAGE);
0069         type= QObject::tr("Image");
0070         break;
0071     case Canvas::ADDFUNCBUTTON:
0072         m_field->setCurrentType(Field::FUNCBUTTON);
0073         type= QObject::tr("function");
0074         break;
0075     case Canvas::BUTTON:
0076         m_field->setCurrentType(Field::BUTTON);
0077         m_field->setBgColor(Qt::red);
0078         type= QObject::tr("Dice Button");
0079         break;
0080     case Canvas::MOVE:
0081     case Canvas::DELETETOOL:
0082     case Canvas::NONE:
0083     default:
0084         type= QObject::tr("Unknown");
0085         break;
0086     }
0087 
0088     setText(QObject::tr("Add %1 Field").arg(type));
0089 }
0090 
0091 void AddFieldCommand::undo()
0092 {
0093     m_canvas->removeItem(m_field->getCanvasField());
0094     m_canvas->update();
0095     m_model->removeField(m_field);
0096 }
0097 
0098 void AddFieldCommand::redo()
0099 {
0100     m_canvas->addItem(m_field->getCanvasField());
0101     if(nullptr != m_model)
0102     {
0103         m_model->appendField(m_field);
0104     }
0105 }
0106 
0107 Field* AddFieldCommand::getField() const
0108 {
0109     return m_field;
0110 }