Class: MyTextEditor
- Defined in:
- ext/ruby/qtruby/examples/textedit/textedit.rb
Instance Method Summary collapse
- #attempt_metadata_load(fname) ⇒ Object
-
#createPopupMenu(pos) ⇒ Object
virtual.
- #has_metadata ⇒ Object
-
#initialize(w = nil) ⇒ MyTextEditor
constructor
A new instance of MyTextEditor.
- #insert_icon ⇒ Object
- #insert_richtext(richtext) ⇒ Object
- #load_image(fname, image_id) ⇒ Object
- #metadata_clear ⇒ Object
- #metadata_fname(fname) ⇒ Object
- #metadata_save_if_has(fname) ⇒ Object
- #new(txt = "") ⇒ Object
- #next_image_id ⇒ Object
- #open ⇒ Object
- #save_as ⇒ Object
Constructor Details
#initialize(w = nil) ⇒ MyTextEditor
Returns a new instance of MyTextEditor.
11 12 13 14 15 16 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 11 def initialize(w = nil) @images = {} @@next_image_id = 0 super(w) self.setTextFormat(Qt::RichText) end |
Instance Method Details
#attempt_metadata_load(fname) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 52 def (fname) return unless File.exists?((fname)) file = File.open((fname)) @xmldoc = REXML::Document.new file @xmldoc.root.elements.each("image") { |image| image_id = image.attributes["ident"] img_fname = image.attributes["filename"] load_image(img_fname, image_id) } end |
#createPopupMenu(pos) ⇒ Object
virtual
41 42 43 44 45 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 41 def createPopupMenu(pos) # virtual pm = Qt::PopupMenu.new pm.insertItem("Insert Image!", self, SLOT('insert_icon()')) pm end |
#has_metadata ⇒ Object
46 47 48 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 46 def !@images.empty? end |
#insert_icon ⇒ Object
34 35 36 37 38 39 40 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 34 def insert_icon fname = Qt::FileDialog.getOpenFileName return if fname.nil? image_id = "image_#{next_image_id}" load_image(fname, image_id) insert_richtext('<qt><img source="'+image_id+'"></qt>') end |
#insert_richtext(richtext) ⇒ Object
17 18 19 20 21 22 23 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 17 def insert_richtext(richtext) # todo, use a rand string unique_string = '000___xxx123456789xxx___xxx123456789xxx___000' insert(unique_string) txt = self.text().gsub(unique_string, richtext) self.setText(txt) end |
#load_image(fname, image_id) ⇒ Object
27 28 29 30 31 32 33 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 27 def load_image(fname, image_id) pixmap = Qt::Pixmap.new(fname) msfactory = Qt::MimeSourceFactory.defaultFactory msfactory.setPixmap(image_id, pixmap) @images[image_id] = fname image_id end |
#metadata_clear ⇒ Object
74 75 76 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 74 def @images = {} end |
#metadata_fname(fname) ⇒ Object
49 50 51 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 49 def (fname) "#{fname}.metadata.xml" end |
#metadata_save_if_has(fname) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 63 def (fname) return if not = REXML::Document.new '<metadata/>' @images.each { |id, img_fname| .root.add_element("image", {"filename"=>img_fname, "ident"=>id}) } file = File.new((fname), "w") file.puts() file.close end |
#new(txt = "") ⇒ Object
77 78 79 80 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 77 def new(txt = "") self.setText(txt) end |
#next_image_id ⇒ Object
24 25 26 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 24 def next_image_id @@next_image_id += 1 end |
#open ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 81 def open fname = Qt::FileDialog.getOpenFileName return if fname.nil? unless File.exists?(fname) Qt::MessageBox.critical(self, "File Does Not Exist", "Sorry, unable to find the requested file!") return end return if fname.nil? txt = File.open(fname).gets(nil) (fname) self.setText(txt) end |
#save_as ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'ext/ruby/qtruby/examples/textedit/textedit.rb', line 94 def save_as fname = Qt::FileDialog.getSaveFileName return if fname.nil? if File.exists?(fname) Qt::MessageBox.critical(self, "File Already Exists", "Sorry, file already exists. Please choose a non-existing filename!") return save_as end file = File.new(fname, "w") file.puts(text()) file.close (fname) emit saved() end |