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

List:       kde-pim
Subject:    [Kde-pim] Re: [PATCH] occasional crashes, bug in DetailledPrintStyle d'tor?
From:       Tobias Koenig <tokoe () kde ! org>
Date:       2002-09-06 14:06:50
[Download RAW message or body]

On Fri, Sep 06, 2002 at 01:09:11PM +0200, Jost Schenck wrote:
> > On Thu, Sep 05, 2002 at 11:50:21PM +0200, Jost Schenck wrote:
> > Hi,
> > 
> > my fix doesn't solve that problem :(
> > 
> > > Ooops, no, this leads to worse problems
> > What problems?
> 
> I don't have the sources here at work, but I think the problem was that in
> the printing wizard -- where you can select the style to be used -- a
> PrintingStyle get's deleted and a new one created every time you
> select something from the combo box.
Ahh, now I see...
The problem is, that the single styles have to add the additonal pages =>
many problems :)
I've attached a patch that add the following methods to PrintingStyle

	addPage( QWidget*, const QString &title );
	showPages();
	hidePages();

addPage() does register any additional page for a style and stores a pointer
in a pageList. showPages() add all the pages, that are stored in the pageList
to the wizard. hidePages() removes all pages from the wizard.
showPages() and hidePages() is called from PrintingWizard every time the style
changed => only the style specific pages are added to the wizard.
With this behaviour we need no ugly double deletion etc.

Could everbody please test the code for (hopefully not existing) bugs
and give me an OK for commiting?

Ciao,
Tobias
-- 
In a world without walls and fences who
needs Windows and Gates???

["printing.diff" (text/plain)]

