]> Shamusworld >> Repos - schematic/blob - src/mainwin.cpp
Minor tweaks to ODBC connection string to work better on win32.
[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("QODBC");
110         bool ok = false;
111
112         // Prime the SQL Settings dialog (in case we need it)
113
114         SQLSettingsDialog sqlSettings;
115         sqlSettings.edit1->setText(dbDriver);
116         sqlSettings.edit2->setText(dbHostName);
117         sqlSettings.edit3->setText(dbName);
118         sqlSettings.edit4->setText(dbUserName);
119         sqlSettings.edit5->setText(dbPassword);
120
121         do
122         {
123                 // Set up the ODBC DB connection with saved settings
124                 // NB: For this to work properly on 64-bit Windows, you need to use the 32-bit driver!
125                 QString odbc = QString("DRIVER={%1};DATABASE=%2;SERVER=%3;UID=%4;PASSWORD=%5")
126                         .arg(dbDriver).arg(dbName).arg(dbHostName).arg(dbUserName).arg(dbPassword);
127                 db.setDatabaseName(odbc);
128                 ok = db.open();
129
130                 // If unsuccessful, run the SQL settings/test dialog
131                 if (!ok)
132                 {
133                         sqlSettings.error1->setText(db.lastError().databaseText());
134                         sqlSettings.error2->setText(db.lastError().driverText());
135
136                         if (sqlSettings.exec())
137                         {
138                                 // User thinks this will work (hit OK button), so prime the variables
139                                 // for the next attempt
140                                 dbDriver = sqlSettings.edit1->text();
141                                 dbHostName = sqlSettings.edit2->text();
142                                 dbName = sqlSettings.edit3->text();
143                                 dbUserName = sqlSettings.edit4->text();
144                                 dbPassword = sqlSettings.edit5->text();
145                         }
146                         else
147                                 return;                                         // User cancelled the dialog, so quit
148                 }
149         }
150         while (!ok);
151
152         // Check to see how many users are in the system; if less than 2, we don't do a login
153         QSqlQuery query;
154         query.prepare("SELECT COUNT(*) FROM User");
155         query.exec();
156         query.next();
157
158         if (query.value(0).toInt() > 1)
159         {
160                 // Do Login dialog
161                 LoginDialog loginDlg;
162                 bool done = false;
163
164                 do
165                 {
166                         bool accept = loginDlg.exec();
167
168                         // Check to see if user cancelled out
169                         if (!accept)
170                                 done = true;
171                         else
172                         {
173                                 // Search DB for this username/login pair
174                                 QSqlQuery query;
175                                 query.prepare("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
176                                 query.addBindValue(loginDlg.edit1->text());
177                                 query.addBindValue(loginDlg.edit2->text());
178                                 query.exec();
179
180                                 while (query.next())
181                                 {
182                                         // We have a winner!
183                                         loggedInUID = query.value(0).toInt();
184                                         fullName = query.value(1).toString();
185                                         login = query.value(2).toString();
186                                         done = true;
187                                 }
188                         }
189                 }
190                 while (!done);
191         }
192         else
193         {
194                 fullName = "Administrator";
195                 login = "admin";
196                 loggedInUID = 1;
197         }
198
199         QString s = QString("User: %1 (%2)").arg(fullName).arg(login);
200         scmWidget->username->setText(s);
201         setCentralWidget(scmWidget);
202 }
203
204
205 void MainWindow::closeEvent(QCloseEvent * event)
206 {
207         WriteSettings();
208         event->accept(); // ignore() if can't close for some reason
209 }
210
211
212 void MainWindow::Open(void)
213 {
214 }
215
216
217 void MainWindow::ShowAboutWin(void)
218 {
219         aboutWin->show();
220 }
221
222
223 void MainWindow::HandleConfigDialog(void)
224 {
225         ConfigDialog dialog(this);
226         dialog.exec();
227 }
228
229
230 void MainWindow::HandleVendorClassDialog(void)
231 {
232         VendorClassDialog dialog(this);
233
234         if (!dialog.exec())
235                 return;
236 }
237
238
239 void MainWindow::HandleNewVendorDialog(void)
240 {
241         NewVendorDialog dialog(this);
242         FillVendorLevelCombo(dialog.combo1);
243         FillContactTypeCombo(dialog.contact->field1);
244         FillVendorClassList(dialog.list);
245
246         if (!dialog.exec())
247                 return;
248
249         // Presumably, the user has given us good data, so we try to populate the
250         // database with this new vendor data.
251         QSqlQuery query;
252         query.prepare("INSERT INTO  VALUES (?, ?, ?)");
253
254         
255 }
256
257
258 void MainWindow::FillVendorLevelCombo(QComboBox * combo)
259 {
260         QSqlQuery query;
261         query.prepare("SELECT VLID, Description FROM VendorLevel");
262         query.exec();
263
264         while (query.next())
265         {
266                 int vlid = query.value(0).toInt();
267                 QString description = query.value(1).toString();
268
269                 combo->addItem(description, vlid);
270         }
271 }
272
273
274 void MainWindow::FillContactTypeCombo(QComboBox * combo)
275 {
276         QSqlQuery query;
277         query.prepare("SELECT CTID, Description FROM ContactType");
278         query.exec();
279
280         while (query.next())
281         {
282                 int ctid = query.value(0).toInt();
283                 QString description = query.value(1).toString();
284
285                 combo->addItem(description, ctid);
286         }
287 }
288
289
290 void MainWindow::FillVendorClassList(QListWidget * list)
291 {
292         std::vector<VendorType> groupList;
293
294         // Pull in definitions from DB for Vendor Classes/Groups
295         QSqlQuery query1;
296         query1.prepare("SELECT vgid, description FROM VendorGroup ORDER BY seqNo");
297         query1.exec();
298
299         while (query1.next())
300         {
301                 VendorType v;
302                 v.key         = query1.value(0).toInt();
303                 v.description = query1.value(1).toString();
304                 groupList.push_back(v);
305         }
306
307 //      QSqlQuery query2;
308         query1.prepare("SELECT vtid, vgid, description FROM VendorType ORDER BY seqNo");
309         query1.exec();
310
311         int previousID = -1, groupListIndex = 0;
312         QListWidgetItem * item;
313
314         while (query1.next())
315         {
316 //              VendorType v;
317                 int vtid            = query1.value(0).toInt();
318                 int vgid            = query1.value(1).toInt();
319                 QString description = query1.value(2).toString();
320
321                 // Check to see if we need to insert new header yet.
322                 // If we're not still in same group, push the next group header into the list
323                 // and continue
324                 if (previousID != vgid)
325                 {
326                         item  = new QListWidgetItem(groupList[groupListIndex].description);
327                         item->setData(Qt::UserRole, groupList[groupListIndex++].key);
328                         item->setFont(*boldFont);
329                         list->addItem(item);
330                         previousID = vgid;
331                 }
332
333                 item  = new QListWidgetItem(description);
334                 item->setData(Qt::UserRole, vtid);
335                 item->setCheckState(Qt::Unchecked);
336                 list->addItem(item);
337         }
338 }
339
340
341 void MainWindow::ReadSettings(void)
342 {
343         QSettings settings("Underground Software", "SCheMatic");
344         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
345         QSize size = settings.value("size", QSize(400, 400)).toSize();
346         resize(size);
347         move(pos);
348 //      pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
349 //      size = settings.value("charWndSize", QSize(200, 200)).toSize();
350 //      ((TTEdit *)qApp)->charWnd->resize(size);
351 //      ((TTEdit *)qApp)->charWnd->move(pos);
352
353         dbDriver = settings.value("dbDriver", "myodbc-5.1").toString();
354         dbHostName = settings.value("dbHostName", "localhost").toString();
355         dbName = settings.value("dbName", "schematic").toString();
356         dbUserName = settings.value("dbUserName", "scm_user").toString();
357         dbPassword = settings.value("dbPassword", "scm_user").toString();
358 }
359
360
361 void MainWindow::WriteSettings(void)
362 {
363         QSettings settings("Underground Software", "SCheMatic");
364         settings.setValue("pos", pos());
365         settings.setValue("size", size());
366 //      settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
367 //      settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
368
369         settings.setValue("dbDriver", dbDriver);
370         settings.setValue("dbHostName", dbHostName);
371         settings.setValue("dbName", dbName);
372         settings.setValue("dbUserName", dbUserName);
373         settings.setValue("dbPassword", dbPassword);
374 }
375