kexi

kexinamewidget.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexinamewidget.h"
21
22#include <tqlabel.h>
23#include <tqlayout.h>
24
25#include <klineedit.h>
26#include <tdemessagebox.h>
27#include <tdelocale.h>
28
29#include <kexiutils/validator.h>
30#include <kexiutils/identifier.h>
31#include <core/kexi.h>
32
33using namespace KexiUtils;
34
35KexiNameWidget::KexiNameWidget( const TQString& message,
36 TQWidget* parent, const char* name, WFlags fl )
37 : TQWidget(parent, name, fl)
38{
39 init(message, TQString(), TQString(), TQString(), TQString());
40}
41
42KexiNameWidget::KexiNameWidget(const TQString& message,
43 const TQString& nameLabel, const TQString& nameText,
44 const TQString& captionLabel, const TQString& captionText,
45 TQWidget * parent, const char * name, WFlags fl)
46{
47 Q_UNUSED( parent );
48 Q_UNUSED( name );
49 Q_UNUSED( fl );
50
51 init(message, nameLabel, nameText, captionLabel, captionText);
52}
53
54void KexiNameWidget::init(
55 const TQString& message,
56 const TQString& nameLabel, const TQString& nameText,
57 const TQString& captionLabel, const TQString& captionText)
58{
59 Q_UNUSED( captionText );
60
61 m_le_name_txtchanged_disable = false;
62 m_le_name_autofill = true;
63 m_caption_required = false;
64
65 lyr = new TQGridLayout( this, 1, 1, 0, 6, "lyr");
66
67 lbl_message = new TQLabel( this, "message" );
68 setMessageText( message );
69 lbl_message->setSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed);
70 lbl_message->setAlignment( TQLabel::AlignTop | TQLabel::WordBreak );
71 lyr->addMultiCellWidget( lbl_message, 0, 0, 0, 1 );
72
73 lbl_caption = new TQLabel( captionLabel.isEmpty() ? i18n( "Caption:" ) : captionLabel,
74 this, "lbl_caption" );
75 lyr->addWidget( lbl_caption, 1, 0 );
76
77 lbl_name = new TQLabel( nameLabel.isEmpty() ? i18n( "Name:" ) : nameLabel,
78 this, "lbl_name" );
79 lyr->addWidget( lbl_name, 2, 0 );
80
81 le_caption = new KLineEdit( nameText, this, "le_caption" );
82 le_caption->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed, 1, 0));
83 lyr->addWidget( le_caption, 1, 1 );
84
85 le_name = new KLineEdit( nameText, this, "le_name" );
86 le_name->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed,1,0));
87 Validator *idValidator = new IdentifierValidator(0, "id_val");
88 le_name->setValidator( m_validator = new MultiValidator(idValidator, this, "val") );
89 lyr->addWidget( le_name, 2, 1 );
90
91 setFocusProxy(le_caption);
92 resize( TQSize(342, 123).expandedTo(minimumSizeHint()) );
93
94 m_nameWarning = i18n("Please enter the name.");
95 m_captionWarning = i18n("Please enter the caption.");
96
97 connect(le_caption, TQ_SIGNAL(textChanged(const TQString&)),
98 this,TQ_SLOT(slotCaptionTxtChanged(const TQString&)));
99 connect(le_name, TQ_SIGNAL(textChanged(const TQString&)),
100 this,TQ_SLOT(slotNameTxtChanged(const TQString&)));
101 connect(le_caption, TQ_SIGNAL(returnPressed()),
102 this,TQ_SIGNAL(returnPressed()));
103 connect(le_name, TQ_SIGNAL(returnPressed()),
104 this,TQ_SIGNAL(returnPressed()));
105}
106
107KexiNameWidget::~KexiNameWidget()
108{
109}
110
111void KexiNameWidget::slotCaptionTxtChanged(const TQString &capt)
112{
113 emit textChanged();
114 if (le_name->text().isEmpty())
115 m_le_name_autofill=true;
116 if (m_le_name_autofill) {
117 m_le_name_txtchanged_disable = true;
118 le_name->setText( string2Identifier(capt).lower() );
119 m_le_name_txtchanged_disable = false;
120 }
121}
122
123void KexiNameWidget::slotNameTxtChanged(const TQString &)
124{
125 emit textChanged();
126 if (m_le_name_txtchanged_disable)
127 return;
128 m_le_name_autofill = false;
129}
130
131void KexiNameWidget::clear()
132{
133 le_name->clear();
134 le_caption->clear();
135}
136
137bool KexiNameWidget::empty() const
138{
139 return le_name->text().isEmpty() || le_caption->text().stripWhiteSpace().isEmpty();
140}
141
142void KexiNameWidget::setNameRequired( bool set )
143{ m_validator->setAcceptsEmptyValue(!set); }
144
145bool KexiNameWidget::isNameRequired() const
146{ return !m_validator->acceptsEmptyValue(); }
147
148void KexiNameWidget::setCaptionText(const TQString& capt)
149{
150 le_caption->setText(capt);
151 m_le_name_autofill = true;
152}
153
154void KexiNameWidget::setNameText(const TQString& name)
155{
156 le_name->setText(name);
157 m_le_name_autofill = true;
158}
159
160void KexiNameWidget::setMessageText(const TQString& msg)
161{
162 if (msg.stripWhiteSpace().isEmpty()) {
163 lbl_message->setText("");
164 lbl_message->hide();
165 } else {
166 lbl_message->setText(msg.stripWhiteSpace()+"<br>");
167 lbl_message->show();
168 }
169 messageChanged();
170}
171
172TQString KexiNameWidget::captionText() const
173{
174 return le_caption->text();
175}
176
177TQString KexiNameWidget::nameText() const
178{
179 return le_name->text().lower();
180}
181
182bool KexiNameWidget::checkValidity()
183{
184 if (isNameRequired() && le_name->text().stripWhiteSpace().isEmpty()) {
185 KMessageBox::sorry(0, m_nameWarning);
186 le_name->setFocus();
187 return false;
188 }
189 if (isCaptionRequired() && le_caption->text().stripWhiteSpace().isEmpty()) {
190 KMessageBox::sorry(0, m_captionWarning);
191 le_caption->setFocus();
192 return false;
193 }
194 TQString dummy, message, details;
195 if (m_validator->check(dummy, le_name->text(), message, details)
196 ==Validator::Error) {
197 KMessageBox::detailedSorry(0, message, details);
198 le_name->setFocus();
199 return false;
200 }
201 return true;
202}
203
204Validator *KexiNameWidget::nameValidator() const
205{
206 return m_validator;
207}
208
209void KexiNameWidget::addNameSubvalidator( Validator* validator, bool owned )
210{
211 m_validator->addSubvalidator( validator, owned );
212}
213
214/*bool KexiNameWidget::eventFilter( TQObject *obj, TQEvent *ev )
215{
216 if (ev->type()==TQEvent::FocusOut && !acceptsEmptyValue()) {
217 if (obj==le_name) {
218 if (le_name->text().isEmpty()) {
219 KMessageBox::information(0, m_nameWarning);
220 le_name->setFocus();
221 return true;
222 }
223 }
224 else if (obj==le_caption) {
225 if (le_caption->text().isEmpty()) {
226 KMessageBox::information(0, m_captionWarning);
227 le_caption->setFocus();
228 return true;
229 }
230 }
231 }
232 return false;
233}*/
234
235#include "kexinamewidget.moc"
236
A validator extending TQValidator with offline-checking for value's validity.
Definition validator.h:46
General Utils.
Definition identifier.h:27
KEXIUTILS_EXPORT TQString string2Identifier(const TQString &s)