Added a "modify line" action.

Originally committed to SVN as r2067.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-16 02:20:34 +00:00
parent 0a0f383b4a
commit fb1de2da07
9 changed files with 85 additions and 6 deletions

View file

@ -84,4 +84,19 @@ namespace Gorgonsub {
ActionPtr GetAntiAction(const Model &model) const; ActionPtr GetAntiAction(const Model &model) const;
void Execute(Model &model); void Execute(Model &model);
}; };
// Modify line
class ActionModify : public Action {
private:
shared_ptr<SectionEntry> entry;
const String section;
int lineNumber;
public:
ActionModify(shared_ptr<SectionEntry> entry,int line,const String &section);
virtual ~ActionModify() {}
ActionPtr GetAntiAction(const Model &model) const;
void Execute(Model &model);
};
}; };

View file

@ -68,6 +68,7 @@ namespace Gorgonsub {
void Finish(); void Finish();
void InsertLine(SectionEntryPtr line,int position=-1,const String section=L""); void InsertLine(SectionEntryPtr line,int position=-1,const String section=L"");
void RemoveLine(int position,const String section); void RemoveLine(int position,const String section);
SectionEntryPtr ModifyLine(int position,const String section);
}; };
typedef shared_ptr<ActionList> ActionListPtr; typedef shared_ptr<ActionList> ActionListPtr;

View file

@ -70,6 +70,7 @@ namespace Gorgonsub {
void RemoveEntryByIndex(size_t index); void RemoveEntryByIndex(size_t index);
void RemoveEntry(SectionEntryPtr entry); void RemoveEntry(SectionEntryPtr entry);
SectionEntryPtr GetEntry(size_t index) const; SectionEntryPtr GetEntry(size_t index) const;
SectionEntryPtr& GetEntryRef(size_t index);
size_t GetEntryCount() const; size_t GetEntryCount() const;
}; };
typedef shared_ptr<Section> SectionPtr; typedef shared_ptr<Section> SectionPtr;

View file

@ -77,6 +77,7 @@ namespace Gorgonsub {
public: public:
virtual SectionEntryType GetType() const =0; virtual SectionEntryType GetType() const =0;
virtual String GetDefaultGroup() const =0; virtual String GetDefaultGroup() const =0;
virtual SectionEntryPtr Clone() const =0;
static const SectionEntryPlainPtr GetAsPlain(const SectionEntryPtr &ptr); static const SectionEntryPlainPtr GetAsPlain(const SectionEntryPtr &ptr);
static const SectionEntryDialoguePtr GetAsDialogue(const SectionEntryPtr &ptr); static const SectionEntryDialoguePtr GetAsDialogue(const SectionEntryPtr &ptr);

View file

@ -45,7 +45,7 @@ SectionPtr Action::GetSection(const Model &model,const String &name) const
} }
/////////////////// Insert line ///////////////////////////// ///////////////////////////// Insert line /////////////////////////////
/////////////// ///////////////
// Constructor // Constructor
@ -79,7 +79,7 @@ void ActionInsert::Execute(Model &model)
/////////////////// Remove line ///////////////////////////// ///////////////////////////// Remove line /////////////////////////////
/////////////// ///////////////
@ -110,3 +110,35 @@ void ActionRemove::Execute(Model &model)
// Remove the line // Remove the line
section->RemoveEntryByIndex(lineNumber); section->RemoveEntryByIndex(lineNumber);
} }
///////////////////////////// Modify line /////////////////////////////
///////////////
// Constructor
ActionModify::ActionModify(shared_ptr<SectionEntry> data,int line,const String &sName)
: entry(data), lineNumber(line), section(sName) {}
/////////////////////////////////
// Create anti-action for insert
ActionPtr ActionModify::GetAntiAction(const Model &model) const
{
SectionPtr sect = GetSection(model,section);
SectionEntryPtr entry = sect->GetEntry(lineNumber);
return ActionPtr(new ActionModify(entry,lineNumber,section));
}
/////////////////////
// Execute insertion
void ActionModify::Execute(Model &model)
{
// Find the section to insert it on
String sectionName = section;
if (sectionName.IsEmpty()) sectionName = entry->GetDefaultGroup();
SectionPtr sect = GetSection(model,sectionName);
// Insert the line
sect->GetEntryRef(lineNumber) = entry;
}

View file