? printing.diff
Index: detailledstyle.cpp
===================================================================
RCS file: /home/kde/kdepim/kaddressbook/printing/detailledstyle.cpp,v
retrieving revision 1.8
diff -u -b -p -r1.8 detailledstyle.cpp
--- detailledstyle.cpp	2002/09/05 22:16:04	1.8
+++ detailledstyle.cpp	2002/09/06 14:01:15
@@ -68,11 +68,10 @@ namespace KABPrinting {
         bool kdeFonts;
         QFont standard=KGlobalSettings::generalFont();
         QFont fixed=KGlobalSettings::fixedFont();
-        // ----- set the preview image:
-        setPreview("detailed-style.png"); // now that is simple, huh?
-        // ----- add our pages:
-        wizard()->insertPage(mPageAppearance, i18n("Appearance"), -1);
-        wizard()->setAppropriate(mPageAppearance, true);
+
+        setPreview( "detailed-style.png" );
+
+        addPage( mPageAppearance, i18n("Appearance") );
         // ----- set some values in the pages:
         // ----- now try to load previous settings from the
         // configuration file and set the UI items accordingly:
@@ -108,17 +107,10 @@ namespace KABPrinting {
         mPageAppearance->layout()->setMargin(KDialog::marginHint());
         mPageAppearance->layout()->setSpacing(KDialog::spacingHint());
         // ----- enable finish in our page:
-        wizard()->setFinishEnabled(mPageAppearance, true);
     }
 
     DetailledPrintStyle::~DetailledPrintStyle()
     {
-        // we have to delete the pages manually, since they are
-        // created as children of the wizard, that is, they are not
-        // deleted when the print style object is:
-        delete mPageAppearance;
-        mPageAppearance = 0;
-
         if( mEPntr != 0 )
           delete mEPntr;
         mEPntr = 0;
@@ -288,7 +280,7 @@ namespace KABPrinting {
 
     PrintStyle *DetailledPrintStyleFactory::create()
     {
-        return new DetailledPrintStyle(parent, name);
+        return new DetailledPrintStyle( mParent, mName );
     }
 
     QString DetailledPrintStyleFactory::description()
Index: mikesstyle.cpp
===================================================================
RCS file: /home/kde/kdepim/kaddressbook/printing/mikesstyle.cpp,v
retrieving revision 1.9
diff -u -b -p -r1.9 mikesstyle.cpp
--- mikesstyle.cpp	2002/08/31 00:01:45	1.9
+++ mikesstyle.cpp	2002/09/06 14:01:15
@@ -268,7 +268,7 @@ namespace KABPrinting
 
     PrintStyle *MikesStyleFactory::create()
     {
-        return new MikesStyle(parent, name);
+        return new MikesStyle( mParent, mName );
     }
 
     QString MikesStyleFactory::description()
Index: printingwizard.cpp
===================================================================
RCS file: /home/kde/kdepim/kaddressbook/printing/printingwizard.cpp,v
retrieving revision 1.7
diff -u -b -p -r1.7 printingwizard.cpp
--- printingwizard.cpp	2002/07/29 12:49:43	1.7
+++ printingwizard.cpp	2002/09/06 14:01:16
@@ -79,31 +79,35 @@ namespace KABPrinting {
         }
     }
 
-    void PrintingWizardImpl::slotStyleSelected(int index)
+    void PrintingWizardImpl::slotStyleSelected( int index )
     {
-        if(index>=0 && index<mBasicPage->cbStyle->count())
-        {
-            if(style!=0)
-            {
-                delete style;
-                style=0;
-            }
-            PrintStyleFactory *factory=styleFactories.at(index);
+      if ( index < 0 || index >= styleFactories.count() )
+        return;
+
+      setFinishEnabled( mBasicPage, false );
+
+      if ( style )
+        style->hidePages();
+
+      if ( mStyleList.at( index ) != 0 )
+        style = mStyleList.at( index );
+      else {
+        PrintStyleFactory *factory = styleFactories.at( index );
             kdDebug() << "PrintingWizardImpl::slotStyleSelected: "
                       << "creating print style "
                       << factory->description() << endl;
-            style=factory->create();
-        }
-        const QPixmap& preview=style->preview();
-        mBasicPage->plPreview->setPixmap(preview); // reset it if it is Null
-        if(preview.isNull())
-        {
-            mBasicPage->plPreview->setText(i18n("(No preview available.)"));
-        }
-        if(pageCount()<=1) // the style did not add pages
-        {
-            setFinishEnabled(mBasicPage, style!=0);
+        style = factory->create();
+        mStyleList.insert( index, style );
         }
+
+      style->showPages();
+
+      const QPixmap& preview = style->preview();
+      mBasicPage->plPreview->setPixmap( preview );
+      if ( preview.isNull() )
+        mBasicPage->plPreview->setText( i18n( "(No preview available.)" ) );
+
+      setFinishEnabled( page( pageCount() - 1 ), true );
     }
 
     KABC::AddressBook* PrintingWizardImpl::document()
Index: printingwizard.h
===================================================================
RCS file: /home/kde/kdepim/kaddressbook/printing/printingwizard.h,v
retrieving revision 1.4
diff -u -b -p -r1.4 printingwizard.h
--- printingwizard.h	2002/06/14 03:20:34	1.4
+++ printingwizard.h	2002/09/06 14:01:16
@@ -64,6 +64,7 @@ namespace KABPrinting {
         void slotStyleSelected(int);
     protected:
         QPtrList<PrintStyleFactory> styleFactories;
+        QPtrList<PrintStyle> mStyleList;
         PrintStyle *style;
         /** The general page. */
         BasicPage *mBasicPage;
Index: printstyle.cpp
===================================================================
RCS file: /home/kde/kdepim/kaddressbook/printing/printstyle.cpp,v
retrieving revision 1.4
diff -u -b -p -r1.4 printstyle.cpp
--- printstyle.cpp	2002/06/14 03:20:34	1.4
+++ printstyle.cpp	2002/09/06 14:01:16
@@ -17,74 +17,91 @@
 #include <kstandarddirs.h>
 #include <kdebug.h>
 
+#include <qwidget.h>
+
 #include "printstyle.h"
 #include "printingwizard.h"
 
-namespace KABPrinting {
+using namespace KABPrinting;
 
-    PrintStyle::PrintStyle(PrintingWizard* parent, const char* name)
-        : QObject(parent, name),
-          mWizard(parent)
-    {
-    }
 
-    PrintStyle::~PrintStyle()
-    {
-    }
+PrintStyle::PrintStyle( PrintingWizard* parent, const char* name )
+  : QObject( parent, name ), mWizard( parent )
+{
+}
+
+PrintStyle::~PrintStyle()
+{
+}
 
-    const QPixmap& PrintStyle::preview()
-    { // this is Null pixmap as long as nothing is assigned to it:
+const QPixmap& PrintStyle::preview()
+{
         return mPreview;
-    }
+}
 
-    void PrintStyle::setPreview(const QPixmap& image)
-    {
-        mPreview=image; // we do not check for Null images etc
-    }
+void PrintStyle::setPreview( const QPixmap& image )
+{
+  mPreview = image;
+}
 
-    bool PrintStyle::setPreview(const QString& filename)
-    {
-        // ----- locate the preview image and set it:
+bool PrintStyle::setPreview( const QString& fileName )
+{
         QPixmap preview;
-        QString file=(QString)"printing/";
-        file.append(filename);
-        QString path=locate("appdata", file);
-        if(path.isNull())
-        {
+  QString path = locate( "appdata", "printing/" + fileName );
+  if ( path.isEmpty() ) {
             kdDebug() << "PrintStyle::setPreview: preview not locatable." << endl;
             return false;
         } else {
-            if(preview.load(path))
-            {
-                setPreview(preview);
+    if ( preview.load( path ) ) {
+      setPreview( preview );
                 return true;
             } else {
-                kdDebug() << "PrintStyle::setPreview: preview at "
-                          << path << " cannot be loaded."
-                          << endl;
+      kdDebug() << "PrintStyle::setPreview: preview at '" << path << "' cannot be loaded." << endl;
                 return false;
             }
         }
-
+}
 
-    }
-
-    PrintingWizard *PrintStyle::wizard()
-    {
+PrintingWizard *PrintStyle::wizard()
+{
         return mWizard;
-    }
+}
 
-    PrintStyleFactory::PrintStyleFactory(PrintingWizard* parent_,
-                                         const char* name_)
-        : parent(parent_),
-          name(name_)
-    {
-    }
-
-    PrintStyleFactory::~PrintStyleFactory()
-    {
-    }
+void PrintStyle::addPage( QWidget *page, const QString &title )
+{
+  if ( mPageList.find( page ) == -1 ) { // not yet in the list
+    mPageList.append( page );
+    mPageTitles.append( title );
+  }
+}
+
+void PrintStyle::showPages()
+{
+  QWidget *wdg = 0;
+  int i = 0;
+  for ( wdg = mPageList.first(); wdg; wdg = mPageList.next(), ++i ) {
+    mWizard->addPage( wdg, mPageTitles[ i ] );
+    if ( i == 0 )
+      mWizard->setAppropriate( wdg, true );
+  }
+
+  if ( wdg )
+    mWizard->setFinishEnabled( wdg, true );
+}
+
+void PrintStyle::hidePages()
+{
+  for ( QWidget *wdg = mPageList.first(); wdg; wdg = mPageList.next() )
+    mWizard->removePage( wdg );
+}
+
+PrintStyleFactory::PrintStyleFactory( PrintingWizard* parent, const char* name )
+        : mParent( parent ), mName( name )
+{
+}
 
+PrintStyleFactory::~PrintStyleFactory()
+{
 }
 
 #include "printstyle.moc"
Index: printstyle.h
===================================================================
RCS file: /home/kde/kdepim/kaddressbook/printing/printstyle.h,v
retrieving revision 1.5
diff -u -b -p -r1.5 printstyle.h
--- printstyle.h	2002/06/14 03:20:34	1.5
+++ printstyle.h	2002/09/06 14:01:16
@@ -23,10 +23,11 @@
 
 namespace KABPrinting {
 
-    class PrintingWizard;
-    class PrintProgress;
+class PrintingWizard;
+class PrintProgress;
 
-    /** The class PrintStyle implements the abstract interface to the
+/**
+ The class PrintStyle implements the abstract interface to the
         PrintingWizards style objects.
         To implement a print style, derive from this class and read
         the information in printingwizard.h to see how this two pieces
@@ -47,60 +48,98 @@ namespace KABPrinting {
         to the wizard object.
     */
 
-    class PrintStyle : public QObject
-    {
+class PrintStyle : public QObject
+{
         Q_OBJECT
+
     public:
-        PrintStyle(PrintingWizard* parent, const char* name=0);
+    PrintStyle( PrintingWizard* parent, const char* name = 0 );
         virtual ~PrintStyle();
-        /** Reimplement this method to actually print. */
+
+    /**
+     Reimplement this method to actually print.
+     */
         virtual void print(QStringList contacts, PrintProgress*)=0;
-        /** Reimplement this method to provide a preview of what will
+
+    /**
+     Reimplement this method to provide a preview of what will
             be printed. It returns an invalid QPixmap by default,
             resulting in a message that no preview is available.
         */
         const QPixmap& preview();
+
+    /**
+     Hide all style specific pages in the wizard.
+     */
+    void hidePages();
+
+    /**
+     Show all style specific pages in the wizard.
+     */
+    void showPages();
+
     protected:
-        /** Load the preview image from the kaddressbook data
+    /**
+     Load the preview image from the kaddressbook data
             directory. The image should be located in the subdirectory
-            "printing. Give only the file name without any prefix as
+     "printing". Give only the file name without any prefix as
             the parameter.
             In case the image cannot be loaded, the preview will show
             a text message that there is no preview available. Do not
             change the preview frame manually if you do not have
             to. The return value is true if loading and setting the
             preview image worked out good.
+     */
+    bool setPreview( const QString& fileName );
+
+    /**
+     Set the preview image.
         */
-        bool setPreview(const QString& filename);
-        /** Set the preview image. */
-        void setPreview(const QPixmap& image);
-        /** Return the wizard object. Styles use it to register pages etc. */
+    void setPreview( const QPixmap& image );
+
+    /**
+     Return the wizard object.
+     */
         PrintingWizard *wizard();
+
+    /**
+     Add additional page to the wizard e.g. a configuration page for
+     the style.
+     */
+    void addPage( QWidget *page, const QString &title );
+
     private:
         PrintingWizard *mWizard;
         QPixmap mPreview;
-    };
+    QPtrList<QWidget> mPageList;
+    QStringList mPageTitles;
+};
 
 
-    /** The factories are used to have all object of the respective
+/**
+ The factories are used to have all object of the respective
         print style created in one place.
         This will maybe be changed to a template because of its simple
         nature :-)
-    */
-    class PrintStyleFactory
-    {
+*/
+class PrintStyleFactory
+{
     public:
-        PrintStyleFactory(PrintingWizard* parent,
-                          const char* name=0);
+    PrintStyleFactory( PrintingWizard* parent, const char* name = 0 );
         virtual ~PrintStyleFactory();
-        virtual PrintStyle *create()=0;
-        /** Overload this method to provide a one-liner description
-            for your print style. */
-        virtual QString description()=0;
+    virtual PrintStyle *create() = 0;
+
+    /**
+     Overload this method to provide a one-liner description
+     for your print style.
+     */
+    virtual QString description() = 0;
+
     protected:
-        PrintingWizard* parent;
-        const char* name;
-    };
+    PrintingWizard* mParent;
+    const char* mName;
+};
+
 }
 
 #endif

_______________________________________________
kde-pim mailing list
kde-pim@mail.kde.org
http://mail.kde.org/mailman/listinfo/kde-pim
kde-pim home page at http://pim.kde.org/

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

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