blib/lib/AnyEvent/Impl/Qt.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 92 CC 3 SCAR 10.2
CC
3
Cov
0%
cb
6.0
timer
6.0
NEW
2.0
CRAP 2.8
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  
85 sub io {
86 my ($class, %arg) = @_;
87  
88 # work around these bugs in Qt:
89 # - adding a callback might destroy other callbacks
90 # - only one callback per fd/poll combination
91 my ($fh, $qt) = AnyEvent::_dupfh $arg{poll}, $arg{fh},
92 Qt::SocketNotifier::Read (), Qt::SocketNotifier::Write ();
93  
94 AnyEvent::Impl::Qt::Io $fh, $qt, $arg{cb}
95 }
96  
97 sub timer {
98 my ($class, %arg) = @_;
99
100 # old Qt treats 0 timeout as "idle"
101 AnyEvent::Impl::Qt::Timer
102 $arg{after} * 1000 || 1,
103 $arg{interval} ? $arg{interval} * 1000 || 1 : undef,
104 $arg{cb}
105 }
106  
107 # newer Qt have no idle mode for timers anymore...
108 #sub idle {
109 # my ($class, %arg) = @_;
110 #
111 # AnyEvent::Impl::Qt::Timer 0, 0, $arg{cb}
112 #}
113  
114 #sub loop {
115 # Qt::app->exec;
116 #}
117  
118 sub _poll {
119 Qt::app->processOneEvent;
120 }
121  
122 sub AnyEvent::CondVar::Base::_wait {
123 Qt::app->processOneEvent until exists $_[0]{_ae_sent};
124 }
125  
126 =head1 SEE ALSO
127  
128 L<AnyEvent>, L<Qt>.
129  
130 =head1 AUTHOR
131  
132 Marc Lehmann <schmorp@schmorp.de>
133 http://anyevent.schmorp.de
134  
135 =cut
136  
137 1
138