File indexing completed on 2024-05-12 16:23:27
0001 /*************************************************************************** 0002 * Copyright (C) 2013 by Linuxstopmotion contributors; * 0003 * see the AUTHORS file for details. * 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 "commandadd.h" 0021 0022 #include <assert.h> 0023 #include <stdlib.h> 0024 #include <new> 0025 #include <memory> 0026 0027 #include "commandremove.h" 0028 #include "src/domain/animation/animationimpl.h" 0029 #include "src/domain/animation/frame.h" 0030 #include "src/domain/animation/workspacefile.h" 0031 #include "src/domain/undo/command.h" 0032 0033 class ErrorHandler; 0034 class FileNameVisitor; 0035 0036 CommandAdd::CommandAdd(AnimationImpl& model, int toScene, int toFrame, int count) 0037 : sv(model), scene(toScene), frame(toFrame) { 0038 frames.reserve(count); 0039 } 0040 0041 CommandAdd::~CommandAdd() { 0042 for (std::vector<Frame*>::iterator i = frames.begin(); 0043 i != frames.end(); ++i) { 0044 delete *i; 0045 } 0046 } 0047 0048 void CommandAdd::addFrame(Frame* frame) { 0049 frames.push_back(frame); 0050 } 0051 0052 Command* CommandAdd::execute() { 0053 std::unique_ptr<CommandRemove> inverse( 0054 new CommandRemove(sv, scene, frame, frames.size())); 0055 sv.addFrames(scene, frame, frames); 0056 // ownership has been passed, so we must forget the frames 0057 frames.clear(); 0058 delete this; 0059 return inverse.release(); 0060 } 0061 0062 void CommandAdd::accept(FileNameVisitor& v) const { 0063 for (std::vector<Frame*>::const_iterator i = frames.begin(); 0064 i != frames.end(); ++i) { 0065 (*i)->accept(v); 0066 } 0067 } 0068 0069 CommandAddFactory::Parameters::Parameters(int scene, int frame, int count) 0070 : sc(scene), fr(frame), frameCount(count), twfs(0), twfCount(0), 0071 parameterCount(0) { 0072 twfs = (TemporaryWorkspaceFile*) malloc( 0073 frameCount * sizeof(TemporaryWorkspaceFile)); 0074 } 0075 0076 CommandAddFactory::Parameters::~Parameters() { 0077 for (int i = 0; i != twfCount; ++i) { 0078 twfs[i].~TemporaryWorkspaceFile(); 0079 } 0080 free(twfs); 0081 } 0082 0083 const char* CommandAddFactory::Parameters::addFrame(const char* filename) { 0084 assert (twfCount != frameCount); 0085 TemporaryWorkspaceFile* p = twfs + twfCount; 0086 new (p) TemporaryWorkspaceFile(filename, WorkspaceFileType::image()); 0087 ++twfCount; 0088 return p->path(); 0089 } 0090 0091 int32_t CommandAddFactory::Parameters::getInteger(int32_t, int32_t) { 0092 assert(parameterCount < 2); 0093 ++parameterCount; 0094 return parameterCount == 1? sc : fr; 0095 } 0096 0097 int32_t CommandAddFactory::Parameters::getHowMany() { 0098 assert(parameterCount == 2); 0099 ++parameterCount; 0100 return frameCount; 0101 } 0102 0103 void CommandAddFactory::Parameters::getString(std::string& out, 0104 const char*) { 0105 assert(3 <= parameterCount); 0106 int index = parameterCount - 3; 0107 ++parameterCount; 0108 out.assign(twfs[index].basename()); 0109 } 0110 0111 void CommandAddFactory::Parameters::retainFiles() { 0112 for (int i = 0; i != twfCount; ++i) { 0113 twfs[i].retainFile(); 0114 } 0115 } 0116 0117 CommandAddFactory::CommandAddFactory(AnimationImpl& model) : sv(model) { 0118 } 0119 0120 CommandAddFactory::~CommandAddFactory() { 0121 } 0122 0123 Command* CommandAddFactory::create(::Parameters& ps, ErrorHandler&) { 0124 int sceneCount = sv.sceneCount(); 0125 if (sceneCount == 0) 0126 return 0; 0127 int scene = ps.getInteger(0, sceneCount - 1); 0128 int frame = ps.getInteger(0, sv.frameCount(scene)); 0129 int count = ps.getHowMany(); 0130 CommandAdd* add = new CommandAdd(sv, scene, frame, count); 0131 std::string frameName; 0132 for (int i = 0; i != count; ++i) { 0133 ps.getString(frameName, "?*.jpg"); 0134 WorkspaceFile wf(frameName.c_str()); 0135 add->addFrame(new Frame(wf)); 0136 } 0137 return add; 0138 }