Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: MyWindow.cpp
- * Author: mvhs977
- *
- * Created on 28 August 2015, 9:45 PM
- */
- #include "MyWindow.h"
- #include "MyRecord.h"
- #include "MyTableModel.h"
- #include<iostream>
- using namespace std;
- MyWindow::MyWindow(vector<RecordPtr> *theData){
- data = theData;
- widget.setupUi(this);
- tablemodel = new MyTableModel(0);
- tablemodel->addTheData(theData);
- widget.tableView->setModel(tablemodel);
- widget.tableView->resizeRowsToContents();
- widget.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
- connect(widget.imageSelector,SIGNAL(clicked()),this,SLOT(chooseFile()));
- connect(widget.roleButton,SIGNAL(clicked()),this,SLOT(addRole()));
- connect(widget.addRecord,SIGNAL(clicked()),this,SLOT(addRecord()));
- connect(
- widget.tableView,
- SIGNAL(clicked(const QModelIndex&)),this,
- SLOT(itemSelection(const QModelIndex&))
- );
- }
- MyWindow::~MyWindow() {
- }
- void MyWindow::chooseFile(){
- QString fileName = QFileDialog::getOpenFileName(this,
- "Open Image", ".", "Image Files (*.png *.jpg *.bmp)");
- if(!fileName.isEmpty()){
- widget.pictureField->setText(fileName);
- }
- }
- void MyWindow::addRole(){
- QString aRole = widget.newRoleName->text().trimmed();
- if (aRole.isEmpty()) return;
- if (widget.roleList->text().isEmpty())
- widget.roleList->setText(aRole);
- else {
- QString roles = widget.roleList->text();
- roles.append(",");
- roles.append(aRole);
- widget.roleList->setText(roles);
- }
- widget.newRoleName->clear();
- }
- void MyWindow::addRecord(){
- bool ok = true;
- string problems;
- QString idstr = widget.idField->text().trimmed();
- QString namestr = widget.nameField->text().trimmed();
- QString filestr = widget.pictureField->text().trimmed();
- QString rolestr = widget.roleList->text();
- if (idstr.isEmpty()){
- ok = false;
- problems.append(" You didn't supply a record id.");
- }
- if (namestr.isEmpty()){
- ok = false;
- problems.append(" You didn't supply a name.");
- }
- if (filestr.isEmpty()){
- ok = false;
- problems.append(" You didn't supply a picture file.");
- }
- if (rolestr.isEmpty()){
- ok = false;
- problems.append(" You didn't define any roles.");
- }
- string filename = filestr.toStdString();
- string stlchars = getImage(filename);
- QString imgchars = stlchars.c_str();
- if (imgchars == NULL) {
- ok = false;
- problems.append(" Unable to load image.");
- }
- string recordid = idstr.toStdString();
- bool idPresent = checkForId(recordid);
- if (idPresent){
- ok = false;
- problems.append("You already have a record with that id.");
- }
- if (!ok) {
- QMessageBox msgBox;
- msgBox.setWindowTitle("Error!");
- msgBox.setText("Invalid record");
- msgBox.setDetailedText(problems.c_str());
- return;
- }
- RecordPtr newrec = new MyRecord(recordid);
- string nstr = namestr.toStdString();
- newrec->setName(nstr);
- string imgstr = imgchars.toStdString();
- newrec->setImage(imgstr);
- QStringList list = rolestr.split(",",QString::SkipEmptyParts);
- QStringList::const_iterator constIterator;
- for (constIterator = list.constBegin(); constIterator != list.constEnd(); ++constIterator){
- string str = (*constIterator).toStdString();
- newrec->addRole(str);
- }
- tablemodel->addRecord(newrec);
- widget.tableView->resizeRowsToContents();
- QMessageBox report;
- report.setWindowTitle("Success!");
- report.exec();
- widget.idField->clear();
- widget.nameField->clear();
- widget.pictureField->clear();
- widget.newRoleName->clear();
- widget.roleList->clear();
- }
- void MyWindow::itemSelection(const QModelIndex &index){
- cout << "Selected row " << index.row() << "\t";
- RecordPtr p = data->at(index.row());
- cout << p->getName() <<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement