Blog

The future of mimec.org

Submitted by mimec on 2017-04-24

The main focus of all my recent work is Bulletcode and this will remain my top priority in the coming months and (hopefully) years. There's a lot going on at the moment, so if you want to get the latest updates, please make sure that you follow my official Twitter channel: @MichalMecinski.

I'm not planning to update my blog dedicated to Qt development, mostly because I'm moving away from this technology and learning something new. However, I'm planning to start writing a new series of technical blog posts about JavaScript and Node.js. Most likely I will switch to some new, more community-oriented blogging platform, but I will post a link here so you don't miss it. Also, soon a new, very cool project using Node.js will be released. Those who follow me on Twitter could already see some early previews. Hint: it will be a multi-player online game :).

The amount of time that I can dedicate to my numerous open-source projects is much smaller than before, but it doesn't mean that nothing's happening in that area. Very soon I will publish the long-awaited version 1.0 of Saladin with a ton of visual and functional improvements. Later this year a bug fix release of WebIssues will be published as well.

Life goes on pt. 2

Submitted by mimec on 2016-09-28

Wow... I just realized that the last time I wrote anything here was nine months ago, and what's worse, almost every word that I wrote is no longer true. The only thing that remains pretty much the same is my job, which is still the same as 11 years ago. I found that at this moment I'm much closer to what I wrote in April 2014, in the previous Life goes on post. So who knows, maybe it will become a new series?

First of all, I'm officially divorced, once and for all. I tried to fight against it, but when your life becomes an endless war, it means that something went terribly wrong. So in the end it's a big step forward after all the turbulences from the past three years. Still, much time has to pass before I can even start thinking about being with someone again, and I must admit that's something that I really miss. It's really ironic that in 2014 I wrote that I'd already gone through all the denial-anger-regret stages, because so much has happened since that time that now I'm in yet another stage of profound sadness.

Also, the project that my company has been working on for almost a year, failed miserably for reasons beyond our control, just like the previous one. Ironically, the reasons were pretty much the same in both cases - the people that we relied on exhibited a very harmful mixture of greed and utter ignorance. We still have to decide what to do next, but honestly, none of us is willing to go through this once again. It surely wasn't a waste of time, working on this project was a great experience and I'm extremely proud of what we have accomplished as a team. However, seeing things that you've created burn and fall because of someone's bad will doesn't feel so great.

So what remains? Not much for now, I must admit. Obviously I have to start working on something again just to remain sane. The most logical thing would be to return to one of my open source projects. For example, there's a pile of feature requests for WebIssues. I don't rule it out, perhaps I will do that next year. However, at this moment I need to start something new and a bit more creative. In my case typically that means creating a game. I still remember the lessons I drew from Mister Tins, so this time I will take a slightly different approach. It will be a simple 2D browser game. There will be stronger focus on graphics and level design, and the game engine will be as simple as possible. Generally the goal is to create a nice looking and fun game with reasonable effort. Recently any plans I make tend to backfire, but on the other hand it's always important to keep trying, so time will show how it goes this time.

Tags

Happy 10th anniversary, mimec.org!

Submitted by mimec on 2015-12-15

Today is a very special day. Excactly ten years ago, on December 15th 2005, I wrote the first post on the mimec.org website. It's become a tradition that I write a short summary of the past year on each anniversary, and I will do it again today, because it was also a very special year for me.

First of all, there were a lot of round anniversaries this year. Almost exactly 10 years ago I graduated from college. WebIssues turned 10 years old in November, although technically it wasn't officially released until September 2006. Fraqtive turned 10 years old in January. And in March it was 10 years since I started my first job - and after those 10 years I still work for the same company, although it grew in size from a few developers to a few hundred, changed its name and relocated its headquarters. Even my primary day job project is still the same after 10 years. So, looking at those numbers, one might think that my life is very stagnant, and I will most likely spend the rest of it in the same place, doing the same things…

