]> Shamusworld >> Repos - schematic/blob - src/vendorclassdialog.cpp
Added a bunch of new functionality
[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 //      dbRef(db)
28 {
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(""));
41
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);
49
50         QVBoxLayout * mainLayout = new QVBoxLayout;
51         mainLayout->addWidget(list);
52         mainLayout->addLayout(buttonLayout);
53         mainLayout->addWidget(buttonBox);
54         setLayout(mainLayout);
55
56         setWindowTitle(tr("Vendor Class Editor"));
57
58         connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
59         connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
60
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)));
67
68         boldFont->setBold(true);
69         LoadList();
70 }
71
72
73 VendorClassDialog::~VendorClassDialog()
74 {
75 }
76
77
78 void VendorClassDialog::AddItem(void)
79 {
80         QListWidgetItem * item  = new QListWidgetItem(tr("New Vendor Class"));
81         item->setFlags(item->flags() | Qt::ItemIsEditable);
82
83         // We'll have to use the setData(Qt::UserRole, bool) to set the header/no header data...
84
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);
90
91         int row = list->row(list->currentItem());
92         list->insertItem(row + 1, item);
93 }
94
95
96 void VendorClassDialog::DeleteItem(void)
97 {
98         int row = list->row(list->currentItem());
99         QListWidgetItem * item = list->takeItem(row);
100
101         // This could be called on an empty list, so we have a sanity check here...
102         if (item)
103                 delete item;
104 }
105
106
107 void VendorClassDialog::MoveItemUp(void)
108 {
109         int row = list->row(list->currentItem());
110
111         if (row == 0)
112                 return;
113
114         QListWidgetItem * item = list->takeItem(row);
115         list->insertItem(row - 1, item);
116         list->setCurrentItem(item);
117 }
118
119
120 void VendorClassDialog::MoveItemDown(void)
121 {
122         int row = list->row(list->currentItem());
123
124         if (row == list->count() - 1)
125                 return;
126
127         QListWidgetItem * item = list->takeItem(row);
128         list->insertItem(row + 1, item);
129         list->setCurrentItem(item);
130 }
131
132
133 void VendorClassDialog::SetHeaderStateOnItem(void)
134 {
135         QListWidgetItem * item = list->currentItem();
136         item->setData(Qt::UserRole, makeHeader->isChecked());
137
138         if (makeHeader->isChecked())
139                 item->setFont(*boldFont);
140         else
141                 item->setFont(*normalFont);
142 }
143
144
145 void VendorClassDialog::CurrentRowChanged(int /*row*/)
146 {
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);
151 }
152
153
154 // This probably should be in the mainwin class...
155 void VendorClassDialog::LoadList(void)
156 {
157         std::vector<VendorType> groupList;
158
159         // Pull in definitions from DB for Vendor Classes/Groups
160         QSqlQuery query1("SELECT vgid, seqNo, description FROM VendorGroup ORDER BY seqNo");
161         query1.exec();
162
163         while (query1.next())
164         {
165                 VendorType v;
166                 v.key         = query1.value(0).toInt();
167                 v.seqNo       = query1.value(1).toInt();
168                 v.description = query1.value(2).toString();
169                 v.isHeader    = true;
170
171                 groupList.push_back(v);
172         }
173
174         QSqlQuery query2("SELECT vtid, vgid, seqNo, description FROM VendorType ORDER BY seqNo");
175         query2.exec();
176
177         int previousID = -1, groupListIndex = 0;
178
179         while (query2.next())
180         {
181                 VendorType v;
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();
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 }
218