~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to YeastDialog.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
 
 * YeastDialog.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 <QWidget>
20
 
#include <QDialog>
21
 
#include <QInputDialog>
22
 
#include <QString>
23
 
#include <string>
24
 
#include <list>
25
 
#include "YeastDialog.h"
26
 
#include "observable.h"
27
 
#include "database.h"
28
 
#include "recipe.h"
29
 
#include "MainWindow.h"
30
 
#include "yeast.h"
31
 
#include "YeastEditor.h"
32
 
 
33
 
YeastDialog::YeastDialog(MainWindow* parent)
34
 
        : QDialog(parent)
35
 
{
36
 
   setupUi(this);
37
 
   mainWindow = parent;
38
 
   yeastEditor = new YeastEditor(this);
39
 
   dbObs = 0;
40
 
   numYeasts = 0;
41
 
 
42
 
   connect( pushButton_addToRecipe, SIGNAL( clicked() ), this, SLOT( addYeast() ) );
43
 
   connect( pushButton_edit, SIGNAL( clicked() ), this, SLOT( editSelected() ) );
44
 
   connect( pushButton_new, SIGNAL( clicked() ), this, SLOT( newYeast() ) );
45
 
   connect( pushButton_remove, SIGNAL(clicked()), this, SLOT( removeYeast() ) );
46
 
}
47
 
 
48
 
void YeastDialog::removeYeast()
49
 
{
50
 
   QModelIndexList selected = yeastTableWidget->selectedIndexes();
51
 
   int row, size, i;
52
 
 
53
 
   size = selected.size();
54
 
   if( size == 0 )
55
 
      return;
56
 
 
57
 
   // Make sure only one row is selected.
58
 
   row = selected[0].row();
59
 
   for( i = 1; i < size; ++i )
60
 
   {
61
 
      if( selected[i].row() != row )
62
 
         return;
63
 
   }
64
 
 
65
 
   Yeast *yeast = yeastTableWidget->getModel()->getYeast(row);
66
 
   dbObs->removeYeast(yeast);
67
 
}
68
 
 
69
 
void YeastDialog::notify(Observable *notifier, QVariant info)
70
 
{
71
 
   if( notifier != dbObs || (info.toInt() != DBYEAST && info.toInt() != DBALL) )
72
 
      return;
73
 
 
74
 
   yeastTableWidget->getModel()->removeAll();
75
 
   populateTable();
76
 
 
77
 
}
78
 
 
79
 
void YeastDialog::startObservingDB()
80
 
{
81
 
   dbObs = Database::getDatabase();
82
 
   setObserved(dbObs);
83
 
   populateTable();
84
 
}
85
 
 
86
 
void YeastDialog::populateTable()
87
 
{
88
 
   std::list<Yeast*>::iterator it, end;
89
 
 
90
 
   if( ! Database::isInitialized() )
91
 
      return;
92
 
 
93
 
   numYeasts = dbObs->getNumYeasts();
94
 
   end = dbObs->getYeastEnd();
95
 
   for( it = dbObs->getYeastBegin(); it != end; ++it )
96
 
      yeastTableWidget->getModel()->addYeast(*it);
97
 
}
98
 
 
99
 
void YeastDialog::addYeast()
100
 
{
101
 
   QModelIndexList selected = yeastTableWidget->selectedIndexes();
102
 
   int row, size, i;
103
 
 
104
 
   size = selected.size();
105
 
   if( size == 0 )
106
 
      return;
107
 
 
108
 
   // Make sure only one row is selected.
109
 
   row = selected[0].row();
110
 
   for( i = 1; i < size; ++i )
111
 
   {
112
 
      if( selected[i].row() != row )
113
 
         return;
114
 
   }
115
 
 
116
 
   Yeast *yeast = yeastTableWidget->getModel()->getYeast(row);
117
 
   mainWindow->addYeastToRecipe(new Yeast(*yeast) ); // Need to add a copy so we don't change the database.
118
 
}
119
 
 
120
 
void YeastDialog::editSelected()
121
 
{
122
 
   QModelIndexList selected = yeastTableWidget->selectedIndexes();
123
 
   int row, size, i;
124
 
 
125
 
   size = selected.size();
126
 
   if( size == 0 )
127
 
      return;
128
 
 
129
 
   // Make sure only one row is selected.
130
 
   row = selected[0].row();
131
 
   for( i = 1; i < size; ++i )
132
 
   {
133
 
      if( selected[i].row() != row )
134
 
         return;
135
 
   }
136
 
 
137
 
   Yeast *yeast = yeastTableWidget->getModel()->getYeast(row);
138
 
   yeastEditor->setYeast(yeast);
139
 
   yeastEditor->show();
140
 
}
141
 
 
142
 
void YeastDialog::newYeast()
143
 
{
144
 
   QString name = QInputDialog::getText(this, tr("Yeast name"),
145
 
                                              tr("Yeast name:"));
146
 
   if( name.isEmpty() )
147
 
      return;
148
 
 
149
 
   Yeast* y = new Yeast();
150
 
   std::string stdname = name.toStdString();
151
 
   y->setName(stdname);
152
 
 
153
 
   dbObs->addYeast(y);
154
 
   yeastEditor->setYeast(y);
155
 
   yeastEditor->show();
156
 
}