geopro/src/app/panels/chart/ExceptionTextDialog.cpp

116 lines
4.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "panels/chart/ExceptionTextDialog.hpp"
#include <QColorDialog>
#include <QComboBox>
#include "EmptyAwareComboBox.hpp"
#include <QFormLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QSlider>
#include <QSpinBox>
#include <QVBoxLayout>
#include "FormKit.hpp"
#include "Theme.hpp"
namespace geopro::app {
ExceptionTextDialog::ExceptionTextDialog(QWidget* parent)
: QDialog(parent), color_(QStringLiteral("#000000")) {
setWindowTitle(QStringLiteral("文本编辑"));
setModal(true);
resize(geopro::app::scaledPx(560), geopro::app::scaledPx(420));
auto* root = formkit::dialogRoot(this);
auto* card = formkit::formCard(this);
auto* cardLay = formkit::cardBody(card);
auto* form = formkit::makeEditForm();
// 字体(对照原版 1宋体/2微软雅黑/3黑体/4楷体值为字符串 int
fontCombo_ = new EmptyAwareComboBox(this);
fontCombo_->addItem(QStringLiteral("宋体"), QStringLiteral("1"));
fontCombo_->addItem(QStringLiteral("微软雅黑"), QStringLiteral("2"));
fontCombo_->addItem(QStringLiteral("黑体"), QStringLiteral("3"));
fontCombo_->addItem(QStringLiteral("楷体"), QStringLiteral("4"));
formkit::capField(fontCombo_);
form->addRow(formkit::editLabel(QStringLiteral("字体")), fontCombo_);
// 大小px默认 12
sizeSpin_ = new QSpinBox(this);
sizeSpin_->setRange(1, 200);
sizeSpin_->setValue(12);
formkit::capField(sizeSpin_);
form->addRow(formkit::editLabel(QStringLiteral("大小")), sizeSpin_);
// 颜色(默认黑)。
colorBtn_ = new QPushButton(color_, this);
colorBtn_->setStyleSheet(QStringLiteral("background:%1;").arg(color_));
connect(colorBtn_, &QPushButton::clicked, this, &ExceptionTextDialog::pickColor);
formkit::capField(colorBtn_);
form->addRow(formkit::editLabel(QStringLiteral("颜色")), colorBtn_);
// 不透明度0100%,默认 100
auto* opRow = new QWidget(this);
auto* opLay = new QHBoxLayout(opRow);
opLay->setContentsMargins(0, 0, 0, 0);
opacitySlider_ = new QSlider(Qt::Horizontal, opRow);
opacitySlider_->setRange(0, 100);
opacitySlider_->setValue(100);
opacityLabel_ = new QLabel(QStringLiteral("100%"), opRow);
opacityLabel_->setFixedWidth(geopro::app::scaledPx(40));
connect(opacitySlider_, &QSlider::valueChanged, this,
[this](int v) { opacityLabel_->setText(QStringLiteral("%1%").arg(v)); });
opLay->addWidget(opacitySlider_, 1);
opLay->addWidget(opacityLabel_);
form->addRow(formkit::editLabel(QStringLiteral("不透明度")), opRow);
cardLay->addLayout(form);
// 内容(必填)。
cardLay->addWidget(new QLabel(QStringLiteral("内容:"), this));
contentEdit_ = new QPlainTextEdit(this);
contentEdit_->setPlaceholderText(QStringLiteral("请输入内容"));
contentEdit_->setFixedHeight(geopro::app::scaledPx(140));
cardLay->addWidget(contentEdit_);
root->addWidget(card);
auto* btnLay = new QHBoxLayout();
btnLay->addStretch();
auto* cancelBtn = new QPushButton(QStringLiteral("取消"), this);
auto* okBtn = new QPushButton(QStringLiteral("确定"), this);
okBtn->setDefault(true);
btnLay->addWidget(cancelBtn);
btnLay->addWidget(okBtn);
root->addLayout(btnLay);
connect(cancelBtn, &QPushButton::clicked, this, &QDialog::reject);
connect(okBtn, &QPushButton::clicked, this, &ExceptionTextDialog::onConfirm);
}
void ExceptionTextDialog::pickColor() {
const QColor c = QColorDialog::getColor(QColor(color_), this, QStringLiteral("颜色"));
if (!c.isValid()) return;
color_ = c.name(QColor::HexRgb);
colorBtn_->setText(color_);
colorBtn_->setStyleSheet(QStringLiteral("background:%1;").arg(color_));
}
QString ExceptionTextDialog::fontFamilyValue() const { return fontCombo_->currentData().toString(); }
int ExceptionTextDialog::fontSize() const { return sizeSpin_->value(); }
QString ExceptionTextDialog::color() const { return color_; }
int ExceptionTextDialog::opacityPercent() const { return opacitySlider_->value(); }
QString ExceptionTextDialog::content() const { return contentEdit_->toPlainText().trimmed(); }
void ExceptionTextDialog::onConfirm() {
if (content().isEmpty()) { // 对照原版:内容必填。
QMessageBox::warning(this, windowTitle(), QStringLiteral("请输入文本内容"));
return;
}
accept();
}
} // namespace geopro::app