Saving user's data for my application - c++

I was wondering what would be the correct method for saving all user data for an application I am working on. The application is in QT. The user inputs a lot of data into the application and the data will be different for every user. I want the ability for the user to save all the current data to a file that can be user by the loaded by the application again once the user wants to use it again or use it on another computer running the application.
What would be the correct and best way to do this? Do I need to use xml format? And then use the xmlreader for QT? Or do I just need to create my own file format and just use the stream to just read everything in. The data in the file will need to be labeled, because it will need to put the data in certain spots on the gui. And the user has the option to dynamically create boxes and tabs that hold certain information.
If you need any more information, please let me know.
A short example:
I am not only reading gui locations.
But the contents of those. For
instance. The user is able to create
tabs that contain edit text boxes. And
those tabs are associated with items
that are in a list. When the user
clicks on an item in the list the user
will be presented with a whole set of
new tabs. And each tab has some
editing forms. The file will need to
contain what is in the list, what tabs
the user has created under each item
in that list and the contents of each
tab associated with the tab of each
item in the list.

In essense, yes you'll be creating your own file format, but the actual content can just be XML in whatever scheme you need. Then you can use Qt's built-in XML processing capabilities to pull the heavy lifting of parsing the text (I personally prefer the DOM model, so I use QDomDocument as my base point), and you'll just need to worry about parsing things to and from the individual nodes.
The Qt framework has some great XML samples if I remember correctly that helped me get off the ground almost immediately. Hope they help!

