~ubuntu-branches/ubuntu/precise/cantor/precise-proposed

« back to all changes in this revision

Viewing changes to src/textentry.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111215141754-bs6og9414z6kq5tx
Tags: 4:4.7.90-0ubuntu1
* new upstream beta release
* Add build-dep on libqalculate-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "textentry.h"
22
22
#include "worksheetentry.h"
23
23
#include "worksheet.h"
 
24
#include "resultproxy.h"
24
25
#include "lib/defaulthighlighter.h"
 
26
#include "lib/latexrenderer.h"
 
27
 
 
28
#include "formulatextobject.h"
25
29
 
26
30
#include <QEvent>
27
31
#include <QKeyEvent>
28
32
#include <QTextDocumentFragment>
 
33
#include <QUrl>
29
34
 
30
35
#include <kdebug.h>
31
36
#include <kzip.h>
 
37
#include <kmenu.h>
 
38
#include <kicon.h>
 
39
#include <klocale.h>
 
40
#include <kstandardaction.h>
 
41
#include <kaction.h>
32
42
 
33
43
 
34
44
TextEntry::TextEntry(QTextCursor position, Worksheet* parent ) : WorksheetEntry( position, parent )
79
89
    return cursor.selection().isEmpty();
80
90
}
81
91
 
 
92
bool TextEntry::worksheetMouseDoubleClickEvent(QMouseEvent* event, const QTextCursor& cursor)
 
93
{
 
94
    QTextCursor c=cursor;
 
95
 
 
96
    for(int pos=cursor.selectionStart()+1;pos<=cursor.selectionEnd();pos++)
 
97
    {
 
98
        c.setPosition(pos);
 
99
        if (c.charFormat().objectType() == FormulaTextObject::FormulaTextFormat)
 
100
            showLatexCode(c);
 
101
    }
 
102
    return true;
 
103
}
 
104
 
82
105
bool TextEntry::acceptRichText()
83
106
{
84
107
    return true;
91
114
    return true;
92
115
}
93
116
 
 
117
bool TextEntry::worksheetContextMenuEvent(QContextMenuEvent* event, const QTextCursor& cursor)
 
118
{
 
119
    Q_UNUSED(cursor);
 
120
    KMenu* defaultMenu = new KMenu(m_worksheet);
 
121
 
 
122
    defaultMenu->addAction(KStandardAction::cut(m_worksheet));
 
123
    defaultMenu->addAction(KStandardAction::copy(m_worksheet));
 
124
    defaultMenu->addAction(KStandardAction::paste(m_worksheet));
 
125
    defaultMenu->addSeparator();
 
126
 
 
127
    if(!m_worksheet->isRunning())
 
128
        defaultMenu->addAction(KIcon("system-run"),i18n("Evaluate Worksheet"),m_worksheet,SLOT(evaluate()),0);
 
129
    else
 
130
        defaultMenu->addAction(KIcon("process-stop"),i18n("Interrupt"),m_worksheet,SLOT(interrupt()),0);
 
131
    defaultMenu->addSeparator();
 
132
 
 
133
    defaultMenu->addAction(KIcon("edit-delete"),i18n("Remove Entry"), m_worksheet, SLOT(removeCurrentEntry()));
 
134
 
 
135
    createSubMenuInsert(defaultMenu);
 
136
 
 
137
    defaultMenu->popup(event->globalPos());
 
138
 
 
139
    return true;
 
140
}
 
141
 
 
142
 
