DialogBase.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. setParentQSS();
  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. /* 点击确定按钮之前执行的操作 */
  93. bool DialogBase::isOKClicked()
  94. {
  95. /* 默认返回true,子类可以重载这个函数来实现自定义的逻辑 */
  96. return true;
  97. }
  98. /* 设置一个控件报警,边框显示红色
  99. * 注意:这个功能需要在qss里设置属性[Warn=true],并实现相应的报警样式 */
  100. void DialogBase::setWarn(QWidget* widget, bool isWarn)
  101. {
  102. if(widget == nullptr)
  103. {
  104. SPDLOG_LOGGER_ERROR(m_logger, "设置控件报警失败,widget为nullptr");
  105. return;
  106. }
  107. widget->setProperty("Warn", isWarn);
  108. widget->style()->unpolish(widget);
  109. widget->style()->polish(widget);
  110. widget->update();
  111. }
  112. /* 初始化UI */
  113. void DialogBase::initUI()
  114. {
  115. /*--------------------- 设置对话框的属性 -----------------------*/
  116. this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
  117. this->setAttribute(Qt::WA_TranslucentBackground, true);
  118. // this->setObjectName("DialogBase");
  119. /*--------------------- 初始化背景容器 -----------------------*/
  120. m_widgetBackground = new QWidget(this);
  121. m_widgetBackground->setObjectName("widget_content__");
  122. QVBoxLayout* layout = new QVBoxLayout(this);
  123. /* 设置边距,就是阴影的宽度 */
  124. layout->setContentsMargins(20, 20, 20, 20);
  125. layout->setSpacing(0);
  126. this->setLayout(layout);
  127. layout->addWidget(m_widgetBackground);
  128. /* 创建背景容器内部的布局 */
  129. m_layoutBackground = new QVBoxLayout(m_widgetBackground);
  130. m_layoutBackground->setContentsMargins(0, 0, 0, 0);
  131. m_layoutBackground->setSpacing(0);
  132. m_widgetBackground->setLayout(m_layoutBackground);
  133. /*--------------------- 初始顶栏 -----------------------*/
  134. /* 初始化顶部标题栏,高度56,下面分割线高度1 */
  135. m_widgetTop = new QWidget(this);
  136. m_widgetTop->setObjectName("widget_top");
  137. m_widgetTop->setFixedHeight(56);
  138. m_layoutBackground->addWidget(m_widgetTop);
  139. QHBoxLayout* topHBoxLayout = new QHBoxLayout(m_widgetTop);
  140. m_widgetTop->setLayout(topHBoxLayout);
  141. /* 初始化标题栏 */
  142. m_labelTitle = new QLabel(m_widgetTop);
  143. m_labelTitle->setObjectName("label_title");
  144. m_labelTitle->setText("Dialog Title");
  145. m_labelTitle->setFixedSize(120, 18);
  146. m_labelTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  147. /* 初始化关闭按钮 */
  148. m_pBtn_Close = new QPushButton(m_widgetTop);
  149. m_pBtn_Close->setObjectName("pBtn_Close");
  150. m_pBtn_Close->resize(32, 32);
  151. /* 安装事件过滤器,主要是处理鼠标悬停事件 */
  152. m_pBtn_Close->installEventFilter(this);
  153. /* 重新布局顶栏 */
  154. layoutTop();
  155. /*--------------------- 初始化内容栏 -----------------------*/
  156. // m_widgetContent = new QWidget(this);
  157. // m_widgetContent->resize(400, 300);
  158. // m_layoutBackground->addWidget(m_widgetContent);
  159. /*--------------------- 初始底栏 -----------------------*/
  160. m_widgetBottom = new QWidget(this);
  161. m_widgetBottom->setObjectName("widget_bottom");
  162. m_widgetBottom->setFixedHeight(88);
  163. m_layoutBackground->addWidget(m_widgetBottom);
  164. QHBoxLayout* bottomHBoxLayout = new QHBoxLayout(m_widgetBottom);
  165. m_widgetBottom->setLayout(bottomHBoxLayout);
  166. /* 初始化取消按钮 */
  167. m_pBtn_Cancel = new QPushButton(m_widgetBottom);
  168. m_pBtn_Cancel->setObjectName("pBtn_Cancel");
  169. m_pBtn_Cancel->setText("取消");
  170. m_pBtn_Cancel->setFixedSize(60, 32);
  171. bottomHBoxLayout->addWidget(m_pBtn_Cancel, 0, Qt::AlignRight | Qt::AlignVCenter);
  172. /* 初始化确认按钮 */
  173. m_pBtn_OK = new QPushButton(m_widgetBottom);
  174. m_pBtn_OK->setObjectName("pBtn_OK");
  175. m_pBtn_OK->setText("确定");
  176. m_pBtn_OK->setFixedSize(60, 32);
  177. bottomHBoxLayout->addWidget(m_pBtn_OK, 0, Qt::AlignRight | Qt::AlignVCenter);
  178. /* 添加一个底部横向弹簧 */
  179. bottomHBoxLayout->insertStretch(0, 1);
  180. /* 设置底部按钮的间距 */
  181. bottomHBoxLayout->setContentsMargins(32, 24, 32, 32);
  182. bottomHBoxLayout->setSpacing(16);
  183. /*--------------------- 设置整体背景 -----------------------*/
  184. /* 这个背景需要在初始化所有的widget控件之后再设置 */
  185. // auto screenRect = QGuiApplication::screenAt(QCursor::pos())->geometry();
  186. // this->resize(screenRect.width(), screenRect.height());
  187. // /* 设置设置区域居中显示 */
  188. // m_widgetBackground->move(screenRect.width() / 2 - m_widgetBackground->width() / 2,
  189. // screenRect.height() / 2 - m_widgetBackground->height() / 2);
  190. /*--------------------- 创建阴影 -----------------------*/
  191. auto pShadow = new OneShadowEffect(this);
  192. m_widgetBackground->setGraphicsEffect(pShadow);
  193. }
  194. /* 初始化其他设置 */
  195. void DialogBase::initSettings()
  196. {
  197. /* 初始化变量 */
  198. m_lastPos = QPoint(0, 0);
  199. /* 连接信号和槽 */
  200. connect(m_pBtn_Close, &QPushButton::clicked, this, &DialogBase::do_pBtn_Close_Clicked);
  201. connect(m_pBtn_OK, &QPushButton::clicked, this, &DialogBase::do_pBtn_OK_Clicked);
  202. connect(m_pBtn_Cancel, &QPushButton::clicked, this, &DialogBase::do_pBtnCancel_clicked);
  203. /* 设置样式表 */
  204. setParentQSS();
  205. }
  206. /* 加载QSS */
  207. void DialogBase::setParentQSS()
  208. {
  209. /* 获取样式表路径 */
  210. QString qssPath = UIStyle.getQSSPath() + "/dialogbase.qss";
  211. QFile file(qssPath);
  212. if(file.open(QFile::ReadOnly))
  213. {
  214. QString qss = file.readAll();
  215. file.close();
  216. m_widgetBackground->setStyleSheet(qss);
  217. } else
  218. {
  219. SPDLOG_LOGGER_ERROR(m_logger, "打开QSS文件失败: {}", qssPath.toStdString());
  220. SPDLOG_LOGGER_ERROR(m_logger, "错误信息: {}", file.errorString().toStdString());
  221. }
  222. }
  223. /* 设置top栏的位置布局,标题和关闭按钮的高度起始位置不一样,不方便使用布局,所有手动修改坐标
  224. * 标题位置32,18,高度18
  225. * 关闭按钮右对齐16,y12,大小32x32 */
  226. void DialogBase::layoutTop()
  227. {
  228. m_labelTitle->move(32, 18);
  229. int x = m_widgetTop->width() - m_pBtn_Close->width() - 16;
  230. m_pBtn_Close->move(x, 12);
  231. }
  232. /* 确认按钮点击事件 */
  233. void DialogBase::do_pBtn_OK_Clicked()
  234. {
  235. if(!isOKClicked())
  236. {
  237. /* 如果子类没有重载这个函数,默认返回true */
  238. return;
  239. }
  240. m_isOK = true;
  241. this->accept();
  242. }
  243. /* 设置QSS */
  244. void DialogBase::do_setQSS(EUIStyle style)
  245. {
  246. setParentQSS();
  247. }
  248. /* 重写鼠标按下事件 */
  249. void DialogBase::mousePressEvent(QMouseEvent *event)
  250. {
  251. m_lastPos = event->globalPos();
  252. event->accept();
  253. }
  254. /* 重写鼠标移动事件 */
  255. void DialogBase::mouseMoveEvent(QMouseEvent *event)
  256. {
  257. auto point = m_widgetTop->mapToGlobal(QPoint(0, 0));
  258. QRect rect(point, m_widgetTop->size());
  259. if(!rect.contains(m_lastPos))
  260. {
  261. event->accept();
  262. return;
  263. }
  264. int dx = event->globalX() - m_lastPos.x();
  265. int dy = event->globalY() - m_lastPos.y();
  266. // m_widgetBackground->move(m_widgetBackground->x() + dx, m_widgetBackground->y() + dy);
  267. move(x() + dx, y() + dy);
  268. m_lastPos = event->globalPos();
  269. event->accept();
  270. }
  271. /* 重写鼠标释放事件 */
  272. void DialogBase::mouseReleaseEvent(QMouseEvent *event)
  273. {
  274. event->accept();
  275. }
  276. /* 显示事件 */
  277. void DialogBase::showEvent(QShowEvent *event)
  278. {
  279. QDialog::showEvent(event);
  280. /* 重新布局顶栏 */
  281. layoutTop();
  282. }
  283. /* 重新设置大小 */
  284. void DialogBase::resizeEvent(QResizeEvent *event)
  285. {
  286. QDialog::resizeEvent(event);
  287. /* 重新布局顶栏 */
  288. layoutTop();
  289. }
  290. /* 事件过滤器 */
  291. bool DialogBase::eventFilter(QObject *watched, QEvent *event)
  292. {
  293. if(watched == m_pBtn_Close && event->type() == QEvent::HoverEnter)
  294. {
  295. m_pBtn_Close->setProperty("Hover", true);
  296. m_pBtn_Close->style()->unpolish(m_pBtn_Close);
  297. m_pBtn_Close->style()->polish(m_pBtn_Close);
  298. m_pBtn_Close->update();
  299. return true;
  300. }
  301. else if(watched == m_pBtn_Close && event->type() == QEvent::HoverLeave)
  302. {
  303. m_pBtn_Close->setProperty("Hover", false);
  304. m_pBtn_Close->style()->unpolish(m_pBtn_Close);
  305. m_pBtn_Close->style()->polish(m_pBtn_Close);
  306. m_pBtn_Close->update();
  307. return true;
  308. }
  309. return QDialog::eventFilter(watched, event);
  310. }