]> Shamusworld >> Repos - schematic/blob - src/vendorclassdialog.cpp
d39a5a0f19b3fb1122485079937987aeac6cc7b5
[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 query1("SELECT vgid, seqNo, description FROM VendorGroup ORDER BY seqNo");
160         query1.exec();
161
162         while (query1.next())
163         {
164                 VendorType v;
165                 v.key         = query1.value(0).toInt();
166                 v.seqNo       = query1.value(1).toInt();
167                 v.description = query1.value(2).toString();
168                 v.isHeader    = true;
169
170                 groupList.push_back(v);
171         }
172
173         QSqlQuery query2("SELECT vtid, vgid, seqNo, description FROM VendorType ORDER BY seqNo");
174         query2.exec();
175
176         int previousID = -1, groupListIndex = 0;
177
178         while (query2.next())
179         {
180                 VendorType v;
181                 v.key         = query2.value(0).toInt();
182                 int vgid      = query2.value(1).toInt();
183                 v.seqNo       = query2.value(2).toInt();
184                 v.description = query2.value(3).toString();
185                 v.isHeader    = false;
186
187                 // Check to see if we need to insert new header yet.
188                 // If we're not still in same group, push the next group header into the list
189                 // and continue
190                 if (previousID != vgid)
191                 {
192                         oldList.push_back(groupList[groupListIndex++]);
193                         previousID = vgid;
194                 }
195
196                 oldList.push_back(v);
197         }
198
199         // Finally, populate the QListWidget
200         for(int i=0; i<oldList.size(); i++)
201         {
202                 QListWidgetItem * item  = new QListWidgetItem(oldList[i].description);
203                 item->setFlags(item->flags() | Qt::ItemIsEditable);
204                 item->setData(Qt::UserRole, oldList[i].isHeader);
205
206                 if (oldList[i].isHeader)
207                         item->setFont(*boldFont);
208
209                 list->insertItem(i, item);
210         }
211 }
212
213
214 void VendorClassDialog::SaveList(void)
215 {
216 }
217