94
143
void TextEntry::setContent(const QString& content)
95
144
{
96
145
    firstValidCursorPosition().insertText(content);
114
163
{
115
164
    Q_UNUSED(archive);
116
165
 
117
 
    QTextCursor cursor = firstValidCursorPosition();
 
166
    bool needsEval=false;
 
167
    //make sure that the latex code is shown instead of the rendered formulas
 
168
    QTextCursor cursor = m_worksheet->document()->find(QString(QChar::ObjectReplacementCharacter), m_frame->firstCursorPosition());
 
169
    while(!cursor.isNull()&&cursor.position()<=m_frame->lastPosition())
 
170
    {
 
171
        QTextCharFormat format=cursor.charFormat();
 
172
        if (format.objectType() == FormulaTextObject::FormulaTextFormat)
 
173
        {
 
174
            showLatexCode(cursor);
 
175
            needsEval=true;
 
176
        }
 
177
 
 
178
        cursor = m_worksheet->document()->find(QString(QChar::ObjectReplacementCharacter), cursor);
 
179
    }
 
180
 
 
181
 
 
182
    cursor = firstValidCursorPosition();
118
183
    cursor.setPosition(lastValidPosition(), QTextCursor::KeepAnchor);
 
184
 
119
185
    const QString& html = cursor.selection().toHtml();
120
186
    kDebug() << html;
121
187
    QDomElement el = doc.createElement("Text");
122
188
    QDomDocument myDoc = QDomDocument();
123
189
    myDoc.setContent(html);
124
190
    el.appendChild(myDoc.documentElement().firstChildElement("body"));
 
191
 
 
192
    if(needsEval)
 
193
        evaluate(false);
125
194
    return el;
126
195
}
127
196
 
147
216
bool TextEntry::evaluate(bool current)
148
217
{
149
218
    Q_UNUSED(current);
 
219
 
 
220
    QTextDocument *doc = m_frame->document();
 
221
    //blahtex::Interface *translator = m_worksheet->translator();
 
222
 
 
223
    QTextCursor cursor = findLatexCode(doc);
 
224
    while (!cursor.isNull())
 
225
    {
 
226
        QString latexCode = cursor.selectedText();
 
227
        kDebug()<<"found latex: "<<latexCode;
 
228
 
 
229
        latexCode.remove(0, 2);
 
230
        latexCode.remove(latexCode.length() - 2, 2);
 
231
 
 
232
 
 
233
        Cantor::LatexRenderer* renderer=new Cantor::LatexRenderer(this);
 
234
        renderer->setLatexCode(latexCode);
 
235
        renderer->setEquationOnly(true);
 
236
        renderer->setEquationType(Cantor::LatexRenderer::InlineEquation);
 
237
        renderer->setMethod(Cantor::LatexRenderer::LatexMethod);
 
238
 
 
239
        renderer->renderBlocking();
 
240
 
 
241
#if 0
 
242
        QTextCharFormat mmlCharFormat;
 
243
        mmlCharFormat.setObjectType(MmlTextObject::MmlTextFormat);
 
244
        mmlCharFormat.setProperty(MmlTextObject::LatexCode, latexCode);
 
245
 
 
246
        if (!translator->ProcessInput(latexCode.toStdWString()))
 
247
            break;
 
248
        QString mmlCode = QString::fromStdWString(translator->GetMathml());
 
249
        kDebug() << mmlCode;
 
250
        if (mmlCode.isEmpty())
 
251
            break;
 
252
 
 
253
        QtMmlDocument renderer;
 
254
        renderer.setBaseFontPointSize(cursor.charFormat().fontPointSize());
 
255
        renderer.setFontName(QtMmlWidget::NormalFont, "STIXGeneral");
 
256
        renderer.setContent(mmlCode);
 
257
 
 
258
        QImage mmlBufferImage(renderer.size(), QImage::Format_ARGB32);
 
259
        mmlBufferImage.fill(QColor(0, 0, 0, 0).value());
 
260
        QPainter painter(&mmlBufferImage);
 
261
        renderer.paint(&painter, mmlBufferImage.rect().topLeft ());
 
262
 
 
263
        mmlCharFormat.setProperty(MmlTextObject::MmlData, mmlBufferImage);
 
264
 
 
265
        cursor.removeSelectedText();
 
266
        cursor.insertText(QString(QChar::ObjectReplacementCharacter), mmlCharFormat);
 
267
 
 
268
#endif
 
269
 
 
270
        bool success=m_worksheet->resultProxy()->renderEpsToResource(renderer->imagePath());
 
271
        kDebug()<<"rendering successfull? "<<success;
 
272
 
 
273
        QString path=renderer->imagePath();
 
274
        KUrl internal=KUrl(path);
 
275
        internal.setProtocol("internal");
 
276
        kDebug()<<"int: "<<internal;
 
277
 
 
278
        QTextCharFormat formulaFormat;
 
279
        formulaFormat.setObjectType(FormulaTextObject::FormulaTextFormat);
 
280
        formulaFormat.setProperty( FormulaTextObject::Data,renderer->imagePath());
 
281
        formulaFormat.setProperty( FormulaTextObject::ResourceUrl, internal);
 
282
        formulaFormat.setProperty( FormulaTextObject::LatexCode, latexCode);
 
283
        formulaFormat.setProperty( FormulaTextObject::FormulaType, renderer->method());
 
284
 
 
285
        cursor.insertText(QString(QChar::ObjectReplacementCharacter), formulaFormat);
 
286
        delete renderer;
 
287
 
 
288
        cursor = findLatexCode(doc);
 
289
 
 
290
    }
 
291
 
150
292
    return true;
151
293
}
152
294
 
153
295
void TextEntry::update()
154
296
{
155
 
 
 
297
    QTextCursor cursor = m_worksheet->document()->find(QString(QChar::ObjectReplacementCharacter), m_frame->firstCursorPosition());
 
298
    while(!cursor.isNull()&&cursor.position()<=m_frame->lastPosition())
 
299
    {
 
300
        QTextCharFormat format=cursor.charFormat();
 
301
        if (format.objectType() == FormulaTextObject::FormulaTextFormat)
 
302
        {
 
303
            kDebug()<<"found a formula... rendering the eps...";
 
304
            QUrl url=qVariantValue<QUrl>(format.property(FormulaTextObject::Data));
 
305
            bool success=m_worksheet->resultProxy()->renderEpsToResource(url);
 
306
            kDebug()<<"rendering successfull? "<<success;
 
307
 
 
308
            //HACK: reinsert this image, to make sure the layout is updated to the new size
 
309
            cursor.deletePreviousChar();
 
310
            cursor.insertText(QString(QChar::ObjectReplacementCharacter), format);
 
311
        }
 
312
 
 
313
        cursor = m_worksheet->document()->find(QString(QChar::ObjectReplacementCharacter), cursor);
 
314
    }
 
315
}
 
316
 
 
317
QTextCursor TextEntry::findLatexCode(QTextDocument *doc) const
 
318
{
 
319
    QTextCursor startCursor = doc->find("$$");
 
320
    if (startCursor.isNull())
 
321
        return startCursor;
 
322
    const QTextCursor endCursor = doc->find("$$", startCursor);
 
323
    if (endCursor.isNull())
 
324
        return endCursor;
 
325
    startCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, 2);
 
326
    startCursor.setPosition(endCursor.position(), QTextCursor::KeepAnchor);
 
327
    return startCursor;
 
328
}
 
329
 
 
330
void TextEntry::showLatexCode(QTextCursor cursor)
 
331
{
 
332
    QString latexCode = qVariantValue<QString>(cursor.charFormat().property(FormulaTextObject::LatexCode));
 
333
    cursor.deletePreviousChar();
 
334
    cursor.insertText("$$"+latexCode+"$$");
156
335
}