00001 00005 /* Copyright © 2009 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 00012 #include "Document.h" 00013 00014 #include <istream> 00015 #include <vector> 00016 00017 #include <libtrack/Track.h> 00018 00019 namespace Document 00020 { 00021 00022 Document::Document(const Track::Theme & theme) 00023 : track(theme, true) 00024 , commands_from_save(0) 00025 , saved_in_history(false) 00026 { 00027 } 00028 00029 Document::Document(std::istream & source, const Track::Theme &theme) 00030 : track(source, theme, true) 00031 , commands_from_save(0) 00032 , saved_in_history(true) 00033 { 00034 } 00035 00036 Document::~Document() 00037 { 00038 } 00039 00040 void Document::do_command(boost::shared_ptr<DocumentDelta> delta) 00041 { 00042 cancel_preview(); 00043 if (!delta) return; 00044 delta->apply(track); 00045 done.push_back(delta); 00046 // We can't redo events in the queue, since they depend on this command not 00047 // having been done. 00048 undone.clear(); 00049 commands_from_save++; 00050 if (commands_from_save <= 0) 00051 { 00052 // We've cleared the redo history that included the saved state. 00053 saved_in_history = false; 00054 } 00055 m_signal_command_run.emit(); 00056 } 00057 00058 void Document::preview_command(boost::shared_ptr<DocumentDelta> delta) 00059 { 00060 cancel_preview(); 00061 if (!delta) return; 00062 temporary_command = delta; 00063 delta->apply(track); 00064 m_signal_command_run.emit(); 00065 } 00066 00067 void Document::cancel_preview() 00068 { 00069 if (temporary_command) 00070 { 00071 temporary_command->unapply(track); 00072 m_signal_command_run.emit(); 00073 temporary_command = boost::shared_ptr<DocumentDelta>(); 00074 } 00075 } 00076 00077 void Document::undo_command() 00078 { 00079 cancel_preview(); 00080 if (!done.empty()) 00081 { 00082 done.back()->unapply(track); 00083 undone.push_back(done.back()); 00084 done.pop_back(); 00085 commands_from_save--; 00086 00087 m_signal_command_run.emit(); 00088 } 00089 } 00090 00091 void Document::redo_command() 00092 { 00093 cancel_preview(); 00094 if (!undone.empty()) 00095 { 00096 undone.back()->apply(track); 00097 done.push_back(undone.back()); 00098 undone.pop_back(); 00099 commands_from_save++; 00100 m_signal_command_run.emit(); 00101 } 00102 } 00103 00104 sigc::signal<void> Document::signal_command_run() 00105 { 00106 return m_signal_command_run; 00107 } 00108 00109 bool Document::get_undo_avaliable() const 00110 { 00111 return !done.empty(); 00112 } 00113 00114 bool Document::get_redo_avaliable() const 00115 { 00116 return !undone.empty(); 00117 } 00118 00119 void Document::save(std::ostream & destination) 00120 { 00121 cancel_preview(); 00122 // Write track information to output stream. 00123 destination << track; 00124 commands_from_save = 0; 00125 saved_in_history = true; 00126 } 00127 00128 bool Document::is_saved() const 00129 { 00130 return saved_in_history && (commands_from_save == 0); 00131 } 00132 00133 const Track::Track & Document::get_track() const 00134 { 00135 return track; 00136 } 00137 00138 unsigned int Document::get_changes_since_save() const 00139 { 00140 if (commands_from_save > 0) return unsigned(commands_from_save); 00141 return unsigned(-commands_from_save); 00142 } 00143 00144 }
Generated at Mon Sep 6 00:41:11 2010 by Doxygen version 1.4.7 for Racer version svn335.