Implementing spellchecking in desktop application in C++

When user is supposed to enter significant amount of text in your application, it’s better to help him/her to control it with checking spelling. Basically, to check spelling you need a dictionary with words and algorithm to order these words. Also it might be useful to provide user with possible corrections for any spelling error. Here where Hunspell comes handy. It’s an open source library built on top of MySpell library and used in a significant number of projects varying from open source projects like Firefox to proprietary like OS X. It contains bindings to a number of platforms (.NET, Ruby etc.) and should be fairly easy to integrate to your project. In this post I’ll discuss how to integrate it to C++/Qt project.

Continue reading Implementing spellchecking in desktop application in C++

Classic Producer-Consumer in Qt/C++

Producer-Consumer is a classic pattern of interaction between two or more threads which share common tasks queue and workers who process that queue. When I came to similar task first I googled for standard approaches in Qt to solve this problem, but they were based on signals/slots plus synchronization primitives while I wanted simple and clear solution. Of course, in the end I’ve invented my own wheel and I invite you to take a look at it.

For the synchronization in Producer-Consumer it’s useful to use Mutex and some kind of WaitingEvent for synchronous waiting until mutex is acquired. In Qt you have QMutex and QWaitCondition which are all that we need.

Let’s suppose we have following data structures:

        QWaitCondition m_WaitAnyItem;
        QMutex m_QueueMutex;
        QVector<T*> m_Queue;

where T is type of messages we’re producing/consuming.
For Producer-Consumer usually we need methods produce() and consume(). Let’s see how we can implement them.

Continue reading Classic Producer-Consumer in Qt/C++

Implementing autocomplete for English in C++

When it comes to implementing autocompletion in C++ in some type of input field, the question is which algorithm to choose and where to get the source for completion. In this post I’ll try to answer both questions.

As for the algorithm, SO gives us hints about tries, segment trees and others. You can find good article about them. Author has implemented some of them in a repository called FACE (fastest auto-complete in the east). You can easily find it on GitHub. This solution is used for the autocompletion in search engine Duck-Duck-Go which should tell you how good it is. Unfortunately their solution requires dependencies on libuv and joyent http-parser, which is not good in case you need just to integrate autocompletion functionality into your C++ application, but not build auto-complete server and send queries to it. Another drawback – libuv and cpp-libface itself fails to compile in Windows which is bad in case you’re building cross-platform solution.

You can find out how to built FACE into your cross-platform C++ application below.

Continue reading Implementing autocomplete for English in C++

Handling drag’n’drop of files in Qt under OS X

If you ever tried to handle drag’n’drop files in your Qt application, you would usually come up with the code like the following.
First of all you will need a Drop Area somewhere in your application, which will handle drops

DropArea {
  anchors.fill: parent
  onDropped: {
    if (drop.hasUrls) {
      var filesCount = yourCppModel.dropFiles(drop.urls)
      console.log(filesCount + ' files added via drag&drop')
    }
 }
}

Where yourCppModel is a model exposed to Qml in main.cpp or wherever like this:

QQmlContext *rootContext = engine.rootContext();
rootContext->setContextProperty("yourCppModel", &myCppModel);

and int dropFiles(const QList<QUrl> &urls) is just an ordinary method exposed to QML via Q_INVOKABLE attribute.

You will sure notice everything works fine unless you’re working under OS X. In OS X instead of QUrls to local files you will get something like this: file:///.file/id=6571367.2773272/. There’s a bug in Qt for that and it even looks closed, but it still doesn’t work for me that’s why I’ve implemented my own helper using mixing of Objective-C and Qt-C++ code.

Continue reading Handling drag’n’drop of files in Qt under OS X

How to keyword and upload photos/illustrations to microstocks

If you’re a photographer new to microstock photography, I highly reccomend you to embark this wonderful way of passive earning using your artworks. But I will suppose that you know what is this about and how basically use it. I would even suppose you’ve passed an exam on some stock agency and uploaded your first artworks. But did you know there’re plenty of stocks? And did you know it’s better to work with all major players of this industry? Shutterstock, iStock, Fotolia, Depositphotos to name just a few. And there is a lot more.

Keywording and uploading your photos to stocks can be fairly considered as the most boring part of this business. It’s quite a long way to upload your files to 10 different websites, keyword them and submit. For such a purpose I’m working on a keywording and uploading tool, called Xpiks. It’s main objective is to help you to dramatically reduce time you spend on keywording and uploading and enable you to upload to more stocks simultaneously. For that reason I added features like parallel editing of several files, keywords suggestion, spell checking, automatic FTP uploading, stock requirements checking and others.

You can see screenshots or read more on how to use it on the official website.

This program is open source and free and available through all major platforms: OS X, Windows and Linux. There’s a support section and you’re welcome to file your bug or post a question there.

Don’t waste more time than needed, download Xpiks and enjoy!

How to download huge folder from Dropbox

If you face a problem to download folder from Dropbox which contains tons of files, no known browser extension can help you. Dropbox moves each file download to it’s separate page and you can’t do it directly.

When I faced this problem I knew I would need to create my own solution and quick googling just confirmed that.

I opened javascript console and extracted all links from the folder. Then I replaced “dl=0” to “dl=1” to get actual download link.

var links = document.querySelectorAll("div.filename a")
var processed = Array.prototype.map.call(links, 
  function(link) { 
    return link["href"].replace("dl=0", "dl=1"); 
})
console.log(processed.join("\n"))

