Advertisement
Guest User

qt app

a guest
Jan 21st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.07 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "login.h"
  3. #include "ui_mainwindow.h"
  4.  
  5. #include <QStandardItem>
  6.  
  7.  
  8.  
  9. MainWindow::MainWindow(QWidget *parent) :
  10.     QMainWindow(parent),
  11.     ui(new Ui::MainWindow)
  12. {
  13.  
  14.     Login log_in;
  15.     log_in.exec();
  16.  
  17.  
  18.     ui->setupUi(this);
  19.  
  20.  
  21.     connect(this->ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(MenuItemClicked(QTreeWidgetItem*,int)));
  22.  
  23.  
  24.  
  25.  
  26.     QTreeWidgetItem *manage = new QTreeWidgetItem(ItemType1);
  27.     manage->setText(0,"Zarzadzaj");
  28.  
  29.  
  30.     new_acc = new QTreeWidgetItem(ItemType2);
  31.     new_acc->setText(0,"Dodaj nowe konto");
  32.     del_acc = new QTreeWidgetItem(ItemType2);
  33.     del_acc->setText(0,"Usun konto");
  34.     pass_acc = new QTreeWidgetItem(ItemType2);
  35.     pass_acc->setText(0,"Zmien haslo");
  36.     perm_acc = new QTreeWidgetItem(ItemType2);
  37.     perm_acc->setText(0,"Uprawnienia");
  38.  
  39.  
  40.     ui->treeWidget->addTopLevelItem(manage);
  41.     manage->addChild(new_acc);
  42.     manage->addChild(del_acc);
  43.     manage->addChild(pass_acc);
  44.     manage->addChild(perm_acc);
  45.  
  46.     finder_acc = new QTreeWidgetItem(ItemType2);
  47.     finder_acc->setText(0,"Wyszukaj w bazie danych");
  48.  
  49.     ui->treeWidget->addTopLevelItem(finder_acc);
  50.  
  51.  
  52.  
  53.     content_add_acc = new content(this);
  54.     ui->container->addWidget(content_add_acc);
  55.  
  56.  
  57.     content_chg_pwd = new change_password(this);
  58.     ui->container->addWidget(content_chg_pwd);
  59.  
  60.     content_del_acc = new change_password(this);
  61.     ui->container->addWidget(content_del_acc);
  62.  
  63.     content_finder = new find_acc(this);
  64.     ui->container->addWidget(content_finder);
  65.  
  66.  
  67.  
  68.     QObject::connect(content_add_acc, SIGNAL(user_data_changed(bool)), this, SLOT(user_data_changed_reciver(bool)));
  69.     connect(content_chg_pwd, SIGNAL(password_changed()), this, SLOT(password_change_reciver()));
  70.     connect(content_del_acc, SIGNAL(delete_account()), this, SLOT(account_delete_reciver()));
  71.     connect(content_finder, SIGNAL(find_pressed()), this, SLOT(finder_recived()));
  72.  
  73.  
  74.  
  75.  
  76.     QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
  77.     db.setHostName("localhost");
  78.     db.setDatabaseName("l2dsn");
  79.     db.setUserName("L2USER");
  80.     db.setPassword("lineage");
  81.  
  82.     if (!db.open())
  83.     {
  84.       QMessageBox::critical(0, QObject::tr("Database Error"),
  85.                 db.lastError().text());
  86.     }
  87.  
  88.     query = new QSqlQuery();
  89.     //query->exec("select * from Konto");
  90.  
  91.     model = new QSqlQueryModel() ;
  92.     model->setQuery(*query);
  93.     ui->tableView->setModel(model);
  94.     ui->tableView->resizeColumnsToContents();
  95.  
  96.  
  97.  
  98.  
  99. }
  100.  
  101. MainWindow::~MainWindow()
  102. {
  103.  
  104.     delete query;
  105.     delete model;
  106.     delete ui;
  107. }
  108.  
  109. void MainWindow::MenuItemClicked(QTreeWidgetItem *item, int)
  110. {
  111.     if(item==new_acc)
  112.     {
  113.         ui->container->setCurrentWidget(content_add_acc);
  114.  
  115.  
  116.     }
  117.  
  118.  
  119.  
  120.     if(item==pass_acc)
  121.     {
  122.         ui->container->setCurrentWidget(content_chg_pwd);
  123.         content_chg_pwd->set_tittle_label(QString("Zmień hasło"));
  124.         content_chg_pwd->set_layout_delete_account(false);
  125.     }
  126.  
  127.     if(item==del_acc)
  128.     {
  129.          ui->container->setCurrentWidget(content_del_acc);
  130.          content_del_acc->set_tittle_label(QString("Usuń konto"));
  131.          content_del_acc->set_layout_delete_account(true);
  132.  
  133.     }
  134.  
  135.     if(item==finder_acc)
  136.     {
  137.         ui->container->setCurrentWidget(content_finder);
  138.  
  139.     }
  140.  
  141.  
  142.  
  143. }
  144.  
  145.  
  146. void MainWindow::manage_create_user()
  147. {
  148.  
  149.  
  150. }
  151.  
  152. void MainWindow::user_data_changed_reciver(bool flaga)
  153. {
  154.     if(flaga==true)
  155.     {
  156.         QSqlQueryModel model;
  157.         QSqlQuery query;
  158.         query.prepare("INSERT INTO Konto (ID_KONTO, LOGIN, HASLO, STATUS_DOSTEPU, E_MAIL) "
  159.                          "VALUES (SEQ_KONTO.nextval, :LOGIN, :HASLO, :STATUS_DOSTEPU, :E_MAIL)");
  160.  
  161.         QString  temp_login, temp_password, temp_repassword, temp_email, temp_access_level;
  162.  
  163.  
  164.  
  165.         temp_login=content_add_acc->get_login();
  166.         temp_password=content_add_acc->get_password();
  167.         temp_repassword=content_add_acc->get_repassword();
  168.         temp_email=content_add_acc->get_email();
  169.         temp_access_level=content_add_acc->get_access();
  170.  
  171.         if(temp_password!=temp_repassword)
  172.         {
  173.             set_status_label("Wystapil blad! Podane hasła nie są ze sobą zgodne ",true);
  174.             return;
  175.         }
  176.  
  177.  
  178.         query.bindValue( ":LOGIN",  temp_login );
  179.         query.bindValue( ":HASLO", temp_password );
  180.         query.bindValue( ":STATUS_DOSTEPU", temp_access_level );
  181.         query.bindValue( ":E_MAIL", temp_email  );
  182.  
  183.         if(!query.exec())
  184.                set_status_label("Wystapil blad: " + query.lastError().text(),true);
  185.             else{
  186.                set_status_label("Uzytkownik dodany pomyslnie",false);
  187.  
  188.                 QSqlQuery recently_added;
  189.                 recently_added.prepare("SELECT * FROM KONTO WHERE LOGIN = :PODANY_LOGIN" );
  190.                 recently_added.bindValue(":PODANY_LOGIN",temp_login);
  191.                 recently_added.exec();
  192.                 this->model->setQuery(recently_added);
  193.                 ui->tableView->setModel(this->model);
  194.             }
  195.     }
  196. }
  197.  
  198. void MainWindow::password_change_reciver()
  199. {
  200.      QString  temp_login, temp_old_pwd, temp_new_pwd, temp_new_re_pwd;
  201.      temp_login=    content_chg_pwd->   get_login();
  202.      temp_old_pwd=  content_chg_pwd ->  get_oldpassword();
  203.      temp_new_pwd=  content_chg_pwd->   get_new_password();
  204.      temp_new_re_pwd=content_chg_pwd->  get_new_repassword();
  205.  
  206.      if(temp_login.isEmpty() || temp_new_pwd.isEmpty() || temp_new_re_pwd.isEmpty() ||temp_old_pwd.isEmpty())
  207.      {
  208.          set_status_label("Prosze uzupelnic wszystkie pola",true);
  209.          return;
  210.      }
  211.  
  212.     QSqlQuery query;
  213.     QString user_passowrd;
  214.  
  215.         query.prepare("SELECT HASLO FROM KONTO WHERE LOGIN = :LOGIN");
  216.         query.bindValue(":LOGIN",temp_login);
  217.  
  218.         if(!query.exec())
  219.         {
  220.             set_status_label("Blad polaczenia z baza danych",true);
  221.             return;
  222.         }
  223.        query.next();
  224.  
  225.         user_passowrd=query.value(0).toString();
  226.          qDebug() << user_passowrd;
  227.         //qDebug() << query.lastError();
  228.  
  229.  
  230.  
  231.         if(user_passowrd!=temp_old_pwd)
  232.         {
  233.             set_status_label("Podane haslo jest nieprawidlowe",true);
  234.             return;
  235.         }
  236.  
  237.        if(temp_new_pwd!=temp_new_re_pwd)
  238.        {
  239.             set_status_label("Upewnij sie ze podane nowe haslo zostalo wprowadzone dwukrotnie identycznie",true);
  240.             return;
  241.        }
  242.  
  243.        QSqlQuery change_pwd;
  244.        change_pwd.prepare("Update KONTO SET HASLO = :NOWE_HASLO WHERE LOGIN = :LOGIN");
  245.        change_pwd.bindValue(":NOWE_HASLO", temp_new_pwd);
  246.        change_pwd.bindValue(":LOGIN",temp_login);
  247.  
  248.        if(!change_pwd.exec())
  249.        {
  250.            set_status_label("Nie udalo sie przeprowadzic aktualizacji w bazie danych",true);
  251.            return;
  252.        }
  253.        else
  254.        {
  255.            set_status_label("Haslo zostalo pomyslnie zmienione",false);
  256.  
  257.            QSqlQuery recently_added;
  258.            recently_added.prepare("SELECT * FROM KONTO WHERE LOGIN = :PODANY_LOGIN" );
  259.            recently_added.bindValue(":PODANY_LOGIN",temp_login);
  260.            recently_added.exec();
  261.            this->model->setQuery(recently_added);
  262.            ui->tableView->setModel(this->model);
  263.        }
  264. }
  265.  
  266. void MainWindow::account_delete_reciver()
  267. {
  268.     QSqlQuery query;
  269.     QString user_passowrd;
  270.     QString login, password, repassword;
  271.     content_del_acc->set_layout_delete_account(true);
  272.     login= content_del_acc->get_login();
  273.     password=content_del_acc->get_oldpassword();
  274.     repassword = content_del_acc-> get_new_password();
  275.  
  276.     if ( login.isEmpty() || password.isEmpty() || repassword.isEmpty())
  277.     {
  278.         set_status_label("Prosze uzupelnic wszystkie pola",true);
  279.         return;
  280.     }
  281.  
  282.      if(password!=repassword)
  283.          if(user_passowrd!=password)
  284.          {
  285.              set_status_label("Podane hasła nie sa ze sobą zgodne",true);
  286.              return;
  287.          }
  288.  
  289.  
  290.  
  291.         query.prepare("SELECT HASLO FROM KONTO WHERE LOGIN = :LOGIN");
  292.         query.bindValue(":LOGIN",login);
  293.  
  294.         if(!query.exec())
  295.         {
  296.             set_status_label("Zapytanie jest niepoprawne",true);
  297.             return;
  298.         }
  299.        query.next();
  300.        user_passowrd=query.value(0).toString();
  301.  
  302.        if(user_passowrd!=password)
  303.        {
  304.            set_status_label("Podane hasło jest nieprawidłowe",true);
  305.            return;
  306.        }
  307.  
  308.  
  309.        QSqlQuery delete_account;
  310.       delete_account.prepare("DELETE FROM KONTO WHERE LOGIN = :LOGIN AND HASLO = :HASLO");
  311.       delete_account.bindValue(":HASLO", password);
  312.       delete_account.bindValue(":LOGIN",login);
  313.  
  314.        if(!delete_account.exec())
  315.        {
  316.            set_status_label("Nie udalo sie przeprowadzic aktualizacji w bazie danych",true);
  317.            return;
  318.        }
  319.  
  320.        else {
  321.  
  322.            set_status_label("Usunieto konto: " +login,false);
  323.            return;
  324.        }
  325. }
  326.  
  327.  
  328. void MainWindow::finder_recived()
  329. {
  330.     int operation=content_finder->get_current_item();
  331.     QString value=content_finder->get_line_value();
  332.     if(value.isEmpty())
  333.     {
  334.         set_status_label("Nazwa wyszukiwanego elementu jest pusta!",true);
  335.         return;
  336.     }
  337.  
  338.     if(operation==0)
  339.     {
  340.         qDebug() << "Operacja 0: Znajdz konto po numerze id";
  341.  
  342.     }
  343.  
  344.     if(operation==1)
  345.     {
  346.         qDebug() << "Operacja 1: Znajdz konto po nazwie gracza";
  347.  
  348.     }
  349.  
  350.      if(operation==2)
  351.      {
  352.          qDebug() << "Operacja 2: Znajadz gracza posiadajacego przedmiot";
  353.  
  354.      }
  355.  
  356.     if(operation==3)
  357.     {
  358.         qDebug() << "Operacja 3: Znajdz graczy o podanym poziomie uprawnienia";
  359.  
  360.     }
  361.  
  362.     if(operation==4)
  363.     {
  364.         qDebug() << "Operacja 4: Znajdz postacie na podanym koncie";
  365.  
  366.     }
  367.  
  368.  
  369.  
  370. }
  371.  
  372.  
  373.  
  374.  
  375. void MainWindow::set_status_label(QString tresc, bool is_error)
  376. {
  377.     ui->status_label->setText("Status: " +tresc);
  378.  
  379.     if(is_error==true)
  380.     {
  381.         ui->status_label->setStyleSheet("QLabel { color : red; }");
  382.     }
  383.  
  384.     else
  385.     {
  386.         ui->status_label->setStyleSheet("QLabel { color : green; }");
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement