21#include <tqapplication.h>
28#include <tqtextstream.h>
35#include "bracketelement.h"
36#include "contextstyle.h"
37#include "formulacursor.h"
38#include "formulaelement.h"
39#include "fractionelement.h"
40#include "indexelement.h"
41#include "kformulacommand.h"
42#include "kformulacompatibility.h"
43#include "kformulacontainer.h"
44#include "kformuladocument.h"
45#include "kformulamathmlread.h"
46#include "kformulamimesource.h"
47#include "matrixelement.h"
48#include "rootelement.h"
49#include "sequenceelement.h"
50#include "symbolelement.h"
51#include "symboltable.h"
52#include "spaceelement.h"
53#include "textelement.h"
57KFORMULA_NAMESPACE_BEGIN
61struct Container::Container_Impl {
63 Container_Impl( Document* doc )
64 : dirty( true ), cursorMoved( false ), document( doc )
88 FormulaElement* rootElement;
93 FormulaCursor* activeCursor;
98 FormulaCursor* internCursor;
110Container::Container(
Document* doc,
int pos,
bool registerMe )
112 impl =
new Container_Impl( doc );
113 impl->rootElement = 0;
119Container::~Container()
129 assert( impl->rootElement == 0 );
131 impl->activeCursor = impl->internCursor =
createCursor();
174 impl->cursorMoved =
true;
197void Container::tell(
const TQString& msg )
213void Container::unregisterFormula()
232 return impl->activeCursor;
237 return impl->activeCursor;
249 impl->activeCursor = cursor;
252 *(impl->internCursor) = *(impl->activeCursor);
253 impl->activeCursor = impl->internCursor;
258bool Container::hasValidCursor()
const
260 return (impl->activeCursor != 0) && !impl->activeCursor->
isReadOnly();
273 ContextStyle& context = impl->document->getContextStyle();
277 context.layoutUnitToPixelY(
rootElement()->getHeight() ) );
279 context.layoutUnitPtToPt( context.pixelYToPt(
rootElement()->getHeight() ) ) );
289const SymbolTable& Container::getSymbolTable()
const
295void Container::draw( TQPainter& painter,
const TQRect& r,
const TQColorGroup& cg,
bool edit )
297 painter.fillRect( r, cg.base() );
298 draw( painter, r, edit );
306 rootElement()->
draw( painter, context.pixelToLayoutUnit( r ), context );
310void Container::checkCursor()
312 if ( impl->cursorMoved ) {
313 impl->cursorMoved =
false;
321 if ( impl->activeCursor == 0 ) {
329void Container::performRequest( Request* request )
331 if ( !hasValidCursor() )
333 execute(
activeCursor()->getElement()->buildCommand(
this, request ) );
340 if (!hasValidCursor())
342 TQClipboard* clipboard = TQApplication::clipboard();
343 const TQMimeSource* source = clipboard->data();
344 if (source->provides( MimeSource::selectionMimeType() )) {
345 TQByteArray data = source->encodedData( MimeSource::selectionMimeType() );
346 TQDomDocument formula;
347 formula.setContent(data);
348 paste( formula, i18n(
"Paste") );
355 TQPtrList<BasicElement> list;
356 list.setAutoDelete(
true );
358 uint count = list.count();
362 for (uint i = 0; i < count; i++) {
376 cursor->
copy( formula );
377 TQClipboard* clipboard = TQApplication::clipboard();
378 clipboard->setData(
new MimeSource(
document(), formula));
384 if (!hasValidCursor())
389 DirectedRemove r( req_remove, beforeCursor );
390 performRequest( &r );
395void Container::emitErrorMsg(
const TQString& msg )
400void Container::execute(
KCommand* command)
402 if ( command != 0 ) {
403 getHistory()->addCommand(command);
411 return TQRect( context.layoutUnitToPixelX(
rootElement()->getX() ),
412 context.layoutUnitToPixelY(
rootElement()->getY() ),
413 context.layoutUnitToPixelX(
rootElement()->getWidth() ),
414 context.layoutUnitToPixelY(
rootElement()->getHeight() ) );
419 if ( impl->activeCursor != 0 ) {
421 const LuPixelRect& cursorRect = impl->activeCursor->getCursorSize();
422 return TQRect( context.layoutUnitToPixelX(
rootElement()->getX() ),
423 context.layoutUnitToPixelY(
rootElement()->getY() ),
424 context.layoutUnitToPixelX(
rootElement()->getWidth() ),
425 context.layoutUnitToPixelY(
rootElement()->getHeight() ) ) |
426 TQRect( context.layoutUnitToPixelX( cursorRect.x() ),
427 context.layoutUnitToPixelY( cursorRect.y() ),
428 context.layoutUnitToPixelX( cursorRect.width() ),
429 context.layoutUnitToPixelY( cursorRect.height() ) );
434double Container::width()
const
437 return context.layoutUnitPtToPt( context.pixelXToPt(
rootElement()->getWidth() ) );
440double Container::height()
const
443 return context.layoutUnitPtToPt( context.pixelYToPt(
rootElement()->getHeight() ) );
450 return context.layoutUnitPtToPt( context.pixelYToPt(
rootElement()->getBaseline() ) );
456 rootElement()->setX( context.pixelToLayoutUnitX( x ) );
457 rootElement()->setY( context.pixelToLayoutUnitY( y ) );
460int Container::fontSize()
const
467 return tqRound( context.baseSize() );
474 execute(
new KFCChangeBaseSize( i18n(
"Base Size Change" ),
this,
rootElement(), pointSize ) );
478void Container::setFontSizeDirect(
int pointSize )
487 if ( ( currentElement = currentElement->getParent() ) != 0 )
490 document()->wrapper()->enableMatrixActions(
false );
495 TQDomDocument ownerDoc = root.ownerDocument();
496 root.appendChild(
rootElement()->getElementDom(ownerDoc));
508 delete impl->rootElement;
509 impl->rootElement = root;
517 kdWarning( DEBUGID ) <<
"Error constructing element tree." << endl;
521 kdWarning( DEBUGID ) <<
"Empty element." << endl;
530 if ( !oasisFormat ) {
539 return loadMathML( doc.documentElement(), oasisFormat );
562 kdDebug( DEBUGID ) <<
"loadMathML" << endl;
566 delete impl->rootElement;
567 impl->rootElement = root;
575 kdWarning( DEBUGID ) <<
"Error constructing element tree." << endl;
579 kdWarning( DEBUGID ) <<
"Empty element." << endl;
589 if (painter.begin(&printer)) {
594 document()->getContextStyle(
false ) );
601 TQRect rect(impl->rootElement->getX(), impl->rootElement->getY(),
602 impl->rootElement->getWidth(), impl->rootElement->getHeight());
604 int realWidth = context.layoutUnitToPixelX( impl->rootElement->getWidth() );
605 int realHeight = context.layoutUnitToPixelY( impl->rootElement->getHeight() );
607 double f = TQMAX(
static_cast<double>( width )/
static_cast<double>( realWidth ),
608 static_cast<double>( height )/
static_cast<double>( realHeight ) );
610 int oldZoom = context.zoom();
611 context.setZoomAndResolution( tqRound( oldZoom*f ), KoGlobal::dpiX(), KoGlobal::dpiY() );
613 kdDebug( DEBUGID ) <<
"Container::drawImage "
614 <<
"(" << width <<
" " << height <<
")"
615 <<
"(" << context.layoutUnitToPixelX( impl->rootElement->getWidth() )
616 <<
" " << context.layoutUnitToPixelY( impl->rootElement->getHeight() ) <<
")"
619 TQPixmap pm( context.layoutUnitToPixelX( impl->rootElement->getWidth() ),
620 context.layoutUnitToPixelY( impl->rootElement->getHeight() ) );
622 TQPainter paint(&pm);
623 impl->rootElement->draw(paint, rect, context);
625 context.setZoomAndResolution( oldZoom, KoGlobal::dpiX(), KoGlobal::dpiY() );
627 return pm.convertToImage();
635TQString Container::formulaString()
640KFORMULA_NAMESPACE_END
642using namespace KFormula;
643#include "kformulacontainer.moc"
Basis of every formula element.
int buildFromMathMLDom(TQDomElement element)
Set this element attribute, build children and call their builFromMathMLDom.
bool buildFromDom(TQDomElement element)
Set this element attribute, build children and call their buildFromDom.
bool loadMathML(const TQDomDocument &doc, bool oasisFormat=false)
Load function.
FormulaElement * rootElement() const
void setFontSize(int pointSize, bool forPrint=false)
Sets the base font size of this formula.
void registerFormula(int pos=-1)
Register and unregister this formula with its document.
void leaveFormula(Container *formula, FormulaCursor *cursor, int cmd)
The cursor wants to leave this formula.
void copy()
Copy the current selection to the clipboard.
FormulaCursor * createCursor()
Returns a new cursor.
void paste()
Insert data from the clipboard.
void initialize()
Needs to be called before anything else can be done with a newly created formula!
void cursorMoved(FormulaCursor *cursor)
The cursor has been moved but the formula hasn't been changed.
void errorMsg(const TQString &)
A message that describes an error.
void baseSizeChanged(int size, bool owned)
The base size changed.
void cursorHasMoved(FormulaCursor *)
Gets called when a request has the side effect of moving the cursor.
virtual void recalc()
Recalc the formula.
void testDirty()
Recalcs the formula and emits the .
void formulaChanged(int width, int height)
The formula has changed and needs to be redrawn.
virtual Document * document() const
TQImage drawImage(int width, int height)
bool load(const TQDomElement &fe)
Loads a formula from the document.
virtual FormulaElement * createMainSequence()
Factory method.
void setActiveCursor(FormulaCursor *cursor)
Sets the cursor that is to be used for any editing.
void save(TQDomElement &root)
Saves the data into the document.
void moveOutLeft(FormulaCursor *)
Inside the formula occurred an event that must be handled outside.
void cut()
Copy and remove.
void print(KPrinter &printer)
Prints the formula.
void updateMatrixActions()
If the cursor is in a matrix, the matrix actions will be enabled, otherwise disabled.
void statusMsg(const TQString &msg)
A message that might be a useful hint.
void changed()
Gets called when ever something changes and we need to recalc.
void input(TQKeyEvent *event)
General input.
void formulaLoaded(FormulaElement *)
Tells the cursors that we have just loaded a new formula.
void elementWillVanish(BasicElement *element)
The element is going to leave the formula with and all its children.
FormulaCursor * activeCursor()
void draw(TQPainter &painter, const TQRect &r, const TQColorGroup &cg, bool edit=false)
Draws the whole thing.
void moveTo(int x, int y)
Moves the formula to a new location.
void elementRemoval(BasicElement *child)
Gets called just before the child is removed from the element tree.
TQRect boundingRect() const
void saveMathML(TQTextStream &stream, bool oasisFormat=false)
Save formula as MathML.
Contains all the style information for the formela.
A document that can contain a lot of formulas (container).
KoCommandHistory * getHistory() const
void activate(Container *formula)
Sets a new formula.
virtual void unregisterFormula(Container *)
Removes a formula from this document.
ContextStyle & getContextStyle(bool edit=false)
TQDomDocument createMathMLDomDocument()
Create a MathML Dom Document, deprecates KFO Dom Document for internal layout TODO: Shouldn't this go...
const SymbolTable & getSymbolTable() const
virtual void registerFormula(Container *, int pos=-1)
Registers a new formula to be part of this document.
void addElement(BasicElement *element)
Collects all elements that are to be added.
Removes the current selection and adds any new elements afterwards.
The command history stores a (user) configurable amount of Commands.
uint countChildren() const