Main View Model
Relevant source files
The following files were used as context for generating this wiki page:
The MainViewModel serves as the central orchestrator for the SSA Consulta Rapida presentation layer. It coordinates various specialized sub-viewmodels, manages the application state (such as pagination, sorting, and UI preferences), and interfaces with the domain query services to fetch and display SSA (Solicitação de Serviço de Apoio) records.
As a QObject based class, it exposes properties and slots to the QML-based user interface, enabling a reactive data flow between the C++ backend and the visual components. It is responsible for handling high-level user actions like searching, navigating pages, and applying column configurations.
Sources: src/presentation/MainViewModel.h:23-25, src/presentation/MainViewModel.cpp:18-30
Architecture and Composition
The MainViewModel follows a compositional architecture, aggregating several specialized view models that handle specific UI concerns. This separation of concerns ensures that the main controller remains manageable while delegating specific logic to dedicated components.
Core Components
| Component | Class | Responsibility |
|---|---|---|
| Search | SearchViewModel | Handles the global search text and trigger logic. |
| Filters | FilterPanelViewModel | Manages complex column-based filters and quick sector selections. |
| Table Data | SsaTableModel | A QAbstractTableModel implementation for displaying paged results. |
| Details | DetailsViewModel | Manages the display of the currently selected record's detailed fields. |
| Settings | ColumnSettingsModel | Handles visibility and width for data columns. |
| Status | StatusViewModel | Provides loading states, error messages, and result summaries. |
Sources: src/presentation/MainViewModel.h:27-33, src/presentation/MainViewModel.cpp:33-59
Relationship Diagram
The following diagram illustrates how the MainViewModel aggregates its children and interacts with the domain service layer.
Data Query Flow
Data retrieval is handled asynchronously to keep the UI responsive. When a query is triggered (via apply(), load(), or pagination), the MainViewModel constructs an SsaPageRequest object and executes it using QtConcurrent.
Query Execution Sequence
This diagram shows the lifecycle of a data request from the UI trigger to the table update.
Request Parameters
The SsaPageRequest structure encapsulates all necessary data for the domain layer to perform the search:
- Pagination:
pageIndexandpageSize(clamped between 10 and 500). - Criteria:
searchText,columnFilters(map), andquickSector. - Sorting:
SortSpeccontainingcolumnKeyandascendingflag. - View:
visibleColumnslist to optimize database selection.
Sources: src/domain/SsaTypes.h:15-17, src/domain/SsaTypes.h:105-114
State Management and Preferences
The MainViewModel manages persistent user settings through an IUserPreferencesStore. Changes to the UI state are often debounced and saved automatically.
UI Configuration Properties
| Property | Type | Description |
|---|---|---|
theme | QString | Current UI theme (e.g., "system", "dark"). |
density | QString | UI spacing density ("compact", "normal", "comfortable"). |
detailsVisible | bool | Toggle for the side details panel visibility. |
detailsPanelWidth | int | Width of the details panel, clamped between 280 and 680. |
pageSize | int | Number of records per page. |
Sources: src/presentation/MainViewModel.h:34-42, src/domain/SsaTypes.h:18-20, src/presentation/MainViewModel.cpp:73-108
Persistence Logic
The view model uses a QTimer (preferencesSaveTimer_) set to 250ms to debounce save operations. When a preference-related property is changed, scheduleSavePreferences() is called, which starts the timer. On timeout, savePreferences() is executed asynchronously.
Navigation and Selection
The model provides standard paged navigation and record selection logic.
- Pagination:
nextPage()andpreviousPage()increment or decrement thepageIndex_before callingload(). Boundary checks are performed againstpageCount(). - Sorting:
sortByColumn(int column)retrieves the column key from the table model. If the same column is clicked twice, it toggles the sort order; otherwise, it defaults to ascending. - Selection:
selectRow(int row)updates theDetailsViewModelwith the record at the specified index from theSsaTableModel.
Sources: src/presentation/MainViewModel.cpp:153-182
Summary
The MainViewModel acts as the engine of the presentation layer, bridging the gap between the stateless domain/query logic and the stateful user interface. It manages a complex lifecycle of asynchronous data fetching, preference persistence, and component coordination, ensuring that the UI remains consistent with the underlying data and user configurations.
Sources: src/presentation/MainViewModel.cpp:18-59, src/presentation/MainViewModel.h:23-50