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

List:       koffice
Subject:    Fwd: [PATCH] kword table col insertion
From:       Emmanuel Touzery <emmanuel.touzery () wanadoo ! fr>
Date:       2002-05-13 13:55:01
[Download RAW message or body]

this message doesn't seem to go through on koffice-devel.
tried to subscribe, but it didn't work (i'm never receiving the mail of 
confirmation of subscription).
anyway if the mail went through on ko-devel, sorry because i already sent 
this one twice there :O(

emmanuel

----------  Forwarded Message  ----------
Subject: [PATCH] kword table col insertion
Date: Mon, 13 May 2002 13:07:33 +0200
From: Emmanuel Touzery <emmanuel.touzery@wanadoo.fr>
To: koffice-devel@kde.org


Hello,

	The attached patch allows to insert a new column in a kword table even if
there is no more space on the page on the right of the table. the table will
simply be resized before adding the column. additionnally, the column size is
then "smart", which means that if you had a two-columns table that took the
whole page width (default when you create a table), you'll get a nicely
proportionned three columns table (with three columns of the same width of
1/3rd of the page).
	the function "resizeWidth" in the kwtableframeset might be not so good, i
don't know the code for tables that well. also maybe you won't like the
arrangement of the code (???).

works here (tm), hope this will be better than my previous patch ;O)

emmanuel.

PS: note that this patch is against one pre-beta1 (JUST before beta1)
CVS. for now i don't have internet at home so i was doing this on this older
checkout (brought my HD at work). i won't have a proper connectivity at home
before some time so if it doesn't apply cleanly and you have some time it's
good if you could merge it in...
is it ok if i send a couple of patches on an "older" CVS?

PPS: posted first on master.kde.org, now on kde.org?

-------------------------------------------------------

["insertcol.diff" (text/x-c++)]

? insertcol.tgz
? insertcol.diff
Index: insdia.cc
===================================================================
RCS file: /home/kde/koffice/kword/insdia.cc,v
retrieving revision 1.19
diff -u -3 -p -r1.19 insdia.cc
--- insdia.cc	13 Oct 2001 00:17:00 -0000	1.19
+++ insdia.cc	13 May 2002 06:29:51 -0000
@@ -118,7 +118,8 @@ bool KWInsertDia::doInsert()
     }
     else
     {
-        KWInsertColumnCommand *cmd = new KWInsertColumnCommand( i18n("Insert \
column"), table, insert); +		// we pass as last parameter the maximum offset that the \
table can use in the page. +		KWInsertColumnCommand *cmd = new KWInsertColumnCommand( \
                i18n("Insert column"), table, insert,  doc->ptPaperWidth() - \
                doc->ptRightBorder());
         //table->insertCol( value->value() - ( rBefore->isChecked() ? 1 : 0 ) );
         cmd->execute();
         doc->addCommand(cmd);
Index: kwcommand.cc
===================================================================
RCS file: /home/kde/koffice/kword/kwcommand.cc,v
retrieving revision 1.133
diff -u -3 -p -r1.133 kwcommand.cc
--- kwcommand.cc	5 Apr 2002 16:30:23 -0000	1.133
+++ kwcommand.cc	13 May 2002 06:29:51 -0000
@@ -890,10 +890,12 @@ void KWDeleteTableCommand::unexecute()
 }
 
 
-KWInsertColumnCommand::KWInsertColumnCommand( const QString &name, KWTableFrameSet * \
_table, int _col ): +KWInsertColumnCommand::KWInsertColumnCommand( const QString \
&name, KWTableFrameSet * _table, int _col, double _maxRight ):  KNamedCommand(name),
     m_pTable(_table),
