00001
#include "astyle_part.h"
00002
00003
#include <qwhatsthis.h>
00004
#include <qvbox.h>
00005
#include <qtextstream.h>
00006
00007
#include <kdeversion.h>
00008
#include <kdebug.h>
00009
#include <kdialogbase.h>
00010
#include <kdevgenericfactory.h>
00011
#include <kiconloader.h>
00012
#include <klocale.h>
00013
#include <kparts/part.h>
00014
#include <kparts/partmanager.h>
00015
#include <ktexteditor/editinterface.h>
00016
#include <ktexteditor/document.h>
00017
#include <ktexteditor/viewcursorinterface.h>
00018
00019
#include <kdevcore.h>
00020
#include <kdevapi.h>
00021
#include <kdevpartcontroller.h>
00022
00023
#include "astyle_widget.h"
00024
#include <astyle_adaptor.h>
00025
00026
static const KAboutData data(
"kdevastyle",
I18N_NOOP(
"Reformat Source"),
"1.0");
00027
00028 typedef KDevGenericFactory<AStylePart> AStyleFactory;
00029 K_EXPORT_COMPONENT_FACTORY( libkdevastyle,
AStyleFactory( &data ) )
00030
00031
AStylePart::
AStylePart(
QObject *parent, const
char *name, const
QStringList &)
00032 :
KDevSourceFormatter("AStyle", "astyle", parent, name ? name : "
AStylePart")
00033 {
00034 setInstance(AStyleFactory::instance());
00035
00036 setXMLFile(
"kdevpart_astyle.rc");
00037
00038 _action =
new KAction(i18n(
"&Reformat Source"), 0,
00039
this, SLOT(beautifySource()), actionCollection(),
"edit_astyle");
00040 _action->setEnabled(
false);
00041 _action->setToolTip(i18n(
"Reformat source"));
00042 _action->setWhatsThis(i18n(
"<b>Reformat source</b><p>Source reformatting functionality using <b>astyle</b> library. "
00043
"Also available in <b>New Class</b> and <b>Subclassing</b> wizards."));
00044
00045 connect(core(), SIGNAL(configWidget(
KDialogBase*)),
this, SLOT(configWidget(
KDialogBase*)));
00046
00047 connect(partController(), SIGNAL(activePartChanged(
KParts::Part*)),
this, SLOT(activePartChanged(
KParts::Part*)));
00048 }
00049
00050
00051 AStylePart::~AStylePart()
00052 {
00053 }
00054
00055
00056 void AStylePart::beautifySource()
00057 {
00058
KTextEditor::EditInterface *iface
00059 = dynamic_cast<KTextEditor::EditInterface*>(
partController()->
activePart());
00060
if (!iface)
00061
return;
00062
00063
ASStringIterator is(iface->
text());
00064
KDevFormatter formatter;
00065
00066 formatter.
init(&is);
00067
00068
QString output;
00069
QTextStream os(&output, IO_WriteOnly);
00070
00071
while (formatter.
hasMoreLines())
00072 os << QString::fromUtf8(formatter.
nextLine().c_str()) <<
endl;
00073
00074 uint col = 0;
00075 uint line = 0;
00076
cursorPos(
partController()->activePart(), &col, &line );
00077
00078
00079
#if defined(KDE_MAKE_VERSION)
00080
# if KDE_VERSION > KDE_MAKE_VERSION(3,1,2)
00081
iface->
setText( output );
00082
# else
00083
iface->
removeText( 0, 0, iface->
numLines()-1, UINT_MAX);
00084 iface->
insertText( 0, 0, output);
00085
# endif
00086
#else
00087
iface->
removeText( 0, 0, iface->
numLines()-1, UINT_MAX);
00088 iface->
insertText( 0, 0, output);
00089
#endif
00090
00091
setCursorPos(
partController()->activePart(), col, line );
00092 }
00093
00094
00095 void AStylePart::configWidget(
KDialogBase *dlg)
00096 {
00097
QVBox *vbox = dlg->
addVBoxPage(i18n(
"Source Formatter"));
00098
AStyleWidget *w =
new AStyleWidget(vbox,
"astyle config widget");
00099 connect(dlg, SIGNAL(okClicked()), w, SLOT(accept()));
00100 }
00101
00102
00103 void AStylePart::activePartChanged(
KParts::Part *part)
00104 {
00105
bool enabled =
false;
00106
00107
KParts::ReadWritePart *rw_part = dynamic_cast<KParts::ReadWritePart*>(part);
00108
00109
if (rw_part)
00110 {
00111
KTextEditor::EditInterface *iface = dynamic_cast<KTextEditor::EditInterface*>(rw_part);
00112
00113
if (iface)
00114 {
00115
QString extension = rw_part->
url().
path();
00116
00117
int pos = extension.findRev(
'.');
00118
if (pos >= 0)
00119 extension = extension.mid(pos);
00120
if (extension ==
".h" || extension ==
".c" || extension ==
".java"
00121 || extension ==
".cpp" || extension ==
".hpp"
00122 || extension ==
".C" || extension ==
".H"
00123 || extension ==
".cxx" || extension ==
".hxx"
00124 || extension ==
".inl" || extension ==
".tlh"
00125 || extension ==
".moc" || extension ==
".xpm"
00126 || extension ==
".diff"|| extension ==
".patch"
00127 || extension ==
".hh" || extension ==
".cc"
00128 || extension ==
".c++" || extension ==
".h++")
00129 enabled =
true;
00130 }
00131 }
00132
00133
_action->
setEnabled(enabled);
00134 }
00135
00136 QString AStylePart::formatSource(
const QString text )
00137 {
00138
ASStringIterator is(
text);
00139
KDevFormatter formatter;
00140
00141 formatter.
init(&is);
00142
00143
QString output;
00144
QTextStream os(&output, IO_WriteOnly);
00145
00146
while (formatter.
hasMoreLines())
00147 os << QString::fromUtf8(formatter.
nextLine().c_str()) <<
endl;
00148
00149
return output;
00150 }
00151
00152 void AStylePart::cursorPos(
KParts::Part *part, uint * line, uint * col )
00153 {
00154
if (!part || !part->inherits(
"KTextEditor::Document"))
return;
00155
00156
KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(part->
widget());
00157
if (iface)
00158 {
00159 iface->
cursorPositionReal( line, col );
00160 }
00161 }
00162
00163 void AStylePart::setCursorPos(
KParts::Part *part, uint line, uint col )
00164 {
00165
if (!part || !part->inherits(
"KTextEditor::Document"))
return;
00166
00167
KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(part->
widget());
00168
if (iface)
00169 {
00170 iface->
setCursorPositionReal( line, col );
00171 }
00172 }
00173
00174
#include "astyle_part.moc"