~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to observable.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
 
 * observable.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 <vector>
20
 
#include "observable.h"
21
 
 
22
 
void Observable::setDefaults()
23
 
{
24
 
   observers = std::vector<Observer*>();
25
 
   doNotify = true;
26
 
}
27
 
 
28
 
Observable::Observable()
29
 
{
30
 
   setDefaults();
31
 
}
32
 
 
33
 
Observable::Observable(Observer* obs)
34
 
{
35
 
   setDefaults();
36
 
   observers.push_back(obs);
37
 
}
38
 
 
39
 
void Observable::addObserver(Observer* obs)
40
 
{
41
 
   observers.push_back(obs);
42
 
}
43
 
 
44
 
bool Observable::removeObserver(Observer* obs)
45
 
{
46
 
   std::vector<Observer*>::iterator iter;
47
 
   
48
 
   for( iter = observers.begin(); iter != observers.end(); ++iter )
49
 
      if( *iter == obs )
50
 
      {
51
 
         observers.erase(iter);
52
 
         return true;
53
 
      }
54
 
   
55
 
   return false;
56
 
}
57
 
 
58
 
void Observable::hasChanged(QVariant info)
59
 
{
60
 
   notifyObservers(info);
61
 
}
62
 
 
63
 
void Observable::notifyObservers(QVariant info)
64
 
{
65
 
   unsigned int i, size=observers.size();
66
 
 
67
 
   if( ! doNotify )
68
 
      return;
69
 
 
70
 
   for( i = 0; i < size; ++i )
71
 
      observers[i]->notify(this, info);
72
 
}
73
 
 
74
 
void Observable::forceNotify()
75
 
{
76
 
   notifyObservers(QVariant());
77
 
}
78
 
 
79
 
void Observable::disableNotification()
80
 
{
81
 
   doNotify = false;
82
 
}
83
 
 
84
 
void Observable::reenableNotification()
85
 
{
86
 
   doNotify = true;
87
 
}