1  #include "mainwindow.h"
2  #include "ui_mainwindow.h"

3  MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
4  {
5     ui->setupUi(this);
     /* The first step is to create an object for
     * the database and initialize the database connection
     * */
6     db = new DataBase();
7     db->connectToDataBase();
      
    /* After that produce content database tables
     * this content will displayed in the tableView and tableViewDevice
     * */
8    for(int i = 1; i < 4; i++){
9         QVariantList data;
10        data.append("Device " + QString::number(i));
11        data.append("192.168.0." + QString::number(i));
12        db->inserIntoDeviceTable(data);
13    }

14    for(int i = 0; i < 10; i++){
15        QVariantList data;
16        QString random = QString::number(qrand() % ((4 + 1) - 1) + 1);
17        data.append(QDate::currentDate());
18        data.append(QTime::currentTime());
19        data.append(random);
20        data.append(random);
21        db->inserIntoMainTable(data);
22    }

    /* Initialize the model to represent the data
     * indicating the names of the columns
     * */
23    this->setupMainModel(TABLE, QStringList() << trUtf8("id")
                                              << trUtf8("Date")
                                              << trUtf8("Time")
                                              << trUtf8("Host Name")
                                              << trUtf8("IP Address")
               );

24    this->setupDeviceModel(DEVICE, QStringList() << trUtf8("id")
                                                 << trUtf8("Host Name")
                                                 << trUtf8("IP Address")
               );
    /* Initialize the appearance of a table with data
     * */
25    this->createUI();
26 }

27 MainWindow::~MainWindow()
28 {
29     delete ui;
30 }

31 void MainWindow::setupMainModel(const QString &tableName, const QStringList &headers)
{
    /* Initializes the data model representation with the installation name
     * in the database table, on which will be accessed in the table
     * */
32    modelMain = new QSqlRelationalTableModel(this);
33    modelMain->setTable(tableName);
    /* Set the connection device table, which will be made data substitution.
     * The method setRelation specified column number in which substitution is made,
     * as well as through QSqlRelation class name of the table,
     * the option for which the sample line and column
     * from which the data will be taken will be made
     * */
34    modelMain->setRelation(3, QSqlRelation(DEVICE, "id", DEVICE_HOSTNAME));
35    modelMain->setRelation(4, QSqlRelation(DEVICE, "id", DEVICE_IP));

    /* Set the columns names in a table with sorted data
     * */
36    for(int i = 0, j = 0; i < modelMain->columnCount(); i++, j++){
37        modelMain->setHeaderData(i, Qt::Horizontal,headers[j]);
38    }
    // Set Sort Ascending column zero data
39    modelMain->setSort(0,Qt::AscendingOrder);
40    modelMain->select();
41 }

42 void MainWindow::setupDeviceModel(const QString &tableName, const QStringList &headers)
43 {
    /* Initializes the data model representation
     * with the installation name in the database table,
     * on which will be accessed in the table
     * */
44    modelDevice = new QSqlRelationalTableModel(this);
45    modelDevice->setTable(tableName);

46    for(int i = 0, j = 0; i < modelDevice->columnCount(); i++, j++){
47        modelDevice->setHeaderData(i,Qt::Horizontal,headers[j]);
48    }

49    modelDevice->setSort(0,Qt::AscendingOrder);
50    modelDevice->select();
51 }

52 void MainWindow::createUI()
53 {
54    ui->tableView->setModel(modelMain);     // We set the model on the TableView
55    ui->tableView->setColumnHidden(0, true);    // Hide the column id Records
56    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
57    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
58    ui->tableView->resizeColumnsToContents();
59    ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
60    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
61    ui->tableView->horizontalHeader()->setStretchLastSection(true);
62    modelMain->select(); // Fetches the data from the table
63    ui->tableViewDevice->setModel(modelDevice);
64    ui->tableViewDevice->setColumnHidden(0, true);
65    ui->tableViewDevice->setSelectionBehavior(QAbstractItemView::SelectRows);
66    ui->tableViewDevice->setSelectionMode(QAbstractItemView::SingleSelection);
67    ui->tableViewDevice->resizeColumnsToContents();
68    ui->tableViewDevice->setItemDelegate(new QSqlRelationalDelegate(ui->tableViewDevice));
69    ui->tableViewDevice->setEditTriggers(QAbstractItemView::NoEditTriggers);
70    ui->tableViewDevice->horizontalHeader()->setStretchLastSection(true);
71    modelDevice->select();
72 }
