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:
- The values that are to be used in a formula report to properties of items. The items are already created and have a list of properties with which I populate a QTableWidget.
- By dragging/droping from the table to a QTextEdit, I'm creating a string with some rich-formating to help the users read the formula more intuitively. Basically, each item on the table has a certain color, so in the QTextEdit, when an item is dropped, it gets a
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:
- some items have properties with the same name. If I create the formula with two different properties (from different items) that have the same name, I can't track the property's value backwards without ambiguity - I was thinking of string compare/string replace the property's name with its value and then calculate with
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
:
- I want him to see
property1
* property1
- but notice that the background-color of each should be different;
- I could place in the QTextEdit something like:
<span style='background-color:red;'> property1 </span> * <span style='background-color:blue;'> property1 </span>
- what I actually want to evaluate (to calculate) is:
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?