PyQt4: PyCon3 Photos on Flickr

May 16, 2009 | Filed Under Events, Qt4 | No Comments

PyCon3 was finished, Only one week is passed and I already miss it. Guido van Rossum, Alex Martelli, Ariya Hidayat, Raymond Hettinger, David Boddie, Fredrik Lundh… C/C++ and Python Developer not .NET “coders” (I hate even more my boring .NET job).

Following the PyCon week, here a simple example of PyQt4 that allows you to see up to 500 photos tagged PyCon3 on Flickr.

PyQt4 PyCon3 Flickr

As always the source code is available here: PyQt4 PyCon3 Flickr Source Code.

Qt4 Touch and Rotate

May 7, 2009 | Filed Under Qt4 | No Comments

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.

Qt Touch and Rotate

The Source Code is available here: Qt4 Touch and Rotate Source Code.

OT: PyCon3 (Italia) starts Tomorrow!

Qt4 JSON Stream Reader

May 2, 2009 | Filed Under Qt4 | No Comments

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.

Qt4 Google Authentication

May 1, 2009 | Filed Under Qt4 | 1 Comment

Today, I’m looking at Google services, Contacts, Maps, Search… to use all this service in your app you need to get The Authentication Token (see the Google Auth For Installed Apps page). So, I’ve implemented a simple class to do this.

QEventLoop q;
THGoogleAuth gAuth("MYNAME@gmail.com");
QObject::connect(&gAuth, SIGNAL(authenticated()), &q, SLOT(quit()));
gAuth.login(THGoogleAuth::Contacts, "MYPASSWORD");
q.exec();

if (gAuth.error() == THGoogleAuth::NoError) {
    qDebug() << "SID" << gAuth.sid();
    qDebug() << "LSID" << gAuth.lsid();
    qDebug() << "AUTH" << gAuth.auth();
} else {
    qDebug() << gAuth.errorString();
}

The Source Code is Available Here: Qt4 Google Auth Source Code.

Qt4 Line Graph

April 25, 2009 | Filed Under Qt4 | 2 Comments

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 :)

Qt4 Linear Graph

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

Qt4 Xml Object Mapper

April 13, 2009 | Filed Under Qt4 | 1 Comment

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_OBJECT

   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>"
                   "<age>21</age>"
                   "<personName>Matteo</personName>"
                   "<surname>Bertozzi</surname>"
                   "<personWeb>http://th30z.netsons.org/</personWeb>"
               "</personInfo>";

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

TestObject obj;
THXmlMapper xmlMapper(&obj);
xmlMapper.setMapping(dataMapping);
xmlMapper.map(xml);
qDebug() << obj.name() << obj.surname() << obj.age() << obj.website();

Source Code is Available Here: Qt4 Xml Object Mapper.

Qt4 PageFlow

April 11, 2009 | Filed Under Qt4 | No Comments

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).

QtPageFlow

Source code is available here: Qt4 PageFlow Source Code.

Qt4 CoverFlow and QThreadPool

April 5, 2009 | Filed Under Qt4 | 2 Comments

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.

Qt4 CoverFlow

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.

« Previous PageNext Page »