~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/MaltinessWidget.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
 * MaltinessWidget.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 <QSize>
 
20
#include <QPainter>
 
21
#include <QSizePolicy>
 
22
#include <Qt>
 
23
#include <QFrame>
 
24
#include "MaltinessWidget.h"
 
25
 
 
26
MaltinessWidget::MaltinessWidget(QWidget* parent) : QWidget(parent)
 
27
{
 
28
   setup();
 
29
}
 
30
 
 
31
MaltinessWidget::MaltinessWidget(Recipe* recipe)
 
32
{
 
33
   setup();
 
34
   observeRecipe(recipe);
 
35
}
 
36
 
 
37
void MaltinessWidget::setup()
 
38
{
 
39
   QSize size(90,30);
 
40
 
 
41
   label = new QLabel(this);
 
42
   // Want to specify a minimum size and have it expand if able.
 
43
   label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
 
44
   // Align text in center, vertically and horizontally.
 
45
   label->setAlignment(Qt::AlignCenter);
 
46
   // Add a border.
 
47
   label->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
 
48
 
 
49
   palette = label->palette();
 
50
 
 
51
   // Set size policy of the MaltinessWidget
 
52
   //sPolicy = QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
 
53
   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
 
54
   setMinimumSize(size);
 
55
}
 
56
 
 
57
void MaltinessWidget::observeRecipe(Recipe* recipe)
 
58
{
 
59
   recObs = recipe;
 
60
   if( recipe != 0 )
 
61
      setObserved(recipe);
 
62
 
 
63
   update();
 
64
}
 
65
 
 
66
void MaltinessWidget::paintEvent(QPaintEvent*)
 
67
{
 
68
   if( recObs == 0 )
 
69
      return;
 
70
 
 
71
   QPainter painter(this);
 
72
 
 
73
   label->resize(size());
 
74
   
 
75
   palette.setColor(QPalette::Active, QPalette::Window, bgColor());
 
76
   label->setPalette(palette);
 
77
   label->setText(fgText());
 
78
 
 
79
   label->render(&painter);
 
80
}
 
81
 
 
82
QSize MaltinessWidget::sizeHint() const
 
83
{
 
84
   return label->sizeHint();
 
85
}
 
86
 
 
87
/*
 
88
QSizePolicy MaltinessWidget::sizePolicy() const
 
89
{
 
90
   return sPolicy;
 
91
}
 
92
*/
 
93
 
 
94
QColor MaltinessWidget::bgColor()
 
95
{
 
96
   switch(region())
 
97
   {
 
98
      case CLOYING:
 
99
         return QColor::fromRgb(252,91,10);
 
100
      case EXTRAMALTY:
 
101
         return QColor::fromRgb(252,134,10);
 
102
      case SLIGHTLYMALTY:
 
103
         return QColor::fromRgb(246,195,6);
 
104
      case BALANCED:
 
105
         return QColor::fromRgb(235,248,11);
 
106
      case SLIGHTLYHOPPY:
 
107
         return QColor::fromRgb(148,251,12);
 
108
      case EXTRAHOPPY:
 
109
         return QColor::fromRgb(12,248,26);
 
110
      case HARSH:
 
111
         return QColor::fromRgb(12,178,13);
 
112
      default:
 
113
         return QColor::fromRgb(255,255,255);
 
114
   }
 
115
}
 
116
 
 
117
QString MaltinessWidget::fgText()
 
118
{
 
119
   switch(region())
 
120
   {
 
121
      case CLOYING:
 
122
         return QString("<b>Cloying</b>");
 
123
      case EXTRAMALTY:
 
124
         return QString("<b>Extra malty</b>");
 
125
      case SLIGHTLYMALTY:
 
126
         return QString("<b>Slightly malty</b>");
 
127
      case BALANCED:
 
128
         return QString("<b>Balanced</b>");
 
129
      case SLIGHTLYHOPPY:
 
130
         return QString("<b>Slightly hoppy</b>");
 
131
      case EXTRAHOPPY:
 
132
         return QString("<b>Extra hoppy</b>");
 
133
      case HARSH:
 
134
         return QString("<b>Way hoppy</b>");
 
135
      default:
 
136
         return QString("");
 
137
   }
 
138
}
 
139
 
 
140
int MaltinessWidget::region()
 
141
{
 
142
   double ibu;
 
143
   double points;
 
144
 
 
145
   if( recObs == 0 )
 
146
      return -1;
 
147
 
 
148
   ibu = recObs->getIBU();
 
149
   points = (recObs->getOg() - 1)*1000;
 
150
 
 
151
   if( (11./3.)*ibu-5./3. < points )
 
152
      return CLOYING;
 
153
   else if( 3*ibu-5 < points && points <= (11./3.)*ibu-5./3. )
 
154
      return EXTRAMALTY;
 
155
   else if( (7./3.)*ibu-5./3. < points && points <= 3*ibu-5 )
 
156
      return SLIGHTLYMALTY;
 
157
   else if( 2*ibu-5 < points && points <= (7./3.)*ibu-5./3. )
 
158
      return BALANCED;
 
159
   else if( (5./3.)*ibu-10./3. < points && points <= 2*ibu-5 )
 
160
      return SLIGHTLYHOPPY;
 
161
   else if( (5./4.)*ibu-3.75 < points && points <= (5./3.)*ibu-10./3. )
 
162
      return EXTRAHOPPY;
 
163
   else
 
164
      return HARSH;
 
165
}
 
166
 
 
167
void MaltinessWidget::notify(Observable *notifier, QVariant /*info*/)
 
168
{
 
169
   if( notifier != recObs )
 
170
      return;
 
171
 
 
172
   update();
 
173
}
 
 
b'\\ No newline at end of file'