1#!/usr/bin/env kross 2 3# Load the Qt4-QtRuby module. 4begin 5 require 'Qt' 6rescue LoadError 7 raise "Failed to load the required QtRuby module. Please install Qt4-QtRuby." 8end 9 10# Load the Words scripting module. 11begin 12 require 'Words' 13rescue LoadError 14 # If loading failed (cause the script was not executed from within 15 # a running Words instance) we try to load the Kross module and 16 # ask it to load the Words module for us. 17 require 'Kross' 18 Words = Kross.module('Words') 19 20 # Testcase to fill Words with some content. You are also able to e.g. 21 # load a document here with "Words.document().openUrl('/path/file.odt')" 22 mytextframeset1 = Words.addTextFrameSet("myFirstTextFrame") 23 mytextframeset1.addTextFrame() 24 mytextframeset1.document().setHtml("<h1>First Header</h1><p>Some text</p><p>Some more text with <b>bold</b> and <i>italic</i> and <u>underline</u> to test the stuff.</p>") 25 mytextframeset2 = Words.addTextFrameSet("mySecondTextFrame") 26 mytextframeset2.addTextFrame() 27 mytextframeset2.document().setHtml("<h1>Second Header</h1><h2>First Sub Header</h2><p>Some text.</p><h2>Second Sub Header</h2><p>Some text.</p>") 28end 29 30class Dialog < Qt::Dialog 31 32 slots 'startClicked()','endClicked()','startOfLineClicked()','endOfLineClicked()' 33 slots 'startOfBlockClicked()','endOfBlockClicked()','previousBlockClicked()','nextBlockClicked()' 34 slots 'startOfWordClicked()','endOfWordClicked()','previousWordClicked()','nextWordClicked()' 35 slots 'upClicked()','downClicked()','leftClicked()','rightClicked()' 36 37 def initialize 38 super() 39 self.windowTitle = 'Cursor' 40 41 layout = Qt::VBoxLayout.new 42 setLayout(layout) 43 44# fs = Words.frameSet(0) 45# doc = fs.document() 46# puts "doc.c() ............................" 47# c = doc.c() 48# puts "position=%s" % c.position() 49# puts "anchor=%s" % c.anchor() 50# puts "hasSelection=%s" % c.hasSelection() 51# puts "selectedText=%s" % c.selectedText() 52# puts "doc.firstc() ............................" 53# c = doc.firstc() 54# puts "position=%s" % c.position() 55# puts "anchor=%s" % c.anchor() 56# puts "hasSelection=%s" % c.hasSelection() 57# puts "selectedText=%s" % c.selectedText() 58# puts "doc.lastc() ............................" 59# c = doc.lastc() 60# puts "position=%s" % c.position() 61# puts "anchor=%s" % c.anchor() 62# puts "hasSelection=%s" % c.hasSelection() 63# puts "selectedText=%s" % c.selectedText() 64# puts "doc.rootFrame().c() ............................" 65# c = doc.rootFrame().c() 66# puts "position=%s" % c.position() 67# puts "anchor=%s" % c.anchor() 68# puts "hasSelection=%s" % c.hasSelection() 69# puts "selectedText=%s" % c.selectedText() 70# puts "doc.rootFrame().firstcPosition() ............................" 71# c = doc.rootFrame().firstcPosition() 72# puts "position=%s" % c.position() 73# puts "anchor=%s" % c.anchor() 74# puts "hasSelection=%s" % c.hasSelection() 75# puts "selectedText=%s" % c.selectedText() 76# puts "doc.rootFrame().lastcPosition() ............................" 77# c = doc.rootFrame().lastcPosition() 78# puts "position=%s" % c.position() 79# puts "anchor=%s" % c.anchor() 80# puts "hasSelection=%s" % c.hasSelection() 81# puts "selectedText=%s" % c.selectedText() 82# puts "fs.frame(0).c() ............................" 83# puts "fs.frame(0).position=%s" % fs.frame(0).position() 84# puts "fs.frame(0).endPosition=%s" % fs.frame(0).endPosition() 85# c = fs.frame(0).c() 86# puts "c.position=%s" % c.position() 87# puts "c.anchor=%s" % c.anchor() 88# puts "c.hasSelection=%s" % c.hasSelection() 89# puts "c.selectedText=%s" % c.selectedText() 90# puts "Words.activec() ............................" 91 92 @cursor = Words.activeCursor() 93 if not @cursor 94 doc = Words.mainFrameSet().document() 95 @cursor = doc.cursor 96 if not @cursor 97 raise "Failed to get the Words cursor." 98 end 99 end 100 101 w = Qt::Widget.new(self) 102 l = Qt::VBoxLayout.new 103 #l.setMargin(0) 104 w.setLayout(l) 105 layout.addWidget(w) 106 l.addWidget( Qt::Label.new("position:%s anchor:%s" % [@cursor.position,@cursor.anchor], w) ) 107 if @cursor.hasSelection 108 l.addWidget( Qt::Label.new("selectionStart:%s selectionEnd:%s" % [@cursor.selectionStart,@cursor.selectionEnd], w) ) 109 #l.addWidget( Qt::Label.new("selectedText:%s" % @cursor.selectedText, w) ) 110 #l.addWidget( Qt::Label.new("selectedHtml:%s" % @cursor.selectedHtml, w) ) 111 end 112 113 w = Qt::Widget.new(self) 114 l = Qt::HBoxLayout.new 115 l.setMargin(0) 116 w.setLayout(l) 117 layout.addWidget(w) 118 btn = Qt::PushButton.new("Start",w) 119 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('startClicked()')) 120 l.addWidget(btn) 121 btn = Qt::PushButton.new("End", w) 122 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('endClicked()')) 123 l.addWidget(btn) 124 btn = Qt::PushButton.new("StartOfLine", w) 125 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('startOfLineClicked()')) 126 l.addWidget(btn) 127 btn = Qt::PushButton.new("EndOfLine", w) 128 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('endOfLineClicked()')) 129 l.addWidget(btn) 130 131 w = Qt::Widget.new(self) 132 l = Qt::HBoxLayout.new 133 l.setMargin(0) 134 w.setLayout(l) 135 layout.addWidget(w) 136 btn = Qt::PushButton.new("StartOfBlock", w) 137 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('startOfBlockClicked()')) 138 l.addWidget(btn) 139 btn = Qt::PushButton.new("EndOfBlock", w) 140 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('endOfBlockClicked()')) 141 l.addWidget(btn) 142 btn = Qt::PushButton.new("PreviousBlock", w) 143 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('previousBlockClicked()')) 144 l.addWidget(btn) 145 btn = Qt::PushButton.new("NextBlock", w) 146 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('nextBlockClicked()')) 147 l.addWidget(btn) 148 149 w = Qt::Widget.new(self) 150 l = Qt::HBoxLayout.new 151 l.setMargin(0) 152 w.setLayout(l) 153 layout.addWidget(w) 154 btn = Qt::PushButton.new("StartOfWord", w) 155 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('startOfWordClicked()')) 156 l.addWidget(btn) 157 btn = Qt::PushButton.new("EndOfWord", w) 158 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('endOfWordClicked()')) 159 l.addWidget(btn) 160 btn = Qt::PushButton.new("PreviousWord", w) 161 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('previousWordClicked()')) 162 l.addWidget(btn) 163 btn = Qt::PushButton.new("NextWord", w) 164 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('nextWordClicked()')) 165 l.addWidget(btn) 166 167 w = Qt::Widget.new(self) 168 l = Qt::HBoxLayout.new 169 l.setMargin(0) 170 w.setLayout(l) 171 layout.addWidget(w) 172 btn = Qt::PushButton.new("Up", w) 173 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('upClicked()')) 174 l.addWidget(btn) 175 btn = Qt::PushButton.new("Down", w) 176 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('downClicked()')) 177 l.addWidget(btn) 178 btn = Qt::PushButton.new("Left", w) 179 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('leftClicked()')) 180 l.addWidget(btn) 181 btn = Qt::PushButton.new("Right", w) 182 connect(btn, SIGNAL('clicked(bool)'), self, SLOT('rightClicked()')) 183 l.addWidget(btn) 184 185 resize( Qt::Size.new(200, 100).expandedTo( minimumSizeHint ) ); 186 end 187 188 def cursor 189 return @cursor 190 end 191 192 #WordLeft 193 #PreviousCharacter 194 #NextCharacter 195 196 def startClicked() 197 @cursor.movePosition(@cursor.Start) 198 end 199 def endClicked() 200 @cursor.movePosition(@cursor.End) 201 end 202 def startOfLineClicked() 203 @cursor.movePosition(@cursor.StartOfLine) 204 end 205 def endOfLineClicked() 206 @cursor.movePosition(@cursor.EndOfLine) 207 end 208 209 def startOfBlockClicked() 210 @cursor.movePosition(@cursor.StartOfBlock) 211 end 212 def endOfBlockClicked() 213 @cursor.movePosition(@cursor.EndOfBlock) 214 end 215 def previousBlockClicked() 216 @cursor.movePosition(@cursor.PreviousBlock) 217 end 218 def nextBlockClicked() 219 @cursor.movePosition(@cursor.NextBlock) 220 end 221 222 def startOfWordClicked() 223 @cursor.movePosition(@cursor.StartOfWord) 224 end 225 def endOfWordClicked() 226 @cursor.movePosition(@cursor.EndOfWord) 227 end 228 def previousWordClicked() 229 @cursor.movePosition(@cursor.PreviousWord) 230 end 231 def nextWordClicked() 232 @cursor.movePosition(@cursor.NextWord) 233 end 234 235 def upClicked() 236 @cursor.movePosition(@cursor.Up) 237 end 238 def downClicked() 239 @cursor.movePosition(@cursor.Down) 240 end 241 def leftClicked() 242 @cursor.movePosition(@cursor.Left) 243 end 244 def rightClicked() 245 @cursor.movePosition(@cursor.Right) 246 end 247 248end 249 250dialog = Dialog.new 251dialog.exec 252puts "setActiveCursor ...............1" 253Words.setActiveCursor( dialog.cursor ) 254puts "setActiveCursor ...............2" 255