File: databasetool.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (230 lines) | stat: -rw-r--r-- 8,438 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
  databasetool.cpp
  -------------------
  Database editor for QBrew
  -------------------
  Copyright 2005-2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QDir>
#include <QFile>
#include <QHeaderView>
#include <QMessageBox>
#include <QTableView>

#include "data.h"
#include "resource.h"

#include "graindelegate.h"
#include "grainmodel.h"
#include "hopdelegate.h"
#include "hopmodel.h"
#include "miscdelegate.h"
#include "miscmodel.h"
#include "styledelegate.h"
#include "stylemodel.h"
#include "databasetool.h"

//////////////////////////////////////////////////////////////////////////////
// Construction, Destruction                                                //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// DatabaseTool()
// --------------
// Constructor

DatabaseTool::DatabaseTool(QWidget* parent)
    : QMainWindow(parent), grainmodel_(0), hopmodel_(0), miscmodel_(0),
      modified_(false)
{
    ui.setupUi(this);
    statusBar()->hide();
    ui.actionsave->setEnabled(false);

    // setup actions
    connect(ui.actionsave, SIGNAL(triggered()), this, SLOT(fileSave()));
    connect(ui.actionquit, SIGNAL(triggered()), this, SLOT(close()));

    // get current font information, for sizing
    QFontMetrics fm(font());
    unsigned mh = (unsigned)(fm.lineSpacing() * 1.5);
    unsigned mw = fm.width('M');

    // grain page
    QWidget *widget = new QWidget();
    grainpage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Grains"));

    grains_ = Data::instance()->grainmap_.values();
    grainmodel_ = new GrainModel(this, &grains_);
    grainpage.view->setModel(grainmodel_);
    QItemDelegate *delegate = new GrainDelegate(this);
    grainpage.view->setItemDelegate(delegate);

    grainpage.view->verticalHeader()->setDefaultSectionSize(mh);
    grainpage.view->verticalHeader()->hide();
    grainpage.view->horizontalHeader()->setClickable(true);
    grainpage.view->horizontalHeader()->setHighlightSections(false);

    grainpage.view->setColumnWidth(GrainModel::NAME, 20*mw);
    grainpage.view->setColumnHidden(GrainModel::WEIGHT, true);
    grainpage.view->setColumnWidth(GrainModel::EXTRACT, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::COLOR, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::TYPE, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::USE, 8*mw);

    // hop page
    widget = new QWidget();
    hoppage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Hops"));

    hops_ = Data::instance()->hopmap_.values();
    hopmodel_ = new HopModel(this, &hops_);
    hoppage.view->setModel(hopmodel_);
    delegate = new HopDelegate(this);
    hoppage.view->setItemDelegate(delegate);

    hoppage.view->verticalHeader()->setDefaultSectionSize(mh);
    hoppage.view->verticalHeader()->hide();
    hoppage.view->horizontalHeader()->setClickable(true);
    hoppage.view->horizontalHeader()->setHighlightSections(false);

    hoppage.view->setColumnHidden(HopModel::WEIGHT, true);
    hoppage.view->setColumnHidden(HopModel::TIME, true);
    hoppage.view->setColumnHidden(HopModel::TYPE, true);
    hoppage.view->setColumnWidth(HopModel::NAME, 20*mw);
    hoppage.view->setColumnWidth(HopModel::ALPHA, 8*mw);

    // misc page
    widget = new QWidget();
    miscpage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Miscellaneous"));

    miscs_ = Data::instance()->miscmap_.values();
    miscmodel_ = new MiscModel(this, &miscs_);
    miscpage.view->setModel(miscmodel_);
    delegate = new MiscDelegate(this);
    miscpage.view->setItemDelegate(delegate);

    miscpage.view->verticalHeader()->setDefaultSectionSize(mh);
    miscpage.view->verticalHeader()->hide();
    miscpage.view->horizontalHeader()->setClickable(true);
    miscpage.view->horizontalHeader()->setHighlightSections(false);

    miscpage.view->setColumnHidden(MiscModel::QUANTITY, true);
    miscpage.view->setColumnWidth(MiscModel::NAME, 20*mw);
    miscpage.view->setColumnWidth(MiscModel::TYPE, 8*mw);
    miscpage.view->horizontalHeader()->setStretchLastSection(true);

    // style page
    widget = new QWidget();
    stylepage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Styles"));

    styles_ = Data::instance()->stylemap_.values();
    stylemodel_ = new StyleModel(this, &styles_);
    stylepage.view->setModel(stylemodel_);
    delegate = new StyleDelegate(this);
    stylepage.view->setItemDelegate(delegate);

    stylepage.view->verticalHeader()->setDefaultSectionSize(mh);
    stylepage.view->verticalHeader()->hide();
    stylepage.view->horizontalHeader()->setClickable(true);
    stylepage.view->horizontalHeader()->setHighlightSections(false);

    stylepage.view->setColumnWidth(StyleModel::NAME, 20*mw);
    stylepage.view->setColumnWidth(StyleModel::OGLOW, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::OGHI, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::FGLOW, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::FGHI, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::IBULOW, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::IBUHI, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::SRMLOW, 8*mw);
    stylepage.view->setColumnWidth(StyleModel::SRMHI, 8*mw);

    // setup connections
    connect(grainmodel_, SIGNAL(modified()),
            this, SLOT(dataModified()));
    connect(grainpage.addbutton, SIGNAL(clicked()),
            grainpage.view, SLOT(addIngredient()));
    connect(grainpage.removebutton, SIGNAL(clicked()),
            grainpage.view, SLOT(removeIngredient()));

    connect(hopmodel_, SIGNAL(modified()),
            this, SLOT(dataModified()));
    connect(hoppage.addbutton, SIGNAL(clicked()),
            hoppage.view, SLOT(addIngredient()));
    connect(hoppage.removebutton, SIGNAL(clicked()),
            hoppage.view, SLOT(removeIngredient()));

    connect(miscmodel_, SIGNAL(modified()),
            this, SLOT(dataModified()));
    connect(miscpage.addbutton, SIGNAL(clicked()),
            miscpage.view, SLOT(addIngredient()));
    connect(miscpage.removebutton, SIGNAL(clicked()),
            miscpage.view, SLOT(removeIngredient()));

    connect(stylemodel_, SIGNAL(modified()),
            this, SLOT(dataModified()));
    connect(stylepage.addbutton, SIGNAL(clicked()),
            stylepage.view, SLOT(addIngredient()));
    connect(stylepage.removebutton, SIGNAL(clicked()),
            stylepage.view, SLOT(removeIngredient()));

    grainmodel_->flush();
    hopmodel_->flush();
    miscmodel_->flush();
    stylemodel_->flush();
}

