Monday, January 27, 2014

Qt's Drag-and-Drop Architecture for Python and PyQt5
Pt 1, an Overview

In the following series of posts I will review the classes and methods needed to implement Drag-and-Drop functionality in a Qt5 program written in PyQt5.

The official documentation (for C++ of course) is found in this overview. It contains links to the reference pages for most of the the important classes, and it covers the basics for a C++ programmer. Doing the mental translation from C++ syntax to Python/PyQt syntax is a habit that the Python programmer needs to learn.

However, I found the official overview somewhat confusing. One problem is that it does not clearly distinguish the design of a drop target, a widget that receives dropped data, and the design of a drag source, a widget that recognizes a mouse drag motion and initiates a drag. These two are quite distinct. They are executed at different times and might be executed in different apps, with the drag beginning in one app and the drop ending in another. Thus you can have a drop without a drag and vice versa. They use different classes and require you to override different class methods. All told, they need separate treatment, which I will give them in this series of posts.

The User's View

The user thinks of drag-and-drop as a single smooth mouse operation: click down on something; move the mouse to something else; let go. During the drag the mouse cursor may change its appearance in some familiar way, perhaps acquiring a plus-sign to indicate a copy will happen or a slashed-circle to indicate that no dropping is allowed.

It is possible for the mouse to acquire a little thumbnail image of the thing being dragged, as a reminder. For example when dragging text in an editor, a translucent copy of the dragged text, or part of it, may follow the mouse cursor.

The user will often be dragging from one place in an application to another place in the same application: dragging a paragraph of text from one place in a document to another, for example; or dragging a list item to a different position in the same list.

But it may be that the user is dragging something from one application and dropping it into a completely different application: for example, dragging text from a Qt editor and dropping it on the host Desktop as a "clipping"; or dragging a URL from a browser window and dropping it into a Qt widget of some kind.

All in all, drag-and-drop is a simple, quick, familiar operation to the user—or should be. But making it happen at the level of program code turns out to be quite complicated.

The Program's View

To the program written in Qt (and specifically PyQt5: in these posts, "Qt" and "PyQt" are synonyms), it is actually not correct to speak of "drag-and-drop" as a single thing. There is drag, the initiating of a drag operation, and there is drop, the delivery of content to a target. These two use completely different classes and methods and are designed in isolation from each other.

Moreover, remember that the drag might start in a completely different application, so it arrives at your Qt code as an unheralded drop with data you didn't prepare. Or a drag that you initiate in your Qt code might end being dropped in some completely unrelated program.

The Qt drag-and-drop support is rather unhelpful in these cases of dragging between different applications. It only works fully as documented when the drag and drop are between widgets in the same application. I'll point out these issues as they come up.

In the next post we'll take a high-level look at drop target code.

Saturday, June 22, 2013

Prius Plug-In Hybrid: Some Numbers

In April 2012 we took delivery of a new Prius Plug-in Hybrid Vehicle (PHV), replacing a 2005 Prius. Now in mid-2013 I have enough history of utility and gasoline use to make a reasonable comparison of the relative costs of these vehicles.

Unfortunately for simple math, almost immediately after we got the PHV, we took off for six weeks in France. This mid-April to May gap messes up the pattern of utility bills and driving history. I had to choose two slightly mismatched periods of time for comparison: for the old vehicle, April 2011 through March 2012; and for the new one, June 2012 through May 2013.

This means that the electricity usage numbers are not precisely comparable. However, both periods span the darkest months of the year and also span one summer. So both include maximum lighting time and maximum home air conditioning. Nevertheless, all of these numbers should be taken as approximate.

Gasoline Usage

Twelve months of the 2005 Prius: 12048 miles, 251 gallons. That's 48mpg or, a more significant measure, 21 gallons per 1000 miles.

Twelve months of the 2012 PHV: 10790 miles, 170 gallons, yielding 63mpg or 15 gallons per 1000 miles.

Bottom line: given our usage patterns, the PHV saves us 6 gallons per 1000 miles driven. At current prices that's about $25 per Kmile, or given our normal 12K/year distance, about (ta-daa!) $275 per year in fuel costs.

Electricity Rates

Palo Alto Utilities charge for electricity usage based on Kilowatt-Hours (KwH) per month. Up to 10 KwH per day is called Tier 1, and charged at $0.09524 per KwH. Usage from 10 to 20 KwH is Tier 2, charged at $0.1302. Our latest bill, usage for May 2013, showed 487 KwH charged at $60.36. That is presumably based on

31 days * 10 KwH = 310 * 0.09524 =$28.5244
487 - 310 = 177 KwH * 0.1302 =$23.0454
total$51.5698
Actual bill$60.36

Hmmm. Think I need to have a talk with the Utilities...

Electricity Consumption

For the period May 2011 through April 2012 we consumed 5206 KwH, an average of 433.8 per month.

For the period July 2012 through June 2013 we consumed 6438 KwH, an average of 536.5 per month.

Thus the PHV seems to have added approximately 100 KwH to our electricity usage, or at Tier 2 rates, about $13 to our monthly electric bill.

The Bottom Line

Based on these somewhat approximate numbers, the PHV is saving us $275 per year in gasoline, while it is is costing us $156 in electricity. For a net saving of (ta-ta-daaaa!) $119 per year.

Since the PHV costs $7,800 more than a regular Prius ($32,000 versus $24,200 currently at toyota.com), it should pay for itself in only... 65 years. (Sad trombone: wah-wah-wah-waaa)

Side Issues

Our driving pattern includes a lot of short local trips. That's why we bought the PHV; its 11-mile battery distance means we often go several days without the gas engine coming on, and often the dashboard readout shows us getting over 100mpg well into a tankful. However, on longer trips and freeway driving the PHV does no better than a normal third-generation Prius, about 55mpg. Our battery-powered local hops pull the average up to 63, or about %15 better than a non-plug-in. But I've talked online with a PHV owner who does nothing but commute 6 miles each way, and is averaging over 160mpg.

I should also note that Palo Alto Utilities is pilot-testing a Time of Day Usage program. Under this scheme, electricity used between 11pm and 6am is discounted $0.019 per KwH. That would lower the cost of the PHV's 100 KwH (all Tier-2) from $0.1302 to $0.1112 for a saving of $2/month. Let's see: hiring an electrician to install some kind of timer in the outdoor outlet where we plug in the car would cost what, $250? So that would take even longer to earn out than the car itself.

Update: The PHV has a built-in charge timer! I can set it to charge itself only between 11pm and 6am. Thus we could realize the Time of Day discount without further expense. That would lower the electricity cost from $156 to $132 per year, increase the savings from $119 to $143 per year, and the PHV pays for itself in only 54 years! Yeah! I have registered for the TOD program but the pilot program is currently closed.

Saturday, January 12, 2013

Raspberry Pi, episode 1

Okey-dokey, I will repurpose this blog yet again to describe trying to use the Raspberry Pi that Paul gave me for Christmas.

There's this much one can say about the happy group of Brits behind the Pi: they are teaching many, many people how to spell Raspppberry.

The actual thing is basically a single-board Linux computer, remarkable mainly because of its tiny size: about the size of a playing card. Central on the board is a single chip that comprises 512MB of RAM, a 32-bit CPU, and a graphics processor. Pretty much everything else on the tiny board is either connectors or resistors and capacitors.

The Pi all hooked up. Clockwise from top right corner: Power via a micro-USB connector; the SD card; some experimenter I/O pins; a yellow RCA connector for an analog TV signal; an audio jack; some blinky lights; two USB plugs for keyboard and mouse; an ethernet cable; the HDMI cable leading to the digital TV.

When I stop to think about it the Pi can make me rather giddy. I remember configuring my first home computer, with separate, 10-inch-wide cards for a 16-bit CPU and for 64kilobytes of RAM. So here's one fingernail-size chip comprising a CPU and memory, both four orders of magnitude more capable. (64e3 versus 51.2e7, and as for the CPU, forget it. The Pi's graphics processor can do 64 gigaFLOPs. That old 2MHz Z80 isn't in the same galaxy.)

Anyway it needs a keyboard and mouse, so I went to Fry's and found an optical USB mouse for $3.99 and a USB keyboard for $5.99. Later I had to go back and get a 2-metre HDMI cable, which cost more than the other two combined.

Getting the OS

The big hurdle in getting the Pi going is loading its OS. The Pi's only provision for mass storage is a single SD card, 8GB or larger. (I try not to think too much about having 8 or 16 gigabytes of memory on a thumbnail-sized chip.) If the SD card is initialized as a FAT-32 file system and loaded with a disk image downloaded from the Raspberry mothership, the Pi will supposedly boot up from it into a full-blown Linux system.

That's what the quick start guide claims. But the problem is getting the disk image written onto the SD card. The Pi instructions are for Windows users; my tool is a Macbook. Nevertheless, I thought I'd done it correctly, following these instructions at the "Embedded Linux Wiki".

It didn't work; or at least, when the Pi was all hooked up and plugged in, the screen went blank.

So I did it again and this time, the system booted up into a typical Linux startup screen going to a config screen.

It's aliiiiive! The Pi with the $4 mouse, $6 keyboard, and an expensive Samsung TV doing duty as a monitor.

I config'd it a bit, setting the locale and the time zone, although the latter was a bit of a puzzle: is California in Alaskan or Aleutian or Pacific time?

A Desktop!

I thoughtlessly told the configurator to update itself, but no ethernet was plugged in, so that hung. So I popped the power in and out and this time when it came up, it was in a graphical desktop!

The desktop after playing around a bit: terminal, Midori browser, debian doc open.

Fall Down Go Boom

I played with the desktop for a bit. Impressive that the Pi had instantly found the LAN and the browser could access the web without problem. I started apt-get update to update the database of installed software, and after a bit, everything hung solid. The mouse pointer still tracked but nothing responded to it.

So after a few minutes I popped the power plug out and in and when it booted now, it said PANIC.

Oh dear oh dear oh dear...

One More Time Unto the SD Card

I put the SD card in my Macbook and reformatted it and copied the image to it again. Checked that the Pi would boot and started through the configuration screens again.

After setting the timezone, there was a long pause. Then a series of file-system messages about inodes, including "This should not happen!" and "Data will be lost" and other stuff. Not helpful messages. In no way suggestive of what to do or how to recover.

Oh dear, some more. The Life Of Pi is finished, it seems.

And then it didn't respond to anything.

Anyone want a Pi?

I conclude that the Raspberry Pi is indeed a Linux platform of amazingly small dimensions. Loaded up with dedicated device-control software it can no doubt do yoeman duty as a lump of embedded smarts. People are doing amazing things with it.

However it is not a toy. It ain't for kids, at least, for kids who aren't ready to type sudo apt-get update without thinking about it. It needs constant hand-holding by an experienced user of command-lines, and that user better have a lot of patience. The software, combined with the iffy hardware qualities of SD card mass storage, is just not reliable. Or at least, this one example was not. The user experience, even for a very knowledgeable techie, is full of frustration. For a non-nerd, it would be hopeless.

Wednesday, April 20, 2011

Changing from DirecTivo to the DirecTV HR24 DVR

For a long time I put off the switch to HD television because doing so would mean giving up our cherished, 8-year-old Series 2 DirecTivo unit. The HD digital video recorder from DirecTV does not use Tivo's patented and deservedly popular user interface.

Finally the wait got too exasperating and we made the switch-over: a new, beautiful Samsung 46C8000 TV, a new A/V receiver, and the DirecTV HR24 DVR. (I ordered the HR24 from Amazon to be sure of getting that model, and not the slower HR23.) This is a summary of the main differences between these DVRs, as seen by a long-time Tivo user. The bottom line is: it's fine, no big problems.

This is a comparison of the standard Tivo user experience, to the DirecTV HR24 experience, as seen by a long-time user of the Series 2 DirecTivo.

The Box

As a physical box the HR24 is much less conspicuous than the DirecTivo. It is smaller in all dimensions, and lighter. Its case is midnight black with a subtle blue-glowing icon. Its primary output is HDMI, a big forward jump compared to the DirecTivo which lacked HDMI.

The Remote

The remote is not as comfortable or intuitive as the Tivo "Peanut." On the other hand, it isn't possible to pick it up and try to use it upside down, as my wife sometimes did with the peanut.

A significant feature that is new to Tivo users: the HR24 has an "off" mode. The DirecTivo was never turned off; it was always producing audio and video output unless you went through several menu levels to put it in Standby mode.

The HR24 remote has ON and OFF buttons. When you turn it off, the HR24 clears out of any menus, cancels any paused recording, shuts off its video and audio outputs and dims its front panel. Obviously it remains "on" internally because it makes timed recordings. The ON/OFF buttons can be programmed to turn your TV on/off as well, so that the TV and DVR come on together. You can also program the remote to operate a separate A/V receiver, but not with a single button-press.

Something else different: when the HR24 has been paused for more than a minute, it goes to a screen-saver mode in which a DirecTV icon wanders around a black screen. The Tivo did not do this; it would sit on a paused image indefinitely.

You can get deep into nested menus with the HR24 interface. However the remote has a dedicated BACK button for backing out of all menus in reverse sequence.

An Interface for the 80s

The first thing the Tivo user notices on meeting the HR24 on-screen user interface, is the cosmetic differences. The Tivo interface used calm, deep colors and had a polished look created by rounded corners, drop-shadows, aliased fonts, and subtle color gradients.

The look of the HR24 interface is anything but subtle and far from polished. It uses garish light-blue, black, yellow and orange colors. Every menu and window is flat and hard-edged: there's not a gradient or drop-shadow anywhere. The only concession to appearance is crudely pixillated rounded corners on some rectangular elements. Fonts, too, are unaliased and chunky. It's a surprise that an HD DVR, which will always be used with a 1920x1080, 24-bit screen, still uses interface elements that would look natural on a 16-color, 640x480 DOS screen of 1982! Twenty-five years of interface design progress ignored.

Cosmetics aside however, the functions that you command through this retro UI are in almost all cases the equal or better than Tivo's.

Playing Recorded Shows

With the Tivo, I most often went to the What's Playing List. Tivo provided a List button to get there, and so does the HR24. When scrolling through this or any other list, you can use Channel up/down to scroll by pages, just as with Tivo. Select a recorded program and press the Play button to start playing it, or hit the Red button to delete it.

A nice feature of the HR24 is a display of disk capacity used. Tivo lacked this feature.

Watching a live or recorded show, the controls are basically the same as Tivo. Play, pause, fast forward, rewind and jump back a few seconds are all similar. The FF button has four speeds, not three. Compared to Tivo the third speed is too slow and the fourth is uncomfortably fast. When you stop fast-forwarding, the HR24 jumps back a bit to compensate for reaction time, but it jumps further than Tivo did, and less predictably.

Tivo made single-frame advance and slo-mo quite easy. It is possible to engage slo-mo with the HR24; I have not found how to do frame-advance. On the other hand, the HR24 lets you set "bookmark" points in a recorded show so you can easily skip back to replay a favorite scene.

One Tivo play feature not easily replaced is Tivo's go-to-end button. On the HR24, the jump-ahead button doesn't go to the end. When playing at normal speed, it skips 30 seconds. If you are at any FF speed it skips 15 minutes. You can force a recording to its end (or to the real-time point if it is still recording) as follows: start FF at any speed, then press the skip-ahead button enough times to bring the recording to the end.

On Tivo, you could bail out of a program with the two-button sequence go-to-end, List. This was a quick way to bring up the Delete yes/no dialog. On the HR24, the same result is obtained with FF then skip (skip, skip...) to the end, and wait a few seconds until the delete dialog appears.

On DirecTivo, deleting a program moved it to a special folder from which you could recover it, sometimes days later. There is no such "trash can" folder on the HR24. A deleted program is gone immediately and forever.

Upcoming from Info

The HR24 has a dedicated INFO button that you can use to get info about a program at any time: while in a list of recorded programs, or while playing a recorded or a live program, or while browsing the on-screen guide. The displayed info includes a "first aired" date, useful for knowing when a program is a rerun.

Something that I always wanted in the Tivo interface was the ability to move easily from info about a recorded show, to a list of upcoming episodes of that show or to the Season pass manager. The HR24 provides this. The info panel brought up by the INFO button has a short menu of things to do, including finding upcoming episodes of the same show, finding other shows with the same performers, setting up to record the series, or going to the series options if the series is already being recorded. Again, you can do this while playing a program, or from the guide, or from search results, or the list of recorded programs. It's very handy.

Search

