00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kexinamewidget.h"
00021
00022 #include <qlabel.h>
00023 #include <qlayout.h>
00024
00025 #include <klineedit.h>
00026 #include <kmessagebox.h>
00027 #include <klocale.h>
00028
00029 #include <kexiutils/validator.h>
00030 #include <kexiutils/identifier.h>
00031 #include <core/kexi.h>
00032
00033 using namespace KexiUtils;
00034
00035 KexiNameWidget::KexiNameWidget( const QString& message,
00036 QWidget* parent, const char* name, WFlags fl )
00037 : QWidget(parent, name, fl)
00038 {
00039 init(message, QString::null, QString::null, QString::null, QString::null);
00040 }
00041
00042 KexiNameWidget::KexiNameWidget(const QString& message,
00043 const QString& nameLabel, const QString& nameText,
00044 const QString& captionLabel, const QString& captionText,
00045 QWidget * parent, const char * name, WFlags fl)
00046 {
00047 Q_UNUSED( parent );
00048 Q_UNUSED( name );
00049 Q_UNUSED( fl );
00050
00051 init(message, nameLabel, nameText, captionLabel, captionText);
00052 }
00053
00054 void KexiNameWidget::init(
00055 const QString& message,
00056 const QString& nameLabel, const QString& nameText,
00057 const QString& captionLabel, const QString& captionText)
00058 {
00059 Q_UNUSED( captionText );
00060
00061 m_le_name_txtchanged_disable = false;
00062 m_le_name_autofill = true;
00063 m_caption_required = false;
00064
00065 lyr = new QGridLayout( this, 1, 1, 0, 6, "lyr");
00066
00067 lbl_message = new QLabel( this, "message" );
00068 setMessageText( message );
00069 lbl_message->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00070 lbl_message->setAlignment( QLabel::AlignTop | QLabel::WordBreak );
00071 lyr->addMultiCellWidget( lbl_message, 0, 0, 0, 1 );
00072
00073 lbl_caption = new QLabel( captionLabel.isEmpty() ? i18n( "Caption:" ) : captionLabel,
00074 this, "lbl_caption" );
00075 lyr->addWidget( lbl_caption, 1, 0 );
00076
00077 lbl_name = new QLabel( nameLabel.isEmpty() ? tr( "Name:" ) : nameLabel,
00078 this, "lbl_name" );
00079 lyr->addWidget( lbl_name, 2, 0 );
00080
00081 le_caption = new KLineEdit( nameText, this, "le_caption" );
00082 le_caption->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0));
00083 lyr->addWidget( le_caption, 1, 1 );
00084
00085 le_name = new KLineEdit( nameText, this, "le_name" );
00086 le_name->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed,1,0));
00087 Validator *idValidator = new IdentifierValidator(0, "id_val");
00088 le_name->setValidator( m_validator = new MultiValidator(idValidator, this, "val") );
00089 lyr->addWidget( le_name, 2, 1 );
00090
00091 setFocusProxy(le_caption);
00092 resize( QSize(342, 123).expandedTo(minimumSizeHint()) );
00093
00094 m_nameWarning = i18n("Please enter the name.");
00095 m_captionWarning = i18n("Please enter the caption.");
00096
00097 connect(le_caption, SIGNAL(textChanged(const QString&)),
00098 this,SLOT(slotCaptionTxtChanged(const QString&)));
00099 connect(le_name, SIGNAL(textChanged(const QString&)),
00100 this,SLOT(slotNameTxtChanged(const QString&)));
00101 connect(le_caption, SIGNAL(returnPressed()),
00102 this,SIGNAL(returnPressed()));
00103 connect(le_name, SIGNAL(returnPressed()),
00104 this,SIGNAL(returnPressed()));
00105 }
00106
00107 KexiNameWidget::~KexiNameWidget()
00108 {
00109 }
00110
00111 void KexiNameWidget::slotCaptionTxtChanged(const QString &capt)
00112 {
00113 emit textChanged();
00114 if (le_name->text().isEmpty())
00115 m_le_name_autofill=true;
00116 if (m_le_name_autofill) {
00117 m_le_name_txtchanged_disable = true;
00118 le_name->setText( string2Identifier(capt).lower() );
00119 m_le_name_txtchanged_disable = false;
00120 }
00121 }
00122
00123 void KexiNameWidget::slotNameTxtChanged(const QString &)
00124 {
00125 emit textChanged();
00126 if (m_le_name_txtchanged_disable)
00127 return;
00128 m_le_name_autofill = false;
00129 }
00130
00131 void KexiNameWidget::clear()
00132 {
00133 le_name->clear();
00134 le_caption->clear();
00135 }
00136
00137 bool KexiNameWidget::empty() const
00138 {
00139 return le_name->text().isEmpty() || le_caption->text().stripWhiteSpace().isEmpty();
00140 }
00141
00142 void KexiNameWidget::setNameRequired( bool set )
00143 { m_validator->setAcceptsEmptyValue(!set); }
00144
00145 bool KexiNameWidget::isNameRequired() const
00146 { return !m_validator->acceptsEmptyValue(); }
00147
00148 void KexiNameWidget::setCaptionText(const QString& capt)
00149 {
00150 le_caption->setText(capt);
00151 m_le_name_autofill = true;
00152 }
00153
00154 void KexiNameWidget::setNameText(const QString& name)
00155 {
00156 le_name->setText(name);
00157 m_le_name_autofill = true;
00158 }
00159
00160 void KexiNameWidget::setMessageText(const QString& msg)
00161 {
00162 if (msg.stripWhiteSpace().isEmpty()) {
00163 lbl_message->setText("");
00164 lbl_message->hide();
00165 } else {
00166 lbl_message->setText(msg.stripWhiteSpace()+"<br>");
00167 lbl_message->show();
00168 }
00169 messageChanged();
00170 }
00171
00172 QString KexiNameWidget::captionText() const
00173 {
00174 return le_caption->text();
00175 }
00176
00177 QString KexiNameWidget::nameText() const
00178 {
00179 return le_name->text().lower();
00180 }
00181
00182 bool KexiNameWidget::checkValidity()
00183 {
00184 if (isNameRequired() && le_name->text().stripWhiteSpace().isEmpty()) {
00185 KMessageBox::sorry(0, m_nameWarning);
00186 le_name->setFocus();
00187 return false;
00188 }
00189 if (isCaptionRequired() && le_caption->text().stripWhiteSpace().isEmpty()) {
00190 KMessageBox::sorry(0, m_captionWarning);
00191 le_caption->setFocus();
00192 return false;
00193 }
00194 QString dummy, message, details;
00195 if (m_validator->check(dummy, le_name->text(), message, details)
00196 ==Validator::Error) {
00197 KMessageBox::detailedSorry(0, message, details);
00198 le_name->setFocus();
00199 return false;
00200 }
00201 return true;
00202 }
00203
00204 Validator *KexiNameWidget::nameValidator() const
00205 {
00206 return m_validator;
00207 }
00208
00209 void KexiNameWidget::addNameSubvalidator( Validator* validator, bool owned )
00210 {
00211 m_validator->addSubvalidator( validator, owned );
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 #include "kexinamewidget.moc"
00236