-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
2159 lines (1892 loc) · 68.3 KB
/
database.cpp
File metadata and controls
2159 lines (1892 loc) · 68.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "database.h"
#include <QWidget>
#include <QLineEdit>
#include <QLayout>
#include <QComboBox>
#include <QLabel> //包含标签类
#include <QFrame> //包含框架类
#include <QGroupBox>
#include <QPushButton> //包含按钮类
#include <QTabWidget>
#include <QTreeWidget>
#include <QPalette> //包含调色板类
#include <QBrush>
#include <QPixmap>
#include <QDateTime>
#include <QtSql> //包含sql
#include <QStringList>
#include <QPluginLoader>
#include <QMessageBox> //包含消息类
#include <QCheckBox>
#include <QTableWidget>
#include <QTimer> //包含计时器类
#include <QDir>
#include <QFileDialog> //包含文件窗口类
/* 构造函数 */
database::database(QObject *parent)
: QObject(parent)
{
//初始化变量
user_num = 0;
user_type = VISITOR;
current_user = "游客";
//新建计时器
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(updateTime()));
timer->start(1000);
//初始化指针
tablewidget = NULL;
tablewidget2 = NULL;
nextbutton = NULL;
lastbutton = NULL;
hboxLayout6 = NULL;
lastbutton = NULL;
nextbutton = NULL;
deleteUserButton = NULL;
grantLentButton = NULL;
lendBookButton = NULL;
returnBookButton = NULL;
window4 = NULL;
for(int i=0;i<10;i++){
item2[i] = NULL;
item3[i] = NULL;
}
//窗口布局
setLayout();
//连接数据库
createConnection();
}
/* 析构函数 */
database::~database()
{
//取消与数据库的连接
db.removeDatabase("book_manage");
}
/* _______________________
| |
| 第一部分:页面布局 |
| |
———————————————————————
*/
/* 初始化布局 */
void database::setLayout()
{
//显示标题栏
setWindowTitle();
//以游客身份新建搜索窗口
setWindowSearchBook(VISITOR);
}
/* 标题窗口布局*/
void database::setWindowTitle()
{
//申请内存
QPalette palette;
loginbutton = new QPushButton(tr("登录")); //创建登录按钮
loginbutton->setStyleSheet("color:rgb(0,0,255)"); //设置登录按钮样式
registerbutton = new QPushButton(tr("注册")); //创建注册按钮
registerbutton->setStyleSheet("color:rgb(0,0,255)"); //设置登录按钮样式
spacelabel2 = new QLabel(); //创建一个标签
spacelabel3 = new QLabel("\n\n");
vboxLayout0 = new QVBoxLayout();
hboxLayout2 = new QHBoxLayout();
hboxLayout3 = new QHBoxLayout();
titleImage = new QWidget(); //创建标题图片区域
window = new QWidget(); //创建总窗口window
vboxLayout3 = new QVBoxLayout();
mainTabWidget = new QTabWidget();
//登录注册按钮状态设置
registerbutton->setFlat(true);
registerbutton->setFixedSize(QSize(60,30)); //设置注册按钮大小
loginbutton->setFlat(true);
loginbutton->setFixedSize(QSize(60,30)); //设置登录按钮大小
//建立信号与槽
connect(registerbutton,SIGNAL(clicked()),this,SLOT(setWindowRegister()));
//点击注册按钮,显示注册窗口
connect(loginbutton,SIGNAL(clicked()),this,SLOT(setWindowLogin()));
//点击登录按钮,显示登录窗口
//设置标题图片
titleImage->setAutoFillBackground(true);
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/logo.png")));
titleImage->setPalette(palette);
titleImage->setLayout(hboxLayout2);
//标题栏布局
hboxLayout3->addWidget(spacelabel2);
hboxLayout3->addWidget(loginbutton);
hboxLayout3->addWidget(registerbutton);
vboxLayout3->addWidget(spacelabel3);
vboxLayout3->addLayout(hboxLayout3);
hboxLayout2->addStretch();
hboxLayout2->addLayout(vboxLayout3);
//将标题图片和标签窗口加入总布局
vboxLayout0->addWidget(titleImage);
vboxLayout0->addWidget(mainTabWidget);
//总窗口显示
window->setAutoFillBackground(true);
window->setLayout(vboxLayout0);
window->setWindowTitle(tr("西安工业大学北方信息工程学院图书借阅系统")); //设置总窗口标题
window->setFixedSize(1300,768); //设置总窗口大小
window->show();
}
/* 登录窗口布局 */
void database::setWindowLogin()
{
//登录时禁用登录和注册按钮
loginbutton->setDisabled(true);
registerbutton->setDisabled(true);
//新建用户和管理员登录窗口
user_loginWidget = createLoginWindow(1);
manager_loginWidget = createLoginWindow(2);
//加入两个登录窗口加入分栏布局,并显示
loginTabWidget = new QTabWidget();
loginTabWidget->setWindowTitle(tr("登录系统"));
loginTabWidget->setFixedSize(500,400); //设置登录窗口大小
loginTabWidget->addTab(user_loginWidget,"学生登录");
loginTabWidget->addTab(manager_loginWidget,"管理员登录");
//禁用关闭按钮
loginTabWidget->setWindowFlags(loginTabWidget->windowFlags()&~Qt::WindowCloseButtonHint);
loginTabWidget->show();
}
/* 搜索书籍界面布局 */
void database::setWindowSearchBook(int type)
{
//如果身份为学生,需要销毁之前的窗口新建
if(type==USER_LOGIN||type==USER_REGISTER){
delete window1;
}
//常量
const int num = 8;
const int columnNum = 3;
const int typenum =23;
//申请内存
window1 = new QWidget();
gridLayout = new QGridLayout();
vboxLayout1 = new QVBoxLayout();
vboxLayout = new QVBoxLayout();
hboxLayout1 = new QHBoxLayout();
hboxLayout = new QHBoxLayout();
groupbox = new QGroupBox();
groupbox1 = new QGroupBox();
clearbutton = new QPushButton(tr("清空"));
searchbutton = new QPushButton(tr("查询"));
tablewidget = new QTableWidget();
tree = new QTreeWidget();
root = new QTreeWidgetItem(QStringList()<<"所有类型");
QTreeWidgetItem *leaf[typenum];
//建立信号与槽
connect(searchbutton,SIGNAL(clicked()),this,SLOT(searchBook()));
//点击搜索按钮,进行搜索操作
connect(clearbutton,SIGNAL(clicked()),this,SLOT(clear()));
//点击清除按钮,进行清除操作
connect(tree,SIGNAL(itemClicked(QTreeWidgetItem *,int)),
this,SLOT(searchBookByType(QTreeWidgetItem *)));
//点击树状列表,进行查找书籍操作
//更新窗口信息
updateTitle(type);
//设置左边的树状图书分类栏
tree->setHeaderLabels(QStringList()<<"图书类型");
tree->addTopLevelItem(root);
tree->setFixedWidth(280);
tree->setColumnWidth(1,10);
QString str2[] = {
"马列主义毛邓思想","哲学","社会科学总论","政治法律","军事","经济","文化科学体育教育","语言文字",
"文学","艺术","历史地理","自然科学总论","数理科学与化学","天文学与地理科学","生物科学","医药卫生",
"工业技术","交通运输","航空航天","环境科学"
};
for(int i=0;i<20;i++){
leaf[i] = new QTreeWidgetItem(QStringList()<<str2[i]);
root->addChild(leaf[i]);
}
tree->expandAll();
//设置高级搜索栏
window = new QWidget();
nameLine = new QLineEdit();
publishLine = new QLineEdit();
authorLine = new QLineEdit();
dateBox = new QComboBox();
dateBox1 = new QComboBox();
priceLine = new QLineEdit();
priceLine1 = new QLineEdit();
isLent = new QComboBox();
label = new QLabel[num];
QString str[] = {"书名","出版社","作者","年份"," --","状态","价位"," --"};
for(int i=0;i<num;i++){
label[i].setText(str[i]);
gridLayout->addWidget(label+i,i/columnNum+1,2*(i%columnNum)+1);
}
dateBox->addItem(" ");
dateBox1->addItem(" ");
for(int i=0;i<60;i++){
dateBox->addItem(QString::number(2019-i)); //设置下拉栏数值
dateBox1->addItem(QString::number(2019-i));
}
isLent->addItem(" ");
isLent->addItem("已借出");
isLent->addItem("未借出");
//表格布局,放置搜索选项
gridLayout->addWidget(nameLine,1,2); //书名
gridLayout->addWidget(publishLine,1,4); //类型
gridLayout->addWidget(authorLine,1,6); //作者名称
gridLayout->addWidget(dateBox,2,2); //年份(起)
gridLayout->addWidget(dateBox1,2,4); //年份(终)
gridLayout->addWidget(isLent,2,6); //借阅状态
gridLayout->addWidget(priceLine,3,2); //价格(从)
gridLayout->addWidget(priceLine1,3,4); //价格(到)
//搜索选项加入群组中
groupbox->setLayout(gridLayout); //设置搜索框
groupbox->setFixedSize(600,140); //设置搜索框大小
//垂直布局,放置两个按钮
vboxLayout1->addStretch();
vboxLayout1->addWidget(searchbutton);
vboxLayout1->addWidget(clearbutton);
vboxLayout1->addStretch();
groupbox1->setLayout(vboxLayout1);
groupbox1->setFixedSize(200,140);
//水平布局,依次加入搜索选项和按钮的垂直布局
hboxLayout1->addWidget(groupbox);
hboxLayout1->setSpacing(30);
hboxLayout1->addWidget(groupbox1);
//垂直布局,依次加入搜索框和显示框
vboxLayout->addLayout(hboxLayout1);
vboxLayout->addWidget(tablewidget);
setWindowShowBook(type);
vboxLayout->addLayout(hboxLayout6);
//水平布局,加入左边栏和右窗口
hboxLayout->addWidget(tree);
hboxLayout->addLayout(vboxLayout);
//窗口1加入水平布局
window1->setLayout(hboxLayout);
mainTabWidget->addTab(window1,"图书查询");
//如果对象为学生,那么新建借书窗口
if(type==USER_LOGIN||type==USER_REGISTER){
setWindowLoanBook(); //显示借书窗口
}
}
/* 单册添加书籍界面布局 */
void database::setWindowAddBook()
{
//申请内存
QLabel *label3 = new QLabel;
label3->setText("(如果有多个作者,请用空格分开)");
addBookWidget = new QWidget(); //增加图书
deletebookWidget = new QWidget(); //删除图书
window3 = new QWidget();
vboxLayout4 = new QVBoxLayout();
vboxLayout5 = new QVBoxLayout();
vboxLayout6 = new QVBoxLayout();
vboxLayout9 = new QVBoxLayout();
hboxLayout4 = new QHBoxLayout();
hboxLayout5 = new QHBoxLayout();
hboxLayout9 = new QHBoxLayout();
deleteOkButton = new QPushButton("确定");
manageTabWidget = new QTabWidget();
gridLayout2 = new QGridLayout();
okbutton = new QPushButton(tr("确定"));
nameLine2 = new QLineEdit();
typeBox2 = new QComboBox();
priceLine2 = new QLineEdit();
authorLine2 = new QLineEdit();
numLine2 = new QLineEdit();
publishLine2 = new QLineEdit();
dateBox2 = new QComboBox();
idLine = new QLineEdit();
label2 = new QLabel[7];
label5 = new QLabel;
label5->setText("请输入待删除书目的编号:");
//建立信号与槽
connect(deleteOkButton,SIGNAL(clicked()),this,SLOT(deleteBook()));
//点击确认删除按钮,进行删除书籍操作
connect(okbutton,SIGNAL(clicked()),this,SLOT(oneAddBook()));
//点击确认按钮,进行添加书本操作
//设置选项框信息
QString str[7] = {"书名","价格","数量","出版社","出版日期",
"分类","作者"};
QString str2[20] = {
"马列主义毛邓思想","哲学","社会科学总论","政治法律","军事","经济","文化科学体育教育","语言文字",
"文学","艺术","历史地理","自然科学总论","数理科学与化学","天文学与地理科学","生物科学","医药卫生",
"工业技术","交通运输","航空航天","环境科学"
};
for(int i = 0;i < 7;i++){
label2[i].setText(str[i]);
}
for(int i=0;i<20;i++){
typeBox2->addItem(str2[i]);
}
for(int i=0;i<60;i++){
dateBox2->addItem(QString::number(2019-i));
}
//窗口布局
//设置选项框表格
gridLayout2->addWidget(label2,1,1);
gridLayout2->addWidget(label2+1,1,3);
gridLayout2->addWidget(label2+2,2,1);
gridLayout2->addWidget(label2+3,2,3);
gridLayout2->addWidget(label2+4,3,1);
gridLayout2->addWidget(label2+5,3,3);
gridLayout2->addWidget(label2+6,4,1);
gridLayout2->addWidget(nameLine2,1,2);
gridLayout2->addWidget(priceLine2,1,4);
gridLayout2->addWidget(numLine2,2,2);
gridLayout2->addWidget(publishLine2,2,4);
gridLayout2->addWidget(dateBox2,3,2);
gridLayout2->addWidget(typeBox2,3,4);
gridLayout2->addWidget(authorLine2,4,2);
gridLayout2->addWidget(label3,4,3,1,2);
//加入确认按钮
hboxLayout5->addStretch();
hboxLayout5->addWidget(okbutton);
hboxLayout5->addStretch();
vboxLayout6->addLayout(gridLayout2);
vboxLayout6->addLayout(hboxLayout5);
addBookWidget->setLayout(vboxLayout6);
hboxLayout9->addStretch();
hboxLayout9->addWidget(deleteOkButton);
hboxLayout9->addStretch();
vboxLayout9->addStretch();
vboxLayout9->addWidget(label5);
vboxLayout9->addWidget(idLine);
vboxLayout9->addLayout(hboxLayout9);
vboxLayout9->addStretch();
//添加分页窗口
deletebookWidget->setLayout(vboxLayout9);
manageTabWidget->addTab(addBookWidget,"添加图书"); //加入添加图书栏
manageTabWidget->addTab(deletebookWidget,"删除书籍"); //加入删除图书栏
vboxLayout5->addSpacing(100);
vboxLayout5->addWidget(manageTabWidget);
vboxLayout5->addSpacing(300);
hboxLayout4->addStretch();
hboxLayout4->addLayout(vboxLayout5);
hboxLayout4->addStretch();
window3->setLayout(hboxLayout4);
}
/* 多册添加书籍界面布局 */
void database::setWindowMutiAddBook()
{
//申请内存
tipLabel = new QLabel(); //添加提示标签
window5 = new QWidget();
tablewidget3 = new QTableWidget();
vboxLayout12 = new QVBoxLayout();
hboxLayout12 = new QHBoxLayout();
hboxLayout13 = new QHBoxLayout();
addBookOkButton = new QPushButton("提交表单"); //添加提交表单按钮
addBookClearButton = new QPushButton("清空表单"); //添加清空表单按钮
loadFileButton = new QPushButton("加载文件"); //添加加载文件按钮
//建立信号与槽
connect(addBookOkButton,SIGNAL(clicked()),this,SLOT(multiAddBook()));
connect(addBookClearButton,SIGNAL(clicked()),this,SLOT(clearAddBookMessage()));
connect(loadFileButton,SIGNAL(clicked()),this,SLOT(openFile()));
//设置选项信息(提示标签文本)
tipLabel->setText("请双击表格进行内容填写,或者通过文本加载。"
"如果有多个作者,请用空格分开\n"
"文本格式:按表格属性填写,同样属性按空格分开。"
"输入下本书籍信息要换行区分");
tipLabel->setFrameShape (QFrame::Box); //提示标签设置框架
QStringList header;
tablewidget3->setRowCount(13); //设置表格
tablewidget3->setColumnCount(7);
header<<"书名"<<"价格"<<"数量"<<"出版社"<<"出版日期"<<"分类"<<"作者";
//初始化表格信息
for(int i=0;i<13;i++){
for(int j=0;j<7;j++){
if(j==4){
publishDate[i] = new QComboBox();
for(int k=0;k<60;k++){
publishDate[i]->addItem(QString::number(2019-k));
}
tablewidget3->setCellWidget(i,j,publishDate[i]);
}
else if(j==5){
QString str[] = {
"马列主义毛邓思想","哲学","社会科学总论","政治法律","军事","经济","文化科学体育教育","语言文字",
"文学","艺术","历史地理","自然科学总论","数理科学与化学","天文学与地理科学","生物科学","医药卫生",
"工业技术","交通运输","航空航天","环境科学"
};
bookType[i] = new QComboBox(); //新建图书种类
for(int k=0;k<20;k++){
bookType[i]->addItem(str[k]);
}
tablewidget3->setCellWidget(i,j,bookType[i]);
}
else{
QTableWidgetItem *item = new QTableWidgetItem("");
tablewidget3->setItem(i,j,item);
}
}
}
tablewidget3->setHorizontalHeaderLabels(header);
//加入按钮和表格
hboxLayout12->addStretch();
hboxLayout12->addWidget(addBookOkButton);
hboxLayout12->addWidget(addBookClearButton);
hboxLayout13->addWidget(tipLabel);
hboxLayout13->addWidget(loadFileButton);
vboxLayout12->addLayout(hboxLayout13);
vboxLayout12->addWidget(tablewidget3);
vboxLayout12->addLayout(hboxLayout12);
window5->setLayout(vboxLayout12);
}
/* 借阅书籍界面布局 */
void database::setWindowLoanBook()
{
//申请内存
window4 = new QWidget();
mainTabWidget->addTab(window4,"借阅情况");
tablewidget2 = new QTableWidget(3,7);
hboxLayout11 = new QHBoxLayout();
vboxLayout11 = new QVBoxLayout();
returnBookButton = new QPushButton("归还书籍");
//建立信号与槽
connect(returnBookButton,SIGNAL(clicked()),this,SLOT(returnBook()));
//点击还书按钮,进行还书操作
//加入按钮和表格
hboxLayout11->addStretch();
hboxLayout11->addWidget(returnBookButton);
hboxLayout11->addStretch();
vboxLayout11->addWidget(tablewidget2);
vboxLayout11->addLayout(hboxLayout11);
vboxLayout11->addStretch();
window4->setLayout(vboxLayout11);
//更新借阅表格
updateLoanBookWindow();
}
/* 用户管理界面布局 */
void database::setWindowUserManage()
{
//申请内存
lastPageButton = new QPushButton("上一页"); //添加上一页按钮
nextPageButton = new QPushButton("下一页"); //同理
deleteUserButton = new QPushButton("删除用户信息");
grantLentButton = new QPushButton("授予/删除借书权限");
tablewidget1 = new QTableWidget(13,11);
window2 = new QWidget();
hboxLayout10 = new QHBoxLayout();
vboxLayout10 = new QVBoxLayout();
//建立信号与槽
connect(lastPageButton,SIGNAL(clicked()),this,SLOT(userLastPage()));
//点击上一页按钮,翻到上一页
connect(nextPageButton,SIGNAL(clicked()),this,SLOT(userNextPage()));
//点击下一页按钮,翻到下一页
connect(deleteUserButton,SIGNAL(clicked()),this,SLOT(deleteUser()));
//点击删除用户按钮,进行删除用户操作
connect(grantLentButton,SIGNAL(clicked()),this,SLOT(grantLent()));
//点击借书授权按钮,进行借书授权操作
//载入用户信息,并更新用户管理表格
loadUserMessage(); //加载用户信息
updateManageWindow(); //更新管理窗口
//加入按钮
hboxLayout10->addStretch();
hboxLayout10->addWidget(deleteUserButton);
hboxLayout10->addWidget(grantLentButton);
hboxLayout10->addWidget(lastPageButton);
hboxLayout10->addWidget(nextPageButton);
//布局
vboxLayout10->addWidget(tablewidget1);
vboxLayout10->addLayout(hboxLayout10);
window2->setLayout(vboxLayout10);
}
/* 用户注册界面布局 */
void database::setWindowRegister()
{
//申请内存
registerWindow = new QWidget(); //添加注册窗口
registerWindow->setWindowTitle(tr("注册系统")); //注册窗口添加标题
gridLayout3 = new QGridLayout();
label4 = new QLabel[3]; //创建标签
label4[0].setText("学号");
label4[1].setText("密码");
label4[2].setText("密码确认");
label3 = new QLabel("请输入以下注册信息:");//创建提示标签
nameLine3 = new QLineEdit(); //创建学号输入栏
passwordLine1 = new QLineEdit(); //创建密码输入栏
passwordLine2 = new QLineEdit();
passwordLine1->setEchoMode(QLineEdit::Password); //设置密码输入样式
passwordLine2->setEchoMode(QLineEdit::Password);
registerOkButton = new QPushButton("注册"); //创建注册按钮
hboxLayout7 = new QHBoxLayout();
hboxLayout8 = new QHBoxLayout();
vboxLayout7 = new QVBoxLayout();
//页面布局
hboxLayout7->addStretch();
hboxLayout7->addWidget(registerOkButton);
hboxLayout7->addStretch();
gridLayout3->addWidget(&label4[0],1,1);
gridLayout3->addWidget(&label4[1],2,1);
gridLayout3->addWidget(&label4[2],3,1);
gridLayout3->addWidget(nameLine3,1,2);
gridLayout3->addWidget(passwordLine1,2,2);
gridLayout3->addWidget(passwordLine2,3,2);
vboxLayout7->addWidget(label3);
vboxLayout7->addLayout(gridLayout3);
vboxLayout7->addLayout(hboxLayout7);
hboxLayout8->addStretch();
hboxLayout8->addLayout(vboxLayout7);
hboxLayout8->addStretch();
registerWindow->setLayout(hboxLayout8);
registerWindow->resize(400,300);
registerWindow->show();
//建立信号与槽
connect(registerOkButton,SIGNAL(clicked()),this,SLOT(Register()));
//点击确认登录按钮,进行登录操作
}
/* 显示书籍信息界面布局 */
void database::setWindowShowBook(int type)
{
//申请内存
hboxLayout6 = new QHBoxLayout();
lastbutton = new QPushButton(tr("上一页")); //创建按钮
nextbutton = new QPushButton(tr("下一页"));
//建立信号与槽
connect(lastbutton,SIGNAL(clicked()),this,SLOT(bookLastPage()));
//点击上一页按钮,翻到上一页
connect(nextbutton,SIGNAL(clicked()),this,SLOT(bookNextPage()));
//点击下一页按钮,翻到下一页
//页面布局
hboxLayout6->addStretch();
hboxLayout6->addWidget(lastbutton);
hboxLayout6->addWidget(nextbutton);
//如果当前使用者为学生,加入借阅书籍按钮
if(type==USER_LOGIN||type==USER_REGISTER){
lendBookButton = new QPushButton(tr("借阅书籍")); //添加借阅按钮
connect(lendBookButton,SIGNAL(clicked()),this,SLOT(lendBook())); //连接信号与槽
hboxLayout6->addWidget(lendBookButton);
}
//初始化当前页和最大页
current_page = 1;
max_page = 1;
//更新显示书目窗口
updateShowBookWindow();
}
/* 更新书籍信息界面 */
void database::updateShowBookWindow()
{
QStringList header;
//清除表格信息
tablewidget->clear();
//设置表格为不可选择、不可修改
tablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tablewidget->setSelectionMode(QAbstractItemView::NoSelection);
//根据当前使用者的不同使用不同的标题栏
if(user_type==USER){
tablewidget->setRowCount(10);
tablewidget->setColumnCount(11);
header<<""<<"编号"<<"书名"<<"价格"<<"库存"<<"入库时间"
<<"入库人员"<<"出版社"<<"出版年份"<<"类型"<<"作者";
}
else{
tablewidget->setRowCount(10);
tablewidget->setColumnCount(10);
header<<"编号"<<"书名"<<"价格"<<"库存"<<"入库时间"
<<"入库人员"<<"出版社"<<"出版年份"<<"类型"<<"作者";
}
//加入标题栏
tablewidget->setHorizontalHeaderLabels(header);
//设置宽度
for(int i=0;i<10;i++){
tablewidget->setColumnWidth(i,120);
}
//求出当前页的最多显示条目
int max;
if(result.length()==0)return;
if(current_page==max_page){
max = result.length()%100/10;
if(max==0)max=10;
}
else max = 10;
//根据当前使用者的不同在表格上显示不同信息
if(user_type==USER){
for(int i=0;i<max;i++){
item2[i] = new QTableWidgetItem();
item2[i]->setCheckState(Qt::Unchecked);
item3[i] = new QTableWidgetItem(result.at(10*i+100*(current_page-1)));
tablewidget->setItem(i,0,item2[i]);
tablewidget->setItem(i,1,item3[i]);
for(int j=1;j<10;j++){
QTableWidgetItem *item = new QTableWidgetItem(result.at(10*i+j+100*(current_page-1)));
tablewidget->setItem(i,j + 1,item);
}
}
}
else{
for(int i=0;i<max;i++){
for(int j=0;j<10;j++){
QTableWidgetItem *item = new QTableWidgetItem(result.at(10*i+j+100*(current_page-1)));
tablewidget->setItem(i,j,item);
}
}
}
}
/* 更新借阅书籍界面 */
void database::updateLoanBookWindow()
{
//更新前加载借阅信息
loadLoanMessage();
QStringList header;
//清空表格信息
tablewidget2->clear();
//设置标题栏
header<<""<<"书本编号"<<"书本名称"<<"借阅日期"<<"单册编号"<<"最晚归还日期"<<"剩余归还天数";
tablewidget2->setHorizontalHeaderLabels(header);
//设置表格为不可修改,不可选择
tablewidget2->setEditTriggers(QAbstractItemView::NoEditTriggers);
tablewidget2->setSelectionMode(QAbstractItemView::NoSelection);
//将信息显示到表格上
for(int i=0;i<loanMessage.length();i++){
item4[i] = new QTableWidgetItem();
item4[i]->setCheckState(Qt::Unchecked);
tablewidget2->setItem(i,0,item4[i]);
for(int j=0;j<6;j++){
QTableWidgetItem *item = new QTableWidgetItem(loanMessage.at(i)[j]);
tablewidget2->setItem(i,j + 1,item);
}
}
}
/* 更新用户管理界面 */
void database::updateManageWindow()
{
//计算当前页面能显示的最多用户数
int max;
if(user_current_page==user_max_page){
max = (user_num%13==0)?13:user_num%13;
}
else{
max = 13;
}
//清除表格信息
tablewidget1->clear();
//设置表格为不可选择,不可修改
tablewidget1->setEditTriggers(QAbstractItemView::NoEditTriggers);
tablewidget1->setSelectionMode(QAbstractItemView::NoSelection);
//设置标题栏
QStringList header;
header<<""<<"学号"<<"密码"<<"借书权限"<<"借阅数目"<<"借阅书籍1"
<<"借阅日期"<<"借阅书籍2"<<"借阅日期"<<"借阅书籍3"<<"借阅日期";
tablewidget1->setHorizontalHeaderLabels(header);
//将信息显示到表格上
for(int i=0;i<max;i++){
int row = i + (user_current_page - 1)*13;
item0[i] = new QTableWidgetItem();
item0[i]->setCheckState(Qt::Unchecked);
item1[i] = new QTableWidgetItem(userMessage.at(row)[0]);
tablewidget1->setItem(i,0,item0[i]);
tablewidget1->setItem(i,1,item1[i]);
for(int j=1;j<10;j++){
QTableWidgetItem *item = new QTableWidgetItem(userMessage.at(row)[j]);
tablewidget1->setItem(i,j+1,item);
}
}
}
/* 更新时间显示 */
void database::updateTime()
{
//获取系统时间
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
QString text = "当前用户: " + current_user + " | 当前时间:"+current_date+" ";
spacelabel2->setText(text);
}
/* 更新标题栏信息 */
void database::updateTitle(int type)
{
//获取系统时间
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
QString text;
//如果当前使用者为游客,更新标题栏,直接返回
if(type==VISITOR){
text = "当前角色:游客 | 当前时间:"+current_date+" ";
spacelabel2->setText(text);
return;
}
//登录后删除登录、注册按钮
delete loginbutton;
delete registerbutton;
//新建退出登录按钮,点击退出登录按钮,进行退出登录操作
quitloginbutton = new QPushButton("退出登录");
quitloginbutton->setStyleSheet("color:rgb(0,0,255)");
connect(quitloginbutton,SIGNAL(clicked()),this,SLOT(quitLogin()));
hboxLayout3->addWidget(quitloginbutton);
quitloginbutton->setFlat(true);
//根据当前使用者显示不同的标题栏
if(type==USER_LOGIN){
text = "当前角色:"+username1->text()+" | 当前时间:"+current_date+" ";
current_user = username1->text();
user_type = USER;
//删除登录窗口
delete loginTabWidget;
}
else if(type==USER_REGISTER){
text = "当前角色:"+nameLine3->text()+" | 当前时间:"+current_date+" ";
current_user = nameLine3->text();
user_type = USER;
//删除注册窗口
delete registerWindow;
}
else if(type==MANAGER){
text = "当前角色:"+username2->text()+" | 当前时间:"+current_date+" ";
current_user = username2->text();
user_type = MANAGER;
//删除登录窗口
delete loginTabWidget;
}
spacelabel2->setText(text);
}
/* 创建登录窗口(登录窗口布局的辅助函数) */
QWidget* database::createLoginWindow(int type)
{
//申请内存
QWidget *widget = new QWidget();
QLabel *username_label = new QLabel;
QLabel *password_label = new QLabel;
QGridLayout *glayout = new QGridLayout;
QPushButton *yesbutton = new QPushButton(tr("确定"));
QPushButton *quitbutton = new QPushButton(tr("退出"));
QHBoxLayout *hlayout = new QHBoxLayout();
QVBoxLayout *vlayout = new QVBoxLayout();
//根据不同类型建立不同的输入栏
if(type==1){
username1 = new QLineEdit();
password1 = new QLineEdit();
password1->setEchoMode(QLineEdit::Password);
glayout->addWidget(username1,1,2);
glayout->addWidget(password1,2,2);
}
else if(type==2){
username2 = new QLineEdit();
password2 = new QLineEdit();
password2->setEchoMode(QLineEdit::Password);
glayout->addWidget(username2,1,2);
glayout->addWidget(password2,2,2);
}
username_label->setText(tr("学号:"));
password_label->setText(tr("密码:"));
//表格布局加入文字标签
glayout->setContentsMargins(50,100,50,100);
glayout->setSpacing(40);
glayout->addWidget(username_label,1,1);
glayout->addWidget(password_label,2,1);
//加入按钮和输入栏
hlayout->addStretch();
hlayout->addWidget(yesbutton);
hlayout->addWidget(quitbutton);
hlayout->addStretch();
vlayout->addLayout(glayout);
vlayout->addLayout(hlayout);
widget->setLayout(vlayout);
//点击退出按钮,进行删除登录窗口操作
connect(quitbutton,SIGNAL(clicked()),this,SLOT(deleteLogin()));
//点击确认按钮,根据类型不同将信号发送到用户登录和管理员登录中
if(type==1){
connect(yesbutton,SIGNAL(clicked()),this,SLOT(userLogin()));
}
else if(type==2){
connect(yesbutton,SIGNAL(clicked()),this,SLOT(managerLogin()));
}
return widget;
}
/* 清空搜索框 */
void database::clear()
{
nameLine->clear();
publishLine->clear();
authorLine->clear();
priceLine->clear();
priceLine1->clear();
}
/* 清空多册添加的表单 */
void database::clearAddBookMessage()
{
//对每一个表单,设置为空
for(int i = 0;i<13;i++){
for(int j = 0;j<7;j++){
if(j!=5&&j!=4){
tablewidget3->item(i,j)->setText("");
}
}
}
//如果有图书信息缓存,清空
for(int i=0;i<multiBookMessage.length();i++){
multiBookMessage.pop_front();
}
}
/* 删除登录界面*/
void database::deleteLogin()
{
//删除登陆界面,并将登录注册按钮设为可使用
delete loginTabWidget;
loginbutton->setEnabled(true);
registerbutton->setEnabled(true);
}
/* 退出登录更新界面*/
void database::quitLogin()
{
//删除退出登录按钮
delete quitloginbutton;
//根据当前使用者的不同,删除对应的窗口以及控件
if(user_type==MANAGER){
delete window3;
delete window2;
delete window5;
}
else if(user_type==USER){
delete window4;
delete lendBookButton;
}
//获取系统时间
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
QString text = "当前角色: 游客 | 当前时间:"+current_date+" ";
//更新使用者类型为游客
user_type = VISITOR;
current_user = "游客";
spacelabel2->setText(text);