aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models/cylindermodel.cpp
AgeCommit message (Collapse)Author
2020-11-24cylindermodel: remove in_planner() useGravatar Berthold Stoeger
in_planner() is problematic, since it is uses desktop-only application state. Since the cylinder-model already has an appropriate inPlanner flag, use this instead. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-09-29Use correct pO2 when computing MOD in equipment tabGravatar Robert C. Helling
The cylinder model is used both in the planner and the equipment tab. We have three preferences for the pO2 that is used to compute MOD: In the planner, there is one for the bottom part of the dive and another one for deco. Those are set in the planenr UI. There is another value, controlled in the Tec Prefernces. That one should be used in the equipment tab rather than the one from the planner. Fixes #2984 Signed-off-by: Robert C. Helling <helling@atdotde.de>
2020-08-21cleanup: consistently use get_cylinder() accessorGravatar Berthold Stoeger
get_cylinder(d, i) is more readable than d->cylinders.cylinders[i]. Moreover, it does bound checking and is more flexible with respect to changing the core data structures. Most places already used this accessor, but some still accessed the cylinders directly. This patch unifies the accesses by consistently switching to get_cylinder(). The affected code is in C++ and accesses the cylinder as reference or object, whereas the get_cylinder() function is C and returns a pointer. This results in funky looking "*get_cylinder(d, i)" expressions. Arguably still better than the original. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-07-11Make MND display depend on O2 narcotic preferenceGravatar Robert C. Helling
A while ago, we introduced a preference whether O2 should be considered narcotic. We used this when computing best mix or when entering the He content via MND. But we forgot to make the displayed MND depend on this preference. This patch add this. Fixes #2895 Signed-off-by: Robert C. Helling <helling@atdotde.de>
2020-05-01core: always keep an empty cylinder at the end of the cylinder arrayGravatar Berthold Stoeger
This will be temporarilly used by the planner to mark consumption of air at the surface. Do this by creating a new function add_cylinder, which replaces add_to_cylinder_table() and takes care of always adding a dummy cylinder at the end of the table. Make the original add_to_cylinder_table() local, so that it cannot be accessed anymore. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-21cleanup: replace constructs of the type &vector[0] by vector.data()Gravatar Berthold Stoeger
It appears that some misguided compiler / library combinations crash on &vector[0] for empty vectors. Even though very unfriendly, they are technically correct, so let's remove these constructs. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07cleanup: remove conditional compilation in cylindermodel.cppGravatar Berthold Stoeger
Parts of the code were not compiled on mobile, because they used the undo-command infrastructure. However, since mobile now also compiles that, we might as well remove the conditional compilation. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07undo: show multiple dive warning when editing equipmentGravatar Berthold Stoeger
When editing cylinders or weights directly in the table widgets, no warning was shown if multiple dives were affected. To solve this, emit signals from the respective models and catch them in dive equipment tab. Not very nice, but it works for now. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07undo: more fine-grained editing of cylinderGravatar Berthold Stoeger
Don't overwrite the full cylinder when editing a single field. Implement three "modes": editing of type, pressure and gasmix. Don't consider individual fields, because some of them are related. E.g. you can change the gasmix by setting the MOD. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylindersModel: use flag to decide whether we are in plannerGravatar Berthold Stoeger
On desktop, we have two CylindersModel concurrently: One in the planner and one on the equipment-tab. They act differently, because the former modifies displayed_dive directly, the latter issues undo commands. To differentiate, we used the in_planner() function. However, that appears extremely brittle, especially when combined with undo-commands. Therefore when generating the model, pass in a parameter that says whether this is for the planner or the equipment tab and use that flag to decide how to act. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07undo/cylinder: call undo command to edit cylinder fieldsGravatar Berthold Stoeger
Call an undo command when editing cylinders, but only if on the EquipmentTab. To keep code changes small, make a copy of the cylinder first, then edit the cylinder as before and then either call an undo command (EquipmentTab) or overwrite the old cylinder (Planner). The memory management here is a bit strange: Since the undo-command itself makes a deep-copy of the passed in cylinder, we only do a shallow copy. If we have to change the type, we allocate the string with an std::string, so that the memory is automatically freed at the end of the function. However, this means that in the planner we have to make a deep copy first, swap old and new cylinder and finally release the old cylinder. Certainly not ideal, but for now the pragmatic thing to do. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07undo/cylinders: Implement editing of the typeGravatar Berthold Stoeger
This one is tricky, as when browsing through the types-combobox, the user is presented with presets without actually changing the dive. We do not want an undo-command for every change-event in the combo-box. Therefore, implement a scheme analoguous to the weight-editing: A temporary row can be set / committed or reset. Sadly, the code is more complex because we have to consider the planner, which is not included in the undo system. Firstly, the planner uses a different model, therefore all interactions are channeled through setData() with special roles. Secondly, in the planner we shouldn't place an undo command, but simply overwrite the dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07cleanup: remove unused CylindersModelFiltered functionsGravatar Berthold Stoeger
add() and remove() are not used anymore since this is done using undo commands. The planner uses CylindersModel instead. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylindersModel: fold CHANGED() macro into setData()Gravatar Berthold Stoeger
The CHANGED macro was defined in the cleanerTableModel header. Since it had only one user, expand it there. The macro was very questionably anyway, as it would set the local "vString" variable. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylindersModel: test for CHANGED() outside of switch statementGravatar Berthold Stoeger
A small code consolidation: With one exception, all targets of the switch statement would test for CHANGED(). Instead do the test once and exit early. This changes the behavior of the function: if not changed, there will be no more dataChanged-signal. However, this appears to be the correct thing to do anyway. And it is easily changed if it matters after all. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylindersModel: remove mark_dive_list_changedGravatar Berthold Stoeger
Cylinder-editing is controlled by undo (either by saving a planned dive or by using the equipment tab). There is no point in setting the dive_list_changed flag. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07cylinders: remove CylindersModel::changedGravatar Berthold Stoeger
Nobody is testing that flag anymore. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylindersModel: listen and react to signalsGravatar Berthold Stoeger
React to signals from the undo-commands and update the model accordingly. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylindersModel: don't test for planner-state in remove()Gravatar Berthold Stoeger
This is only called from the planner. Therefore, the test is redundant. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07undo: reorder cylinders on remove-cylinder undo/redoGravatar Berthold Stoeger
The cylinders in the events must be reordered if we remove a cylinder. To avoid duplication of code, move the reordering function into qthelper.cpp, though it might not be ideal there. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07cleanup: remove CylindersModels::rowsGravatar Berthold Stoeger
Access the number of cylinders in the dive directly instead. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07CylinderModel: make dive dynamicGravatar Berthold Stoeger
The CylinderModel always accessed the global "displayed_dive" and in some special cases also "current_dive". To implement cylinder undo, the model should work on an arbitrary dive. Therefore, in analogy to the weight model, make the dive dynamic. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-07core: add create_new_cylinder() functionGravatar Berthold Stoeger
Turn the code in CylindersModel that creates a new cylinder for addition into its own function to avoid code duplication. This will be used from the undo commands. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-03-11Grammar: replaces 'indexes' by 'indices'Gravatar Robert C. Helling
Grammar-nazi ran git grep -l 'indexes' | xargs sed -i '' -e 's/indexes/indices/g' to prevent future wincing when reading the source code. Unfortunatly, Qt itself is infected as in QModelIndexList QItemSelection::indexes() const Signed-off-by: Robert C. Helling <helling@atdotde.de>
2020-02-28cleanup: simplify string-comparison in CylindersModel::setData()Gravatar Berthold Stoeger
Convert to QString instead of a QByteArray. Use qPrintable() instead of data(). This might do one more UTF16->UTF8 conversion. However, this is completely irrelevant, since we don't change the type of a cylinder in a tight loop. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-28cleanup: fix leak in CylindersModel::setData()Gravatar Berthold Stoeger
The C-string cyl->type.description was set without the old data being freed. Free the old string before overwriting the pointer. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-28cleanup: make CylindersModel::cylinderAt privateGravatar Berthold Stoeger
The last outside use of the function was replaced by calls to data(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-28crash fix: Don't cast to CylindersModel or CylindersModelFilteredGravatar Berthold Stoeger
The tank-info-delegate cast its model to CylindersModelFiltered, since this is what the equipment-tab uses since implementing the filtering of unused cylinders. However, the planner users the same delegate and still uses the unfiltered CylindersModel. This means that the (dynamic) cast returns a null pointer and crashes. One possibility would be to derive CylindersModelFiltered and CylindersModel from the same class that defines virtual functions and cast to that class. This is a different attempt: don't cast (i.e. stay with a QAbstractItemModel and play it via Qt's model-view system. Firstly, replace the passInData function by a role to setData(). Secondly, read the working-pressure and size via new columns using data(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-24CylinderModel: remove unnecessary "Is used" headerGravatar Berthold Stoeger
This header is a rebase artifact and introduces a pointless column in the cylinder tables. It was erroneously introduced in 6622f42aab937e72cc11cb5512012394aa687767. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-11Planner: make cylinder-model subobject of planner-modelGravatar Berthold Stoeger
The cylinder-model had an instance() function, but actually there were two cylinder models: one used by the equipment tab, one used by the planner. This is misleading. Therefore, remove the instance() function and make the cylinder-model a subobject of the planner-model. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-11Cylinders: Add CylindersModelFilteredGravatar Berthold Stoeger
When the show_unused_cylinders flag is not set, the cylinder tables in the equipment tab and the planner should not show unused cylinders. However, the code in CylindersModel is fundamentally broken if the unused cylinders are not at the end of the list: The correct number of cylinders is shown, but not the correct cylinders. Therefore, add a higher-level CylindersModelFiltered model on top of CylindersModel that does the actual filtering. Some calls are routed through to the base model (notably those that take indexes, as these have to be mapped), for some calls the caller has to get access to the source model first. We might want to adjust this. For filtering, reuse the already existing show_cylinder function and export it via CylindersModel. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-02-04Cleanup: lower-case filenames in core/subsurface-qt/Gravatar Berthold Stoeger
We tend to use lower-case filenames. Let's do it for these files as well. Simple search & replace. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-01-30CylindersModel: don't crash if accessing non-existing cylinderGravatar Berthold Stoeger
It shouldn't happen, but currently we overwrite the displayed_dive without updating the CylindersModel. Thus, CylindersModel may now crash when the new displayed_dive has less cylinders than the old one. For now, catch this condition. Treat the root cause later. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-01-10code cleanup: introduce empty_cylinder constantGravatar Dirk Hohndel
This deals with the issue of initializing structs in C++. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-11-26Cylinders: Fix crash when removing cylindersGravatar Berthold Stoeger
Change the remove() function of the cylinder and weight models to take the index by value. The code used to take it by reference and the reference would be invalidated when removing rows from the model! Reported-by: Gaetan Bisson <bisson@archlinux.org> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09Cylinders: access cylinders with get_cylinder()Gravatar Berthold Stoeger
Instead of accessing the cylinder table directly, use the get_cylinder() function. This gives less unwieldy expressions. But more importantly, the function does bound checking. This is crucial for now as the code hasn't be properly audited since the change to arbitrarily sized cylinder tables. Accesses of invalid cylinder indexes may lead to silent data-corruption that is sometimes not even noticed by valgrind. Returning NULL instead of an invalid pointer will make debugging much easier. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09Core: remove MAX_CYLINDERS restrictionGravatar Berthold Stoeger
Instead of using fixed size arrays, use a new cylinder_table structure. The code copies the weightsystem code, but is significantly more complex because cylinders are such an integral part of the core. Two functions to access the cylinders were added: get_cylinder() and get_or_create_cylinder() The former does a simple array access and supposes that the cylinder exists. The latter is used by the parser(s) and if a cylinder with the given id does not exist, cylinders up to that id are generated. One point will make C programmers cringe: the cylinder structure is passed by value. This is due to the way the table-macros work. A refactoring of the table macros is planned. It has to be noted that the size of a cylinder_t is 64 bytes, i.e. 8 long words on a 64-bit architecture, so passing on the stack is probably not even significantly slower than passing as reference. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09Cleanup: Make cylinder formatting functions const-cleanGravatar Berthold Stoeger
In qt-models/cylindermodel.cpp the various formatting functions can take a pointer-to-const cylinder. Thus, the data() function can likewise treat the cylinder as const - as it should. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09Cylinders: dynamically allocate cylinder arraysGravatar Berthold Stoeger
When keeping track of cylinder related data, the code was using static arrays of MAX_CYLINDERS length. If we want to use dynamically sized cylinder arrays, these have to be dynamically allocated. In C++ code, this is trivial: simply replace the C-style arrays by std::vector<>. Don't use QVector, as no reference counting or COW semantics are needed here. These are purely local and unshared arrays. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-10-31Planner: Add option to treat O2 as narcoticGravatar Robert C. Helling
When computing the best mix for a target depth, for helium, one can either require that the partial pressure of N2 is the same as at the target depth or the partial pressure of N2 plus O2. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2019-08-08Core: pass dive, cylinder-id to fill_default_cylinderGravatar Berthold Stoeger
The fill_default_cylinder() function calculated the MOD based on the currently displayed dive. This does not seem to make sense: - When importing dives, why would we care about the altitude and salinity of the currently displayed dive, possibly from a different trip. - The planner is supposed to be thread-safe and should not touch global variables. Of course this means that the importing-functions have to fill out altitude and salinity before creating the default cylinder, but this is their problem. For a freshly created dive they will get the default values, which still seems less random than the values from the displayed dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-07-18Cleanup: remove includes from qthelper.hGravatar Berthold Stoeger
To reduce interdependencies, remove the dive.h and divelist.h includes in qthelper.h Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-23Undo: don't send signals batched by tripGravatar Berthold Stoeger
Since the default view is batched by trips, signals were sent trip-wise. This seemed like a good idea at first, but when more and more parts used these signals, it became a burden. Therefore push the batching to the part of the code where it is needed: the trip view. The divesAdded and divesDeleted are not yet converted, because these are combined with trip addition/deletion. This should also be detangled, but not now. Since the dive-lists were sorted in the processByTrip function, the dive-list model now does its own sorting. This will have to be audited. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: return directly from data() methodsGravatar Berthold Stoeger
There is this anti-pattern in QModel data() functions to assign to a "ret" variable and return at the end of the function. This is inefficient, as the object is not directly constructed at the space reserved by the caller. Change the functions in WeightModel and CylinderModel to return the objects directly. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: move trip-related functions into own translation unitGravatar Berthold Stoeger
These functions were spread out over dive.c and divelist.c. Move them into their own file to make all this a bit less monolithic. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Undo: update cylinder and weight models on pasteGravatar Berthold Stoeger
When pasting (or undoing paste) the cylinders or weights may change. Send the appropriate signals and update the models accordingly. Currently, this means copying from current dive to displayed dive, but hopefully we can get rid of "displayed_dive" in the not so distant future. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Dive site: add dive site list tabGravatar Berthold Stoeger
Add a very simple tab-widget presenting the list of known dive sites. The table is rendered using our custom "TableView". The (mis)uses the "LocationInformationModel". It moves the items to be displayed (delete, name, description, number of dives) to the front and makes the others hidden. Moreover, it was necessary to limit the geo-tag decoration role to the name to avoid having the icon next to each column. Make the trash-can icon active and the name and description editable. This is modelled after the cylinders-table code. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-03-29Make sure surface air is not considered a deco gasGravatar Robert C. Helling
it creapt in through the gaschange events. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-08-23Cleanup: pass gasmix by valueGravatar Berthold Stoeger
In a previous commit, the get_gasmix_* functions were changed to return by value. For consistency, also pass gasmix by value. Note that on common 64-bit platforms struct gasmix is the size of a pointer [2 * 32 bit vs. 64 bit] and therefore uses the same space on the stack. On 32-bit platforms, the stack use is probably doubled, but in return a dereference is avoided. Supporting arbitrary gas-mixes (H2, Ar, ...) will be such an invasive change that going back to pointers is probably the least of our worries. This commit is a step in const-ifying input parameters (passing by value is the ultimate way of signaling that the input parameter will not be changed [unless there are references to said parameter]). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-06-24Localization: remove gettextFromC::instance()Gravatar Berthold Stoeger
There were a handfull instances of the kind 1) gettextFromC::instance()->tr(...) 2) gettextFromC::instance()->trGettext(...) 1) is pointless, as tr is a static function. All instances of 2) were likewise pointless, because trGettext() returns a C-string, which was then immediately converted to a QString. Thus, replace both constructs by gettextFromC::tr(...). After this change there was only one user of gettextFromC::instance() left, viz. the C-interface funtion trGettext(). Therefore, remove gettextFromC::instance() and do all the caching / translating directly in the global trGettext(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>