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),
28 addItem->setToolButtonStyle(Qt::ToolButtonTextOnly);
29 addItem->setText("+");
30 addItem->setToolTip(tr("Add new vendor class or group."));
31 deleteItem->setToolButtonStyle(Qt::ToolButtonTextOnly);
32 deleteItem->setText("-");
33 deleteItem->setToolTip(tr("Delete highlighted item."));
34 moveItemUp->setArrowType(Qt::UpArrow);
35 moveItemUp->setToolTip(tr("Move highlighted item up in the list."));
36 moveItemDown->setArrowType(Qt::DownArrow);
37 moveItemDown->setToolTip(tr("Move highlighted item down in the list."));
38 makeHeader->setToolTip(tr("Make highlighted item into a vendor group."));
39 // list->setToolTip(tr(""));
41 QHBoxLayout * buttonLayout = new QHBoxLayout;
42 buttonLayout->addWidget(addItem);
43 buttonLayout->addWidget(deleteItem);
44 buttonLayout->addWidget(moveItemUp);
45 buttonLayout->addWidget(moveItemDown);
46 buttonLayout->addStretch();
47 buttonLayout->addWidget(makeHeader);
49 QVBoxLayout * mainLayout = new QVBoxLayout;
50 mainLayout->addWidget(list);
51 mainLayout->addLayout(buttonLayout);
52 mainLayout->addWidget(buttonBox);
53 setLayout(mainLayout);
55 setWindowTitle(tr("Vendor Class Editor"));
57 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
58 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
60 connect(addItem, SIGNAL(clicked()), this, SLOT(AddItem()));
61 connect(deleteItem, SIGNAL(clicked()), this, SLOT(DeleteItem()));
62 connect(moveItemUp, SIGNAL(clicked()), this, SLOT(MoveItemUp()));
63 connect(moveItemDown, SIGNAL(clicked()), this, SLOT(MoveItemDown()));
64 connect(makeHeader, SIGNAL(clicked()), this, SLOT(SetHeaderStateOnItem()));
65 connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(CurrentRowChanged(int)));
67 boldFont->setBold(true);
72 VendorClassDialog::~VendorClassDialog()
77 void VendorClassDialog::AddItem(void)
79 QListWidgetItem * item = new QListWidgetItem(tr("New Vendor Class"));
80 item->setFlags(item->flags() | Qt::ItemIsEditable);
82 // We'll have to use the setData(Qt::UserRole, bool) to set the header/no header data...
84 // item->setCheckState(Qt::Unchecked);
85 // Qt::ItemFlags f = item->flags();
86 // //nope, this doesn't work. the above fucks something up that's not unfuckable.
87 // f &= ~Qt::ItemIsUserCheckable;
88 // item->setFlags(f | Qt::ItemIsEditable);
90 int row = list->row(list->currentItem());
91 list->insertItem(row + 1, item);
95 void VendorClassDialog::DeleteItem(void)
97 int row = list->row(list->currentItem());
98 QListWidgetItem * item = list->takeItem(row);
100 // This could be called on an empty list, so we have a sanity check here...
106 void VendorClassDialog::MoveItemUp(void)
108 int row = list->row(list->currentItem());
113 QListWidgetItem * item = list->takeItem(row);
114 list->insertItem(row - 1, item);
115 list->setCurrentItem(item);
119 void VendorClassDialog::MoveItemDown(void)
121 int row = list->row(list->currentItem());
123 if (row == list->count() - 1)
126 QListWidgetItem * item = list->takeItem(row);
127 list->insertItem(row + 1, item);
128 list->setCurrentItem(item);
132 void VendorClassDialog::SetHeaderStateOnItem(void)
134 QListWidgetItem * item = list->currentItem();
135 item->setData(Qt::UserRole, makeHeader->isChecked());
137 if (makeHeader->isChecked())
138 item->setFont(*boldFont);
140 item->setFont(*normalFont);
144 void VendorClassDialog::CurrentRowChanged(int /*row*/)
146 // Show our indicator that this item is a header/group or not
147 QListWidgetItem * item = list->currentItem();
148 bool isHeader = item->data(Qt::UserRole).toBool();
149 makeHeader->setChecked(isHeader);
153 // This probably should be in the mainwin class...
154 void VendorClassDialog::LoadList(void)
156 std::vector<VendorType> groupList;
158 // Pull in definitions from DB for Vendor Classes/Groups
160 query.prepare("SELECT vgid, seqNo, description FROM VendorGroup ORDER BY seqNo");
166 v.key = query.value(0).toInt();
167 v.seqNo = query.value(1).toInt();
168 v.description = query.value(2).toString();
171 groupList.push_back(v);
174 query.prepare("SELECT vtid, vgid, seqNo, description FROM VendorType ORDER BY seqNo");
177 int previousID = -1, groupListIndex = 0;
182 v.key = query.value(0).toInt();
183 int vgid = query.value(1).toInt();
184 v.seqNo = query.value(2).toInt();
185 v.description = query.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)
218 We need to do a few things here. One approach is to drop the VendorGroup and VendorType
219 tables and recreate them, but that approach might cause VendorSpecificTypes to contain
222 Another approach is to keep track of keys (vgid for VendorGroup items, vtid & vgid for
223 VendorType items) and do an update for items with keys, and a regular add for items
224 that don't have keys (items that the user added to the dialog). That approach will keep
225 VendorSpecificTypes from being corrupted, but requires more overhead in the tracking
228 One way to track those items would be to put the array # into the user data area; that
229 way when we get here we can query that data to determine if there's keys associated with
230 an entry or not and how to handle it. Unfortunately, we're already using the user data
231 role to determine whether or not an item is a header. But we could fix that by giving
232 it a number and using that number as an index into oldList--but that wouldn't work for
235 So maybe the correct approach is to compare what's in the ListWidget with what's in
236 oldList, that way we can ignore things that didn't change. But what about those that did?
237 In that case, you're screwed.
239 It would still be useful to do that in order to detect the case where the user deleted
240 something and then changed his mind and put it back.