- 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.
191 lines
5.5 KiB
Markdown
191 lines
5.5 KiB
Markdown
# 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
|
|
|
|
1. **Open Medicine Manager**:
|
|
- Launch the application
|
|
- Go to `Tools` → `Manage Medicines...`
|
|
|
|
2. **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"
|
|
|
|
3. **Edit a Medicine**:
|
|
- Select a medicine from the list
|
|
- Click "Edit Medicine"
|
|
- Modify the fields as needed
|
|
- Click "Save"
|
|
|
|
4. **Remove a Medicine**:
|
|
- Select a medicine from the list
|
|
- Click "Remove Medicine"
|
|
- Confirm the removal
|
|
|
|
### Programmatically
|
|
|
|
```python
|
|
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:
|
|
|
|
```json
|
|
{
|
|
"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 management
|
|
- `src/medicine_management_window.py` - UI for managing medicines
|
|
- `medicines.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 programmatically
|
|
- `test_medicine_system.py` - Comprehensive system test
|
|
|
|
## Troubleshooting
|
|
|
|
### Medicine Not Appearing
|
|
1. Check `medicines.json` file exists and is valid JSON
|
|
2. Restart the application after manual JSON edits
|
|
3. Check logs for any loading errors
|
|
|
|
### CSV Issues
|
|
1. Backup your data before adding/removing medicines
|
|
2. New medicines will have empty data for existing entries
|
|
3. Removed medicine data is preserved but not displayed
|
|
|
|
### Color Issues
|
|
1. Colors must be in hex format: #RRGGBB
|
|
2. Ensure colors are visually distinct
|
|
3. Default color #DDA0DD is used for invalid colors
|
|
|
|
## Development
|
|
|
|
To extend the system:
|
|
1. Add new properties to the `Medicine` dataclass
|
|
2. Update the UI forms to handle new properties
|
|
3. Modify the JSON serialization if needed
|
|
4. Update the medicine management window
|