Thursday, May 7, 2009

PyCon3 Italia, 8-9-10 May 2009


I will spend the next few days in Florence for PyCon. Probably on Monday, I'll publish some photos and why not, code examples from PyCon.

See you there!

Wednesday, May 6, 2009

Qt4 Touch and Rotate

Alan has asked my How I've implemented the Touch and Rotate feature of the Moko. The answer is very simple, It's all Magic! There're very few lines of code to do it, because all the magic is made by atan2.

Atan2 is a variation of the arctangent function. For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it.

The Source Code is available here: Qt4 Touch and Rotate Source Code.
OT: PyCon3 (Italia) starts Tomorrow!

Saturday, May 2, 2009

Qt4 JSON Stream Reader

JSON, short for JavaScript Object Notation, is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects).

One way to use JSON with Qt4 is using the QScriptEngine (engine.Evaluate(jsonSource)), But I like Reinventing the wheel, so I've written a simple JSON Stream Reader that works "almost" like the QXmlStreamReader.

You've a readNext() method that read the next token and returns its type, and two properties name() and values() that Returns the name of the Property, Object or Array and the Value of the Property in a QVariant that can be Bool, Int, Double or String.

Below there's an Example of How to use the JSON Stream Reader. Maybe you can use it to create an automatic Object Mapper like the Xml Object Mapper that I've posted a couple of weeks ago.


QFile file("test.json");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << file.errorString();
return(1);
}

THJsonStreamReader reader(&file);
while (!reader.atEnd()) {
switch (reader.readNext()) {
case THJsonStreamReader::PropertyNumerical:
qDebug() << " - Property Numerical" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyString:
qDebug() << " - Property String" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyFalse:
qDebug() << " - Property False" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyTrue:
qDebug() << " - Property True" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyNull:
qDebug() << " - Property Null" << reader.name();
break;
case THJsonStreamReader::Object:
qDebug() << "Object" << reader.name();
break;
case THJsonStreamReader::ObjectEnd:
qDebug() << "Object End";
break;
case THJsonStreamReader::Array:
qDebug() << "Array" << reader.name();
break;
case THJsonStreamReader::ArrayEnd:
qDebug() << "Array End";
break;
}
}

file.close();



The Source Code is Available Here: Qt4 JSON Stream Reader Source Code.

Friday, May 1, 2009

iPhone: Touch, Drag, Hit!

Yesterday, Fredrik has asked me something like "Drag & Drop with Hit Test" on the iPhone. Here's a simple example.  There's a simple UIView class that handles the touch events and a protocol that says that if the "DragMe View" has hit or not the "HitMe View". Below you can see the result and the Source Code Link.

The source code is available here: Touch, Drag, Hit Source Code.

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("192.168.3.2", "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; ");
db.ExecuteNonQuery(sp1.ToString());

// 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; ");
db.ExecuteNonQuery(sp2.ToString());

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");
db.ExecuteStoredProcedure("tb2sp1");
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");

db.Disconnect();


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 - theo@gmail.com";
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 - theo@gmail.com";
QString delim = QRegExp::escape(" !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~");
QRegExp regexp(QString("[%1]").arg(delim),Qt::CaseSensitive,QRegExp::RegExp2);
qDebug() << str.split(regexp, QString::SkipEmptyParts);