@ -118,3 +118,15 @@ void ActionList::RemoveLine(int position,const String section)
ActionPtr action = ActionPtr (new ActionRemove(position,section)); ActionPtr action = ActionPtr (new ActionRemove(position,section));
AddAction(action); AddAction(action);
} }
/////////////////////////////////
// Insert a "modify line" action
SectionEntryPtr ActionList::ModifyLine(int position,const String section)
{
SectionPtr sect = model.GetSection(section);
SectionEntryPtr entry = sect->GetEntry(position)->Clone();
ActionPtr action = ActionPtr (new ActionModify(entry,position,section));
AddAction(action);
return entry;
}

View file

@ -91,12 +91,15 @@ namespace Gorgonsub {
DialogueASS(); DialogueASS();
DialogueASS(const String &data,int version); DialogueASS(const String &data,int version);
// Basic features
String GetDefaultGroup() const { return L"Events"; }
SectionEntryPtr Clone() const { return SectionEntryPtr(new DialogueASS(*this)); }
// Capabilities // Capabilities
bool HasText() const { return true; } bool HasText() const { return true; }
bool HasTime() const { return true; } bool HasTime() const { return true; }
bool HasStyle() const { return true; } bool HasStyle() const { return true; }
bool HasMargins() const { return true; } bool HasMargins() const { return true; }
String GetDefaultGroup() const { return L"Events"; }
// Read accessors // Read accessors
const String& GetText() const { return text; } const String& GetText() const { return text; }
@ -158,14 +161,16 @@ namespace Gorgonsub {
StyleASS(); StyleASS();
StyleASS(String data,int version); StyleASS(String data,int version);
// Basic features
String GetDefaultGroup() const;
SectionEntryPtr Clone() const { return SectionEntryPtr(new StyleASS(*this)); }
// Read accessors // Read accessors
String GetName() const { return name; } String GetName() const { return name; }
String GetFontName() const { return font; } String GetFontName() const { return font; }
float GetFontSize() const { return fontSize; } float GetFontSize() const { return fontSize; }
Colour GetColour(int n) const { return colour.at(n); } Colour GetColour(int n) const { return colour.at(n); }
int GetMargin(int n) const { return margin.at(n); } int GetMargin(int n) const { return margin.at(n); }
String GetDefaultGroup() const;
}; };
// Raw line // Raw line
@ -178,9 +183,12 @@ namespace Gorgonsub {
PlainASS(); PlainASS();
PlainASS(String _data) : data(_data) {} PlainASS(String _data) : data(_data) {}
// Basic features
String GetDefaultGroup() const { return L"Events"; }
SectionEntryPtr Clone() const { return SectionEntryPtr(new PlainASS(*this)); }
String GetText() const { return data; } String GetText() const { return data; }
void SetText(const String &_data) { data = _data; } void SetText(const String &_data) { data = _data; }
String GetDefaultGroup() const { return L"Events"; }
}; };
// Advanced Substation Alpha format base class // Advanced Substation Alpha format base class

View file

@ -75,6 +75,11 @@ SectionEntryPtr Section::GetEntry(size_t index) const
return entries[index]; return entries[index];
} }
SectionEntryPtr& Section::GetEntryRef(size_t index)
{
return entries[index];
}
size_t Section::GetEntryCount() const size_t Section::GetEntryCount() const
{ {
return entries.size(); return entries.size();

View file

@ -84,6 +84,8 @@ int main()
ActionListPtr actions = control.CreateActionList(L"Insert line"); ActionListPtr actions = control.CreateActionList(L"Insert line");
actions->InsertLine(line,2); actions->InsertLine(line,2);
actions->RemoveLine(3,L"Events"); actions->RemoveLine(3,L"Events");
SectionEntryDialoguePtr diag = dynamic_pointer_cast<SectionEntryDialogue> (actions->ModifyLine(10,L"Events"));
diag->SetText(L"Hay guise sup");
actions->Finish(); actions->Finish();
timer.Pause(); timer.Pause();
cout << "Done in " << timer.Time() << " ms.\n"; cout << "Done in " << timer.Time() << " ms.\n";
@ -98,6 +100,8 @@ int main()
timer.Pause(); timer.Pause();
cout << "Done in " << timer.Time() << " ms.\n"; cout << "Done in " << timer.Time() << " ms.\n";
control.SaveFile(L"subs_out2.ass",L"UTF-8"); control.SaveFile(L"subs_out2.ass",L"UTF-8");
control.Undo();
control.SaveFile(L"subs_out3.ass",L"UTF-8");
} }
catch (std::exception &e) { catch (std::exception &e) {