summaryrefslogtreecommitdiffstats
path: root/part/kxeelementdialog.cpp
blob: b5006d0195ca0fcfdaebe619683607012e3d8aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
                          kxeelementdialog.cpp  -  description
                             -------------------
    begin                : Mit Apr 17 2002
    copyright            : (C) 2002, 2003 by The KXMLEditor Team
    email                : lvanek@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kxeelementdialog.h"

#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqpushbutton.h>

#include <klineedit.h>
#include <kdebug.h>
#include <tdelocale.h>

KXEElementDialog::KXEElementDialog( TQWidget * pParent, const char * pszName, bool fModal, WFlags fl )
	: KXEElementDialogBase( pParent, pszName, fModal, fl )
{
	connect( m_pEditNsURI, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(slotNsURIChanged(const TQString&)) );
	connect( m_pEditName, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(slotNameChanged(const TQString&)) );
}

void KXEElementDialog::clearDialog()
{
	m_pEditNsURI->clear();
	m_pEditNsURI->setEnabled(true);
	m_pEditNsURI->show();
	m_pLblNsURI->setEnabled(true);
	m_pLblNsURI->show();

	m_pEditPrefix->clear();
	m_pEditPrefix->setDisabled(true);
	m_pEditPrefix->show();
	m_pLblPrefix->setEnabled(true);
	m_pLblPrefix->show();

	m_pEditName->clear();

	m_pComboInsert->setCurrentItem(0);
}

void KXEElementDialog::fillDialog( bool bNsURIIsNull )
{
	m_pEditNsURI->setDisabled(true);

	if ( bNsURIIsNull )
	{
		m_pEditNsURI->hide();
		m_pLblNsURI->hide();
		m_pLblNsURI->setDisabled(true);

		m_pEditPrefix->setDisabled(true);
		m_pEditPrefix->hide();
		m_pLblPrefix->hide();
		m_pLblPrefix->setDisabled(true);
	}
	else
	{
		m_pEditNsURI->setText( m_strNsURI );
		m_pEditNsURI->show();
		m_pLblNsURI->show();
		m_pLblNsURI->setEnabled(true);

		m_pEditPrefix->setText( m_strPrefix );
		m_pEditPrefix->setEnabled(true);
		m_pEditPrefix->show();
		m_pLblPrefix->show();
		m_pLblPrefix->setEnabled(true);
	}

	m_pComboInsert->hide();
	m_pComboInsert->setDisabled(true);
	m_pLblInsert->hide();
	m_pLblInsert->setDisabled(true);

	m_pEditName->setText( m_strName );
}

int KXEElementDialog::exec( bool bEditExisting, bool bRootElement, bool bNsURIIsNull )
{
  if(bEditExisting)
  {
    fillDialog( bNsURIIsNull );
  }
  else
  {
    if( bRootElement )
    {
      m_pComboInsert->hide();
      m_pComboInsert->setDisabled(true);
      m_pLblInsert->hide();
      m_pLblInsert->setDisabled(true);
    }
  
    clearDialog();
  }

	int iReturn = exec();
	if ( iReturn == Accepted )
	{
		m_strNsURI = m_pEditNsURI->text();
    m_strPrefix = m_pEditPrefix->text();
    m_strName = m_pEditName->text();

    m_bAtTop = ( m_pComboInsert->currentItem() == 0 );
	}

	return iReturn;
}

int KXEElementDialog::exec()
{
	if ( m_pEditName->text().isEmpty() )
		m_pBtnOK->setEnabled(false);
	else
		m_pBtnOK->setEnabled(true);
		
	m_pEditName->setFocus();
	m_pBtnOK->setDefault(true);

	return KXEElementDialogBase::exec();
}

void KXEElementDialog::slotNsURIChanged( const TQString & strNewNsURI )
{
	if ( strNewNsURI.isEmpty() )
		m_pEditPrefix->setEnabled(false);
	else
		m_pEditPrefix->setEnabled(true);
}

void KXEElementDialog::slotNameChanged( const TQString & strNewName )
{
  TQString strMessage = checkName(strNewName);

  m_pTextLabelMessage->setText(strMessage);
  
	if ( strNewName.isEmpty()  || (strMessage.length() > 0))
		m_pBtnOK->setEnabled(false);
	else
		m_pBtnOK->setEnabled(true);
}

// Check, if XML element name is OK
TQString KXEElementDialog::checkName(const TQString strElementName)
{
  if(strElementName.length() == 0)
    return  "";
    
  // test for space
  if(strElementName.find(' ') >= 0)
    return i18n("Element name cannot contain space !");

  // test for xml, XML ... on start   
  if(strElementName.find("xml", 0, false) == 0)
    return i18n("Element name cannot start with 'xml' or 'XML' !");

  // check first character
  TQChar firstChar(strElementName[0]);
  if((firstChar != '_') && !firstChar.isLetter())
  {
    return i18n("Element name must start with an underscore or a letter !");
  }
    
  // Forbidden characters
  TQString strForbiddenChars("&@#$%^()%+?=:<>;\"'*");
  for(unsigned int i = 0; i < strForbiddenChars.length(); i++)
  {
    TQChar ch = strForbiddenChars[i];

    if(strElementName.find(ch) >= 0)
      return i18n("Element name cannot contain character: %1 !").arg(ch);
  }

  return "";
}

#include "kxeelementdialog.moc"