DialogBase.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "DialogBase.h"
  2. #include <QMouseEvent>
  3. #include <QVBoxLayout>
  4. #include <QLabel>
  5. #include <QPushButton>
  6. #include <QBoxLayout>
  7. #include <QStyle>
  8. #include <qsize.h>
  9. #include "OneShadowEffect.h"
  10. DialogBase::DialogBase(QWidget *parent)
  11. : QDialog(parent)
  12. {
  13. m_logger = spdlog::get("ACASetting");
  14. if(m_logger == nullptr)
  15. {
  16. fmt::print("DialogBase: 未发现ACASetting日志记录器,请检查日志记录器是否已初始化。\n");
  17. return;
  18. }
  19. initUI();
  20. initSettings();
  21. /* 设置qss */
  22. connect(&UIStyle, &UIStyleManager::signal_qssChanged, this, &DialogBase::do_setQSS);
  23. /* 设置默认的UI */
  24. setQSS();
  25. }
  26. DialogBase::~DialogBase()
  27. {
  28. }
  29. /* 设置标题 */
  30. void DialogBase::setTitle(const QString &title, QSize size)
  31. {
  32. m_labelTitle->setText(title);
  33. m_labelTitle->setFixedSize(size);
  34. }
  35. /* 获取标题 */
  36. QString DialogBase::getTitle() const
  37. {
  38. return m_labelTitle ? m_labelTitle->text() : QString();
  39. }
  40. /* 设置内容容器 */
  41. bool DialogBase::setContentWidget(QWidget *widget)
  42. {
  43. if(widget == nullptr)
  44. {
  45. SPDLOG_LOGGER_ERROR(m_logger, "设置内容容器失败,widget为nullptr");
  46. return false;
  47. }
  48. if(m_widgetContent == widget)
  49. {
  50. return true; // 如果已经是这个widget了,就不需要重新设置
  51. }
  52. if(m_widgetContent != nullptr)
  53. {
  54. m_layoutBackground->removeWidget(m_widgetContent);
  55. delete m_widgetContent;
  56. m_widgetContent = nullptr;
  57. }
  58. m_widgetContent = widget;
  59. m_layoutBackground->insertWidget(1, m_widgetContent);
  60. return true;
  61. }
  62. /* 设置低栏容器 */
  63. bool DialogBase::setBottomWidget(QWidget *widget)
  64. {
  65. if(widget == nullptr)
  66. {
  67. SPDLOG_LOGGER_ERROR(m_logger, "设置底部容器失败,widget为nullptr");
  68. return false;
  69. }
  70. if(m_widgetBottom != nullptr)
  71. {
  72. m_layoutBackground->removeWidget(m_widgetBottom);
  73. delete m_widgetBottom;
  74. m_widgetBottom = nullptr;
  75. }
  76. m_widgetBottom = widget;
  77. m_layoutBackground->insertWidget(2, m_widgetBottom);
  78. return true;
  79. }
  80. /* 移除底栏 */
  81. bool DialogBase::removeBottomWidget()
  82. {
  83. if(m_widgetBottom == nullptr)
  84. {
  85. return true;
  86. }
  87. m_layoutBackground->removeWidget(m_widgetBottom);
  88. delete m_widgetBottom;
  89. m_widgetBottom = nullptr;
  90. return true;
  91. }
  92. /* 初始化UI */
  93. void DialogBase::initUI()
  94. {
  95. /*--------------------- 设置对话框的属性 -----------------------*/
  96. this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
  97. this->setAttribute(Qt::WA_TranslucentBackground, true);
  98. // this->setObjectName("DialogBase");
  99. /*--------------------- 初始化背景容器 -----------------------*/
  100. m_widgetBackground = new QWidget(this);
  101. m_widgetBackground->setObjectName("widget_background");
  102. QVBoxLayout* layout = new QVBoxLayout(this);
  103. /* 设置边距,就是阴影的宽度 */
  104. layout->setContentsMargins(20, 20, 20, 20);
  105. layout->setSpacing(0);
  106. this->setLayout(layout);
  107. layout->addWidget(m_widgetBackground);
  108. /* 创建背景容器内部的布局 */
  109. m_layoutBackground = new QVBoxLayout(m_widgetBackground);
  110. m_layoutBackground->setContentsMargins(0, 0, 0, 0);
  111. m_layoutBackground->setSpacing(0);
  112. m_widgetBackground->setLayout(m_layoutBackground);
  113. /*--------------------- 初始顶栏 -----------------------*/
  114. /* 初始化顶部标题栏,高度56,下面分割线高度1 */
  115. m_widgetTop = new QWidget(this);
  116. m_widgetTop->setObjectName("widget_top");
  117. m_widgetTop->setFixedHeight(56);
  118. m_layoutBackground->addWidget(m_widgetTop);
  119. QHBoxLayout* topHBoxLayout = new QHBoxLayout(m_widgetTop);
  120. m_widgetTop->setLayout(topHBoxLayout);
  121. /* 初始化标题栏 */
  122. m_labelTitle = new QLabel(m_widgetTop);
  123. m_labelTitle->setObjectName("label_title");
  124. m_labelTitle->setText("Dialog Title");
  125. m_labelTitle->setFixedSize(120, 18);
  126. m_labelTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  127. /* 初始化关闭按钮 */
  128. m_pBtn_Close = new QPushButton(m_widgetTop);
  129. m_pBtn_Close->setObjectName("pBtn_Close");
  130. m_pBtn_Close->resize(32, 32);
  131. /* 安装事件过滤器,主要是处理鼠标悬停事件 */
  132. m_pBtn_Close->installEventFilter(this);
  133. /* 重新布局顶栏 */
  134. layoutTop();
  135. /*--------------------- 初始化内容栏 -----------------------*/
  136. // m_widgetContent = new QWidget(this);
  137. // m_widgetContent->resize(400, 300);
  138. // m_layoutBackground->addWidget(m_widgetContent);
  139. /*--------------------- 初始底栏 -----------------------*/
  140. m_widgetBottom = new QWidget(this);
  141. m_widgetBottom->setObjectName("widget_bottom");
  142. m_widgetBottom->setFixedHeight(88);
  143. m_layoutBackground->addWidget(m_widgetBottom);
  144. QHBoxLayout* bottomHBoxLayout = new QHBoxLayout(m_widgetBottom);
  145. m_widgetBottom->setLayout(bottomHBoxLayout);
  146. /* 初始化取消按钮 */
  147. m_pBtn_Cancel = new QPushButton(m_widgetBottom);
  148. m_pBtn_Cancel->setObjectName("pBtn_Cancel");
  149. m_pBtn_Cancel->setText("取消");
  150. m_pBtn_Cancel->setFixedSize(60, 32);
  151. bottomHBoxLayout->addWidget(m_pBtn_Cancel, 0, Qt::AlignRight | Qt::AlignVCenter);
  152. /* 初始化确认按钮 */
  153. m_pBtn_OK = new QPushButton(m_widgetBottom);
  154. m_pBtn_OK->setObjectName("pBtn_OK");
  155. m_pBtn_OK->setText("确定");
  156. m_pBtn_OK->setFixedSize(60, 32);
  157. bottomHBoxLayout->addWidget(m_pBtn_OK, 0, Qt::AlignRight | Qt::AlignVCenter);
  158. /* 添加一个底部横向弹簧 */
  159. bottomHBoxLayout->insertStretch(0, 1);
  160. /* 设置底部按钮的间距 */
  161. bottomHBoxLayout->setContentsMargins(32, 24, 32, 32);
  162. bottomHBoxLayout->setSpacing(16);
  163. /*--------------------- 设置整体背景 -----------------------*/
  164. /* 这个背景需要在初始化所有的widget控件之后再设置 */
  165. // auto screenRect = QGuiApplication::screenAt(QCursor::pos())->geometry();
  166. // this->resize(screenRect.width(), screenRect.height());
  167. // /* 设置设置区域居中显示 */
  168. // m_widgetBackground->move(screenRect.width() / 2 - m_widgetBackground->width() / 2,
  169. // screenRect.height() / 2 - m_widgetBackground->height() / 2);
  170. /*--------------------- 创建阴影 -----------------------*/
  171. auto pShadow = new OneShadowEffect(this);
  172. m_widgetBackground->setGraphicsEffect(pShadow);
  173. }
  174. /* 初始化其他设置 */
  175. void DialogBase::initSettings()
  176. {
  177. /* 初始化变量 */
  178. m_lastPos = QPoint(0, 0);
  179. /* 连接信号和槽 */
  180. connect(m_pBtn_Close, &QPushButton::clicked, this, &DialogBase::do_pBtn_Close_Clicked);
  181. connect(m_pBtn_OK, &QPushButton::clicked, this, &DialogBase::do_pBtn_OK_Clicked);
  182. connect(m_pBtn_Cancel, &QPushButton::clicked, this, &DialogBase::do_pBtnCancel_clicked);
  183. /* 设置样式表 */
  184. // setQSS();
  185. }
  186. /* 加载QSS */
  187. void DialogBase::setQSS()
  188. {
  189. /* 获取样式表路径 */
  190. QString qssPath = UIStyle.getQSSPath() + "/dialogbase.qss";
  191. QFile file(qssPath);
  192. if(file.open(QFile::ReadOnly))
  193. {
  194. QString qss = file.readAll();
  195. file.close();
  196. this->setStyleSheet(qss);
  197. } else
  198. {
  199. SPDLOG_LOGGER_ERROR(m_logger, "打开QSS文件失败: {}", qssPath.toStdString());
  200. SPDLOG_LOGGER_ERROR(m_logger, "错误信息: {}", file.errorString().toStdString());
  201. }
  202. }
  203. /* 设置top栏的位置布局,标题和关闭按钮的高度起始位置不一样,不方便使用布局,所有手动修改坐标
  204. * 标题位置32,18,高度18
  205. * 关闭按钮右对齐16,y12,大小32x32 */
  206. void DialogBase::layoutTop()
  207. {
  208. m_labelTitle->move(32, 18);
  209. int x = m_widgetTop->width() - m_pBtn_Close->width() - 16;
  210. m_pBtn_Close->move(x, 12);
  211. }
  212. /* 确认按钮点击事件 */
  213. void DialogBase::do_pBtn_OK_Clicked()
  214. {
  215. m_isOK = true;
  216. this->accept();
  217. }
  218. /* 设置QSS */
  219. void DialogBase::do_setQSS(EUIStyle style)
  220. {
  221. setQSS();
  222. }
  223. /* 重写鼠标按下事件 */
  224. void DialogBase::mousePressEvent(QMouseEvent *event)
  225. {
  226. m_lastPos = event->globalPos();
  227. event->accept();
  228. }
  229. /* 重写鼠标移动事件 */
  230. void DialogBase::mouseMoveEvent(QMouseEvent *event)
  231. {
  232. auto point = m_widgetTop->mapToGlobal(QPoint(0, 0));
  233. QRect rect(point, m_widgetTop->size());
  234. if(!rect.contains(m_lastPos))
  235. {
  236. event->accept();
  237. return;
  238. }
  239. int dx = event->globalX() - m_lastPos.x();
  240. int dy = event->globalY() - m_lastPos.y();
  241. // m_widgetBackground->move(m_widgetBackground->x() + dx, m_widgetBackground->y() + dy);
  242. move(x() + dx, y() + dy);
  243. m_lastPos = event->globalPos();
  244. event->accept();
  245. }
  246. /* 重写鼠标释放事件 */
  247. void DialogBase::mouseReleaseEvent(QMouseEvent *event)
  248. {
  249. event->accept();
  250. }
  251. /* 显示事件 */
  252. void DialogBase::showEvent(QShowEvent *event)
  253. {
  254. QDialog::showEvent(event);
  255. /* 重新布局顶栏 */
  256. layoutTop();
  257. }
  258. /* 重新设置大小 */
  259. void DialogBase::resizeEvent(QResizeEvent *event)
  260. {
  261. QDialog::resizeEvent(event);
  262. /* 重新布局顶栏 */
  263. layoutTop();
  264. }
  265. /* 事件过滤器 */
  266. bool DialogBase::eventFilter(QObject *watched, QEvent *event)
  267. {
  268. if(watched == m_pBtn_Close && event->type() == QEvent::HoverEnter)
  269. {
  270. m_pBtn_Close->setProperty("Hover", true);
  271. m_pBtn_Close->style()->unpolish(m_pBtn_Close);
  272. m_pBtn_Close->style()->polish(m_pBtn_Close);
  273. m_pBtn_Close->update();
  274. return true;
  275. }
  276. else if(watched == m_pBtn_Close && event->type() == QEvent::HoverLeave)
  277. {
  278. m_pBtn_Close->setProperty("Hover", false);
  279. m_pBtn_Close->style()->unpolish(m_pBtn_Close);
  280. m_pBtn_Close->style()->polish(m_pBtn_Close);
  281. m_pBtn_Close->update();
  282. return true;
  283. }
  284. return QDialog::eventFilter(watched, event);
  285. }