Saturday, August 30, 2008

Qt4 TreeView - Items Filtering

In a few words, I've a QTreeView that displays folder items. I Need to filter items, showing empty folders.

QSortFilterProxyModel: For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.

Using QSortFilterProxyModel the result is that the filter is applied on the foldes, so we've something like a root only filter. The solution is simple.. Inherits QSortFilterProxyModel and...
class QfTreeProxyModel : public QSortFilterProxyModel {
  public:
    QfTreeProxyModel (QObject *parent = 0)
      : QSortFilterProxyModel(parent)
   {
      setFilterCaseSensitivity(Qt::CaseInsensitive);
   }

  protected:
    bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
    {
      QModelIndex idx = sourceModel()->index(source_row, 0, source_parent);
      if (sourceModel()->hasChildren(idx))
         return(true);
      return(QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent));
    }
};

Here you can find the example source code: http://th30z.netsons.org/wp-content/uploads/qfilteredtreeview.cpp

Monday, August 11, 2008

Qt4 Mac Dock Icon Click

When you click the "close button" of an application's window, on Mac OS X the window will be hidden but the application continue running. When you click on the Dock Icon, the application's window will be showed. This is the default behaviour for Mac Apps but Qt Apps doesn't the same. So, how we could implement this behaviour?
#ifndef _APPLICATION_H_
#define _APPLICATION_H_

// Qt4 Headers
include <QApplication>
include <QWidget>

// Carbon Headers
#ifdef Q_WS_MAC
#include <Carbon/Carbon.h>
#endif

class Application : public QApplication {
Q_OBJECT

public:
Application (int& argc, char **argv);

QWidget *mainWindow (void) const;

private:
QWidget *m_mainWindow;

#ifdef Q_WS_MAC
AEEventHandlerUPP m_appleEventProcessorUPP;
#endif
};

#endif // !_APPLICATION_H_

Here, we've implemented an Application class that Inherits from QApplication. This class contains a reference to the App's Main Window.
#ifdef Q_WS_MAC
static OSStatus appleEventProcessor(const AppleEvent *ae,
AppleEvent *event,
long handlerRefCon)
{
Application *app = (Application *) handlerRefCon;

OSType aeID = typeWildCard;
OSType aeClass = typeWildCard;

AEGetAttributePtr(ae, keyEventClassAttr, typeType, 0,
&aeClass, sizeof(aeClass), 0);
AEGetAttributePtr(ae, keyEventIDAttr, typeType, 0,
&aeID, sizeof(aeID), 0);

if (aeClass == kCoreEventClass) {
if (aeID == kAEReopenApplication) {
app->mainWindow()->show();
}
return noErr;
}

return eventNotHandledErr;
}
#endif

Application::Application (int& argc, char **argv)
: QApplication(argc, argv)
{
// Don't Quit the App on Window Close
setQuitOnLastWindowClosed(false);

// Initialize Main Window
m_mainWindow = new QLabel("Test Main Window");

#ifdef Q_WS_MAC
// Install Reopen Application Event (Dock Clicked)
m_appleEventProcessorUPP = AEEventHandlerUPP(appleEventProcessor);
AEInstallEventHandler(kCoreEventClass, kAEReopenApplication,
m_appleEventProcessorUPP, (long) this, true);
#endif
}

QWidget *Application::mainWindow (void) const {
return(m_mainWindow);
}

Thats all folks! Run the app, Click on close button and then click on the dock icon to see the result.

Just for Fun - Qt4 DeviantArt

Just for Fun, I've developed a "Ten Minutes App" to demonstrate how to retrieve images from DeviantArt Feed. (This will be a Quartica Plugin).



You can find the source here: deviantart-sample.tar.bz2

Qt4 "Freedesktop" MIME Type

A couple of weeks ago, i've released at Qt Apps a Qt Component called QFreeDesktopMime that is a simple class for determining the MIME type of files using FreeDesktop Specification.

How to use this class? it's really simple.

Example 1: Get Information about specified MIME Type
QFreeDesktopMime mime;
QString description = mime.description("application/epub+zip");
QString expandedAcronym = mime.expandedAcronym("text/x-xslfo");
QString acronym = mime.acronym("text/x-xslfo");

Example 2: Extract MIME Type and information from File Name (Results based on File Extension)
QFreeDesktopMime mime;
QString mimeType = mime.fromFileName("test.png");
QString mimeDescription = mime.description();

Example 3: Extract MIME Type and information from File (Data)
QFreeDesktopMime mime;
QString mimeType = mime.fromFile("/home/oz/myUnknownFile");
QString mimeDescription = mime.description();

If fromFile() method doesn't find the matching data it tries to use fromFileName() method to get the MIME Type. In all cases if file's MIME Type wasn't found it returns an empty QString.

Saturday, August 9, 2008

Qt4 Image Transfers Viewer

Today a new Qt4 Effect developed for Quartica File Transfers View. The main idea is to remove the unuseful progressbar from Downloading Images and replace it with something more intuitive.

In this way, you can see the full image and the current completed part. And now a Very Low quality video that demonstrate how it works (please, wait 7 seconds before going away).

Wednesday, August 6, 2008

Qt4 ThumbViewer

It's only a Work in Progress class, but this screenshot will be the Album Viewer Widget of Quartica.
All the images are behind a shadow effect and so it looks all darker. What do you think about it?