]> Shamusworld >> Repos - schematic/blobdiff - src/vendorclassdialog.cpp
Added a bunch of new functionality
[schematic] / src / vendorclassdialog.cpp
diff --git a/src/vendorclassdialog.cpp b/src/vendorclassdialog.cpp
new file mode 100644 (file)
index 0000000..aef142b
--- /dev/null
@@ -0,0 +1,218 @@
+//
+// vendorclassdialog.cpp: Vendor Class editing dialog
+//
+// Part of the SCheMatic Project
+// (C) 2012 Underground Software
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// WHO  WHEN        WHAT
+// ---  ----------  ------------------------------------------------------------
+// JLH  09/20/2011  Created this file
+
+#include "vendorclassdialog.h"
+#include <QtSql>
+
+
+VendorClassDialog::VendorClassDialog(QWidget * parent/*= 0*/): QDialog(parent),
+       buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel)),
+       addItem(new QToolButton),
+       deleteItem(new QToolButton),
+       moveItemUp(new QToolButton),
+       moveItemDown(new QToolButton),
+       makeHeader(new QCheckBox(tr("&Heading"))),
+       normalFont(new QFont),
+       boldFont(new QFont),
+       list(new QListWidget)
+//     dbRef(db)
+{
+       addItem->setToolButtonStyle(Qt::ToolButtonTextOnly);
+       addItem->setText("+");
+       addItem->setToolTip(tr("Add new vendor class or group."));
+       deleteItem->setToolButtonStyle(Qt::ToolButtonTextOnly);
+       deleteItem->setText("-");
+       deleteItem->setToolTip(tr("Delete highlighted item."));
+       moveItemUp->setArrowType(Qt::UpArrow);
+       moveItemUp->setToolTip(tr("Move highlighted item up in the list."));
+       moveItemDown->setArrowType(Qt::DownArrow);
+       moveItemDown->setToolTip(tr("Move highlighted item down in the list."));
+       makeHeader->setToolTip(tr("Make highlighted item into a vendor group."));
+//     list->setToolTip(tr(""));
+
+       QHBoxLayout * buttonLayout = new QHBoxLayout;
+       buttonLayout->addWidget(addItem);
+       buttonLayout->addWidget(deleteItem);
+       buttonLayout->addWidget(moveItemUp);
+       buttonLayout->addWidget(moveItemDown);
+       buttonLayout->addStretch();
+       buttonLayout->addWidget(makeHeader);
+
+       QVBoxLayout * mainLayout = new QVBoxLayout;
+       mainLayout->addWidget(list);
+       mainLayout->addLayout(buttonLayout);
+       mainLayout->addWidget(buttonBox);
+       setLayout(mainLayout);
+
+       setWindowTitle(tr("Vendor Class Editor"));
+
+       connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
+       connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
+
+       connect(addItem, SIGNAL(clicked()), this, SLOT(AddItem()));
+       connect(deleteItem, SIGNAL(clicked()), this, SLOT(DeleteItem()));
+       connect(moveItemUp, SIGNAL(clicked()), this, SLOT(MoveItemUp()));
+       connect(moveItemDown, SIGNAL(clicked()), this, SLOT(MoveItemDown()));
+       connect(makeHeader, SIGNAL(clicked()), this, SLOT(SetHeaderStateOnItem()));
+       connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(CurrentRowChanged(int)));
+
+       boldFont->setBold(true);
+       LoadList();
+}
+
+
+VendorClassDialog::~VendorClassDialog()
+{
+}
+
+
+void VendorClassDialog::AddItem(void)
+{
+       QListWidgetItem * item  = new QListWidgetItem(tr("New Vendor Class"));
+       item->setFlags(item->flags() | Qt::ItemIsEditable);
+
+       // We'll have to use the setData(Qt::UserRole, bool) to set the header/no header data...
+
+//     item->setCheckState(Qt::Unchecked);
+//     Qt::ItemFlags f = item->flags();
+//     //nope, this doesn't work. the above fucks something up that's not unfuckable.
+//     f &= ~Qt::ItemIsUserCheckable;
+//     item->setFlags(f | Qt::ItemIsEditable);
+
+       int row = list->row(list->currentItem());
+       list->insertItem(row + 1, item);
+}
+
+
+void VendorClassDialog::DeleteItem(void)
+{
+       int row = list->row(list->currentItem());
+       QListWidgetItem * item = list->takeItem(row);
+
+       // This could be called on an empty list, so we have a sanity check here...
+       if (item)
+               delete item;
+}
+
+
+void VendorClassDialog::MoveItemUp(void)
+{
+       int row = list->row(list->currentItem());
+
+       if (row == 0)
+               return;
+
+       QListWidgetItem * item = list->takeItem(row);
+       list->insertItem(row - 1, item);
+       list->setCurrentItem(item);
+}
+
+
+void VendorClassDialog::MoveItemDown(void)
+{
+       int row = list->row(list->currentItem());
+
+       if (row == list->count() - 1)
+               return;
+
+       QListWidgetItem * item = list->takeItem(row);
+       list->insertItem(row + 1, item);
+       list->setCurrentItem(item);
+}
+
+
+void VendorClassDialog::SetHeaderStateOnItem(void)
+{
+       QListWidgetItem * item = list->currentItem();
+       item->setData(Qt::UserRole, makeHeader->isChecked());
+
+       if (makeHeader->isChecked())
+               item->setFont(*boldFont);
+       else
+               item->setFont(*normalFont);
+}
+
+
+void VendorClassDialog::CurrentRowChanged(int /*row*/)
+{
+       // Show our indicator that this item is a header/group or not
+       QListWidgetItem * item = list->currentItem();
+       bool isHeader = item->data(Qt::UserRole).toBool();
+       makeHeader->setChecked(isHeader);
+}
+
+
+// This probably should be in the mainwin class...
+void VendorClassDialog::LoadList(void)
+{
+       std::vector<VendorType> groupList;
+
+       // Pull in definitions from DB for Vendor Classes/Groups
+       QSqlQuery query1("SELECT vgid, seqNo, description FROM VendorGroup ORDER BY seqNo");
+       query1.exec();
+
+       while (query1.next())
+       {
+               VendorType v;
+               v.key         = query1.value(0).toInt();
+               v.seqNo       = query1.value(1).toInt();
+               v.description = query1.value(2).toString();
+               v.isHeader    = true;
+
+               groupList.push_back(v);
+       }
+
+       QSqlQuery query2("SELECT vtid, vgid, seqNo, description FROM VendorType ORDER BY seqNo");
+       query2.exec();
+
+       int previousID = -1, groupListIndex = 0;
+
+       while (query2.next())
+       {
+               VendorType v;
+               v.key         = query2.value(0).toInt();
+               int vgid      = query2.value(1).toInt();
+               v.seqNo       = query2.value(2).toInt();
+               v.description = query2.value(3).toString();
+               v.isHeader    = false;
+
+               // 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)
+               {
+                       oldList.push_back(groupList[groupListIndex++]);
+                       previousID = vgid;
+               }
+
+               oldList.push_back(v);
+       }
+
+       // Finally, populate the QListWidget
+       for(int i=0; i<oldList.size(); i++)
+       {
+               QListWidgetItem * item  = new QListWidgetItem(oldList[i].description);
+               item->setFlags(item->flags() | Qt::ItemIsEditable);
+               item->setData(Qt::UserRole, oldList[i].isHeader);
+
+               if (oldList[i].isHeader)
+                       item->setFont(*boldFont);
+
+               list->insertItem(i, item);
+       }
+}
+
+
+void VendorClassDialog::SaveList(void)
+{
+}
+