selectdialog.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <kbuttonbox.h>
00025 #include <klistbox.h>
00026 #include <klocale.h>
00027 #include <kmessagebox.h>
00028
00029 #include <qgroupbox.h>
00030 #include <qlayout.h>
00031
00032 #include "resource.h"
00033
00034 #include "selectdialog.h"
00035
00036 using namespace KRES;
00037
00038 SelectDialog::SelectDialog( QPtrList<Resource> list, QWidget *parent,
00039 const char *name )
00040 : KDialog( parent, name, true )
00041 {
00042 setCaption( i18n( "Resource Selection" ) );
00043 resize( 300, 200 );
00044
00045 QVBoxLayout *mainLayout = new QVBoxLayout( this );
00046 mainLayout->setMargin( marginHint() );
00047
00048 QGroupBox *groupBox = new QGroupBox( 2, Qt::Horizontal, this );
00049 groupBox->setTitle( i18n( "Resources" ) );
00050
00051 mResourceId = new KListBox( groupBox );
00052
00053 mainLayout->addWidget( groupBox );
00054
00055 mainLayout->addSpacing( 10 );
00056
00057 KButtonBox *buttonBox = new KButtonBox( this );
00058
00059 buttonBox->addStretch();
00060 buttonBox->addButton( KStdGuiItem::ok(), this, SLOT( accept() ) );
00061 buttonBox->addButton( KStdGuiItem::cancel(), this, SLOT( reject() ) );
00062 buttonBox->layout();
00063
00064 mainLayout->addWidget( buttonBox );
00065
00066
00067 uint counter = 0;
00068 for ( uint i = 0; i < list.count(); ++i ) {
00069 Resource *resource = list.at( i );
00070 if ( resource && !resource->readOnly() ) {
00071 mResourceMap.insert( counter, resource );
00072 mResourceId->insertItem( resource->resourceName() );
00073 counter++;
00074 }
00075 }
00076
00077 mResourceId->setCurrentItem( 0 );
00078 connect( mResourceId, SIGNAL(returnPressed(QListBoxItem*)),
00079 SLOT(accept()) );
00080 connect( mResourceId, SIGNAL( executed( QListBoxItem* ) ),
00081 SLOT( accept() ) );
00082 }
00083
00084 Resource *SelectDialog::resource()
00085 {
00086 if ( mResourceId->currentItem() != -1 )
00087 return mResourceMap[ mResourceId->currentItem() ];
00088 else
00089 return 0;
00090 }
00091
00092 Resource *SelectDialog::getResource( QPtrList<Resource> list, QWidget *parent )
00093 {
00094 if ( list.count() == 0 ) {
00095 KMessageBox::error( parent, i18n( "There is no resource available!" ) );
00096 return 0;
00097 }
00098
00099 if ( list.count() == 1 ) return list.first();
00100
00101
00102
00103 Resource *found = 0;
00104 Resource *it = list.first();
00105 while ( it ) {
00106 if ( !it->readOnly() ) {
00107 if ( found ) {
00108 found = 0;
00109 break;
00110 } else
00111 found = it;
00112 }
00113 it = list.next();
00114 }
00115
00116 if ( found )
00117 return found;
00118
00119 SelectDialog dlg( list, parent );
00120 if ( dlg.exec() == KDialog::Accepted ) return dlg.resource();
00121 else return 0;
00122 }
|