void stuffLender::viewBkButtonSlot()
{
tag = BooksView;
setBooksEnv(tag);
switch(booksTableHit)
{
case false: populateBooks(); break;
default: break;
}
populateBooksTable();
addRowToBooks();
}
void stuffLender::populateBooks()
{
bookList = new QList<book>;
int bkNr = 0;
queryBooks.exec("SELECT nr, title, edition FROM books;");
while(queryBooks.next())
{
bkNr = queryBooks.value(0).toInt();
QVector<author> authors;
QString selStmt
= "SELECT nr, fname, lname, minitial FROM authors WHERE nr IN (SELECT authorNr FROM bookAuthors WHERE bookNr = ";
selStmt += ");";
queryBookAuthors.exec(selStmt);
while(queryBookAuthors.next())
{
author newAuthor(queryBookAuthors.value(0).toInt(), queryBookAuthors.value(1).toString(), queryBookAuthors.value(2).toString(), queryBookAuthors.value(3).toString());
authors.append(newAuthor);
}
book newBk(bkNr, queryBooks.value(1).toString(), queryBooks.value(2).toInt(), authors);
bookList->append(newBk);
}
booksTableHit = true;
}
void stuffLender::populateBooksTable()
{
ui.booksOnLoan->hide();
ui.booksTable->setRowCount(0);
ui.booksTable->show();
ui.
booksTable->setHorizontalHeaderLabels
(QStringList() <<
"Title" <<
"Author/s" <<
"Ed.");
int nrOfAuthors = 0;
for(int row = 0; row < bookList->count(); row++)
{
addRowToBooks();
const QVector<author> *theAuthors = bookList->at(row).getAuthors();
nrOfAuthors = theAuthors->count();
for(int idx = 0; idx < nrOfAuthors; idx++)
{
if(idx == nrOfAuthors - 1)
authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial();
else
authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial() + ", ";
}
ui.booksTable->item(row, 0)->setText(bookList->at(row).getTitle());
ui.booksTable->item(row, 1)->setText(authors);
ui.
booksTable->item
(row,
2)->setText
(QString::number(bookList
->at
(row
).
getEdition()));
}
//ui.booksTable->removeRow(ui.booksTable->rowCount());
}
void stuffLender::addRowToBooks()
{
int row = ui.booksTable->rowCount();
ui.booksTable->insertRow(row);
item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 0, item0);
item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 1, item1);
item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 2, item2);
}
void stuffLender::viewBkButtonSlot()
{
tag = BooksView;
setBooksEnv(tag);
switch(booksTableHit)
{
case false: populateBooks(); break;
default: break;
}
populateBooksTable();
addRowToBooks();
}
void stuffLender::populateBooks()
{
QSqlQuery queryBooks;
QSqlQuery queryBookAuthors;
bookList = new QList<book>;
int bkNr = 0;
queryBooks.exec("SELECT nr, title, edition FROM books;");
while(queryBooks.next())
{
bkNr = queryBooks.value(0).toInt();
QVector<author> authors;
QString selStmt = "SELECT nr, fname, lname, minitial FROM authors WHERE nr IN (SELECT authorNr FROM bookAuthors WHERE bookNr = ";
selStmt += QString::number(bkNr);
selStmt += ");";
queryBookAuthors.exec(selStmt);
while(queryBookAuthors.next())
{
author newAuthor(queryBookAuthors.value(0).toInt(), queryBookAuthors.value(1).toString(), queryBookAuthors.value(2).toString(), queryBookAuthors.value(3).toString());
authors.append(newAuthor);
}
book newBk(bkNr, queryBooks.value(1).toString(), queryBooks.value(2).toInt(), authors);
bookList->append(newBk);
}
booksTableHit = true;
}
void stuffLender::populateBooksTable()
{
ui.booksOnLoan->hide();
ui.booksTable->setRowCount(0);
ui.booksTable->show();
ui.booksTable->setHorizontalHeaderLabels(QStringList() << "Title" << "Author/s" << "Ed.");
int nrOfAuthors = 0;
for(int row = 0; row < bookList->count(); row++)
{
addRowToBooks();
QString authors;
const QVector<author> *theAuthors = bookList->at(row).getAuthors();
nrOfAuthors = theAuthors->count();
for(int idx = 0; idx < nrOfAuthors; idx++)
{
if(idx == nrOfAuthors - 1)
authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial();
else
authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial() + ", ";
}
ui.booksTable->item(row, 0)->setText(bookList->at(row).getTitle());
ui.booksTable->item(row, 1)->setText(authors);
ui.booksTable->item(row, 2)->setText(QString::number(bookList->at(row).getEdition()));
}
//ui.booksTable->removeRow(ui.booksTable->rowCount());
}
void stuffLender::addRowToBooks()
{
int row = ui.booksTable->rowCount();
ui.booksTable->insertRow(row);
QTableWidgetItem *item0 = new QTableWidgetItem;
item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 0, item0);
QTableWidgetItem *item1 = new QTableWidgetItem;
item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 1, item1);
QTableWidgetItem *item2 = new QTableWidgetItem;
item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 2, item2);
}
To copy to clipboard, switch view to plain text mode
Bookmarks