Find and replace dialog for text editing operations.
More...
#include <PReplaceDialog.h>
|
| virtual void | onFindTextAvailable (const QString &) |
| | Slot: Handles changes to the find text input.
|
| |
Find and replace dialog for text editing operations.
This dialog provides a comprehensive interface for finding and replacing text within the musredit editor. It supports various search options and replacement modes to accommodate different user workflows.
- Search Options:
- The dialog offers several options to refine search behavior:
- Case sensitive: Match text case exactly vs. ignore case
- Whole words only: Match complete words vs. partial matches
- From cursor: Start from current position vs. from document beginning
- Find backwards: Search upward vs. downward
- Selected text only: Limit search to selection vs. entire document
- Replacement Modes:
- Users can choose how replacements are performed:
- Replace: Find and replace one occurrence at a time
- Replace All: Replace all occurrences without confirmation
- Prompt on replace: Ask for confirmation before each replacement (launches PReplaceConfirmationDialog for interactive control)
- Text Input Features:
- Combo boxes for find/replace text maintain search history
- Previous searches are accessible via dropdown
- Empty find text disables replace button to prevent errors
- Scope Control:
- Selected text checkbox: Enabled only when text is selected in editor
- When enabled, restricts operations to selection boundaries
- When disabled/unchecked, operates on entire document
- State Management:
- The dialog uses PFindReplaceData structure to:
- Initialize dialog controls from previous search parameters
- Store current dialog settings for use by calling code
- Maintain search state across multiple find/replace operations
- Preserve user preferences between dialog invocations
- Dialog Workflow:
- User opens dialog (Edit → Replace or Ctrl+H)
- Dialog loads previous search parameters from PFindReplaceData
- User enters find text and replacement text
- User configures search options via checkboxes
- User clicks Replace, Replace All, or Close
- Dialog updates PFindReplaceData with current settings
- Calling code executes requested operation using updated data
- Button State Management:
- Replace button is enabled only when find text is non-empty
- Button state updates dynamically as user types
- Prevents execution of invalid search operations
- Note
- The dialog is modal, requiring user interaction before continuing.
-
Changes to settings are only applied when user clicks Replace/Replace All.
-
The dialog maintains search history through combo box items.
- See also
- PFindReplaceData Structure defining all find/replace parameters
-
PReplaceConfirmationDialog For per-replacement confirmation
-
musredit For the main editor with text manipulation capabilities
Definition at line 118 of file PReplaceDialog.h.
◆ PReplaceDialog()
| PReplaceDialog::PReplaceDialog |
( |
PFindReplaceData * | data, |
|
|
const bool | selection, |
|
|
QWidget * | parent = nullptr ) |
Constructs the find and replace dialog.
Constructor - Initializes the find and replace dialog.
Initializes the dialog with existing find/replace parameters and configures UI controls based on current editor state. The dialog is configured as modal and populates all fields from the provided data structure.
- Parameters
-
| data | Pointer to PFindReplaceData structure containing previous search parameters and settings. The dialog reads from this structure to initialize controls and writes to it when getData() is called. Must not be nullptr. |
| selection | true if text is currently selected in the editor, false otherwise. Controls the enabled state of the "Selected text only" checkbox. When false, the checkbox is disabled and unchecked. |
| parent | Pointer to the parent widget (typically the main editor window). If nullptr, the dialog has no parent. Parent relationship ensures proper centering and cleanup. |
- See also
- PFindReplaceData For the parameter structure definition
-
getData() Retrieves updated parameters after dialog use
Sets up the find-and-replace dialog with comprehensive search options and replacement controls. The dialog is initialized from existing search parameters and configured based on current editor state.
Initialization Process:
- Base Class Initialization: Calls QDialog constructor with parent and stores pointer to data structure
- UI Setup: Loads dialog layout from Qt Designer UI file including:
- Find text combo box with history
- Replace text combo box with history
- Search option checkboxes
- Replace, Replace All, and Close buttons
- Modal Configuration: Sets dialog as modal to ensure focused interaction
- Replace Button Validation: Disables Replace button if find text is empty, preventing invalid search operations from being initiated
- Selection State Handling: If no text is selected in the editor:
- Unchecks "Selected text only" checkbox
- Disables the checkbox to prevent invalid configuration This provides clear visual feedback about available search scope
- Parameter Loading: Populates all UI controls from PFindReplaceData:
- Find text: Loaded into combo box item 0 (maintains history)
- Replace text: Loaded into combo box item 0 (maintains history)
- Case sensitive: Checkbox reflects previous setting
- Whole words only: Checkbox reflects previous setting
- From cursor: Checkbox reflects previous setting
- Find backwards: Checkbox reflects previous setting
- Prompt on replace: Checkbox reflects previous setting
- Selected text: Checkbox reflects previous setting (if selection exists)
State-Based Behavior:
The dialog adapts its initial state based on editor context:
- Empty find text → Replace button disabled
- Non-empty find text → Replace button enabled
- No selection → Selected text checkbox disabled and unchecked
- Has selection → Selected text checkbox enabled, state from data
Combo Box History: The combo boxes use item 0 for the current/default text. Additional items (if present) maintain search/replace history, allowing users to quickly reuse previous patterns.
- Parameters
-
| data | Pointer to PFindReplaceData structure containing:
- Previous find/replace text strings
- All search option flags
- Scope settings This structure is both read from (initialization) and written to (via getData()) during the dialog's lifetime. Must not be nullptr.
|
| selection | Indicates whether text is currently selected in the editor:
- true: Selected text checkbox is enabled and state loaded from data parameter, allowing search within selection
- false: Selected text checkbox is disabled and unchecked, forcing full-document scope
|
| parent | Pointer to the parent widget (typically the main musredit window). The parent relationship provides:
- Automatic dialog centering over parent
- Proper z-order (dialog appears on top)
- Consistent theme and styling
- Automatic cleanup when parent is destroyed
|
- Note
- The dialog is created but not displayed by the constructor. The caller must invoke exec() to show it modally.
-
The Replace button's enabled state is managed dynamically through the onFindTextAvailable() slot as the user types.
-
Combo box item 0 is used for current text; additional items could provide search history functionality.
-
The selected text checkbox is handled specially: only checked if selection exists AND the data parameter indicates it should be used.
- See also
- PFindReplaceData For the complete parameter structure
-
getData() Retrieves updated parameters after dialog use
-
onFindTextAvailable() Manages Replace button state dynamically
Definition at line 135 of file PReplaceDialog.cpp.
◆ ~PReplaceDialog()
| virtual PReplaceDialog::~PReplaceDialog |
( |
| ) |
|
|
inlinevirtual |
Virtual destructor.
Default destructor implementation. Qt's parent-child ownership system automatically handles cleanup of UI elements.
- Note
- Declared virtual to ensure proper cleanup in inheritance hierarchies.
Definition at line 156 of file PReplaceDialog.h.
◆ getData()
Retrieves updated find/replace parameters from the dialog.
Collects all current settings from dialog UI controls and updates the internal PFindReplaceData structure. This method should be called after the dialog is accepted to obtain the user's search and replace configuration.
The retrieved data includes:
- Find text string (from combo box current text)
- Replacement text string (from combo box current text)
- Case sensitive flag
- Whole words only flag
- From cursor flag (search starting point)
- Find backwards flag (search direction)
- Selected text only flag (if checkbox enabled)
- Prompt on replace flag (confirmation mode)
- Returns
- PFindReplaceData* Pointer to the updated parameter structure. This is the same pointer passed to the constructor, with all fields updated from current dialog state.
- Note
- The selected text flag is only updated if the checkbox is enabled.
-
This method is typically called by the main editor after the dialog is accepted via Replace or Replace All buttons.
- See also
- PFindReplaceData For the complete parameter structure definition
Collects all current settings from the dialog's UI controls and updates the internal PFindReplaceData structure. This method provides a complete snapshot of the user's search and replace configuration.
Retrieved Parameters:
Text Strings:
- findText: Current text in the find combo box (what to search for)
- replaceText: Current text in the replace combo box (replacement string)
Both strings are retrieved using currentText(), which returns the text visible in the combo box (either from item selection or direct user input).
Search Options (Boolean Flags):
- caseSensitive: true = match case exactly, false = ignore case
- Example: "THEORY" vs "theory" are different when true
- wholeWordsOnly: true = match complete words only, false = allow partial
- Example: searching "fit" won't match "musrfit" when true
- fromCursor: true = start from current cursor position, false = from beginning
- Affects where the search operation begins
- findBackwards: true = search upward, false = search downward
- Controls search direction through the document
Scope Options:
- selectedText: true = limit search to selection, false = entire document
- Conditional update: Only updated if checkbox is enabled
- If checkbox is disabled (no selection), this flag is not modified
- Prevents setting invalid scope when no selection exists
Replacement Behavior:
- promptOnReplace: true = confirm each replacement, false = replace automatically
Conditional Logic:
The selectedText flag has special handling:
if (fSelectedText_checkBox->isEnabled())
fData->selectedText = fSelectedText_checkBox->isChecked();
PFindReplaceData * fData
Pointer to find/replace parameter structure storing all settings.
This ensures that when no selection exists (checkbox disabled), the flag retains its previous value rather than being forced to false. This prevents unintended changes to the data structure for subsequent operations where a selection might exist.
Usage Pattern:
This method is typically called by the main editor after the dialog is accepted:
- User clicks Replace or Replace All button
- Dialog is accepted (exec() returns)
- Calling code invokes getData()
- Updated parameters are used to perform find/replace operation
- Search/replace engine uses settings from returned structure
- Returns
- PFindReplaceData* Pointer to the updated parameter structure. This is the same pointer passed to the constructor, now with all fields updated from current dialog state. The caller owns this structure and is responsible for its lifetime management.
- Note
- The returned pointer is never nullptr (it's the constructor parameter).
-
The selectedText flag is only updated when the checkbox is enabled.
-
Current combo box text is used, not necessarily a selected item, allowing users to enter new search patterns.
-
This method does not validate the parameters; validation should be performed by the calling code if needed.
- See also
- PFindReplaceData For the complete parameter structure definition
-
PReplaceDialog::PReplaceDialog() Where the data pointer is stored
-
PReplaceConfirmationDialog Used when promptOnReplace is true
Definition at line 243 of file PReplaceDialog.cpp.
◆ onFindTextAvailable
| void PReplaceDialog::onFindTextAvailable |
( |
const QString & | | ) |
|
|
protectedvirtualslot |
Slot: Handles changes to the find text input.
Called when the find text combo box content changes. Updates the enabled state of the Replace button based on whether the find text is empty. This prevents users from initiating invalid search operations.
Button state logic:
- If find text is non-empty: Enable Replace button
- If find text is empty: Disable Replace button
This provides immediate visual feedback about whether the current configuration is valid for replacement operations.
- Parameters
-
| str | The new text content (parameter not currently used in implementation, as the method queries the combo box directly). |
- Note
- This slot is typically connected to the combo box's editTextChanged() or currentTextChanged() signal.
-
The parameter is provided by Qt's signal-slot mechanism but is not used in the current implementation.
Called whenever the find text combo box content changes. This slot provides dynamic validation and user feedback by managing the Replace button's enabled state based on whether valid search text is present.
Validation Logic:
The method implements a simple but effective validation rule:
- Non-empty find text: Enable Replace button
- Allows user to proceed with find/replace operation
- Indicates that a valid search can be performed
- Empty find text: Disable Replace button
- Prevents execution of meaningless search operations
- Provides immediate visual feedback that input is required
User Experience Benefits:
- Immediate Feedback: Button state updates as user types, providing instant indication of whether the configuration is valid
- Error Prevention: Disabling the button prevents users from attempting to search for empty strings, which would either produce errors or meaningless results
- Clear Affordance: The disabled state clearly communicates that the find text field requires input before proceeding
- Consistent Behavior: Works in tandem with constructor initialization, maintaining consistent button state throughout dialog lifetime
Signal Connection:
This slot is typically connected to one of these combo box signals:
- editTextChanged(QString): Emitted when user types or edits text
- currentTextChanged(QString): Emitted when displayed text changes
The connection is likely established in the UI file or during dialog setup.
Implementation Note:
The method receives a QString parameter (the new text) from Qt's signal-slot mechanism, but the current implementation queries the combo box directly via currentText() rather than using the parameter. This approach:
- Ensures consistency with actual combo box state
- Avoids potential timing issues with signal delivery
- Simplifies the logic by using a single data source
- Parameters
-
| str | The new text content from the combo box. Currently unused in the implementation as the method queries the combo box directly. Parameter present to match expected slot signature for Qt signals. |
- Note
- This validation is purely UI-related; the actual find/replace engine may perform additional validation when the operation is executed.
-
The method only affects the Replace button; the Replace All button (if different) might have separate validation logic.
-
Empty string check uses QString's default comparison; whitespace-only strings are considered non-empty.
- See also
- PReplaceDialog::PReplaceDialog() Where initial button state is set
-
QComboBox::currentText() Method used to query combo box state
Definition at line 322 of file PReplaceDialog.cpp.
◆ fData
Pointer to find/replace parameter structure storing all settings.
Definition at line 214 of file PReplaceDialog.h.
The documentation for this class was generated from the following files: