-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbookexhibition.cpp
More file actions
198 lines (183 loc) · 5.78 KB
/
Copy pathbookexhibition.cpp
File metadata and controls
198 lines (183 loc) · 5.78 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
#include "bookexhibition.h"
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>
#include <vector>
#include <QVector>
extern qreal dpi;
BookExhibition::BookExhibition(QWidget *parent) : QMdiSubWindow(parent)
{
setWindowFlags(Qt::FramelessWindowHint);
this->setGeometry(150*dpi,170*dpi,859*dpi,533*dpi);
this->setStyleSheet("background-color:#ffffff;border:none");
cursorLayer=1;
currentPage=0;
maxPages=0;
nextPage=new ToolButton(this);
nextPage->setGeometry(460*dpi,500*dpi,30*dpi,30*dpi);
nextPage->setIcon(QPixmap(":/Images/Icons/NextPage.png"));
connect(nextPage,SIGNAL(clicked()),this,SLOT(toNextPage()));
prePage=new ToolButton(this);
prePage->setGeometry(360*dpi,500*dpi,30*dpi,30*dpi);
prePage->setIcon(QPixmap(":/Images/Icons/PrePage.png"));
connect(prePage,SIGNAL(clicked()),this,SLOT(toPrePage()));
}
BookExhibition::~BookExhibition()
{
delete nextPage;
delete prePage;
}
void BookExhibition::toNextPage()
{
if(currentPage<maxPages)
currentPage+=1;
update();
}
void BookExhibition::toPrePage()
{
if(currentPage>0)
currentPage-=1;
update();
}
void BookExhibition::refreshDesp()
{
despList.clear();
if(books.size()==0)
return;
for(unsigned int i=0;i<books.size();i++)
{
QVector<QString> a;
QString desc=(this->books)[i]->getStringByTag("description");
if(desc.length()==0)
{
a.push_back(QString("本书暂无简介。"));
}
else
{
int j=0,l=0;
while(j<5&&l+25<desc.length())
{
if(j==0)
{
a.push_back(desc.mid(0,23));
l+=23;
}
else
{
a.push_back(desc.mid(l,25));
l+=25;
}
j++;
}
if(j==5)
a.push_back(desc.mid(l,20)+".......");
else
a.push_back(desc.mid(l,25));
}
despList.push_back(a);
}
}
Book* BookExhibition::bookOnDisplay()
{
if(books.size()>0)
return books[9*currentPage+cursorLayer-1];
else
return Q_NULLPTR;
}
void BookExhibition::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QFont titleFont("微软雅黑",17);
titleFont.setBold(true);
QFont authorFont("宋体",13);
QFont subFont("微软雅黑",13);
QPen dashLine(QColor("#cccccc"));
QPen titleText(QColor("#5b5b5b"));
QPen subscript(Qt::gray);
painter.setFont(titleFont);
dashLine.setStyle(Qt::DashLine);
painter.setPen(dashLine);
int h=0*dpi;
if(books.size()>0)
maxPages=(books.size()-1)/9;
else
maxPages=0;
int index; //当前显示书籍的索引
if(books.size()==0) //未发现符合条件的图书
{
//显示提示字符
}
else
{
/*
* 这里可以添加简介字符串的预处理
*/
for(int i=1;(i+currentPage*9<=books.size())&&i<=9;i++)
{
index=i-1+currentPage*9;
if(i<cursorLayer)
h+=41*dpi;
else if(i==cursorLayer)
h+=41*4*dpi;
else
h+=41*dpi;
painter.drawLine(0,h,this->width(),h);
if(i==cursorLayer)
{
painter.drawPixmap(20*dpi,h-164*dpi+2*dpi,QPixmap((this->books)[index]->getStringByTag("loc")).scaled(120*dpi,160*dpi,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
painter.setPen(titleText);
painter.drawText(20*dpi+120*dpi,h-164*dpi+30*dpi,(this->books)[index]->getStringByTag("title"));
painter.setPen(subscript);
painter.setFont(authorFont);
painter.drawText((20+120)*dpi,h-164*dpi+55*dpi,(this->books)[index]->getStringByTag("author"));
painter.drawText(20*dpi+120*dpi,h-164*dpi+80*dpi,(this->books)[index]->getStringByTag("press"));
//打印简介
for(int j=0;j<despList[index].size();j++)
{
if(j==0)
painter.drawText(20*dpi+430*dpi,h-164*dpi+55*dpi+20*j*dpi,despList[index].at(j));
else
painter.drawText(20*dpi+400*dpi,h-164*dpi+55*dpi+20*j*dpi,despList[index].at(j));
}
}
else
{
painter.setPen(titleText);
painter.setFont(titleFont);
painter.drawText(20*dpi,h-41*dpi+30*dpi,(this->books)[index]->getStringByTag("title"));
}
painter.setPen(dashLine);
}
}
painter.setPen(subscript);
painter.setFont(subFont);
painter.drawText(20*dpi,520*dpi,QString("共%1条搜索结果").arg(QString::number(books.size())));
painter.drawText(408*dpi,520*dpi,QString("%1 / %2").arg(QString::number(currentPage+1)).arg(maxPages+1));
}
void BookExhibition::mouseMoveEvent(QMouseEvent *event)
{
int y=(event->pos()).y();
if(y>=0&&y<(cursorLayer-1)*41*dpi)
{
cursorLayer=y/(41*dpi)+1;
update();
}
else if(y>=(cursorLayer+3)*41*dpi&&y<this->height())
{
cursorLayer=y/(41*dpi)-2;
update();
}
if(currentPage<maxPages&&cursorLayer>=9) //除尾页外每页显示9组数据,鼠标层数为10则置为9
cursorLayer=9;
else if(currentPage==maxPages&&cursorLayer>=(books.size()-1)%9+1) //尾页显示books.size()%10+1组数据,鼠标层数超过则显示最后一组
cursorLayer=(books.size()-1)%9+1;
}
void BookExhibition::mousePressEvent(QMouseEvent *event)
{
if(event->button()&Qt::LeftButton)
{
int y=event->pos().y();
if(y>=0*dpi&&y<=492*dpi)
emit bookInfoClicked();
}
}