| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Copyright (C) 1997-2019 by Dimitri van Heesch. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, and distribute this software and its |
| 6 |
* documentation under the terms of the GNU General Public License is hereby |
| 7 |
* granted. No representations are made about the suitability of this software |
| 8 |
* for any purpose. It is provided "as is" without express or implied warranty. |
| 9 |
* See the GNU General Public License for more details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "inputbool.h" |
| 14 |
#include "helplabel.h" |
| 15 |
#include "config_msg.h" |
| 16 |
|
| 17 |
#include <QCheckBox> |
| 18 |
#include <QTextStream> |
| 19 |
#include <QGridLayout> |
| 20 |
|
| 21 |
InputBool::InputBool( QGridLayout *layout, int &row, |
| 22 |
const QString &id, bool checked, |
| 23 |
const QString &docs ) |
| 24 |
: m_default(checked), m_docs(docs), m_id(id) |
| 25 |
{ |
| 26 |
m_lab = new HelpLabel(id); |
| 27 |
m_cb = new QCheckBox; |
| 28 |
layout->addWidget(m_lab,row, 0); |
| 29 |
layout->addWidget(m_cb,row, 1); |
| 30 |
m_enabled = true; |
| 31 |
m_state=!checked; // force update |
| 32 |
setValue(checked); |
| 33 |
connect( m_cb, SIGNAL(toggled(bool)), SLOT(setValue(bool)) ); |
| 34 |
connect( m_lab, SIGNAL(enter()), SLOT(help()) ); |
| 35 |
connect( m_lab, SIGNAL(reset()), SLOT(reset()) ); |
| 36 |
row++; |
| 37 |
} |
| 38 |
|
| 39 |
void InputBool::help() |
| 40 |
{ |
| 41 |
showHelp(this); |
| 42 |
} |
| 43 |
|
| 44 |
void InputBool::setEnabled(bool b) |
| 45 |
{ |
| 46 |
m_enabled = b; |
| 47 |
m_cb->setEnabled(b); |
| 48 |
m_lab->setEnabled(b); |
| 49 |
updateDefault(); |
| 50 |
updateDependencies(); |
| 51 |
} |
| 52 |
|
| 53 |
void InputBool::updateDependencies() |
| 54 |
{ |
| 55 |
for (int i=0;i<m_dependencies.count();i++) |
| 56 |
{ |
| 57 |
m_dependencies[i]->setEnabled(m_enabled && m_state); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void InputBool::setValue( bool s ) |
| 62 |
{ |
| 63 |
if (m_state!=s) |
| 64 |
{ |
| 65 |
m_state=s; |
| 66 |
updateDefault(); |
| 67 |
updateDependencies(); |
| 68 |
m_cb->setChecked( s ); |
| 69 |
m_value = m_state; |
| 70 |
emit changed(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void InputBool::updateDefault() |
| 75 |
{ |
| 76 |
if (m_state==m_default || !m_lab->isEnabled()) |
| 77 |
{ |
| 78 |
m_lab->setText(QString::fromLatin1("<qt>")+m_id+QString::fromLatin1("</qt>")); |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
m_lab->setText(QString::fromLatin1("<qt><font color='red'>")+m_id+QString::fromLatin1("</font></qt>")); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
QVariant &InputBool::value() |
| 87 |
{ |
| 88 |
return m_value; |
| 89 |
} |
| 90 |
|
| 91 |
bool InputBool::convertToBool(const QVariant &value,bool &isValid) |
| 92 |
{ |
| 93 |
QString v = value.toString().toLower(); |
| 94 |
if (v==QString::fromLatin1("yes") || v==QString::fromLatin1("true") || |
| 95 |
v==QString::fromLatin1("1") || v==QString::fromLatin1("all")) |
| 96 |
{ |
| 97 |
isValid = true; |
| 98 |
return true; |
| 99 |
} |
| 100 |
else if (v==QString::fromLatin1("no") || v==QString::fromLatin1("false") || |
| 101 |
v==QString::fromLatin1("0") || v==QString::fromLatin1("none")) |
| 102 |
{ |
| 103 |
isValid = true; |
| 104 |
return false; |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
isValid = false; |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void InputBool::update() |
| 114 |
{ |
| 115 |
bool isValid=false; |
| 116 |
bool b = convertToBool(m_value,isValid); |
| 117 |
if (isValid) |
| 118 |
{ |
| 119 |
m_state = b; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
config_warn("argument '%s' for option %s is not a valid boolean value." |
| 124 |
" Using the default: %s!",qPrintable(m_value.toString()),qPrintable(m_id),m_default?"YES":"NO"); |
| 125 |
m_state = m_default; |
| 126 |
} |
| 127 |
m_cb->setChecked( m_state ); |
| 128 |
updateDefault(); |
| 129 |
updateDependencies(); |
| 130 |
} |
| 131 |
|
| 132 |
void InputBool::reset() |
| 133 |
{ |
| 134 |
setValue(m_default); |
| 135 |
} |
| 136 |
|
| 137 |
void InputBool::writeValue(QTextStream &t,TextCodecAdapter *codec,bool) |
| 138 |
{ |
| 139 |
if (m_state) |
| 140 |
t << codec->encode(QString::fromLatin1("YES")); |
| 141 |
else |
| 142 |
t << codec->encode(QString::fromLatin1("NO")); |
| 143 |
} |
| 144 |
|
| 145 |
bool InputBool::isDefault() |
| 146 |
{ |
| 147 |
return m_state == m_default; |
| 148 |
} |
| 149 |
|