File indexing completed on 2024-05-12 05:09:24

0001 /***************************************************************************
0002     Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "fieldcommand.h"
0026 #include "../collection.h"
0027 #include "../controller.h"
0028 #include "../tellico_debug.h"
0029 
0030 #include <KLocalizedString>
0031 
0032 using Tellico::Command::FieldCommand;
0033 
0034 FieldCommand::FieldCommand(Mode mode_, Tellico::Data::CollPtr coll_,
0035                            Tellico::Data::FieldPtr activeField_, Tellico::Data::FieldPtr oldField_/*=0*/)
0036     : QUndoCommand()
0037     , m_mode(mode_)
0038     , m_coll(coll_)
0039     , m_activeField(activeField_)
0040     , m_oldField(oldField_)
0041 {
0042   init();
0043 }
0044 
0045 FieldCommand::FieldCommand(QUndoCommand* parent, Mode mode_, Tellico::Data::CollPtr coll_,
0046                            Tellico::Data::FieldPtr activeField_, Tellico::Data::FieldPtr oldField_/*=0*/)
0047     : QUndoCommand(parent)
0048     , m_mode(mode_)
0049     , m_coll(coll_)
0050     , m_activeField(activeField_)
0051     , m_oldField(oldField_)
0052 {
0053   init();
0054 }
0055 
0056 void FieldCommand::init() {
0057   switch(m_mode) {
0058     case FieldAdd:
0059       setText(i18n("Add %1 Field", m_activeField->title()));
0060       break;
0061     case FieldModify:
0062       setText(i18n("Modify %1 Field", m_activeField->title()));
0063       break;
0064     case FieldRemove:
0065       setText(i18n("Delete %1 Field", m_activeField->title()));
0066       break;
0067   }
0068 
0069   if(!m_coll) {
0070     myDebug() << "null collection pointer";
0071   } else if(!m_activeField) {
0072     myDebug() << "null active field pointer";
0073   }
0074 #ifndef NDEBUG
0075 // just some sanity checking
0076   if(m_mode == FieldAdd && m_oldField) {
0077     myDebug() << "adding field, but pointers are wrong";
0078   } else if(m_mode == FieldModify && !m_oldField) {
0079     myDebug() << "modifying field, but pointers are wrong";
0080   } else if(m_mode == FieldRemove && m_oldField) {
0081     myDebug() << "removing field, but pointers are wrong";
0082   }
0083 #endif
0084 }
0085 
0086 void FieldCommand::redo() {
0087   if(!m_coll || !m_activeField) {
0088     return;
0089   }
0090 
0091   switch(m_mode) {
0092     case FieldAdd:
0093       // if there's currently a field in the collection with the same name, it will get overwritten
0094       // so save a pointer to it here, the collection should not delete it
0095       m_oldField = m_coll->fieldByName(m_activeField->name());
0096       m_coll->addField(m_activeField);
0097       Controller::self()->addedField(m_coll, m_activeField);
0098       break;
0099 
0100     case FieldModify:
0101       m_coll->modifyField(m_activeField);
0102       Controller::self()->modifiedField(m_coll, m_oldField, m_activeField);
0103       break;
0104 
0105     case FieldRemove:
0106       m_coll->removeField(m_activeField);
0107       Controller::self()->removedField(m_coll, m_activeField);
0108       break;
0109   }
0110 }
0111 
0112 void FieldCommand::undo() {
0113   if(!m_coll || !m_activeField) {
0114     return;
0115   }
0116 
0117   switch(m_mode) {
0118     case FieldAdd:
0119       m_coll->removeField(m_activeField);
0120       Controller::self()->removedField(m_coll, m_activeField);
0121       if(m_oldField) {
0122         m_coll->addField(m_oldField);
0123         Controller::self()->addedField(m_coll, m_oldField);
0124       }
0125       break;
0126 
0127     case FieldModify:
0128       m_coll->modifyField(m_oldField);
0129       Controller::self()->modifiedField(m_coll, m_activeField, m_oldField);
0130       break;
0131 
0132     case FieldRemove:
0133       m_coll->addField(m_activeField);
0134       Controller::self()->addedField(m_coll, m_activeField);
0135       break;
0136   }
0137 }