~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to EquipmentComboBox.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
 
 * EquipmentComboBox.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 "EquipmentComboBox.h"
20
 
#include <list>
21
 
 
22
 
EquipmentComboBox::EquipmentComboBox(QWidget* parent)
23
 
        : QComboBox(parent)
24
 
{
25
 
   recipeObs = 0;
26
 
}
27
 
 
28
 
void EquipmentComboBox::startObservingDB()
29
 
{
30
 
   if( Database::isInitialized() )
31
 
   {
32
 
      dbObs = Database::getDatabase();
33
 
      addObserved(dbObs);
34
 
 
35
 
      std::list<Equipment*>::iterator it, end;
36
 
 
37
 
      end = dbObs->getEquipmentEnd();
38
 
 
39
 
      for( it = dbObs->getEquipmentBegin(); it != end; ++it )
40
 
         addEquipment(*it);
41
 
      repopulateList();
42
 
   }
43
 
 
44
 
   setCurrentIndex(-1);
45
 
}
46
 
 
47
 
void EquipmentComboBox::addEquipment(Equipment* equipment)
48
 
{
49
 
   equipmentObs.push_back(equipment);
50
 
   addObserved(equipment);
51
 
 
52
 
   addItem( tr(equipment->getName().c_str()) );
53
 
}
54
 
 
55
 
void EquipmentComboBox::removeAllEquipments()
56
 
{
57
 
   /*
58
 
   removeAllObserved(); // Don't want to observe anything.
59
 
   equipmentObs.clear(); // Delete internal list.
60
 
    */
61
 
 
62
 
   unsigned int i;
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.
67
 
}
68
 
 
69
 
void EquipmentComboBox::notify(Observable *notifier, QVariant info)
70
 
{
71
 
   unsigned int i, size;
72
 
 
73
 
   // Notifier could be the database.
74
 
   if( notifier == dbObs && (info.toInt() == DBEQUIP || info.toInt() == DBALL) )
75
 
   {
76
 
      Equipment* previousSelection = getSelected();
77
 
      
78
 
      removeAllEquipments();
79
 
      std::list<Equipment*>::iterator it, end;
80
 
 
81
 
      end = dbObs->getEquipmentEnd();
82
 
 
83
 
      for( it = dbObs->getEquipmentBegin(); it != end; ++it )
84
 
         addEquipment(*it);
85
 
      repopulateList();
86
 
 
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());
93
 
      else
94
 
         setCurrentIndex(-1); // Or just give up.
95
 
   }
96
 
   else if( notifier == recipeObs )
97
 
   {
98
 
      // All we care about is the equipment in the recipe.
99
 
      if( recipeObs->getEquipment() )
100
 
         setIndexByEquipmentName( (recipeObs->getEquipment())->getName() );
101
 
      else
102
 
         setCurrentIndex(-1); // Or just give up.
103
 
   }
104
 
   else // Otherwise, we know that one of the equipments changed.
105
 
   {
106
 
      size = equipmentObs.size();
107
 
      for( i = 0; i < size; ++i )
108
 
         if( notifier == equipmentObs[i] )
109
 
         {
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()));
113
 
         }
114
 
   }
115
 
}
116
 
 
117
 
void EquipmentComboBox::setIndexByEquipmentName(std::string name)
118
 
{
119
 
   int ndx;
120
 
 
121
 
   ndx = findText( tr(name.c_str()), Qt::MatchExactly );
122
 
   /*
123
 
   if( ndx == -1 )
124
 
      return;
125
 
   */
126
 
   
127
 
   setCurrentIndex(ndx);
128
 
}
129
 
 
130
 
void EquipmentComboBox::repopulateList()
131
 
{
132
 
   unsigned int i, size;
133
 
   clear();
134
 
 
135
 
   size = equipmentObs.size();
136
 
   for( i = 0; i < size; ++i )
137
 
      addItem( tr(equipmentObs[i]->getName().c_str()) );
138
 
}
139
 
 
140
 
Equipment* EquipmentComboBox::getSelected()
141
 
{
142
 
   if( currentIndex() >= 0 )
143
 
      return equipmentObs[currentIndex()];
144
 
   else
145
 
      return 0;
146
 
}
147
 
 
148
 
void EquipmentComboBox::observeRecipe(Recipe* rec)
149
 
{
150
 
   if( rec )
151
 
   {
152
 
      if( recipeObs )
153
 
         removeObserved(recipeObs);
154
 
      recipeObs = rec;
155
 
      addObserved(recipeObs);
156
 
 
157
 
      if( recipeObs->getEquipment() )
158
 
         setIndexByEquipmentName( (recipeObs->getEquipment())->getName() );
159
 
      else
160
 
         setCurrentIndex(-1);
161
 
   }
162
 
}