Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. /*
  2. * File: MyWindow.cpp
  3. * Author: mvhs977
  4. *
  5. * Created on 28 August 2015, 9:45 PM
  6. */
  7.  
  8. #include "MyWindow.h"
  9. #include "MyRecord.h"
  10. #include "MyTableModel.h"
  11. #include<iostream>
  12. using namespace std;
  13.  
  14. MyWindow::MyWindow(vector<RecordPtr> *theData){
  15. data = theData;
  16. widget.setupUi(this);
  17.  
  18. tablemodel = new MyTableModel(0);
  19. tablemodel->addTheData(theData);
  20. widget.tableView->setModel(tablemodel);
  21. widget.tableView->resizeRowsToContents();
  22. widget.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  23. connect(widget.imageSelector,SIGNAL(clicked()),this,SLOT(chooseFile()));
  24. connect(widget.roleButton,SIGNAL(clicked()),this,SLOT(addRole()));
  25. connect(widget.addRecord,SIGNAL(clicked()),this,SLOT(addRecord()));
  26. connect(
  27. widget.tableView,
  28. SIGNAL(clicked(const QModelIndex&)),this,
  29. SLOT(itemSelection(const QModelIndex&))
  30. );
  31. }
  32.  
  33.  
  34. MyWindow::~MyWindow() {
  35. }
  36.  
  37. void MyWindow::chooseFile(){
  38.  
  39. QString fileName = QFileDialog::getOpenFileName(this,
  40. "Open Image", ".", "Image Files (*.png *.jpg *.bmp)");
  41. if(!fileName.isEmpty()){
  42. widget.pictureField->setText(fileName);
  43. }
  44. }
  45.  
  46.  
  47. void MyWindow::addRole(){
  48. QString aRole = widget.newRoleName->text().trimmed();
  49. if (aRole.isEmpty()) return;
  50.  
  51. if (widget.roleList->text().isEmpty())
  52. widget.roleList->setText(aRole);
  53. else {
  54. QString roles = widget.roleList->text();
  55. roles.append(",");
  56. roles.append(aRole);
  57. widget.roleList->setText(roles);
  58. }
  59. widget.newRoleName->clear();
  60. }
  61.  
  62. void MyWindow::addRecord(){
  63. bool ok = true;
  64. string problems;
  65.  
  66. QString idstr = widget.idField->text().trimmed();
  67. QString namestr = widget.nameField->text().trimmed();
  68. QString filestr = widget.pictureField->text().trimmed();
  69. QString rolestr = widget.roleList->text();
  70. if (idstr.isEmpty()){
  71. ok = false;
  72. problems.append(" You didn't supply a record id.");
  73. }
  74. if (namestr.isEmpty()){
  75. ok = false;
  76. problems.append(" You didn't supply a name.");
  77. }
  78. if (filestr.isEmpty()){
  79. ok = false;
  80. problems.append(" You didn't supply a picture file.");
  81. }
  82. if (rolestr.isEmpty()){
  83. ok = false;
  84. problems.append(" You didn't define any roles.");
  85. }
  86. string filename = filestr.toStdString();
  87. string stlchars = getImage(filename);
  88. QString imgchars = stlchars.c_str();
  89. if (imgchars == NULL) {
  90. ok = false;
  91. problems.append(" Unable to load image.");
  92. }
  93. string recordid = idstr.toStdString();
  94. bool idPresent = checkForId(recordid);
  95. if (idPresent){
  96. ok = false;
  97. problems.append("You already have a record with that id.");
  98. }
  99. if (!ok) {
  100. QMessageBox msgBox;
  101. msgBox.setWindowTitle("Error!");
  102. msgBox.setText("Invalid record");
  103. msgBox.setDetailedText(problems.c_str());
  104. return;
  105. }
  106. RecordPtr newrec = new MyRecord(recordid);
  107. string nstr = namestr.toStdString();
  108. newrec->setName(nstr);
  109. string imgstr = imgchars.toStdString();
  110. newrec->setImage(imgstr);
  111. QStringList list = rolestr.split(",",QString::SkipEmptyParts);
  112. QStringList::const_iterator constIterator;
  113. for (constIterator = list.constBegin(); constIterator != list.constEnd(); ++constIterator){
  114. string str = (*constIterator).toStdString();
  115. newrec->addRole(str);
  116. }
  117. tablemodel->addRecord(newrec);
  118. widget.tableView->resizeRowsToContents();
  119. QMessageBox report;
  120. report.setWindowTitle("Success!");
  121. report.exec();
  122. widget.idField->clear();
  123. widget.nameField->clear();
  124. widget.pictureField->clear();
  125. widget.newRoleName->clear();
  126. widget.roleList->clear();
  127. }
  128.  
  129. void MyWindow::itemSelection(const QModelIndex &index){
  130. cout << "Selected row " << index.row() << "\t";
  131. RecordPtr p = data->at(index.row());
  132. cout << p->getName() <<endl;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement