22#include "KWFormulaFrameSet.h"
24#include "KWDocument.h"
26#include "KWViewMode.h"
31#include <kformulacontainer.h>
32#include <kformuladocument.h>
33#include <kformulaview.h>
34#include <KoOasisContext.h>
36#include <KoXmlWriter.h>
39#include <tdemessagebox.h>
41#include <tdetempfile.h>
42#include <dcopobject.h>
43#include "KWordFormulaFrameSetIface.h"
44#include "KWordFormulaFrameSetEditIface.h"
56KWFormulaFrameSet::KWFormulaFrameSet( KWDocument *doc,
const TQString & name )
57 :
KWFrameSet( doc ), m_changed( false ), m_edit( 0 )
60 m_name = doc->generateFramesetName( i18n(
"Formula %1" ) );
67KWFormulaFrameSet::KWFormulaFrameSet( KWDocument* doc,
const TQDomElement& frameTag,
68 const TQDomElement& mathTag, KoOasisContext& context )
69 :
KWFrameSet( doc ), m_changed( false ), m_edit( 0 )
71 m_name = frameTag.attributeNS( KoXmlNS::draw,
"name", TQString() );
72 if ( doc->frameSetByName( m_name ) )
73 m_name = doc->generateFramesetName( m_name +
" %1" );
77 context.styleStack().save();
78 context.fillStyleStack( frameTag, KoXmlNS::draw,
"style-name",
"graphic" );
79 loadOasisFrame( frameTag, context );
80 context.styleStack().restore();
82 formula->loadMathML( mathTag );
85void KWFormulaFrameSet::init()
89 formula = m_doc->formulaDocument()->createFormula( -1,
false );
93 formula->moveTo( 0, 0 );
95 connect( formula, TQ_SIGNAL( formulaChanged(
double,
double ) ),
96 this, TQ_SLOT( slotFormulaChanged(
double,
double ) ) );
97 connect( formula, TQ_SIGNAL( errorMsg(
const TQString& ) ),
98 this, TQ_SLOT( slotErrorMessage(
const TQString& ) ) );
111 TQRect rect = formula->boundingRect();
112 slotFormulaChanged(rect.width(), rect.height());
115KWordFrameSetIface* KWFormulaFrameSet::dcopObject()
118 m_dcop =
new KWordFormulaFrameSetIface(
this );
123KWFormulaFrameSet::~KWFormulaFrameSet()
125 kdDebug() << k_funcinfo << endl;
129void KWFormulaFrameSet::addFrame(
KWFrame *frame,
bool recalc )
131 kdDebug() << k_funcinfo << endl;
133 frame->setWidth( formula->width() );
134 frame->setHeight( formula->height() );
136 KWFrameSet::addFrame(
frame, recalc );
138 formula->registerFormula();
142void KWFormulaFrameSet::deleteFrame(
unsigned int num,
bool remove,
bool recalc )
144 kdDebug() << k_funcinfo << endl;
147 formula->unregisterFormula();
153 return new KWFormulaFrameSetEdit(
this, canvas);
156void KWFormulaFrameSet::drawFrameContents(
KWFrame* ,
157 TQPainter* painter,
const TQRect& crect,
158 const TQColorGroup& cg,
bool onlyChanged,
162 if ( m_changed || !onlyChanged )
167 bool printing = painter->device()->devType() == TQInternal::Printer;
168 bool clipping =
true;
173 clipping = painter->hasClipping();
177 painter->setClipping(
false );
180 pix = doubleBufferPixmap( crect.size() );
181 p =
new TQPainter( pix );
182 p->translate( -crect.x(), -crect.y() );
187 if ( m_edit->getFormulaView() ) {
188 m_edit->getFormulaView()->draw( *p, crect, cg );
191 formula->draw( *p, crect, cg );
195 formula->draw( *p, crect, cg );
201 painter->drawPixmap( crect.topLeft(), *pix );
204 painter->setClipping( clipping );
210void KWFormulaFrameSet::slotFormulaChanged(
double width,
double height )
212 if ( m_frames.isEmpty() )
215 double oldWidth = m_frames.first()->width();
216 double oldHeight = m_frames.first()->height();
218 m_frames.first()->setWidth( width );
219 m_frames.first()->setHeight( height );
222 kWordDocument()->layout();
223 if ( ( oldWidth != width ) || ( oldHeight != height ) ) {
224 kWordDocument()->repaintAllViews(
false );
225 kWordDocument()->updateRulerFrameStartEnd();
233 formula->startEvaluation();
237void KWFormulaFrameSet::slotErrorMessage(
const TQString& msg )
239 KMessageBox::error( 0, msg );
242MouseMeaning KWFormulaFrameSet::getMouseMeaningInsideFrame(
const KoPoint& )
244 return MEANING_MOUSE_INSIDE_TEXT;
247TQDomElement KWFormulaFrameSet::save(TQDomElement& parentElem,
bool saveFrames)
249 if ( m_frames.isEmpty() )
250 return TQDomElement();
251 TQDomElement framesetElem = parentElem.ownerDocument().createElement(
"FRAMESET");
252 parentElem.appendChild(framesetElem);
256 TQDomElement formulaElem = parentElem.ownerDocument().createElement(
"FORMULA");
257 framesetElem.appendChild(formulaElem);
258 formula->save(formulaElem);
262void KWFormulaFrameSet::saveOasis(KoXmlWriter& writer, KoSavingContext& context,
bool)
const
264 KWFrame *
frame = m_frames.getFirst();
265 frame->startOasisFrame( writer, context.mainStyles(),
name() );
267 KTempFile contentTmpFile;
268 contentTmpFile.setAutoDelete(
true );
269 TQFile* tmpFile = contentTmpFile.file();
271 TQTextStream stream(tmpFile);
272 stream.setEncoding( TQTextStream::UnicodeUTF8 );
273 formula->saveMathML( stream,
true );
276 writer.startElement(
"draw:object" );
277 writer.startElement(
"math:math" );
278 writer.addCompleteElement( tmpFile );
284void KWFormulaFrameSet::load(TQDomElement& attributes,
bool loadFrames)
287 TQDomElement formulaElem = attributes.namedItem(
"FORMULA").toElement();
288 paste( formulaElem );
291void KWFormulaFrameSet::paste( TQDomNode& formulaElem )
293 if (!formulaElem.isNull()) {
295 formula = m_doc->formulaDocument()->createFormula( -1,
false );
296 connect(formula, TQ_SIGNAL(formulaChanged(
double,
double)),
297 this, TQ_SLOT(slotFormulaChanged(
double,
double)));
298 connect( formula, TQ_SIGNAL( errorMsg(
const TQString& ) ),
299 this, TQ_SLOT( slotErrorMessage(
const TQString& ) ) );
301 m_doc->formulaDocument()->setCreationStrategy(
"Oasis" );
302 if ( !formula->loadMathML( formulaElem.firstChild().toElement() ) ) {
303 kdError(32001) <<
"Error loading formula" << endl;
307 kdError(32001) <<
"Missing math tag in FRAMESET" << endl;
311void KWFormulaFrameSet::moveFloatingFrame(
int frameNum,
const KoPoint &position )
313 kdDebug() << k_funcinfo << endl;
315 if ( !m_frames.isEmpty() ) {
316 formula->setDocumentPosition( position.x(), position.y()+formula->baseline() );
320int KWFormulaFrameSet::floatingFrameBaseline(
int )
322 if ( !m_frames.isEmpty() )
324 return m_doc->ptToLayoutUnitPixY( formula->baseline() );
329void KWFormulaFrameSet::setAnchorFormat( KoTextFormat* format,
int )
331 if ( !m_frames.isEmpty() ) {
332 formula->setFontSizeDirect( format->pointSize() );
337TQPixmap* KWFormulaFrameSet::m_bufPixmap = 0;
342TQPixmap* KWFormulaFrameSet::doubleBufferPixmap(
const TQSize& s )
344 if ( !m_bufPixmap ) {
345 int w = TQABS( s.width() );
346 int h = TQABS( s.height() );
347 m_bufPixmap =
new TQPixmap( w, h );
349 if ( m_bufPixmap->width() < s.width() ||
350 m_bufPixmap->height() < s.height() ) {
351 m_bufPixmap->resize( TQMAX( s.width(), m_bufPixmap->width() ),
352 TQMAX( s.height(), m_bufPixmap->height() ) );
360KWFormulaFrameSetEdit::KWFormulaFrameSetEdit(KWFormulaFrameSet* fs,
KWCanvas* canvas)
363 formulaView =
new KFormula::View( fs->getFormula() );
365 connect( formulaView, TQ_SIGNAL( cursorChanged(
bool,
bool ) ),
366 this, TQ_SLOT( cursorChanged(
bool,
bool ) ) );
367 connect( fs->getFormula(), TQ_SIGNAL( leaveFormula( Container*, FormulaCursor*,
int ) ),
368 this, TQ_SLOT( slotLeaveFormula( Container*, FormulaCursor*,
int ) ) );
372 m_canvas->gui()->getView()->showFormulaToolbar(
true);
377DCOPObject* KWFormulaFrameSetEdit::dcopObject()
380 dcop =
new KWordFormulaFrameSetEditIface(
this );
384KWFormulaFrameSetEdit::~KWFormulaFrameSetEdit()
386 formulaFrameSet()->m_edit = 0;
389 m_canvas->gui()->getView()->showFormulaToolbar(
false);
392 formulaFrameSet()->getFormula()->startEvaluation();
393 formulaFrameSet()->setChanged();
394 m_canvas->repaintChanged( formulaFrameSet(),
true );
398const KFormula::View* KWFormulaFrameSetEdit::getFormulaView()
const {
return formulaView; }
399KFormula::View* KWFormulaFrameSetEdit::getFormulaView() {
return formulaView; }
401void KWFormulaFrameSetEdit::keyPressEvent( TQKeyEvent* event )
404 formulaView->keyPressEvent( event );
407void KWFormulaFrameSetEdit::mousePressEvent( TQMouseEvent* event,
413 formulaView->mousePressEvent( event, pos-tl );
416void KWFormulaFrameSetEdit::mouseMoveEvent( TQMouseEvent* event,
421 formulaView->mouseMoveEvent( event, pos-tl );
424void KWFormulaFrameSetEdit::mouseReleaseEvent( TQMouseEvent* event,
429 formulaView->mouseReleaseEvent( event, pos-tl );
432void KWFormulaFrameSetEdit::focusInEvent()
435 if ( formulaView != 0 ) {
436 formulaView->focusInEvent(0);
440void KWFormulaFrameSetEdit::focusOutEvent()
444 if ( formulaView != 0 ) {
445 formulaView->focusOutEvent(0);
449void KWFormulaFrameSetEdit::copy()
451 formulaView->getDocument()->copy();
454void KWFormulaFrameSetEdit::cut()
456 formulaView->getDocument()->cut();
459void KWFormulaFrameSetEdit::paste()
461 formulaView->getDocument()->paste();
464void KWFormulaFrameSetEdit::pasteData( TQMimeSource* ,
int ,
bool )
469void KWFormulaFrameSetEdit::selectAll()
471 formulaView->slotSelectAll();
474void KWFormulaFrameSetEdit::moveHome()
476 formulaView->moveHome( KFormula::WordMovement );
478void KWFormulaFrameSetEdit::moveEnd()
480 formulaView->moveEnd( KFormula::WordMovement );
483void KWFormulaFrameSetEdit::removeFormula()
485 if ( formulaFrameSet()->isFloating() ) {
486 KWCanvas* canvas = m_canvas;
491 TQKeyEvent keyEvent( TQEvent::KeyPress, Key_Backspace, 0, 0 );
492 canvas->currentFrameSetEdit()->keyPressEvent( &keyEvent );
496void KWFormulaFrameSetEdit::cursorChanged(
bool visible,
bool )
502 TQPoint nPoint = frameSet()->kWordDocument()->zoomPoint(
m_currentFrame->topLeft() );
503 nPoint += formulaView->getCursorPoint();
505 TQPoint p = m_canvas->viewMode()->normalToView( nPoint );
506 m_canvas->ensureVisible( p.x(), p.y() );
509 formulaFrameSet()->setChanged();
510 m_canvas->repaintChanged( formulaFrameSet(),
true );
513void KWFormulaFrameSetEdit::slotLeaveFormula( KFormula::Container*,
514 KFormula::FormulaCursor* cursor,
517 kdDebug() << k_funcinfo << endl;
519 if ( cursor == formulaView->getCursor() ) {
521 case KFormula::Container::EXIT_LEFT:
524 case KFormula::Container::EXIT_RIGHT:
527 case KFormula::Container::EXIT_ABOVE:
530 case KFormula::Container::EXIT_BELOW:
533 case KFormula::Container::REMOVE_FORMULA:
540#include "KWFormulaFrameSet.moc"
Class: KWCanvas This class is responsible for the rendering of the frames to the screen as well as th...
This object is created to edit a particular frameset in a particular view The view's canvas creates i...
bool exitLeft()
Called if the cursor tries to leave the frameset at its beginning.
bool exitRight()
Called if the cursor tries to leave the frameset at its end.
KWFrame * m_currentFrame
The Frameset-Edit implementation is responsible for updating that one (to the frame where the current...
Class: KWFrameSet Base type, a frameset holds content as well as frames to show that content.
virtual void moveFloatingFrame(int frameNum, const KoPoint &position)
Move the frame frameNum to the given position - this is called when the frame is anchored and the anc...
virtual void deleteFrame(unsigned int num, bool remove=true, bool recalc=true)
Delete a frame from the set of frames this frameSet has.
virtual void load(TQDomElement &framesetElem, bool loadFrames=true)
load from XML - when loading
virtual void updateFrames(int flags=0xff)
Called when our frames change, or when another frameset's frames change.
void saveCommon(TQDomElement &parentElem, bool saveFrames)
save the common attributes for the frameset
KWFrame * frame(unsigned int _num) const
get a frame by number
TQString name() const
get/set frameset name. For tables in particular, this must be unique
This class represents a single frame.
Abstract base class for KWCanvas's view modes.