But even though a lot of things remained the same for such a long time, the last year also brought a lot of substantial changes. I overcame a serious crisis in my family, and we are back together, although a year ago nothing indicated that this would ever be possible. I think that this is my greatest personal accomplishment, and I simply owed this to my son. Obviously it doesn't mean that it's all a bed of roses now, quite on the contrary, but it was a very valuable lesson for all of us, and I will definitely not let the most important things get out of control again.

Today is also the first anniversary of Bulletcode, a software company founded by me and two of my friends. At the moment it's still more of a hobby than a real business, we put more money into it than we make, and we try to put as much work into it as our day jobs allow. But the whole year was a huge, invaluable experience for us all. We started the company to work on a very promising project, which unfortunately failed miserably for reasons that were beyond our control. So we ended up with a company which generated costs, without any projects, with no business partners, and with no idea what to do next. But instead of shutting it down, we took the challenge and started looking for new ideas and opportunities. I cannot reveal yet what we are working on, it's all a bit of a mistery and conspiracy, but it's definitely the most interesting project I've ever participated in, and we're all waiting impatiently to release our first product.

Model-View-Presenter in QtQuick pt. 2

Submitted by mimec on 2015-06-03

In the previous article I explained the basics of a simple Model-View-Presenter architecture in QtQuick. Now it's time to explain the implementation details. I will start with the Presenter QML component which is the base component of all presenters:

FocusScope {
    id: root

    property Item view
    property QtObject model

    property var modelToViewBindings: []
    property var viewToModelBindings: []

    Component.onCompleted: {
        root.view.anchors.fill = root;
        root.view.focus = true;

        for ( i in root.modelToViewBindings ) {
            name = root.modelToViewBindings[ i ];
            dynamicBinding.createObject( root, {
                target: root.view, property: name, source: root.model, sourceProperty: name
            } );
        }

        for ( var i in root.viewToModelBindings ) {
            var name = root.viewToModelBindings[ i ];
            dynamicBinding.createObject( root, {
                target: root.model, property: name, source: root.view, sourceProperty: name
            } );
        }
    }

    Component {
        id: dynamicBinding

        Binding {
            id: binding

            property var source
            property string sourceProperty

            value: binding.source[ binding.sourceProperty ]
        }
    }
}

As you can see, the presenter is a FocusScope, so that it can be used as a top-level visual component of a window or can be embedded in some more complex layout. The presenter contains a view property which holds a reference to the embedded view component and a model property which holds a reference to the model object. The Component.onCompleted function makes sure that the view fills the entire presenter and that it has keyboard focus enabled.

The remaining code is related to binding properties between the model and the view and vice versa in a simplified way. The modelToViewBindings property contains an array of names of properties that should be bound from the model to the view. The first loop in the Component.onCompleted function creates a Binding object for each property binding. The target of the binding is a property of the view. The value of that property is bound to the corresponding source property in the model. Note that JavaScript makes it possible to access a property of an object with a given name using the [] operator, because an object is essentially an associative array. The binding from the view to the model works in exactly the same way. As demonstrated in the example in the previous article, it's even possible to create bi-directional bindings.

The view and model objects are created in the actual presenter component which inherits Presenter. As I explained in the previous article, we want to be able to switch both the view and the model as easily as possible. In order to do that, I implemented a simple factory, which is basically a shared library implemented in JavaScript:

.pragma library

var config = {
    modules: {},
    urls: { views: "views/", models: "models/" }
};

function createObject( type, name, parent ) {
    var module = config.modules[ type ];
    if ( module !== undefined ) {
        var qml = "import " + module + "; " + name + " {}";
        return Qt.createQmlObject( qml, parent );
    } else {
        var url = config.urls[ type ];
        if ( url !== undefined ) {
            var component = Qt.createComponent( url + name + ".qml", parent );
            var object = component.createObject( parent );
            if ( object === null )
                console.log( "Could not create " + name + ":\n" + component.errorString() );
            return object;
        } else {
            console.log( "Factory is not configured for " + type );
            return null;
        }
    }
}

function createView( name, parent ) {
    return createObject( "views", name, parent );
}

function createModel( name, parent ) {
    return createObject( "models", name, parent );
}

The config variable specifies the names of C++ modules and/or URLs of directories that contain QML files with the implementation of views and models. By default, the models can be simple mock-ups written in QML, which makes it possible to run the whole application directly using the qmlscene tool without writing a single line of code in C++.

The createObject() function first looks for a registered C++ module of the given type. If present, it creates a simple component declaration in QML and calls Qt.createQmlObject() to create the instance of the component. When there is no C++ module registered, the function tries to load the component from the QML file in the given location. The createView() and createModel() functions are wrappers that can be used for simplicity.

By changing the default configuration of the factory at run-time, you can not only switch between mock-up models and real models implemented in C++, but also, for example, to use different views in mobile and desktop versions of the application. For example, the Component.onCompleted function of your top-level QML component could contain the following code to override the default configuration:

if ( typeof models !== 'undefined' )
    Factory.config.modules.models = models;

The C++ application can register the classes which implement the models in a module and pass the name of that module to the QML engine as a property of the root context, like this:

qmlEngine->rootContext()->setContextProperty( "models", "MyApp.Models 1.0" );

Unfortunately, it's not possible to pass variables from C++ code directly to shared JavaScript libraries which use the ".pragma library" directive (according to this thread, this is done this way on purpose). The method shown above can be used to work around this limitation; just keep in mind that the factory needs to be configured before any objects are actually created.

Model-View-Presenter in QtQuick

Submitted by mimec on 2015-05-19

One of the advantages of using QtQuick is that it's possible to fully separate the user interface from application logic. Not only there are separate classes or components, but even the programming language used in both layers is completely different. The user interface can be written (or designed) using QML, a declarative and highly expressive language created strictly for the purpose of developing modern UI. The application logic on the other hand can be written in C++, which is powerful, abstract and yet exceptionally fast. Such approach has many advantages over writing the UI directly in C++, because with all it's strengths, it's not well suited for UI development. One of these advantages is that you can have two separate teams in the project, and the UI team doesn't need to know C++, and the application logic team doesn't need to know QML.

But wait, eventually these two layers have to be integrated somehow, right? There are several possibilities of doing such integration, and at first they seem quite complex, which defeats the purpose of simplifying the development process. The Qt tutorials and documentation don't give a lot of help. They only mention a general rule of thumb that you should never directly access or manipulate QML objects from C++ code, although the API certainly makes this possible. The whole idea is that the QML code should create and use objects implemented in C++ to perform various operations. But then if a QML button directly invokes some method implemented in C++, this has nothing to do with decoupling the application logic from the UI.

The solution to this problem is well known and it can also be successfully applied to QML and C++. The idea is to introduce another layer of abstraction between the application logic (the model) and the UI (the view). There are several variants of such architecture, the most common ones are Model-View-Presenter and Model-View-Controller. The differences are quite subtle and I'm not going to dig into the details. They mostly have to do with the lifetime and control flow between these layers. Generally MVC is commonly used in web applications, and MVP is often used in desktop applications, but this doesn't always have to be true. My goal wasn't to strictly follow any particular approach, but to find one that is best suited to solve the given problem, which is what design patterns are all about.