-    m_colPos(_col)
+    m_colPos(_col),
+	m_maxRight(_maxRight),
+	m_oldWidth(0)
 {
     Q_ASSERT(m_pTable);
     m_ListFrameSet.clear();
@@ -903,8 +905,21 @@ void KWInsertColumnCommand::execute()
 {
     kdDebug() << "KWInsertColumnCommand::execute" << endl;
     KWDocument * doc = m_pTable->kWordDocument();
-    m_pTable->insertCol( m_colPos,m_ListFrameSet);
-    doc->updateAllFrames();
+	double newColSize = 0; // 0 means that KWTableFrameSet::insertCol() will figure out \
itself +	// a insert column = KWTableFrameSet::m_sDefaultColWidth +2 (border), see \
kwtableframeset.cc +	if (m_pTable->boundingRect().right() + \
KWTableFrameSet::m_sDefaultColWidth +2 >= static_cast<int>(m_maxRight)) +	{	// must \
create space (resize the table) +		m_oldWidth = m_pTable->boundingRect().width();
+		// here we calculate the new table size for a table that would take the
+		// entire width of the page, which what the user wants 99% of the time.
+		double newTableWidth =m_maxRight - m_pTable->boundingRect().left();
+		newColSize = newTableWidth / (m_pTable->getCols()+1);
+		double resizeTableWidth = m_maxRight - m_pTable->boundingRect().left();
+		m_pTable->resizeWidth(resizeTableWidth - newColSize);
+	}
+	m_pTable->insertCol(m_colPos, m_ListFrameSet, QPtrList<KWFrame>(), newColSize);
+    Q_ASSERT(m_pTable->boundingRect().right() <= m_maxRight);
+	doc->updateAllFrames();
     doc->layout();
     doc->updateResizeHandles( );
     doc->repaintAllViews();
@@ -925,7 +940,12 @@ void KWInsertColumnCommand::unexecute()
     doc->terminateEditing(m_pTable);
     doc->frameSelectedChanged();
     m_pTable->deleteCol( m_colPos);
-    doc->updateAllFrames();
+	// now undo the resize of the table if necessary:
+	if (m_oldWidth) {
+		// yes, the table was resized, let's undo that :
+		m_pTable->resizeWidth(m_oldWidth);
+	}
+	doc->updateAllFrames();
     doc->layout();
     doc->updateResizeHandles( );
     doc->repaintAllViews();
@@ -967,7 +987,7 @@ void KWInsertRowCommand::unexecute()
     }
     doc->terminateEditing(m_pTable);
     m_pTable->deleteRow( m_rowPos);
-    doc->frameSelectedChanged();
+	doc->frameSelectedChanged();
     doc->updateAllFrames();
     doc->layout();
     doc->updateResizeHandles( );
Index: kwcommand.h
===================================================================
RCS file: /home/kde/koffice/kword/kwcommand.h,v
retrieving revision 1.59
diff -u -3 -p -r1.59 kwcommand.h
--- kwcommand.h	5 Apr 2002 16:30:23 -0000	1.59
+++ kwcommand.h	13 May 2002 06:29:52 -0000
@@ -336,7 +336,8 @@ public:
 class KWInsertColumnCommand : public KNamedCommand
 {
 public:
-    KWInsertColumnCommand( const QString &name, KWTableFrameSet * _table, int _pos);
+    /* for the last parameter, _maxRight, you should pass the maximum offset that \
the table can use at its right (normally m_maxRight - \
m_pTable->boundingRect().left())*/ +	KWInsertColumnCommand( const QString &name, \
KWTableFrameSet * _table, int _pos, double _maxRight);  ~KWInsertColumnCommand() {}
 
     void execute();
@@ -345,6 +346,8 @@ protected:
     KWTableFrameSet *m_pTable;
     QPtrList<KWFrameSet> m_ListFrameSet;
     unsigned int m_colPos;
+	double m_maxRight; // this is the maximum x of the right part of the table (used so \
that the table does no go off the page) +	double m_oldWidth; // will be 0 after \
execute() if the width of the table was not changed by the operation  };
 
 
Index: kwtableframeset.cc
===================================================================
RCS file: /home/kde/koffice/kword/kwtableframeset.cc,v
retrieving revision 1.191
diff -u -3 -p -r1.191 kwtableframeset.cc
--- kwtableframeset.cc	3 Apr 2002 06:11:15 -0000	1.191
+++ kwtableframeset.cc	13 May 2002 06:29:52 -0000
@@ -178,7 +178,7 @@ void KWTableFrameSet::addCell( Cell *cel
 
 KoRect KWTableFrameSet::boundingRect() {
     return KoRect(m_colPositions[0],    // left
-                m_rowPositions[0],      // right
+                m_rowPositions[0],      // top
                 m_colPositions.last()-m_colPositions[0], // width
                 m_rowPositions.last()-m_rowPositions[0]);// height
 }
@@ -601,6 +601,25 @@ kdDebug() << "diff += " <<  topOfPage  <
     kdDebug(32004) << "KWTableFrameSet::recalcRows done" << endl;
 }
 
+void KWTableFrameSet::resizeWidth( double width ) {
+	Q_ASSERT(width != 0);
+	Q_ASSERT(boundingRect().width() != 0);
+	kdDebug() << "bounding width before resize " << boundingRect().width() << endl;
+	double growth = width / boundingRect().width();
+
+	// since we move all the columns, we also move the 1st one,
+	// depending where it is on the page.
+	// just compensate by substracting that offset.
+	double moveOffset = m_colPositions[0] * growth - m_colPositions[0];
+
+	for (uint i=0; i<m_colPositions.count(); i++) {
+		m_colPositions[i] = m_colPositions[i] * growth - moveOffset;
+	}
+	finalize();
+	kdDebug() << "bounding width after resize" << boundingRect().width() << endl;
+	Q_ASSERT(boundingRect().width() - width < 0.01);
+}
+
 void KWTableFrameSet::setBoundingRect( KoRect rect, CellSize widthMode, CellSize \
heightMode ) {  // Column postions..
     m_rowPositions.clear();
@@ -932,9 +951,11 @@ kdDebug(32004) << "adjusting " << rowPos
         finalize();
 }
 
-void KWTableFrameSet::insertCol( unsigned int newColNumber,QPtrList<KWFrameSet> \
                redoFrameset, QPtrList<KWFrame>redoFrame ) {
-    double width=60;
-    if(! redoFrame.isEmpty()) {
+void KWTableFrameSet::insertCol( unsigned int newColNumber,QPtrList<KWFrameSet> \
redoFrameset, QPtrList<KWFrame>redoFrame, double width /* =0.*/ ) { +	if (width == 0)
+		// using default width
+		width= KWTableFrameSet::m_sDefaultColSize;
+	if(! redoFrame.isEmpty()) {
         KWFrame *f=redoFrameset.at(0)->frame(0);
         width=f->width() + f->leftBorder().ptWidth + f->rightBorder().ptWidth;
     }
Index: kwtableframeset.h
===================================================================
RCS file: /home/kde/koffice/kword/kwtableframeset.h,v
retrieving revision 1.86
diff -u -3 -p -r1.86 kwtableframeset.h
--- kwtableframeset.h	5 Apr 2002 16:30:23 -0000	1.86
+++ kwtableframeset.h	13 May 2002 06:29:52 -0000
@@ -52,7 +52,6 @@ class KWordFrameSetIface;
  * can be used to describe the same thing: one table-cell
  */
 
-
 class KWTableFrameSet : public KWFrameSet
 {
     Q_OBJECT
@@ -140,6 +139,12 @@ public:
      */
     void setBoundingRect( KoRect rect, CellSize widthMode, CellSize heightMode );
 
+	/**
+	 *  change the width of the table, keeping the proportions of the cells
+	 * (if one is wider than the others, it is still wider after resize)
+	 */
+	void resizeWidth( double width );
+
     /** resize and position all cells */
     void recalcCols(int _col=-1,int _row=-1);
     void recalcRows(int _col=-1,int _row=-1);
@@ -183,7 +188,7 @@ public:
     /** insert a row of new cells, use the getCols() call to decide how many cells \
are created */  void insertRow( unsigned int _idx,QPtrList<KWFrameSet> \
listFrameSet=QPtrList<KWFrameSet>(),QPtrList<KWFrame>listFrame=QPtrList<KWFrame>(), \
                bool _recalc = true, bool _removeable = false );
     /** insert a column of new cells use the getRows() call to decide how many cells \
                are created */
-    void insertCol( unsigned int _idx,QPtrList<KWFrameSet> \
listFrameSet=QPtrList<KWFrameSet>(), QPtrList<KWFrame> \
listFrame=QPtrList<KWFrame>()); +    void insertCol( unsigned int \
_idx,QPtrList<KWFrameSet> listFrameSet=QPtrList<KWFrameSet>(), QPtrList<KWFrame> \
listFrame=QPtrList<KWFrame>(), double width = 0.);  
     /** remove all the cells in a certain row */
     void deleteRow( unsigned int _idx, bool _recalc = true );
@@ -354,6 +359,8 @@ public:
     // Set the cell which is currently being edited
     void setCurrentCell( KWFrameSet * fs );
     void setCurrentCell( const KoPoint & dPoint );
+
+	static const uint m_sDefaultColSize = 60;
 
 protected:
     KWFrameSetEdit * m_currentCell;
Index: kwview.cc
===================================================================
RCS file: /home/kde/koffice/kword/kwview.cc,v
retrieving revision 1.559
diff -u -3 -p -r1.559 kwview.cc
--- kwview.cc	17 Apr 2002 07:05:21 -0000	1.559
+++ kwview.cc	13 May 2002 06:29:56 -0000
@@ -3100,19 +3100,9 @@ void KWView::tableInsertCol()
     KWTableFrameSet *table = m_gui->canvasWidget()->getCurrentTable();
     Q_ASSERT(table);
     // value = 62 because a insert column = 60 +2 (border )see kwtableframeset.cc
-    if ( table->boundingRect().right() + 62 > static_cast<int>( \
                m_doc->ptPaperWidth() ) )
-    {
-        KMessageBox::sorry( this,
-                            i18n( "There is not enough space at the right of the \
                table "
-                                  "to insert a new column." ),
-                            i18n( "Insert Column" ) );
-    }
-    else
-    {
-        KWInsertDia dia( this, "", table, m_doc, KWInsertDia::COL, \
                m_gui->canvasWidget() );
-        dia.setCaption( i18n( "Insert Column" ) );
-        dia.exec();
-    }
+    KWInsertDia dia( this, "", table, m_doc, KWInsertDia::COL, m_gui->canvasWidget() \
); +    dia.setCaption( i18n( "Insert Column" ) );
+    dia.exec();
 }
 
 void KWView::tableDeleteRow()


____________________________________
koffice mailing list
koffice@mail.kde.org
To unsubscribe please visit:
http://mail.kde.org/mailman/listinfo/koffice

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

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