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 <QtGui>
#include <QDomDocument> #include <QDomDocument>


#include "Document.h" #include "EditorSettings.h"





XmlformatPlugin::XmlformatPlugin() : QObject(), JuffPlugin() XmlformatPlugin::XmlformatPlugin() : QObject(), JuffPlugin()
{ {
actDoc = new QAction(QIcon(":xmlwrap"), tr("Format XML Document"), this); 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() { void XmlformatPlugin::init() {
Expand Down Expand Up @@ -71,30 +70,72 @@ Juff::ActionList XmlformatPlugin::mainMenuActions(Juff::MenuID id) const
return list; return list;
} }


void XmlformatPlugin::formatDocument() void XmlformatPlugin::format()
{ {
Juff::Document* doc = api()->currentDocument(); Juff::Document* doc = api()->currentDocument();
// api()->currentDocument() never returns NULL, it returns // api()->currentDocument() never returns NULL, it returns
// NullDoc if there is no docs. // NullDoc if there is no docs.
if ( doc->isNull() ) if ( doc->isNull() )
return; 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; QString content;
QDomDocument dom;
QString errmsg;
int errline, errcolumn;

if (! doc->getText(content)) if (! doc->getText(content))
return; 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; QDomDocument dom;
QString errmsg; QString errmsg;
int errline, errcolumn; 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)) if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{ {
QString newContent = dom.toString(4); QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
doc->setText(newContent); doc->replaceSelectedText(newContent);
} }
else else
{ {
QMessageBox::information(0, tr("XML format error"), 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/>" + "<br/>"
+ errmsg); + errmsg);
} }
Expand Down
7 changes: 6 additions & 1 deletion plugins/xmlformat/xmlformat.h
Expand Up @@ -25,6 +25,8 @@
#include <QtCore/QObject> #include <QtCore/QObject>


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



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


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


private: private:
QAction * actDoc; QAction * actDoc;

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


#endif #endif

0 comments on commit b89be5d

Please sign in to comment.