Skip to content

Commit

Permalink
Merge pull request #2 from pvanek/master
Browse files Browse the repository at this point in the history
xmlformat improvements (selection) as discussed on jabber
  • Loading branch information
Mezomish committed Feb 3, 2012
2 parents 3119850 + 9602d07 commit b89be5d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
55 changes: 48 additions & 7 deletions plugins/xmlformat/xmlformat.cpp
Expand Up @@ -25,14 +25,13 @@
#include <QtGui>
#include <QDomDocument>

#include "Document.h"

#include "EditorSettings.h"


XmlformatPlugin::XmlformatPlugin() : QObject(), JuffPlugin()
{
actDoc = new QAction(QIcon(":xmlwrap"), tr("Format XML Document"), this);
connect(actDoc, SIGNAL(triggered()), this, SLOT(formatDocument()));
connect(actDoc, SIGNAL(triggered()), this, SLOT(format()));
}

void XmlformatPlugin::init() {
Expand Down Expand Up @@ -71,30 +70,72 @@ Juff::ActionList XmlformatPlugin::mainMenuActions(Juff::MenuID id) const
return list;
}

void XmlformatPlugin::formatDocument()
void XmlformatPlugin::format()
{
Juff::Document* doc = api()->currentDocument();
// api()->currentDocument() never returns NULL, it returns
// NullDoc if there is no docs.
if ( doc->isNull() )
return;

// if there is a selection - try to format a selection.
// In the other case format all document
if (doc->hasSelectedText())
{
formatSelection(doc);
}
else
{
formatDocument(doc);
}
}

void XmlformatPlugin::formatDocument(Juff::Document *doc)
{
QString content;
QDomDocument dom;
QString errmsg;
int errline, errcolumn;

if (! doc->getText(content))
return;

if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
doc->setText(newContent);
}
else
{
QMessageBox::information(0, tr("XML format error"),
tr("Cannot format XML due error (line: %1, column: %2)").arg(errline).arg(errcolumn)
+ "<br/>"
+ errmsg);
}
}

void XmlformatPlugin::formatSelection(Juff::Document *doc)
{
QString content;
QDomDocument dom;
QString errmsg;
int errline, errcolumn;

if (! doc->getSelectedText(content))
return;

int line, column, tmp1, tmp2;
doc->getSelection(line, column, tmp1, tmp2);

if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
QString newContent = dom.toString(4);
doc->setText(newContent);
QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
doc->replaceSelectedText(newContent);
}
else
{
QMessageBox::information(0, tr("XML format error"),
tr("Cannot format XML due error (line: %1, column: %2)").arg(errline).arg(errcolumn)
tr("Cannot format XML due error (line: %1, column: %2)").arg(errline+line).arg(errline==1 ? errcolumn+column : errcolumn)
+ "<br/>"
+ errmsg);
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/xmlformat/xmlformat.h
Expand Up @@ -25,6 +25,8 @@
#include <QtCore/QObject>

#include "JuffPlugin.h"
#include "Document.h"


/*! Try to format content of the current dcument as a XML or
similar markup language (HTML, SGML,...)
Expand All @@ -47,10 +49,13 @@ class XmlformatPlugin : public QObject, public JuffPlugin {
Juff::ActionList mainMenuActions(Juff::MenuID) const;

public slots:
void formatDocument();
void format();

private:
QAction * actDoc;

void formatDocument(Juff::Document *doc);
void formatSelection(Juff::Document *doc);
};

#endif

0 comments on commit b89be5d

Please sign in to comment.