summaryrefslogtreecommitdiffstats
path: root/part/kxetexteditordialog.cpp
blob: 18c782c4c6265849c448f8daaa21d914274bdb38 (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
/***************************************************************************
                          kxetexteditordialog.cpp  -  description
                             -------------------
    begin                : Ne pro 14 2003
    copyright            : (C) 2003 by The KXMLEditor Team
    email                : lvanek.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 "kxetexteditordialog.h"

#include "kxmleditorfactory.h"
#include "kxeconfiguration.h"
#include "kxetextviewsettings.h"

#include <tqdom.h>
#include <tqpushbutton.h>

#include <tdemessagebox.h>
#include <ktextedit.h>
#include <tdelocale.h>
#include <kdebug.h>

KXETextEditorDialog::KXETextEditorDialog(TQWidget *parent, const char *name)
  : KXETextEditorDialogBase(parent,name)
{
  m_pSyntaxHighlighter = new KXESyntaxHighlighter(m_pTextEditor);

  connect( m_pTextEditor, SIGNAL(textChanged()), this, SLOT(slotTextChanged()) );

  // Apply current configuration
  slotTextViewSettingsChanged();
  // and make sure to be informed about its changes.
  connect( KXMLEditorFactory::configuration()->textview(), SIGNAL(sigChanged()), this, SLOT(slotTextViewSettingsChanged()) );
}

KXETextEditorDialog::~KXETextEditorDialog()
{
  delete m_pSyntaxHighlighter;
}

void KXETextEditorDialog::slotTextChanged()
{
	if ( m_pTextEditor->text().isEmpty())
		m_pButtonOk->setEnabled(false);
	else
		m_pButtonOk->setEnabled(true);
}

void KXETextEditorDialog::slotValidate()
{
  validateXml(true);
}

void KXETextEditorDialog::accept()
{
  if(validateXml(false))
    KXETextEditorDialogBase::accept();
}

bool KXETextEditorDialog::validateXml(bool bInfoIfOK)
{
  TQString strXML = "<root>" + editorText()  + "</root>";

	// create XML documemt from text
	TQString strErrorMsg;
  int iErrorLine, iErrorColumn;
	TQDomDocument doc;

 	if(!doc.setContent(strXML, true, &strErrorMsg, &iErrorLine, &iErrorColumn) )
 	{ kdDebug() << "KXETextEditorDialog::validateXml: Failed parsing the file." << endl;

    KMessageBox::error(this,
									 		i18n("%1 in line %2, column %3").arg(strErrorMsg).arg(iErrorLine).arg(iErrorColumn),
									 		i18n("Parsing error !"));


    m_pTextEditor->setCursorPosition(iErrorLine - 1, iErrorColumn - 1);

    return false;
	}

  // check if root item already exists
	if(!doc.firstChild().firstChild().isElement())
		{ KMessageBox::sorry(this, i18n("You are changed root element to another node type, while editing !"));
			return false;
		}

  if(bInfoIfOK)
    KMessageBox::information(this, i18n("OK"));

  return true;
}

void KXETextEditorDialog::slotTextViewSettingsChanged()
{
	m_pSyntaxHighlighter->setColorDefaultText( KXMLEditorFactory::configuration()->textview()->colorDfltText() );
	m_pSyntaxHighlighter->setColorElementNames( KXMLEditorFactory::configuration()->textview()->colorElemNames() );
	m_pSyntaxHighlighter->setColorAttributeNames( KXMLEditorFactory::configuration()->textview()->colorAttrNames() );
	m_pSyntaxHighlighter->setColorAttributeValues( KXMLEditorFactory::configuration()->textview()->colorAttrValues() );
	m_pSyntaxHighlighter->setColorXmlSyntaxChars( KXMLEditorFactory::configuration()->textview()->colorSyntaxChars() );
	m_pSyntaxHighlighter->setColorComments( KXMLEditorFactory::configuration()->textview()->colorComments() );
	m_pSyntaxHighlighter->setColorSyntaxError( KXMLEditorFactory::configuration()->textview()->colorErrors() );
	
	if(KXMLEditorFactory::configuration()->textview()->isWrapOn())
	{
		m_pTextEditor->setHScrollBarMode(TQScrollView::AlwaysOff);
		m_pTextEditor->setWordWrap(TQTextEdit::WidgetWidth);
	}

	m_pSyntaxHighlighter->rehighlight();
}

#include "kxetexteditordialog.moc"