On 01/12 04:38, Jeremy Evans wrote:
> Fairly simple, these are ruby bindings for Qt4.  I originally started
> work on this at p2k10, and just recently picked it up again and got it
> working.
> 
> Tested on i386.  Compiles on amd64, but I haven't had a chance to test
> there yet.  Looking for OKs.
> 
> Jeremy

Made some changes so that you can install it side by side with
kdebindings3 (the ruby binding to Qt3).  Also, if you want to
test this, you'll need to use:

LD_PRELOAD=/usr/lib/libpthread.so.13.1

when running the ruby interpreter.  We don't enable pthreads in ruby as
it causes some issues, which is why you need to use LD_PRELOAD.

Tested on i386 and amd64. Looking for OKs.

Jeremy

If you want something to test with, here's a program (apologies for the
ugliness):

#!/usr/local/bin/ruby
require 'Qt4'
require 'set'

#Qt.debug_level = Qt::DebugLevel::High

class Qt::TextCursor
  def move_to_start
    move_position(Start, KeepAnchor)
  end

  def block_for_position(pos)
    set_position(pos, KeepAnchor)
    block
  end

  def set_block_background(block, format)
    set_position(block.position)
    select(BlockUnderCursor)
    set_char_format(format)
  end

  def insert_new_block
    insert_block
    move_position(NextBlock, KeepAnchor)
  end
end

class FilenameEditorSynchronizer < Qt::Object
  LIGHT_GRAY = Qt::TextCharFormat.new do
    set_background Qt::Brush.new(Qt::Color.new(211, 211, 211), Qt::SolidPattern)
  end
  WHITE = Qt::TextCharFormat.new do
    set_background Qt::Brush.new(Qt::Color.new(255, 255, 255), Qt::SolidPattern)
  end

  attr_reader :block_map, :ec, :oc, :ds, :cs

  def initialize(edit, orig)
    super()
    @ds = [edit.document, orig.document]
    Qt::Object.connect(edit.document, SIGNAL('contentsChange(int, int, int)'), 
self, SLOT('contents_change(int, int, int)'))
    @ec, @oc = @cs = [Qt::TextCursor.new(@ds.first), 
Qt::TextCursor.new(@ds.last)]
    reset_text
  end

  def reset_text
    @block_map = []
    @ignore = true
    ds.each{|d| d.clear}
    cs.each{|c| c.move_to_start}
    i = -1
    Dir['*'].sort.each do |f|
      i += 1
      if i != 0
        cs.each{|c| c.insert_new_block}
      end
      block_map << oc.block
      cs.each{|c| c.insert_text(f)}
    end
    @ignore = false
  end

  def change_dir
    return unless dir = Qt::FileDialog.getExistingDirectory(nil, "Select a 
Directory")
    Dir.chdir(dir)
    reset_text
    emit directory_changed("File Renamer - #{Dir.pwd}")
  end
  
  def contents_change(pos, rem, add)
    if !@ignore and eb = ec.block_for_position(pos) and ob = 
block_map[eb.block_number]
      format = eb.text != ob.text ? LIGHT_GRAY : WHITE
      ec.set_block_background(eb, format)
      oc.set_block_background(ob, format)
    end
  end

  def rename_files
    ed, od = @ds
    ed.block_count.times do |i|
      nt = ed.find_block_by_number(i).text
      ot = od.find_block_by_number(i).text
      File.rename(ot, nt) unless nt == ot
    end
    reset_text
  end
  
  signals 'files_renamed()', 'directory_changed(const QString&)'
  slots 'contents_change(int, int, int)', 'rename_files()', 'change_dir()', 
'reset_text()'
end

class FilenameEditor < Qt::TextEdit
  attr_reader :captured_events, :capture
  
  def initialize(*)
    super
    self.auto_fill_background = true
    self.word_wrap_mode = Qt::TextEdit::NoWrap
    self.font = Qt::Font.new('Bitstream Vera Sans Mono', 12, 75)
  end

  def keyPressEvent(event)
    super
    captured_events.push([event.key, event.modifiers, event.text]) if capture
  end

  def replay
    return unless captured_events
    @capture = false
    captured_events.each{|event| Qt::Application.sendEvent(self, 
Qt::KeyEvent.new(Qt::KeyEvent::KeyPress, *event))}
  end
    
  def start_capture
    @capture = true
    @captured_events = []
  end
  
  def stop_capture
    @capture = false
  end
  
  slots 'replay()', 'start_capture()', 'stop_capture()'
end

class FilenameViewer < Qt::TextEdit
  def initialize(*)
    super
    self.auto_fill_background = true
    self.word_wrap_mode = Qt::TextEdit::NoWrap
    self.font = Qt::Font.new('Bitstream Vera Sans Mono', 12, 75)
    self.read_only = true
  end
end

class RenamerGUI < Qt::Application
  def initialize(*)
    super
    @main = Qt::Widget.new
    @main_box = Qt::VBoxLayout.new

    @main_box.add_widget(@buttons = Qt::Widget.new)
    @button_box = Qt::HBoxLayout.new
    @button_box.add_widget(@start_capture = Qt::PushButton.new("Start Capture"))
    @button_box.add_widget(@stop_capture = Qt::PushButton.new("Stop Capture"))
    @button_box.add_widget(@replay = Qt::PushButton.new("Replay Capture"))
    @button_box.add_widget(@change_dir = Qt::PushButton.new("Change Directory"))
    @button_box.add_widget(@reload = Qt::PushButton.new("Reload Files"))
    @button_box.add_widget(@rename = Qt::PushButton.new("Rename Files"))
    
    @main_box.add_widget(@texts = Qt::Widget.new)
    @text_box = Qt::HBoxLayout.new
    @text_box.add_widget(@orig_names = FilenameViewer.new)
    @text_box.add_widget(@new_names = FilenameEditor.new)
    @new_names.vertical_scroll_bar = @orig_names.vertical_scroll_bar
    @new_names.horizontal_scroll_bar = @orig_names.horizontal_scroll_bar

    @buttons.layout = @button_box
    @texts.layout = @text_box
    @main.layout = @main_box

    @synchronizer = FilenameEditorSynchronizer.new(@new_names, @orig_names)
    
    Qt::Object.connect(@start_capture, SIGNAL('clicked()'), @new_names, 
SLOT('start_capture()'))
    Qt::Object.connect(@stop_capture, SIGNAL('clicked()'), @new_names, 
SLOT('stop_capture()'))
    Qt::Object.connect(@replay, SIGNAL('clicked()'), @new_names, 
SLOT('replay()'))

    Qt::Object.connect(@change_dir, SIGNAL('clicked()'), @synchronizer, 
SLOT('change_dir()'))
    Qt::Object.connect(@reload, SIGNAL('clicked()'), @synchronizer, 
SLOT('reset_text()'))
    Qt::Object.connect(@rename, SIGNAL('clicked()'), @synchronizer, 
SLOT('rename_files()'))
    
    Qt::Object.connect(@synchronizer, SIGNAL('directory_changed(const 
QString&)'), @main, SLOT('setWindowTitle(const QString&)'))

    @main.window_title = "File Renamer - #{Dir.pwd}"
    @main.resize(900, 900)
    @main.show
    self
  end
end

if __FILE__ == $0
  Dir.chdir(ARGV[0]) if ARGV[0] && ARGV[0].length > 0
  RenamerGUI.new([]).exec
end

Attachment: ruby-qt4.tar.gz
Description: application/tar-gz

Reply via email to