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