]> Shamusworld >> Repos - schematic/blobdiff - src/mainwin.cpp
Move DB access to NoteDialog class, new AlertDialog class.
[schematic] / src / mainwin.cpp
index b7e174818d91a468e5b7156eeeff78a957d6b3c7..5feacc5c1cb9c082d91770d65b9b3d883dd757c7 100644 (file)
 //#define DEBUGTP                              // Toolpalette debugging...
 
 #include "mainwin.h"
-#include <QtSql>
-//#include "main.h"
 #include "about.h"
+#include "configdialog.h"
 #include "logindialog.h"
+#include "newvendordialog.h"
+#include "scmwidget.h"
+#include "sqlsettingsdialog.h"
+#include "vendorclassdialog.h"
 
 
 MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
+       boldFont(new QFont),
        loggedInUID(0)
 {
        setWindowIcon(QIcon(":/res/schematic.png"));
@@ -41,6 +45,18 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
        aboutAct->setStatusTip(tr("Blatant self-promotion"));
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
 
+       configAct = new QAction(QIcon(":/res/schematic.png"), tr("&Configure..."), this);
+       configAct->setStatusTip(tr("Configure SCheMatic"));
+       connect(configAct, SIGNAL(triggered()), this, SLOT(HandleConfigDialog()));
+
+       vendorClassAct = new QAction(QIcon(":/res/schematic.png"), tr("&Edit Vendor Classes..."), this);
+       vendorClassAct->setStatusTip(tr("Edit Vendor Classes"));
+       connect(vendorClassAct, SIGNAL(triggered()), this, SLOT(HandleVendorClassDialog()));
+
+       newVendorAct = new QAction(QIcon(":/res/schematic.png"), tr("&Add Vendor..."), this);
+       newVendorAct->setStatusTip(tr("Create a new vendor"));
+       connect(newVendorAct, SIGNAL(triggered()), this, SLOT(HandleNewVendorDialog()));
+
 //     helpAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&Contents..."), this);
 //     helpAct->setStatusTip(tr("Help is available, if you should need it"));
 //     connect(helpAct, SIGNAL(triggered()), this, SLOT(ShowHelpWin()));
@@ -56,6 +72,11 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
 //     fileMenu->addAction(configAct);
        menu->addAction(quitAppAct);
 
+       menu = menuBar()->addMenu(tr("&Edit"));
+       menu->addAction(configAct);
+       menu->addAction(vendorClassAct);
+       menu->addAction(newVendorAct);
+
        menu = menuBar()->addMenu(tr("&Help"));
 //     menu->addAction(helpAct);
        menu->addAction(aboutAct);
@@ -80,49 +101,106 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
        statusBar()->showMessage(tr("Ready"));
 
        ReadSettings();
+       boldFont->setBold(true);
 
        // Finally, set up database connection
 
-       QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
-       db.setHostName("localhost");
-       db.setDatabaseName("schematic");
-       db.setUserName("scm_user");
-       db.setPassword("scm_user");
-       bool ok = db.open();
+       db = QSqlDatabase::addDatabase("QODBC");
+       bool ok = false;
 
-#warning "!!! NEED TO SET UP DB SETUP DIALOG IF CONNECTION FAILED !!!"
+       // Prime the SQL Settings dialog (in case we need it)
 
+       SQLSettingsDialog sqlSettings;
+       sqlSettings.edit1->setText(dbDriver);
+       sqlSettings.edit2->setText(dbHostName);
+       sqlSettings.edit3->setText(dbName);
+       sqlSettings.edit4->setText(dbUserName);
+       sqlSettings.edit5->setText(dbPassword);
 
-//     printf("Database connection was %sopened successfully.\n", (ok ? "" : "NOT "));
+       do
+       {
+               // Set up the ODBC DB connection with saved settings
+               // NB: For this to work properly on 64-bit Windows, you need to use the 32-bit driver!
+               QString odbc = QString("DRIVER={%1};DATABASE=%2;SERVER=%3;UID=%4;PASSWORD=%5")
+                       .arg(dbDriver).arg(dbName).arg(dbHostName).arg(dbUserName).arg(dbPassword);
+               db.setDatabaseName(odbc);
+               ok = db.open();
+
+               // If unsuccessful, run the SQL settings/test dialog
+               if (!ok)
+               {
+                       sqlSettings.error1->setText(db.lastError().databaseText());
+                       sqlSettings.error2->setText(db.lastError().driverText());
+
+                       if (sqlSettings.exec())
+                       {
+                               // User thinks this will work (hit OK button), so prime the variables
+                               // for the next attempt
+                               dbDriver = sqlSettings.edit1->text();
+                               dbHostName = sqlSettings.edit2->text();
+                               dbName = sqlSettings.edit3->text();
+                               dbUserName = sqlSettings.edit4->text();
+                               dbPassword = sqlSettings.edit5->text();
+                       }
+                       else
+                               return;                                         // User cancelled the dialog, so quit
+               }
+       }
+       while (!ok);
 
-       // Do Login dialog
-       LoginDialog login;
-       bool done = false;
+       // Check to see how many users are in the system; if less than 2, we don't do a login
+       QSqlQuery query;
+       query.prepare("SELECT COUNT(*) FROM User");
+       query.exec();
+       query.next();
 
-       do
+       if (query.value(0).toInt() > 1)
        {
-               bool accept = login.exec();
+               // Do Login dialog
+               LoginDialog loginDlg;
+               bool done = false;
 
-               // Check to see if user cancelled out
-               if (!accept)
-                       done = true;
-               else
+               do
                {
-                       // Search DB for this username/login pair
-                       QSqlQuery query("SELECT UID FROM User WHERE Login=? AND Password=?");
-                       query.addBindValue(login.edit1->text());
-                       query.addBindValue(login.edit2->text());
-                       query.exec();
+                       bool accept = loginDlg.exec();
 
-                       while (query.next())
-                       {
-                               // We have a winner!
-                               loggedInUID = query.value(0).toInt();
+                       // Check to see if user cancelled out
+                       if (!accept)
                                done = true;
+                       else
+                       {
+                               // Search DB for this username/login pair
+                               QSqlQuery query;
+                               query.prepare("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
+                               query.addBindValue(loginDlg.edit1->text());
+                               query.addBindValue(loginDlg.edit2->text());
+                               query.exec();
+
+                               while (query.next())
+                               {
+                                       // We have a winner!
+                                       loggedInUID = query.value(0).toInt();
+                                       fullName = query.value(1).toString();
+                                       login = query.value(2).toString();
+                                       done = true;
+                               }
                        }
                }
+               while (!done);
        }
-       while (!done);
+       else
+       {
+               fullName = "Administrator";
+               login = "admin";
+               loggedInUID = 1;
+       }
+
+       scmWidget = new SCMWidget(this);
+       QString s = QString("User: %1 (%2)").arg(fullName).arg(login);
+       scmWidget->username->setText(s);
+       scmWidget->currentUID = loggedInUID;
+       scmWidget->UpdateNotes();
+       setCentralWidget(scmWidget);
 }
 
 
@@ -144,6 +222,239 @@ void MainWindow::ShowAboutWin(void)
 }
 
 
+void MainWindow::HandleConfigDialog(void)
+{
+       ConfigDialog dialog(this);
+       dialog.exec();
+}
+
+
+void MainWindow::HandleVendorClassDialog(void)
+{
+       VendorClassDialog dialog(this);
+
+       if (!dialog.exec())
+               return;
+}
+
+
+void MainWindow::HandleNewVendorDialog(void)
+{
+       NewVendorDialog dialog(this);
+       FillVendorLevelCombo(dialog.combo1);
+       FillContactTypeCombo(dialog.contact->field1);
+       FillVendorClassList(dialog.list);
+
+       if (!dialog.exec())
+               return;
+
+       // Presumably, the user has given us good data, so we try to populate the
+       // database with this new vendor data.
+
+       // First, see if there is anything missing from the dialog...
+       
+
+       // Create new vendor entry...
+       int vid = -1;
+       bool hasNDA = (dialog.checkbox1->checkState() == Qt::Checked ? true : false);
+       int vlid = dialog.combo1->itemData(dialog.combo1->currentIndex()).toInt();
+       QSqlQuery query;
+       query.prepare("INSERT INTO Vendor VALUES ('', ?, ?, ?)");
+       query.addBindValue(vlid);
+       query.addBindValue(hasNDA);
+       query.addBindValue(dialog.edit1->text());
+       query.exec();
+
+       // Get VID from record just inserted
+       query.prepare("SELECT vid FROM Vendor WHERE vlid=? AND signedNDA=? AND name=?");
+       query.addBindValue(vlid);
+       query.addBindValue(hasNDA);
+       query.addBindValue(dialog.edit1->text());
+       query.exec();
+       query.next();
+       vid = query.value(0).toInt();
+//printf("NVD: New Vendor VID=%i\n", vid);
+
+       // Add primary address...
+       query.prepare("INSERT INTO Location VALUES ('', ?, ?, ?, ?, ?, ?)");
+       query.addBindValue(vid);
+       query.addBindValue(dialog.address->field1->text());
+       query.addBindValue(dialog.address->field2->text());
+       query.addBindValue(dialog.address->field3->text());
+       query.addBindValue(dialog.address->field4->text());
+       query.addBindValue(dialog.address->field5->text());
+       query.exec();
+
+       // Fix up primary contact type
+       int ctid = -1;
+
+       // -1 in currentIndex() means nothing was selected, so create new entry
+//NOT SO FAST: Seems it defaults to whatever is found there. So need to fix that. >:-(
+//     if (dialog.contact->field1->currentIndex() == -1)
+       if (dialog.contact->newContactType)
+       {
+               query.prepare("INSERT INTO ContactType VALUES ('', ?)");
+               query.addBindValue(dialog.contact->field1->currentText());
+               query.exec();
+
+               query.prepare("SELECT ctid FROM ContactType WHERE description=?");
+               query.addBindValue(dialog.contact->field1->currentText());
+               query.exec();
+               query.next();
+               ctid = query.value(0).toInt();
+       }
+       else
+       {
+               ctid = dialog.contact->field1->itemData(dialog.contact->field1->currentIndex()).toInt();
+       }
+
+       // Add primary contact...
+       query.prepare("INSERT INTO Contact VALUES ('', ?, ?, ?, ?, ?, ?, ?, ?)");
+       query.addBindValue(vid);
+       query.addBindValue(ctid);
+       query.addBindValue(dialog.contact->field2->text());
+       query.addBindValue(dialog.contact->field3->text());
+       query.addBindValue(dialog.contact->field4->text());
+       query.addBindValue(dialog.contact->field5->text());
+       query.addBindValue(dialog.contact->field6->text());
+       query.addBindValue(dialog.contact->field7->text());
+       query.exec();
+
+       // Add vendor classes...
+       for(int i=0; i<dialog.list->count(); i++)
+       {
+               QListWidgetItem * item = dialog.list->item(i);
+
+               if (item->checkState() == Qt::Checked)
+               {
+                       int vtid = item->data(Qt::UserRole).toInt();
+                       query.prepare("INSERT INTO VendorSpecificTypes VALUES (?, ?)");
+                       query.addBindValue(vid);
+                       query.addBindValue(vtid);
+                       query.exec();
+               }
+       }
+
+       // Update UI
+       scmWidget->GetVendorIDs();
+       scmWidget->GetVendor(vid);
+
+//printf("NVD: Contact Type index = %i\n", dialog.contact->field1->currentIndex());
+//printf("NVD: Contact type ='%s'\n", dialog.contact->field1->currentText().toAscii().data());
+//-1 means no contact selected; check currentText() to see if it has anything in it or not
+
+       // Tables to fill:
+#if 0
+Vendor
+VID (P-key) | VLID | signedNDA | Name
+CREATE TABLE Vendor (vid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, vlid INT, signedNDA TINYINT(1) NOT NULL DEFAULT 0, name VARCHAR(100), FOREIGN KEY (vlid) REFERENCES VendorLevel(vlid)) ENGINE=INNODB;
+
+VendorType
+VTID (P-key) | VGID | SeqNo | Description
+CREATE TABLE VendorType (vtid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, vgid INT, seqNo INT(4) NOT NULL, description VARCHAR(100), FOREIGN KEY (vgid) REFERENCES VendorGroup(vgid)) ENGINE=INNODB;
+
+VendorSpecificTypes
+VID (multi) | VTID (multi)
+CREATE TABLE VendorSpecificTypes (vid INT, vtid INT, FOREIGN KEY (vid) REFERENCES Vendor(vid), FOREIGN KEY (vtid) REFERENCES VendorType(vtid)) ENGINE=INNODB;
+
+Location
+LID (P-key) | VID | Address | City | State | Country | Code
+CREATE TABLE Location (lid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, vid INT, address VARCHAR(100), city VARCHAR(64), state VARCHAR(64), country VARCHAR(64), code VARCHAR(32), FOREIGN KEY (vid) REFERENCES Vendor(vid)) ENGINE=INNODB;
+
+Contact
+CID (P-key) | VID | CTID | Name | Email | Address | Phone1 | Phone2 | Fax
+CREATE TABLE Contact (cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, vid INT, CTID int, name VARCHAR(64), email VARCHAR(64), address VARCHAR(200), phone1 VARCHAR(32), phone2 VARCHAR(32), fax VARCHAR(32), FOREIGN KEY (vid) REFERENCES Vendor(vid), FOREIGN KEY (ctid) REFERENCES ContactType(ctid)) ENGINE=INNODB;
+
+ContactType
+CTID (P-key) | Description
+CREATE TABLE ContactType (ctid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, description VARCHAR(100)) ENGINE=INNODB;
+#endif
+}
+
+
+void MainWindow::FillVendorLevelCombo(QComboBox * combo)
+{
+       QSqlQuery query;
+       query.prepare("SELECT VLID, Description FROM VendorLevel");
+       query.exec();
+
+       while (query.next())
+       {
+               int vlid = query.value(0).toInt();
+               QString description = query.value(1).toString();
+
+               combo->addItem(description, vlid);
+       }
+}
+
+
+void MainWindow::FillContactTypeCombo(QComboBox * combo)
+{
+       QSqlQuery query;
+       query.prepare("SELECT CTID, Description FROM ContactType");
+       query.exec();
+
+       while (query.next())
+       {
+               int ctid = query.value(0).toInt();
+               QString description = query.value(1).toString();
+
+               combo->addItem(description, ctid);
+       }
+}
+
+
+void MainWindow::FillVendorClassList(QListWidget * list)
+{
+       std::vector<VendorType> groupList;
+
+       // Pull in definitions from DB for Vendor Classes/Groups
+       QSqlQuery query1;
+       query1.prepare("SELECT vgid, description FROM VendorGroup ORDER BY seqNo");
+       query1.exec();
+
+       while (query1.next())
+       {
+               VendorType v;
+               v.key         = query1.value(0).toInt();
+               v.description = query1.value(1).toString();
+               groupList.push_back(v);
+       }
+
+//     QSqlQuery query2;
+       query1.prepare("SELECT vtid, vgid, description FROM VendorType ORDER BY seqNo");
+       query1.exec();
+
+       int previousID = -1, groupListIndex = 0;
+       QListWidgetItem * item;
+
+       while (query1.next())
+       {
+//             VendorType v;
+               int vtid            = query1.value(0).toInt();
+               int vgid            = query1.value(1).toInt();
+               QString description = query1.value(2).toString();
+
+               // Check to see if we need to insert new header yet.
+               // If we're not still in same group, push the next group header into the list
+               // and continue
+               if (previousID != vgid)
+               {
+                       item  = new QListWidgetItem(groupList[groupListIndex].description);
+                       item->setData(Qt::UserRole, groupList[groupListIndex++].key);
+                       item->setFont(*boldFont);
+                       list->addItem(item);
+                       previousID = vgid;
+               }
+
+               item  = new QListWidgetItem(description);
+               item->setData(Qt::UserRole, vtid);
+               item->setCheckState(Qt::Unchecked);
+               list->addItem(item);
+       }
+}
+
+
 void MainWindow::ReadSettings(void)
 {
        QSettings settings("Underground Software", "SCheMatic");
@@ -155,6 +466,12 @@ void MainWindow::ReadSettings(void)
 //     size = settings.value("charWndSize", QSize(200, 200)).toSize();
 //     ((TTEdit *)qApp)->charWnd->resize(size);
 //     ((TTEdit *)qApp)->charWnd->move(pos);
+
+       dbDriver = settings.value("dbDriver", "myodbc-5.1").toString();
+       dbHostName = settings.value("dbHostName", "localhost").toString();
+       dbName = settings.value("dbName", "schematic").toString();
+       dbUserName = settings.value("dbUserName", "scm_user").toString();
+       dbPassword = settings.value("dbPassword", "scm_user").toString();
 }
 
 
@@ -165,4 +482,11 @@ void MainWindow::WriteSettings(void)
        settings.setValue("size", size());
 //     settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
 //     settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
+
+       settings.setValue("dbDriver", dbDriver);
+       settings.setValue("dbHostName", dbHostName);
+       settings.setValue("dbName", dbName);
+       settings.setValue("dbUserName", dbUserName);
+       settings.setValue("dbPassword", dbPassword);
 }
+