- Implemented MedicineManagementWindow for adding, editing, and removing medicines. - Created MedicineManager to handle medicine configurations, including loading and saving to JSON. - Updated UIManager to dynamically generate medicine-related UI components based on the MedicineManager. - Enhanced test suite with mock objects for MedicineManager to ensure proper functionality in DataManager tests. - Added validation for medicine input fields in the UI. - Introduced default medicine configurations for initial setup.
5.5 KiB
5.5 KiB
Modular Medicine System
The MedTracker application now features a modular medicine system that allows users to dynamically add, edit, and remove medicines without modifying the source code.
Features
✨ Dynamic Medicine Management
- Add new medicines through the UI or programmatically
- Edit existing medicines - change names, dosages, colors, etc.
- Remove medicines - clean up unused medications
- Automatic UI updates - all interface elements update automatically
🎛️ Medicine Configuration
Each medicine has the following configurable properties:
- Key: Internal identifier (e.g., "bupropion")
- Display Name: User-friendly name (e.g., "Bupropion")
- Dosage Info: Dosage information (e.g., "150/300 mg")
- Quick Doses: Common dose amounts for quick selection
- Color: Hex color for graph display (e.g., "#FF6B6B")
- Default Enabled: Whether to show in graphs by default
📁 Configuration Storage
- Medicines are stored in
medicines.json - Automatically created with default medicines on first run
- Human-readable JSON format for easy manual editing
Usage
Through the UI
-
Open Medicine Manager:
- Launch the application
- Go to
Tools→Manage Medicines...
-
Add a Medicine:
- Click "Add Medicine"
- Fill in the required fields:
- Key (alphanumeric, underscores, hyphens only)
- Display Name
- Dosage Info
- Quick Doses (comma-separated)
- Graph Color (hex format, e.g., #FF6B6B)
- Default Enabled checkbox
- Click "Save"
-
Edit a Medicine:
- Select a medicine from the list
- Click "Edit Medicine"
- Modify the fields as needed
- Click "Save"
-
Remove a Medicine:
- Select a medicine from the list
- Click "Remove Medicine"
- Confirm the removal
Programmatically
from medicine_manager import MedicineManager, Medicine
# Initialize manager
medicine_manager = MedicineManager()
# Add a new medicine
new_medicine = Medicine(
key="sertraline",
display_name="Sertraline",
dosage_info="50mg",
quick_doses=["25", "50", "100"],
color="#9B59B6",
default_enabled=False
)
medicine_manager.add_medicine(new_medicine)
Manual Configuration
Edit medicines.json directly:
{
"medicines": [
{
"key": "your_medicine",
"display_name": "Your Medicine",
"dosage_info": "25mg",
"quick_doses": ["25", "50"],
"color": "#FF6B6B",
"default_enabled": false
}
]
}
What Updates Automatically
When you add, edit, or remove medicines, the following components update automatically:
🖥️ User Interface
- Input Form: Medicine checkboxes in the main form
- Data Table: Column headers and display
- Edit Windows: Medicine fields and dose tracking
- Graph Controls: Toggle buttons for medicines
📊 Data Management
- CSV Headers: Automatically include new medicine columns
- Data Loading: Dynamic column type detection
- Data Entry: Medicine data is stored with appropriate columns
📈 Graphing
- Toggle Controls: Show/hide medicines in graphs
- Color Coding: Each medicine uses its configured color
- Legend: Medicine names and information in graph legends
Default Medicines
The system comes with these default medicines:
| Medicine | Dosage | Default Graph | Color |
|---|---|---|---|
| Bupropion | 150/300 mg | ✅ | Red (#FF6B6B) |
| Hydroxyzine | 25 mg | ❌ | Teal (#4ECDC4) |
| Gabapentin | 100 mg | ❌ | Blue (#45B7D1) |
| Propranolol | 10 mg | ✅ | Green (#96CEB4) |
| Quetiapine | 25 mg | ❌ | Yellow (#FFEAA7) |
Technical Details
Architecture
- MedicineManager: Core class handling medicine CRUD operations
- Medicine: Data class representing individual medicines
- Dynamic UI: Components rebuild themselves when medicines change
- Backward Compatibility: Existing data continues to work
Files Involved
src/medicine_manager.py- Core medicine managementsrc/medicine_management_window.py- UI for managing medicinesmedicines.json- Configuration storage- Updated:
main.py,ui_manager.py,data_manager.py,graph_manager.py
CSV Data Format
The CSV structure adapts automatically:
date,depression,anxiety,sleep,appetite,medicine1,medicine1_doses,medicine2,medicine2_doses,...,note
Migration Notes
Existing Data
- Existing CSV files continue to work
- Old medicine columns are preserved
- New medicines get empty columns for existing entries
Backward Compatibility
- Hard-coded medicine references have been replaced with dynamic loading
- All existing functionality is preserved
- No data loss during updates
Examples
See these example scripts:
add_medicine_example.py- Shows how to add medicines programmaticallytest_medicine_system.py- Comprehensive system test
Troubleshooting
Medicine Not Appearing
- Check
medicines.jsonfile exists and is valid JSON - Restart the application after manual JSON edits
- Check logs for any loading errors
CSV Issues
- Backup your data before adding/removing medicines
- New medicines will have empty data for existing entries
- Removed medicine data is preserved but not displayed
Color Issues
- Colors must be in hex format: #RRGGBB
- Ensure colors are visually distinct
- Default color #DDA0DD is used for invalid colors
Development
To extend the system:
- Add new properties to the
Medicinedataclass - Update the UI forms to handle new properties
- Modify the JSON serialization if needed
- Update the medicine management window