Another great solution is to use internal database implementation (QSQL on top of sqlite). Compared to the xml solution, it might be more versatile (update when needed, can use external keys). Qt has some rgeat examples about using it aas well.
In terms of dependencies, XML solution will require you to use xml and xmlpatterns (if you want to validate stuff), whereas sqlite solution will require QSQL + sqlite plugin. I think that sqlite guarantees atomicity of writing , thus preventing corruption of data (think : the user is killing the app while it's saving).

Related

Seeking Qt5 WYSIGWG editor design advice

I am planning a new desktop application. It will provide a WYSIWYG editor for HL7
files (OSS Project). HL7 is a structured file format often used by hospitals for
exchanging data between systems.
The basic structure of the format comprises of records which are \r
delimited. A record can have N fields which are | delimited. A field itself
can be sub-devided into components ^ and sub-components & and
fields can be repeated ~ (similar to an array). Every message type has a
different number of fields/components and sub-components. Empty fields at the
end of a record can be omitted.
Example of a simple record:
OBX|14|NM|0050–5^Calcium||8.9|mg/dl|8.4–10.4||||F
I have already implemented an efficient parser which turns a whole file into a
hirarchial data structure in C. I want to implement an application that
allows editing these files like in a text editor. I want to keep the underlaying
hirarchial datastructure at any point, so it is easy to validate structure and
content of every field, upon user changes, quickly.
Also note worthy is, that I already implemented a viewer where the document
strcuture is displayed in a QTreeView. The structure is using a QAbstractItemModel.
Now to my actual question: how would I approach the problem of having a text editor
with a structured data model in Qt. I have done some research:
there is a QDomDocument, it seems to be made to work with xml data. I could convert my structure into XML but can the dom object be used with a text editor ?
Is there a way to bind a tree like document model to QTextEdit or QPlainTextEdit?
is it possible to bind a QAbstractItemModel to a document in QTextEdit or QPlainTextEdit?
What would be the best approach to tackle the problem of having a textual representation of a tree that gets updated once text and/or stricture is changed by editing in the text field?
Performance and cross platform capability is important, so this project will be implemented in C++.
Thanks for any advice and examples appreciated if you have any.
-S

Keping history in Winforms C++

I have an application made in winform using C++ (developed in VS 2010). The GUIs have certain text fields, radio button, check boxes etc. To operate the software one has to fill in these fields/buttons/boxes etc.
There are roughly such 50 different GUIs, having roughly 20 fields in each one.
I want that after the application is closed, and restarted, most recent parameters in this fields automatically fill in, so the user do not need to re-enter all those values again.
What is the easiest and simplest way to achieve this?
This is best solved using MVC pattern where the Model contains the data that was filled. The view contains the way it will be presented(such as in Winforms) And lastly the controller which besides doing business logic will need to do some work (for saving the state).
I prefer using serialization for this. You can serialize the model fields and read them back(fields that are necessary only).
If you don't like serialization you can try writing to INI files that will be easy to modify.
Thirdly you can use a database mdf file to store the state.
But to do all this well you will need to modify the App to use MVC architecture.
You can use the Windows Registry with
WindowsFormName (subkey)
---------> ControlName (subkey)
-----------------> ControlValue (subkey)
When the Form is Closing you save the values for each control in the Registry
When the Form is Loading you read the values
If you are satisfied with a quick and dirty solution you can try this:
Implement a save mechanism:
Iterate to all the form controls (recursively if there are panels or
other containers)
If the control is edit (or other type that holds
data) save the control text together with his name
Store the name,
value pairs (INI file for example)
Implement a load mechanism. This assumes that there are no dependencies between values (If there are automated computed fields do not save them)
Load the values from the storage (if you choose files the file name can be the form name)
iterate to all the form controls and if you find its name in loaded data set the value
Once implemented this approach can be used for all the forms and it will cope with adding/removing controls. However if there are business rules maybe is better to use a MVC approach and serialize the model.

Saving user's data for my application part 2

My first question was: should I use dom, sax, or sqlite to save the data the user is inputting into my application. The answer I am going with is to use DOM.
My second question is: How should I load the contents of the file into the application when the user decides to open the file? Should it go through the whole file and distribute all the data to the correct spots in the GUI once the user clicks "open" on the file? Or should it only open the stuff up as the user clicks on certain areas?
My third question is: How does qt handle knowing when things have changed? How would i know when the user has changed something and ask them to save the file?
If you do not understand, please let me know and I will try to explain again.
Example:
I am not only reading gui locations.
But the contents of those. For
instance. The user is able to create
tabs that contain edit text boxes. And
those tabs are associated with items
that are in a list. When the user
clicks on an item in the list the user
will be presented with a whole set of
new tabs. And each tab has some
editing forms. The file will need to
contain what is in the list, what tabs
the user has created under each item
in that list and the contents of each
tab associated with the tab of each
item in the list.
Sorry that I posted another question that is similar to my last, but the other question was answered and now I need a new post.
Question 2: This very much depends on how much data you're dealing with. It will be much easier to load everything in one step. If you are expecting complex documents, it might be better to do it incrementally, but I would strongly recommend starting with the simpler approach.
Question 3: Qt does not handle this, except in as far as widgets will fire signals when they are modified. You need to do it, using a model of some sort. You could just use the DOM document directly as the model, although it may help maintainability to abstract the save format. Each change the user makes would cause a change in the model. You will need to detect when e.g. the user edits some text, update your model appropriately and keep track of whether it has changed since the last save.
What do you want to achieve with your solution? If you want to simply set Configuration why not using a simple Ini file (QSettings Class).
I don't know your application, but you should be able to recognise changes (lets say, if the user changed a QLineEdit or hit a radioButton).
There would be also a "sync" method for QSettings, which "rereads" the file you are working with. Qt won't recognise changes itself, you have to do that on your own.

How do you save data in MFC?

I still remember in Delphi, developer can just make the UI(textbox, listbox...) directly connect to database, and then when user click a button, just call the post action, then the data will be saved automatically.
What I want to know is that is there any similar mechanism in MFC? Or I can use GetDlgItem(...).Text and then use this value to save to database ?
Or any other suggestions will be appreciated.
In VC++ , you have to use Microsoft ActiveX Data Object Library (ADO typelib) .
To store data you can follow these steps:
1.Retrive data from all controls
2.Validate the data retrived
3.Use sql query to store the data to database.
You can use ODBC API which is independent of any database management system.
http://msdn.microsoft.com/en-us/library/ms714562(VS.85).aspx
http://www.odbc.net/api/index.shtml
To be fair on Delphi these are specialized widgets, not the ordinary GDI textbox etc, but controls with an additional database aware layer that are connected to dataset and tables.
.NET has something similar concepts too, don't know about MFC
MFC's abstraction of data in Doc/View/Frame is in CDocument. When you save the document, MFC prompts the user for the file name if the file does not have a saved path, then construct a CArchive on the file and triggers CDocument::Serialize. You can store the connection string in your document class and use it to save data in CDocument::Serialize.
If you have a file based database, it is easier to integrate. Override CDocument::OnNewDocument to create a new file based database for the document, and
override CDocument::OnOpenDocument to read from existing database. If you don't have a file based database, you can suppress the file dialog with a CDocument::SaveModified override that saves the data and clears the modified flag.

XSL TreeView Define whether the xsl div is open/closed

I'v done a tree view in xsl using a javascript function
I want to change the icons depending on the status (+ for to open , - for to close)
This questions is as clear as thick molasses in a pool of mud. (Will try to answer, though.)
I assume you mean XML stylesheets with xsl. If you meant Excel, it should have been xls. But let's assume you mean stylesheets and you're using it to generate a webpage which contains a treeview. In this treeview there are icons indicating if the node is expanded or not. If expanded, display +, else display -. Am I right, here?
Now, it depends on how you've implemented this treeview in your stylesheet. The most practical way would be to just send the tree data fully expanded to the page and let the Javascript handle this client-side. In that case, all you need to know is how to expand and collapse nodes in Javascript with the additional icon change.
Another possible implementation would be when expanding and collapsing is done serverside, thus you'd only send the visible data. In that case you can also just tell the page which icon to use and there would be no need for any javascript. The icon would just be a link back to the server, updating the data through a new requests which builds a new webpage.
A third option would be the WEB 2.0 solution, where you just send the list as a collapsed treeview and every time the user clicks an icon, a AJAX event gets triggered, collecing the additional node data and changing the icon of the treenode.
These are three very different techniques and they're not always the best solution. The first solution is a problem when dealing with lots of data in your tree. It needs to load it all. The second option will generate a lot more traffic with the server but handles better with large amounts of data because you only display the open node. The third option is a bit of a mixture between the first two options. You don't need all data from the beginning and you're not recreating the webpage over and over again. But it's also more complex to code.
Now, I wonder which of these options you use. Once we know this, we can help you. (Edit your question to provide this information and perhaps even add the JavaScript tag to it.)
To be honest, xsl is only used to change the shape of an XML document and it knows nothing about treeviews or whatever. So I don't see any link between xsl and treeviews. It's just that you use xsl to transform your data into something that some Javascript library can process as a treeview. Which Javascript library is this?