DialogBase.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <QGuiApplication>
  10. #include <QScreen>
  11. #include "OneShadowEffect.h"
  12. DialogBase::DialogBase(QWidget *parent)
  13. : QDialog(parent)
  14. {
  15. m_logger = spdlog::get("ACASetting");
  16. if(m_logger == nullptr)
  17. {
  18. fmt::print("DialogBase: 未发现ACASetting日志记录器,请检查日志记录器是否已初始化。\n");
  19. return;
  20. }
  21. initUI();
  22. initSettings();
  23. /* 设置qss */
  24. connect(&UIStyle, &UIStyleManager::signal_qssChanged, this, &DialogBase::do_setQSS);
  25. /* 设置默认的UI */
  26. setParentQSS();
  27. }
  28. DialogBase::DialogBase(bool isFullScreen, QWidget *parent)
  29. : QDialog(parent), m_isFullScreen(isFullScreen)
  30. {
  31. m_logger = spdlog::get("ACASetting");
  32. if(m_logger == nullptr)
  33. {
  34. fmt::print("DialogBase: 未发现ACASetting日志记录器,请检查日志记录器是否已初始化。\n");
  35. return;
  36. }
  37. initUI();
  38. initSettings();
  39. /* 设置qss */
  40. connect(&UIStyle, &UIStyleManager::signal_qssChanged, this, &DialogBase::do_setQSS);
  41. /* 设置默认的UI */
  42. setParentQSS();
  43. }
  44. DialogBase::~DialogBase()
  45. {
  46. }
  47. /* 设置标题 */
  48. void DialogBase::setTitle(const QString &title, QSize size)
  49. {
  50. m_labelTitle->setText(title);
  51. m_labelTitle->setFixedSize(size);
  52. }
  53. /* 获取标题 */
  54. QString DialogBase::getTitle() const
  55. {
  56. return m_labelTitle ? m_labelTitle->text() : QString();
  57. }
  58. /* 设置内容容器 */
  59. bool DialogBase::setContentWidget(QWidget *widget)
  60. {
  61. if(widget == nullptr)
  62. {
  63. SPDLOG_LOGGER_ERROR(m_logger, "设置内容容器失败,widget为nullptr");
  64. return false;
  65. }
  66. if(m_widgetContent == widget)
  67. {
  68. return true; // 如果已经是这个widget了,就不需要重新设置
  69. }
  70. if(m_widgetContent != nullptr)
  71. {
  72. m_layoutBackground->removeWidget(m_widgetContent);
  73. delete m_widgetContent;
  74. m_widgetContent = nullptr;
  75. }
  76. m_widgetContent = widget;
  77. m_layoutBackground->insertWidget(1, m_widgetContent);
  78. return true;
  79. }
  80. /* 设置低栏容器 */
  81. bool DialogBase::setBottomWidget(QWidget *widget)
  82. {
  83. if(widget == nullptr)
  84. {
  85. SPDLOG_LOGGER_ERROR(m_logger, "设置底部容器失败,widget为nullptr");
  86. return false;
  87. }
  88. if(m_widgetBottom != nullptr)
  89. {
  90. m_layoutBackground->removeWidget(m_widgetBottom);
  91. delete m_widgetBottom;
  92. m_widgetBottom = nullptr;
  93. }
  94. m_widgetBottom = widget;
  95. m_layoutBackground->insertWidget(2, m_widgetBottom);
  96. return true;
  97. }
  98. /* 移除底栏 */
  99. bool DialogBase::removeBottomWidget()
  100. {
  101. if(m_widgetBottom == nullptr)
  102. {
  103. return true;
  104. }
  105. m_layoutBackground->removeWidget(m_widgetBottom);
  106. delete m_widgetBottom;
  107. m_widgetBottom = nullptr;
  108. return true;
  109. }
  110. /* 点击确定按钮之前执行的操作 */
  111. bool DialogBase::isOKClicked()
  112. {
  113. /* 默认返回true,子类可以重载这个函数来实现自定义的逻辑 */
  114. return true;
  115. }
  116. /* 设置一个控件报警,边框显示红色
  117. * 注意:这个功能需要在qss里设置属性[Warn=true],并实现相应的报警样式 */
  118. void DialogBase::setWarn(QWidget* widget, bool isWarn)
  119. {
  120. if(widget == nullptr)
  121. {
  122. SPDLOG_LOGGER_ERROR(m_logger, "设置控件报警失败,widget为nullptr");
  123. return;
  124. }
  125. widget->setProperty("Warn", isWarn);
  126. widget->style()->unpolish(widget);
  127. widget->style()->polish(widget);
  128. widget->update();
  129. }
  130. /* 初始化UI */
  131. void DialogBase::initUI()
  132. {
  133. /*--------------------- 设置对话框的属性 -----------------------*/
  134. this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
  135. this->setAttribute(Qt::WA_TranslucentBackground, true);
  136. // this->setObjectName("DialogBase");
  137. /*--------------------- 初始化背景容器 -----------------------*/
  138. /* 如果需要背后接近透明的全屏遮罩,就设置为true,在this和m_widgetBackground之间添加
  139. * 一层接近透明的遮罩 */
  140. if(m_isFullScreen)
  141. {
  142. auto screenRect = QGuiApplication::screenAt(QCursor::pos())->geometry();
  143. this->resize(screenRect.width(), screenRect.height());
  144. /* 创建最外层的透明背景 */
  145. m_widgetTransparent = new QWidget(this);
  146. m_widgetTransparent->setObjectName("widget_transparent");
  147. m_widgetTransparent->setStyleSheet(R"(
  148. QWidget#widget_transparent {background: rgba(0, 0, 0, 0.01)};
  149. )");
  150. m_widgetTransparent->resize(screenRect.width(), screenRect.height());
  151. m_widgetTransparent->move(0, 0);
  152. }
  153. /* 创建真正的容器 */
  154. m_widgetBackground = new QWidget(this);
  155. m_widgetBackground->setObjectName("widget_content__");
  156. // m_widgetBackground->resize(400, 300);
  157. /* 判断m_widgetBackground是哪一层 */
  158. if(m_isFullScreen)
  159. {
  160. m_widgetBackground->setParent(m_widgetTransparent);
  161. /* 设置设置区域居中显示 */
  162. m_widgetBackground->move(m_widgetTransparent->width() / 2 - m_widgetBackground->width() / 2,
  163. m_widgetTransparent->height() / 2 - m_widgetBackground->height() / 2);
  164. }else {
  165. QVBoxLayout* layout = new QVBoxLayout(this);
  166. /* 设置边距,就是阴影的宽度 */
  167. layout->setContentsMargins(20, 20, 20, 20);
  168. layout->setSpacing(0);
  169. this->setLayout(layout);
  170. layout->addWidget(m_widgetBackground);
  171. }
  172. /* 创建背景容器内部的布局 */
  173. m_layoutBackground = new QVBoxLayout(m_widgetBackground);
  174. m_layoutBackground->setContentsMargins(0, 0, 0, 0);
  175. m_layoutBackground->setSpacing(0);
  176. m_widgetBackground->setLayout(m_layoutBackground);
  177. /*--------------------- 初始顶栏 -----------------------*/
  178. /* 初始化顶部标题栏,高度56,下面分割线高度1 */
  179. m_widgetTop = new QWidget(this);
  180. m_widgetTop->setObjectName("widget_top");
  181. m_widgetTop->setFixedHeight(56);
  182. m_layoutBackground->addWidget(m_widgetTop);
  183. QHBoxLayout* topHBoxLayout = new QHBoxLayout(m_widgetTop);
  184. m_widgetTop->setLayout(topHBoxLayout);
  185. /* 初始化标题栏 */
  186. m_labelTitle = new QLabel(m_widgetTop);
  187. m_labelTitle->setObjectName("label_title");
  188. m_labelTitle->setText("Dialog Title");
  189. m_labelTitle->setFixedSize(120, 18);
  190. m_labelTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  191. /* 初始化关闭按钮 */
  192. m_pBtn_Close = new QPushButton(m_widgetTop);
  193. m_pBtn_Close->setObjectName("pBtn_Close");
  194. m_pBtn_Close->resize(32, 32);
  195. /* 安装事件过滤器,主要是处理鼠标悬停事件 */
  196. m_pBtn_Close->installEventFilter(this);
  197. /* 重新布局顶栏 */
  198. layoutTop();
  199. /*--------------------- 初始化内容栏 -----------------------*/
  200. // m_widgetContent = new QWidget(this);
  201. // m_widgetContent->resize(400, 300);
  202. // m_layoutBackground->addWidget(m_widgetContent);
  203. /*--------------------- 初始底栏 -----------------------*/
  204. m_widgetBottom = new QWidget(this);
  205. m_widgetBottom->setObjectName("widget_bottom");
  206. m_widgetBottom->setFixedHeight(88);
  207. m_layoutBackground->addWidget(m_widgetBottom);
  208. QHBoxLayout* bottomHBoxLayout = new QHBoxLayout(m_widgetBottom);
  209. m_widgetBottom->setLayout(bottomHBoxLayout);
  210. /* 初始化取消按钮 */
  211. m_pBtn_Cancel = new QPushButton(m_widgetBottom);
  212. m_pBtn_Cancel->setObjectName("pBtn_Cancel");
  213. m_pBtn_Cancel->setText("取消");
  214. m_pBtn_Cancel->setFixedSize(60, 32);
  215. bottomHBoxLayout->addWidget(m_pBtn_Cancel, 0, Qt::AlignRight | Qt::AlignVCenter);
  216. /* 初始化确认按钮 */
  217. m_pBtn_OK = new QPushButton(m_widgetBottom);
  218. m_pBtn_OK->setObjectName("pBtn_OK");
  219. m_pBtn_OK->setText("确定");
  220. m_pBtn_OK->setFixedSize(60, 32);
  221. bottomHBoxLayout->addWidget(m_pBtn_OK, 0, Qt::AlignRight | Qt::AlignVCenter);
  222. /* 添加一个底部横向弹簧 */
  223. bottomHBoxLayout->insertStretch(0, 1);
  224. /* 设置底部按钮的间距 */
  225. bottomHBoxLayout->setContentsMargins(32, 24, 32, 32);
  226. bottomHBoxLayout->setSpacing(16);
  227. /*--------------------- 创建阴影 -----------------------*/
  228. auto pShadow = new OneShadowEffect(this);
  229. m_widgetBackground->setGraphicsEffect(pShadow);
  230. }
  231. /* 初始化其他设置 */
  232. void DialogBase::initSettings()
  233. {
  234. /* 初始化变量 */
  235. m_lastPos = QPoint(0, 0);
  236. /* 连接信号和槽 */
  237. connect(m_pBtn_Close, &QPushButton::clicked, this, &DialogBase::do_pBtn_Close_Clicked);
  238. connect(m_pBtn_OK, &QPushButton::clicked, this, &DialogBase::do_pBtn_OK_Clicked);
  239. connect(m_pBtn_Cancel, &QPushButton::clicked, this, &DialogBase::do_pBtnCancel_clicked);
  240. /* 设置样式表 */
  241. setParentQSS();
  242. }
  243. /* 加载QSS */
  244. void DialogBase::setParentQSS()
  245. {
  246. /* 获取样式表路径 */
  247. QString qssPath = UIStyle.getQSSPath() + "/dialogbase.qss";
  248. QFile file(qssPath);
  249. if(file.open(QFile::ReadOnly))
  250. {
  251. QString qss = file.readAll();
  252. file.close();
  253. m_widgetBackground->setStyleSheet(qss);
  254. } else
  255. {
  256. SPDLOG_LOGGER_ERROR(m_logger, "打开QSS文件失败: {}", qssPath.toStdString());
  257. SPDLOG_LOGGER_ERROR(m_logger, "错误信息: {}", file.errorString().toStdString());
  258. }
  259. }
  260. /* 设置top栏的位置布局,标题和关闭按钮的高度起始位置不一样,不方便使用布局,所有手动修改坐标
  261. * 标题位置32,18,高度18
  262. * 关闭按钮右对齐16,y12,大小32x32 */
  263. void DialogBase::layoutTop()
  264. {
  265. m_labelTitle->move(32, 18);
  266. int x = m_widgetTop->width() - m_pBtn_Close->width() - 16;
  267. m_pBtn_Close->move(x, 12);
  268. }
  269. /* 确认按钮点击事件 */
  270. void DialogBase::do_pBtn_OK_Clicked()
  271. {
  272. if(!isOKClicked())
  273. {
  274. /* 如果子类没有重载这个函数,默认返回true */
  275. return;
  276. }
  277. m_isOK = true;
  278. this->accept();
  279. }
  280. /* 设置QSS */
  281. void DialogBase::do_setQSS(EUIStyle style)
  282. {
  283. setParentQSS();
  284. }
  285. /* 重写鼠标按下事件 */
  286. void DialogBase::mousePressEvent(QMouseEvent *event)
  287. {
  288. m_lastPos = event->globalPos();
  289. event->accept();
  290. }
  291. /* 重写鼠标移动事件 */
  292. void DialogBase::mouseMoveEvent(QMouseEvent *event)
  293. {
  294. auto point = m_widgetTop->mapToGlobal(QPoint(0, 0));
  295. QRect rect(point, m_widgetTop->size());
  296. if(!rect.contains(m_lastPos))
  297. {
  298. event->accept();
  299. return;
  300. }
  301. int dx = event->globalX() - m_lastPos.x();
  302. int dy = event->globalY() - m_lastPos.y();
  303. if(m_isFullScreen)
  304. {
  305. m_widgetBackground->move(m_widgetBackground->x() + dx, m_widgetBackground->y() + dy);
  306. }else {
  307. move(x() + dx, y() + dy);
  308. }
  309. m_lastPos = event->globalPos();
  310. event->accept();
  311. }
  312. /* 重写鼠标释放事件 */
  313. void DialogBase::mouseReleaseEvent(QMouseEvent *event)
  314. {
  315. event->accept();
  316. }
  317. /* 显示事件 */
  318. void DialogBase::showEvent(QShowEvent *event)
  319. {
  320. QDialog::showEvent(event);
  321. if(m_isFullScreen)
  322. {
  323. /* 设置设置区域居中显示 */
  324. m_widgetBackground->move(m_widgetTransparent->width() / 2 - m_widgetBackground->width() / 2,
  325. m_widgetTransparent->height() / 2 - m_widgetBackground->height() / 2);
  326. }
  327. /* 重新布局顶栏 */
  328. layoutTop();
  329. }
  330. /* 重新设置大小 */
  331. void DialogBase::resizeEvent(QResizeEvent *event)
  332. {
  333. QDialog::resizeEvent(event);
  334. if(m_isFullScreen)
  335. {
  336. /* 设置设置区域居中显示 */
  337. m_widgetBackground->move(m_widgetTransparent->width() / 2 - m_widgetBackground->width() / 2,
  338. m_widgetTransparent->height() / 2 - m_widgetBackground->height() / 2);
  339. }
  340. /* 重新布局顶栏 */
  341. layoutTop();
  342. }
  343. /* 事件过滤器 */
  344. bool DialogBase::eventFilter(QObject *watched, QEvent *event)
  345. {
  346. if(watched == m_pBtn_Close && event->type() == QEvent::HoverEnter)
  347. {
  348. m_pBtn_Close->setProperty("Hover", true);
  349. m_pBtn_Close->style()->unpolish(m_pBtn_Close);
  350. m_pBtn_Close->style()->polish(m_pBtn_Close);
  351. m_pBtn_Close->update();
  352. return true;
  353. }
  354. else if(watched == m_pBtn_Close && event->type() == QEvent::HoverLeave)
  355. {
  356. m_pBtn_Close->setProperty("Hover", false);
  357. m_pBtn_Close->style()->unpolish(m_pBtn_Close);
  358. m_pBtn_Close->style()->polish(m_pBtn_Close);
  359. m_pBtn_Close->update();
  360. return true;
  361. }
  362. return QDialog::eventFilter(watched, event);
  363. }