PageWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "PageWidget.h"
  2. #include "ui_PageWidget.h"
  3. #include <QtGlobal>
  4. #include <QHBoxLayout>
  5. #include <QMouseEvent>
  6. #include <QKeyEvent>
  7. #include <QDebug>
  8. #include <QIntValidator>
  9. PageWidget::PageWidget(int blockSize, QWidget *parent) : QWidget(parent),
  10. ui(new Ui::PageWidget) {
  11. ui->setupUi(this);
  12. QString qss = QString(".QLabel[page=\"true\"] { padding: 1px; }")
  13. + QString(".QLabel[currentPage=\"true\"] { color: rgb(190, 0, 0);}")
  14. + QString(".QLabel[page=\"true\"]:hover { color: white; border-radius: 4px; background-color: qlineargradient(spread:reflect, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(53, 121, 238, 255), stop:1 rgba(0, 202, 237, 255));}");
  15. this->setStyleSheet(qss);
  16. setBlockSize(blockSize);
  17. initialize();
  18. maxPage = 0;
  19. ui->pageLineEdit->setText(QString::number(1));
  20. }
  21. PageWidget::~PageWidget() {
  22. delete ui;
  23. delete pageLabels;
  24. }
  25. bool PageWidget::eventFilter(QObject *watched, QEvent *e) {
  26. if (e->type() == QEvent::MouseButtonRelease) {
  27. int page = -1;
  28. if (watched == ui->previousPageLabel) { page = getCurrentPage() - 1; }
  29. if (watched == ui->nextPageLabel) { page = getCurrentPage() + 1; }
  30. for (int i = 0; i < pageLabels->count(); ++i) {
  31. if (watched == pageLabels->at(i)) {
  32. page = pageLabels->at(i)->text().toInt();
  33. break;
  34. }
  35. }
  36. if (-1 != page) {
  37. setCurrentPage(page, true);
  38. return true;
  39. }
  40. }
  41. if (watched == ui->pageLineEdit && e->type() == QEvent::KeyRelease) {
  42. QKeyEvent *ke = static_cast<QKeyEvent *>(e);
  43. if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
  44. {
  45. int page = ui->pageLineEdit->text().toInt();
  46. if(page > maxPage)
  47. {
  48. page = maxPage;
  49. ui->pageLineEdit->setText(QString::number(page));
  50. }
  51. setCurrentPage(page, true);
  52. return true;
  53. }
  54. }
  55. return QWidget::eventFilter(watched, e);
  56. }
  57. int PageWidget::getBlockSize() const {
  58. return blockSize;
  59. }
  60. int PageWidget::getMaxPage() const {
  61. return maxPage;
  62. }
  63. int PageWidget::getCurrentPage() const {
  64. return currentPage;
  65. }
  66. void PageWidget::setMaxPage(int page) {
  67. page = qMax(page, 1);
  68. if (maxPage != page) {
  69. maxPage = page;
  70. currentPage = 1;
  71. updatePageLabels();
  72. }
  73. }
  74. void PageWidget::setCurrentPage(int page, bool signalEmitted) {
  75. page = qMax(page, 1);
  76. page = qMin(page, maxPage);
  77. if (page != currentPage) {
  78. currentPage = page;
  79. updatePageLabels();
  80. ui->pageLineEdit->setText(QString::number(page));
  81. if (signalEmitted) {
  82. emit currentPageChanged(page);
  83. }
  84. }
  85. }
  86. QComboBox *PageWidget::getComboBox() const
  87. {
  88. return ui->comboBox;
  89. }
  90. void PageWidget::setBlockSize(int blockSize) {
  91. blockSize = qMax(blockSize, 3);
  92. if (blockSize % 2 == 0) {
  93. ++blockSize;
  94. }
  95. this->blockSize = blockSize;
  96. }
  97. // 分成三个部分, 左...中...右
  98. void PageWidget::initialize() {
  99. ui->pageLineEdit->installEventFilter(this);
  100. ui->pageLineEdit->setValidator(new QIntValidator(1, 10000000, this));
  101. ui->nextPageLabel->setProperty("page", "true");
  102. ui->previousPageLabel->setProperty("page", "true");
  103. ui->nextPageLabel->installEventFilter(this);
  104. ui->previousPageLabel->installEventFilter(this);
  105. pageLabels = new QList<QLabel *>();
  106. // 设置布局
  107. QHBoxLayout *leftLayout = new QHBoxLayout();
  108. QHBoxLayout *centerLayout = new QHBoxLayout();
  109. QHBoxLayout *rightLayout = new QHBoxLayout();
  110. leftLayout->setContentsMargins(0, 0, 0, 0);
  111. leftLayout->setSpacing(0);
  112. centerLayout->setContentsMargins(0, 0, 0, 0);
  113. centerLayout->setSpacing(0);
  114. rightLayout->setContentsMargins(0, 0, 0, 0);
  115. rightLayout->setSpacing(0);
  116. for (int i = 0; i < blockSize * 3-2; ++i) {
  117. QLabel *label = new QLabel(QString::number(i + 1));
  118. label->setProperty("page", "true");
  119. label->installEventFilter(this);
  120. pageLabels->append(label);
  121. if (i < 1) {
  122. leftLayout->addWidget(label);
  123. } else if (i < blockSize * 2) {
  124. centerLayout->addWidget(label);
  125. } else {
  126. rightLayout->addWidget(label);
  127. }
  128. }
  129. ui->leftPagesWidget->setLayout(leftLayout);
  130. ui->centerPagesWidget->setLayout(centerLayout);
  131. ui->rightPagesWidget->setLayout(rightLayout);
  132. initCombo();
  133. }
  134. void PageWidget::initCombo(){
  135. ui->comboBox->addItem("10条/页", QVariant(10)); // 显示文本为 "Apple",值为 1
  136. ui->comboBox->addItem("50条/页", QVariant(50));
  137. ui->comboBox->addItem("100条/页", QVariant(100));
  138. ui->comboBox->addItem("200条/页", QVariant(200));
  139. }
  140. void PageWidget::updatePageLabels() {
  141. ui->leftSeparateLabel->hide();
  142. ui->rightSeparateLabel->hide();
  143. if (maxPage <= blockSize * 3) {
  144. for (int i = 0; i < pageLabels->count(); i += 1) {
  145. QLabel *label = pageLabels->at(i);
  146. if (i < maxPage) {
  147. label->setText(QString::number(i + 1));
  148. label->show();
  149. } else {
  150. label->hide();
  151. }
  152. if (currentPage - 1 == i) {
  153. label->setProperty("currentPage", "true");
  154. } else {
  155. label->setProperty("currentPage", "false");
  156. }
  157. label->setStyleSheet("/**/");
  158. }
  159. return;
  160. }
  161. int c = currentPage;
  162. int n = blockSize;
  163. int m = maxPage;
  164. int centerStartPage = 0;
  165. if (c >= 1 && c < n + n / 2 + 1) {
  166. // 显示前 n * 2 个, 后 n 个: 只显示右边的分隔符
  167. centerStartPage = 1;
  168. ui->rightSeparateLabel->show();
  169. } else if (c > m - n - n / 2 && c <= m) {
  170. // 显示前 n 个, 后 n * 2 个: 只显示左边的分隔符
  171. centerStartPage = m - n - n ;
  172. ui->leftSeparateLabel->show();
  173. } else {
  174. // 显示[1, n], [c - n/2, c + n/2], [m - n + 1, m]: 两个分隔符都显示
  175. centerStartPage = c - n;
  176. ui->rightSeparateLabel->show();
  177. ui->leftSeparateLabel->show();
  178. }
  179. pageLabels->at(0)->setText(QString::number(1));// 前面 n 个
  180. for (int i = 1; i < 6; ++i) {
  181. pageLabels->at(i)->setText(QString::number(centerStartPage + i)); // 中间 n 个
  182. }
  183. pageLabels->at(6)->setText(QString::number(m));// 后面 n 个
  184. for (int i = 0; i < pageLabels->count(); ++i) {
  185. QLabel *label = pageLabels->at(i);
  186. int page = label->text().toInt();
  187. if (page == currentPage) {
  188. label->setProperty("currentPage", "true");
  189. } else {
  190. label->setProperty("currentPage", "false");
  191. }
  192. label->setStyleSheet("/**/");
  193. label->show();
  194. }
  195. }