2
* StyleComboBox.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 "StyleComboBox.h"
22
StyleComboBox::StyleComboBox(QWidget* parent)
28
void StyleComboBox::startObservingDB()
30
if( ! Database::isInitialized() )
31
Database::initialize();
33
dbObs = Database::getDatabase();
36
std::list<Style*>::iterator it, end;
38
end = dbObs->getStyleEnd();
40
for( it = dbObs->getStyleBegin(); it != end; ++it )
47
void StyleComboBox::addStyle(Style* style)
49
styleObs.push_back(style);
52
addItem( tr(style->getName().c_str()) );
55
void StyleComboBox::removeAllStyles()
58
for( i = 0; i < styleObs.size(); ++i )
59
removeObserved(styleObs[i]);
60
styleObs.clear(); // Clear the internal list.
61
clear(); // Clear the combo box's visible list.
64
void StyleComboBox::notify(Observable *notifier, QVariant info)
68
// Notifier could be the database.
69
if( notifier == dbObs && (info.toInt() == DBSTYLE || info.toInt() == DBALL) )
71
Style* previousSelection = getSelected(); // Remember what we had.
74
std::list<Style*>::iterator it, end;
76
end = dbObs->getStyleEnd();
78
for( it = dbObs->getStyleBegin(); it != end; ++it )
82
// Need to reset the selected entry if we observe a recipe.
83
if( recipeObs && recipeObs->getStyle() )
84
setIndexByStyleName( (recipeObs->getStyle())->getName() );
85
// Or, try to select the same thing we had selected last.
86
else if( previousSelection )
87
setIndexByStyleName(previousSelection->getName());
89
setCurrentIndex(-1); // Or just give up.
91
else if( notifier == recipeObs )
93
// All we care about is the style in the recipe.
94
if( recipeObs->getStyle() )
95
setIndexByStyleName( (recipeObs->getStyle())->getName() );
97
setCurrentIndex(-1); // Or just give up.
99
else // Otherwise, we know that one of the styles changed.
101
size = styleObs.size();
102
for( i = 0; i < size; ++i )
103
if( notifier == styleObs[i] )
105
// Notice we assume 'i' is an index into both 'styleObs' and also
106
// to the text list in this combo box...
107
setItemText(i, tr(styleObs[i]->getName().c_str()));
112
void StyleComboBox::setIndexByStyleName(std::string name)
116
ndx = findText( tr(name.c_str()), Qt::MatchExactly );
122
setCurrentIndex(ndx);
125
void StyleComboBox::repopulateList()
127
unsigned int i, size;
128
clear(); // Remove all items in the visible list.
130
size = styleObs.size();
131
for( i = 0; i < size; ++i )
132
addItem( tr(styleObs[i]->getName().c_str()) );
135
Style* StyleComboBox::getSelected()
137
if( currentIndex() >= 0 )
138
return styleObs[currentIndex()];
143
void StyleComboBox::observeRecipe(Recipe* rec)
145
// Make sure caller isn't stupid.
148
// Remove any previous association.
150
removeObserved(recipeObs);
153
addObserved(recipeObs);
155
if( recipeObs->getStyle() )
156
setIndexByStyleName( (recipeObs->getStyle())->getName() );