lib

KoTextCustomItem.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KoRichText.h"
21#include "KoTextFormat.h"
22#include "KoTextParag.h"
23#include "KoTextZoomHandler.h"
24#include "KoTextDocument.h"
25#include <kdebug.h>
26#include <kcommand.h>
27
28
29//void KoTextCustomItem::setPainter( TQPainter*, bool adjust ){ if ( adjust ) width = 0; }
30//void KoTextCustomItem::setPainter( TQPainter*, bool ){ resize(); } // changed for kotext
31
32bool KoTextCustomItem::enter( KoTextCursor *, KoTextDocument *&doc, KoTextParag *&parag, int &idx, int &ox, int &oy, bool atEnd )
33{
34 doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; Q_UNUSED( atEnd ) return true;
35
36}
37bool KoTextCustomItem::enterAt( KoTextCursor *, KoTextDocument *&doc, KoTextParag *&parag, int &idx, int &ox, int &oy, const TQPoint & )
38{
39 doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return true;
40}
41bool KoTextCustomItem::next( KoTextCursor *, KoTextDocument *&doc, KoTextParag *&parag, int &idx, int &ox, int &oy )
42{
43 doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return true;
44}
45bool KoTextCustomItem::prev( KoTextCursor *, KoTextDocument *&doc, KoTextParag *&parag, int &idx, int &ox, int &oy )
46{
47 doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return true;
48}
49bool KoTextCustomItem::down( KoTextCursor *, KoTextDocument *&doc, KoTextParag *&parag, int &idx, int &ox, int &oy )
50{
51 doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return true;
52}
53bool KoTextCustomItem::up( KoTextCursor *, KoTextDocument *&doc, KoTextParag *&parag, int &idx, int &ox, int &oy )
54{
55 doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return true;
56}
57
59{
60 Q_ASSERT( paragraph() );
61 KoTextParag * parag = paragraph();
62 return parag->findCustomItem( this );
63}
64
66{
67 KoTextParag * parag = paragraph();
68 //kdDebug(32500) << "KoTextCustomItem::format index=" << index() << " format=" << parag->at( index() )->format() << endl;
69 return parag->at( index() )->format();
70}
71
72
73void KoTextCustomItem::draw(TQPainter* p, int _x, int _y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected )
74{
75 KoTextZoomHandler *zh=textDocument()->paintingZoomHandler();
76 //kdDebug(32500)<<" x :"<<_x<<" y :"<<_y<<" cx :"<<cx<<" cy :"<<cy<<" ch :"<<ch<<" cw :"<<cw<<endl;
77
78 // Calculate index only once
79 // Hmm, should pass it to drawCustomItem...
80 int charIndex = index();
81 KoTextStringChar* stringChar = paragraph()->at( charIndex );
82
83 // Convert x, y, cx, cy, cw and ch from Layout Units to pixels.
84 int x = zh->layoutUnitToPixelX(_x) /*+ stringChar->pixelxadj*/;
85 int y = zh->layoutUnitToPixelY(_y);
86 cx = zh->layoutUnitToPixelX(cx);
87 cy = zh->layoutUnitToPixelY(cy);
88 cw = zh->layoutUnitToPixelX(_x,cw);
89 ch = zh->layoutUnitToPixelY(_y,ch);
90 int wpix = zh->layoutUnitToPixelX(_x,width);
91 int hpix = zh->layoutUnitToPixelX(_y,height);
92 //kdDebug(32500)<<"After x :"<<x<<" y :"<<y<<" cx :"<<cx<<" cy :"<<cy<<" ch :"<<ch<<" cw :"<<cw<<endl;
93 int ascentpix = zh->layoutUnitToPixelY( _y, ascent() );
94
95 KoTextFormat * fmt = stringChar->format();
96
97 //bool forPrint = ( p->device()->devType() == TQInternal::Printer );
98 p->setFont( fmt->screenFont( zh ) );
99
100 int offset=0;
101 if ( fmt->vAlign() == KoTextFormat::AlignSuperScript )
102 offset = -( hpix - p->fontMetrics().height() );
103
104 if ( fmt->shadowDistanceX() != 0 || fmt->shadowDistanceY() != 0 ) {
105 int sx = fmt->shadowX( zh );
106 int sy = fmt->shadowY( zh );
107 if ( sx != 0 || sy != 0 )
108 {
109 p->save();
110 p->translate( sx, sy );
111 drawCustomItem(p, x, y, wpix, hpix, ascentpix, cx, cy, cw, ch, cg, selected, offset, true);
112 p->restore();
113 }
114 }
115 drawCustomItem(p, x, y, wpix, hpix, ascentpix, cx, cy, cw, ch, cg, selected, offset, false);
116}
117
119
120void CustomItemsMap::insertItems( const KoTextCursor & startCursor, int size )
121{
122 if ( isEmpty() )
123 return;
124
125 KoTextCursor cursor( startCursor );
126 for ( int i = 0; i < size; ++i )
127 {
128 CustomItemsMap::Iterator it = find( i );
129 if ( it != end() )
130 {
131 kdDebug(32500) << "CustomItemsMap::insertItems setting custom item " << it.data() << endl;
132 cursor.parag()->setCustomItem( cursor.index(), it.data(), 0 );
133 it.data()->setDeleted( false );
134 }
135 cursor.gotoRight();
136 }
137}
138
140{
141 Iterator it = begin();
142 for ( ; it != end(); ++it )
143 {
144 KoTextCustomItem * item = it.data();
145 KCommand * itemCmd = item->deleteCommand();
146 if ( itemCmd && macroCmd )
147 {
148 macroCmd->addCommand( itemCmd );
149 itemCmd->execute(); // the item-specific delete stuff hasn't been done
150 }
151 item->setDeleted( true );
152 }
153}
void deleteAll(KMacroCommand *macroCmd)
Delete all the items from the map, adding their commands into macroCmd.
void insertItems(const KoTextCursor &startCursor, int size)
Insert all the items from the map, into the existing text.
virtual void execute()=0
void addCommand(KCommand *command)
KoTextCustomItem is the base class for custom items (i.e.
KoTextFormat * format() const
The formatting given to this 'special' character Slightly slow (does a linear search in the paragraph...
virtual void drawCustomItem(TQPainter *p, int x, int y, int wpix, int hpix, int ascentpix, int cx, int cy, int cw, int ch, const TQColorGroup &cg, bool selected, int offset, bool drawingShadow)=0
All coordinates are in pixels.
KoTextDocument * textDocument() const
The text document in which this customitem is.
int index() const
The index in paragraph(), where this anchor is Slightly slow (does a linear search in the paragraph).
virtual void setDeleted(bool b)
When the user deletes a custom item, it isn't destroyed but moved into the undo/redo history - setDel...
Each character (KoTextStringChar) points to a KoTextFormat that defines the formatting of that charac...
int shadowY(KoTextZoomHandler *zh) const
Return the amount of pixels for the vertical shadow distance at a given zoom level.
int shadowX(KoTextZoomHandler *zh) const
Return the amount of pixels for the horizontal shadow distance at a given zoom level.
TQFont screenFont(const KoTextZoomHandler *zh) const
Returns the font to be used at the zoom & resolution given by 'zh'.
This class extends KoZoomHandler to add support for WYSIWYG text layouting.
int layoutUnitToPixelX(int lupix) const
The "layout unit pixel" -> "[zoomed] view pixel" conversions.