The search function of the HR24 is more accessible, more elaborate, and more useful than Tivo's Wishlists. You enter text in a similar way, by navigating a matrix of characters. But as soon as you begin entering text, search results begin to appear -- as with a Google search. The suggested results are quite good. For many searches, as few as two characters will produce the thing you want in the list of tentative results.

The most common search is for the name of a show, but you can also search a channel name (e.g. ESPN) to get a list of shows on that channel; or search a person's name and see all shows in which that person appears; or search a category (e.g. REALITY) and see all shows in that category. Of you can do a keyword search, finding every program whose listing contains that keyword. Once you have a list of shows by any means, you can browse in it, and hit Record or INFO on any one. You can also set "Auto-record" to record all shows that match a particular search, similar to Tivo's auto-recorded wishlists.

The last 15 searches you've done are available in a list so you can easily repeat them. The search also supports Boolean AND, OR and NOT functions and other special keywords. These are not documented in the user manual; you learn about them in online forums.

Performance

On internet forums there were many complaints about sluggish response of the previous DirecTV DVR, the HR23. The HR24 is apparently much faster. It is as responsive as the Series 2 Tivo in almost everything. The only place I have noticed any sluggishness is in the response to the RECORD button. Sometimes it takes a couple of seconds to respond to this button. That's awkward because, if you press the button twice, you have requested recording the entire series of that program. So you learn to press once, firmly, then wait for the ® icon to appear.

Missing Suggestions

The only major Tivo function that the HR24 lacks is Suggestions. I miss my old weekly exercise of sitting down to go through 80 to 100 programs in the Suggestions folder, deleting the majority but intrigued by some. Suggestions should be easy to implement; the algorithm would be just like Amazon's "Customers who bought this also bought X" feature. DirecTV could easily tell my DVR, "Customers who record the programs you do, also record these other programs, grab a few if you have space." I don't see why they don't do this.

No Regrets

I put off converting to HD and the DirecTV DVR for a long time out of reluctance to give up the Tivo user interface. Now that I've done it, the new environment is quite comfortable and usable. If a high-def DirecTivo ever does appear, I won't be in any rush to change back to it.

Monday, November 29, 2010

Nice 40-mile ride

I had a plan for quite a ride today, starting at 7am. Recently I had my annual physical, and the doctor ordered some blood tests, for which I have to go to the clinic 5 miles away "npo," fasting. Also I wanted to deliver something to Campbell, 19 miles away. So the plan was to start at 7, give the blood, have breakfast, and ride on to Campbell.

Well, the temperature at 7am was 35f, which made me rethink. Come to find out Marian needed to have a blood sample taken also, so we drove together to the clinic, gave up our red cells, and drove home for breakfast. When a reasonable (55f) temperature arrived at 9am, I set out on the ride to Campbell.

I had charged up the BionX battery beforehand, and I am happy to say that a ride of 40 miles (well, alright, 39.6 miles) used up only 5 of the 8 bars in the battery-state indicator. That's using assist level 2 about half the time, and level 1 the other half. There's plenty of juice for the 20-mile round trip to the gym on Wednesday. I think. We'll see.

This was a ride along familiar roads: Foothill Expressway, Homestead, Stelling, Prospect, Campbell.The streets, even busy ones like Campbell Avenue, were very quiet. There were many colorful Maple and Pistache trees along the streets. The sky was brilliantly clear; it was one of those winter days when cold Pacific air blows in from the Gulf of Alaska and makes every detail of the Bay Area hills stand out in glittering detail. Here is a view from Cupertino, east toward the Mt. Hamilton Observatory which is over 30 miles (50k) away.

Here are the exact pixels from the middle of that picture.

If I had started riding toward those so-clear little domes, it would have taken four hours to reach them, or 90 minutes to drive it.

I had stopped to take pictures of the Mary Avenue Bike Bridge. This new bridge was built in 2009 to give easy foot and bike access over I-280. It's a very nice cable-stayed bridge. Today I noticed the sun eclipsed by the north pylon:

At the south end of the bridge is a charming bronze sculpture of california quails, overlooked by an ominous red-tailed hawk.

Thursday, November 25, 2010

Longer Legs - maybe

It has been cold. Well, cold for California. Chilly, damp, gray mornings, or the last couple of days, clear frosty mornings. I don't like to ride in the cold. I have a pair of leggings that I bought 15 years ago. I wore them often when I was commuting by bike, but haven't had them on in two years at least. This week I've been jogging, either on the street or driving to the gym and jogging on the treadmill, which is terminally boring even with an iPod loaded with podcasts (Skeptic's Guide, Geologic, Security Now ... my jogging companions.)

