[not ready for review] fix mirror settings modified in registry before user press accept button - #370
[not ready for review] fix mirror settings modified in registry before user press accept button#370atsju wants to merge 22 commits into
Conversation
…y mirror settings
|
🚀 New build available for commit |
|
🚀 New build available for commit |
|
🚀 New build available for commit |
|
🚀 New build available for commit |
atsju
left a comment
There was a problem hiding this comment.
Hi @gr5
while this PR is not ready for review, could you have a look at it and tell me honnestly what you think about it ?
It's quite large and indirectly does more than title says.
I added some comments in the review pane and in presentation to help out.
The build should be functionnal except for elipse related features.
I have a new personal project restoring an old lathe, so I would rather not spend hours finishing this if it doesn't pass initial smell test. I already spent quite some time to arrive here.
| { | ||
| QSettings set; | ||
| QString path = set.value("mirrorConfigFile").toString(); | ||
| QString path = SettingsFacade::instance().appStore().load().mirrorConfigFile; |
There was a problem hiding this comment.
all direct access to Qsettings have been repalced with settingsFacade
| cv::Scalar mean,std; | ||
| cv::meanStdDev(wf->workData,mean,std,wf->workMask); | ||
| double stdVal = std.val[0]* md->lambda/outputLambda; | ||
| double stdVal = std.val[0]* md->currentSettings().lambda/outputLambda; |
There was a problem hiding this comment.
all parameters like lambda, diameter and other are in a settings struc of the mirror for better access control and management when settings are modifed
| // Persistent mirror configuration copy (source of truth) | ||
| MirrorSettings m_current; | ||
|
|
||
| // Working copy for dialog edits (discarded on Cancel, committed to m_current on OK) | ||
| MirrorSettings m_draft; |
There was a problem hiding this comment.
mirror settings (draft during edition and current when accepted) are no private and managed with accessors.
| @@ -0,0 +1,53 @@ | |||
| #include "settingsfacade.h" | |||
There was a problem hiding this comment.
facade file is scalable and supposed to be the only entry point for every Qsettings (not in this PR, but in extremely long term it's doable if we want)
|
|
||
| #include <QSettings> | ||
|
|
||
| #define SETTINGS_STORE_LOAD_FIELD_FROM_QSETTINGS(type, name, defaultValue, key, converter) \ |
There was a problem hiding this comment.
the store is accessed by facade and uses some macro to read the settingsstores_fields.inc file
| @@ -0,0 +1,51 @@ | |||
| // Shared settings schema list. | |||
| // Entry format for callbacks: | |||
| // FIELD(type, memberName, defaultValue, key, converter) | |||
There was a problem hiding this comment.
this is the really neat part
FIELD(type, memberName, defaultValue, key, converter)
Everthing is typed here only once. It's the main source a truth.
I know Dale has been extremelly carefull to copy paste the same default aclues, key strings, converters but this solved programatically the risk of error.
It also centralises all the used keys in one knwoledge file. I did not change any key to not break compatibility.
There was a problem hiding this comment.
I think this should be ".h". I read about .inc but I use 3 different editors regularly to look at the code. QT is great where I can right click something and find it's definition. But the other editors I have to specify which type of file to search through and I don't want to add ".inc" or ".*".
You didn't define FIELD macro/function yet, right? I didn't find that anywhere. I assume that's what you meant by you still had work to do.
There was a problem hiding this comment.
I can rename to .h no problem. It was .inc because it's not "really" code.
FIELD is not a macro this all already works.
search for SETTINGS_STORE_FOR_EACH_MIRROR_FIELD and SETTINGS_STORE_DECLARE_STRUCT_FIELD for example
There was a problem hiding this comment.
Maybe put a few starter comments in there. Like how roc and diameter are in mm. That will remind/encourage me to add other comments in the future if I'm trying to understand some setting.
In QT creator I put the cursor on a line of code in averagewavefrontfilesdlg.cpp where it was looking at lambda. I hit F2 key and it properly jumped to correct line in the .inc file. So if there was a comment there I would see it.
It is definitely nice to see the defaults there on the same line as the variable type (double) and what it's called in the registry.
|
I will try to get to this tomorrow. I spent most of my days away from the computer today and yesterday. Tomorrow I should have more time. |
|
I love the bit where you have m_draft and m_current. By the way please add an "Apply" button to the mirror dialog so people can see the result of their change without closing the dialog. I know your love your simplification of storing these variables but there are only 15. Someone trying to understand the code would normally look at the 15 in the .h file and possibly get to see comments about each one explaining things like the units (mm versus inches) and other helpful details. Not sure where to look for that info. I guess in the ".inc" file. I don't hate it. I don't love it. If Dale wants to add another member variable to mirrordlg, will he figure this out? Will he know to go to the settingsstores_fields.inc file? It is pretty nice and elegant. You have such ambition - I love that part. Let me talk to Dale. |
|
Yes "apply" button is a good idea. I also had in mind to add comments about each of the qsettings into the .inc to centralise it but somehow I didn't end doing it. Note the settings store is merely a proof of the concept and is applied only for mirror settings. there are plenty of other Qsettings that could use the same way (or not). I believe it's arround 210 settings in total QsettingsList.txt. Edit: I probably misunderstood you were talking about the 15 member variables in stuct or the 15 Qsettings. Anyway, most of comment is true for both. |
|
Thank you for having looked at it ! |
|
I want to make sure you know that mirror settings should never be saved in the QSettings as the source for the mirror project. They are unique to each mirror and test setup. So they must be read at startup each time from the current mirror configuration file. Not from QSettings. |
|
What? What if there is no mirror file? And if the mirror is set to the latest mirror file, then qsettings should also reflect the settings of the latest mirror file anyway so why would it matter. Isn't the way it works now is that it's stored in qsettings? |
|
When you say "mirror project" are you referring to this PR? This pr is about mirrorDlg. I assume that's what you mean by "mirror project". |
|
Dale I sent you a long email yesterday. I hope you got it. |
|
No I did not get it. |
|
mirror project is the thing a user creates when testing a mirror. Usually unique to each mirror. Usually a directory with at least a mirror config file. From dftfring you switch mirror projects by loading a different mirror config. IIRC the source of truth for DFTFringe is the mirror dialog which was created from the mirror config. I don't remember if any classes use the Qsettings to get mirror config data. But if they do that was what I use to do. These days I always use the mirror dialog. It is a singleton just for that purpose. Yes the mirror dialog stores it state in QSettings and recovers them. from there. Yes some people like an apply button. I don't think I ever thought it needed one. It would be nice to allow the user to ack out recent changes when they made a mistake. I was just too lazy to implement that. |
I just resent it. |
fix #121
mentionning #224 #234
I'm really sorry this one is quite long. It was difficult to split and even now some ellipse related things are temporarly brocken.
short version
The mirror dialog's settings were exposed as public members, allowing user interactions to corrupt the persistent copy even on Cancel. This PR implements a draft pattern + settings facade to fix it:
m_current(persistent copy) is now protected; external code reads it viacurrentSettings()(read-only)m_draft(working copy) handles all user edits; discarded on Cancel, committed on OKSettingsFacadeprovides single atomic save/load point; eliminates duplicate QSettings callsm_draft; removed 13+ public data membersLong version explanation
Problem Fixed
The singleton mirror dialog's settings were exposed as public members scattered across the class, violating encapsulation and creating a critical flaw: the
Cancelbutton could not reliably discard edits because the persistent settings copy was unprotected and could be corrupted by user interactions.Solution Implemented: Draft Pattern + Settings Facade
1. Architectural Changes
New Infrastructure:
SettingsFacade (new singleton): Centralized access point for all application settings
MirrorSettingsStore & ApplicationSettingsStore (new): Private store classes with friend-only access to
mirrordlgand facadesettingsstores_fields.incsettingsstores_fields.inc (new): Single source of truth for all settings schema
diameter,roc,cc,flipH, etc.)projectPath,mirrorConfigFile,lastPath)Dual-Copy Pattern in
mirrorDlg:2. Transactional Edit Semantics
Before Dialog Shown:
loadDraftFromSettings()for safety (in case dialog used withoutshowEvent())m_currentandm_draftare initialized from persistent storageWhile User Edits:
m_draft(working copy)m_currentviacurrentSettings()(persistent copy)On Dialog Close:
OK/Accept: Commits
m_draft → m_current → QSettingsatomically via facadem_current = m_draft(merge working to persistent)SettingsFacade::instance().saveMirrorSettings(m_current)(atomic save)Cancel: Discards
m_draftentirelyloadDraftFromSettings()reloads from persistent storageshowEvent() Override:
loadDraftFromSettings()before dialog becomes visible3. Encapsulation Improvements
Removed Public Data Members:
diameter,roc,obs,cc,flipH,lambda,doNull,fringeSpacing,aperatureReduction,m_useAnnular,m_outlineShape,m_verticalAxis, etc. (13+ exposed members)MirrorSettings m_draftstructReplaced With Managed Access:
mirrorDlg::get_Instance()->currentSettings()returnsconst MirrorSettings&(read-only)Added Inline Getters for Computed Values:
Compile-Time Access Control:
→ No way for external code to call
save()directly; enforced at compile-time by linker4. Settings Persistence Flow
Old Design:
New Design:
Benefits
m_current) is now protected from Cancel operationscurrentSettings()(persistent), dialog editsm_draft(working)Changed Files
Core Architecture
Mirror Dialog Refactoring
m_draft, addedloadDraftFromSettings()andshowEvent(), simplifiedon_buttonBox_accepted()Build System
Future Work (Next PRs)
Critical TODOs from mirrordlg.h
1. Persist Unit Preference (mm vs. inches)
Current Behavior: Unit preference (
mm) is transient—resets to default on restart.Solution:
bool unitsMMfield tosettingsstores_fields.incshowEvent()andon_buttonBox_accepted()mm(true)initializationBenefit: Remembers user's preferred unit system across sessions.
2. Clarify & Consolidate Outline Shape API
Current Problem:
m_currentand QSettingssetMinorAxis(), sometimes direct member accessSolution:
adoptWavefrontSettings()to accept outline shape:setOutlineShape()separate methodsBenefit: Eliminates inconsistent state where shape is changed but not saved.
3. Clean allispe outline helper
Current Confusion:
setMinorAxisis called from outlining window and can modify mirror setting at each outline.Solution:
Benefit: Eliminates API confusion; clear roles for each method.
Technical Notes
Why X-Macros?
Reduces boilerplate and eliminates source-of-truth duplication. A single field definition in
settingsstores_fields.incautomatically generates:Adding a new mirror setting requires exactly one edit location.
Why Compile-Time Friend Enforcement?
Prevents accidental calls to
save()from unintended code locations. The linker will reject anysave()call outside the allowed friend scope—impossible to miss in code review or CI.Why Always Reload on showEvent()?
Ensures the dialog never operates on stale data if another dialog modified settings between invocations. Also handles programmatic dialog reuse without explicit reset calls.
References