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

List:       kde-devel
Subject:    KFileDialog patch - request for comments
From:       Trevor Harmon <trevor () vocaro ! com>
Date:       2002-06-26 17:25:25
[Download RAW message or body]

The QFileDialog class can be customized by adding widgets at runtime. (Refer 
to function QFileDialog::addWidgets.) KFileDialog doesn't have a similar 
feature, and because I need one, I'm submitting a patch. I'm satisfied with 
the results, but I'd like comments before I submit it to the maintainer.

This patch adds an extra parameter to the constructor, a QWidget. By default, 
this is set to zero to ensure source compatibility. If specified, the 
constructor adds the given QWidget below the Filter list.  Note that the 
QWidget can be a container of other QWidgets, allowing for any variety of 
widgets that the developer needs to display. If you want a screenshot of how 
this looks, just let me know.

This is less powerful than QFileDialog, which can, for example, put widgets on 
the left and right sides of the box.  But I don't really see a need for this, 
especially considering the amount of work needed to add such extra features. 
(I believe it would require a complete re-write of the layout code for 
KFileDialog.) Any dissent?

A possibly big problem is binary compatibility, which I don't know much about. 
I think that by changing the constuctor's signature, any KDE 3.0 program that 
uses KFileDialog would have to be recompiled if this patch is applied for KDE 
3.1. Is this correct?

A possible solution is instead to add a function. Doing this would not, AFAIK, 
break binary compatibility. So I could, for instance, do what QFileDialog 
does and have an addWidget function for passing a custom QWidget. The problem 
with this is that the widget would have to be inserted *after* the dialog has 
already been laid out. I tried to implement it this way at first, but I could 
never get it working, not even after two hours of work. Perhaps somebody 
knows how to do it?

Trevor

P.S. In an earlier thread, I asked about the licensing implications of copying 
Qt's implementation of QFileDialog::addWidget. Just so you know, I never 
looked at the sources of QFileDialog while implementing this patch. (Although 
I did study the documentation a lot!) That means there should be no licensing 
problems.




["kfiledialog_test.cpp" (text/x-c++src)]

#include <kapp.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kpushbutton.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>
#include <qlayout.h>

int main( int argc, char **argv )
{
    KApplication a( argc, argv , "p2");

    KPushButton *hello=new KPushButton( i18n("Hello World !"), 0 );
    hello->setAutoResize( TRUE );
    QObject::connect( hello, SIGNAL(clicked()), &a, SLOT(quit()) );

    // Set up a group box with radio buttons, inside container widget w
    QWidget *w = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout(w);
    QButtonGroup* grp = new QButtonGroup( 1, QGroupBox::Horizontal, "Button Group", w);
    grp->setExclusive(true);
    layout->addWidget(grp);
    QRadioButton *rb = new QRadioButton("radio1", grp);
    rb->setChecked(true);
    new QRadioButton("radio2", grp);

    a.setMainWidget( hello );
    hello->show();

    KFileDialog dlg(QString::null, "*", a.mainWidget(), "hello_filedialog", true, w);
    dlg.show();

    return a.exec();
}


["kfiledialog.cpp.patch" (text/x-diff)]

--- kfiledialog.cpp.orig	Thu Jun 27 01:22:09 2002
+++ kfiledialog.cpp	Thu Jun 27 00:53:11 2002
@@ -109,6 +109,7 @@ struct KFileDialogPrivate
     QHBoxLayout *btngroup;
 
     QWidget *mainWidget;
+    QWidget *customWidget;
 
     QLabel *myStatusLine;
 
@@ -157,7 +158,7 @@ KURL *KFileDialog::lastDirectory; // to 
 static KStaticDeleter<KURL> ldd;
 
 KFileDialog::KFileDialog(const QString& startDir, const QString& filter,
-                         QWidget *parent, const char* name, bool modal)
+                         QWidget *parent, const char* name, bool modal, QWidget *w)
     : KDialogBase( parent, name, modal, QString::null, 0 )
 {
     d = new KFileDialogPrivate();
@@ -168,6 +169,7 @@ KFileDialog::KFileDialog(const QString& 
     d->hasView = false;
     d->mainWidget = new QWidget( this, "KFileDialog::mainWidget");
     setMainWidget( d->mainWidget );
+    d->customWidget = w;
     d->okButton = new KPushButton( KStdGuiItem::ok(), d->mainWidget );
     d->okButton->setDefault( true );
     d->cancelButton = new KPushButton(KStdGuiItem::cancel(), d->mainWidget);
@@ -939,7 +941,11 @@ void KFileDialog::initGUI()
         d->myStatusLine->show();
     }
 
-    vbox->addSpacing(3);
+    if ( d->customWidget != 0 )
+    {
+        d->customWidget->reparent( d->mainWidget, QPoint() );
+        vbox->addWidget( d->customWidget );
+    }
 
     setTabOrder(ops,  locationEdit);
     setTabOrder(locationEdit, filterWidget);

["kfiledialog.h.patch" (text/x-diff)]

--- kfiledialog.h.orig	Thu Jun 27 01:22:15 2002
+++ kfiledialog.h	Thu Jun 27 00:51:25 2002
@@ -116,7 +116,7 @@ public:
       */
     KFileDialog(const QString& startDir, const QString& filter,
 		QWidget *parent, const char *name,
-		bool modal);
+		bool modal, QWidget *w = 0);
 
     /**
      * Destructs the file dialog.

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

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

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