Stripping Diacritics in Qt

As someone dealing with languages different than English, I quite often need to deal with diacritics. You know, characters such as č, ć, đ, š, ž, and similar. Even in English texts I can sometime see them, e.g. voilà. And yes, quite often you can omit them and still have understandable text. But often you simply cannot because it changes meaning of the word.

One place where this often bites me is search. It’s really practical for search to be accent insensitive since that allows me to use English keyboard even though I am searching for content in another language. Search that would ignore diacritics would be awesome.

And over the time I implemented something like that in all my apps. As I am building a new QText, it came time to implement it in C++ with a Qt flavor. And, unlike C#, C++ was not really built with a lot of internationalization in mind.

Solution comes from non-other than now late Michael S. Kaplan. While his blog was deleted by Microsoft (great loss!), there are archives of his work still around - courtesy of people who loved his work. His solution was in C# (that’s how I actually rembered it - I already needed that once) and it was beautifully simple. Decompose unicode string, remove non-spacing mark characters, and finally combine what’s left back to a unicode string.

In Qt’s C++, that would be something like this:

QString stripDiacritics(QString text) {
    QString formD = text.normalized(QString::NormalizationForm_D);

    QString filtered;
    for (int i = 0; i < formD.length(); i++) {
        if (formD.at(i).category() != QChar::Mark_NonSpacing) {
            filtered.append(formD.at(i));
        }
    }

    return filtered.normalized(QString::NormalizationForm_C);
}

Poured Potatoes (Užljevak)

Illustration

Užljevak is one of my favorite childhood foods. Due to it’s simplicity and the low cost, this was on our table at least once or twice a week. It’s a peasant food using only pantry ingredients and it’s quite forgiving when it comes to preparation.

This recipe is one of the simplest užljevak recipes I know but it’s definitely not the only one. My mother alone used a few different recipes and you’ll find probably every Bosnian family has another few. They all will taste similar though so trying one will give you an idea what you’re working with.

Ingredients

Quantities given are just for the orientation purpose and can be fudged quite a lot while still getting an excellent result.

  • 600 g Russet potatoes (about 3 big ones)
  • 300 g Flour (about 14 spoons)
  • 1 Yellow onion
  • 3 dcl Water
  • 2 dcl Milk
  • 1 dcl Oil
  • 25 g Salt (1½ tablespoon)
  • 1.5 g Black pepper (⅔ teaspoon)

Instructions

Peel potatoes and cut them into small square pieces (0.5 cm side). Leave them in a cold water for about 10 minutes and then drain the water to remove (some) starch. While potatoes are soaking, finely dice onion. The finer, the better.

Mix all the ingredients (including oil) together and you should end with a runny mixture similar to what you would use for crepes (a slightly less dense than a pancake mix). You can fine tune it by adding flour or water but the end result will be good as long as you are in a ballpark. Leave it alone for 10 minutes.

Use that time to preheat the oven to 220 °C (425 °F) with a well-oiled wide pan inside. You can also go with parchment paper but in that case preheat only pan and place parchment only when you are ready to pour.

Once oven is at temperature get the pan out (place parchment paper if that’s your groove) and pour the mixture into the hot pan. It should be about 2-3 cm in height. Be very careful as pan will be really hot. Once poured, get it immediately into the oven.

Bake for 1 hour and then turn on a broiler for another 5 minutes. Once browned, it’s ready to go out.

Let it rest for 10 minutes, cut it into square pieces, and enjoy it with some yogurt on side.


PS: Some would say the only way to eat užljevak iz with homemade sour milk. Getting yogurt is not quite the same taste but it’s definitely easier.

Renaming Master Branch to Main

Illustration

GitHub is making a major change to the default branch name. As of October 1st, the default branch will be called main instead of master. While this is done just for the new repositories and the official recommendation is to wait until the end of year for the existing ones, I was never the one to follow the rules.

To locally change the name of the branch, you just need to move it.

git branch -m master main

Next step is telling GitHub you have a new branch:

git push -u origin main

If you go to GitHub now, youl’ll see both main and master present with master still being the default. To change this you’ll need to go into the repository settings and switch default branch there.

Only once that step is done, you can delete master branch forever.

git push origin --delete master

Now the existing repository now has main as the default branch name.

As you can see, currently this process is a bit involved and I am sure that GitHub will automate it reasonably soon. You might want to wait with local renames until they do. My plan is to update branch names as I push updates to my projects. Active repositories will get the update sooner while some old repositories might stay with master forever.

That’s all fine and dandy but what about the new repositories? Well, there’s a setting for that too. Just adjust init.defaultBranch Git property.

git config --global init.defaultBranch main

And now you’re ready to roll.


PS: I will not get into politics whether this change was necessary or not. As a Croat, I will never fully understand the slavery and the emotional impact having the master branch might have. For me this change is more of pragmatism. The exact name doesn’t matter much to me and main is a better choice anyhow.

Headless VirtualBox on Ubuntu Server 20.04

As expected, first we need to install VirtualBox.

sudo wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
echo "deb [arch=amd64] https://download.virtualbox.org/virtualbox/debian focal contrib" \
    | sudo tee /etc/apt/sources.list.d/virtualbox.list
