Saturday, April 25, 2009

Qt4 Line Graph

Just back from Bike Trip, It's always inspiring... In the last minutes, I've written a quick Qt Line Graph demo that maybe I'll use in the my next Project, if I find a bit of time.

The code is far (far, far, far...) from perfect but is enough for a Test/Demo :)

The Source code is available here: Qt4 Line Graph Source Code.

Friday, April 24, 2009

C# Oracle for Beginners

In the last two days, at work I had a couple of hours to play with Oracle.
I've made a simple class to Wrap the .NET OracleConnection and here there's a simple example with Connection/Disconnection a couple of CREATE TABLE, INSERT a STORED PROCEDURE and a FUNCTION a couple of SELECT to show JOIN and other operations.

OracleDatabase db = new OracleDatabase();
db.Connect("", "system", "password");

// Create And Populate Tables
db.ExecuteNonQuery("CREATE TABLE tb1 (a NUMBER NOT NULL, b CHAR(20))");
db.ExecuteNonQuery("INSERT INTO tb1 (a, b) VALUES (10, 'Matteo')");
db.ExecuteNonQuery("INSERT INTO tb1 (a, b) VALUES (20, 'Mauro')");

db.ExecuteNonQuery("CREATE TABLE tb2 (a NUMBER NOT NULL, b CHAR(20))");
db.ExecuteNonQuery("INSERT INTO tb2 (a, b) VALUES (10, 'San Francisco')");
db.ExecuteNonQuery("INSERT INTO tb2 (a, b) VALUES (20, 'Cuppertino')");

// A Store Procedure cannot Return Value
StringBuilder sp1 = new StringBuilder();
sp1.Append("CREATE OR REPLACE PROCEDURE tb2sp1 ");
sp1.Append("IS ");
sp1.Append("BEGIN ");
sp1.Append("INSERT INTO tb1 (a, b) VALUES (30, 'SP1 TEST'); ");
sp1.Append("END; ");

// A Function can Return a Value
StringBuilder sp2 = new StringBuilder();
sp2.Append("CREATE OR REPLACE function tb2sp2 (idx NUMBER) ");
sp2.Append(" RETURN NUMBER ");
sp2.Append("IS ");
sp2.Append(" var_count NUMBER; ");
sp2.Append("BEGIN ");
sp2.Append(" SELECT COUNT(*) INTO var_count FROM tb1; ");
sp2.Append(" return (var_count + idx); ");
sp2.Append("END; ");

List<OracleDatabase.DataRecord> r1 = db.Execute("select * from tb1");
List<OracleDatabase.DataRecord> r2 = db.Execute("select a, b, (a * 2 + 1) from tb1");
List<OracleDatabase.DataRecord> r3 = db.Execute("select tb1.a as ID, tb1.b as NAME,
tb2.b as PLACE from tb1 INNER JOIN tb2 ON tb1.a = tb2.a");
List<OracleDatabase.DataRecord> r4 = db.Execute("select * from tb1");
object sp2RetVal = db.ExecuteFunction("tb2sp2(10)");

db.ExecuteNonQuery("DROP PROCEDURE tb2sp1");
db.ExecuteNonQuery("DROP FUNCTION tb2sp2");
db.ExecuteNonQuery("DROP TABLE tb2");
db.ExecuteNonQuery("DROP TABLE tb1");


Here you can find the Source Code: Oracle for Beginners Source Code.

[TIP] Words Tokenization

Sometimes is useful to split the input text in a list of words to Indexing or Searching data.
Here is how to extract words from a sentence in C.

char str[] = "Hi, I'm a test. (This is just a test). "
"Join The #qt IRC Channel!"
"GNU/Linux -";
char delims[] = " !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~";

char *result = NULL;
result = strtok(str, delims);
while(result != NULL) {
printf("%s\n", result);
result = strtok(NULL, delims);

...and this is the Qt way.

QString str = "Hi, I'm a test. (This is just a test). "
"Join The #qt IRC Channel! GNU/Linux -";
QString delim = QRegExp::escape(" !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~");
QRegExp regexp(QString("[%1]").arg(delim),Qt::CaseSensitive,QRegExp::RegExp2);
qDebug() << str.split(regexp, QString::SkipEmptyParts);

Saturday, April 18, 2009

Bernardo The Smallest HTTPD Ever!

Sometimes I need an HTTP Server that allows me to run C or Python code or other scripts, and I need something that require 0 seconds of configuration. So I've decided to write a small httpd that does this work. (Use only for your test, not on a work machine!).

Bernardo (The name is inspired by the Zorro's servant) is a small HTTPD (Less that 200 lines of code) that allows you to Execute C, Python, Perl, PHP code or just "display" a file.

The Usage is Very simple: bernardo-httpd where www dir will be di root of your server and port is the TCP Port of the Service.

And now a simple code Example. You need to complete the HTTP Response Headers and then you can output your data. http://localhost:8080/ will display the "Hello Python" page.

#!/usr/bin/env python

import os

if __name__ == '__main__':
print "Content-Type: text/html"

print "<html>"
print "<head><title>Hello Python</title></head>"
print "<body>"
print "<h1>Hello Python</h1>"
print "<p><b>Request Method</b>: ", os.environ['REQUEST_METHOD']
print "<p><b>Query String</b>: ", os.environ['QUERY_STRING']
print "</body>"
print "<html>"

The Source Code is Available Here: Bernardo HTTPD Source Code.

Monday, April 13, 2009

Qt4 Xml Object Mapper

I'm looking at Google App Engine, to realize my Web Services (Obviously with Python). Web Service Response are generally in XML, and in the 90% of the case you'll need to map Xml Response with your Objects.
I've written a simple class that allows you to handle this task in a easy way, and with a quite good flexibility.

class TestObject : public QObject {

Q_PROPERTY(QString surname READ surname WRITE setSurname)
Q_PROPERTY(QUrl website READ website WRITE setWebSite)
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int age READ age WRITE setAge)


The First step is writing Objects that inherit from QObject and adding your properties. The Mapper uses the introspection to assign the values.

Mapping and Introspection: Maybe you don't like the Xml Node Name as your property's name, in this case you can use the HashMap that allows you to say "Hei, the TagName 'A' is the property 'foo'!". There's also a protected virtual method called convertData that tries to covert Xml Node Text Value to your property's type, take a look at this example.

QString xml = "<personInfo>"

THXmlHashMap dataMapping;
dataMapping.insert("personName", "name");
dataMapping.insert("personWeb", "website");

TestObject obj;
THXmlMapper xmlMapper(&obj);
qDebug() << << obj.surname() << obj.age() <<;

Source Code is Available Here: Qt4 Xml Object Mapper.

Saturday, April 11, 2009

Qt/Cocoa DataStream (Binary Communication)

In a time of REST protocols... I still love the Binary Communication. I don't have much ideas this period, I need to find a different job (C# doesn't fit my code necessity). The Example Source code, contains two Folder. One is the Qt4 TcpServer the second one is the ugly application that you can see below :)

Note: This example doesn't have anything related to the Qt 4.5 Cocoa Support.

Run the Qt4 Server, and the Cocoa Application (Remember to change the SERVER_HOST in AppController.m) and try to send some binary data. The Qt Server will receive this data it will resend you. Protocol is Very easy 1 byte of Data Type (Int8, Int16, ..) and Data. Remember that Intel arch is Little Endian.

Note: This is just a "Draft" a "non complete" example only to give the idea on how it works.

The Source Code is available here: Qt/Cocoa DataStream Source Code.

Qt4 PageFlow

Just another example that I don't have finished last Sunday. This is a simple widget that tries to be similar at the iPhone Mobile Safari Tabs Page. It's a really nice concept used also by Palm Pre to display the running apps. To switch  item swipe left or right (Press Mouse Button, Move mouse in a specified direction and Release it).

Source code is available here: Qt4 PageFlow Source Code.

Sunday, April 5, 2009

Qt4 CoverFlow and QThreadPool

My OpenMoko cannot stay without CoverFlow, so here some code. But I need a lightweight class that allows me to load all files in a directory or a list of files. Scanning directory with QDir entryList can take long time, and loading all the images can take much more, So what I need is a class that solve this problem internally.

I've choose the way of QRunnable and QThreadPool to solve this problem, so I've a class that execute QDir EntryList and another one that load an Image. When file is Ready (FileName, Path) a gray square is displayed and will be replaced by the Image once it is fully loaded and scaled. Using this method you can flow between files even if CPU and Disk are still loading to load images, this means No UI Freeze when CoverFlow starts.

Note 1: You can flow between images using Left and Right Key or Clicking with mouse at the Left or the Right of the Central one.
Note 2: If you want a CoverFlow with a great transition effect take a look at Ariya's PictureFlow.

The Source code Is Available Here: Qt4 CoverFlow Source Code.

Saturday, April 4, 2009

Qt4 Photos Preview

After a great start of the year, working on B*Tree and other Indexing Data Structure, it's about two month that I'm experimenting with User Interface... My Love still remains Networking and File-Systems but it's fun drawing images, shadows and UI Components.

Ok, Networking and Bytes can wait till next week. Today I've watched the PALM Pre Preview, No Back buttons, No Save buttons touch capability outside the glass... Data Abstraction... It's really nice.

But, There are so much things... so I've extracted a tiny nice one. A Nice way to display a "Photo Folders". You can customize the output a bit, Shadow, Photo Size, Second and Third Photo Offsets, but it's an example not a reusable class...

You can find the Source Code Here: Qt4 Photos Preview Source Code.

QtCentre Contest 2008 a Nice Surprise...

Backing home yesterday, I've found a postal package from QtCentre, there's also a QtCentre T-Shirt but this raining day doesn't permit to have great photos.

I would like to thanks again, all the QtCentre, Contest Sponsors (ICS, KDAB, BitRock, Thorsen, ...) and the "Trolltech" Team, for this, for the OpenMoko, and the incredible jobs they do!