After I copied those to file links_to_download. If your processed array is too big, you can print it to console by chunks, using slice(start, end) method from Javascript. Now the problem is to download them.

I came up with wget for such problem. Linux and OS X users should have wget available (OS X users can install it via e.g. homebrew). Windows users have to download it separately, install and add to the PATH environmental variable. Additionaly I used –trust-server-names and –content-disposition parameters to save real filenames instead of dropbox hashed url. Then I faced a problem that it fails to download a file on first request and request timeout is quite big so I’ve set it to 5 seconds. Now it makes several timed-out requests, but they quickly resolve to the successful one.

wget --content-disposition --trust-server-names 
  --timeout=5 -i links_to_download

Also in order to download via https in Windows you probably will need to use “–no-check-certificate“.

Tips and tricks to improve performance of your ACM solution

Here I gathered system-programming tricks that can improve performance of your solution in C++ dramatically!

  • Use scanf/printf functions for standard IO instead of cin/cout
  • Memory-align buffers and structures to WORD size of your architecture (4 bytes for 32-bit and 8 bytes for 64-bit)
  • Use arrays instead of linked lists (to use memory block caching)
  • Avoid “if” stamements in loops
  • If-close should contain code, which is more likely to execute (if-condition == true)
  • Use inlining for short functions
  • Use objects allocated on stack but not on heap (local objects for functions instead of allocated with malloc/new)
  • Use pre-calculated hardcoded data (e.g. you can store first N prime numbers or first N Fibonacci numbers in order not to calculate them every time you need one)

Interesting issues and features of Qt programming

In this post I enlist all interesting facts and issues I’ve experienced while developing my first project in Qt (from 5.3 to 5.4 5.6)

18.02 – QSettings interface in Qml transformed bool to string and was always true on deserialization

18.02 – QByteArray returned from local scope crashed with heap corruption on return of function (destructor of QByteArray)

Qt can delete your object in it’s gui loop. QObject should have CppManaged attribute and it should be set before returning object to UI code.

26.02 QTimer can be only started when EventLoop is already running (e.g. with app.exec()) otherwise you get an error “QTimer can be started in QThread”

Drag and Drop files in OS X inserts NSUrl instead of QUrl and you have to convert it using Objective C to real filepath. You can add .m file to Qt project and write C++/Objective-C code.

23.03 QtConcurrent::mapped can accept a struct with operator() but only with inner typedef T return_type; where T is mapped type for correct QFuture<T> conversion. Also, QtConcurrent::mapped cannot be cancelled.

30.05 Qt Column allows to do animations, but has issues with stretching and ColumnLayout has no issues with stretching, but doesn’t allow you to do animations. I had to use simple anchors layout and States with Transitions to animate properties I wanted

1.06 Qt ListView has Transitions add/remove/removeDisplaced/displaced/etc, which allow to create nice animations for adding/deleting items from listview and to make UI really nice

6.06 Qt lacks standard Zip/Unzip functionality. QuaZip makes life easier, but has minor issues with compilation (linking etc).

22.07 TextInput’s EditingFinished signal fires twice in OS X

6.09 Windeployqt does not pack qml dlls into the bundle, you have to do it manually from your <Qt-bin-dir>/qml. You need to copy QtQuick and QtQuick2 directories and others which you use

19.09 Tab control is a loader and you can’t access it’s child object by Id. You have to create a property of Tab and do double binding inside and outside Tab to that property.

25.10 Repeater in GridLayout does not respect QAbstractItemModel changes, but does respect once put into Flow

23.11 Always initialize boolean fields, volatile or not. Always do that because in other case you will get tons of unpredictable behavior.

28.11 Use QVector instead of QList for most types. If you have released public version with wrong type and users depend on it, you’re screwed

12.12 QML ListView fails to update itself after sophisticated filter/remove operations. You have to manually positionViewAtBeginning() stuff

20.03 QFile in Windows does not respect flag QIODevice::Unbuffered because of “lack of native support in Windows” (bullshit) as of Qt 5.6

21.03 QProcess has super strange problems. Exiftool does not work with unicode from QProcess but works when launched via cmd.exe

2.05 QWaitCondition destructor produced a warning that it’s destroyed while inner Mutex is still locked. Was able to debug that using export QT_FATAL_WARNINGS=true in the running environment

20.07 QString::fromLatin1() truncated buffer of QByteArray parameter if the latter contained some eol/nl characters

Text encryption in Qt/C++ with tiny AES 128bit

Have you ever needed a small, really small encryption in your C++ project for some piece of text? Say, credentials, login details or any other sensitive data? Of course, the best way is to keep just hash of salted password, but… What if you just need to do it and the size is so much critical for you?

There’re openSSL library and Crypto++ library which are monsters with tons of encryption algorithms, used in a number of solid projects etc. But.. they are big! I don’t want 30Mb library in my tiny project, which weights 10 Mb with high-resolution icons for OS X which weight by itself 5Mb. So I don’t want to sacrifice the size but still need encryption. Meet tiny-AES. It’s really small AES 128-bit library which does encryption in CBC and ECB modes. It really contains everything you needed just to encrypt and decrypt your sensitive data and forget about it.

You can find example under the hood.

Continue reading Text encryption in Qt/C++ with tiny AES 128bit