161 lines
4.4 KiB
C++
Executable File
161 lines
4.4 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Helper Classes to manage requirements for end-users to ui elements
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*
|
|
* Created by Mike Wille, SLSE, 2016-08-19
|
|
*/
|
|
|
|
#include "ui_requirement.h"
|
|
|
|
#include <LtsWidgets/lts_message_dialog.h>
|
|
|
|
#include <QApplication>
|
|
#include <QLabel>
|
|
|
|
|
|
namespace LtsWidgets
|
|
{
|
|
UiRequirement::UiRequirement(QLabel *descriptionLabel, const std::function<bool()> &requirementCheck, const QString &userWarningMessage)
|
|
: m_descriptionLabel(descriptionLabel)
|
|
, m_requirementCheck(requirementCheck)
|
|
, m_lastResult(true)
|
|
, m_userWarningMessage(userWarningMessage)
|
|
{
|
|
retranslate();
|
|
}
|
|
|
|
|
|
void UiRequirement::reset()
|
|
{
|
|
if (!isValid())
|
|
return;
|
|
m_descriptionLabel->setText(m_originalText);
|
|
m_descriptionLabel->setToolTip(m_originalTooltip);
|
|
}
|
|
|
|
|
|
void UiRequirement::setTexts()
|
|
{
|
|
if (!isValid())
|
|
return;
|
|
if (!m_lastResult) // requirement not met? change text color but only if this is NOT a simulation!
|
|
{
|
|
m_descriptionLabel->setText(QStringLiteral("<span style=\"color:#aa0000;\">\u2022 %1</span>").arg(m_originalText)); // add requirement-indicator unicode 'bullet'
|
|
|
|
if (!m_userWarningMessage.isEmpty())
|
|
{
|
|
m_descriptionLabel->setCursor(Qt::WhatsThisCursor);
|
|
m_descriptionLabel->setToolTip(m_userWarningMessage);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (m_originalText.startsWith(QStringLiteral("\u2022 ")))
|
|
return;
|
|
|
|
m_descriptionLabel->setText(QStringLiteral("\u2022 %1").arg(m_originalText)); // set back to original text, as the requirement is now met and add requirement-indicator unicode 'bullet'
|
|
m_descriptionLabel->setCursor(Qt::ArrowCursor);
|
|
m_descriptionLabel->setToolTip(m_originalTooltip);
|
|
}
|
|
}
|
|
|
|
|
|
void UiRequirement::retranslate()
|
|
{
|
|
if (!isValid())
|
|
return;
|
|
|
|
if (m_originalText.startsWith(QStringLiteral("\u2022 ")))
|
|
return;
|
|
|
|
auto x = m_descriptionLabel->text();
|
|
if (x.startsWith(QStringLiteral("\u2022 ")))
|
|
return;
|
|
|
|
m_originalText = x;
|
|
|
|
m_originalTooltip = m_descriptionLabel->toolTip();
|
|
setTexts();
|
|
}
|
|
|
|
|
|
bool UiRequirement::checkRequirement()
|
|
{
|
|
if (!isValid())
|
|
return m_lastResult;
|
|
|
|
m_lastResult = m_requirementCheck();
|
|
|
|
if (!m_descriptionLabel->isVisible() && !m_descriptionLabel->property("checkMe").toBool()) // do not check when label is not visible (on some cases there are elemts which must not be used, so they are hidden and therefor not a requirement, and therefor "a met requirement")
|
|
{
|
|
return true;
|
|
}
|
|
|
|
setTexts();
|
|
|
|
return m_lastResult;
|
|
}
|
|
|
|
|
|
bool UiRequirementList::checkRequirements(QWidget *messageOnThisParent)
|
|
{
|
|
bool result = true; // as long no requirement failed (even if there are none), the result is positive
|
|
for (auto i = m_requirementList.begin(); i != m_requirementList.end(); ++i)
|
|
{
|
|
UiRequirement &r = (*i);
|
|
|
|
// Todo: Delete from Vector!
|
|
if (!r.isValid())
|
|
continue;
|
|
|
|
if (!r.checkRequirement()) // requirement failed, so all requirements failed, but keep on checking the requirements (to show the user what exactly is wrong)
|
|
{
|
|
result = false; // to NOT break here, checkRequirement() marks all important errors, so it must be called, even if result was already set to false
|
|
}
|
|
}
|
|
|
|
if (!result && messageOnThisParent) // show a message that one or more requirements are not met, but only when a pointer has been given to put the LtsMessageDialog on
|
|
{
|
|
LtsWidgets::LtsMessageDialog::showMessage(messageOnThisParent,
|
|
QApplication::translate("LtsWidgets::LtsDialog",
|
|
"There are elements that have invalid or no value.<br>"
|
|
"Please change values in the elements which are marked with red text."),
|
|
QApplication::translate("LtsWidgets::LtsDialog", "Operation not complete"));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
void UiRequirementList::addRequirement(const UiRequirement &uiRequirement)
|
|
{
|
|
m_requirementList.append(uiRequirement);
|
|
}
|
|
|
|
|
|
void UiRequirementList::clear()
|
|
{
|
|
for (auto i = m_requirementList.begin(); i != m_requirementList.end(); ++i)
|
|
{
|
|
UiRequirement &uir = *i;
|
|
uir.reset();
|
|
}
|
|
|
|
m_requirementList.clear();
|
|
}
|
|
|
|
|
|
void UiRequirementList::retranslate()
|
|
{
|
|
for (auto i = m_requirementList.begin(); i != m_requirementList.end(); ++i)
|
|
{
|
|
UiRequirement &r = (*i);
|
|
|
|
r.retranslate();
|
|
}
|
|
}
|
|
} // namespace LtsWidgets
|