]> Shamusworld >> Repos - schematic/blob - src/vendorclassdialog.cpp
Move DB access to NoteDialog class, new AlertDialog class.
[schematic] / src / vendorclassdialog.cpp
1 //
2 // vendorclassdialog.cpp: Vendor Class editing dialog
3 //
4 // Part of the SCheMatic Project
5 // (C) 2012 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  09/20/2011  Created this file
12
13 #include "vendorclassdialog.h"
14 #include <QtSql>
15
16
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),
25         boldFont(new QFont),
26         list(new QListWidget)
27 {
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(""));
40
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);
48
49         QVBoxLayout * mainLayout = new QVBoxLayout;
50         mainLayout->addWidget(list);
51         mainLayout->addLayout(buttonLayout);
52         mainLayout->addWidget(buttonBox);
53         setLayout(mainLayout);
54
55         setWindowTitle(tr("Vendor Class Editor"));
56
57         connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
58         connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
59
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)));
66
67         boldFont->setBold(true);
68         LoadList();
69 }
70
71
72 VendorClassDialog::~VendorClassDialog()
73 {
74 }
75
76
77 void VendorClassDialog::AddItem(void)
78 {
79         QListWidgetItem * item  = new QListWidgetItem(tr("New Vendor Class"));
80         item->setFlags(item->flags() | Qt::ItemIsEditable);
81
82         // We'll have to use the setData(Qt::UserRole, bool) to set the header/no header data...
83
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);
89
90         int row = list->row(list->currentItem());
91         list->insertItem(row + 1, item);
92 }
93
94
95 void VendorClassDialog::DeleteItem(void)
96 {
97         int row = list->row(list->currentItem());
98         QListWidgetItem * item = list->takeItem(row);
99
100         // This could be called on an empty list, so we have a sanity check here...
101         if (item)
102                 delete item;
103 }
104
105
106 void VendorClassDialog::MoveItemUp(void)
107 {
108         int row = list->row(list->currentItem());
109
110         if (row == 0)
111                 return;
112
113         QListWidgetItem * item = list->takeItem(row);
114         list->insertItem(row - 1, item);
115         list->setCurrentItem(item);
116 }
117
118
119 void VendorClassDialog::MoveItemDown(void)
120 {
121         int row = list->row(list->currentItem());
122
123         if (row == list->count() - 1)
124                 return;
125
126         QListWidgetItem * item = list->takeItem(row);
127         list->insertItem(row + 1, item);
128         list->setCurrentItem(item);
129 }
130
131
132 void VendorClassDialog::SetHeaderStateOnItem(void)
133 {
134         QListWidgetItem * item = list->currentItem();
135         item->setData(Qt::UserRole, makeHeader->isChecked());
136
137         if (makeHeader->isChecked())
138                 item->setFont(*boldFont);
139         else
140                 item->setFont(*normalFont);
141 }
142
143
144 void VendorClassDialog::CurrentRowChanged(int /*row*/)
145 {
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);
150 }
151
152
153 // This probably should be in the mainwin class...
154 void VendorClassDialog::LoadList(void)
155 {
156         std::vector<VendorType> groupList;
157
158         // Pull in definitions from DB for Vendor Classes/Groups
159         QSqlQuery query;
160         query.prepare("SELECT vgid, seqNo, description FROM VendorGroup ORDER BY seqNo");
161         query.exec();
162
163         while (query.next())
164         {
165                 VendorType v;
166                 v.key         = query.value(0).toInt();
167                 v.seqNo       = query.value(1).toInt();
168                 v.description = query.value(2).toString();
169                 v.isHeader    = true;
170
171                 groupList.push_back(v);
172         }
173
174         query.prepare("SELECT vtid, vgid, seqNo, description FROM VendorType ORDER BY seqNo");
175         query.exec();
176
177         int previousID = -1, groupListIndex = 0;
178
179         while (query.next())
180         {
181                 VendorType v;
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();
186                 v.isHeader    = false;
187
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
190                 // and continue
191                 if (previousID != vgid)
192                 {
193                         oldList.push_back(groupList[groupListIndex++]);
194                         previousID = vgid;
195                 }
196
197                 oldList.push_back(v);
198         }
199
200         // Finally, populate the QListWidget
201         for(int i=0; i<oldList.size(); i++)
202         {
203                 QListWidgetItem * item  = new QListWidgetItem(oldList[i].description);
204                 item->setFlags(item->flags() | Qt::ItemIsEditable);
205                 item->setData(Qt::UserRole, oldList[i].isHeader);
206
207                 if (oldList[i].isHeader)
208                         item->setFont(*boldFont);
209
210                 list->insertItem(i, item);
211         }
212 }
213
214
215 void VendorClassDialog::SaveList(void)
216 {
217 #if 0
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
220 invalid data.
221
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
226 department.
227
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
233 new items! :-P
234
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.
238
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.
241
242
243 #endif
244
245 }
246