]> Shamusworld >> Repos - schematic/blobdiff - src/mainwin.cpp
Switched to ODBC driver for connections to MySQL database
[schematic] / src / mainwin.cpp
index 44161ba81c6f6deec3bfffd6e7a82c9c21f2f870..c360a4eaa7a89ad6186716904f5022180a71a781 100644 (file)
 #include "configdialog.h"
 #include "logindialog.h"
 #include "newvendordialog.h"
+#include "scmwidget.h"
 #include "sqlsettingsdialog.h"
 #include "vendorclassdialog.h"
 
 
 MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
+       scmWidget(new SCMWidget(this)),
+       boldFont(new QFont),
        loggedInUID(0)
 {
        setWindowIcon(QIcon(":/res/schematic.png"));
@@ -99,29 +102,42 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
        statusBar()->showMessage(tr("Ready"));
 
        ReadSettings();
+       boldFont->setBold(true);
 
        // Finally, set up database connection
 
-       db = QSqlDatabase::addDatabase("QMYSQL");
+//     db = QSqlDatabase::addDatabase("QMYSQL");
+       db = QSqlDatabase::addDatabase("QODBC");
        bool ok = false;
 
        // Prime the SQL Settings dialog (in case we need it)
 
        SQLSettingsDialog sqlSettings;
-       sqlSettings.edit1->setText(dbHostName);
-       sqlSettings.edit2->setText(dbName);
-       sqlSettings.edit3->setText(dbUserName);
-       sqlSettings.edit4->setText(dbPassword);
-
+       sqlSettings.edit1->setText(dbDriver);
+       sqlSettings.edit2->setText(dbHostName);
+       sqlSettings.edit3->setText(dbName);
+       sqlSettings.edit4->setText(dbUserName);
+       sqlSettings.edit5->setText(dbPassword);
+
+#if 0
+db = QSqlDatabase::addDatabase("QODBC");
+db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};DATABASE=agp-dbserver01;");
+db.setUserName("xcdr");
+db.setPassword("xcdr");
+#endif
        do
        {
                // Set up the DB connection with saved settings
+               QString odbc = QString("DRIVER={%1};DATABASE=%2;").arg(dbDriver).arg(dbName);
                db.setHostName(dbHostName);
-               db.setDatabaseName(dbName);
+               db.setDatabaseName(odbc);
                db.setUserName(dbUserName);
                db.setPassword(dbPassword);
                ok = db.open();
 
+//printf("Error: %s\n", db.lastError().databaseText().toAscii().data());
+//printf("Error: %s\n", db.lastError().driverText().toAscii().data());
+
                // If unsuccessful, run the SQL settings/test dialog
                if (!ok)
                {
@@ -129,10 +145,11 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
                        {
                                // User thinks this will work (hit OK button), so prime the variables
                                // for the next attempt
-                               dbHostName = sqlSettings.edit1->text();
-                               dbName = sqlSettings.edit2->text();
-                               dbUserName = sqlSettings.edit3->text();
-                               dbPassword = sqlSettings.edit4->text();
+                               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
@@ -140,34 +157,64 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
        }
        while (!ok);
 
+       // 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();
+
+       if (query.value(0).toInt() > 1)
+       {
        // Do Login dialog
-       LoginDialog login;
+       LoginDialog loginDlg;
        bool done = false;
 
        do
        {
-               bool accept = login.exec();
+               bool accept = loginDlg.exec();
 
                // Check to see if user cancelled out
                if (!accept)
                        done = true;
                else
                {
+#if 0
+                       // Search DB for this username/login pair
+                       QSqlQuery query("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
+                       query.addBindValue(loginDlg.edit1->text());
+                       query.addBindValue(loginDlg.edit2->text());
+                       query.exec();
+#else
                        // 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());
+                       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();
+#endif
 
                        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);
+       }
+       else
+       {
+               fullName = "Administrator";
+               login = "admin";
+               loggedInUID = 1;
+       }
+
+       QString s = QString("User: %1 (%2)").arg(fullName).arg(login);
+       scmWidget->username->setText(s);
+       setCentralWidget(scmWidget);
 }
 
 
@@ -208,9 +255,102 @@ void MainWindow::HandleVendorClassDialog(void)
 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.
+       QSqlQuery query;
+       query.prepare("INSERT INTO  VALUES (?, ?, ?)");
+
+       
+}
+
+
+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);
+       }
 }
 
 
@@ -226,6 +366,7 @@ void MainWindow::ReadSettings(void)
 //     ((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();
@@ -241,6 +382,7 @@ void MainWindow::WriteSettings(void)
 //     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);