/src/KontrahenciLista.cpp

https://github.com/rafalrusin/qfaktury · C++ · 217 lines · 150 code · 23 blank · 44 comment · 17 complexity · 3a35557f37d0dae083d4f337451bd767 MD5 · raw file

  1. #include "moc_KontrahenciLista.cpp"
  2. #include <QMessageBox>
  3. #include <QTextStream>
  4. #include <QDesktopServices>
  5. #include <QUrl>
  6. /** Constructor
  7. */
  8. KontrahenciLista::KontrahenciLista(QWidget *parent): QDialog(parent) {
  9. setupUi(this);
  10. init();
  11. }
  12. /** Init
  13. */
  14. void KontrahenciLista::init() {
  15. // qDebug () << __FUNCTION__;
  16. companiesList.clear();
  17. officesList.clear();
  18. // read data
  19. readKontr();
  20. // load data
  21. QString customer;
  22. listBox1->clear();
  23. foreach (customer, companiesList)
  24. listBox1->addItem(customer.split("|")[0]);
  25. // connects
  26. connect(okBtn, SIGNAL(clicked()), this, SLOT(doAccept()));
  27. connect(cancelBtn, SIGNAL(clicked()), this, SLOT(close()));
  28. connect(comboBox1, SIGNAL(activated(int)), this, SLOT(comboBox1Changed()));
  29. connect(listBox1, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(doAccept()));
  30. connect(listBox1, SIGNAL(itemSelectionChanged ()), this, SLOT(mouseSelect()));
  31. connect(listBox1, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(updateDetails(QListWidgetItem *)));
  32. connect(labelWWWE, SIGNAL(linkActivated (const QString &)), this, SLOT(openCustomerWWW(const QString &)));
  33. connect(labelWWWE, SIGNAL(linkHovered ( const QString &)), this, SLOT(openCustomerWWW(const QString &)));
  34. }
  35. // *************************** SLOTS START *************************************
  36. void KontrahenciLista::mouseSelect() {
  37. // qDebug() << __FILE__ << __LINE__ << __FUNCTION__;
  38. updateDetails(listBox1->currentItem());
  39. }
  40. /** Slot
  41. * Connected to accept signal
  42. */
  43. void KontrahenciLista::doAccept() {
  44. QList<QListWidgetItem *> selected = listBox1->selectedItems();
  45. if (!selected.isEmpty()) {
  46. ret = detailsToString();
  47. accept();
  48. } else {
  49. QMessageBox::information(this, "QFaktury", trUtf8("Wskaż kontrahenta."),
  50. QMessageBox::Ok);
  51. }
  52. }
  53. /** Slot
  54. * comboBox1 changed
  55. */
  56. void KontrahenciLista::comboBox1Changed() {
  57. // qDebug (__FUNCTION__);
  58. listBox1->clear();
  59. clearDetails();
  60. QString customer;
  61. switch (comboBox1->currentIndex()) {
  62. case 0:
  63. foreach (customer, companiesList)
  64. listBox1->addItem(customer.split("|")[0]);
  65. break;
  66. case 1:
  67. foreach (customer, officesList)
  68. listBox1->addItem(customer.split("|")[0]);
  69. break;
  70. }
  71. }
  72. /** Slot
  73. * Update details when user selected
  74. */
  75. void KontrahenciLista::updateDetails(QListWidgetItem *item) {
  76. QStringList custDetails;
  77. QString customer;
  78. switch (comboBox1->currentIndex()) {
  79. case 0:
  80. foreach (customer, companiesList) {
  81. custDetails = customer.split("|");
  82. if (item->text().compare(custDetails[0]) == 0) {
  83. displayDetails(custDetails);
  84. }
  85. }
  86. break;
  87. case 1:
  88. foreach (customer, officesList) {
  89. custDetails = customer.split("|");
  90. if (item->text().compare(custDetails[0]) == 0) {
  91. displayDetails(custDetails);
  92. }
  93. }
  94. break;
  95. }
  96. }
  97. /** Slot
  98. * Opens the WEBSITE of the customer
  99. */
  100. void KontrahenciLista::openCustomerWWW(const QString &url) {
  101. // Doesn't work. Why?
  102. qDebug() << __FUNCTION__ << url;
  103. QDesktopServices::openUrl(QUrl(url));
  104. }
  105. // *************************** SLOTS END *************************************
  106. /** Load selected customer data
  107. */
  108. void KontrahenciLista::readKontr() {
  109. QDomDocument doc(sett().getCustomersDocName());
  110. QDomElement root;
  111. QDomElement office;
  112. QDomElement company;
  113. QFile file(sett().getCustomersXml());
  114. if (!file.open(QIODevice::ReadOnly)) {
  115. qDebug() << "File" << file.fileName() << "doesn't exists";
  116. return;
  117. } else {
  118. QTextStream stream(&file);
  119. if (!doc.setContent(stream.readAll())) {
  120. qDebug("can not set content ");
  121. file.close();
  122. return;
  123. } else {
  124. root = doc.documentElement();
  125. office = root.firstChild().toElement();
  126. company = root.lastChild().toElement();
  127. }
  128. for (QDomNode n = company.firstChild(); !n.isNull(); n = n.nextSibling()) {
  129. companiesList.append(xmlDataToString(n));
  130. }
  131. for (QDomNode n = office.firstChild(); !n.isNull(); n = n.nextSibling()) {
  132. officesList.append(xmlDataToString(n));
  133. }
  134. }
  135. }
  136. /** Loads data one string
  137. */
  138. QString KontrahenciLista::xmlDataToString(QDomNode n) {
  139. QString text;
  140. text = n.toElement().attribute("name") + "|";
  141. text += n.toElement().attribute("address") + "|";
  142. text += n.toElement().attribute("code") + " " + n.toElement().attribute("place") + "|";
  143. text += n.toElement().attribute("tic") + "|";
  144. text += n.toElement().attribute("account") + "|";
  145. text += n.toElement().attribute("phone") + "|";
  146. text += n.toElement().attribute("email") + "|";
  147. text += n.toElement().attribute("www");
  148. return text;
  149. }
  150. /** Loads data to labels
  151. */
  152. void KontrahenciLista::displayDetails(QStringList custDetails) {
  153. labelNameE->setText(custDetails[0]);
  154. labelAddressE->setText(custDetails[1]);
  155. labelCityE->setText(custDetails[2]);
  156. labelTicE->setText(custDetails[3]);
  157. labelAccountE->setText(custDetails[4]);
  158. labelPhoneE->setText(custDetails[5]);
  159. labelEmailE->setText(custDetails[6]);
  160. labelWWWE->setText(custDetails[7]);
  161. }
  162. /** Clear labels
  163. */
  164. void KontrahenciLista::clearDetails() {
  165. labelNameE->setText("");
  166. labelAddressE->setText("");
  167. labelCityE->setText("");
  168. labelTicE->setText("");
  169. labelAccountE->setText("");
  170. labelPhoneE->setText("");
  171. labelEmailE->setText("");
  172. labelWWWE->setText("");
  173. }
  174. /** Labels to string
  175. */
  176. QString KontrahenciLista::detailsToString() {
  177. QString ret = labelNameE->text();
  178. if (!labelAddressE->text().isEmpty())
  179. ret += "," + labelAddressE->text();
  180. if (!labelCityE->text().isEmpty())
  181. ret += "," + labelCityE->text();
  182. if (!labelTicE->text().isEmpty())
  183. ret += "," + trUtf8("NIP: ") + labelTicE->text();
  184. /* not required
  185. if (!labelAccountE->text().isEmpty())
  186. ret += ", " + labelAccountE->text();
  187. if (!labelPhoneE->text().isEmpty())
  188. ret += ", " + labelPhoneE->text();
  189. if (!labelEmailE->text().isEmpty())
  190. ret += ", " + labelEmailE->text();
  191. if (!labelWWWE->text().isEmpty())
  192. ret += ", " + labelWWWE->text(); */
  193. return ret;
  194. }