#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editdialog.h"
#include "finddialog.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->splitter->setStretchFactor(1, 2);
ui->openButton->setEnabled(false);
ui->editButton->setEnabled(false);
ui->createButton->setEnabled(false);
ui->removeButton->setEnabled(false);
ui->findButton->setEnabled(false);
ui->resetButton->setEnabled(false);
ui->DSNEdit->setText(tr("DEFAULT"));
connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(dbConnect()));
connect(ui->openButton, SIGNAL(clicked()), this, SLOT(dbOpen()));
connect(ui->findButton, SIGNAL(clicked()), this, SLOT(dbFind()));
connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(dbReset()));
connect(ui->createButton, SIGNAL(clicked()), this, SLOT(dbCreate()));
connect(ui->editButton, SIGNAL(clicked()), this, SLOT(dbEdit()));
connect(ui->removeButton, SIGNAL(clicked()), this, SLOT(dbRemove()));
}
void MainWindow::dbConnect()
{
db = QSqlDatabase::addDatabase("QODBC3");
db.setDatabaseName(ui->DSNEdit->text());
if(!db.open())
QMessageBox::warning(this, tr("Ошибка соединения с базой данных"),
db.lastError().text());
else
{
QMessageBox::information(this, tr("Статус соединения"),
tr("Соединение установлено"));
}
ui->openButton->setEnabled(true);
ui->createButton->setEnabled(true);
ui->removeButton->setEnabled(true);
}
void MainWindow::dbOpen()
{
model = new QSqlTableModel(this, db);
model->setTable("Books");
model->setSort(0, Qt::AscendingOrder);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
if(!model->select())
{
QMessageBox::warning(this, tr("Ошибка. Невозможно открыть таблицу"),
model->lastError().text());
return;
}
ui->dbTable->setModel(model);
ui->dbTable->setEditTriggers(QTableView::NoEditTriggers);
ui->dbTable->hideColumn(table_cover);
ui->dbTable->resizeColumnsToContents();
ui->dbTable->setCurrentIndex(model->index(0, 0));
ui->editButton->setEnabled(true);
ui->findButton->setEnabled(true);
ui->resetButton->setEnabled(true);
}
void MainWindow::dbCreate()
{
if(db.tables().contains("Books") )
{
QMessageBox::warning(this, tr("Ошибка создания"),
tr("Таблица уже создана. Отменено"));
return;
}
QSqlQuery query;
QString table_statement = "CREATE table books ("
"id NUMBER NOT NULL,"
"name VARCHAR(30) NOT NULL PRIMARY KEY AUTOINCREMENT,"
"auth VARCHAR(30) NOT NULL,"
"dill VARCHAR(30) NOT NULL,"
"date DATE NOT NULL,"
"howm VARCHAR(5) NOT NULL,"
"howc VARCHAR(5) NOT NULL,"
"cover BLOB ";
query.exec(table_statement);
QMessageBox::information(this, tr("Статус создания"),
tr("Таблица успешно создана"));
ui->createButton->setEnabled(false);
ui->removeButton->setEnabled(true);
}
void MainWindow::dbEdit()
{
if(db.isOpen() && ui->dbTable->model() != NULL)
{
QModelIndex id = ui->dbTable->currentIndex();
EditDialog *EditForm = new EditDialog(model, id.row(), this);
EditForm->exec();
ui->dbTable->resizeColumnsToContents();
delete EditForm;
}
else
{
QMessageBox::warning(this, tr("Ошибка редактирования"),
tr("Таблица не выбрана"));
}
}
void MainWindow::dbRemove()
{
if(!db.tables().contains("books"))
{
QMessageBox::warning(this, tr("Ошибка удаления"),
tr("Таблицы не существует. Отменено"));
return;
}
QSqlQuery query;
query.exec("DROP table BOOKS");
QMessageBox::information(this, tr("Статус удаления"),
tr("Таблица успешно удалена"));
ui->createButton->setEnabled(true);
ui->removeButton->setEnabled(false);
}
void MainWindow::dbFind()
{
if(db.isOpen() && ui->dbTable->model() != NULL)
{
FindDialog dialog(model, this);
dialog.exec();
}
else
{
QMessageBox::warning(this, tr("Ошибка поиска"),
tr("Таблица не выбрана"));
}
}
void MainWindow::dbReset()
{
model->setFilter("");
model->select();
}
MainWindow::~MainWindow()
{
delete ui;
}