WSL/SLF GitLab Repository

Commit 51b3c724 authored by Michael Reisecker's avatar Michael Reisecker
Browse files

New validation markers in text fields

Now we have small icons to make things clearer
parent 2acf890f
......@@ -164,7 +164,7 @@ This is INIshell's user guide.
<parameter type="helptext" wrap="true">
<help>This is a list of the panels &lt;i&gt;INIshell&lt;/i&gt; supports. You can play around with the settings and use the &lt;font color=&quot;#268bd2&quot;&gt;Preview&lt;/font&gt; window to see what they do.&lt;br&gt;</help>
</parameter>
<frame caption="Text input">
<frame caption="Text input" key="help-textfield">
<parameter key="COORDSYS" type="text" help="e. g. CH1903">
<help>Example panel for text input. Enter your coordinate system, for example &quot;CH1903&quot;.</help>
</parameter>
......
......@@ -43,6 +43,7 @@
#include "src/main/colors.h"
#include <QDir>
#include <QFontMetrics>
#include <QMainWindow>
#include <QtWidgets>
......
......@@ -181,10 +181,10 @@ void Atomic::setPanelStyle(const PanelStyle &style, const bool &set, QWidget *wi
QLabel *key_label( qobject_cast<QLabel *>(getEmphasisWidget()) );
if (key_label) {
if (set) {
if (!key_label->text().startsWith(Cst::star + " "))
key_label->setText(Cst::star + " " + key_label->text());
if (!key_label->text().startsWith(Cst::u_star + " "))
key_label->setText(Cst::u_star + " " + key_label->text());
} else {
if (key_label->text().startsWith(Cst::star + " ")) {
if (key_label->text().startsWith(Cst::u_star + " ")) {
QString pure_label( key_label->text() ); //non-const copy
key_label->setText(pure_label.mid(2));
}
......
/*****************************************************************************/
/*****************************************************************************/
/* Copyright 2019 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/*****************************************************************************/
/* This file is part of INIshell.
......@@ -20,10 +20,11 @@
#include "Label.h"
#include "src/main/constants.h"
#include "src/main/expressions.h"
#include "src/main/inishell.h"
#include <QDesktopServices>
#include <QFontMetrics>
#include <QHBoxLayout>
#include <QToolButton>
#ifdef DEBUG
#include <iostream>
......@@ -48,20 +49,23 @@ Textfield::Textfield(const QString &section, const QString &key, const QDomNode
connect(textfield_, &QLineEdit::textEdited, this, &Textfield::checkValue);
/* action button */
check_button_ = new QToolButton; //a button that can pop up if the text has a certain format
check_button_->setVisible(false);
check_button_->setAutoRaise(true);
check_button_->setText(Cst::globe);
connect(check_button_, &QToolButton::clicked, this, &Textfield::checkButtonClicked);
nav_button_ = new QToolButton; //a button that can pop up if the text has a certain format
nav_button_->setStyleSheet("* {border: none}");
QSize sz_label;
sz_label.setWidth(static_cast<int>(fontMetrics().boundingRect(Cst::u_globe).width()) * 1.2);
sz_label.setHeight(static_cast<int>(fontMetrics().boundingRect(Cst::u_globe).height()));
nav_button_->setFixedSize(sz_label);
nav_button_->setCursor(Qt::PointingHandCursor);
connect(nav_button_, &QToolButton::clicked, this, &Textfield::navButtonClicked);
/* layout of textbox plus button and the main layout */
auto *field_button_layout( new QHBoxLayout );
field_button_layout->addWidget(textfield_);
field_button_layout->addWidget(check_button_);
auto *nav_button_layout( new QHBoxLayout );
nav_button_layout->addWidget(textfield_);
nav_button_layout->addWidget(nav_button_);
auto *textfield_layout( new QHBoxLayout );
setLayoutMargins(textfield_layout);
textfield_layout->addWidget(key_label, 0, Qt::AlignLeft);
textfield_layout->addLayout(field_button_layout);
textfield_layout->addLayout(nav_button_layout);
/* choose size of text box */
const QString size( options.toElement().attribute("size") );
......@@ -123,18 +127,27 @@ void Textfield::checkValue(const QString &text)
static const int idx_full = 0;
setDefaultPanelStyles(text);
if (coord_match.captured(idx_full) == text && !text.isEmpty()) {
check_button_->setVisible(true);
nav_button_->setText(Cst::u_globe);
} else {
check_button_->setVisible(false);
if (!validation_regex_.isNull()) { //check against regex specified in XML
const QRegularExpression rex_xml(validation_regex_);
const QRegularExpressionMatch xml_match(rex_xml.match(text));
setInvalidStyle(xml_match.captured(idx_full) != text && !text.isEmpty());
const bool is_invalid = xml_match.captured(idx_full) != text && !text.isEmpty();
setInvalidStyle(is_invalid);
nav_button_->setText(is_invalid? Cst::u_warning : Cst::u_valid);
nav_button_->setProperty("invalid", is_invalid? "true" : "false"); //set "invalid" style on label-button also
nav_button_->style()->unpolish(nav_button_);
nav_button_->style()->polish(nav_button_);
} else { //check for (arithmetic) expressions
bool evaluation_success;
const bool is_expression = expr::checkExpression(text, evaluation_success,
substitutions_, needs_prefix_for_evaluation_);
setInvalidStyle((is_expression && !evaluation_success) && !text.isEmpty());
const bool is_invalid = (is_expression && !evaluation_success) && !text.isEmpty();
setInvalidStyle(is_invalid);
nav_button_->setProperty("invalid", is_invalid? "true" : "false");
nav_button_->setText(is_invalid? Cst::u_warning : (is_expression? Cst::u_valid : ""));
nav_button_->style()->unpolish(nav_button_);
nav_button_->style()->polish(nav_button_);
}
}
setIniValue(text); //checks are just a hint - set anyway
......@@ -145,8 +158,13 @@ void Textfield::checkValue(const QString &text)
* @details Currently, the button is only shown for coordinates and will open the Browser
* with a map link.
*/
void Textfield::checkButtonClicked() //for now this button only pops up for coordinates
void Textfield::navButtonClicked() //for now this button only pops up for coordinates
{
if (nav_button_->text() != Cst::u_globe) { //"valid" resp. "invalid" icons
getMainWindow()->loadHelp("Input panels 1", "help-textfield");
return;
}
static const QString regex_coord(R"(\Alatlon\s*\(([-\d\.]+)(?:,)\s*([-\d\.]+)((?:,)\s*([-\d\.]+))?\))");
static const QRegularExpression rex_coord(regex_coord);
const QRegularExpressionMatch coord_match(rex_coord.match(textfield_->text()));
......
......@@ -28,6 +28,7 @@
#include <QLineEdit>
#include <QString>
#include <QToolButton>
#include <QWidget>
#include <QtXml>
......@@ -47,13 +48,13 @@ class Textfield : public Atomic {
std::vector<std::pair<QString, QString>> substitutions_; //user-set substitutions to translate to tinyexpr
QString validation_regex_;
QLineEdit *textfield_ = nullptr;
QToolButton *check_button_ = nullptr;
QToolButton *nav_button_ = nullptr;
bool needs_prefix_for_evaluation_ = true;
private slots:
void onPropertySet() override;
void checkValue(const QString &text);
void checkButtonClicked();
void navButtonClicked();
};
#endif // TEXTFIELD_H
......@@ -30,9 +30,11 @@
namespace Cst {
/* Unicode character literals */
static const QString star( QChar( 0x2217 ) ); //∗
static const QString u_star( QChar( 0x2217 ) ); //∗
static const uint globe_32 = 0x1F30D;
static const QString globe( QString::fromUcs4(&globe_32, 1) ); //🌍
static const QString u_globe( QString::fromUcs4(&globe_32, 1) ); //🌍
static const QString u_warning( QChar (0x26A0 ) ); //⚠
static const QString u_valid ( QChar ( 0x2713 ) );
/* main window */
static constexpr int width_inishell_default = 1400;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment