#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
SETICON;
ui->uaer_data_show->setStyleSheet("QTableWidget{background-color:transparent}");
maketable(); //创建表
createActions(); //创建菜单
/***********绘制图形***************///ui->widget //this
painterin = new QPainter(ui->widget);
painterout = new QPainter(ui->widget);
painter1 = new QPainter(ui->widget);
painter2 = new QPainter(ui->widget);
/**********************************/
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(makeday())); //默认使用绘制当天消费的统计图,当用户显示当月,当年,总计的统计.5,秒后恢复到当天
timer->start(5000);
timeid = new QTimer(this);
connect(timeid,SIGNAL(timeout()),this,SLOT(timedate())); //获取本地时间
timeid->start(100);
lable = new QLabel(this);
ui->statusbar->addWidget(lable);
lablename = new QLabel(this);
ui->statusbar->addWidget(lablename); //向statusbar添加label控件
connect(ui->uaer_data_show->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(onHeaderCliked(int))); //表头排序连接
paixu =true; //表头金额升序降序排列标志位
timer1 = new QTimer(this);//移动dimer
connect(timer1,SIGNAL(timeout()),this,SLOT(movepaintertrue())); //槽函数
they = 250; //起始位置
colortip = false; //红色标记标志位 默认为不标记红色支出
//初始化查询时间
ui->time1->setDateTime(QDateTime::currentDateTime());
ui->time2->setDateTime(QDateTime::currentDateTime());
ui->cancle->setEnabled(false);
ui->moneyedit1->setText("0.0");
ui->moneyedit2->setText("0.0");
ui->allmonetin->setText("0.0");
ui->allmoneyout->setText("0.0");
ui->radioButton3->setChecked(true);
userchangeinforname = "";
ui->uaer_data_show->setContextMenuPolicy(Qt::CustomContextMenu); //设置鼠标右键触发
}
//为了调用移动函数
void MainWindow::movepaintergo()
{
QTimer::singleShot(10,this,SLOT(movepainter())); //停止10毫秒后开始移动绘图
}
void MainWindow::movepainter() //再一次调用移动函数
{
timer1->start(25);
}
void MainWindow::movepaintertrue() //这尼玛才是真的移动函数
{
ui->widget->move(750,they);
they = they -10;
if(they == -20)
{
timer1->stop();
}
makeday(); //这个其实是没有什么用的,只是为了消除下面的小红点
}
// 表头排序
void MainWindow::onHeaderCliked(int x)
{
if(x == 1)
{
Sequence_time(); //单击表头按时间排序
}
else if(x ==2)
{
Sequence_type(); //单击表头按收支类型排序
}
else if(x == 4) //单击表头按金额排序
{
if(paixu)
{
moneyupto(); //金额升序排序
paixu = false;
}
else
{
moneydownto(); //金额降序排列
paixu = true;
}
}
else if(x == 6)
{
on_uaer_data_show_cellClicked(5, 2); //解决单行数据修改无法保存问题//哈哈,只需要重新调用,写入假的坐标就好//妈妈再也不用担心了
}
}
//更新时间函数
void MainWindow::timedate()
{
QDateTime dt;
QTime time;
QDate date;
dt.setTime(time.currentTime());
dt.setDate(date.currentDate());
QString currentDate = dt.toString("当前时间:yyyy年MM月dd日hh时mm分 ");
QFont font;
font.setBold(true);
font.setPointSize(11);
lable->setFont(font);
lable->setText(currentDate);
}
//获取用户名
void MainWindow::getdataname(QString name) //在statusbar显示信息
{
userchangeinforname = name;
userdataname = "User_Data_";
userdataname += name;
userdataname +=".db";
insertdata(); //调用数据库插入函数
QString username = "当前登陆用户: ";
username += name;
username += " 数据库:";
username += userdataname;
lablename->setText(username);
QFont font;
font.setBold(true);
font.setPointSize(11);
lablename->setFont(font);
}
//创建表
void MainWindow::maketable()
{
ui->uaer_data_show->setSelectionBehavior(QAbstractItemView::SelectRows); //选中整行
ui->uaer_data_show->verticalHeader()->hide();//隐藏行号
ui->uaer_data_show->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
ui->uaer_data_show->setColumnCount(7); //设置显示7列
QStringList header;
header << tr("记录号")<< tr("时间")<< tr("收支类型")<< tr("事物")<< tr("金额")<< tr("支付方式")<< tr("备注");
ui->uaer_data_show->setHorizontalHeaderLabels(header); //设计表头
QFont font;
font.setBold(false); //设置粗体
font.setPointSize(9);
ui->uaer_data_show->horizontalHeader()->setFont(font); //这是表头为粗体
ui->uaer_data_show->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //列自动分配
ui->uaer_data_show->setFrameShape(QFrame::NoFrame); //设置不现实边框
ui->uaer_data_show->setShowGrid(false); //设置不显示表格
ui->uaer_data_show->horizontalHeader()->setHighlightSections(false);//防止单行塌陷
}
//插入信息
void MainWindow::insertdata()
{
userdata = dbopen.initDatabase(userdataname);
QSqlQuery query(userdata);
query.exec("select * from UserData");
while (query.next())
{
int row_cout = ui->uaer_data_show->rowCount();
//ui->uaer_data_show->setBackgroundRole(QColor(255,0,0));
ui->uaer_data_show->insertRow(row_cout);
ui->uaer_data_show->setItem(row_cout,0,new QTableWidgetItem(query.value(0).toString()));
ui->uaer_data_show->setItem(row_cout,1,new QTableWidgetItem(query.value(1).toString()));
ui->uaer_data_show->setItem(row_cout,2,new QTableWidgetItem(query.value(2).toString()));
ui->uaer_data_show->setItem(row_cout,3,new QTableWidgetItem(query.value(3).toString()));
ui->uaer_data_show->setItem(row_cout,4,new QTableWidgetItem(query.value(4).toString()));
ui->uaer_data_show->setItem(row_cout,5,new QTableWidgetItem(query.value(5).toString()));
ui->uaer_data_show->setItem(row_cout,6,new QTableWidgetItem(query.value(6).toString()));
}
if((ui->uaer_data_show->rowCount())==0) //判断是否有数据存在,如果没有,则将部分按钮设置为不可用
{
ui->action->setEnabled(false);
ui->action_delete->setEnabled(false);
ui->action_out->setEnabled(false);
ui->action_7->setEnabled(false);
ui->action_8->setEnabled(false);
ui->action_9->setEnabled(false);
ui->action_10->setEnabled(false);
}
if(ui->uaer_data_show->rowCount()<2) //注意这是的rowcount ,是+1的
{
ui->action_7->setEnabled(false);
ui->action_8->setEnabled(false);
ui->action_9->setEnabled(false);
ui->action_10->setEnabled(false);
}
ui->uaer_data_show->show();
dbopen.closedb();
settablewidgetcolor();
makeday();
if(ui->uaer_data_show->rowCount()>13)
{
int max =ui->uaer_data_show->rowCount()+2;
ui->uaer_data_show->verticalScrollBar()->setMaximum(max);
ui->uaer_data_show->verticalScrollBar()->setValue(max);
}
else
{
ui->uaer_data_show->verticalScrollBar()->setMaximum(13);
}
}
/*****************隐藏标题栏可以移动代码*********************/
void MainWindow::mousePressEvent(QMouseEvent *e)
{
last = e->globalPos();
}
void MainWindow::mouse