File indexing completed on 2022-11-29 12:54:54

0001 /*
0002  *  SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 
0008 #include "coursecommands.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include "core/course.h"
0013 #include "core/lesson.h"
0014 
0015 SetCourseTitleCommand::SetCourseTitleCommand(Course* course, const QString& oldTitle, QUndoCommand* parent) :
0016     QUndoCommand(parent),
0017     m_course(course),
0018     m_oldTitle(oldTitle),
0019     m_newTitle(course->title())
0020 {
0021     setText(i18n("Set title"));
0022 }
0023 
0024 void SetCourseTitleCommand::undo()
0025 {
0026     m_course->setTitle(m_oldTitle);
0027 }
0028 
0029 void SetCourseTitleCommand::redo()
0030 {
0031     m_course->setTitle(m_newTitle);
0032 }
0033 
0034 bool SetCourseTitleCommand::mergeWith(const QUndoCommand* command)
0035 {
0036     const SetCourseTitleCommand* setCourseTitleCommand = static_cast<const SetCourseTitleCommand*>(command);
0037 
0038     if (m_course != setCourseTitleCommand->m_course)
0039         return false;
0040 
0041     m_newTitle = setCourseTitleCommand->m_newTitle;
0042     return true;
0043 }
0044 
0045 int SetCourseTitleCommand::id() const
0046 {
0047     return 0x9a2e787f;
0048 }
0049 
0050 SetCourseKeyboadLayoutNameCommand::SetCourseKeyboadLayoutNameCommand(Course* course, const QString& oldKeyboardLayoutName, QUndoCommand* parent) :
0051     QUndoCommand(parent),
0052     m_course(course),
0053     m_oldKeyboardLayoutName(oldKeyboardLayoutName),
0054     m_newKeyboardLayoutName(course->keyboardLayoutName())
0055 {
0056     setText(i18n("Set keyboard layout"));
0057 }
0058 
0059 void SetCourseKeyboadLayoutNameCommand::undo()
0060 {
0061     m_course->setKeyboardLayoutName(m_oldKeyboardLayoutName);
0062 }
0063 
0064 void SetCourseKeyboadLayoutNameCommand::redo()
0065 {
0066     m_course->setKeyboardLayoutName(m_newKeyboardLayoutName);
0067 }
0068 
0069 bool SetCourseKeyboadLayoutNameCommand::mergeWith(const QUndoCommand* command)
0070 {
0071     const SetCourseKeyboadLayoutNameCommand* setCourseKeyboardLayoutNameCommand = static_cast<const SetCourseKeyboadLayoutNameCommand*>(command);
0072 
0073     if (m_course != setCourseKeyboardLayoutNameCommand->m_course)
0074         return false;
0075 
0076     m_newKeyboardLayoutName = setCourseKeyboardLayoutNameCommand->m_newKeyboardLayoutName;
0077     return true;
0078 }
0079 
0080 int SetCourseKeyboadLayoutNameCommand::id() const
0081 {
0082     return 0xc6c1d878;
0083 }
0084 
0085 SetCourseDescriptionCommand::SetCourseDescriptionCommand(Course* course, const QString& oldDescription, QUndoCommand* parent) :
0086     QUndoCommand(parent),
0087     m_course(course),
0088     m_oldDescription(oldDescription),
0089     m_newDescription(course->description())
0090 {
0091     setText(i18n("Edit description"));
0092 }
0093 
0094 void SetCourseDescriptionCommand::undo()
0095 {
0096     m_course->setDescription(m_oldDescription);
0097 }
0098 
0099 void SetCourseDescriptionCommand::redo()
0100 {
0101     m_course->setDescription(m_newDescription);
0102 }
0103 
0104 bool SetCourseDescriptionCommand::mergeWith(const QUndoCommand* command)
0105 {
0106     const SetCourseDescriptionCommand* setCourseDescriptionCommand = static_cast<const SetCourseDescriptionCommand*>(command);
0107 
0108     if (m_course != setCourseDescriptionCommand->m_course)
0109         return false;
0110 
0111     m_newDescription = setCourseDescriptionCommand->m_newDescription;
0112     return true;
0113 }
0114 
0115 int SetCourseDescriptionCommand::id() const
0116 {
0117     return 0x264f63fb;
0118 }
0119 
0120 AddLessonCommand::AddLessonCommand(Course* course, int lessonIndex,const QString& lessonId, QUndoCommand* parent) :
0121     QUndoCommand(parent),
0122     m_course(course),
0123     m_lessonIndex(lessonIndex),
0124     m_lessonId(lessonId)
0125 {
0126     setText(i18n("Add lesson"));
0127 }
0128 
0129 void AddLessonCommand::undo()
0130 {
0131     m_course->removeLesson(m_lessonIndex);
0132 }
0133 
0134 void AddLessonCommand::redo()
0135 {
0136     Lesson* lesson = new Lesson();
0137 
0138     lesson->setId(m_lessonId);
0139 
0140     if (m_lessonIndex == m_course->lessonCount())
0141     {
0142         m_course->addLesson(lesson);
0143     }
0144     else
0145     {
0146         m_course->insertLesson(m_lessonIndex, lesson);
0147     }
0148 }
0149 
0150 bool AddLessonCommand::mergeWith(const QUndoCommand* command)
0151 {
0152     Q_UNUSED(command);
0153     return false;
0154 }
0155 
0156 int AddLessonCommand::id() const
0157 {
0158     return 0x34f95ddf;
0159 }
0160 
0161 RemoveLessonCommand::RemoveLessonCommand(Course* course, int lessonIndex, QUndoCommand* parent) :
0162     QUndoCommand(parent),
0163     m_course(course),
0164     m_lessonIndex(lessonIndex),
0165     m_backupLesson(nullptr)
0166 {
0167     setText(i18n("Remove lesson"));
0168 }
0169 
0170 RemoveLessonCommand::~RemoveLessonCommand()
0171 {
0172     if (m_backupLesson)
0173     {
0174         delete m_backupLesson;
0175     }
0176 }
0177 
0178 void RemoveLessonCommand::undo()
0179 {
0180     Q_ASSERT(m_backupLesson);
0181 
0182     if (m_lessonIndex == m_course->lessonCount())
0183     {
0184         m_course->addLesson(m_backupLesson);
0185     }
0186     else
0187     {
0188         m_course->insertLesson(m_lessonIndex, m_backupLesson);
0189     }
0190 
0191     m_backupLesson = nullptr;
0192 }
0193 
0194 void RemoveLessonCommand::redo()
0195 {
0196     m_backupLesson = new Lesson();
0197     m_backupLesson->copyFrom(m_course->lesson(m_lessonIndex));
0198     m_course->removeLesson(m_lessonIndex);
0199 }
0200 
0201 bool RemoveLessonCommand::mergeWith(const QUndoCommand* command)
0202 {
0203     Q_UNUSED(command)
0204     return false;
0205 }
0206 
0207 int RemoveLessonCommand::id() const
0208 {
0209     return 0x36f2ef42;
0210 }
0211 
0212 MoveLessonCommand::MoveLessonCommand(Course* course, int oldLessonIndex, int newLessonIndex, QUndoCommand* parent) :
0213     QUndoCommand(parent),
0214     m_course(course),
0215     m_oldLessonIndex(oldLessonIndex),
0216     m_newLessonIndex(newLessonIndex)
0217 {
0218     setText(i18n("Move lesson"));
0219 }
0220 
0221 void MoveLessonCommand::undo()
0222 {
0223     Lesson* lesson = new Lesson();
0224 
0225     lesson->copyFrom(m_course->lesson(m_newLessonIndex));
0226     m_course->removeLesson(m_newLessonIndex);
0227 
0228     if (m_oldLessonIndex == m_course->lessonCount())
0229     {
0230         m_course->addLesson(lesson);
0231     }
0232     else
0233     {
0234         m_course->insertLesson(m_oldLessonIndex, lesson);
0235     }
0236 
0237 }
0238 
0239 void MoveLessonCommand::redo()
0240 {
0241     Lesson* lesson = new Lesson();
0242 
0243     lesson->copyFrom(m_course->lesson(m_oldLessonIndex));
0244     m_course->removeLesson(m_oldLessonIndex);
0245 
0246     if (m_newLessonIndex == m_course->lessonCount())
0247     {
0248         m_course->addLesson(lesson);
0249     }
0250     else
0251     {
0252         m_course->insertLesson(m_newLessonIndex, lesson);
0253     }
0254 }
0255 
0256 bool MoveLessonCommand::mergeWith(const QUndoCommand* command)
0257 {
0258     const MoveLessonCommand* moveLessonCommand = static_cast<const MoveLessonCommand*>(command);
0259 
0260     if (m_course != moveLessonCommand->m_course)
0261         return false;
0262 
0263     if (m_newLessonIndex != moveLessonCommand->m_oldLessonIndex)
0264         return false;
0265 
0266     m_newLessonIndex = moveLessonCommand->m_newLessonIndex;
0267     return true;
0268 }
0269 
0270 int MoveLessonCommand::id() const
0271 {
0272     return 0xf3993bad;
0273 }
0274 
0275 SetLessonTitleCommand::SetLessonTitleCommand(Course* course, int lessonIndex, const QString& oldTitle, QUndoCommand* parent) :
0276     QUndoCommand(parent),
0277     m_course(course),
0278     m_lessonIndex(lessonIndex),
0279     m_oldTitle(oldTitle),
0280     m_newTitle(course->lesson(lessonIndex)->title())
0281 {
0282     setText(i18n("Set lesson title"));
0283 }
0284 
0285 void SetLessonTitleCommand::undo()
0286 {
0287     m_course->lesson(m_lessonIndex)->setTitle(m_oldTitle);
0288 }
0289 
0290 void SetLessonTitleCommand::redo()
0291 {
0292     m_course->lesson(m_lessonIndex)->setTitle(m_newTitle);
0293 }
0294 
0295 bool SetLessonTitleCommand::mergeWith(const QUndoCommand* command)
0296 {
0297     const SetLessonTitleCommand* setLessonTitleCommand = static_cast<const SetLessonTitleCommand*>(command);
0298 
0299     if (m_course != setLessonTitleCommand->m_course)
0300         return false;
0301 
0302     if (m_lessonIndex != setLessonTitleCommand->m_lessonIndex)
0303         return false;
0304 
0305     m_newTitle = setLessonTitleCommand->m_newTitle;
0306     return true;
0307 }
0308 
0309 int SetLessonTitleCommand::id() const
0310 {
0311     return 0x4499bef5;
0312 }
0313 
0314 SetLessonNewCharactersCommand::SetLessonNewCharactersCommand(Course* course, int lessonIndex, const QString& oldNewCharacters, QUndoCommand* parent) :
0315     QUndoCommand(parent),
0316     m_course(course),
0317     m_lessonIndex(lessonIndex),
0318     m_oldNewCharacters(oldNewCharacters),
0319     m_newNewCharacters(course->lesson(lessonIndex)->newCharacters())
0320 {
0321     setText(i18n("Set new characters for lesson"));
0322 }
0323 
0324 void SetLessonNewCharactersCommand::undo()
0325 {
0326     m_course->lesson(m_lessonIndex)->setNewCharacters(m_oldNewCharacters);
0327 }
0328 
0329 void SetLessonNewCharactersCommand::redo()
0330 {
0331     m_course->lesson(m_lessonIndex)->setNewCharacters(m_newNewCharacters);
0332 }
0333 
0334 bool SetLessonNewCharactersCommand::mergeWith(const QUndoCommand* command)
0335 {
0336     const SetLessonNewCharactersCommand* setLessonNewCharactersCommand = static_cast<const SetLessonNewCharactersCommand*>(command);
0337 
0338     if (m_course != setLessonNewCharactersCommand->m_course)
0339         return false;
0340 
0341     if (m_lessonIndex != setLessonNewCharactersCommand->m_lessonIndex)
0342         return false;
0343 
0344     m_newNewCharacters = setLessonNewCharactersCommand->m_newNewCharacters;
0345     return true;
0346 }
0347 
0348 int SetLessonNewCharactersCommand::id() const
0349 {
0350     return 0x325e1dc2;
0351 }
0352 
0353 SetLessonTextCommand::SetLessonTextCommand(Course* course, int lessonIndex, const QString& oldText, QUndoCommand* parent) :
0354     QUndoCommand(parent),
0355     m_course(course),
0356     m_lessonIndex(lessonIndex),
0357     m_oldText(oldText),
0358     m_newText(course->lesson(lessonIndex)->text())
0359 {
0360     setText(i18n("Set lesson text"));
0361 }
0362 
0363 void SetLessonTextCommand::undo()
0364 {
0365     m_course->lesson(m_lessonIndex)->setText(m_oldText);
0366 }
0367 
0368 void SetLessonTextCommand::redo()
0369 {
0370     m_course->lesson(m_lessonIndex)->setText(m_newText);
0371 }
0372 
0373 bool SetLessonTextCommand::mergeWith(const QUndoCommand* command)
0374 {
0375     const SetLessonTextCommand* setLessonTextCommand = static_cast<const SetLessonTextCommand*>(command);
0376 
0377     if (m_course != setLessonTextCommand->m_course)
0378         return false;
0379 
0380     if (m_lessonIndex != setLessonTextCommand->m_lessonIndex)
0381         return false;
0382 
0383     m_newText = setLessonTextCommand->m_newText;
0384     return true;
0385 }
0386 
0387 int SetLessonTextCommand::id() const
0388 {
0389     return 0xdea53874;
0390 }