! \class ConstantsWidget \brief Widget for selecting supported mathematical and physical constants that can be used in expressions in \c ExpressionTextEdit. \ingroup frontend */
| 19 | \ingroup frontend |
| 20 | */ |
| 21 | ConstantsWidget::ConstantsWidget(QWidget* parent) |
| 22 | : QWidget(parent) { |
| 23 | ui.setupUi(this); |
| 24 | ui.bInsert->setIcon(QIcon::fromTheme(QStringLiteral("edit-paste"))); |
| 25 | ui.bCancel->setIcon(QIcon::fromTheme(QStringLiteral("dialog-cancel"))); |
| 26 | m_expressionParser = ExpressionParser::getInstance(); |
| 27 | |
| 28 | for (int i = 0; i < (int)Parsing::ConstantGroups::END; i++) |
| 29 | ui.cbGroup->addItem(Parsing::constantGroupsToString(static_cast<Parsing::ConstantGroups>(i)), i); |
| 30 | |
| 31 | // SLOTS |
| 32 | connect(ui.leFilter, &QLineEdit::textChanged, this, &ConstantsWidget::filterChanged); |
| 33 | connect(ui.cbGroup, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConstantsWidget::groupChanged); |
| 34 | connect(ui.lwConstants, &QListWidget::currentTextChanged, this, &ConstantsWidget::constantChanged); |
| 35 | connect(ui.bInsert, &QPushButton::clicked, this, &ConstantsWidget::insertClicked); |
| 36 | connect(ui.bCancel, &QPushButton::clicked, this, &ConstantsWidget::canceled); |
| 37 | connect(ui.lwConstants, &QListWidget::itemDoubleClicked, this, &ConstantsWidget::insertClicked); |
| 38 | |
| 39 | // set the focus to the search field and select the first group after the widget is shown |
| 40 | QTimer::singleShot(0, this, [=]() { |
| 41 | ui.leFilter->setFocus(); |
| 42 | this->groupChanged(0); |
| 43 | }); |
| 44 | |
| 45 | // set the minimum size to show the longest constant name without any horizontal scroll bar |
| 46 | const QStringList& names = m_expressionParser->constantsNames(); |
| 47 | QString maxName; |
| 48 | int maxLength = 0; |
| 49 | for (const auto& name : names) { |
| 50 | int length = name.length(); |
| 51 | if (length > maxLength) { |
| 52 | maxLength = length; |
| 53 | maxName = name; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | QFont font; |
| 58 | QFontMetrics fm(font); |
| 59 | const auto margins = layout()->contentsMargins(); |
| 60 | const int width = fm.horizontalAdvance(maxName) + margins.left() + margins.right() + qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent); |
| 61 | setMinimumWidth(width); |
| 62 | } |
| 63 | |
| 64 | /*! |
| 65 | * shows all constants of the selected group and selects the first one in the QStringList |
nothing calls this directly
no test coverage detected