summaryrefslogtreecommitdiffstats
path: root/part/kxetexteditordialog.cpp
blob: d4b35b86b2b0a88a998089f31c65455dc7d0c523 (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
/***************************************************************************
                          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 <qdom.h>
#include <qpushbutton.h>

#include <kmessagebox.h>
#include <ktextedit.h>
#include <klocale.h>
#include <kdebug.h>

KXETextEditorDialog::KXETextEditorDialog(QWidget *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)
{
  QString strXML = "<root>" + editorText()  + "</root>";

	// create XML documemt from text
	QString strErrorMsg;
  int iErrorLine, iErrorColumn;
	QDomDocument 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(QScrollView::AlwaysOff);
		m_pTextEditor->setWordWrap(QTextEdit::WidgetWidth);
	}

	m_pSyntaxHighlighter->rehighlight();
}