]> Shamusworld >> Repos - schematic/blob - src/scmwidget.cpp
Move DB access to NoteDialog class, new AlertDialog class.
[schematic] / src / scmwidget.cpp
1 //
2 // scmwidget.cpp - Main widget container
3 //
4 // by James Hammons
5 // (C) 2012 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  09/24/2012  Created this file
12 //
13
14 #include "scmwidget.h"
15 #include <QtSql>
16 #include "addresswidget.h"
17 #include "alertdialog.h"
18 #include "contactwidget.h"
19 #include "notedialog.h"
20
21
22 SCMWidget::SCMWidget(QWidget * parent/*= 0*/): QWidget(parent),
23         purchaseOrders(new QTreeView),
24         vendorName(new QLabel),
25         ndaSigned(new QLabel),
26         vendorAddress(new QTabWidget),
27         vendorContact(new QTabWidget),
28         vendorClass(new QListWidget),
29         username(new QLabel),
30         vendorLevel(new VendorLevelWidget),
31         alerts(new QListWidget),
32         notes(new QListWidget),
33         vaw1(new AddressWidget(this)),
34         vcw1(new ContactWidget(this)),
35
36         nextVendorButton(new QToolButton),
37         previousVendorButton(new QToolButton),
38         editVendor(new QPushButton("Edit Vendor")),
39         addVendor(new QPushButton("Add Vendor")),
40         addLocation(new QPushButton("Add Location")),
41         addContact(new QPushButton("Add Contact")),
42         createAlert(new QPushButton("Create")),
43         createNote(new QPushButton("Create")),
44         showOpen(new QPushButton("Open")),
45         showClosed(new QPushButton("Closed")),
46         showAll(new QPushButton("All")),
47         createPO(new QPushButton("Create")),
48         vendorRelated(new QCheckBox("Show POs for this Vendor only")),
49         vidCursor(0)
50 {
51         // Create main page widgets & layout
52
53         purchaseOrders->setRootIsDecorated(false);
54         purchaseOrders->setAlternatingRowColors(true);
55
56         QAbstractItemModel * model = new QStandardItemModel(0, 7);
57         model->setHeaderData(0, Qt::Horizontal, tr("PO #"));
58         model->setHeaderData(1, Qt::Horizontal, tr("Vendor"));
59         model->setHeaderData(2, Qt::Horizontal, tr("Desc."));
60         model->setHeaderData(3, Qt::Horizontal, tr("OAD"));
61         model->setHeaderData(4, Qt::Horizontal, tr("UAD"));
62         model->setHeaderData(5, Qt::Horizontal, tr("Docs"));
63         model->setHeaderData(6, Qt::Horizontal, tr("FAI"));
64
65         model->insertRow(0);
66         model->setData(model->index(0, 0), "FE823724");
67         model->setData(model->index(0, 1), "Sanford & Sons");
68         model->setData(model->index(0, 2), "Misc. Junk");
69         model->setData(model->index(0, 3), "09/30/2012");
70         model->setData(model->index(0, 4), "");
71         model->setData(model->index(0, 5), "");
72         model->setData(model->index(0, 6), "");
73
74         model->insertRow(0);
75         model->setData(model->index(0, 0), "89237923-123");
76         model->setData(model->index(0, 1), "Digikey");
77         model->setData(model->index(0, 2), "Small capacitors");
78         model->setData(model->index(0, 3), "10/08/2012");
79         model->setData(model->index(0, 4), "11/30/2012");
80         model->setData(model->index(0, 5), "1");
81         model->setData(model->index(0, 6), "Yes");
82
83         model->insertRow(0);
84         model->setData(model->index(0, 0), "T9234CS32");
85         model->setData(model->index(0, 1), "Big, Faceless Company");
86         model->setData(model->index(0, 2), "Important Stuff you need RIGHT NOW");
87         model->setData(model->index(0, 3), "09/28/2012");
88         model->setData(model->index(0, 4), "09/29/2012");
89         model->setData(model->index(0, 5), "5");
90         model->setData(model->index(0, 6), "Yes");
91
92         purchaseOrders->setModel(model);
93
94         nextVendorButton->setArrowType(Qt::RightArrow);
95         nextVendorButton->setToolTip(tr("Move to next Vendor in Database."));
96         previousVendorButton->setArrowType(Qt::LeftArrow);
97         previousVendorButton->setToolTip(tr("Move to previous Vendor in Database."));
98
99         QFont * vendorFont = new QFont;
100         vendorFont->setPointSize(12);
101         vendorName->setFont(*vendorFont);
102         vendorName->setText("Vendor Name");
103
104         QHBoxLayout * hbox1 = new QHBoxLayout;
105         hbox1->addWidget(previousVendorButton);
106         hbox1->addWidget(nextVendorButton);
107         hbox1->addWidget(vendorName);
108
109         // Vendor address widgets
110
111 //      vaw1->SetFields("123 Any Street", "Any City", "Anywhere", "USA", "01234");
112         vendorAddress->addTab(vaw1, tr("Primary"));
113         vendorAddress->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
114
115         // Vendor contact widgets
116
117 //      vcw1->SetFields("Sales", "Joe Blow", "joe.blow@widget.com", "Singapore", "512-222-2222", "", "512-122-2123");
118         vendorContact->addTab(vcw1, tr("Contact #1"));
119         vendorContact->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
120
121         // Vendor class list
122
123         QVBoxLayout * vbox1 = new QVBoxLayout;
124         vbox1->addWidget(vendorClass);
125         QGroupBox * gb1 = new QGroupBox(tr("Vendor Classes"));
126         gb1->setLayout(vbox1);
127
128         // Vendor action push buttons
129
130         QVBoxLayout * vbox4 = new QVBoxLayout;
131         QHBoxLayout * hbox3 = new QHBoxLayout;
132         QGroupBox * gb2 = new QGroupBox(tr("Vendor Actions"));
133
134         vbox4->addWidget(editVendor);
135         vbox4->addWidget(addVendor);
136         vbox4->addWidget(addLocation);
137         vbox4->addWidget(addContact);
138         gb2->setLayout(vbox4);
139
140         hbox3->addWidget(vendorLevel);
141         hbox3->addWidget(gb2);
142
143         // Alerts & notes
144
145         QVBoxLayout * vbox5 = new QVBoxLayout;
146         QHBoxLayout * hbox4 = new QHBoxLayout;
147         QHBoxLayout * hbox5 = new QHBoxLayout;
148
149         QLabel * label1 = new QLabel(tr("Alerts"));
150         QLabel * label2 = new QLabel(tr("Notes"));
151         hbox4->addWidget(label1);
152         hbox4->addStretch();
153         hbox4->addWidget(createAlert);
154         hbox5->addWidget(label2);
155         hbox5->addStretch();
156         hbox5->addWidget(createNote);
157
158         vbox5->addLayout(hbox4);
159         vbox5->addWidget(alerts);
160         vbox5->addLayout(hbox5);
161         vbox5->addWidget(notes);
162
163         // Purchase Order headers
164
165         QHBoxLayout * hbox6 = new QHBoxLayout;
166         QLabel * label3 = new QLabel(tr("Purchase Orders"));
167         hbox6->addWidget(label3);
168         hbox6->addSpacing(32);
169         hbox6->addWidget(showOpen);
170         hbox6->addWidget(showClosed);
171         hbox6->addWidget(showAll);
172         hbox6->addSpacing(16);
173         hbox6->addWidget(vendorRelated);
174         hbox6->addStretch();
175         hbox6->addWidget(createPO);
176
177         // Horizontal line
178
179         QFrame * hline = new QFrame(this);
180         hline->setFrameShape(QFrame::HLine);
181         hline->setFrameShadow(QFrame::Sunken);
182
183         QFrame * hline2 = new QFrame(this);
184         hline2->setFrameShape(QFrame::HLine);
185         hline2->setFrameShadow(QFrame::Sunken);
186
187         QHBoxLayout * hbox2 = new QHBoxLayout;
188         QVBoxLayout * vbox2 = new QVBoxLayout;
189         QVBoxLayout * vbox3 = new QVBoxLayout;
190
191         vbox3->addWidget(vendorContact);
192 //      vbox3->addWidget(vendorLevel);
193         vbox3->addLayout(hbox3);
194
195         vbox2->addWidget(vendorAddress);
196         vbox2->addWidget(gb1);
197
198         hbox2->addLayout(vbox2);
199         hbox2->addLayout(vbox3);
200         hbox2->addLayout(vbox5);
201
202         QVBoxLayout * mainLayout = new QVBoxLayout;
203         mainLayout->addWidget(username);
204         mainLayout->addWidget(hline);
205         mainLayout->addLayout(hbox1);
206         mainLayout->addLayout(hbox2);
207         mainLayout->addWidget(hline2);
208         mainLayout->addLayout(hbox6);
209         mainLayout->addWidget(purchaseOrders);
210         setLayout(mainLayout);
211
212         // Set up actions
213
214         connect(nextVendorButton, SIGNAL(clicked()), this, SLOT(GetNextVendor()));
215         connect(previousVendorButton, SIGNAL(clicked()), this, SLOT(GetPreviousVendor()));
216         connect(createNote, SIGNAL(clicked()), this, SLOT(CreateNote()));
217         connect(createAlert, SIGNAL(clicked()), this, SLOT(CreateAlert()));
218         connect(createPO, SIGNAL(clicked()), this, SLOT(CreatePurchaseOrder()));
219         connect(showOpen, SIGNAL(clicked()), this, SLOT(ShowOpenPOs()));
220         connect(showClosed, SIGNAL(clicked()), this, SLOT(ShowClosedPOs()));
221         connect(showAll, SIGNAL(clicked()), this, SLOT(ShowAllPOs()));
222 //      connect(addVendor, SIGNAL(clicked()), this, SLOT(AddVendor()));
223         connect(addLocation, SIGNAL(clicked()), this, SLOT(AddLocation()));
224         connect(addContact, SIGNAL(clicked()), this, SLOT(AddContact()));
225         connect(editVendor, SIGNAL(clicked()), this, SLOT(EditVendor()));
226
227         GetVendorIDs();
228         GetVendor(vendorID[vidCursor]);
229 }
230
231
232 void SCMWidget::GetNextVendor(void)
233 {
234         if (vidCursor < (vendorID.size() - 1))
235         {
236                 vidCursor++;
237                 GetVendor(vendorID[vidCursor]);
238         }
239 }
240
241
242 void SCMWidget::GetPreviousVendor(void)
243 {
244         if (vidCursor > 0)
245         {
246                 vidCursor--;
247                 GetVendor(vendorID[vidCursor]);
248         }
249 }
250
251
252 void SCMWidget::CreateNote(void)
253 {
254         NoteDialog dlg(currentUID);
255
256         if (dlg.exec() == false)
257                 return;
258
259         UpdateNotes();
260 }
261
262
263 void SCMWidget::CreateAlert(void)
264 {
265         AlertDialog dlg(currentUID);
266
267         if (dlg.exec() == false)
268                 return;
269
270         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
271 //      UpdateAlerts();
272 }
273
274
275 void SCMWidget::CreatePurchaseOrder(void)
276 {
277         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
278 }
279
280
281 void SCMWidget::ShowOpenPOs(void)
282 {
283         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
284 }
285
286
287 void SCMWidget::ShowClosedPOs(void)
288 {
289         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
290 }
291
292
293 void SCMWidget::ShowAllPOs(void)
294 {
295         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
296 }
297
298
299 void SCMWidget::AddVendor(void)
300 {
301         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
302 }
303
304
305 void SCMWidget::AddLocation(void)
306 {
307         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
308 }
309
310
311 void SCMWidget::AddContact(void)
312 {
313         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
314 }
315
316
317 void SCMWidget::EditVendor(void)
318 {
319         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
320 }
321
322
323 void SCMWidget::GetVendorIDs(void)
324 {
325         vendorID.clear();
326
327         QSqlQuery query;
328         query.prepare("SELECT vid FROM Vendor ORDER BY name");
329         query.exec();
330
331         while (query.next())
332         {
333                 int vid = query.value(0).toInt();
334                 vendorID.push_back(vid);
335         }
336 }
337
338
339 void SCMWidget::GetVendor(int key)
340 {
341         // Get main vendor data
342         QSqlQuery query;
343         query.prepare("SELECT v.name, signedNDA, l.address, city, state, country, code, "
344                 "c.name, email, c.address, phone1, phone2, fax, description, v.vlid FROM "
345                 "Vendor AS v LEFT OUTER JOIN Location AS l ON v.vid = l.vid "
346                 "LEFT OUTER JOIN (Contact AS c JOIN ContactType AS ct ON c.ctid = ct.ctid) "
347                 "ON v.vid = c.vid WHERE v.vid = ?");
348         query.addBindValue(key);
349         query.exec();
350
351         // There's a possibility that the query will return multiple rows (which we need
352         // to handle, with multiple tabs), but for now, we ignore any and grab the first.
353         if (query.next())
354         {
355                 vendorName->setText(query.value(0).toString());
356
357                 vaw1->SetFields(query.value(2).toString(),
358                         query.value(3).toString(), query.value(4).toString(),
359                         query.value(5).toString(), query.value(6).toString());
360                 vcw1->SetFields(query.value(13).toString(),
361                         query.value(7).toString(), query.value(8).toString(),
362                         query.value(9).toString(), query.value(10).toString(),
363                         query.value(11).toString(), query.value(12).toString());
364
365                 int vlid = query.value(14).toInt();
366                 vendorLevel->DoQuery(vlid);
367         }
368
369         // Get vendor classes
370         query.prepare("SELECT description FROM VendorSpecificTypes AS vst, VendorType AS vt "
371                 "WHERE vst.vid = ? AND vst.vtid = vt.vtid ORDER BY seqNo");
372         query.addBindValue(key);
373         query.exec();
374
375         vendorClass->clear();
376
377         while (query.next())
378         {
379                 QListWidgetItem * item = new QListWidgetItem(query.value(0).toString());
380                 vendorClass->addItem(item);
381         }
382 }
383
384
385 void SCMWidget::UpdateNotes(void)
386 {
387 //              QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
388 //NID (P-key) | UID | POID | Note
389
390         QSqlQuery query;
391         query.prepare("SELECT note FROM Notes WHERE uid = ?");
392         query.addBindValue(currentUID);
393         query.exec();
394
395         notes->clear();
396
397         while (query.next())
398         {
399                 QListWidgetItem * item = new QListWidgetItem(query.value(0).toString());
400                 notes->addItem(item);
401         }
402 }
403
404
405 void SCMWidget::UpdateAlerts(void)
406 {
407         // TODO: Implementation
408 }
409