~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/MiscDialog.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
 * MiscDialog.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 "MiscDialog.h"
 
26
#include "observable.h"
 
27
#include "database.h"
 
28
#include "recipe.h"
 
29
#include "MainWindow.h"
 
30
#include "misc.h"
 
31
#include "MiscEditor.h"
 
32
 
 
33
MiscDialog::MiscDialog(MainWindow* parent)
 
34
        : QDialog(parent)
 
35
{
 
36
   setupUi(this);
 
37
   mainWindow = parent;
 
38
   dbObs = 0;
 
39
   numMiscs = 0;
 
40
   miscEdit = new MiscEditor(this);
 
41
 
 
42
   connect( pushButton_addToRecipe, SIGNAL( clicked() ), this, SLOT( addMisc() ) );
 
43
   connect( pushButton_new, SIGNAL(clicked()), this, SLOT( newMisc() ) );
 
44
   connect( pushButton_edit, SIGNAL(clicked()), this, SLOT(editSelected()) );
 
45
   connect( pushButton_remove, SIGNAL(clicked()), this, SLOT(removeMisc()) );
 
46
}
 
47
 
 
48
void MiscDialog::removeMisc()
 
49
{
 
50
   QModelIndexList selected = miscTableWidget->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
   Misc* m = miscTableWidget->getModel()->getMisc(row);
 
66
   dbObs->removeMisc(m);
 
67
}
 
68
 
 
69
void MiscDialog::notify(Observable *notifier, QVariant info)
 
70
{
 
71
   if( notifier != dbObs || (info.toInt() != DBMISC && info.toInt() != DBALL) )
 
72
      return;
 
73
 
 
74
   miscTableWidget->getModel()->removeAll();
 
75
   populateTable();
 
76
}
 
77
 
 
78
void MiscDialog::startObservingDB()
 
79
{
 
80
   dbObs = Database::getDatabase();
 
81
   setObserved(dbObs);
 
82
   populateTable();
 
83
}
 
84
 
 
85
void MiscDialog::populateTable()
 
86
{
 
87
   std::list<Misc*>::iterator it, end;
 
88
 
 
89
   if( ! Database::isInitialized() )
 
90
      return;
 
91
 
 
92
   numMiscs = dbObs->getNumMiscs();
 
93
   end = dbObs->getMiscEnd();
 
94
   for( it = dbObs->getMiscBegin(); it != end; ++it )
 
95
      miscTableWidget->getModel()->addMisc(*it);
 
96
}
 
97
 
 
98
void MiscDialog::addMisc()
 
99
{
 
100
   QModelIndexList selected = miscTableWidget->selectedIndexes();
 
101
   int row, size, i;
 
102
 
 
103
   size = selected.size();
 
104
   if( size == 0 )
 
105
      return;
 
106
 
 
107
   // Make sure only one row is selected.
 
108
   row = selected[0].row();
 
109
   for( i = 1; i < size; ++i )
 
110
   {
 
111
      if( selected[i].row() != row )
 
112
         return;
 
113
   }
 
114
 
 
115
   Misc *misc = miscTableWidget->getModel()->getMisc(row);
 
116
   mainWindow->addMiscToRecipe(new Misc(*misc) ); // Need to add a copy so we don't change the database.
 
117
}
 
118
 
 
119
void MiscDialog::editSelected()
 
120
{
 
121
   QModelIndexList selected = miscTableWidget->selectedIndexes();
 
122
   int row, size, i;
 
123
 
 
124
   size = selected.size();
 
125
   if( size == 0 )
 
126
      return;
 
127
 
 
128
   // Make sure only one row is selected.
 
129
   row = selected[0].row();
 
130
   for( i = 1; i < size; ++i )
 
131
   {
 
132
      if( selected[i].row() != row )
 
133
         return;
 
134
   }
 
135
 
 
136
   Misc* m = miscTableWidget->getModel()->getMisc(row);
 
137
   miscEdit->setMisc(m);
 
138
   miscEdit->show();
 
139
}
 
140
 
 
141
void MiscDialog::newMisc()
 
142
{
 
143
   QString name = QInputDialog::getText(this, tr("Misc name"),
 
144
                                              tr("Misc name:"));
 
145
   if(name.isEmpty())
 
146
      return;
 
147
 
 
148
   Misc *m = new Misc();
 
149
   std::string stdname = name.toStdString();
 
150
   m->setName(stdname);
 
151
 
 
152
   dbObs->addMisc(m);
 
153
   miscEdit->setMisc(m);
 
154
   miscEdit->show();
 
155
}