| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::Qt - AnyEvent adaptor for Qt | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use Qt; | ||
| 9 | |||
| 10 | my $app = Qt::Application \@ARGV; # REQUIRED! | ||
| 11 | |||
| 12 | # this module gets loaded automatically as required | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This module provides transparent support for AnyEvent. You don't have | ||
| 17 | to do anything to make Qt work with AnyEvent except by loading Qt | ||
| 18 | before creating the first AnyEvent watcher I<and instantiating the | ||
| 19 | Qt::Application object>. Failure to do so will result in segfaults, | ||
| 20 | which is why this model doesn't work as a default model and will not be | ||
| 21 | autoprobed (but it will be autodetected when the main program uses Qt). | ||
| 22 | |||
| 23 | Qt suffers from the same limitations as Event::Lib and Tk, the workaround | ||
| 24 | is also the same (duplicating file descriptors). | ||
| 25 | |||
| 26 | Qt doesn't support idle events, so they are being emulated. | ||
| 27 | |||
| 28 | Avoid Qt if you can. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | package AnyEvent::Impl::Qt::Io; | ||
| 33 | |||
| 34 | use Qt; | ||
| 35 | use Qt::isa qw(Qt::SocketNotifier); # Socket? what where they smoking | ||
| 36 | use Qt::slots cb => []; | ||
| 37 | |||
| 38 | sub NEW { | ||
| 39 | my ($class, $fh, $mode, $cb) = @_; | ||
| 40 | shift->SUPER::NEW (fileno $fh, $mode); | ||
| 41 | this->{fh} = $fh; | ||
| 42 | this->{cb} = $cb; | ||
| 43 | this->connect (this, SIGNAL "activated(int)", SLOT "cb()"); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub cb { | ||
| 47 | this->setEnabled (0); # required according to the docs. heavy smoking required. | ||
| 48 | this->{cb}->(); | ||
| 49 | this->setEnabled (1); | ||
| 50 | } | ||
| 51 | |||
| 52 | package AnyEvent::Impl::Qt::Timer; | ||
| 53 | |||
| 54 | use Qt; | ||
| 55 | use Qt::isa qw(Qt::Timer); | ||
| 56 | use Qt::slots cb => []; | ||
| 57 | |||
| 58 | # having to go through these contortions just to get a timer event is | ||
| 59 | # considered an advantage over other gui toolkits how? | ||
| 60 | |||
| 61 | sub NEW { | ||
| 62 | my ($class, $after, $interval, $cb) = @_; | ||
| 63 | shift->SUPER::NEW (); | ||
| 64 | this->{interval} = $interval; | ||
| 65 | this->{cb} = $cb; | ||
| 66 | this->connect (this, SIGNAL "timeout()", SLOT "cb()"); | ||
| 67 | this->start ($after, 1); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub cb { | ||
| 71 | this->start (this->{interval}, 1) if defined this->{interval}; | ||
| 72 | this->{cb}->(); | ||
| 73 | } | ||
| 74 | |||
| 75 | package AnyEvent::Impl::Qt; | ||
| 76 | |||
| 77 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 78 | use Qt; | ||
| 79 | |||
| 80 | use AnyEvent::Impl::Qt::Timer; | ||
| 81 | use AnyEvent::Impl::Qt::Io; | ||
| 82 | |||
| 83 | our $app = Qt::Application \@ARGV; # REQUIRED! | ||
| 84 | sub io { | ||
| 85 | my ($class, %arg) = @_; | ||
| 86 | |||
| 87 | # work around these bugs in Qt: | ||
| 88 | # - adding a callback might destroy other callbacks | ||
| 89 | # - only one callback per fd/poll combination | ||
| 90 | my ($fh, $qt) = AnyEvent::_dupfh $arg{poll}, $arg{fh}, | ||
| 91 | Qt::SocketNotifier::Read (), Qt::SocketNotifier::Write (); | ||
| 92 | |||
| 93 | AnyEvent::Impl::Qt::Io $fh, $qt, $arg{cb} | ||
| 94 | } | ||
| 95 | |||
| 96 | sub timer { | ||
| 97 | my ($class, %arg) = @_; | ||
| 98 | |||
| 99 | # old Qt treats 0 timeout as "idle" | ||
| 100 | AnyEvent::Impl::Qt::Timer | ||
| 101 | $arg{after} * 1000 || 1, | ||
| 102 | $arg{interval} ? $arg{interval} * 1000 || 1 : undef, | ||
| 103 | $arg{cb} | ||
| 104 | } | ||
| 105 | |||
| 106 | # newer Qt have no idle mode for timers anymore... | ||
| 107 | #sub idle { | ||
| 108 | # my ($class, %arg) = @_; | ||
| 109 | # | ||
| 110 | # AnyEvent::Impl::Qt::Timer 0, 0, $arg{cb} | ||
| 111 | #} | ||
| 112 | |||
| 113 | sub one_event { | ||
| 114 | Qt::app->processOneEvent; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub loop { | ||
| 118 | Qt::app->exec; | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | |||
| 123 | =head1 SEE ALSO | ||
| 124 | |||
| 125 | L<AnyEvent>, L<Qt>. | ||
| 126 | |||
| 127 | =head1 AUTHOR | ||
| 128 | |||
| 129 | Marc Lehmann <schmorp@schmorp.de> | ||
| 130 | http://home.schmorp.de/ | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 |