Qt qsTr() handling plurals - qt

Im developing and applicacion and want to translate some text, the problem is when i want to handle in QML the plurals.
In c++, the way to handle the plurals is as simple as:
int n = messages.count();
showMessage(tr("%n message(s) saved", 0, n));
and will translate without problems
source:
https://doc.qt.io/qt-5/i18n-source-translation.html#handling-plurals
When I try to do the same with QML doesnt work. After carefull review of some literature, and some comments I found out a "solution", that it's actually people reporting a bug.
var second = qsTr("%b radios", "0", map.radio).arg(map.radio)
source:https://bugreports.qt.io/browse/QTBUG-11579
When I lupdate, in the QtLinguistic it appears the two fields for plural and singular form, but in the application does not work.
I tried several modifications such as:
var a = map.totalSongs;
var first = qsTr("%a songs", "0", parseInt(a))
var second = qsTr("%b radios", "0", map.radio)
var first = qsTr("%a songs", "0", parseInt(a)).arg(map.totalSongs)
var second = qsTr("%b radios", "0", map.radio).arg(map.radio)
var first = qsTr("%a songs", "0", a)
var second = qsTr("%b radios", "0", b)
In QtLinguistic im writting the translation:
%b radio - Singular
%b radios - Plural
Any modification fails to work.
Can some one tell me how to use qstr() to handle the plurals?
Other question related:
Lets say I want to have a text "%1 songs - %2 radios", where in spanish should result in
//As example
if(%1 = 10 && %2 = 10) => "10 canciones - radios"
else if(%1 = 1 && %2 = 10) => "1 cancion - 10 radios"
else if(%1 = 10 && %2 = 1) => "10 canciones - 1 radio"
How to do it? I think either qstr() or tr() can not handle this situation., but just want to verify it with you guys :D
Thanks in advance

I couldnt accept that was not working so I went a little further and find a solution, maybe seems obvious, but I dont think its so.
Doesnt work
var a = map.totalSongs;
var first = qsTr("%a songs", "0", a)
Works because we use the variable N
var n = map.radio;
var first = qsTr("%n songs", "0", n)

Related

tower of hanoi initial configuration?

I am reading the code my friend wrote for his tower of hanoi solution. But it's hard for me to figure out what his code does since I don't understand his init configuration and ending configuration .
def T(init, final):
if len(init) == 0:
return
if init[0] == final[0]:
T(init[1:], final[1:])
else:
fro = init[0]
to = final[0]
spare = other(init[0], final[0])
ic = spare * (len(init) - 1)
T(init[1:], ic)
print("move from %s to %s " % (fro, to))
T(ic, final[1:])
def other(char1, char2):
towers = "ABC"
towers = towers.replace(char1, "")
towers = towers.replace(char2, "")
return towers
init = "ABCBA"
final = "BCBAC"
T(init, final)
Here, he has init = "ABCBA" and final = "BCBAC". The code works fine but I don't get why he is doing this.
Any help is appreciated.
init and final configurations are just the order of disks' size from large to small and their respective rod, denoted as a letter (A, B or C in this case).
init = "ABCBA" is when you have largest disk at 'A', second largest at 'B', third largest at 'C' and so on.
Say, you have
init = "AB"
final = "AA"
the program would output
move from B to A
since you have the smaller disk sitting at B, all you have to do is to move it to A to obtain AA.

Image compare autoit [duplicate]

I am looking for a way to find duplicate images using AutoIt. I've looked into PixelSearch and SearchImage but neither do exactly what I need them to do.
I am trying to compare 2 images by filename and see if they are the same image (a duplicate). The best way I've thought to do it would be to:
1) Get both image sizes in pixels
2) Use a while loop to get the color of each pixel and store it in an array
3) Check to see if both arrays are equal to each other.
Does anybody have any ideas on how to achieve this?
I just did some more research on this subject and built a small UDF based on a few answers I read. (Mainly based off of monoceres's answer on AutoItScript.com). I figured I would post my solution here to help any future developers!
CompareImagesUDF.au3
Func _CompareImages($ciImageOne, $ciImageTwo)
_GDIPlus_Startup()
$fname1=$ciImageOne
If $fname1="" Then Exit
$fname2=$ciImageTwo
If $fname2="" Then Exit
$bm1 = _GDIPlus_ImageLoadFromFile($fname1)
$bm2 = _GDIPlus_ImageLoadFromFile($fname2)
; MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
Return CompareBitmaps($bm1, $bm2)
_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
_GDIPlus_Shutdown()
EndFunc
Func CompareBitmaps($bm1, $bm2)
$Bm1W = _GDIPlus_ImageGetWidth($bm1)
$Bm1H = _GDIPlus_ImageGetHeight($bm1)
$BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
$Stride = DllStructGetData($BitmapData1, "Stride")
$Scan0 = DllStructGetData($BitmapData1, "Scan0")
$ptr1 = $Scan0
$size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4
$Bm2W = _GDIPlus_ImageGetWidth($bm2)
$Bm2H = _GDIPlus_ImageGetHeight($bm2)
$BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
$Stride = DllStructGetData($BitmapData2, "Stride")
$Scan0 = DllStructGetData($BitmapData2, "Scan0")
$ptr2 = $Scan0
$size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4
$smallest = $size1
If $size2 < $smallest Then $smallest = $size2
$call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)
_GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
_GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)
Return ($call[0]=0)
EndFunc ;==>CompareBitmaps
Now to compare imagages, all you have to do is include the CompareImagesUDF.au3 file and call the function.
CompareImagesExample.au3
#Include "CompareImagesUDF.au3"
; Define the two images (They can be different file formats)
$img1 = "Image1.jpg"
$img2 = "Image2.jpg"
; Compare the two images
$duplicateCheck = _CompareImages($img1, $img2)
MsgBox(0,"Is Duplicate?", $duplicateCheck)
If you want to find out if both images are an exact match, regardless if names are the same or different, use the built-in Crypt function _Crypt_HashFile with MD2 or MD5 to make a hash of both files and compare that.

Pyqt bind function to dinamically generated push buttons

Using the PyQT library I am currently having issues with binding a function to the dinamically generated pushbuttons resulting from a loop.
So far I have managed to generate the buttons and bind a function to them with the lambda command. The problem is that since I need every single button to open a different file I find myself in an odd situation as all the button do open the same one. The last value assigned to the variable.
Any idea on how to fix the situation? As a last note, sorry in case of stupid mistakes. I am new to OOP and PyQT.
def searchTheStuff(self):
found = 0
data = MainWindow.intermediateCall()
f1 = open('Path.txt', 'r')
path = f1.read()
f1.close()
Yinc = 25
Y = 40
X = 20
results = 0
for path, dirs, files in os.walk(path, topdown=True):
for name in files:
if name.endswith('.txt'):
fullpath = os.path.join(path, name)
mail = open_close(fullpath)
if mail.find(data) != -1 and results<3:
found = 1
self.buttons.append(QtGui.QPushButton(self))
print fullpath
command = lambda : webbrowser.open()
self.buttons[-1].clicked.connect(command)
self.buttons[-1].setText(_translate("self", name, None))
self.buttons[-1].setGeometry(QtCore.QRect(X, Y, 220, 20))
results = results+1
if results == 33:
X = 260
Y = 15
Y = Y + Yinc
if found == 0:
self.label = QtGui.QLabel(self)
self.label.setGeometry(QtCore.QRect(20, 40, 321, 21))
self.label.setText(_translate("self", "No templates have been found:", None))

How would you index a table that is being initialized?

An example of what I desire:
local X = {["Alpha"] = 5, ["Beta"] = this.Alpha+3}
print(X.Beta) --> error: [string "stdin"]:1: attempt to index global 'this' (a nil value)
is there a way to get this working, or a substitute I can use without too much code bloat(I want it to look presentable, so fenv hacks are out of the picture)
if anyone wants to take a crack at lua, repl.it is a good testing webpage for quick scripts
No there is no way to do this because the table does not yet exist and there is no notion of "self" in Lua (except via syntactic sugar for table methods). You have to do it in two steps:
local X = {["Alpha"] = 5}
X["Beta"] = X.Alpha+3
Note that you only need the square brackets if your key is not a string or if it is a string with characters other than any of [a-z][A-Z][0-9]_.
local X = {Alpha = 5}
X.Beta = X.Alpha+3
Update:
Based on what I saw on your pastebin, you probably should do this slightly differently:
local Alpha = 5
local X = {
Alpha = Alpha,
Beta = Alpha+3,
Gamma = someFunction(Alpha),
Eta = Alpha:method()
}
(obviously Alpha has no method because in the example it is a number but you get the idea, just wanted to show if Alpha were an object).

How do I convert a list of strings into a URL query string in functional style?

This question is about functional programming, not about how to build URL query strings. The language I'm working in is Objective-C but that's also not especially relevant.
I have a starting data structure that's an array of strings. Each odd element is the parameter name, each even one is the value eg, [firstname, bob, lastname, smith, gender, male].
Is there an idiomatic FP approach to converting this list into a URL query string (eg. "firstname=bob&lastname=smith&gender=male")?
I'm thinking something along the lines of 'partition using a mod 2 predicate' to give me 2 lists of keys and values respectively, then zipWith a function that url escapes the keys and values and joins them with '='. That will give me a list of 'a=b' strings. Then I should be able to do a fold-left to insert the '&' symbols.
I think I may have just answered my own question! Sweet! Posting it anyway in case someone more familiar with FP idioms wants to comment.
Thanks!
Here's a more-or-less direct translation of your English into C#:
var list = new[]{"firstname", "bob", "lastname", "smith", "gender", "male"};
string.Join("&",
list.Where((k, i) => i % 2 == 0)
.Zip(list.Where((v, i) => i % 2 == 1),
(k, v) => k + "=" + v));
This would be more or less it, in Haskell using only standard prelude functions:
queryStr paramList = joinAmp $ map (uncurry joinEq) $ group2 paramList
where joinEq x y = x ++ "=" ++ y
joinAmp = foldr1 (\x y -> x ++ "&" ++ y)
group2 [] = []
group2 (x:y:xs) = (x, y) : group2 xs
qStr = queryStr ["firstname", "bob", "lastname", "smith", "gender", "male"]
Check whether your environment has a string joining function. E.g., in Python you can do "&".join(lst) for the joinAmp function, above. I've used a right-fold because of the way Haskell implements string concatenation.
String o;
for (i=0; i< -1 + size(strings); i=i+2) {
if (i==0) {
o += "?";
} else {
o += "&";
};
o+= strings[i]+"="+ strings[i+1];
}
print o;

Resources