DatabaseTool::~DatabaseTool() {}

void DatabaseTool::fileSave()
{
    // TODO: use QDesktopServices in next non-bugfix release (0.5.0)
    QString localbase = QDIR_HOME + "/." + Resource::DATA_FILE;

    QFileInfo finfo(localbase);
    if (finfo.exists() && !finfo.isWritable()) {
        // no write permission
        QMessageBox::warning(this, Resource::TITLE,
                             tr("<p>Unable to save the database."
                                "You do not have permission "
                                "to write to %1").arg(localbase));
    } else {
        // sync with Data...
        Data::instance()->clearGrains();
        foreach(Grain grain, grains_) {
            Data::instance()->insertGrain(grain);
        }
        Data::instance()->clearHops();
        foreach(Hop hop, hops_) {
            Data::instance()->insertHop(hop);
        }
        Data::instance()->clearMiscs();
        foreach(Misc misc, miscs_) {
            Data::instance()->insertMisc(misc);
        }
        Data::instance()->clearStyles();
        foreach(Style style, styles_) {
            Data::instance()->insertStyle(style);
        }

        if (!Data::instance()->saveData(localbase)) {
            // error in saving file
            QMessageBox::warning(this, Resource::TITLE,
                                 tr("<p>Unable to save the database."
                                    "Error in saving %1").arg(localbase));
        }
        ui.actionsave->setEnabled(false);
        modified_ = false;
    }
}

void DatabaseTool::dataModified()
{
    ui.actionsave->setEnabled(true);
    modified_ = true;
}