Crafting Qt Web Runtime Widgets

It’s amazing how simple it is to create desktop widgets with Qt Web Runtime. I was able to create this “flip clock” widget in less then hour and most of that time I spent tweaking the background image pixels in Pixelmator :)

The code behind this widget is only around 20 lines of HTML, 20 lines of JavaScript, 35 lines of CSS and one config file.

In HTML I only create divs for hours, minutes and date. In JavaScript I refresh the view to show current time and date and start the timer which updates the values in every 60s. Layout is done with a background PNG image and hours, minutes and date DIVs are placed on top of the image in CSS with position:fixed styling.

index.html


...

  
00
00
-

JavaScript (script.js)

function refresh() {
	var time = new Date();
	var hours = time.getHours();
	if(hours<10) {
		hours = "0" + hours;
	}
	var minutes = time.getMinutes();
	if(minutes<10){
		minuts = "0" + minutes;
	}
	$("#hours").text(hours);
	$("#minutes").text(minutes);

	var day = time.getDay();
	var month = time.getMonth();
	var weekday = ["Sunday", "Monday", 
"Tuesday", "Wednesday", 
"Thursday", "Friday", 
"Saturday"];
	var months = [
"Jan", "Feb", "Mar", 
"Apr", "May", "June", 
"July", "Aug", "Sep", 
"Oct", "Nov", "Dec"];	

	$("#date").text(weekday[day] + ", " + time.getDate() + " " + months[month]);
}

You can download the source code in ZIP here. You can install the widget directly to your Nokia N900 here.

Analog clock service for web apps

I’m nowadays working in an international company which has offices in multiple countries. I was looking for a way to get our Confluence, Enterprise Wiki, to display current time in all of our offices in one page. I didn’t find any simple solutions as lots of services just provided me a chunk of HTML that I could add to my page to create a dynamic clock but wiki syntax doesn’t really let me do that.

I’m a great fan of Google Charts API. Developers at Google have developed a really nice URL based API for the charting purposes. This was inspiration for me when I created Clock Service API for my own needs.

Clock Service is a simple servlet implementation that generates an image with the specified parameters. API can be used to define image size (s=100), timezone (tz=zone), title (t=Helsinki) and 12h/24h format (f=24). With this I can use the following URLs to produce three different clock images for three different timezones.

/clockservice/clock?tz=America/New_York&s=150&t=Atlanta&f=12
/clockservice/clock?tz=Europe/Helsinki&s=150&t=Helsinki&f=12
/clockservice/clock?tz=Australia/Sydney&s=150&t=Sydney&f=12

If you use those URLs in IMG tags then the result would look like this:

Clock (Atlanta)Clock (Helsinki)Clock (Sydney)

The images above aren’t created dynamically as I don’t have any public servlet container available.

The code behind the service is really quite simple. There are only two classes ClockServlet and Clock. Servlet implementation is only used for parsing the parameters from the request and writing the bytes from Clock.getImageData(..) to the web response.

Timezones are handled using the Java’s Calendar and TimeZone classes like this:

  1. TimeZone tz = TimeZone.getTimeZone("Europe/Helsinki");
  2. Calendar cal = Calendar.getInstance(tz);
  3. int hour = cal.get(Calendar.HOUR);
  4. int minute = cal.get(Calendar.MINUTE);

You can find the source code and compiled WAR package from the application page, here. Application is distributed under the Apache License v2.0.