]> Shamusworld >> Repos - schematic/blob - src/mainwin.cpp
Switched to ODBC driver for connections to MySQL database
[schematic] / src / mainwin.cpp
1 //
2 // mainwin.cpp - The Supply Chain Manager-O-Matic
3 // by James Hammons
4 // (C) 2012 Underground Software
5 //
6 // JLH = James Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  09/14/2012  Created this file
11 //
12
13 // Uncomment this for debugging...
14 //#define DEBUG
15 //#define DEBUGFOO                              // Various tool debugging...
16 //#define DEBUGTP                               // Toolpalette debugging...
17
18 #include "mainwin.h"
19 #include "about.h"
20 #include "configdialog.h"
21 #include "logindialog.h"
22 #include "newvendordialog.h"
23 #include "scmwidget.h"
24 #include "sqlsettingsdialog.h"
25 #include "vendorclassdialog.h"
26
27
28 MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
29         scmWidget(new SCMWidget(this)),
30         boldFont(new QFont),
31         loggedInUID(0)
32 {
33         setWindowIcon(QIcon(":/res/schematic.png"));
34         setWindowTitle("SCheMatic");
35
36         setUnifiedTitleAndToolBarOnMac(true);
37
38         // Create actions
39
40         quitAppAct = new QAction(tr("E&xit"), this);
41         quitAppAct->setShortcut(QKeySequence(tr("Ctrl+q")));
42         quitAppAct->setStatusTip(tr("Quit SCheMatic"));
43         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
44
45         aboutAct = new QAction(QIcon(":/res/schematic.png"), tr("&About..."), this);
46         aboutAct->setStatusTip(tr("Blatant self-promotion"));
47         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
48
49         configAct = new QAction(QIcon(":/res/schematic.png"), tr("&Configure..."), this);
50         configAct->setStatusTip(tr("Configure SCheMatic"));
51         connect(configAct, SIGNAL(triggered()), this, SLOT(HandleConfigDialog()));
52
53         vendorClassAct = new QAction(QIcon(":/res/schematic.png"), tr("&Edit Vendor Classes..."), this);
54         vendorClassAct->setStatusTip(tr("Edit Vendor Classes"));
55         connect(vendorClassAct, SIGNAL(triggered()), this, SLOT(HandleVendorClassDialog()));
56
57         newVendorAct = new QAction(QIcon(":/res/schematic.png"), tr("&Add Vendor..."), this);
58         newVendorAct->setStatusTip(tr("Create a new vendor"));
59         connect(newVendorAct, SIGNAL(triggered()), this, SLOT(HandleNewVendorDialog()));
60
61 //      helpAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&Contents..."), this);
62 //      helpAct->setStatusTip(tr("Help is available, if you should need it"));
63 //      connect(helpAct, SIGNAL(triggered()), this, SLOT(ShowHelpWin()));
64
65         // Create menus & toolbars
66
67         QMenu * menu = menuBar()->addMenu(tr("&File"));
68 //      fileMenu->addAction(powerAct);
69 //      fileMenu->addAction(pauseAct);
70 //      fileMenu->addAction(frameAdvanceAct);
71 //      fileMenu->addAction(filePickAct);
72 //      fileMenu->addAction(useCDAct);
73 //      fileMenu->addAction(configAct);
74         menu->addAction(quitAppAct);
75
76         menu = menuBar()->addMenu(tr("&Edit"));
77         menu->addAction(configAct);
78         menu->addAction(vendorClassAct);
79         menu->addAction(newVendorAct);
80
81         menu = menuBar()->addMenu(tr("&Help"));
82 //      menu->addAction(helpAct);
83         menu->addAction(aboutAct);
84
85         QToolBar * toolbar = addToolBar(tr("Stuff"));
86 //      toolbar->addAction(powerAct);
87 //      toolbar->addAction(pauseAct);
88 //      toolbar->addAction(filePickAct);
89 //      toolbar->addAction(useCDAct);
90 //      toolbar->addSeparator();
91 //      toolbar->addAction(x1Act);
92 //      toolbar->addAction(x2Act);
93 //      toolbar->addAction(x3Act);
94 //      toolbar->addSeparator();
95 //      toolbar->addAction(ntscAct);
96 //      toolbar->addAction(palAct);
97 //      toolbar->addSeparator();
98 //      toolbar->addAction(blurAct);
99 //      toolbar->addAction(fullScreenAct);
100
101         //      Create status bar
102         statusBar()->showMessage(tr("Ready"));
103
104         ReadSettings();
105         boldFont->setBold(true);
106
107         // Finally, set up database connection
108
109 //      db = QSqlDatabase::addDatabase("QMYSQL");
110         db = QSqlDatabase::addDatabase("QODBC");
111         bool ok = false;
112
113         // Prime the SQL Settings dialog (in case we need it)
114
115         SQLSettingsDialog sqlSettings;
116         sqlSettings.edit1->setText(dbDriver);
117         sqlSettings.edit2->setText(dbHostName);
118         sqlSettings.edit3->setText(dbName);
119         sqlSettings.edit4->setText(dbUserName);
120         sqlSettings.edit5->setText(dbPassword);
121
122 #if 0
123 db = QSqlDatabase::addDatabase("QODBC");
124 db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};DATABASE=agp-dbserver01;");
125 db.setUserName("xcdr");
126 db.setPassword("xcdr");
127 #endif
128         do
129         {
130                 // Set up the DB connection with saved settings
131                 QString odbc = QString("DRIVER={%1};DATABASE=%2;").arg(dbDriver).arg(dbName);
132                 db.setHostName(dbHostName);
133                 db.setDatabaseName(odbc);
134                 db.setUserName(dbUserName);
135                 db.setPassword(dbPassword);
136                 ok = db.open();
137
138 //printf("Error: %s\n", db.lastError().databaseText().toAscii().data());
139 //printf("Error: %s\n", db.lastError().driverText().toAscii().data());
140
141                 // If unsuccessful, run the SQL settings/test dialog
142                 if (!ok)
143                 {
144                         if (sqlSettings.exec())
145                         {
146                                 // User thinks this will work (hit OK button), so prime the variables
147                                 // for the next attempt
148                                 dbDriver = sqlSettings.edit1->text();
149                                 dbHostName = sqlSettings.edit2->text();
150                                 dbName = sqlSettings.edit3->text();
151                                 dbUserName = sqlSettings.edit4->text();
152                                 dbPassword = sqlSettings.edit5->text();
153                         }
154                         else
155                                 return;                                         // User cancelled the dialog, so quit
156                 }
157         }
158         while (!ok);
159
160         // Check to see how many users are in the system; if less than 2, we don't do a login
161         QSqlQuery query;
162         query.prepare("SELECT COUNT(*) FROM User");
163         query.exec();
164         query.next();
165
166         if (query.value(0).toInt() > 1)
167         {
168         // Do Login dialog
169         LoginDialog loginDlg;
170         bool done = false;
171
172         do
173         {
174                 bool accept = loginDlg.exec();
175
176                 // Check to see if user cancelled out
177                 if (!accept)
178                         done = true;
179                 else
180                 {
181 #if 0
182                         // Search DB for this username/login pair
183                         QSqlQuery query("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
184                         query.addBindValue(loginDlg.edit1->text());
185                         query.addBindValue(loginDlg.edit2->text());
186                         query.exec();
187 #else
188                         // Search DB for this username/login pair
189                         QSqlQuery query;
190                         query.prepare("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
191                         query.addBindValue(loginDlg.edit1->text());
192                         query.addBindValue(loginDlg.edit2->text());
193                         query.exec();
194 #endif
195
196                         while (query.next())
197                         {
198                                 // We have a winner!
199                                 loggedInUID = query.value(0).toInt();
200                                 fullName = query.value(1).toString();
201                                 login = query.value(2).toString();
202                                 done = true;
203                         }
204                 }
205         }
206         while (!done);
207         }
208         else
209         {
210                 fullName = "Administrator";
211                 login = "admin";
212                 loggedInUID = 1;
213         }
214
215         QString s = QString("User: %1 (%2)").arg(fullName).arg(login);
216         scmWidget->username->setText(s);
217         setCentralWidget(scmWidget);
218 }
219
220
221 void MainWindow::closeEvent(QCloseEvent * event)
222 {
223         WriteSettings();
224         event->accept(); // ignore() if can't close for some reason
225 }
226
227
228 void MainWindow::Open(void)
229 {
230 }
231
232
233 void MainWindow::ShowAboutWin(void)
234 {
235         aboutWin->show();
236 }
237
238
239 void MainWindow::HandleConfigDialog(void)
240 {
241         ConfigDialog dialog(this);
242         dialog.exec();
243 }
244
245
246 void MainWindow::HandleVendorClassDialog(void)
247 {
248         VendorClassDialog dialog(this);
249
250         if (!dialog.exec())
251                 return;
252 }
253
254
255 void MainWindow::HandleNewVendorDialog(void)
256 {
257         NewVendorDialog dialog(this);
258         FillVendorLevelCombo(dialog.combo1);
259         FillContactTypeCombo(dialog.contact->field1);
260         FillVendorClassList(dialog.list);
261
262         if (!dialog.exec())
263                 return;
264
265         // Presumably, the user has given us good data, so we try to populate the
266         // database with this new vendor data.
267         QSqlQuery query;
268         query.prepare("INSERT INTO  VALUES (?, ?, ?)");
269
270         
271 }
272
273
274 void MainWindow::FillVendorLevelCombo(QComboBox * combo)
275 {
276         QSqlQuery query;
277         query.prepare("SELECT VLID, Description FROM VendorLevel");
278         query.exec();
279
280         while (query.next())
281         {
282                 int vlid = query.value(0).toInt();
283                 QString description = query.value(1).toString();
284
285                 combo->addItem(description, vlid);
286         }
287 }
288
289
290 void MainWindow::FillContactTypeCombo(QComboBox * combo)
291 {
292         QSqlQuery query;
293         query.prepare("SELECT CTID, Description FROM ContactType");
294         query.exec();
295
296         while (query.next())
297         {
298                 int ctid = query.value(0).toInt();
299                 QString description = query.value(1).toString();
300
301                 combo->addItem(description, ctid);
302         }
303 }
304
305
306 void MainWindow::FillVendorClassList(QListWidget * list)
307 {
308         std::vector<VendorType> groupList;
309
310         // Pull in definitions from DB for Vendor Classes/Groups
311         QSqlQuery query1;
312         query1.prepare("SELECT vgid, description FROM VendorGroup ORDER BY seqNo");
313         query1.exec();
314
315         while (query1.next())
316         {
317                 VendorType v;
318                 v.key         = query1.value(0).toInt();
319                 v.description = query1.value(1).toString();
320                 groupList.push_back(v);
321         }
322
323 //      QSqlQuery query2;
324         query1.prepare("SELECT vtid, vgid, description FROM VendorType ORDER BY seqNo");
325         query1.exec();
326
327         int previousID = -1, groupListIndex = 0;
328         QListWidgetItem * item;
329
330         while (query1.next())
331         {
332 //              VendorType v;
333                 int vtid            = query1.value(0).toInt();
334                 int vgid            = query1.value(1).toInt();
335                 QString description = query1.value(2).toString();
336
337                 // Check to see if we need to insert new header yet.
338                 // If we're not still in same group, push the next group header into the list
339                 // and continue
340                 if (previousID != vgid)
341                 {
342                         item  = new QListWidgetItem(groupList[groupListIndex].description);
343                         item->setData(Qt::UserRole, groupList[groupListIndex++].key);
344                         item->setFont(*boldFont);
345                         list->addItem(item);
346                         previousID = vgid;
347                 }
348
349                 item  = new QListWidgetItem(description);
350                 item->setData(Qt::UserRole, vtid);
351                 item->setCheckState(Qt::Unchecked);
352                 list->addItem(item);
353         }
354 }
355
356
357 void MainWindow::ReadSettings(void)
358 {
359         QSettings settings("Underground Software", "SCheMatic");
360         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
361         QSize size = settings.value("size", QSize(400, 400)).toSize();
362         resize(size);
363         move(pos);
364 //      pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
365 //      size = settings.value("charWndSize", QSize(200, 200)).toSize();
366 //      ((TTEdit *)qApp)->charWnd->resize(size);
367 //      ((TTEdit *)qApp)->charWnd->move(pos);
368
369         dbDriver = settings.value("dbDriver", "myodbc-5.1").toString();
370         dbHostName = settings.value("dbHostName", "localhost").toString();
371         dbName = settings.value("dbName", "schematic").toString();
372         dbUserName = settings.value("dbUserName", "scm_user").toString();
373         dbPassword = settings.value("dbPassword", "scm_user").toString();
374 }
375
376
377 void MainWindow::WriteSettings(void)
378 {
379         QSettings settings("Underground Software", "SCheMatic");
380         settings.setValue("pos", pos());
381         settings.setValue("size", size());
382 //      settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
383 //      settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
384
385         settings.setValue("dbDriver", dbDriver);
386         settings.setValue("dbHostName", dbHostName);
387         settings.setValue("dbName", dbName);
388         settings.setValue("dbUserName", dbUserName);
389         settings.setValue("dbPassword", dbPassword);
390 }
391