forked from luebking/qarma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmessagebox.cpp
139 lines (128 loc) · 4 KB
/
gmessagebox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "gmessagebox.h"
#include "dapplication.h"
#include <QStyle>
#include <QPixmap>
#include <QLabel>
#include <QDebug>
DWIDGET_USE_NAMESPACE
GMessageBox::GMessageBox(QWidget *parent): DDialog(parent)
{
}
GMessageBox::GMessageBox(QWidget *parent,
const QString &title,
const QString &text): DDialog(parent)
{
this->setText(text);
this->setTitle(title);
}
GMessageBox::Icon GMessageBox::icon() const
{
return nowIcon;
}
void GMessageBox::setIcon(Icon icon)
{
enum QStyle::StandardPixmap dialogIcon;
nowIcon = icon;
switch(icon)
{
case Icon::NoIcon:
return;
case Icon::Critical:
dialogIcon = (enum QStyle::StandardPixmap)11;
break;
case Icon::Information:
dialogIcon = (enum QStyle::StandardPixmap)9;
break;
case Icon::Question:
dialogIcon = (enum QStyle::StandardPixmap)12;
break;
case Icon::Warning:
dialogIcon = (enum QStyle::StandardPixmap)10;
break;
}
// 获取设备像素比 (适配高分屏)
qreal scaleFactor = this->devicePixelRatioF();
int iconSize = static_cast<int>(64 * scaleFactor); // 基准大小为64px,根据比例调整
QPixmap pixmap = DApplication::style()->standardIcon(dialogIcon).pixmap(iconSize, iconSize);
pixmap.setDevicePixelRatio(scaleFactor); // 设置像素比以确保显示清晰
this->setIconPixmap(pixmap);
this->setWindowIcon(DApplication::style()->standardIcon(dialogIcon));
}
void GMessageBox::setStandardButtonsWithList(QList<StandardButtons> buttons)
{
QStringList buttonsList;
for(StandardButtons i: buttons) {
switch(i) {
case StandardButton::Ok:
buttonsList << tr("OK");
break;
case StandardButton::Save:
buttonsList << tr("Save");
break;
case StandardButton::SaveAll:
buttonsList << tr("Save All");
break;
case StandardButton::Open:
buttonsList << tr("Open");
break;
case StandardButton::Yes:
buttonsList << tr("Yes");
break;
case StandardButton::YesToAll:
buttonsList << tr("Yes To All");
break;
case StandardButton::No:
buttonsList << tr("No");
break;
case StandardButton::NoToAll:
buttonsList << tr("No To All");
break;
case StandardButton::Abort:
buttonsList << tr("Abort");
break;
case StandardButton::Retry:
buttonsList << tr("Retry");
break;
case StandardButton::Ignore:
buttonsList << tr("Ignore");
break;
case StandardButton::Close:
buttonsList << tr("Close");
break;
case StandardButton::Cancel:
buttonsList << tr("Cancel");
break;
case StandardButton::Discard:
buttonsList << tr("Discard");
break;
case StandardButton::Help:
buttonsList << tr("Help");
break;
case StandardButton::Apply:
buttonsList << tr("Apply");
break;
case StandardButton::Reset:
buttonsList << tr("Reset");
break;
case StandardButton::RestoreDefaults:
buttonsList << tr("Restore Defaults");
break;
}
}
for(int i = 0; i < buttonsList.count() - 1; ++i) {
this->addButton(buttonsList.at(i));
}
this->addButton(buttonsList.at(buttonsList.length() - 1), true, ButtonType::ButtonRecommend);
}
void GMessageBox::setText(const QString &text)
{
this->setWindowTitle(text);
QLabel *label = findChild<QLabel*>("MessageLabel");
if (label) {
qDebug() << "Find Label";
label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
label->setAttribute(Qt::WA_TransparentForMouseEvents, false);
}
QString newText = text;
this->setMessage(newText.replace("\\n", "\n"));
}