222 lines
7.5 KiB
C++
222 lines
7.5 KiB
C++
#include "dbBrowser.h"
|
|
#include "ui_dbBrowser.h"
|
|
#include "mainwindow.h"
|
|
#include "attributeView.h"
|
|
#include <QTabBar>
|
|
|
|
DatabaseBrowser::DatabaseBrowser(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::DatabaseBrowser)
|
|
, m_pMainWindow(nullptr)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseBrowser::onTabCloseRequested);
|
|
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &DatabaseBrowser::onCurrentTabChanged);
|
|
|
|
connect(ui->btnAdd, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_addRecord);
|
|
connect(ui->btnRemove, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_removeRecord);
|
|
connect(ui->btnSave, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_submitChanges);
|
|
connect(ui->btnCancle, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_cancleChanges);
|
|
connect(ui->btnRefresh, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_refreshData);
|
|
}
|
|
|
|
DatabaseBrowser::~DatabaseBrowser()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void DatabaseBrowser::setMainWindow(MainWindow* window)
|
|
{
|
|
m_pMainWindow = window;
|
|
}
|
|
|
|
int DatabaseBrowser::tabIndex(const QString& tabText)
|
|
{
|
|
for(int i = 0; i < ui->tabWidget->count(); i++)
|
|
{
|
|
if(ui->tabWidget->tabText(i) == tabText)
|
|
return i;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttributeGroup& attributeGroup)
|
|
{
|
|
QString tabText = attributeGroup.strModelName + "/" + attributeGroup.strGroupName;
|
|
int index = tabIndex(tabText);
|
|
if(index != -1)
|
|
{
|
|
ui->tabWidget->setCurrentIndex(index);
|
|
return;
|
|
}
|
|
|
|
AttributeView* view = new AttributeView(attributeGroup, ui->tabWidget, connection);
|
|
connect(view->model(), &AttributeTableModel::showMessage, this, &DatabaseBrowser::onShowMessage);
|
|
connect(view->model(), &AttributeTableModel::syncDataStatus, this, &DatabaseBrowser::onSyncDataStatus);
|
|
connect(view->delegate(), &AttributeTableDelegate::showMessage, this, &DatabaseBrowser::onShowMessage);
|
|
index = ui->tabWidget->addTab(view, QIcon(":/img/images/icon_hierarchy.png"), tabText);
|
|
//添加自定义按钮
|
|
/*QPushButton* closeBtn = new QPushButton("");
|
|
closeBtn->setProperty("index", index);
|
|
connect(closeBtn, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClick_tabCloseBtn);
|
|
closeBtn->setFixedSize(12, 12);
|
|
closeBtn->setStyleSheet("QPushButton\n"
|
|
"{\n"
|
|
" border-image: url(:/img/images/btn_close_default.png);\n"
|
|
"}\n"
|
|
"QPushButton:hover\n"
|
|
"{\n"
|
|
" border-image: url(:/img/images/btn_close_hover.png);\n"
|
|
"}\n"
|
|
"QPushButton:pressed\n"
|
|
"{\n"
|
|
" border-image: url(:/img/images/btn_close_pressed.png);\n"
|
|
"}");
|
|
QTabBar* tabBar = ui->tabWidget->tabBar();
|
|
tabBar->setTabButton(index, QTabBar::RightSide, closeBtn);*/
|
|
|
|
ui->tabWidget->setCurrentIndex(index);
|
|
}
|
|
|
|
void DatabaseBrowser::onTabCloseRequested(int index)
|
|
{
|
|
/*QObject* sender = QObject::sender();
|
|
QPushButton* button = qobject_cast<QPushButton*>(sender);
|
|
if(!button)
|
|
return;
|
|
|
|
int index = button->property("index").toInt();*/
|
|
QWidget* widget = ui->tabWidget->widget(index);
|
|
ui->tabWidget->removeTab(index);
|
|
delete widget;
|
|
}
|
|
|
|
void DatabaseBrowser::onCurrentTabChanged(int index)
|
|
{
|
|
if(index == -1) //最后一个tab关闭时会触发
|
|
{
|
|
ui->btnAdd->setEnabled(false);
|
|
ui->btnRemove->setEnabled(false);
|
|
ui->btnCancle->setEnabled(false);
|
|
ui->btnRefresh->setEnabled(false);
|
|
ui->btnSave->setEnabled(false);
|
|
ui->btnCancle->setEnabled(false);
|
|
ui->recordInfo->clear();
|
|
ui->lineEdit->setEnabled(false);
|
|
ui->lineEdit->setText("1");
|
|
return;
|
|
}
|
|
|
|
QWidget* widget = ui->tabWidget->widget(index);
|
|
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
|
|
if(attributeView)
|
|
{
|
|
ui->btnAdd->setEnabled(true);
|
|
ui->btnRemove->setEnabled(true);
|
|
ui->btnCancle->setEnabled(true);
|
|
ui->btnRefresh->setEnabled(true);
|
|
attributeView->active();
|
|
}
|
|
}
|
|
|
|
void DatabaseBrowser::onBtnClicked_addRecord()
|
|
{
|
|
QWidget* widget = ui->tabWidget->currentWidget();
|
|
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
|
|
if(attributeView)
|
|
{
|
|
QTableView* view = attributeView->view();
|
|
AttributeTableModel* model = attributeView->model();
|
|
if(view && model)
|
|
{
|
|
int row = model->rowCount(); //当前插入都是在最后一行
|
|
/*QModelIndex currentIndex = view->currentIndex();
|
|
if(currentIndex.row() != -1)
|
|
row = currentIndex.row();*/
|
|
model->insertRecord(row);
|
|
ui->btnSave->setEnabled(true);
|
|
ui->btnCancle->setEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DatabaseBrowser::onBtnClicked_removeRecord()
|
|
{
|
|
QWidget* widget = ui->tabWidget->currentWidget();
|
|
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
|
|
if(attributeView)
|
|
{
|
|
QTableView* view = attributeView->view();
|
|
AttributeTableModel* model = attributeView->model();
|
|
if(view && model)
|
|
{
|
|
QModelIndex currentIndex = view->currentIndex();
|
|
int row = currentIndex.row();
|
|
if( row != -1)
|
|
{
|
|
model->removeRecord(row);
|
|
ui->btnSave->setEnabled(true);
|
|
ui->btnCancle->setEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void DatabaseBrowser::onBtnClicked_submitChanges()
|
|
{
|
|
QWidget* widget = ui->tabWidget->currentWidget();
|
|
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
|
|
if(attributeView)
|
|
{
|
|
QTableView* view = attributeView->view();
|
|
AttributeTableModel* model = attributeView->model();
|
|
if(view && model)
|
|
model->submitChanges();
|
|
}
|
|
}
|
|
|
|
void DatabaseBrowser::onBtnClicked_cancleChanges()
|
|
{
|
|
QWidget* widget = ui->tabWidget->currentWidget();
|
|
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
|
|
if(attributeView)
|
|
{
|
|
QTableView* view = attributeView->view();
|
|
AttributeTableModel* model = attributeView->model();
|
|
if(view && model)
|
|
model->cancleChanges();
|
|
}
|
|
}
|
|
|
|
void DatabaseBrowser::onBtnClicked_refreshData()
|
|
{
|
|
QWidget* widget = ui->tabWidget->currentWidget();
|
|
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
|
|
if(attributeView)
|
|
{
|
|
QTableView* view = attributeView->view();
|
|
AttributeTableModel* model = attributeView->model();
|
|
if(view && model)
|
|
model->refresh();
|
|
}
|
|
}
|
|
|
|
void DatabaseBrowser::onSyncDataStatus(bool hasModifiedData, const PaginationInfo& paginationInfo)
|
|
{
|
|
ui->btnSave->setEnabled(!hasModifiedData);
|
|
ui->btnCancle->setEnabled(!hasModifiedData);
|
|
|
|
QString recordInfo = QString::fromWCharArray(L"共 %1 条记录").arg(paginationInfo.totalEntries);
|
|
ui->recordInfo->setText(recordInfo);
|
|
ui->lineEdit->setText(QString::number(paginationInfo.currentPage));
|
|
ui->lineEdit->setEnabled(true);
|
|
}
|
|
|
|
void DatabaseBrowser::onShowMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
|
|
{
|
|
if(m_pMainWindow)
|
|
m_pMainWindow->showMessageDialog(type, strTitle, strContent);
|
|
}
|