sudo apt update
sudo apt-get install --yes virtualbox-6.1
sudo systemctl status vboxdrv

sudo usermod -aG vboxusers $USER

Since our server is headless, we will need a remote acces. This means installing Oracle’s extension pack with RDP support. You should be good installing this on your home system but you will need license for production deployments.

VBOXVER=`vboxmanage -v | cut -dr -f1`
wget -P /tmp \
    https://download.virtualbox.org/virtualbox/$VBOXVER/Oracle_VM_VirtualBox_Extension_Pack-$VBOXVER.vbox-extpack
vboxmanage extpack install /tmp/Oracle_VM_VirtualBox_Extension_Pack-$VBOXVER.vbox-extpack

With installation ready, we can approach creating VM. I’ll give an example of the Ubuntu Desktop but really anything goes.

mkdir -p /srv/virtualbox

vboxmanage createvm \
    --ostype Ubuntu_64 \
    --basefolder "^^/srv/virtualbox^^" \
    --register \
    --name "^^Test^^"

vboxmanage modifyvm "^^Test^^" \
    --memory 1024 \
    --nic1 nat \
    --vrde on --vrdeport ^^33890^^

vboxmanage createhd \
    --filename "^^/srv/virtualbox/Test/Test.vdi^^" \
    --format VDI --size ^^10240^^

vboxmanage storagectl "^^Test^^" \
    --name "SATA" \
    --add sata

vboxmanage storageattach "^^Test^^" \
    --storagectl SATA --port 0 --type hdd \
    --medium "/srv/virtualbox/Test/Test.vdi"

vboxmanage storageattach "^^Test^^" \
    --storagectl SATA --port 15 --type dvddrive \
    --medium ^^/tmp/ubuntu-20.04-desktop-amd64.iso^^

If you are using firewall, make sure to allow port through. For example, iptables would need the following adjustment.

iptables -A INPUT -p tcp --dport 33890 -j ACCEPT

With this, we’re finally ready to start the virtual machine.

vboxmanage startvm "^^Test^^" --type headless

To connect to it just use any Remote Desktop application.


PS: Authentication for RDP is highly recommended and you should probably check documentation to see what works best for you.

Printing Large Objects on 3D Printer

3D printers for me are often solution in search of a problem. This is especially true in the lower price bracket where often you can spend significant amounts of time and material trying to get a perfect print. But boy, they are a lot of fun.

And when it comes to wasting time, I found getting acceptable print for objects with large footprints is really a drag. And looking up what worked for others is not as straightforward as giving a recipe since setup will depend on the printer, filament, slicer, and bunch of other small variables. I will share here what works for me 90% of the time on Ender 3 Pro with MatterHackers Build PLA and using Cura as a slicer.

When it comes to Cura, I love Standard Quality setting. While Ender 3 can perform well with the higher quality for small items, quite often printing with less than 0.2 mm extrusion is finicky and requires quite a lot of care. With 0.2 mm you won’t necessary get the best it can offer but it usually won’t cause any issues either.

Having a heated bed is pretty much mandatory for relaxed printing. I just set Build Plate Temperature to 60 °C for PLA. There is actually some room to go higher but going too wild will often make bottom layers unevenly shrink as they cool down.

Extrusion temperature depends on the filament and every manufacturer has a preferred range. For MatterHackers Build PLA that range is 180-220 °C. I set Printing Temperature smack in the middle to 205 °C. I set Printing Temperature Initial Layer a bit higher to 215 °C as it really helps with the initial adhesion.

While fan is awesome I find it cools stuff way too fast at the full speed. I just set Fan Speed to 50% and that seems to work nicely. Of course Initial Fan Speed is left at 0%.

For bigger objects I always change Build Plate Adhesion Type to use Raft. While smaller objects work just fine with Skirt, I often left large print overnight only to find them messed-up in the morning because edges started lifting off. You can also avoid this by adjusting temperature, using better surface, or some type of adhesion. However, I prefer the raft to any of those alternatives as it works even when the other settings are a bit off.

I also like to increase Initial Layer Height to 0.4 mm as it helps with removing model from the raft but that comes at a cost of slightly rougher bottom layer. I find that a worthwhile exchange. If PLA is misbehaving and I get “stringy” bottom, I might also increase Initial Layer Line Width to 150% or 200% but mostly I leave it at 100%.

From larger objects I expect a bit more of structural stability so I change Infill Pattern to Gyroid with Infill Density of 40%. I usually don’t go higher but, if I don’t need print to be sturdy or object is a bit smaller, I might go as low as 10%.

Some models might require supports and here I found Cura settings way too conservative. I always increase Support Speed to 50 mm (matching my print speed) and I lower Support Density to 10% so removal is easier. With Ender 3 Pro you can quite often go more aggressive but I found 50 mm works so well with whatever I throw at it that I don’t bother going higher.

As matter of preference I set Combing Mode to Off as I prefer “rougher” look of the final layer. I also set Z Hop When Retracted as it seems to work better with thin walls.

All these settings, while not perfect for any particular print, fail me so rarely that I have them set as a default and change them only if there is something special I am going for.