File: toolbars_state.rb

package info (click to toggle)
psi 0.14-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 12,704 kB
  • ctags: 22,303
  • sloc: cpp: 150,140; ansic: 26,319; xml: 1,215; makefile: 236; python: 178; ruby: 129; sh: 27
file content (135 lines) | stat: -rwxr-xr-x 3,167 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env ruby -wKU

require 'base64'
require 'iconv'

raise "Pass toolbars-state data as argument." if ARGV.length != 1
data = Base64.decode64(ARGV[0])

$toolBarStateMarker    = 0xfe
$toolBarStateMarkerEx  = 0xfc
$dockWidgetStateMarker = 0xfd

class MainWinState
  def initialize(data)
    @data = data
    @offset = 0
  end

  def unpack(format, byte_length)
    result = @data.unpack("@#{@offset}#{format}")
    @offset += byte_length
    return result
  end

  def readQString
    bytes = unpack("N", 4)[0]
    return "" if bytes == 0xffffffff
    len = bytes / 2
    Iconv.open('UTF-8', 'UCS-2BE') do |cd|
      result = cd.iconv(@data[@offset, bytes])
      @offset += bytes
      return result
    end
  end

  def toSigned(num)
    length = 32
    mid = 2**(length-1) 
    max_unsigned = 2**length 
    (num >= mid) ? num - max_unsigned : num
  end

  def unpackRect(geom0, geom1)
    floating = geom0 & 1
    x = y = w = h = 0

    if floating != 0
      geom0 >>= 1;

      x = (geom0 & 0x0000ffff) - 0x7FFF
      y = (geom1 & 0x0000ffff) - 0x7FFF

      geom0 >>= 16;
      geom1 >>= 16;

      w = geom0 & 0x0000ffff;
      h = geom1 & 0x0000ffff;
    end

    return "(x:#{x} y:#{y} w:#{w} h:#{h} floating:#{floating != 0 ? 'true' : 'false'})"
  end

  def toolBarAreaName(pos)
    case pos
      when 0: "LeftDock"
      when 1: "RightDock"
      when 2: "TopDock"
      when 3: "BottomDock"
    end
  end

  def toolBarAreaLayoutRestoreState(tmarker)
    lines = unpack("N", 4)[0]
    (0...lines).each do |j|
      pos = unpack("N", 4)[0]
      cnt = unpack("N", 4)[0]
      (0...cnt).each do |k|
        objectName = readQString
        shown      = unpack("C", 1)[0]
        item_pos   = unpack("N", 4)[0]
        item_size  = unpack("N", 4)[0]
        geom0      = unpack("N", 4)[0]
        rect       = ""

        if tmarker == $toolBarStateMarkerEx
          geom1 = unpack("N", 4)[0]
          rect = unpackRect(geom0, geom1)
        end
        puts "#{objectName}"
        puts "\tpos:       #{toolBarAreaName(pos)}"
        puts "\tshown:     #{shown != 0 ? 'true' : 'false'}"
        puts "\titem_pos:  #{item_pos}"
        puts "\titem_size: #{item_size}"
        puts "\trect:      #{rect}"
      end
    end
  end

  def dockAreaLayoutRestoreState
    puts "dockAreaLayoutRestoreState:"
    cnt = unpack("N", 4)[0]
    (0...cnt).each do |i|
      raise "TODO"
    end

    width, height = unpack("NN", 8)
    puts "\tcentralWidgetRect = #{width} x #{height}"

    (0...4).each do |i|
      puts "\tcornerData[#{i}] = #{unpack('N', 4)}"
    end
  end

  def restoreState
    marker, version = unpack("NN", 8)
    raise "Incompatible format: #{marker}, #{version}" if marker != 0xff or version != 0

    while true do
      marker = unpack("C", 1)[0]

      case marker
      when $toolBarStateMarker, $toolBarStateMarkerEx
        # puts "toolBarAreaLayoutRestoreState"
        toolBarAreaLayoutRestoreState(marker)
      when $dockWidgetStateMarker
        # puts "dockAreaLayoutRestoreState"
        dockAreaLayoutRestoreState
      end

      break if @offset >= @data.length
    end
  end
end

MainWinState.new(data).restoreState