Kate
exporterpluginview.cpp
Go to the documentation of this file.
00001 00024 #include "exporterpluginview.h" 00025 00026 #include "exporterplugin.h" 00027 #include "abstractexporter.h" 00028 #include "htmlexporter.h" 00029 00030 #include <ktexteditor/document.h> 00031 #include <ktexteditor/view.h> 00032 #include <ktexteditor/highlightinterface.h> 00033 00034 #include <kactioncollection.h> 00035 #include <kaction.h> 00036 #include <kfiledialog.h> 00037 #include <ktemporaryfile.h> 00038 #include <ksavefile.h> 00039 #include <kio/netaccess.h> 00040 00041 #include <QtCore/QMimeData> 00042 #include <QtGui/QApplication> 00043 #include <QtGui/QClipboard> 00044 #include <QtCore/QTextCodec> 00045 00046 #include <kdebug.h> 00047 00048 ExporterPluginView::ExporterPluginView(KTextEditor::View* view) 00049 : QObject(view), KXMLGUIClient(view), m_view(view) 00050 { 00051 setComponentData( ExporterPluginFactory::componentData() ); 00052 setXMLFile("ktexteditor_exporterui.rc"); 00053 00054 m_copyAction = actionCollection()->addAction("edit_copy_html", this, SLOT(exportToClipboard())); 00055 m_copyAction->setIcon(KIcon("edit-copy")); 00056 m_copyAction->setText(i18n("Copy as &HTML")); 00057 m_copyAction->setWhatsThis(i18n("Use this command to copy the currently selected text as HTML to the system clipboard.")); 00058 m_copyAction->setEnabled(m_view->selection()); 00059 00060 m_fileExportAction = actionCollection()->addAction("file_export_html", this, SLOT(exportToFile())); 00061 m_fileExportAction->setText(i18n("E&xport as HTML...")); 00062 m_fileExportAction->setWhatsThis(i18n("This command allows you to export the current document" 00063 " with all highlighting information into a HTML document.")); 00064 00065 connect(m_view, SIGNAL(selectionChanged(KTextEditor::View*)), 00066 this, SLOT(updateSelectionAction(KTextEditor::View*))); 00067 } 00068 00069 ExporterPluginView::~ExporterPluginView() 00070 { 00071 } 00072 00073 void ExporterPluginView::updateSelectionAction(KTextEditor::View* view) 00074 { 00075 Q_ASSERT(view == m_view); Q_UNUSED(view) 00076 m_copyAction->setEnabled(m_view->selection()); 00077 } 00078 00079 void ExporterPluginView::exportToClipboard() 00080 { 00081 if (!m_view->selection()) { 00082 return; 00083 } 00084 00085 QMimeData *data = new QMimeData(); 00086 00087 QString s; 00088 QTextStream output( &s, QIODevice::WriteOnly ); 00089 exportData(true, output); 00090 00091 data->setHtml(s); 00092 data->setText(s); 00093 00094 QApplication::clipboard()->setMimeData(data); 00095 } 00096 00097 void ExporterPluginView::exportToFile() 00098 { 00099 KUrl url = KFileDialog::getSaveUrl(m_view->document()->documentName(), "text/html", 00100 m_view, i18n("Export File as HTML")); 00101 00102 if ( url.isEmpty() ) { 00103 return; 00104 } 00105 00106 QString filename; 00107 00108 if ( url.isLocalFile() ) { 00109 filename = url.toLocalFile(); 00110 } else { 00112 KTemporaryFile tmp; // ### only used for network export 00113 tmp.setAutoRemove(false); 00114 tmp.open(); 00115 filename = tmp.fileName(); 00116 } 00117 00118 KSaveFile savefile(filename); 00119 if (savefile.open()) { 00120 QTextStream outputStream ( &savefile ); 00121 00122 exportData(false, outputStream); 00123 00124 savefile.finalize(); //check error? 00125 } 00126 // else 00127 // {/*ERROR*/} 00128 00129 if ( !url.isLocalFile() ) { 00130 KIO::NetAccess::upload( filename, url, 0 ); 00131 } 00132 } 00133 00134 void ExporterPluginView::exportData(const bool useSelection, QTextStream &output) 00135 { 00136 const KTextEditor::Range range = useSelection ? m_view->selectionRange() : m_view->document()->documentRange(); 00137 const bool blockwise = useSelection ? m_view->blockSelection() : false; 00138 00139 if ( (blockwise || range.onSingleLine()) && (range.start().column() > range.end().column() ) ) { 00140 return; 00141 } 00142 00143 //outputStream.setEncoding(QTextStream::UnicodeUTF8); 00144 output.setCodec(QTextCodec::codecForName("UTF-8")); 00145 00147 AbstractExporter* exporter; 00148 00149 exporter = new HTMLExporter(m_view, output, !useSelection); 00150 00151 KTextEditor::HighlightInterface* hiface = qobject_cast<KTextEditor::HighlightInterface*>(m_view->document()); 00152 00153 const KTextEditor::Attribute::Ptr noAttrib(0); 00154 00155 for (int i = range.start().line(); (i <= range.end().line()) && (i < m_view->document()->lines()); ++i) 00156 { 00157 const QString &line = m_view->document()->line(i); 00158 00159 QList<KTextEditor::HighlightInterface::AttributeBlock> attribs; 00160 if ( hiface ) { 00161 attribs = hiface->lineAttributes(i); 00162 } 00163 00164 int lineStart = 0; 00165 int remainingChars = line.length(); 00166 if ( blockwise || range.onSingleLine() ) { 00167 lineStart = range.start().column(); 00168 remainingChars = range.columnWidth(); 00169 } else if ( i == range.start().line() ) { 00170 lineStart = range.start().column(); 00171 } else if ( i == range.end().line() ) { 00172 remainingChars = range.end().column(); 00173 } 00174 00175 int handledUntil = lineStart; 00176 00177 foreach ( const KTextEditor::HighlightInterface::AttributeBlock& block, attribs ) { 00178 // honor (block-) selections 00179 if ( block.start + block.length <= lineStart ) { 00180 continue; 00181 } else if ( block.start >= lineStart + remainingChars ) { 00182 break; 00183 } 00184 int start = qMax(block.start, lineStart); 00185 if ( start > handledUntil ) { 00186 exporter->exportText( line.mid( handledUntil, start - handledUntil ), noAttrib ); 00187 } 00188 int length = qMin(block.length, remainingChars); 00189 exporter->exportText( line.mid( start, length ), block.attribute); 00190 handledUntil = start + length; 00191 } 00192 00193 if ( handledUntil < lineStart + remainingChars ) { 00194 exporter->exportText( line.mid( handledUntil, remainingChars ), noAttrib ); 00195 } 00196 00197 exporter->closeLine(i == range.end().line()); 00198 } 00199 00200 delete exporter; 00201 00202 output.flush(); 00203 } 00204 00205 #include "exporterpluginview.moc" 00206 00207 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference