//mainwindow.cpp
#include "mainwindow.h"
#include "logindlg.h"
#include "scriptdlg.h"
#include <QMessageBox>
#include <QIcon>
#include <QMdiSubWindow>
#include <QStandardItemModel>
#include <QTableView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/*设置窗口标题*/
this->setWindowTitle(tr("CDMA无线基站管理系统"));
/*设置窗口光标(窗口左上角图片)*/
this->setWindowIcon(QIcon("main.png"));
/*QMdiArea控件只能在MainWindow控件中使用,是用来实现多文档界面的必备控件*/
mdiarea1=new QMdiArea();
/*设置QMdiArea控件的背景图片*/
mdiarea1->setBackground(Qt::NoBrush);
/*背景图片设置为11.jpg,子窗口大小可以调整*/
mdiarea1->setStyleSheet("background-image:url(10.jpg)");
/*当子窗口的范围超过父窗口的显示范围时,设置自动具有横向滚动条*/
mdiarea1->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
/*设置自动具有纵向滚动条*/
mdiarea1->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
this->setCentralWidget(mdiarea1);
/*初始化基本控件*/
initmenubar();
/*初始化数据库连接*/
sqlhelper=new Vmysql();
}
//初始化控件
void MainWindow::initmenubar()
{
/*管理菜单*/
menu1=this->menuBar()->addMenu(tr("管理"));
login_in=new QAction(tr("登录"),this);
login_in->setShortcut(tr("Ctrl+U"));
connect(login_in,SIGNAL(triggered()),this,SLOT(login_inclick()));
login_out=new QAction(tr("注销"),this);
login_out->setShortcut(tr("Ctrl+B"));
connect(login_out,SIGNAL(triggered()),this,SLOT(login_outclick()));
quit1=new QAction(tr("退出"),this);
quit1->setShortcut(tr("Ctrl+W"));
connect(quit1,SIGNAL(triggered()),this,SLOT(quit_click()));
menu1->addAction(login_in);
menu1->addAction(login_out);
/*加入空格*/
menu1->addSeparator();
menu1->addAction(quit1);
/*数据菜单*/
menu2=this->menuBar()->addMenu(tr("数据"));
addstation=new QAction(tr("添加基站"),this);
addstation->setShortcut(tr("Ctrl+D"));\
//addstation->setEnabled(false);
connect(addstation,SIGNAL(triggered()),this,SLOT(add_station()));
addstationcontrl=new QAction(tr("添加基站控制器"),this);
addstationcontrl->setShortcut(tr("Ctrl+E"));
connect(addstationcontrl,SIGNAL(triggered()),this,SLOT(add_stationcontrl()));
addMSC=new QAction(tr("添加MSC"),this);
addMSC->setShortcut(tr("Ctrl+F"));
connect(addMSC,SIGNAL(triggered()),this,SLOT(add_MSC()));
delstation=new QAction(tr("删除基站"),this);
delstation->setShortcut(tr("Ctrl+G"));
connect(delstation,SIGNAL(triggered()),this,SLOT(del_station()));
delstationcontrl=new QAction(tr("删除基站控制器"),this);
delstationcontrl->setShortcut(tr("Ctrl+H"));
connect(delstationcontrl,SIGNAL(triggered()),this,SLOT(del_stationcontrl()));
delMSC=new QAction(tr("删除MSC"),this);
delMSC->setShortcut(tr("Ctrl+I"));
connect(delMSC,SIGNAL(triggered()),this,SLOT(del_MSC()));
sestation=new QAction(tr("查询基站"),this);
sestation->setShortcut(tr("Ctrl+J"));
connect(sestation,SIGNAL(triggered()),this,SLOT(sel_station()));
sestationcontrl=new QAction(tr("查询基站控制器"),this);
sestationcontrl->setShortcut(tr("Ctrl+K"));
connect(sestationcontrl,SIGNAL(triggered()),this,SLOT(sel_stationcontrl()));
seMSC=new QAction(tr("查询MSC"),this);
seMSC->setShortcut(tr("Ctrl+L"));
connect(seMSC,SIGNAL(triggered()),this,SLOT(sel_MSC()));
upstation=new QAction(tr("修改基站"),this);
upstation->setShortcut(tr("Ctrl+M"));
connect(upstation,SIGNAL(triggered()),this,SLOT(update_station()));
upstationcontrl=new QAction(tr("修改基站控制器"),this);
upstationcontrl->setShortcut(tr("Ctrl+N"));
connect(upstationcontrl,SIGNAL(triggered()),this,SLOT(update_stationcontrl()));
upMSC=new QAction(tr("修改MSC"),this);
upMSC->setShortcut(tr("Ctrl+O"));
connect(upMSC,SIGNAL(triggered()),this,SLOT(update_MSC()));
exscript=new QAction(tr("执行脚本"),this);
exscript->setShortcut(tr("Ctrl+P"));
connect(exscript,SIGNAL(triggered()),this,SLOT(exec_script()));
menu2->addAction(addstation);
menu2->addAction(addstationcontrl);
menu2->addAction(addMSC);
menu2->addAction(delstation);
menu2->addAction(delstationcontrl);
menu2->addAction(delMSC);
menu2->addAction(sestation);
menu2->addAction(sestationcontrl);
menu2->addAction(seMSC);
menu2->addAction(upstation);
menu2->addAction(upstationcontrl);
menu2->addAction(upMSC);
menu2->addAction(exscript);
/*窗口菜单*/
menu3=this->menuBar()->addMenu(tr("窗口"));
cascade1=new QAction(tr("层叠窗口"),this);
cascade1->setShortcut(tr("Ctrl+Q"));
connect(cascade1,SIGNAL(triggered()),this,SLOT(cascade_window()));
parallel=new QAction(tr("并列窗口"),this);
parallel->setShortcut(tr("Ctrl+R"));
connect(parallel,SIGNAL(triggered()),this,SLOT(parallel_window()));
menu3->addAction(cascade1);
menu3->addAction(parallel);
/*帮助菜单*/
menu4=this->menuBar()->addMenu(tr("帮助"));
helper=new QAction(tr("帮助"),this);
helper->setShortcut(tr("Ctrl+s"));
connect(helper,SIGNAL(triggered()),this,SLOT(helper_click()));
mabout=new QAction(tr("关于"),this);
mabout->setShortcut(tr("Ctrl+T"));
connect(mabout,SIGNAL(triggered()),this,SLOT(mabout_click()));
menu4->addAction(helper);
menu4->addSeparator();
menu4->addAction(mabout);
/*未登录,禁用所有按钮*/
close_action();
}
/*登录*/
void MainWindow::login_inclick()
{
loginDlg logd1;
/*exec()方法会产生一个模式对话框,show()方法只会产生普通的对话框*/
logd1.exec();
/*
* 当模式对话框打开时,主窗口是不能接受输入输出的;
* 当非模式对话框打开时, 主窗口可以接受输入输出
* ----------------------------------
* 模式对话框与非模式对话框的重大区别:
* exec()函数会阻塞当前进程,
* 但是show()不会阻塞当前进程,会使login_inclick()函数直接执行完毕,
* logd1是一个局部变量,login_inclick()函数执行完毕,会导致logd1的内存被释放,
* 从而导致界面上对话框也随着消失,所以用show()看不到结果,
* 但是可以通过将logd1变量定义在堆内存上,避免这种情况,
* 但是这也会导致用户可以打开多个登陆对话框*/
//logd1.show();
if(!logd1.islogin)
{
/*判断用户是否点击了取消按钮*/
return;
}
if(logd1.username.isEmpty())
{
QMessageBox::critical(this,"错误信息","用户名不可以为空!");
return;
}
if(logd1.userpass.isEmpty())
{
QMessageBox::critical(this,"错误信息","密码不可以为空!");
return;
}
if(logd1.dbname.isEmpty())
{
QMessageBox::critical(this,"错误信息","数据库名称不可以为空!");
return;
}
if(logd1.ipaddr.isEmpty())
{
QMessageBox::critical(this,"错误信息","IP地址不可以为空!");
return;
}
/*连接数据库*/
int res=sqlhelper->sql_connect(logd1.ipaddr.toStdString().data()
,logd1.username.toStdString().data()
,logd1.userpass.toStdString().data()
,logd1.dbname.toStdString().data());
if(res<0)
{
QMessageBox::critical(this,"错误信息",sqlhelper->ger_error());
}else
{
QMessageBox::information(this,"提示信息","登录成功");
/*开启所有SQL按钮*/
open_action();
}
}
//注销
void MainWindow::login_outclick()
{
if(QMessageBox::question(this,"确认退出","您确定要注销吗?",QMessageBox::Ok|QMessageBox::No,QMessageBox::Ok)==QMessageBox::Yes)
{
//断开数据库连接
sqlhelper->sql_disconnect();
/*禁用所有按钮*/
close_action();
}
}
//退出
void MainWindow::quit_click()
{
this->close();
}
/*添加基站*/
void MainWindow::add_station()
{
//
}
/*添加基站控制器*/
void MainWindow::add_stationcontrl()
{
//
}
/*添加MSC*/
void MainWindow::add_MSC()
{
//
}
/*删除基站*/
void MainWindow::del_station()
{
//
}
/*删除基站控制器*/
void MainWindow::del_stationcontrl()
{
//
}
/*删除MSC*/
void MainWindow::del_MSC()
{
//
}
/*查询基站*/
void MainWindow::sel_station()
{
//
}
/*查询基站控制器*/
void MainWindow::sel_stationcontrl()
{
//
}
/*查询MSC*/
void MainWindow::sel_MSC()
{
//
}
/*修改基站*/
void MainWindow::update_station()
{
//
}
/*修改基站控制器*/
void MainWindow::update_stationcontrl()
{
//
}
/*修改MSC*/
void MainWindow::update_MSC()
{
//
}
/*执行脚本*/
void MainWindow::exec_script()
{
ScriptDlg w1;
w1.exec();
if(!w1.isexec) return;
exec_query(w1.strsql.toStdString().data());
}
/*执行SQL查询*/
void MainWindow::exec_query(const char *strsql)
{
if(!judge_sqltype(strsql))
{
//执行SQL更新
if(sqlhelper->sql_exec(strsql)<0)
{
QMessageBox::critical(this,"错误提示",sqlhelper->ger_error());
}
}else
{
//执行SQL查询
QStandardItemModel *model=NULL;
if(sqlhelper->sql_query(strsql,&model)<0)
{
QMessageBox::critical(this,"错误提示",sqlhelper->ger_error());
return;
}
if(model==NULL)
{
QMessageBox::critical(this,"错误提示","model 为空!");
return;
}
/*创建view*/
display_view("查询结果",model);
}
}
/*判断SQL操作的类型*/
bool MainWindow::judge_sqltype(const char *strsql)
{
bool flag=true;
//1.判断SQL执行的是查询还是更新
char tempbuf[100]={0};
/*遍历字符数据,将第一个空格前的所有字符转化成大写*/
for(int i=0;i<(int)strlen(strsql);i++)
{
if(strsql[i]!=' ')
{
if(strsql[i]>96&&strsql[i]<123)
{
//小写转化成大写
tempbuf[i]=strsql[i]-32;
}else
{
tempbuf[i]=strsql[i];
}
}else
{
break;
}
}
if(strncmp(tempbuf,"SET",3)==0)
{
flag=false;
}else if(strncmp(tempbuf,"INSERT",6)==0)
{
flag=false;
}else if(strncmp(tempbuf,"DELETE",6)==0)
{
flag=false;
}else if(strncmp(tempbuf,"UPDATE",6)==0)
{
flag=false;
}else if(strncmp(tempbuf,"USE",3)==0)
{
flag=false;
}
return flag;
}
/*创建显示view*/
void MainWindow::display_view(const char *actionname,QStandardItemModel *model)
{
/*在QT中,model中存放数据,view中显示数据*/
/*定义view,QTableView继承于QWidget,QTableView在被销毁的同时,会自动销毁对应的model*/
QTableView *view1=new QTableView();
/*代表关闭这个widget的时候,自动将这个widget delete*/
view1->setAttribute(Qt::WA_DeleteOnClose);
/*多文档控件QMdiArea通过addSubWindow()将一个widget变成一个子窗口*/
mdiarea1->addSubWindow(view1);
view1->setWindowTitle(actionname);
/*设置背景图片*/
view1->setStyleSheet("background-image:url(11.jpg)");
/*view1继承于QWidget,如果没有model,那么view不会显示数据*/
view1->setModel(model);
view1->show();
/*必须在show()方法之后使用activeSubWindow()获取在最前面的子窗口*/
/*多文档中的子窗口widget设置大小,用resize()方法没有效果*/
//w1->resize(300,200);
/*QMdiArea控件通过activeSubWindow()方法得到当前活动窗口(最前边的子窗口)*/
/*注意引用头文件QMdiSubWindow */
mdiarea1->activeSubWindow()->resize(this->width()-100,this->height()-100);
}
/*层叠窗口*/
void MainWindow::cascade_window()
{
mdiarea1->cascadeSubWindows();
}
/*并列窗口*/
void MainWindow::parallel_window()
{
mdiarea1->tileSubWindows();
}
/*帮助*/
void MainWindow::helper_click()
{
QMessageBox::information(this,"帮助信息","暂无帮助信息");
}
/*关于*/
void MainWindow::mabout_click()
{
QMessageBox::about(this,"关于","版权所有,侵权必究!");
}
/*退出程序事件*/
void MainWindow::closeEvent(QCloseEvent *event)
{
if(QMessageBox::question(this,"退出程序",tr("您确定要退出吗?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No)==QMessageBox::Yes)
{
event->accept();
}else{
event->ignore();
}
}
/*未登录设置所有SQL按钮不可用*/
void MainWindow::close_action()
{
addstation->setEnabled(false);
addstationcontrl->setEnabled(false);
addMSC->setEnabled(false);
delstation->setEnabled(false);
delstationcontrl->setEnabled(false);
delMSC->setEnabled(false);
sestation->setEnabled(false);
sestationcontrl->setEnabled(false);
seMSC->setEnabled(false);
upstation->setEnabled(false);
upstationcontrl->setEnabled(false);
upMSC->setEnabled(false);
exscript->setEnabled(false);
cascade1->setEnabled(false);
parallel->setEnabled(false);
}
/*设置所有SQL按钮可用*/
void MainWindow::open_action()
{
addstation->setEnabled(true);
addstationcontrl->setEnabled(true);
addMSC->setEnabled(true);
delstation->setEnabled(true);
delstationcontrl->setEnabled(true);
delMSC->setEnabled(true);
sestation->setEnabled(true);
sestationcontrl->setEnabled(true);
seMSC->setEnabled(true);
upstation->setEnabled(true);
upstationcontrl->setEnabled(true);
upMSC->setEnabled(true);
exscript->setEnabled(true);
cascade1->setEnabled(true);
parallel->setEnabled(true);
}
MainWindow::~MainWindow()
{
/*释放数据库连接*/
delete sqlhelper;
}