2 // vendorclassdialog.cpp: Vendor Class editing dialog
4 // Part of the SCheMatic Project
5 // (C) 2012 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 09/20/2011 Created this file
13 #include "vendorclassdialog.h"
17 VendorClassDialog::VendorClassDialog(QWidget * parent/*= 0*/): QDialog(parent),
18 buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel)),
19 addItem(new QToolButton),
20 deleteItem(new QToolButton),
21 moveItemUp(new QToolButton),
22 moveItemDown(new QToolButton),
23 makeHeader(new QCheckBox(tr("&Heading"))),
24 normalFont(new QFont),
29 addItem->setToolButtonStyle(Qt::ToolButtonTextOnly);
30 addItem->setText("+");
31 addItem->setToolTip(tr("Add new vendor class or group."));
32 deleteItem->setToolButtonStyle(Qt::ToolButtonTextOnly);
33 deleteItem->setText("-");
34 deleteItem->setToolTip(tr("Delete highlighted item."));
35 moveItemUp->setArrowType(Qt::UpArrow);
36 moveItemUp->setToolTip(tr("Move highlighted item up in the list."));
37 moveItemDown->setArrowType(Qt::DownArrow);
38 moveItemDown->setToolTip(tr("Move highlighted item down in the list."));
39 makeHeader->setToolTip(tr("Make highlighted item into a vendor group."));
40 // list->setToolTip(tr(""));
42 QHBoxLayout * buttonLayout = new QHBoxLayout;
43 buttonLayout->addWidget(addItem);
44 buttonLayout->addWidget(deleteItem);
45 buttonLayout->addWidget(moveItemUp);
46 buttonLayout->addWidget(moveItemDown);
47 buttonLayout->addStretch();
48 buttonLayout->addWidget(makeHeader);
50 QVBoxLayout * mainLayout = new QVBoxLayout;
51 mainLayout->addWidget(list);
52 mainLayout->addLayout(buttonLayout);
53 mainLayout->addWidget(buttonBox);
54 setLayout(mainLayout);
56 setWindowTitle(tr("Vendor Class Editor"));
58 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
59 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
61 connect(addItem, SIGNAL(clicked()), this, SLOT(AddItem()));
62 connect(deleteItem, SIGNAL(clicked()), this, SLOT(DeleteItem()));
63 connect(moveItemUp, SIGNAL(clicked()), this, SLOT(MoveItemUp()));
64 connect(moveItemDown, SIGNAL(clicked()), this, SLOT(MoveItemDown()));
65 connect(makeHeader, SIGNAL(clicked()), this, SLOT(SetHeaderStateOnItem()));
66 connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(CurrentRowChanged(int)));
68 boldFont->setBold(true);
73 VendorClassDialog::~VendorClassDialog()
78 void VendorClassDialog::AddItem(void)
80 QListWidgetItem * item = new QListWidgetItem(tr("New Vendor Class"));
81 item->setFlags(item->flags() | Qt::ItemIsEditable);
83 // We'll have to use the setData(Qt::UserRole, bool) to set the header/no header data...
85 // item->setCheckState(Qt::Unchecked);
86 // Qt::ItemFlags f = item->flags();
87 // //nope, this doesn't work. the above fucks something up that's not unfuckable.
88 // f &= ~Qt::ItemIsUserCheckable;
89 // item->setFlags(f | Qt::ItemIsEditable);
91 int row = list->row(list->currentItem());
92 list->insertItem(row + 1, item);
96 void VendorClassDialog::DeleteItem(void)
98 int row = list->row(list->currentItem());
99 QListWidgetItem * item = list->takeItem(row);
101 // This could be called on an empty list, so we have a sanity check here...
107 void VendorClassDialog::MoveItemUp(void)
109 int row = list->row(list->currentItem());
114 QListWidgetItem * item = list->takeItem(row);
115 list->insertItem(row - 1, item);
116 list->setCurrentItem(item);
120 void VendorClassDialog::MoveItemDown(void)
122 int row = list->row(list->currentItem());
124 if (row == list->count() - 1)
127 QListWidgetItem * item = list->takeItem(row);
128 list->insertItem(row + 1, item);
129 list->setCurrentItem(item);
133 void VendorClassDialog::SetHeaderStateOnItem(void)
135 QListWidgetItem * item = list->currentItem();
136 item->setData(Qt::UserRole, makeHeader->isChecked());
138 if (makeHeader->isChecked())
139 item->setFont(*boldFont);
141 item->setFont(*normalFont);
145 void VendorClassDialog::CurrentRowChanged(int /*row*/)
147 // Show our indicator that this item is a header/group or not
148 QListWidgetItem * item = list->currentItem();
149 bool isHeader = item->data(Qt::UserRole).toBool();
150 makeHeader->setChecked(isHeader);
154 // This probably should be in the mainwin class...
155 void VendorClassDialog::LoadList(void)
157 std::vector<VendorType> groupList;
159 // Pull in definitions from DB for Vendor Classes/Groups
160 QSqlQuery query1("SELECT vgid, seqNo, description FROM VendorGroup ORDER BY seqNo");
163 while (query1.next())
166 v.key = query1.value(0).toInt();
167 v.seqNo = query1.value(1).toInt();
168 v.description = query1.value(2).toString();
171 groupList.push_back(v);
174 QSqlQuery query2("SELECT vtid, vgid, seqNo, description FROM VendorType ORDER BY seqNo");
177 int previousID = -1, groupListIndex = 0;
179 while (query2.next())
182 v.key = query2.value(0).toInt();
183 int vgid = query2.value(1).toInt();
184 v.seqNo = query2.value(2).toInt();
185 v.description = query2.value(3).toString();
188 // Check to see if we need to insert new header yet.
189 // If we're not still in same group, push the next group header into the list
191 if (previousID != vgid)
193 oldList.push_back(groupList[groupListIndex++]);
197 oldList.push_back(v);
200 // Finally, populate the QListWidget
201 for(int i=0; i<oldList.size(); i++)
203 QListWidgetItem * item = new QListWidgetItem(oldList[i].description);
204 item->setFlags(item->flags() | Qt::ItemIsEditable);
205 item->setData(Qt::UserRole, oldList[i].isHeader);
207 if (oldList[i].isHeader)
208 item->setFont(*boldFont);
210 list->insertItem(i, item);
215 void VendorClassDialog::SaveList(void)