Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef EKZ_H
  2. #define EKZ_H
  3.  
  4. #include <qmainwindow.h>
  5. #include <QInputDialog>
  6. #include <QMessageBox>
  7. #include "ui_ekz.h"
  8.  
  9. class Action {
  10.  
  11. protected:
  12.  
  13.     bool completed = false;
  14.     QString name = "";
  15.  
  16. public:
  17.  
  18.     Action(QString name)
  19.     {
  20.         this->name = name;
  21.     }
  22.  
  23.     QString getName()
  24.     {
  25.         return name;
  26.     }
  27.  
  28.     void setCompleted(bool isComplete)
  29.     {
  30.         this->completed = isComplete;
  31.     }
  32.  
  33.     bool isComleted()
  34.     {
  35.         return completed;
  36.     }
  37.  
  38. };
  39.  
  40. class Ekz: public QMainWindow {
  41. Q_OBJECT
  42.  
  43. protected:
  44.  
  45.     QList<Action*> actions;
  46.  
  47. public:
  48.  
  49.     Ekz(QWidget *parent = 0)
  50.     {
  51.         ui.setupUi(this);
  52.         ui.tblActions->setEditTriggers(QAbstractItemView::NoEditTriggers);
  53.     }
  54.  
  55.     ~Ekz()
  56.     {
  57.  
  58.     }
  59.  
  60.     QTableWidgetItem* getTableItem(QTableWidget* table, int x, int y)
  61.     {
  62.         QTableWidgetItem* item = table->item(x, y);
  63.  
  64.         if (item == nullptr)
  65.         {
  66.             item = new QTableWidgetItem("");
  67.             table->setItem(x, y, item);
  68.         }
  69.  
  70.         return item;
  71.     }
  72.  
  73.     void updateTable()
  74.     {
  75.         QTableWidget* table = ui.tblActions;
  76.  
  77.         table->setColumnCount(1);
  78.         table->setRowCount(actions.size());
  79.  
  80.         Action* act = nullptr;
  81.  
  82.         for (int i1 = 0; i1 < table->rowCount(); i1 ++)
  83.         {
  84.             act = actions.at(i1);
  85.  
  86.             QTableWidgetItem* item = getTableItem(table, i1, 0);
  87.  
  88.             item->setText(act->getName());
  89.             item->setBackground(QBrush(act->isComleted() ? Qt::green : Qt::red));
  90.         }
  91.     }
  92.  
  93.     Action* findAction(QString name)
  94.     {
  95.         for (int i1 = 0; i1 < actions.size(); i1 ++)
  96.         {
  97.             if (actions.at(i1)->getName() == name)
  98.             {
  99.                 return actions.at(i1);
  100.             }
  101.         }
  102.  
  103.         return nullptr;
  104.     }
  105.  
  106. public slots:
  107.  
  108.     void on_actAdd_triggered()
  109.     {
  110.         QString name = QInputDialog::getText(nullptr, "Ввод", "Введите название дела");
  111.  
  112.         if (name.isEmpty())
  113.         {
  114.             return;
  115.         }
  116.  
  117.         if (findAction(name) != nullptr)
  118.         {
  119.             QMessageBox::warning(nullptr, "Упс...", "В вашем списке уже существуе такое дело!");
  120.  
  121.             return;
  122.         }
  123.  
  124.         actions.append(new Action(name));
  125.  
  126.         updateTable();
  127.     }
  128.  
  129.     void on_actRemove_triggered()
  130.     {
  131.         int index = ui.tblActions->currentRow();
  132.  
  133.         Action* act = actions.at(index);
  134.  
  135.         actions.removeAt(index);
  136.  
  137.         updateTable();
  138.  
  139.         delete act;
  140.     }
  141.  
  142.     void on_actComplete_triggered()
  143.     {
  144.         Action* act = actions.at(ui.tblActions->currentRow());
  145.  
  146.         act->setCompleted(true);
  147.  
  148.         updateTable();
  149.     }
  150.  
  151.     void on_actUncomplete_triggered()
  152.     {
  153.         Action* act = actions.at(ui.tblActions->currentRow());
  154.  
  155.         act->setCompleted(false);
  156.  
  157.         updateTable();
  158.     }
  159.  
  160. private:
  161.     Ui::EkzClass ui;
  162. };
  163.  
  164. #endif // EKZ_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement