Debian Codegraph @

src/app/view/formatdialog.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: GPL-3.0-only
4
5 #include "formatdialog.h"
6
7 #include <DFontSizeManager>
8 #include <QEvent>
9 #include <QFontMetrics>
10 #include <QDebug>
11 #include <QMessageBox>
12
13 NewStr autoCutText(const QString &text, DLabel *pDesLbl)
14 {
15 if (text.isEmpty() || nullptr == pDesLbl) {
16 return NewStr();
17 }
18
19 QFont font; // 应用使用字体对象
20 QFontMetrics font_label(font);
21 QString strText = text;
22 int titlewidth = font_label.width(strText);
23 QString str;
24 NewStr newstr;
25 int width = pDesLbl->width();
26 if (titlewidth < width) {
27 newstr.strList.append(strText);
28 newstr.resultStr += strText;
29 } else {
30 for (int i = 0; i < strText.count(); i++) {
31 str += strText.at(i);
32
33 if (font_label.width(str) > width) { //根据label宽度调整每行字符数
34 str.remove(str.count() - 1, 1);
35 newstr.strList.append(str);
36 newstr.resultStr += str + "\n";
37 str.clear();
38 --i;
39 }
40 }
41 newstr.strList.append(str);
42 newstr.resultStr += str;
43 }
44 newstr.fontHeifht = font_label.height();
45 return newstr;
46 }
47
48
49 FormatDialog::FormatDialog(QWidget *parent)
50 : DDialog(parent)
51 {
52 setFixedWidth(380);
53 // 设置对话框图标
54 setIcon(QMessageBox::standardIcon(QMessageBox::Icon::Warning));
55 }
56
57 FormatDialog::~FormatDialog()
58 {
59
60 }
61
62 void FormatDialog::initDialog(const QString &titleText, const QString &strFormatText, const QString btnMsg1, DDialog::ButtonType btnType1, const QString btnMsg2, DDialog::ButtonType btnType2)
63 {
64 // 设置title
65 if (!titleText.isEmpty())
66 setTitle(titleText);
67 m_strFormatText = strFormatText;
68 // 描述内容
69 DLabel *pFormatLbl = new DLabel(this);
70 pFormatLbl->setObjectName("ContentLabel");
71 pFormatLbl->setFixedWidth(340);
72 DFontSizeManager::instance()->bind(pFormatLbl, DFontSizeManager::T6, QFont::Medium);
73 pFormatLbl->setText(strFormatText);
74 pFormatLbl->setAlignment(Qt::AlignCenter);
75
76 // 按钮
77 if (btnMsg2.isEmpty()) {
78 addButton(btnMsg1, false, btnType1);
79 } else {
80 addButton(btnMsg1, false, btnType1);
81 addButton(btnMsg2, true, btnType2);
82 }
83 addContent(pFormatLbl, Qt::AlignHCenter); // 使用Qt::AlignHCenter效果最好
84
85 autoFeed(pFormatLbl);
86 // 这里主要是对1050dtk对DDialog的改动进行的微调,给pFormatLbl足够的空间做伸展。bug#105285
87 m_iDialogOldHeight = this->height() + 10;
88 }
89
90
91 void FormatDialog::autoFeed(DLabel *label)
92 {
93 NewStr newstr = autoCutText(m_strFormatText, label);
94 label->setText(newstr.resultStr);
95 int height_lable = newstr.strList.size() * newstr.fontHeifht;
96 label->setFixedHeight(height_lable);
97 if (0 == m_iLabelOldHeight) { // 第一次exec自动调整
98 adjustSize();
99 } else {
100 setFixedHeight(m_iDialogOldHeight - m_iLabelOldHeight + height_lable); //字号变化后自适应调整
101 }
102 m_iLabelOldHeight = height_lable;
103 m_iDialogOldHeight = height();
104 }
105
106 void FormatDialog::changeEvent(QEvent *event)
107 {
108 if (event->type() == QEvent::FontChange) {
109 Dtk::Widget::DLabel *p = findChild<Dtk::Widget::DLabel *>("ContentLabel");
110 if (nullptr != p) {
111 autoFeed(p);
112 }
113 }
114 DDialog::changeEvent(event);
115 }
116