2008-01-30 07:35:51 +01:00
|
|
|
// Copyright (c) 2008, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2008-03-18 04:44:00 +01:00
|
|
|
// AEGISUB/ATHENASUB
|
2008-01-30 07:35:51 +01:00
|
|
|
//
|
|
|
|
// Website: http://www.aegisub.net
|
|
|
|
// Contact: mailto:amz@aegisub.net
|
|
|
|
//
|
|
|
|
|
2008-11-16 02:22:20 +01:00
|
|
|
#include "athenasub.h"
|
2008-08-03 18:17:35 +02:00
|
|
|
#include "model.h"
|
2008-08-09 22:43:27 +02:00
|
|
|
#include "controller.h"
|
2008-03-18 04:44:00 +01:00
|
|
|
using namespace Athenasub;
|
2008-01-30 07:35:51 +01:00
|
|
|
|
|
|
|
|
2008-09-20 23:21:28 +02:00
|
|
|
///////////////
|
|
|
|
// Constructor
|
|
|
|
CModel::CModel()
|
|
|
|
: undoLimit(20), readOnly(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-30 07:35:51 +01:00
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
// Adds a listener to be notified whenever things change
|
2008-08-03 18:17:35 +02:00
|
|
|
void CModel::AddListener(View listener)
|
2008-01-30 07:35:51 +01:00
|
|
|
{
|
|
|
|
wxASSERT(listener);
|
|
|
|
listeners.push_back(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
// Notify all listeners
|
2008-08-03 18:17:35 +02:00
|
|
|
void CModel::DispatchNotifications(Notification notification) const
|
2008-01-30 07:35:51 +01:00
|
|
|
{
|
|
|
|
for (ViewList::const_iterator cur=listeners.begin();cur!=listeners.end();cur++) {
|
2008-08-03 18:17:35 +02:00
|
|
|
//(*cur)->Notify(notification);
|
2008-01-30 07:35:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////
|
|
|
|
// Processes an action list
|
2008-09-20 23:21:28 +02:00
|
|
|
// type == 0 : direct action
|
|
|
|
// type == 1 : undo
|
|
|
|
// type == 2 : redo
|
2008-09-19 05:11:23 +02:00
|
|
|
void CModel::ProcessActionList(CActionList &_actionList,int type)
|
2008-01-30 07:35:51 +01:00
|
|
|
{
|
2008-03-14 03:02:48 +01:00
|
|
|
// Copy the list
|
2008-11-16 02:22:20 +01:00
|
|
|
//shared_ptr<CActionList> actions = shared_ptr<CActionList>(new CActionList(_actionList));
|
2008-11-22 20:56:41 +01:00
|
|
|
shared_ptr<CActionList> actions = shared_ptr<CActionList>(new CActionList(_actionList));
|
2008-11-16 02:22:20 +01:00
|
|
|
bool canUndo = actions->CanUndo();
|
2008-03-14 03:02:48 +01:00
|
|
|
|
2008-03-14 04:59:46 +01:00
|
|
|
// Setup undo
|
2008-11-22 20:56:41 +01:00
|
|
|
shared_ptr<CActionList> undo = shared_ptr<CActionList>(new CActionList(actions->GetModel(),actions->GetName(),actions->GetOwner(),canUndo));
|
2008-03-14 04:59:46 +01:00
|
|
|
ActionStack *stack;
|
|
|
|
if (type == 1) stack = &redoStack;
|
|
|
|
else stack = &undoStack;
|
2008-03-14 04:33:27 +01:00
|
|
|
|
2008-03-14 04:59:46 +01:00
|
|
|
// Execute actions
|
2008-08-03 18:17:35 +02:00
|
|
|
std::list<Action>::const_iterator cur;
|
2008-11-16 02:22:20 +01:00
|
|
|
std::list<Action> acts = actions->GetActions();
|
|
|
|
for (cur=acts.begin();cur!=acts.end();cur++) {
|
2008-03-14 04:59:46 +01:00
|
|
|
// Inserts the opposite into the undo action first
|
2008-11-16 02:22:20 +01:00
|
|
|
if (canUndo) undo->AddActionStart((*cur)->GetAntiAction());
|
2008-03-14 04:59:46 +01:00
|
|
|
|
|
|
|
// Execute the action itself
|
2008-11-11 04:54:20 +01:00
|
|
|
(*cur)->Execute();
|
2008-03-14 03:02:48 +01:00
|
|
|
}
|
2008-01-30 07:35:51 +01:00
|
|
|
|
2008-03-14 04:59:46 +01:00
|
|
|
// Insert into undo stack
|
2008-11-16 02:22:20 +01:00
|
|
|
if (canUndo) {
|
2008-03-16 07:41:23 +01:00
|
|
|
stack->push_back(undo);
|
2008-09-20 23:21:28 +02:00
|
|
|
if (stack->size() > undoLimit) stack->pop_front();
|
2008-03-16 07:41:23 +01:00
|
|
|
if (type == 0) redoStack.clear();
|
2008-03-14 04:59:46 +01:00
|
|
|
}
|
|
|
|
|
2008-01-30 07:35:51 +01:00
|
|
|
// Notify listeners
|
|
|
|
DispatchNotifications(Notification());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-12 20:28:24 +01:00
|
|
|
//////////////////
|
|
|
|
// Load subtitles
|
2008-11-21 05:13:33 +01:00
|
|
|
void CModel::Load(Reader &input,const Format _format)
|
2008-03-08 04:08:50 +01:00
|
|
|
{
|
2008-03-08 08:14:14 +01:00
|
|
|
// Autodetect format
|
2008-03-14 03:02:48 +01:00
|
|
|
if (!_format) {
|
2008-03-08 04:08:50 +01:00
|
|
|
// TODO
|
2008-03-08 08:14:14 +01:00
|
|
|
|
|
|
|
// No format found
|
2008-03-18 04:44:00 +01:00
|
|
|
THROW_ATHENA_EXCEPTION(Exception::No_Format_Handler);
|
2008-03-08 04:08:50 +01:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:14:14 +01:00
|
|
|
// Get handler
|
2008-11-16 01:58:08 +01:00
|
|
|
FormatHandler handler = _format->GetHandler();
|
2008-03-18 04:44:00 +01:00
|
|
|
if (!handler) THROW_ATHENA_EXCEPTION(Exception::No_Format_Handler);
|
2008-03-08 04:08:50 +01:00
|
|
|
|
2008-03-13 22:28:22 +01:00
|
|
|
// Clear the model first
|
|
|
|
Clear();
|
|
|
|
|
2008-03-08 04:08:50 +01:00
|
|
|
// Load
|
2008-11-21 05:13:33 +01:00
|
|
|
handler->Load(*this,input);
|
2008-03-14 03:02:48 +01:00
|
|
|
|
|
|
|
// Set the format
|
|
|
|
format = _format;
|
2008-03-08 04:08:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
2008-03-12 20:28:24 +01:00
|
|
|
// Save subtitles
|
2008-11-23 03:39:00 +01:00
|
|
|
void CModel::Save(Writer output,const Format _format) const
|
2008-03-08 04:08:50 +01:00
|
|
|
{
|
2008-03-14 03:02:48 +01:00
|
|
|
// Use another format
|
|
|
|
if (_format && _format != format) {
|
2008-03-13 06:39:03 +01:00
|
|
|
// TODO
|
2008-03-18 04:44:00 +01:00
|
|
|
THROW_ATHENA_EXCEPTION(Exception::TODO);
|
2008-03-13 06:39:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get handler
|
2008-11-16 01:58:08 +01:00
|
|
|
FormatHandler handler = format->GetHandler();
|
2008-03-18 04:44:00 +01:00
|
|
|
if (!handler) THROW_ATHENA_EXCEPTION(Exception::No_Format_Handler);
|
2008-03-13 06:39:03 +01:00
|
|
|
|
|
|
|
// Load
|
2008-11-21 05:13:33 +01:00
|
|
|
handler->Save(*this,output);
|
2008-03-08 04:08:50 +01:00
|
|
|
}
|
2008-03-08 23:49:26 +01:00
|
|
|
|
|
|
|
|
2008-03-13 06:39:03 +01:00
|
|
|
/////////////////////////
|
|
|
|
// Inserts a new section
|
2008-11-16 02:02:34 +01:00
|
|
|
Section CModel::AddSection(String name)
|
2008-03-13 06:39:03 +01:00
|
|
|
{
|
2008-11-16 01:58:08 +01:00
|
|
|
ConstSection prev = GetSection(name);
|
2008-03-18 04:44:00 +01:00
|
|
|
if (prev) THROW_ATHENA_EXCEPTION(Exception::Section_Already_Exists);
|
2008-11-16 02:02:34 +01:00
|
|
|
|
|
|
|
Section result = Section(new CSection(name));
|
|
|
|
sections.push_back(result);
|
|
|
|
return result;
|
2008-03-13 06:39:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-08 23:49:26 +01:00
|
|
|
//////////////////
|
|
|
|
// Gets a section
|
2008-11-11 04:54:20 +01:00
|
|
|
ConstSection CModel::GetSection(String name) const
|
|
|
|
{
|
|
|
|
size_t len = sections.size();
|
|
|
|
for (size_t i=0;i<len;i++) {
|
|
|
|
if (sections[i]->GetName() == name) return sections[i];
|
|
|
|
}
|
|
|
|
return SectionPtr();
|
|
|
|
}
|
|
|
|
|
2008-11-16 01:58:08 +01:00
|
|
|
Section CModel::GetMutableSection(String name)
|
2008-03-08 23:49:26 +01:00
|
|
|
{
|
2008-03-13 06:39:03 +01:00
|
|
|
size_t len = sections.size();
|
|
|
|
for (size_t i=0;i<len;i++) {
|
|
|
|
if (sections[i]->GetName() == name) return sections[i];
|
2008-03-08 23:49:26 +01:00
|
|
|
}
|
2008-03-13 04:42:27 +01:00
|
|
|
return SectionPtr();
|
2008-03-08 23:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-13 06:39:03 +01:00
|
|
|
////////////////////////
|
|
|
|
// Get section by index
|
2008-11-11 04:54:20 +01:00
|
|
|
ConstSection CModel::GetSectionByIndex(size_t index) const
|
|
|
|
{
|
|
|
|
return sections.at(index);
|
|
|
|
}
|
|
|
|
|
2008-11-16 01:58:08 +01:00
|
|
|
Section CModel::GetMutableSectionByIndex(size_t index)
|
2008-03-08 23:49:26 +01:00
|
|
|
{
|
2008-03-13 06:39:03 +01:00
|
|
|
return sections.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// Get section count
|
2008-08-03 18:17:35 +02:00
|
|
|
size_t CModel::GetSectionCount() const
|
2008-03-13 06:39:03 +01:00
|
|
|
{
|
|
|
|
return sections.size();
|
2008-03-08 23:49:26 +01:00
|
|
|
}
|
2008-03-13 22:28:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
//////////
|
|
|
|
// Clear
|
2008-08-03 18:17:35 +02:00
|
|
|
void CModel::Clear()
|
2008-03-13 22:28:22 +01:00
|
|
|
{
|
|
|
|
sections.clear();
|
2008-03-16 07:41:23 +01:00
|
|
|
undoStack.clear();
|
|
|
|
redoStack.clear();
|
2008-03-14 04:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Can undo/redo?
|
2008-08-03 18:17:35 +02:00
|
|
|
bool CModel::CanUndo(const String owner) const
|
2008-03-14 04:33:27 +01:00
|
|
|
{
|
|
|
|
(void) owner;
|
|
|
|
return undoStack.size() > 0;
|
|
|
|
}
|
2008-08-03 18:17:35 +02:00
|
|
|
bool CModel::CanRedo(const String owner) const
|
2008-03-14 04:33:27 +01:00
|
|
|
{
|
|
|
|
(void) owner;
|
|
|
|
return redoStack.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Perform an undo
|
2008-08-03 18:17:35 +02:00
|
|
|
void CModel::Undo(const String owner)
|
2008-03-14 04:33:27 +01:00
|
|
|
{
|
2008-03-14 04:59:46 +01:00
|
|
|
ActivateStack(undoStack,true,owner);
|
2008-03-14 04:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Perform a redo
|
2008-08-03 18:17:35 +02:00
|
|
|
void CModel::Redo(const String owner)
|
2008-03-14 04:33:27 +01:00
|
|
|
{
|
2008-03-14 04:59:46 +01:00
|
|
|
ActivateStack(redoStack,false,owner);
|
2008-03-14 04:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// Perform undo/redo
|
2008-08-09 22:43:27 +02:00
|
|
|
void CModel::ActivateStack(ActionStack stack,bool isUndo,const String &owner)
|
2008-03-14 04:33:27 +01:00
|
|
|
{
|
|
|
|
// TODO: do something with this
|
|
|
|
(void) owner;
|
|
|
|
|
|
|
|
// Process list
|
2008-09-19 05:11:23 +02:00
|
|
|
//ProcessActionList(stack.back(),isUndo?1:2);
|
|
|
|
ProcessActionList(*static_pointer_cast<CActionList>(stack.back()),isUndo?1:2);
|
2008-03-14 04:33:27 +01:00
|
|
|
|
|
|
|
// Pop original
|
2008-03-16 07:41:23 +01:00
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
// Get undo/redo messages
|
2008-08-03 18:17:35 +02:00
|
|
|
String CModel::GetUndoMessage(const String owner) const
|
2008-03-16 07:41:23 +01:00
|
|
|
{
|
|
|
|
(void) owner;
|
|
|
|
if (CanUndo()) return undoStack.back()->GetName();
|
2008-11-08 23:07:23 +01:00
|
|
|
return "";
|
2008-03-16 07:41:23 +01:00
|
|
|
}
|
2008-08-03 18:17:35 +02:00
|
|
|
String CModel::GetRedoMessage(const String owner) const
|
2008-03-16 07:41:23 +01:00
|
|
|
{
|
|
|
|
(void) owner;
|
|
|
|
if (CanRedo()) return redoStack.back()->GetName();
|
2008-11-08 23:07:23 +01:00
|
|
|
return "";
|
2008-03-13 22:28:22 +01:00
|
|
|
}
|
2008-07-27 20:26:36 +02:00
|
|
|
|
|
|
|
|
2008-08-09 22:43:27 +02:00
|
|
|
/////////////////////
|
|
|
|
// Create controller
|
|
|
|
Controller CModel::CreateController() {
|
|
|
|
return Controller(new CController(Model(weakThis)));
|
2008-09-20 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// Sets the maximum number of undo levels
|
|
|
|
void CModel::SetUndoLimit(size_t levels)
|
|
|
|
{
|
|
|
|
undoLimit = levels;
|
|
|
|
while (undoStack.size() > undoLimit) {
|
|
|
|
undoStack.pop_front();
|
|
|
|
}
|
|
|
|
}
|