But the bike—when I finally get back on it—should be better. Last week when the rain started, I did what I said I'd do the first rainy spell. I took the back wheel off the bike (no easy job) and took it up to the local bike shop service department along with the 11-32 freehub.

Wait. The back story is that when I was restoring the bike, I assumed that when I motorized it, it would still be using the same rear cluster, so I ordered the new front chain rings in about 10% smaller sizes than the old. But then it turned out that the BionX hub only supports a freewheel not a modern freehub, and further, that the freewheel BionX supplied was an 8-speed with a smallest gear of 14 teeth.

I described this back in June, noting that I had managed to obtain a freewheel with 11-32 gearing. But I hadn't installed it.

The reason I didn't install it is that I couldn't. I tried at one point to get the existing freewheel off, but they are notoriously difficult and I couldn't manage it. (The freewheel threads onto the hub, or in this case onto the motor casing, in such a direction that your pedaling torque tightens it.) Then I took the bike back to Joe Robinson for another reason and asked him to try to replace it. He returned it saying he had not been able to get the thing off either.

There matters rested for a couple of months, but the setup wasn't satisfactory; on a downhill I couldn't spin past 30mph, maybe 31. I used to be able to pedal past 40mph on a suitable slope.

So, OK, on this rainy day I removed the wheel and took it and the new freewheel up to the bike store and at the service counter I started saying, I want to replace this, the guy who sold it to me couldn't get it off. Before I'd finished that sentence, the mechanic, without a word, picked up the wheel and walked away to the back of the shop, where he slapped a tool into a vise, dropped the wheel on it, gave a mighty heave, and spun the freewheel off. He was back at the counter in less than a minute. He dabbed a little grease onto the threads, spun the new freewheel into place, and said, "OK?"

OK, indeed. I took it home and reinstalled the wheel, and the bike's been sitting in the garage since, waiting for better weather or more motivation.

Saturday, November 13, 2010

New grip shifts

I had wanted to replace the old shifters during the restoration. I bought a left-right pair of eight-speed Grip Shifts from Easy Racers when I picked up the frame. I forget the reason just now, but due to some issue, I retained the old left-hand (front) shifter but installed the right-side one.

Not too long after I started riding the new setup, that right-hand shift knob started to slip. Under the pressure of my awesome right hand, its rubber grip began to slip. It would pull away from the mechanism and I'd push it back in place.

This became increasingly annoying so I decided to replace it yet again. I went to local bike stores and looked at the shifters being used on modern mountain bikes. Some wouldn't work for my bike, for example because the shifter was integrated with the brake lever, and I need to keep my separate brake levers. Others looked rather cheap. Actually the nicest-looking grip shift is the one made by Shimano as part of their Nexus system. Unfortunately, the Nexus is an internally-geared hub. If I were setting up a new, non-electric, recumbent, the Nexus hub and related parts would make a very nice installation. But the center of my rear wheel is taken up by the BionX motor, and it seemed very likely that the Nexus shifter would not be compatible with my external 8-speed derailleur.

What I settled on was the SRAM "centera" twist-grip. It has a longer grip that looks well-bonded to the mechanism. It also has a somewhat toy-like gear indicator but otherwise its quality seems good for a plastic part.

I ordered a pair of shifters from Amazon; they arrived yesterday and I installed them this morning. Here is the right one in place,

...and here is the left one.

Happily the installation lacked drama. Installing cables often results in operatic drama and high-pitched swearing fits, but not this time. The nearest it came to that was when I was trying to fit the old, longer cable to the rear. The one that came with the new shifter was juuuuuust a few inches too short.

This meant threading the old cable tip down through the core of the shifter. And of course, one wire didn't want to go, wanted to spiral back and jam. I fired up my old soldering iron and soldered the tip to hold the wires together. Then it went. And the adjustment wasn't hard either, and it shifts smoothly: tick-clunk, tick-clunk.

Of course the indicator reads "backward" with 8 being the lowest gear and 1, the highest. But that's always been true because the derailleur works backward, pulling toward the higher gears.