My implementation of the Model-View-Presenter architecture in QtQuick is based on the following assumptions:

  • A view is a visual QML component which defines the look of the application and has no logic (except for basic validation). It can be anything from a single ListView to a complex form with lots of different controls. The view should have some signals that correspond to user actions (for example indicating that a button was clicked). It also may have some properties that can be used for passing data to the view (for example to change the text of some label) and from the view (for example to retrieve text entered by the user). It doesn't require or directly use any components related to application logic, so a view can be created and tested independently from the other two layers.
  • A model is a C++ class that implements some aspect of the application logic. It typically has a number of methods that perform some operations. It may also have signals, which are useful to indicate that an asynchronous operation has completed. Properties can be used to pass data to and from the model, for example parameters and results of an operation. The model doesn't know anything about any visual components of the application. It can be created independently from the other two layers, which is useful for unit testing, automation, etc.
  • A presenter is what glues the view and model together. It's a visual component written in QML, but its only visible content is the view that's embedded in it. The presenter also creates the model object. Then it connects appropriate signals and slots from the view and the model and bind their properties. These connections and bindings can include additional logic, but it's best to design the interface of the view and model in such way that they can work directly together. In that case the presenter's only job is to create and set up these two objects and the overhead of having the third layer between the UI and the application logic is very small.

The MVP architecture makes it possible for the views and models to be created and tested independently, by two different teams, as long as their interfaces (signals, slots and properties) are designed in a compatible way. Another advantage of such approach is that it makes prototyping really easy. Designing a good user interface is a difficult craft and often the best solution is to quickly build and test different variants and prototypes. But the prototype often needs to demonstrate not only the appearance, but also the behavior of the application, so it's very useful to have some mock-up of application logic. This can be done by writing mock-up models which simply pretend to perform complex operations by returning fixed or random data. Such mock-up models can even be created in QML, which makes it possible to run and test the entire prototype without writing a single line of C++ code!

I will write more about writing mock-up models and switching between mock-ups and real models in the next article. For now let's assume that a model is a simple C++ class which inherits QObject and a view is a simple QML component which inherits Item or one of its sub-classes. The key and most interesting part of this architecture is obviously the presenter, so let's take a look at an example:

Presenter {
    id: root

    signal showMainWindow()

    view: Factory.createView( "LoginView", root )
    model: Factory.createModel( "LoginModel", root )

    modelToViewBindings: [ "login", "error" ]
    viewToModelBindings: [ "login", "password" ]

    Connections {
        target: root.view
        onLoginClicked: { root.view.disableView(); root.model.beginLogin(); }
    }

    Connections {
        target: root.model
        onLoginSucceeded: { root.showMainWindow(); }
        onLoginFailed: { root.view.enableView(); }
    }
}

As you can see, this component inherits the Presenter class. I will show you the implementation of that class in the next article, but for now let's skip the technical details and take a look at how the actual presenter is implemented, in this case a simple login window.

The showMainWindow() signal is used to communicate to some higher level object (for example the application object) that it should display the main window of the application containing an appropriate presenter. In real life applications there will often be multiple views that can be switched or even displayed side by side, and multiple models that provide functionality for these views. The application will never interact with these views and models directly but rather will create appropriate presenters to handle them.

The view and model properties contain references to the view and model objects which are owned by the presenter. These objects are created using a helper Factory class, which makes it possible to dynamically switch between mock-up models implemented in QML and real models implemented in C++, or for example between views designed for mobile and desktop version of the same application. I will write more about the factory in the next article.

The modelToViewBindings and viewToModelBindings properties make it easy to bind properties of the same name and type between the model and the view. Again, I will present the underlying implementation in the next article, but generally the idea is to create a number of Binding components dynamically using a very simple syntax. Obviously in more complex cases it's possible to set up the bindings manually. The bindings can be unidirectional or bidirectional, like in case of the "login" property. For example, the model can remember and initialize the view with the last used login, and the view can pass the new login entered by the user back to the model.

The last part of the presenter handles signals from both the view and the model. In most cases, the signals from the model are passed directly to slots in the view and vice versa, but they can also be connected to a signal in the presenter itself or can contain more complex logic. In this simple example, when the login button is clicked, the view becomes inactive (with some visual hint like a busy indicator) and the model begins processing the login request asynchronously (for example by contacting a server or database). When login completes successfully, the showMainWindow() signal is emitted to the parent of the presenter, and in case of an error the view is activated again and some error message is displayed through the "error" property. This is obviously a very simplified example, but it represents what might happen in a typical modern application.

You can find the next articles of this series here.