Advertisement
Guest User

Untitled

a guest
May 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.48 KB | None | 0 0
  1. #pragma once
  2. #include "Service.h"
  3. #include <qwidget.h>
  4. #include <qlistwidget.h>
  5. #include <qpushbutton.h>
  6. #include <qlineedit.h>
  7. #include <qboxlayout.h>
  8. #include "activity.h"
  9. #include <vector>
  10. #include <qlabel.h>
  11. #include "activitatiCurente.h"
  12.  
  13. class ConsoleGui : public QWidget{
  14. private:
  15.     Service& srv;
  16.     QListWidget* list;
  17.     QListWidget* cos;
  18.     QListWidget* listaFiltre;
  19.     QPushButton* store;
  20.     QPushButton* remove;
  21.     QPushButton* sortByTitle;
  22.     QLineEdit* txtTitle;
  23.     QLineEdit* txtType;
  24.     QLineEdit* txtRandom;
  25.     QLineEdit* txtDescriere;
  26.     QLineEdit* txtDurata;
  27.     QLineEdit* txtDescriereFiltru;
  28.     QLineEdit* txtTipFiltru;
  29.     QLineEdit* txtExportCvs;
  30.     QLineEdit* txtExportHtml;
  31.     QPushButton* sortByDescription;
  32.     QPushButton* sortTipTime;
  33.     QPushButton* filtreButton;
  34.     QPushButton* undoButton;
  35.     QPushButton* modificareButton;
  36.     QPushButton* cosButton;
  37.     QPushButton* adaugaListaButton;
  38.     QPushButton* randomInListaButton;
  39.     QPushButton* golesteListaButton;
  40.     QPushButton* filtruDescButton;
  41.     QPushButton* filtruTipButton;
  42.     QPushButton* exportCvsButton;
  43.     QPushButton* exportHtmlButton;
  44.     void init();
  45.     void connectSignalsSlots();
  46.     void reloadList(const std::vector<Activity>& acts);
  47.     void reloadCos(activitatiCurente& acts);
  48.     void addActivity();
  49.     void connectSignalCos();
  50.     void connectSignalFiltre();
  51.     void reloadListaFiltre(const std::vector<Activity>& acts);
  52. public:
  53.     ConsoleGui(Service srv) : srv{ srv } {
  54.         init();
  55.         connectSignalsSlots();
  56.         reloadList(srv.getAll());
  57.     }
  58. };
  59.  
  60.  
  61.  
  62. #include "UiGUI.h"
  63. #include <algorithm>
  64. #include <QtWidgets/QHBoxLayout>
  65. #include <QtWidgets/QVBoxLayout>
  66. #include <QtWidgets/QFormLayout>
  67. #include <QtWidgets/QLabel>
  68. #include <QtWidgets/QMessageBox>
  69. #include <string>
  70. #include <qdebug.h>
  71. #include "export.h"
  72.  
  73. void ConsoleGui::init() {
  74.     QVBoxLayout* vL = new QVBoxLayout;
  75.     setLayout(vL);
  76.  
  77.     //adaug lista si sub doua butoane de sort
  78.     QWidget* widDreapta = new QWidget;
  79.     QVBoxLayout* vl = new QVBoxLayout;
  80.     widDreapta->setLayout(vl);
  81.     list = new QListWidget;
  82.     vl->addWidget(list);
  83.  
  84.     QWidget* widBtnDreapta = new QWidget;
  85.     QHBoxLayout* lyBtnsDr = new QHBoxLayout;
  86.     widBtnDreapta->setLayout(lyBtnsDr);
  87.     this->sortByTitle = new QPushButton("Sort by title");
  88.     lyBtnsDr->addWidget(this->sortByTitle);
  89.     this->sortByDescription = new QPushButton("Sort description");
  90.     lyBtnsDr->addWidget(sortByDescription);
  91.     this->sortTipTime = new QPushButton("SortTip&Time");
  92.     lyBtnsDr->addWidget(sortTipTime);
  93.     vL->addWidget(widDreapta);
  94.     vL->addWidget(widBtnDreapta);
  95.     this->undoButton = new QPushButton("&Undo");
  96.     this->modificareButton = new QPushButton("&Modifica");
  97.     this->cosButton = new QPushButton("&Cos de activitati");
  98.     this->filtreButton = new QPushButton("&Filtre");
  99.     QWidget* down = new QWidget;
  100.     QVBoxLayout* lyDown = new QVBoxLayout;
  101.     down->setLayout(lyDown);
  102.     lyDown->addWidget(undoButton);
  103.     lyDown->addWidget(modificareButton);
  104.     lyDown->addWidget(cosButton);
  105.     lyDown->addWidget(filtreButton);
  106.     vL->addWidget(down);
  107.  
  108.     //DETAIL
  109.     QWidget* details = new QWidget;
  110.     QFormLayout* fL = new QFormLayout;
  111.     details->setLayout(fL);
  112.     QLabel* lblTitle = new QLabel("Titlu");
  113.     txtTitle = new QLineEdit;
  114.     fL->addRow(lblTitle, txtTitle);
  115.     QLabel* lblType = new QLabel("Tip");
  116.     txtType = new QLineEdit;
  117.     fL->addRow(lblType, txtType);
  118.     QLabel* lblDescriere = new QLabel("Descriere");
  119.     txtDescriere = new QLineEdit;
  120.     fL->addRow(lblDescriere, txtDescriere);
  121.     QLabel* lblDurata = new QLabel("Durata");
  122.     txtDurata = new QLineEdit;
  123.     fL->addRow(lblDurata, txtDurata);
  124.     vL->addWidget(details);
  125.     store = new QPushButton("&Store");
  126.     remove = new QPushButton("&Remove");
  127.     vL->addWidget(store);
  128.     vL->addWidget(remove);
  129. }
  130.  
  131. void ConsoleGui::addActivity() {
  132.     try {
  133.         this->srv.add(txtTitle->text().toStdString(), txtDescriere->text().toStdString(), txtType->text().toStdString(), txtDurata->text().toInt());
  134.         //ctr.add(txtType->text().toStdString(), txtSpecies->text().toStdString(), txtPrice->text().toDouble());
  135.         //reloadList(ctr.getAllPets());
  136.         reloadList(srv.getAll());
  137.     }
  138.     catch (ValidateException&) {
  139.         QMessageBox::warning(this, "Warning","Nu e bine ");
  140.     }
  141.     catch (RepoERROR&) {
  142.         QMessageBox::warning(this, "Warning", "Mai exista unul ");
  143.     }
  144. }
  145.  
  146. void ConsoleGui::connectSignalsSlots() {
  147.     QObject::connect(store, &QPushButton::clicked, this, &ConsoleGui::addActivity);
  148.    
  149.     QObject::connect(sortByTitle, &QPushButton::clicked, this, [&]() {
  150.         reloadList(this->srv.sortareTitlu());
  151.     });
  152.  
  153.     QObject::connect(sortByDescription, &QPushButton::clicked, this, [&]() {
  154.         reloadList(this->srv.sortareDescriere());
  155.     });
  156.  
  157.     QObject::connect(sortTipTime, &QPushButton::clicked, this, [&]() {
  158.         reloadList(this->srv.sortareTipSiDurata());
  159.     });
  160.  
  161.     QObject::connect(modificareButton, &QPushButton::clicked, this, [&]() {
  162.         /*
  163.         QLineEdit* txtTitle;
  164.         QLineEdit* txtType;
  165.         QLineEdit* txtDescriere;
  166.         QLineEdit* txtDurata;
  167.         */
  168.         QString titlu = this->txtTitle->text();
  169.         QString descriere = this->txtDescriere->text();
  170.         QString type = this->txtType->text();
  171.         QString durata = this->txtDurata->text();
  172.         try {
  173.             srv.deleteActivity(titlu.toStdString());
  174.         }
  175.         catch (StergereERROR&) {
  176.             QMessageBox::warning(this, "Warning", "!!! ");
  177.         }
  178.         try {
  179.             srv.add(titlu.toStdString(), descriere.toStdString(), type.toStdString(), durata.toInt());
  180.         }
  181.         catch (ValidateException&) {
  182.             QMessageBox::warning(this, "Warning", "!!! ");
  183.         }
  184.         reloadList(srv.getAll());
  185.     });
  186.  
  187.     QObject::connect(cosButton, &QPushButton::clicked, this, [&]() {
  188.         QWidget* widgetCos = new QWidget();
  189.         widgetCos->setWindowTitle("Cos Activitati");
  190.         QFormLayout* lay = new QFormLayout;
  191.         cos = new QListWidget;
  192.         widgetCos->setLayout(lay);
  193.         lay->addWidget(cos);
  194.         adaugaListaButton = new QPushButton("&Adauga in lista");
  195.         QLabel* lblRandom = new QLabel("Random:");
  196.         txtRandom = new QLineEdit;
  197.         randomInListaButton = new QPushButton("&Random");
  198.         lay->addRow(lblRandom, txtRandom);
  199.         lay->addWidget(randomInListaButton);
  200.         golesteListaButton = new QPushButton("&Goleste lista");
  201.         lay->addWidget(adaugaListaButton);
  202.         lay->addWidget(golesteListaButton);
  203.         QLabel* lblCVS = new QLabel("CVS file : ");
  204.         QLabel* lblHtml = new QLabel("HTML file :");
  205.         exportCvsButton = new QPushButton("Export CVS");
  206.         exportHtmlButton = new QPushButton("Export Html");
  207.         txtExportCvs = new QLineEdit;
  208.         txtExportHtml = new QLineEdit;
  209.         lay->addRow(lblCVS,txtExportCvs);
  210.         lay->addWidget(exportCvsButton);
  211.         lay->addRow(lblHtml, txtExportHtml);
  212.         lay->addWidget(exportHtmlButton);
  213.         connectSignalCos();
  214.         widgetCos->show();
  215.     });
  216.  
  217.     QObject::connect(undoButton, &QPushButton::clicked, this, [&]() {
  218.         try {
  219.             this->srv.undo();
  220.         }
  221.         catch (RepoERROR& ex) {
  222.             QMessageBox::warning(this, "Warning", QString::fromStdString(ex.getMessage()));
  223.         }
  224.         reloadList(this->srv.getAll());
  225.     });
  226.  
  227.     QObject::connect(list, &QListWidget::itemSelectionChanged,this, [&]() {
  228.         if (this->list->selectedItems().isEmpty()) {
  229.             this->txtDurata->setText("");
  230.             this->txtType->setText("");
  231.             this->txtDescriere->setText("");
  232.             this->txtTitle->setText("");
  233.             return;
  234.         }
  235.         QListWidgetItem* selItem = list->selectedItems().at(0);
  236.         QString titlu = selItem->text();
  237.         Activity act = srv.cautaActivitate(titlu.toStdString());
  238.         this->txtTitle->setText(titlu);
  239.         this->txtDurata->setText(QString::number(act.getTime()));
  240.         this->txtDescriere->setText(QString::fromStdString(act.getDescription()));
  241.         this->txtType->setText(QString::fromStdString(act.getType()));
  242.  
  243.     });
  244.  
  245.     QObject::connect(remove, &QPushButton::clicked, this, [&]() {
  246.         QString titlu = this->txtTitle->text();
  247.         try {
  248.             srv.deleteActivity(titlu.toStdString());
  249.         }
  250.         catch (StergereERROR&) {
  251.             QMessageBox::warning(this, "Warning", "Selectati ceva ! ");
  252.         }
  253.         reloadList(srv.getAll());
  254.     });
  255.    
  256.     QWidget::connect(filtreButton, &QPushButton::clicked, [&]() {
  257.         QWidget* widgetFiltre = new QWidget;
  258.         QFormLayout* ly = new QFormLayout;
  259.         widgetFiltre->setLayout(ly);
  260.         this->txtDescriereFiltru = new QLineEdit;
  261.         this->txtTipFiltru = new QLineEdit;
  262.         QLabel* lblDescFiltru = new QLabel("Descriere");
  263.         QLabel* lblTipFiltru = new QLabel("Tip");
  264.         this->filtruDescButton = new QPushButton("FiltrareDescriere");
  265.         this->filtruTipButton = new QPushButton("FiltrareTip");
  266.         ly->addRow(lblDescFiltru, txtDescriereFiltru);
  267.         ly->addWidget(filtruDescButton);
  268.         ly->addRow(lblTipFiltru, txtTipFiltru);
  269.         ly->addWidget(filtruTipButton);
  270.         listaFiltre = new QListWidget;
  271.         ly->addWidget(listaFiltre);
  272.         connectSignalFiltre();
  273.         widgetFiltre->setWindowTitle("Filtre");
  274.         widgetFiltre->show();
  275.     });
  276.  
  277. }
  278.  
  279. void ConsoleGui::connectSignalCos() {
  280.     QWidget::connect(adaugaListaButton, &QPushButton::clicked, [&]() {
  281.         QString title = txtTitle->text();
  282.         QString type = txtType->text();
  283.         QString descriere = txtDescriere->text();
  284.         QString durata = txtDurata->text();
  285.         this->srv.adaugaInListaService(title.toStdString());
  286.         reloadCos(this->srv.getLista());
  287.     });
  288.  
  289.     QWidget::connect(golesteListaButton, &QPushButton::clicked, [&]() {
  290.         this->srv.golesteListaService();
  291.         reloadCos(this->srv.getLista());
  292.     });
  293.  
  294.     QWidget::connect(randomInListaButton, &QPushButton::clicked, [&]() {
  295.         this->srv.randomService(txtRandom->text().toUInt());
  296.         reloadCos(this->srv.getLista());
  297.     });
  298.    
  299.     QWidget::connect(exportCvsButton, &QPushButton::clicked, [&]() {
  300.         QString numeF = txtExportCvs->text();
  301.         exportToCVS(numeF.toStdString(), this->srv.getAll());
  302.     });
  303.  
  304.     QWidget::connect(exportHtmlButton, &QPushButton::clicked, [&]() {
  305.         QString numeF = txtExportHtml->text();
  306.         exportToHTML(numeF.toStdString(), this->srv.getAll());
  307.     });
  308.  
  309. }
  310.  
  311. void ConsoleGui::reloadList(const std::vector<Activity>& acts) {
  312.     auto colorize = srv.getAll();
  313.     list->clear();
  314.     for (const auto& act : acts) {
  315.         QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(act.getTitle()), list);
  316.         item->setData(Qt::UserRole, QString::fromStdString(act.getType()));//adaug in lista (invizibil) si type
  317.         //if (std::find(colorize.cbegin(), colorize.cend(), p) !=colorize.end()){//merge pt ca am adaugat operator== la Pet    
  318.             //item->setBackground(QBrush{ QColor{Qt::red},Qt::CrossPattern });     
  319.             //item->setBackgroundColor(QColor{ Qt::red });
  320.         //}
  321.     }
  322. }
  323.  
  324. void ConsoleGui::reloadCos(activitatiCurente& acts) {
  325.     const std::vector<Activity>& activitati = acts.getLista();
  326.     cos->clear();
  327.     for (const auto& act : activitati) {
  328.         QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(act.getTitle()), cos);
  329.     }
  330. }
  331.  
  332. void ConsoleGui::reloadListaFiltre(const std::vector<Activity>& acts) {
  333.     auto colorize = srv.getAll();
  334.     listaFiltre->clear();
  335.     for (const auto& act : acts) {
  336.         QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(act.getTitle()), listaFiltre);
  337.         item->setData(Qt::UserRole, QString::fromStdString(act.getType()));//adaug in lista (invizibil) si type
  338.         //if (std::find(colorize.cbegin(), colorize.cend(), p) !=colorize.end()){//merge pt ca am adaugat operator== la Pet    
  339.             //item->setBackground(QBrush{ QColor{Qt::red},Qt::CrossPattern });     
  340.             //item->setBackgroundColor(QColor{ Qt::red });
  341.         //}
  342.     }
  343. }
  344.  
  345. void ConsoleGui::connectSignalFiltre() {
  346.  
  347.     QObject::connect(filtruDescButton, &QPushButton::clicked, [&]() {
  348.         QString desc = this->txtDescriereFiltru->text();
  349.         reloadListaFiltre(this->srv.filtrareDescriereServ(desc.toStdString()));
  350.         //reloadListaFiltre(this->srv.getAll());
  351.     });
  352.  
  353.     QObject::connect(filtruTipButton, &QPushButton::clicked, [&]() {
  354.         QString tip = this->txtTipFiltru->text();
  355.         reloadListaFiltre(this->srv.filtrareTipServ(tip.toStdString()));
  356.     });
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement