[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: shift+enter in kword
From:       Krister Wicksell Eriksson <krister.wicksell () spray ! se>
Date:       2002-02-28 13:31:34
[Download RAW message or body]

Hi

On Wednesday 27 February 2002 21:08, David Faure wrote:
> On Wednesday 27 February 2002 19:29, Krister Wicksell Eriksson wrote:
> > Hi
> >
> > Wanted to learn some of the kword code so I have started trying to
> > implement line break (shift+enter) cause that is a thing I really miss in
> > kword.
> >
> > Have attached a patch that I be happy if someone could take a look at and
> > give some feedback.
>
> Wow! This is really great job. I'm amazed, you seem to have grasped the
> text formatter quickly, as complex as it is ;)))
>
> I like the formatting-chars drawing change very much. The UI part is fine
> too, no problem.
>
> I'm surprised by the patch to the formatter though. It's already supposed
> to have support for line breaks, by using the character '\n' in the string
> (and enabling something like setAllowNewLine() in paragraphs). This comes
> from the original Qt classes, and was never tested/used in KWord. Did you
> try it out and determined you'd prefer writing separate code for this, or
> did you miss that code ? If it works and it's the same functionality, I'd
> rather use it than add special support for 0x0b with its own code.

Have rewritten my patch with setNewLinesAllowed(TRUE) and'\n' insted. Don't 
know if this is any better. Had to change QTextParag::paint() so it now sends 
'\n' to drawParagString(), if not there is no way (at least that I know of) 
to know where to draw the line breaks in drawFormattingChars(). The thing is 
if there are only a line break on a line no char from that line i sent to 
drawFormattingChars().

>
> David, really glad to see more and more KWord contributions coming in ;)

Hope this mail will reach the list...got a return on the last mail I sent 
saying that it had to be approved by a moderator cause I was not on the 
list...but I am.

/Krister

-- 
krister.wicksell@spray.se
http://hem.spray.se/krister.wicksell/
["koffice-20020228.patch" (text/x-diff)]

Index: kword/kwtextparag.cc
===================================================================
RCS file: /home/kde/koffice/kword/kwtextparag.cc,v
retrieving revision 1.189
diff -u -3 -p -u -r1.189 kwtextparag.cc
--- kword/kwtextparag.cc	2002/02/20 13:58:15	1.189
+++ kword/kwtextparag.cc	2002/02/28 12:46:13
@@ -81,19 +81,19 @@ void KWTextParag::drawFormattingChars( Q
                     KoTextFormat* format = static_cast<KoTextFormat *>( at( length() \
                - 1 )->format() );
                     int w = format->screenFontMetrics(zh).width('x'); // see \
KWTextFrameSet::formatVertically  int size = QMIN( w, h_pix * 3 / 4 );
-                    int arrowsize = zh->zoomItY( 2 );
-                    // x,y is the bottom right corner of the reversed L
+                    // x,y is the bottom right corner of the ¶
                     //kdDebug() << "startX=" << startX << " bw=" << bw << " w=" << w \
<< endl;  int x = ( startX_pix + bw ) + w - 1;
-                    int y = lastY_pix + baseLine_pix - arrowsize;
+                    int y = lastY_pix + baseLine_pix;
                     //kdDebug() << "KWTextParag::drawFormattingChars drawing CR at " \
                << x << "," << y << endl;
-                    painter.drawLine( x, y - size, x, y );
-                    painter.drawLine( x, y, x - size, y );
-                    // Now the arrow
-                    painter.drawLine( x - size, y, x - size + arrowsize, y - \
                arrowsize );
-                    painter.drawLine( x - size, y, x - size + arrowsize, y + \
arrowsize ); +                    painter.drawLine( (int)(x - size * 0.2), y - size, \
(int)(x - size * 0.2), y ); +                    painter.drawLine( (int)(x - size * \
0.5), y - size, (int)(x - size * 0.5), y ); +                    painter.drawLine( x, \
y, (int)(x - size * 0.7), y ); +                    painter.drawLine( x, y - size, \
(int)(x - size * 0.5), y - size); +                    painter.drawArc( x - size, y - \
size, size, (int)(size / 2), -90*16, -180*16 );  }
             }
+
             // Now draw spaces and tabs
             int end = QMIN( start + len, length() - 1 ); // don't look at the \
trailing space  for ( int i = start ; i < end ; ++i )
@@ -122,13 +122,33 @@ void KWTextParag::drawFormattingChars( Q
 
                     int availWidth = ch.pixelwidth;
 
+                    KoTextFormat* format = static_cast<KoTextFormat *>( ch.format() \
                );
                     int x = zh->layoutUnitToPixelX( ch.x ) + ch.pixelxadj + \
                availWidth / 2;
-                    int size = QMIN( \
availWidth,zh->layoutUnitToPixelX(ch.format()->width('W')) ) / 2 ; // actually the \
half size +                    int size = QMIN( \
                availWidth,format->screenFontMetrics(zh).width('W')) / 2 ; // \
                actually the half size
                     int y = lastY_pix + baseLine_pix - zh->layoutUnitToPixelY( \
ch.ascent()/2 );  int arrowsize = zh->zoomItY( 2 );
                     painter.drawLine( x + size, y, x - size, y );
                     painter.drawLine( x + size, y, x + size - arrowsize, y - \
                arrowsize );
                     painter.drawLine( x + size, y, x + size - arrowsize, y + \
arrowsize ); +                }
+                else if ( ch.c == '\n' )
+                {
+                    // draw line break
+                    KoTextFormat* format = static_cast<KoTextFormat *>( ch.format() \
); +                    int w = format->screenFontMetrics(zh).width('X');
+                    int size = QMIN( w, h_pix * 3 / 4 );
+                    int arrowsize = zh->zoomItY( 2 );
+                    KoTextStringChar &ch = string()->at(i);
+                    // x,y is the bottom right corner of the reversed L
+                    //kdDebug() << "startX=" << startX << " bw=" << bw << " w=" << w \
<< endl; +                    int x = zh->layoutUnitToPixelX( ch.x ) + ch.pixelxadj + \
w - 1; +                    int y = lastY_pix + baseLine_pix - arrowsize;
+                    //kdDebug() << "KWTextParag::drawFormattingChars drawing Line \
Break at " << x << "," << y << endl; +                    painter.drawLine( x, y - \
size, x, y ); +                    painter.drawLine( x, y, (int)(x - size * 0.7), y \
); +                    // Now the arrow
+                    painter.drawLine( (int)(x - size * 0.7), y, (int)(x - size * 0.7 \
+ arrowsize), y - arrowsize ); +                    painter.drawLine( (int)(x - size \
* 0.7), y, (int)(x - size * 0.7 + arrowsize), y + arrowsize );  }
             }
             painter.restore();
Index: kword/kwview.cc
===================================================================
RCS file: /home/kde/koffice/kword/kwview.cc,v
retrieving revision 1.496
diff -u -3 -p -u -r1.496 kwview.cc
--- kword/kwview.cc	2002/02/27 16:42:39	1.496
+++ kword/kwview.cc	2002/02/28 12:46:13
@@ -815,11 +815,14 @@ void KWView::setupActions()
                         this, SLOT( slotNonbreakingSpace() ), actionCollection(), \
                "nonbreaking_space" );
     KAction* actSoftHyphen = new KAction( i18n( "Insert soft hyphen" ), \
                CTRL+Key_Minus,
                         this, SLOT( slotSoftHyphen() ), actionCollection(), \
"soft_hyphen" ); +    KAction* actLineBreak = new KAction( i18n( "Line break" ), \
SHIFT+Key_Return, +                        this, SLOT( slotLineBreak() ), \
actionCollection(), "line_break" );  // Necessary for the actions that are not \
plugged anywhere  KAccel * accel = new KAccel( this );
     actNbsp ->plugAccel( accel );
     accel = new KAccel( this ); // needed ?
     actSoftHyphen->plugAccel( accel );
+    actLineBreak->plugAccel( accel );
 }
 
 void KWView::refreshMenuExpression()
@@ -4190,6 +4193,13 @@ void KWView::showDocStructure()
     m_doc->reorganizeGUI();
 }
 
+void KWView::slotSoftHyphen()
+{
+    KWTextFrameSetEdit * edit = currentTextEdit();
+    if ( edit )
+        edit->insertSoftHyphen();
+}
+
 void KWView::slotNonbreakingSpace()
 {
     KWTextFrameSetEdit * edit = currentTextEdit();
@@ -4197,11 +4207,11 @@ void KWView::slotNonbreakingSpace()
         edit->insertNonbreakingSpace();
 }
 
-void KWView::slotSoftHyphen()
+void KWView::slotLineBreak()
 {
     KWTextFrameSetEdit * edit = currentTextEdit();
     if ( edit )
-        edit->insertSoftHyphen();
+        edit->insertLineBreak();
 }
 
 void KWView::refreshAllVariable()
Index: kword/kwview.h
===================================================================
RCS file: /home/kde/koffice/kword/kwview.h,v
retrieving revision 1.138
diff -u -3 -p -u -r1.138 kwview.h
--- kword/kwview.h	2002/02/27 01:12:03	1.138
+++ kword/kwview.h	2002/02/28 12:46:13
@@ -312,6 +312,7 @@ public slots:
     void showDocStructure();
 
     void slotSoftHyphen();
+    void slotLineBreak();
     void slotNonbreakingSpace();
 
     void refreshAllVariable();
Index: lib/kotext/kotextformatter.cc
===================================================================
RCS file: /home/kde/koffice/lib/kotext/kotextformatter.cc,v
retrieving revision 1.17
diff -u -3 -p -u -r1.17 kotextformatter.cc
--- lib/kotext/kotextformatter.cc	2002/02/18 14:31:24	1.17
+++ lib/kotext/kotextformatter.cc	2002/02/28 12:46:13
@@ -206,7 +206,7 @@ int KoTextFormatter::format( Qt3::QTextD
 	     ( wrapAtColumn() == -1 && x + ww > w && lastBreak != -1 ||
 	       wrapAtColumn() == -1 && x + ww > w - 4 && lastBreak == -1 && \
allowBreakInWords() ||  wrapAtColumn() != -1 && col >= wrapAtColumn() ) ||
-	       parag->isNewLinesAllowed() && lastChr == '\n' ) {
+	       parag->isNewLinesAllowed() && lastChr == '\n' && lastBreak > -1 ) {
 #ifdef DEBUG_FORMATTER
 	    qDebug( "BREAKING" );
 #endif
Index: lib/kotext/kotextparag.cc
===================================================================
RCS file: /home/kde/koffice/lib/kotext/kotextparag.cc,v
retrieving revision 1.38
diff -u -3 -p -u -r1.38 kotextparag.cc
--- lib/kotext/kotextparag.cc	2002/02/21 21:30:31	1.38
+++ lib/kotext/kotextparag.cc	2002/02/28 12:46:13
@@ -34,6 +34,7 @@ KoTextParag::KoTextParag( KoTextDocument
 {
     //kdDebug() << "KoTextParag::KoTextParag " << this << endl;
     m_item = 0L;
+    setNewLinesAllowed(TRUE);
 }
 
 KoTextParag::~KoTextParag()
@@ -440,9 +441,17 @@ void KoTextParag::drawParagString( QPain
     if ( lastFormat->textBackgroundColor().isValid() )
         painter.fillRect( startX_pix, lastY_pix, bw, h_pix, \
lastFormat->textBackgroundColor() );  
-    drawParagStringInternal( painter, s, start, len, startX_pix,
+    // don't want to draw line breaks but want them when drawing formatting chars
+    int draw_len = len;
+    int draw_bw = bw;
+    if ( at( start + len - 1 )->c == '\n' ) {
+        draw_len--;
+        draw_bw -= at( start + len - 1 )->pixelwidth;
+    }
+
+    drawParagStringInternal( painter, s, start, draw_len, startX_pix,
                              lastY_pix, baseLine_pix,
-                             bw, // Note that bw is already in pixels (see \
QTextParag::paint) +                             draw_bw, // Note that bw is already \
                in pixels (see QTextParag::paint)
                              h_pix, drawSelections, lastFormat, i, selectionStarts,
                              selectionEnds, cg, rightToLeft, zh );
 
@@ -662,8 +671,7 @@ int KoTextParag::nextTab( int chnum, int
                     // Look for the next tab (or EOL)
                     int c = chnum + 1;
                     int w = 0;
-                    // We include the trailing space in the calculation because QRT \
                actually formats it
-                    while ( c < string()->length() - 1 && string()->at( c ).c != \
'\t' ) +                    while ( c < string()->length() - 1 && string()->at( c ).c \
!= '\t' && string()->at( c ).c != '\n' )  {
                         KoTextStringChar & ch = string()->at( c );
                         // Determine char width (same code as the one in \
QTextFormatterBreak[In]Words::format()) @@ -686,7 +694,7 @@ int KoTextParag::nextTab( \
int chnum, int  int w = 0;
                     int decimalPoint = \
KGlobal::locale()->decimalSymbol()[0].unicode();  bool digitFound = false;
-                    while ( c < string()->length()-1 && string()->at( c ).c != '\t' \
) +                    while ( c < string()->length()-1 && string()->at( c ).c != \
'\t' && string()->at( c ).c != '\n' )  {
                         KoTextStringChar & ch = string()->at( c );
                         if ( ch.c.isDigit() )
Index: lib/kotext/kotextview.cc
===================================================================
RCS file: /home/kde/koffice/lib/kotext/kotextview.cc,v
retrieving revision 1.40
diff -u -3 -p -u -r1.40 kotextview.cc
--- lib/kotext/kotextview.cc	2002/02/27 01:10:45	1.40
+++ lib/kotext/kotextview.cc	2002/02/28 12:46:13
@@ -939,6 +939,12 @@ void KoTextView::insertSoftHyphen()
                           false /* no newline */, true, i18n("Insert Soft Hyphen") \
);  }
 
+void KoTextView::insertLineBreak()
+{
+    textObject()->insert( cursor(), currentFormat(), QChar('\n'),
+                          false /* no newline */, true, i18n("Insert Line Break") );
+}
+
 void KoTextView::insertNonbreakingSpace()
 {
     textObject()->insert( cursor(), currentFormat(), QChar(0xa0) /* see QRichText \
                */,
Index: lib/kotext/kotextview.h
===================================================================
RCS file: /home/kde/koffice/lib/kotext/kotextview.h,v
retrieving revision 1.31
diff -u -3 -p -u -r1.31 kotextview.h
--- lib/kotext/kotextview.h	2002/02/27 01:10:45	1.31
+++ lib/kotext/kotextview.h	2002/02/28 12:46:13
@@ -120,6 +120,7 @@ public:
 
 
     void insertSoftHyphen();
+    void insertLineBreak();
     void insertNonbreakingSpace();
     void insertSpecialChar(QChar _c);
     void changeCaseOfText(KoChangeCaseDia::TypeOfCase _type);
Index: lib/kotext/qrichtext.cpp
===================================================================
RCS file: /home/kde/koffice/lib/kotext/qrichtext.cpp,v
retrieving revision 1.32
diff -u -3 -p -u -r1.32 qrichtext.cpp
--- lib/kotext/qrichtext.cpp	2002/02/21 21:30:31	1.32
+++ lib/kotext/qrichtext.cpp	2002/02/28 12:46:13
@@ -4192,8 +4192,7 @@ void QTextParag::paint( QPainter &painte
 	    lastFormat = chr->format();
 	    lastY = cy;
 	    startX = chr->x;
-	    if ( !chr->isCustom() && chr->c != '\n' )
-		paintEnd = i;
+	    paintEnd = i;
 	    bw = cw;
 	    if ( !chr->isCustom() )
 		continue;
@@ -4225,13 +4224,8 @@ void QTextParag::paint( QPainter &painte
 				 lastFormat, i, selectionStarts, selectionEnds, cg, lastDirection );
 	    }
 	    if ( !chr->isCustom() ) {
-		if ( chr->c != '\n' ) {
-		    paintStart = i;
-		    paintEnd = i;
-		} else {
-		    paintStart = i+1;
-		    paintEnd = -1;
-		}
+		paintStart = i;
+		paintEnd = i;
 		lastFormat = chr->format();
 		lastY = cy;
 		startX = chr->x;
@@ -4257,12 +4251,10 @@ void QTextParag::paint( QPainter &painte
 		}
 	    }
 	} else {
-	    if ( chr->c != '\n' ) {
-		if( chr->rightToLeft ) {
-		    startX = chr->x;
-		}
-		paintEnd = i;
+	    if( chr->rightToLeft ) {
+		startX = chr->x;
 	    }
+	    paintEnd = i;
 	    bw += cw;
 	}
 	lastBaseLine = baseLine;


_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/koffice-devel

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic