I am building an application that requires a graphical interface for a calculation process. The calculation is basically a formula written by the user to be evaluated with QScriptEngine - ie, Javascript.
The thing is I'm having trouble breaking down the problem into smaller steps. The general objectives are as follows:
background-color
formatting with the same color, and displays the property's name; something like: <span style='background-color:red;'> propertyName </span>
The drag/drop interface as well as the formating is coded and working as expected. But now I have a few problems:
evaluate()
.I have been breaking my head on how to get around this and I thought of creating a QMultiMap
to hold the item: [property, value]
relationships, and then, replacing on the string that will be evaluated. But again, I would need to check form which item the property came from, and I don't know how I can do that.
I'm new to Qt/C++ and I know that most of my code has some big faulty practices, and its being done more in the way of hacking my way through the objectives I require, more than building a good structure - so every new problem gets a more complex solution each time.
Even so, how would you suggest I should tackle this problem? By this time I think it is better to not post my code just yet, because it is too long (and probably painful) to look at. If someone requires a specific portion to better understand the context of the problem, let me know and I'll post here.
Also, I have had other question here in SO when I started to think about this - might be useful to check for context: here.
UPDATE:
In reply to @Riateche's comment:
Imagine this scenario:
Item A : [property1, value1]
[property2, value2]
Item B : [property1, value3]
[property2, value4]
Now, imagine the user wants to perform ItemA.property1 * ItemB.property1
:
property1
* property1
- but notice that the background-color of each should be different;<span style='background-color:red;'> property1 </span> * <span style='background-color:blue;'> property1 </span>
value1
* value3
- in which these represent double types.UPDATE 2
After thinking a little bit about this, while @Riateche's approach seems simple, I wasn't able to find a way change a tag's attribute (at least in rich text, maybe there is one with QWebkit, but that is not what I need). So I was thinking if building another string (that will be evaluated), at the same time the user builds a string with drag and drop. For instance, let's imagine the user drags and drops something like:
property1
* property1
At the same time I would build other string that contained
value1
* value3
And this would be the evaluated string. Even so, the problem with the user editing the string would still be there - if the user changes the drag/drop string, I need to update the evaluation string again -requiring me to once again check the origin of the data. Any other ideas?
You should put all information important for the formula evaluation to the text edit. You can make it invisible to user. For example, you can put the following to the text edit:
<span style='background-color:red;'><a name='Item A,property1'></a>property1</span>
The Item A,property1
text will be invisible to user, but textEdit->toHtml()
result will contain it.