| 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 KINPUTDIALOG_H |
| 24 |
#define KINPUTDIALOG_H |
| 25 |
|
| 26 |
#include <QLineEdit> |
| 27 |
|
| 28 |
#include "kdialog.h" |
| 29 |
#include <QtWidgets/qtwidgetsglobal.h> |
| 30 |
|
| 31 |
namespace kdk |
| 32 |
{ |
| 33 |
|
| 34 |
/** @defgroup DialogBoxModule |
| 35 |
* @{ |
| 36 |
*/ |
| 37 |
|
| 38 |
class KInputDialogPrivate; |
| 39 |
|
| 40 |
/** |
| 41 |
* @brief 输入对话框,接口与 QInputDialog 相同。 |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
class GUI_EXPORT KInputDialog : public KDialog |
| 45 |
{ |
| 46 |
Q_OBJECT |
| 47 |
Q_ENUMS(InputMode InputDialogOption) |
| 48 |
QDOC_PROPERTY(InputMode inputMode READ inputMode WRITE setInputMode) |
| 49 |
QDOC_PROPERTY(QString labelText READ labelText WRITE setLabelText) |
| 50 |
QDOC_PROPERTY(InputDialogOptions options READ options WRITE setOptions) |
| 51 |
QDOC_PROPERTY(QString textValue READ textValue WRITE setTextValue NOTIFY textValueChanged) |
| 52 |
QDOC_PROPERTY(int intValue READ intValue WRITE setIntValue NOTIFY intValueChanged) |
| 53 |
QDOC_PROPERTY(int doubleValue READ doubleValue WRITE setDoubleValue NOTIFY doubleValueChanged) |
| 54 |
QDOC_PROPERTY(QLineEdit::EchoMode textEchoMode READ textEchoMode WRITE setTextEchoMode) |
| 55 |
QDOC_PROPERTY(bool comboBoxEditable READ isComboBoxEditable WRITE setComboBoxEditable) |
| 56 |
QDOC_PROPERTY(QStringList comboBoxItems READ comboBoxItems WRITE setComboBoxItems) |
| 57 |
QDOC_PROPERTY(int intMinimum READ intMinimum WRITE setIntMinimum) |
| 58 |
QDOC_PROPERTY(int intMaximum READ intMaximum WRITE setIntMaximum) |
| 59 |
QDOC_PROPERTY(int intStep READ intStep WRITE setIntStep) |
| 60 |
QDOC_PROPERTY(double doubleMinimum READ doubleMinimum WRITE setDoubleMinimum) |
| 61 |
QDOC_PROPERTY(double doubleMaximum READ doubleMaximum WRITE setDoubleMaximum) |
| 62 |
QDOC_PROPERTY(int doubleDecimals READ doubleDecimals WRITE setDoubleDecimals) |
| 63 |
QDOC_PROPERTY(QString okButtonText READ okButtonText WRITE setOkButtonText) |
| 64 |
QDOC_PROPERTY(QString cancelButtonText READ cancelButtonText WRITE setCancelButtonText) |
| 65 |
QDOC_PROPERTY(double doubleStep READ doubleStep WRITE setDoubleStep) |
| 66 |
|
| 67 |
public: |
| 68 |
enum InputDialogOption { |
| 69 |
NoButtons = 0x00000001, |
| 70 |
UseListViewForComboBoxItems = 0x00000002, |
| 71 |
UsePlainTextEditForTextInput = 0x00000004 |
| 72 |
}; |
| 73 |
|
| 74 |
Q_DECLARE_FLAGS(InputDialogOptions, InputDialogOption) |
| 75 |
|
| 76 |
enum InputMode { |
| 77 |
TextInput, |
| 78 |
IntInput, |
| 79 |
DoubleInput |
| 80 |
}; |
| 81 |
|
| 82 |
KInputDialog(QWidget *parent = nullptr); |
| 83 |
~KInputDialog(); |
| 84 |
|
| 85 |
/** |
| 86 |
* @brief 设置输入模式 |
| 87 |
* @param mode |
| 88 |
*/ |
| 89 |
void setInputMode(InputMode mode); |
| 90 |
|
| 91 |
/** |
| 92 |
* @brief 返回输入模式 |
| 93 |
* @return InputMode |
| 94 |
*/ |
| 95 |
InputMode inputMode() const; |
| 96 |
|
| 97 |
/** |
| 98 |
* @brief 设置提示信息 |
| 99 |
* @param text |
| 100 |
*/ |
| 101 |
void setLabelText(const QString &text); |
| 102 |
|
| 103 |
/** |
| 104 |
* @brief 获取提示信息 |
| 105 |
* @return QString |
| 106 |
*/ |
| 107 |
QString labelText() const; |
| 108 |
|
| 109 |
/** |
| 110 |
* @brief 设置option类型 |
| 111 |
* @param option |
| 112 |
* @param on |
| 113 |
*/ |
| 114 |
void setOption(InputDialogOption option, bool on = true); |
| 115 |
|
| 116 |
/** |
| 117 |
* @brief 返回是否设置了option类型 |
| 118 |
* @param option |
| 119 |
* @return bool |
| 120 |
*/ |
| 121 |
bool testOption(InputDialogOption option) const; |
| 122 |
|
| 123 |
/** |
| 124 |
* @brief 设置options类型 |
| 125 |
* @param options |
| 126 |
*/ |
| 127 |
void setOptions(InputDialogOptions options); |
| 128 |
/** |
| 129 |
* @brief 返回options类型 |
| 130 |
* @return InputDialogOptions |
| 131 |
*/ |
| 132 |
InputDialogOptions options() const; |
| 133 |
|
| 134 |
/** |
| 135 |
* @brief 设置文本值 |
| 136 |
* @param text |
| 137 |
*/ |
| 138 |
void setTextValue(const QString &text); |
| 139 |
/** |
| 140 |
* @brief 获取当前文本值 |
| 141 |
* @return QString |
| 142 |
*/ |
| 143 |
QString textValue() const; |
| 144 |
|
| 145 |
/** |
| 146 |
* @brief 设置保存文本值的回声模式 QLineEdit::EchoMode |
| 147 |
* @param mode |
| 148 |
*/ |
| 149 |
void setTextEchoMode(QLineEdit::EchoMode mode); |
| 150 |
/** |
| 151 |
* @brief 返回保存文本值的回声模式 |
| 152 |
* @return QLineEdit::EchoMode |
| 153 |
*/ |
| 154 |
QLineEdit::EchoMode textEchoMode() const; |
| 155 |
|
| 156 |
/** |
| 157 |
* @brief 设置输入对话框中使用的组合框是否可编辑 |
| 158 |
* @param editable |
| 159 |
*/ |
| 160 |
void setComboBoxEditable(bool editable); |
| 161 |
|
| 162 |
/** |
| 163 |
* @brief 返回输入对话框中使用的组合框是否可编辑 |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
bool isComboBoxEditable() const; |
| 167 |
|
| 168 |
/** |
| 169 |
* @brief 设置输入对话框组合框中使用的项目 |
| 170 |
* @param items |
| 171 |
*/ |
| 172 |
void setComboBoxItems(const QStringList &items); |
| 173 |
|
| 174 |
/** |
| 175 |
* @brief 返回输入对话框组合框中使用的项目 |
| 176 |
* @return QStringList |
| 177 |
*/ |
| 178 |
QStringList comboBoxItems() const; |
| 179 |
|
| 180 |
/** |
| 181 |
* @brief 设置int类型值 |
| 182 |
* @param value |
| 183 |
*/ |
| 184 |
void setIntValue(int value); |
| 185 |
|
| 186 |
/** |
| 187 |
* @brief 获取int类型值 |
| 188 |
* @return int |
| 189 |
*/ |
| 190 |
int intValue() const; |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief 设置int类型最小值 |
| 194 |
* @param min |
| 195 |
*/ |
| 196 |
void setIntMinimum(int min); |
| 197 |
/** |
| 198 |
* @brief 获取int类型最小值 |
| 199 |
* @return int |
| 200 |
*/ |
| 201 |
int intMinimum() const; |
| 202 |
|
| 203 |
/** |
| 204 |
* @brief 设置int类型最大值 |
| 205 |
* @param max |
| 206 |
*/ |
| 207 |
void setIntMaximum(int max); |
| 208 |
/** |
| 209 |
* @brief 获取int类型最大值 |
| 210 |
* @return int |
| 211 |
*/ |
| 212 |
int intMaximum() const; |
| 213 |
|
| 214 |
/** |
| 215 |
* @brief 设置int类型值范围 |
| 216 |
* @param min |
| 217 |
* @param max |
| 218 |
*/ |
| 219 |
void setIntRange(int min, int max); |
| 220 |
|
| 221 |
/** |
| 222 |
* @brief 设置步长 |
| 223 |
* @param step |
| 224 |
*/ |
| 225 |
void setIntStep(int step); |
| 226 |
/** |
| 227 |
* @brief 获取int类型步长 |
| 228 |
* @return int |
| 229 |
*/ |
| 230 |
int intStep() const; |
| 231 |
|
| 232 |
/** |
| 233 |
* @brief 设置double类型值 |
| 234 |
* @param value |
| 235 |
*/ |
| 236 |
void setDoubleValue(double value); |
| 237 |
/** |
| 238 |
* @brief 获取double类型值 |
| 239 |
* @return double |
| 240 |
*/ |
| 241 |
double doubleValue() const; |
| 242 |
|
| 243 |
/** |
| 244 |
* @brief 设置double类型最小值 |
| 245 |
* @param min |
| 246 |
*/ |
| 247 |
void setDoubleMinimum(double min); |
| 248 |
|
| 249 |
/** |
| 250 |
* @brief 获取double类型最小值 |
| 251 |
* @return double |
| 252 |
*/ |
| 253 |
double doubleMinimum() const; |
| 254 |
|
| 255 |
/** |
| 256 |
* @brief 设置double类型最大值 |
| 257 |
* @param max |
| 258 |
*/ |
| 259 |
void setDoubleMaximum(double max); |
| 260 |
|
| 261 |
/** |
| 262 |
* @brief 获取double类型最大值 |
| 263 |
* @return double |
| 264 |
*/ |
| 265 |
double doubleMaximum() const; |
| 266 |
|
| 267 |
/** |
| 268 |
* @brief 设置double类型值范围 |
| 269 |
* @param min |
| 270 |
* @param max |
| 271 |
*/ |
| 272 |
void setDoubleRange(double min, double max); |
| 273 |
|
| 274 |
/** |
| 275 |
* @brief 以小数为单位设置双自旋盒的精度 |
| 276 |
* @param decimals |
| 277 |
*/ |
| 278 |
void setDoubleDecimals(int decimals); |
| 279 |
|
| 280 |
/** |
| 281 |
* @brief 获取双自旋盒的精度 |
| 282 |
* @return int |
| 283 |
*/ |
| 284 |
int doubleDecimals() const; |
| 285 |
|
| 286 |
/** |
| 287 |
* @brief 设置OK按钮的文本 |
| 288 |
* @param text |
| 289 |
*/ |
| 290 |
void setOkButtonText(const QString &text); |
| 291 |
|
| 292 |
/** |
| 293 |
* @brief 获取OK按钮的文本 |
| 294 |
* @return QString |
| 295 |
*/ |
| 296 |
QString okButtonText() const; |
| 297 |
|
| 298 |
/** |
| 299 |
* @brief 设置cancel按钮的文本 |
| 300 |
* @param text |
| 301 |
*/ |
| 302 |
void setCancelButtonText(const QString &text); |
| 303 |
/** |
| 304 |
* @brief 获取cancel按钮的文本 |
| 305 |
* @return |
| 306 |
*/ |
| 307 |
QString cancelButtonText() const; |
| 308 |
|
| 309 |
using QDialog::open; |
| 310 |
/** |
| 311 |
* @brief 将对话框显示为窗口模式对话框,并立即返回。 |
| 312 |
* @param receiver |
| 313 |
* @param member |
| 314 |
*/ |
| 315 |
void open(QObject *receiver, const char *member); |
| 316 |
|
| 317 |
/** |
| 318 |
* @brief 获取部件的推荐最小尺寸 |
| 319 |
* @return QSize |
| 320 |
*/ |
| 321 |
QSize minimumSizeHint() const override; |
| 322 |
|
| 323 |
/** |
| 324 |
* @brief 获取保存部件的推荐尺寸 |
| 325 |
* @return |
| 326 |
*/ |
| 327 |
QSize sizeHint() const override; |
| 328 |
|
| 329 |
/** |
| 330 |
* @brief 设置窗口部件是否可见 |
| 331 |
* @param visible |
| 332 |
*/ |
| 333 |
void setVisible(bool visible) override; |
| 334 |
|
| 335 |
/** |
| 336 |
* @brief 获取背景提示文本 |
| 337 |
* @return |
| 338 |
* @since 1.2.0 |
| 339 |
*/ |
| 340 |
QString placeholderText() const; |
| 341 |
|
| 342 |
/** |
| 343 |
* @brief 设置背景提示文本 |
| 344 |
* @since 1.2.0 |
| 345 |
*/ |
| 346 |
void setPlaceholderText(const QString &); |
| 347 |
|
| 348 |
/** |
| 349 |
* @brief 静态便利函数,用于从用户处获取单行字符串。 |
| 350 |
* @param parent |
| 351 |
* @param label |
| 352 |
* @param echo |
| 353 |
* @param text |
| 354 |
* @param ok |
| 355 |
* @param flags |
| 356 |
* @param inputMethodHints |
| 357 |
* @return |
| 358 |
*/ |
| 359 |
static QString getText(QWidget *parent, const QString &label, |
| 360 |
QLineEdit::EchoMode echo = QLineEdit::Normal, |
| 361 |
const QString &text = QString(), bool *ok = nullptr, |
| 362 |
Qt::WindowFlags flags = Qt::WindowFlags(), |
| 363 |
Qt::InputMethodHints inputMethodHints = Qt::ImhNone); |
| 364 |
|
| 365 |
/** |
| 366 |
* @brief 静态便利函数,用于从用户处获取多行字符串。 |
| 367 |
* @param parent |
| 368 |
* @param label |
| 369 |
* @param text |
| 370 |
* @param ok |
| 371 |
* @param flags |
| 372 |
* @param inputMethodHints |
| 373 |
* @return |
| 374 |
*/ |
| 375 |
static QString getMultiLineText(QWidget *parent, const QString &label, |
| 376 |
const QString &text = QString(), bool *ok = nullptr, |
| 377 |
Qt::WindowFlags flags = Qt::WindowFlags(), |
| 378 |
Qt::InputMethodHints inputMethodHints = Qt::ImhNone); |
| 379 |
|
| 380 |
/** |
| 381 |
* @brief 静态便利函数,用于让用户从字符串列表中选择一个项目。 |
| 382 |
* @param parent |
| 383 |
* @param label |
| 384 |
* @param items |
| 385 |
* @param current |
| 386 |
* @param editable |
| 387 |
* @param ok |
| 388 |
* @param flags |
| 389 |
* @param inputMethodHints |
| 390 |
* @return |
| 391 |
*/ |
| 392 |
static QString getItem(QWidget *parent, const QString &label, |
| 393 |
const QStringList &items, int current = 0, bool editable = true, |
| 394 |
bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(), |
| 395 |
Qt::InputMethodHints inputMethodHints = Qt::ImhNone); |
| 396 |
|
| 397 |
/** |
| 398 |
* @brief 静态便利函数,用于从用户处获取整数输入值。 |
| 399 |
* @param parent |
| 400 |
* @param label |
| 401 |
* @param value |
| 402 |
* @param minValue |
| 403 |
* @param maxValue |
| 404 |
* @param step |
| 405 |
* @param ok |
| 406 |
* @param flags |
| 407 |
* @return |
| 408 |
*/ |
| 409 |
static int getInt(QWidget *parent, const QString &label, int value = 0, |
| 410 |
int minValue = -2147483647, int maxValue = 2147483647, |
| 411 |
int step = 1, bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); |
| 412 |
|
| 413 |
/** |
| 414 |
* @brief 静态便利函数,用于从用户处获取浮点数。 |
| 415 |
* @param parent |
| 416 |
* @param label |
| 417 |
* @param value |
| 418 |
* @param minValue |
| 419 |
* @param maxValue |
| 420 |
* @param decimals |
| 421 |
* @param ok |
| 422 |
* @param flags |
| 423 |
* @return |
| 424 |
*/ |
| 425 |
static double getDouble(QWidget *parent, const QString &label, |
| 426 |
double value = 0, double minValue = -2147483647, |
| 427 |
double maxValue = 2147483647, int decimals = 1, bool *ok = nullptr, |
| 428 |
Qt::WindowFlags flags = Qt::WindowFlags()); |
| 429 |
|
| 430 |
/** |
| 431 |
* @brief 设置doubel类型步长 |
| 432 |
* @param step |
| 433 |
*/ |
| 434 |
void setDoubleStep(double step); |
| 435 |
/** |
| 436 |
* @brief 获取doubel类型步长 |
| 437 |
* @return double |
| 438 |
*/ |
| 439 |
double doubleStep() const; |
| 440 |
|
| 441 |
Q_SIGNALS: |
| 442 |
// ### emit signals! |
| 443 |
void textValueChanged(const QString &text); |
| 444 |
void textValueSelected(const QString &text); |
| 445 |
void intValueChanged(int value); |
| 446 |
void intValueSelected(int value); |
| 447 |
void doubleValueChanged(double value); |
| 448 |
void doubleValueSelected(double value); |
| 449 |
|
| 450 |
public: |
| 451 |
void done(int result) override; |
| 452 |
|
| 453 |
protected: |
| 454 |
void changeTheme(); |
| 455 |
|
| 456 |
private: |
| 457 |
KInputDialogPrivate *const d_ptr; |
| 458 |
Q_DISABLE_COPY(KInputDialog) |
| 459 |
Q_DECLARE_PRIVATE(KInputDialog) |
| 460 |
Q_PRIVATE_SLOT(d_ptr, void _q_textChanged(const QString &)) |
| 461 |
Q_PRIVATE_SLOT(d_ptr, void _q_plainTextEditTextChanged()) |
| 462 |
Q_PRIVATE_SLOT(d_ptr, void _q_currentRowChanged(const QModelIndex &, const QModelIndex &)) |
| 463 |
}; |
| 464 |
|
| 465 |
Q_DECLARE_OPERATORS_FOR_FLAGS(KInputDialog::InputDialogOptions) |
| 466 |
} |
| 467 |
/** |
| 468 |
* @example testinputdialog/widget.h |
| 469 |
* @example testinputdialog/widget.cpp |
| 470 |
* @} |
| 471 |
*/ |
| 472 |
#endif // KINPUTDIALOG_H |