Constantly programming in C ++ / Qt, I noticed that it would be more convenient to store my code snippets somewhere and have
quick access to them. Of course, I began to search and found many ready-made organizers and websites. I tried them, but all this did not suit me.
I just wanted
quick access - which means for global hotkeys. To click - entered the words in the search - got the code. But only two programs had such functions, but they were paid and not cross-platform.
Therefore I undertook to write the program on Qt. It turned out to be so convenient that I can no
longer imagine my coding without it, and of course I decided to publish it -
what if someone else is useful ? Under the cut talk about the problems and tasks that had to be solved, lay out my experience, as well as a review of the program itself and its source code.

Development history
For a start, I decided - the program should be small, comfortable and smart. This means that the program should be placed in the tray so that it does not interfere in the taskbar. Fortunately, Qt provides for this a convenient cross-platform solution in the form of a class QSystemTrayIcon. Also, the program must
necessarily work on global hotkeys. Here is a problem! The fact is that Qt, unfortunately, does not support them out of the box. On that is library
libqxt which I very successfully used almost year under Qt 4.8. However, under Qt 5, for some reason that was unclear to me, they didn’t work out normally (at that time), and under Win7, she didn’t want to pack up, and I gave up. Instead, I inserted the slightest bit of native code — the winApi call to the RegisterHotKey function. This is the only native insert in the program - and I think it will be quite easy to modify it under Linux or Mac.
')
I decided to save the pieces of code in the XML database. Maybe this is not the best solution, but it still copes with the task. And in general, I got used to it.
As a basis for the editor, I took, of course, QTextEdit. A pleasant surprise was waiting for me here - the code copied from QtCreator is copied as HTML with markup and, therefore, with syntax highlighting. Immediately, the need to write the built-in syntax highlighting for C ++ \ Qt disappeared by itself. QtCreator has a very cool syntax highlighting, especially if you customize it.

Then, when it was already possible to insert the code into the window and it went into the database, and then into the xml file, the question arose of how to get it conveniently and quickly. To do this, I wrote a small search engine that looks for pieces of code where all the words specified with a space are found. While it is enough. There are already about 100 code fragments in my personal database and still I am perfectly aware of them among them.
Also made keywords. Well, without them? The search goes not only by the main code, but also by them. In cues, you can specify the programming languages ​​to which this paste belongs, for example, “c ++, qt” or “qml, js”. If the search resulted in code fragments from several languages, you can differentiate them by adding a keyword-language.
Modifying the program, I drew a logo in
GIMPe - braces. Simple and clear.
Program Overview
You can download the Windows distribution of the program
here or compile it from source by downloading it from
github . Just unpack them not on the desktop, as many do. The distribution kit, along with all the dlls, weighs 16 MB in a packed form and 40 in unpacked - Qt5 is significantly over-fat and even the simplest program requires dragging a whole mass of libraries. But I think that this is not a problem, especially since it does not affect the speed of the program.
Unzip the program in any folder on the disk and run. It will create a config.xml configuration file in its folder, and then, when saved, a clean database for your base.xml code. Actually, what the main menu looks like - you can see on the screen in the title of the article. The program interface is all in English so that there are no problems with the build on some systems - messages and signatures so far in the source code.
A new code snippet is added using the global hotkey
ctrl + D , and the search is done using
win + V. You can throw a stone at me for such “global” hotkeys, but experiments have shown that they are the most convenient. Yes, you are probably used to send ctrl + D code from QtCreator somewhere. If this is so critical, you can go into the constructor of the CodePaster class and change hot tags there for those that please you. In the future, this can be done through the GUI.
We continue. Suppose you have written some kind of convenient algorithm, or have mastered a new design of a language that you don’t have any desire to type every time. You simply select your block of code in your favorite IDE (for example, in creator) - and copy as usual via ctrl + C. Here it will be convenient and very close - without releasing ctrl - click on D. A window will pop up over your IDE - and the code will jump into it already! The program automatically reads the clipboard, pasting code from it. There are 2 more small features here - the signature is inserted in front of the code in the form of the comment "//", and the extra indents are removed from the code itself. Those. if you copied it from the middle of the program, it will move to the left until it stops.

You just click "OK" - and your code is already in the database! Moreover, it was immediately saved to the hard disk in order not to lose data in case of any failure.
Next - when you wanted to quickly copy this code - press win + ctrl. Why him? It seems to me a convenient option and is not involved anywhere. You will see a small search engine on the code, and from there you can copy the fragments you need. As for the search engine itself, each piece of code has a context menu. You can remove it from the database, you can edit it, or you can view its properties.


As you can see, the code fragments are separated by "---", each has its own id, as well as
small buns - the program remembers the date of addition and how many times it was copied.
We had to work a little to match some text block inside QTextEdit to some Code object - when the search is updated, a “map” is formed - a sheet of special objects, each of which stores the starting and ending position in QTextEdit and a pointer to the code object that corresponds to this area.
By the way - the program can be used not only for the code, but in general for any text. For example, I very successfully scored my mailboxes and telephone there, copying them when necessary. It turns out something like a notebook with quick access and search.
Examples
Let us give a simple example of using the program - if you suddenly need to throw a message box for debugging in your program (namely, not qDebug output) - having prepared the code beforehand, you simply type win + V and drive in “mes” - this is enough to find the right piece code and copy it. You will spend one hotkey press, a set of 3 characters, Enter (which can be removed in the future), and selection-copying. Now let's compare - how much time will you spend to copy it from the same assistant? Even if it is on the global hot key - you have to get focus for the search line through the selection or keys, type “mess” there, select the desired class with the mouse (!), Scroll down to the place where there is an example (if it exists) and then copy him. It is at least 2 times longer. Qt Assistant is just the perfect documentation, but it is not suitable for copying and pasting constantly used pieces of code.
What options do you offer? Go to
pastebin every time and search there? This is also an extra delay. Enter in Google the same requests 100 times? But the majority of programmers with whom I spoke on this topic - and they did it - they google it every time. But you found it once - you can save it to some “cache” for quick access, change it if necessary - and use it for health! This cache should be a program that is well integrated with the system for greater convenience.
Let's give another example. I often sit on the
prog.org.ru forum and constantly find or get a lot of good solutions to various small tasks. For example, using QSettings to add a program to Windows startup. Or how to dynamically load the ui-form. There are whole algorithms. Every time to look for something found once - this is not comfortable. Nice and beautiful solutions nice to save for yourself for future use.
Another example - I once saved the code for the bat-file, which adds a variable to the windows environment variables. When it is needed, I simply copy it to the command line instead of using an inconvenient GUI.
Of course, you can also offer to save code snippets to files, but as soon as the question of quick access and search, as well as convenient organization of such files arises, you will see that this is not the best solution. Large chunks of code — entire libraries and classes — are of course worth to be stored in this way, but these are small fragments that we often use.
If you work with several programming languages ​​at once, the advantages of using such a program will become even more significant. You can forget how this or that construction looks in a certain language - and through the search it is easy to remember.
Source Code Overview
I wrote the source code strictly according to the
Qt Coding Style standard. At times I used C ++ 11 constructs. I think when
reading it, there will be no problems. All windows are based on widgets, but then I plan to translate them into QML.
In general - do not judge strictly. This is my first public project and I hope that at least someone will come in handy if not a program, then those moments that I have outlined here. If the program turns out to be really useful and you add it, then I can modify it. The plans are to make it even more convenient (there are ideas), syntax highlighting for other languages, and support for hotkeys for Linux \ Mac.
Also, I do not exclude that somewhere in the article or in the code there are typos and / or errors. I will be glad to accept great criticism.
So - I am waiting for your feedback!