~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/StyleComboBox.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
 * StyleComboBox.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 "StyleComboBox.h"
 
20
#include <list>
 
21
 
 
22
StyleComboBox::StyleComboBox(QWidget* parent)
 
23
        : QComboBox(parent)
 
24
{
 
25
   recipeObs = 0;
 
26
}
 
27
 
 
28
void StyleComboBox::startObservingDB()
 
29
{
 
30
   if( ! Database::isInitialized() )
 
31
      Database::initialize();
 
32
 
 
33
   dbObs = Database::getDatabase();
 
34
   addObserved(dbObs);
 
35
 
 
36
   std::list<Style*>::iterator it, end;
 
37
 
 
38
   end = dbObs->getStyleEnd();
 
39
 
 
40
   for( it = dbObs->getStyleBegin(); it != end; ++it )
 
41
      addStyle(*it);
 
42
   repopulateList();
 
43
 
 
44
   setCurrentIndex(-1);
 
45
}
 
46
 
 
47
void StyleComboBox::addStyle(Style* style)
 
48
{
 
49
   styleObs.push_back(style);
 
50
   addObserved(style);
 
51
 
 
52
   addItem( tr(style->getName().c_str()) );
 
53
}
 
54
 
 
55
void StyleComboBox::removeAllStyles()
 
56
{
 
57
   unsigned int i;
 
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.
 
62
}
 
63
 
 
64
void StyleComboBox::notify(Observable *notifier, QVariant info)
 
65
{
 
66
   unsigned int i, size;
 
67
 
 
68
   // Notifier could be the database.
 
69
   if( notifier == dbObs && (info.toInt() == DBSTYLE || info.toInt() == DBALL) )
 
70
   {
 
71
      Style* previousSelection = getSelected(); // Remember what we had.
 
72
      
 
73
      removeAllStyles();
 
74
      std::list<Style*>::iterator it, end;
 
75
 
 
76
      end = dbObs->getStyleEnd();
 
77
 
 
78
      for( it = dbObs->getStyleBegin(); it != end; ++it )
 
79
         addStyle(*it);
 
80
      repopulateList();
 
81
 
 
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());
 
88
      else
 
89
         setCurrentIndex(-1); // Or just give up.
 
90
   }
 
91
   else if( notifier == recipeObs )
 
92
   {
 
93
      // All we care about is the style in the recipe.
 
94
      if( recipeObs->getStyle() )
 
95
         setIndexByStyleName( (recipeObs->getStyle())->getName() );
 
96
      else
 
97
         setCurrentIndex(-1); // Or just give up.
 
98
   }
 
99
   else // Otherwise, we know that one of the styles changed.
 
100
   {
 
101
      size = styleObs.size();
 
102
      for( i = 0; i < size; ++i )
 
103
         if( notifier == styleObs[i] )
 
104
         {
 
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()));
 
108
         }
 
109
   }
 
110
}
 
111
 
 
112
void StyleComboBox::setIndexByStyleName(std::string name)
 
113
{
 
114
   int ndx;
 
115
 
 
116
   ndx = findText( tr(name.c_str()), Qt::MatchExactly );
 
117
   /*
 
118
   if( ndx == -1 )
 
119
      return;
 
120
   */
 
121
   
 
122
   setCurrentIndex(ndx);
 
123
}
 
124
 
 
125
void StyleComboBox::repopulateList()
 
126
{
 
127
   unsigned int i, size;
 
128
   clear(); // Remove all items in the visible list.
 
129
 
 
130
   size = styleObs.size();
 
131
   for( i = 0; i < size; ++i )
 
132
      addItem( tr(styleObs[i]->getName().c_str()) );
 
133
}
 
134
 
 
135
Style* StyleComboBox::getSelected()
 
136
{
 
137
   if( currentIndex() >= 0 )
 
138
      return styleObs[currentIndex()];
 
139
   else
 
140
      return 0;
 
141
}
 
142
 
 
143
void StyleComboBox::observeRecipe(Recipe* rec)
 
144
{
 
145
   // Make sure caller isn't stupid.
 
146
   if( rec )
 
147
   {
 
148
      // Remove any previous association.
 
149
      if( recipeObs )
 
150
         removeObserved(recipeObs);
 
151
      
 
152
      recipeObs = rec;
 
153
      addObserved(recipeObs);
 
154
 
 
155
      if( recipeObs->getStyle() )
 
156
         setIndexByStyleName( (recipeObs->getStyle())->getName() );
 
157
      else
 
158
         setCurrentIndex(-1);
 
159
   }
 
160
}