selectdialog.cpp
00001 /* 00002 This file is part of libkresources. 00003 00004 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 00005 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> 00006 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #include <kbuttonbox.h> 00025 #include <klistbox.h> 00026 #include <klocale.h> 00027 #include <kmessagebox.h> 00028 00029 #include <tqgroupbox.h> 00030 #include <tqlayout.h> 00031 00032 #include "resource.h" 00033 00034 #include "selectdialog.h" 00035 00036 using namespace KRES; 00037 00038 SelectDialog::SelectDialog( TQPtrList<Resource> list, TQWidget *parent, 00039 const char *name ) 00040 : KDialog( parent, name, true ) 00041 { 00042 setCaption( i18n( "Resource Selection" ) ); 00043 resize( 300, 200 ); 00044 00045 TQVBoxLayout *mainLayout = new TQVBoxLayout( this ); 00046 mainLayout->setMargin( marginHint() ); 00047 00048 TQGroupBox *groupBox = new TQGroupBox( 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(), TQT_TQOBJECT(this), TQT_SLOT( accept() ) ); 00061 buttonBox->addButton( KStdGuiItem::cancel(), TQT_TQOBJECT(this), TQT_SLOT( reject() ) ); 00062 buttonBox->layout(); 00063 00064 mainLayout->addWidget( buttonBox ); 00065 00066 // setup listbox 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, TQT_SIGNAL(returnPressed(TQListBoxItem*)), 00079 TQT_SLOT(accept()) ); 00080 connect( mResourceId, TQT_SIGNAL( executed( TQListBoxItem* ) ), 00081 TQT_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( TQPtrList<Resource> list, TQWidget *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 // the following lines will return a writeable resource if only _one_ writeable 00102 // resource exists 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 }