In the graphics-dojo's example "drag move charm", we can move the window not only with grabbing title bar, but also with grabbing top level widget. I learn this example and start to write by QtRuby like below:
require 'Qt4'
class DragMoveData
attr_accessor :moving, :start_drag
alias :moving? :moving
end
class DragMoveCharm < Qt::Object
def initialize parent = nil
super
@drag_move_data = {}
end
def activate_on widget
unless @drag_move_data.include?(widget)
data = DragMoveData.new
data.start_drag = Qt::Point.new(0, 0)
data.moving = false
@drag_move_data[widget] = data
widget.install_event_filter(self)
end
end
def deactivate_from widget
@drag_move_data.delete(widget)
widget.remove_event_filter(self)
end
protected
def event_filter(widget, event)
consumed = widget.is_a?(Qt::Widget)
if consumed
type = event.type
case(type)
when Qt::Event::MouseButtonPress,
Qt::Event::MouseButtonRelease,
Qt::Event::MouseMove
consumed = event.kind_of?(Qt::MouseEvent) &&
event.modifiers == Qt::NoModifier &&
@drag_move_data.include?(widget)
if consumed
data = @drag_move_data[widget]
case(type)
when Qt::Event::MouseButtonPress
consumed = event.button == Qt::LeftButton
if consumed
data.start_drag = event.global_pos
data.moving = true
end
when Qt::Event::MouseButtonRelease
data.start_drag = Qt::Point.new(0, 0)
data.moving = false
consumed = false
when Qt::Event::MouseMove
consumed = data.moving?
if consumed
pos = event.global_pos
widget.move(widget.pos + pos - data.start_drag)
data.start_drag = pos;
end
end
end
else
consumed = false
end
end
consumed
end
alias :eventFilter :event_filter
end
and main codes to execute above classes:
require 'dragmovecharm'
require 'qtwebkit'
def image_show
Qt::Label.new do
self.text = "<img src='#{::File.dirname($PROGRAM_NAME)}/pudding.jpg'>"
self.adjust_size
self.window_title = 'Drag to move around'
show
end
end
def mini_browser
Qt::Dialog.new do
self.layout = Qt::VBoxLayout.new do
self.set_contents_margins(3, 15, 3, 3)
end
self.window_flags = Qt::FramelessWindowHint
self.size_grip_enabled = true
self.window_title = 'Drag to move around'
show
tab = Qt::TabWidget.new(self) do
search = Qt::WebView.new(self)
search.load(Qt::Url.new('http://www.google.com/m?hl=en'))
search.set_focus
self.add_tab(search, 'Search')
news = Qt::WebView.new(self)
news.load(Qt::Url.new('http://www.google.com/m/news?source=mobileproducts'))
self.add_tab(news, 'News')
bbc = Qt::WebView.new(self)
bbc.load(Qt::Url.new('http://news.bbc.co.uk/text_only.stm'))
self.add_tab(bbc, 'BBC')
fb = Qt::WebView.new(self)
fb.load(Qt::Url.new('http://iphone.facebook.com'))
self.add_tab(fb, 'Facebook')
end
self.layout.add_widget(tab)
self.resize(350, 500)
(tab.document_mode = true) if Qt::version >= '4.5'
end
end
Qt::Application.new(ARGV) do
charm = DragMoveCharm.new
charm.activate_on(image_show)
charm.activate_on(mini_browser)
exec
end
Run these codes, 2 charms(widgets) is shown successfully.
But, these can only be moved with grabbing title bar. These yet cannot be moved with grabbing top level widget. to be continued...
No comments:
Post a Comment