Ivan Čukić

Which one is clearer?

This is a excerpt from a real project, just with variables renamed.

EDIT: * I’ve removed the issues from the original code so that this can be a real style vote, without sidetracking the readers.

Although, by fixing the code, I removed one of the things that I think the advantage of the second one is – it is much harder to make errors and dead code.

const qreal ratio = someFunction();

if (ratio < 1.5) {
    result = multiplier;
} else if (ratio < 2.0) {
    result = multiplier * 1.5;
} else if (ratio < 2.5) {
    result = multiplier * 2.0;
} else if (ratio < 3.0) {
    result = multiplier * 3.0;
} else {
    result = multiplier * ratio;
}

return result;

This was the usual imperative code that you’d find almost everywhere. And this is the alternative (functional) way of doing the same:

const qreal ratio = someFunction();

return multiplier * ( 
    (ratio < 1.5) ? 1.0 :
    (ratio < 2.0) ? 1.5 :
    (ratio < 2.5) ? 2.0 :
    (ratio < 3.0) ? 3.0 :
    /* else */      ratio
);

The second one is kinda what you’d do in maths:

       /  1.0     x < 1.5
       |  1.5     1.5 ≤ x < 2.0
f(x) = <  2.0     2.0 ≤ x < 2.5
       |  3.0     2.5 ≤ x < 3.0
       \  x       otherwise

What do you find more readable?

Activities API for QML

I had an unfortunate intermission in kactivities development due to a hard drive failure.

Fortunately, I managed to salvage significant parts of my work, but without git history. Now, it is ready for prime-time.

You are now able to access the activity manager without the need for the ‘clumsy’ activities data-engine. Just import org.kde.activities and create an instance of the ActivityModel.

  import org.kde.activities 0.1 as Activities

  ListView {
      id: main

      model: modelMain

      Activities.ActivityModel {
          id: modelMain
      }
  }

But, this is not all. You are not limited only to retrieve the data, it is also possible to create, delete or alter them. The service control API is asynchronous. Therefore, all methods, apart from regular arguments, receive a continuation function that is called when the method is executed.

Something along these lines:

  modelMain.setCurrentActivity(idOfTheActivity,
    function(result) {
      console.log("Activity switched")
    }
  )

  modelMain.createActivity(nameForTheActivity,
    function(result) {
      console.log("Activity id is: " + result)
    }
  )

Well, that is all for now. I’m thinking of adding overloads without callbacks, but these methods are not really meant to be called by everybody, so I’m not sure how important a more pleasant API actually is. :)

SQL results in range-for

I’ve been irritated for the way a QSqlQuery result-set is supposed to be processed for some time now – with while(query.next()) and getting the results via query.value(...). I was always thinking it would be nice if it were more accessible… namely, accessible using iterators.

To be able to do something like this:

  for (auto &record: query) {
      // do something with
      record[0]
      // or
      record["ID"]
  }

You have a simple .h file to include, and it will just work. [1]

The issue is that it is only tailored to match only this use-case – it can be improved to do more, and to allow more iterators over the same query, but I don’t really care to do that :)

[1] https://gist.github.com/ivan-cukic/7346513

Activity switching questionnaire

I have a few questions that I’d like to hear an answer to from a wider user (and developer) community. Those revolve around the activity switching UI and the things it could have in plasma 2.

Currently, the switcher shows the name and the icon of an activity, and allows you to:

  • switch activities (duh)
  • create / delete activities
  • start / stop activities
  • change the name and icon

This is rather rudimentary. We have more information for the activities that somebody could find useful. We know:

  • which windows are currently open in an activity
  • which documents are open in an activity (for applications that support that kind of thing)
  • which documents have been opened in an activity in the past, along with which were most popular (kept open the longest, accessed multiple times etc.)
  • what is the wallpaper in the current activity, for running activities maybe even the screenshots of the windows

These are some of the things we could have:

  • marking activity as private in the sense of the private mode web browsing which would stop the usage tracking, etc. (not to be confused with encrypted activities we planned for Plasma Active which proved to be infeasible)
  • instead of creating blank or templated activities, we could optionally create activities containing the work from the last hour or so. This would move the windows and documents you are currently working on to the new activity (and move all the related recent usage statistics along with them)
  • some kind of search (for what?)

All comments on above, ideas for new things and even mock-ups are welcome!

boost.range as a dependency

I am coming close to adding boost.range as a dependency to non-library parts of KActivities.

Every time I have to filter out a list, join the elements in a non-standard manner.

So far, I am resisting the temptation, but I have no clue for how long I will be able to manage it :)

Plasma calendar mock-up

I’m working on different ideas for the activity switcher for plasma 2. Unfortunately, I haven’t had much inspiration for that. The only thing I had a real inspiration for was the calendar.

I really dislike out current calendar widget. It is confusing, and the layout is overcrowded and asymmetric (in a bad way :) ). It often confuses me when it shows events – I always think everything is today.

So I made this. It is just a mock-up, no real code yet. Opinions?

cal-mock

Slides from Qt Dev Days: continuations, asynchronous APIs, etc.

Presentation Announcement

I’ve been asked a few times to post the slides of my talk online. Lets hope we’ll have the videos soon as well.

The slides are now available on SlideShare.

Continuations and asynchronous programming at Qt Dev Days

There are quite a few nice talks in the C++ track of Qt Dev Days this year. So many that I don’t expect to move away from that room at all.

I’ll be talking about a fun and clean way to work with asynchronous APIs on Wednesday at the Cafe Moscow in Berlin. I hope I will not get sleepy after lunch.

The talk will be about dealing with asynchronous API using futures (QFuture, std::future, boost::future, QDBusPendingReply…) and continuations. It will be about inversion-of-control, variadic templates, lazy evaluation, functors etc. There will be a few c++11 headache-inducing features demonstrated.

See you in Berlin!

All is well that ends^Wstarts well

The summer is over, but we are only at the beginning of the wonderful Plasma2/KF5 voyage. So, what did I do so far, you ask?

A few things.

Shell switching

For some reason, I always start singing “Shell switching with my two favourite allies” when I get to think about this new Plasma mechanism.

This was the main part of my project for the summer – making the infrastructure to let Plasma adapt itself based on the environment it is running in. For the details on this, check out “Switching the Plasma Shells” post.

The main, and most fun side-effect of this work is that from now on, any QML-based application is able to access the hardware information provided by the uber-awesome Solid library, while it got even more hardware support – namely, it now detects the input devices under udev-based systems.

Activities for the new age

Porting KActivities to KF5/Qt5 was not supposed to be a part of the GSoC. The reason it came to be was that is it much more important for the rest of the development process of Plasma, than some of the originally planned tasks. Though, those are mostly done as well, but will be merged at a later stage – no point in putting finishing touches to something that is still under heavy development.

For more information about the changes in KActivities, see “KActivities for Qt5/KF5″.

Clang Format

This is just a short one.

What would you say if I told you that you can have an indent-like command that actually understands your code?

And that it has plugins for vim, emacs and a few other editors/IDEs that will allow you to reformat your code on the fly?

If you would say ‘Yes please!’, just visit this page and enjoy: Clang Format.

It works quite nicely, except that it doesn’t really speak Qt. Qt’s macro-keywords like Q_SIGNALS, foreach and similar will still require your manual formatting.