~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/RecipeComboBox.cpp

  • Committer: Philip Greggory Lee
  • Date: 2009-08-23 16:53:43 UTC
  • Revision ID: git-v1:f8d1a25135bd92f06c46c562293800e4faa42c61
Made a src/ and ui/ directory and moved everything.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * RecipeComboBox.cpp is part of Brewtarget, and is Copyright Philip G. Lee
 
3
 * (rocketman768@gmail.com), 2009.
 
4
 *
 
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.
 
9
 
 
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.
 
14
 
 
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/>.
 
17
 */
 
18
 
 
19
#include <QComboBox>
 
20
#include <QWidget>
 
21
#include <Qt>
 
22
#include <list>
 
23
#include <string>
 
24
 
 
25
#include "database.h"
 
26
#include <QString>
 
27
#include "RecipeComboBox.h"
 
28
 
 
29
RecipeComboBox::RecipeComboBox(QWidget* parent)
 
30
        : QComboBox(parent)
 
31
{
 
32
}
 
33
 
 
34
void RecipeComboBox::startObservingDB()
 
35
{
 
36
   if( Database::isInitialized() )
 
37
   {
 
38
      dbObs = Database::getDatabase();
 
39
      addObserved(dbObs);
 
40
 
 
41
      std::list<Recipe*>::iterator it, end;
 
42
 
 
43
      end = dbObs->getRecipeEnd();
 
44
 
 
45
      for( it = dbObs->getRecipeBegin(); it != end; ++it )
 
46
         addRecipe(*it);
 
47
      repopulateList();
 
48
   }
 
49
}
 
50
 
 
51
void RecipeComboBox::addRecipe(Recipe* recipe)
 
52
{
 
53
   recipeObs.push_back(recipe);
 
54
   addObserved(recipe);
 
55
 
 
56
   addItem( tr(recipe->getName().c_str()) );
 
57
}
 
58
 
 
59
void RecipeComboBox::removeAllRecipes()
 
60
{
 
61
   unsigned int i;
 
62
   for( i = 0; i < recipeObs.size(); ++i )
 
63
      removeObserved(recipeObs[i]);
 
64
   recipeObs.clear(); // Clear internal list.
 
65
   clear(); // Clear the combo box's visible list.
 
66
}
 
67
 
 
68
void RecipeComboBox::notify(Observable *notifier, QVariant info)
 
69
{
 
70
   unsigned int i, size;
 
71
 
 
72
   // Notifier could be the database. Only pay attention if the number of
 
73
   // recipes has changed.
 
74
   if( notifier == dbObs && (info.toInt() == DBRECIPE || info.toInt() == DBALL) )
 
75
   {
 
76
      removeAllRecipes();
 
77
      std::list<Recipe*>::iterator it, end;
 
78
 
 
79
      end = dbObs->getRecipeEnd();
 
80
 
 
81
      for( it = dbObs->getRecipeBegin(); it != end; ++it )
 
82
         addRecipe(*it);
 
83
      repopulateList();
 
84
   }
 
85
   else // Otherwise, we know that one of the recipes changed.
 
86
   {
 
87
      size = recipeObs.size();
 
88
      for( i = 0; i < size; ++i )
 
89
         if( notifier == recipeObs[i] )
 
90
         {
 
91
            // Notice we assume 'i' is an index into both 'recipeObs' and also
 
92
            // to the text list in this combo box...
 
93
            setItemText(i, tr(recipeObs[i]->getName().c_str()));
 
94
         }
 
95
   }
 
96
}
 
97
 
 
98
void RecipeComboBox::setIndexByRecipeName(std::string name)
 
99
{
 
100
   int ndx;
 
101
 
 
102
   ndx = findText( tr(name.c_str()), Qt::MatchExactly );
 
103
 
 
104
   setCurrentIndex(ndx);
 
105
}
 
106
 
 
107
void RecipeComboBox::setIndex(int ndx)
 
108
{
 
109
   setCurrentIndex(ndx);
 
110
}
 
111
 
 
112
void RecipeComboBox::repopulateList()
 
113
{
 
114
   unsigned int i, size;
 
115
   clear();
 
116
 
 
117
   size = recipeObs.size();
 
118
   for( i = 0; i < size; ++i )
 
119
      addItem( tr(recipeObs[i]->getName().c_str()) );
 
120
}
 
121
 
 
122
Recipe* RecipeComboBox::getSelectedRecipe()
 
123
{
 
124
   if( currentIndex() >= 0 )
 
125
      return recipeObs[currentIndex()];
 
126
   else
 
127
      return 0;
 
128
}