| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef KMESSAGEBOX_H |
| 24 |
#define KMESSAGEBOX_H |
| 25 |
|
| 26 |
#include "gui_g.h" |
| 27 |
#include "kdialog.h" |
| 28 |
#include <QAbstractButton> |
| 29 |
#include <QCheckBox> |
| 30 |
#include <QDialog> |
| 31 |
|
| 32 |
namespace kdk |
| 33 |
{ |
| 34 |
|
| 35 |
/** |
| 36 |
* @defgroup DialogBoxModule |
| 37 |
*/ |
| 38 |
class KMessageBoxPrivate; |
| 39 |
|
| 40 |
/** |
| 41 |
* @brief 继承自KDialog,提供一个可自定义设置按钮和图标的对话框,类似QMessageBox |
| 42 |
* @since 2.0.2.1 |
| 43 |
*/ |
| 44 |
class GUI_EXPORT KMessageBox : public KDialog |
| 45 |
{ |
| 46 |
Q_OBJECT |
| 47 |
public: |
| 48 |
/** |
| 49 |
* @brief The Icon enum,icon的五种类型,与 QMessageDialogOptions::Icon 保持同步 |
| 50 |
*/ |
| 51 |
enum Icon { |
| 52 |
// keep this in sync with QMessageDialogOptions::Icon |
| 53 |
NoIcon = 0, |
| 54 |
Information = 1, |
| 55 |
Warning = 2, |
| 56 |
Critical = 3, |
| 57 |
Question = 4, |
| 58 |
}; |
| 59 |
Q_ENUM(Icon) |
| 60 |
|
| 61 |
/** |
| 62 |
* @brief The ButtonRole enum,可供设置的按钮role |
| 63 |
*/ |
| 64 |
enum ButtonRole { |
| 65 |
InvalidRole = -1, |
| 66 |
AcceptRole, |
| 67 |
RejectRole, |
| 68 |
DestructiveRole, |
| 69 |
ActionRole, |
| 70 |
HelpRole, |
| 71 |
YesRole, |
| 72 |
NoRole, |
| 73 |
ResetRole, |
| 74 |
ApplyRole, |
| 75 |
NRoles |
| 76 |
}; |
| 77 |
|
| 78 |
/** |
| 79 |
* @brief The StandardButton enum,可供添加的按钮类型,与 QDialogButtonBox::StandardButton |
| 80 |
* 和 QPlatformDialogHelper::StandardButton 保持同步 |
| 81 |
*/ |
| 82 |
enum StandardButton { |
| 83 |
// keep this in sync with QDialogButtonBox::StandardButton and QPlatformDialogHelper::StandardButton |
| 84 |
NoButton = 0x00000000, |
| 85 |
Ok = 0x00000400, |
| 86 |
Save = 0x00000800, |
| 87 |
SaveAll = 0x00001000, |
| 88 |
Open = 0x00002000, |
| 89 |
Yes = 0x00004000, |
| 90 |
YesToAll = 0x00008000, |
| 91 |
No = 0x00010000, |
| 92 |
NoToAll = 0x00020000, |
| 93 |
Abort = 0x00040000, |
| 94 |
Retry = 0x00080000, |
| 95 |
Ignore = 0x00100000, |
| 96 |
Close = 0x00200000, |
| 97 |
Cancel = 0x00400000, |
| 98 |
Discard = 0x00800000, |
| 99 |
Help = 0x01000000, |
| 100 |
Apply = 0x02000000, |
| 101 |
Reset = 0x04000000, |
| 102 |
RestoreDefaults = 0x08000000, |
| 103 |
|
| 104 |
FirstButton = Ok, // internal |
| 105 |
LastButton = RestoreDefaults, // internal |
| 106 |
|
| 107 |
YesAll = YesToAll, // obsolete |
| 108 |
NoAll = NoToAll, // obsolete |
| 109 |
|
| 110 |
Default = 0x00000100, // obsolete |
| 111 |
Escape = 0x00000200, // obsolete |
| 112 |
FlagMask = 0x00000300, // obsolete |
| 113 |
ButtonMask = ~FlagMask // obsolete |
| 114 |
}; |
| 115 |
typedef StandardButton Button; |
| 116 |
|
| 117 |
Q_DECLARE_FLAGS(StandardButtons, StandardButton) |
| 118 |
Q_FLAG(StandardButtons) |
| 119 |
|
| 120 |
KMessageBox(QWidget *parent = nullptr); |
| 121 |
~KMessageBox(); |
| 122 |
|
| 123 |
/** |
| 124 |
* @brief 自定义KMessageBox的提示图标 |
| 125 |
* @param icon |
| 126 |
*/ |
| 127 |
void setCustomIcon(const QIcon &icon); |
| 128 |
|
| 129 |
/** |
| 130 |
* @brief 添加一个自定义按钮 |
| 131 |
* @param button |
| 132 |
* @param role |
| 133 |
*/ |
| 134 |
void addButton(QAbstractButton *button, ButtonRole role); |
| 135 |
|
| 136 |
/** |
| 137 |
* @brief 添加设置好文本的按钮 |
| 138 |
* @param text |
| 139 |
* @param role |
| 140 |
* @return |
| 141 |
*/ |
| 142 |
QPushButton *addButton(const QString &text, ButtonRole role); |
| 143 |
|
| 144 |
/** |
| 145 |
* @brief 添加一个标准按钮并且返回这个按钮 |
| 146 |
* @param button |
| 147 |
* @return |
| 148 |
*/ |
| 149 |
QPushButton *addButton(StandardButton button); |
| 150 |
|
| 151 |
/** |
| 152 |
* @brief 移除一个按钮 |
| 153 |
* @param button |
| 154 |
*/ |
| 155 |
void removeButton(QAbstractButton *button); |
| 156 |
|
| 157 |
/** |
| 158 |
* @brief 返回与标准按钮对应的指针,如果此消息框中不存在标准按钮,则返回0。 |
| 159 |
* @param which |
| 160 |
* @return |
| 161 |
*/ |
| 162 |
QAbstractButton *button(StandardButton which) const; |
| 163 |
|
| 164 |
/** |
| 165 |
* @brief 返回已添加到消息框中的所有按钮的列表 |
| 166 |
* @return |
| 167 |
*/ |
| 168 |
QList<QAbstractButton *> buttons() const; |
| 169 |
|
| 170 |
/** |
| 171 |
* @brief 返回指定按钮的按钮角色,如果按钮为0或尚未添加到消息框中,此函数将返回InvalidRole |
| 172 |
* @param button |
| 173 |
* @return |
| 174 |
*/ |
| 175 |
KMessageBox::ButtonRole buttonRole(QAbstractButton *button) const; |
| 176 |
|
| 177 |
/** |
| 178 |
* @brief 返回KMessageBox中显示的复选框 |
| 179 |
* @return |
| 180 |
*/ |
| 181 |
QCheckBox *checkBox() const; |
| 182 |
|
| 183 |
/** |
| 184 |
* @brief 设置KMessageBox显示的复选框,未设置则为0 |
| 185 |
* @param cb |
| 186 |
*/ |
| 187 |
void setCheckBox(QCheckBox *cb); |
| 188 |
|
| 189 |
/** |
| 190 |
* @brief 获取KMessageBox的文本 |
| 191 |
* @return |
| 192 |
*/ |
| 193 |
QString text() const; |
| 194 |
|
| 195 |
/** |
| 196 |
* @brief 设置KMessageBox的文本 |
| 197 |
* @param text |
| 198 |
*/ |
| 199 |
void setText(const QString &text); |
| 200 |
|
| 201 |
/** |
| 202 |
* @brief 获取KMessageBox信息性文本的描述 |
| 203 |
* @return |
| 204 |
*/ |
| 205 |
QString informativeText() const; |
| 206 |
|
| 207 |
/** |
| 208 |
* @brief 设置KMessageBox信息性文本的描述 |
| 209 |
* @param text |
| 210 |
*/ |
| 211 |
void setInformativeText(const QString &text); |
| 212 |
|
| 213 |
/** |
| 214 |
* @brief 获取KMessageBox的图标 |
| 215 |
* @return |
| 216 |
*/ |
| 217 |
Icon icon() const; |
| 218 |
|
| 219 |
/** |
| 220 |
* @brief 设置KMessageBox的图标 |
| 221 |
* @param icon |
| 222 |
*/ |
| 223 |
void setIcon(Icon icon); |
| 224 |
|
| 225 |
/** |
| 226 |
* @brief 返回当前KMessageBox的icon |
| 227 |
* @return |
| 228 |
*/ |
| 229 |
QPixmap iconPixmap() const; |
| 230 |
|
| 231 |
/** |
| 232 |
* @brief 设置当前KMessageBox的icon |
| 233 |
* @param pixmap |
| 234 |
*/ |
| 235 |
void setIconPixmap(const QPixmap &pixmap); |
| 236 |
|
| 237 |
/** |
| 238 |
* @brief KMessageBox中标准按钮的集合 |
| 239 |
* @return |
| 240 |
*/ |
| 241 |
KMessageBox::StandardButtons standardButtons() const; |
| 242 |
|
| 243 |
/** |
| 244 |
* @brief 设置多个标准按钮 |
| 245 |
* @param buttons |
| 246 |
*/ |
| 247 |
void setStandardButtons(KMessageBox::StandardButtons buttons); |
| 248 |
|
| 249 |
/** |
| 250 |
* @brief 返回与给定按钮对应的标准按钮枚举值,如果给定按钮不是标准按钮,则返回NoButton |
| 251 |
* @param button |
| 252 |
* @return |
| 253 |
*/ |
| 254 |
KMessageBox::StandardButton standardButton(QAbstractButton *button) const; |
| 255 |
|
| 256 |
/** |
| 257 |
* @brief 返回KMessageBox的默认按钮 |
| 258 |
* @return |
| 259 |
*/ |
| 260 |
QPushButton *defaultButton() const; |
| 261 |
|
| 262 |
/** |
| 263 |
* @brief 设置KMessageBox的默认按钮 |
| 264 |
* @param button |
| 265 |
*/ |
| 266 |
void setDefaultButton(QPushButton *button); |
| 267 |
|
| 268 |
/** |
| 269 |
* @brief 设置KMessageBox的默认按钮 |
| 270 |
* @param button |
| 271 |
*/ |
| 272 |
void setDefaultButton(KMessageBox::StandardButton button); |
| 273 |
|
| 274 |
/** |
| 275 |
* @brief 返回被点击的按钮 |
| 276 |
* @return |
| 277 |
*/ |
| 278 |
QAbstractButton *clickedButton() const; |
| 279 |
|
| 280 |
/** |
| 281 |
* @brief 返回用于标准图标的pixmap。 |
| 282 |
* @param icon |
| 283 |
* @return |
| 284 |
*/ |
| 285 |
static QPixmap standardIcon(Icon icon); |
| 286 |
|
| 287 |
/** |
| 288 |
* @brief 打开带有给定标题和文本的信息消息框。对话框最多可以有三个按钮 |
| 289 |
* @param parent |
| 290 |
* @param title |
| 291 |
* @param text |
| 292 |
* @param buttons |
| 293 |
* @param defaultButton |
| 294 |
* @return |
| 295 |
*/ |
| 296 |
static StandardButton information(QWidget *parent, const QString &title, |
| 297 |
const QString &text, StandardButtons buttons = Ok, |
| 298 |
StandardButton defaultButton = NoButton); |
| 299 |
|
| 300 |
/** |
| 301 |
* @brief 打开一个带有给定标题和文本的问题消息框 |
| 302 |
* @param parent |
| 303 |
* @param title |
| 304 |
* @param text |
| 305 |
* @param buttons |
| 306 |
* @param defaultButton |
| 307 |
* @return |
| 308 |
*/ |
| 309 |
static StandardButton question(QWidget *parent, const QString &title, |
| 310 |
const QString &text, StandardButtons buttons = StandardButtons(Yes | No), |
| 311 |
StandardButton defaultButton = NoButton); |
| 312 |
/** |
| 313 |
* @brief 打开一个带有给定标题和文本的警告消息框 |
| 314 |
* @param parent |
| 315 |
* @param title |
| 316 |
* @param text |
| 317 |
* @param buttons |
| 318 |
* @param defaultButton |
| 319 |
* @return |
| 320 |
*/ |
| 321 |
static StandardButton warning(QWidget *parent, const QString &title, |
| 322 |
const QString &text, StandardButtons buttons = Ok, |
| 323 |
StandardButton defaultButton = NoButton); |
| 324 |
/** |
| 325 |
* @brief 用给定的标题和文本打开一个关键信息框 |
| 326 |
* @param parent |
| 327 |
* @param title |
| 328 |
* @param text |
| 329 |
* @param buttons |
| 330 |
* @param defaultButton |
| 331 |
* @return |
| 332 |
*/ |
| 333 |
static StandardButton critical(QWidget *parent, const QString &title, |
| 334 |
const QString &text, StandardButtons buttons = Ok, |
| 335 |
StandardButton defaultButton = NoButton); |
| 336 |
/** |
| 337 |
* @brief 用给定的标题和文本打开一个成功消息框 |
| 338 |
* @param parent |
| 339 |
* @param title |
| 340 |
* @param text |
| 341 |
* @param buttons |
| 342 |
* @param defaultButton |
| 343 |
* @return |
| 344 |
*/ |
| 345 |
static StandardButton success(QWidget *parent, const QString &title, |
| 346 |
const QString &text, StandardButtons buttons = Ok, |
| 347 |
StandardButton defaultButton = NoButton); |
| 348 |
|
| 349 |
/** |
| 350 |
* @brief 返回informativeLabel |
| 351 |
* @return |
| 352 |
* @since 3.0.1.0 |
| 353 |
*/ |
| 354 |
QLabel *informativeLabel(); |
| 355 |
|
| 356 |
Q_SIGNALS: |
| 357 |
void buttonClicked(QAbstractButton *button); |
| 358 |
|
| 359 |
protected: |
| 360 |
bool event(QEvent *e) override; |
| 361 |
|
| 362 |
private: |
| 363 |
Q_DECLARE_PRIVATE(KMessageBox) |
| 364 |
KMessageBoxPrivate *const d_ptr; |
| 365 |
}; |
| 366 |
|
| 367 |
} |
| 368 |
#endif // KMESSAGEBOX_H |