2
* EquipmentComboBox.cpp is part of Brewtarget, and is Copyright Philip G. Lee
3
* (rocketman768@gmail.com), 2009.
5
* Brewtarget is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Brewtarget is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "EquipmentComboBox.h"
22
EquipmentComboBox::EquipmentComboBox(QWidget* parent)
28
void EquipmentComboBox::startObservingDB()
30
if( Database::isInitialized() )
32
dbObs = Database::getDatabase();
35
std::list<Equipment*>::iterator it, end;
37
end = dbObs->getEquipmentEnd();
39
for( it = dbObs->getEquipmentBegin(); it != end; ++it )
47
void EquipmentComboBox::addEquipment(Equipment* equipment)
49
equipmentObs.push_back(equipment);
50
addObserved(equipment);
52
addItem( tr(equipment->getName().c_str()) );
55
void EquipmentComboBox::removeAllEquipments()
58
removeAllObserved(); // Don't want to observe anything.
59
equipmentObs.clear(); // Delete internal list.
63
for( i=0; i < equipmentObs.size(); ++i )
64
removeObserved(equipmentObs[i]);
65
equipmentObs.clear(); // Clear internal list.
66
clear(); // Clear the combo box's visible list.
69
void EquipmentComboBox::notify(Observable *notifier, QVariant info)
73
// Notifier could be the database.
74
if( notifier == dbObs && (info.toInt() == DBEQUIP || info.toInt() == DBALL) )
76
Equipment* previousSelection = getSelected();
78
removeAllEquipments();
79
std::list<Equipment*>::iterator it, end;
81
end = dbObs->getEquipmentEnd();
83
for( it = dbObs->getEquipmentBegin(); it != end; ++it )
87
// Need to reset the selected entry if we observe a recipe.
88
if( recipeObs && recipeObs->getEquipment() )
89
setIndexByEquipmentName( (recipeObs->getEquipment())->getName() );
90
// Or, try to select the same thing we had selected last.
91
else if( previousSelection )
92
setIndexByEquipmentName(previousSelection->getName());
94
setCurrentIndex(-1); // Or just give up.
96
else if( notifier == recipeObs )
98
// All we care about is the equipment in the recipe.
99
if( recipeObs->getEquipment() )
100
setIndexByEquipmentName( (recipeObs->getEquipment())->getName() );
102
setCurrentIndex(-1); // Or just give up.
104
else // Otherwise, we know that one of the equipments changed.
106
size = equipmentObs.size();
107
for( i = 0; i < size; ++i )
108
if( notifier == equipmentObs[i] )
110
// Notice we assume 'i' is an index into both 'equipmentObs' and also
111
// to the text list in this combo box...
112
setItemText(i, tr(equipmentObs[i]->getName().c_str()));
117
void EquipmentComboBox::setIndexByEquipmentName(std::string name)
121
ndx = findText( tr(name.c_str()), Qt::MatchExactly );
127
setCurrentIndex(ndx);
130
void EquipmentComboBox::repopulateList()
132
unsigned int i, size;
135
size = equipmentObs.size();
136
for( i = 0; i < size; ++i )
137
addItem( tr(equipmentObs[i]->getName().c_str()) );
140
Equipment* EquipmentComboBox::getSelected()
142
if( currentIndex() >= 0 )
143
return equipmentObs[currentIndex()];
148
void EquipmentComboBox::observeRecipe(Recipe* rec)
153
removeObserved(recipeObs);
155
addObserved(recipeObs);
157
if( recipeObs->getEquipment() )
158
setIndexByEquipmentName( (recipeObs->getEquipment())->getName() );