File indexing completed on 2024-05-12 16:40:55
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004 Jarosław Staniek <staniek@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KexiNameWidget.h" 0021 #include <core/kexi.h> 0022 0023 #include <KDbIdentifierValidator> 0024 #include <KDb> 0025 0026 #include <KMessageBox> 0027 #include <KLocalizedString> 0028 0029 #include <QLabel> 0030 #include <QGridLayout> 0031 #include <QLineEdit> 0032 0033 class Q_DECL_HIDDEN KexiNameWidget::Private 0034 { 0035 public: 0036 0037 Private() {} 0038 0039 QLabel* lbl_message; 0040 QLabel* lbl_caption; 0041 QLabel* lbl_name; 0042 QLineEdit* le_caption; 0043 QLineEdit* le_name; 0044 QGridLayout* lyr; 0045 KDbMultiValidator *validator; 0046 QString nameWarning, captionWarning; 0047 QString originalNameText; 0048 0049 bool le_name_txtchanged_disable; 0050 bool le_name_autofill; 0051 bool caption_required; 0052 }; 0053 0054 KexiNameWidget::KexiNameWidget(const QString& message, QWidget* parent) 0055 : QWidget(parent) 0056 , d(new Private) 0057 { 0058 init(message, QString(), QString(), QString(), QString()); 0059 } 0060 0061 KexiNameWidget::KexiNameWidget(const QString& message, 0062 const QString& nameLabel, const QString& nameText, 0063 const QString& captionLabel, const QString& captionText, 0064 QWidget * parent) 0065 : QWidget(parent) 0066 , d(new Private) 0067 { 0068 init(message, nameLabel, nameText, captionLabel, captionText); 0069 } 0070 0071 void KexiNameWidget::init( 0072 const QString& message, 0073 const QString& nameLabel, const QString& nameText, 0074 const QString& captionLabel, const QString& captionText) 0075 { 0076 Q_UNUSED(captionText); 0077 setObjectName("KexiNameWidget"); 0078 0079 d->le_name_txtchanged_disable = false; 0080 d->le_name_autofill = true; 0081 d->caption_required = false; 0082 0083 d->lyr = new QGridLayout(this); 0084 0085 d->lbl_message = new QLabel(this); 0086 setMessageText(message); 0087 d->lbl_message->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0088 d->lbl_message->setAlignment(Qt::AlignTop|Qt::AlignLeft); 0089 d->lbl_message->setWordWrap(true); 0090 d->lbl_message->setTextInteractionFlags(Qt::TextBrowserInteraction); 0091 d->lyr->addWidget(d->lbl_message, 0, 0, 1, 2); 0092 0093 d->lbl_caption = new QLabel(captionLabel.isEmpty() ? xi18n("Caption:") : captionLabel, 0094 this); 0095 d->lbl_caption->setAlignment(Qt::AlignVCenter|Qt::AlignRight); 0096 d->lyr->addWidget(d->lbl_caption, 1, 0); 0097 0098 d->lbl_name = new QLabel(nameLabel.isEmpty() ? xi18n("Name:") : nameLabel, 0099 this); 0100 d->lbl_name->setAlignment(Qt::AlignVCenter|Qt::AlignRight); 0101 d->lyr->addWidget(d->lbl_name, 2, 0); 0102 0103 d->le_caption = new QLineEdit(this); 0104 setCaptionText(nameText); 0105 QSizePolicy le_captionSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0106 le_captionSizePolicy.setHorizontalStretch(1); 0107 d->le_caption->setSizePolicy(le_captionSizePolicy); 0108 d->le_caption->setClearButtonEnabled(true); 0109 d->lyr->addWidget(d->le_caption, 1, 1); 0110 0111 d->le_name = new QLineEdit(this); 0112 setNameText(nameText); 0113 le_captionSizePolicy.setHorizontalStretch(1); 0114 d->le_name->setSizePolicy(le_captionSizePolicy); 0115 d->le_name->setClearButtonEnabled(true); 0116 KDbIdentifierValidator *idValidator = new KDbIdentifierValidator(0); 0117 idValidator->setLowerCaseForced(true); 0118 d->le_name->setValidator(d->validator = new KDbMultiValidator(idValidator, this)); 0119 d->lyr->addWidget(d->le_name, 2, 1); 0120 0121 setFocusProxy(d->le_caption); 0122 resize(QSize(342, 123).expandedTo(minimumSizeHint())); 0123 0124 d->nameWarning = xi18n("Please enter the name."); 0125 d->captionWarning = xi18n("Please enter the caption."); 0126 0127 connect(d->le_caption, SIGNAL(textChanged(QString)), 0128 this, SLOT(slotCaptionTextChanged(QString))); 0129 connect(d->le_name, SIGNAL(textChanged(QString)), 0130 this, SLOT(slotNameTextChanged(QString))); 0131 connect(d->le_caption, SIGNAL(returnPressed()), 0132 this, SIGNAL(returnPressed())); 0133 connect(d->le_name, SIGNAL(returnPressed()), 0134 this, SIGNAL(returnPressed())); 0135 } 0136 0137 KexiNameWidget::~KexiNameWidget() 0138 { 0139 delete d; 0140 } 0141 0142 QLabel* KexiNameWidget::captionLabel() const 0143 { 0144 return d->lbl_caption; 0145 } 0146 0147 QLabel* KexiNameWidget::nameLabel() const 0148 { 0149 return d->lbl_name; 0150 } 0151 0152 QLineEdit* KexiNameWidget::captionLineEdit() const 0153 { 0154 return d->le_caption; 0155 } 0156 0157 QLineEdit* KexiNameWidget::nameLineEdit() const 0158 { 0159 return d->le_name; 0160 } 0161 0162 QLabel* KexiNameWidget::messageLabel() const 0163 { 0164 return d->lbl_message; 0165 } 0166 0167 QString KexiNameWidget::messageText() const 0168 { 0169 return d->lbl_message->text(); 0170 } 0171 0172 0173 0174 void KexiNameWidget::slotCaptionTextChanged(const QString &capt) 0175 { 0176 emit textChanged(); 0177 if (d->le_name->text().isEmpty()) 0178 d->le_name_autofill = true; 0179 if (d->le_name_autofill) { 0180 d->le_name_txtchanged_disable = true; 0181 d->le_name->setText(KDb::stringToIdentifier(capt).toLower()); 0182 d->le_name_txtchanged_disable = false; 0183 } 0184 } 0185 0186 void KexiNameWidget::slotNameTextChanged(const QString &) 0187 { 0188 emit textChanged(); 0189 if (d->le_name_txtchanged_disable) 0190 return; 0191 d->le_name_autofill = false; 0192 } 0193 0194 void KexiNameWidget::clear() 0195 { 0196 d->le_name->clear(); 0197 d->le_caption->clear(); 0198 } 0199 0200 bool KexiNameWidget::empty() const 0201 { 0202 return d->le_name->text().isEmpty() || d->le_caption->text().trimmed().isEmpty(); 0203 } 0204 0205 void KexiNameWidget::setNameRequired(bool set) 0206 { 0207 d->validator->setAcceptsEmptyValue(!set); 0208 } 0209 0210 bool KexiNameWidget::isCaptionRequired() const { 0211 return d->caption_required; 0212 } 0213 0214 void KexiNameWidget::setCaptionRequired(bool set) { 0215 d->caption_required = set; 0216 } 0217 0218 0219 bool KexiNameWidget::isNameRequired() const 0220 { 0221 return !d->validator->acceptsEmptyValue(); 0222 } 0223 0224 void KexiNameWidget::setCaptionText(const QString& capt) 0225 { 0226 d->le_caption->setText(capt); 0227 d->le_name_autofill = true; 0228 } 0229 0230 void KexiNameWidget::setNameText(const QString& name) 0231 { 0232 d->le_name->setText(name); 0233 d->originalNameText = name; 0234 d->le_name_autofill = true; 0235 } 0236 0237 void KexiNameWidget::setWarningForName(const QString& txt) 0238 { 0239 d->nameWarning = txt; 0240 } 0241 0242 void KexiNameWidget::setWarningForCaption(const QString& txt) 0243 { 0244 d->captionWarning = txt; 0245 } 0246 0247 0248 void KexiNameWidget::setMessageText(const QString& msg) 0249 { 0250 if (msg.trimmed().isEmpty()) { 0251 d->lbl_message->setText(QString()); 0252 d->lbl_message->hide(); 0253 } else { 0254 d->lbl_message->setText(msg.trimmed() + "<br>"); 0255 d->lbl_message->show(); 0256 } 0257 messageChanged(); 0258 } 0259 0260 QString KexiNameWidget::captionText() const 0261 { 0262 return d->le_caption->text().trimmed(); 0263 } 0264 0265 QString KexiNameWidget::nameText() const 0266 { 0267 return d->le_name->text().trimmed(); 0268 } 0269 0270 QString KexiNameWidget::originalNameText() const 0271 { 0272 return d->originalNameText; 0273 } 0274 0275 0276 bool KexiNameWidget::checkValidity() 0277 { 0278 if (isNameRequired() && d->le_name->text().trimmed().isEmpty()) { 0279 KMessageBox::sorry(0, d->nameWarning); 0280 d->le_name->setFocus(); 0281 return false; 0282 } 0283 if (isCaptionRequired() && d->le_caption->text().trimmed().isEmpty()) { 0284 KMessageBox::sorry(0, d->captionWarning); 0285 d->le_caption->setFocus(); 0286 return false; 0287 } 0288 QString dummy, message, details; 0289 if (d->validator->check(dummy, d->le_name->text(), &message, &details) 0290 == KDbValidator::Error) 0291 { 0292 KMessageBox::detailedSorry(0, message, details); 0293 d->le_name->setFocus(); 0294 return false; 0295 } 0296 return true; 0297 } 0298 0299 KDbValidator *KexiNameWidget::nameValidator() const 0300 { 0301 return d->validator; 0302 } 0303 0304 void KexiNameWidget::addNameSubvalidator(KDbValidator* validator, bool owned) 0305 { 0306 d->validator->addSubvalidator(validator, owned); 0307 } 0308