kresources Library API Documentation

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., 59 Temple Place - Suite 330, 00021 Boston, MA 02111-1307, USA. 00022 */ 00023 00024 #include <kbuttonbox.h> 00025 #include <kglobalsettings.h> 00026 #include <klistbox.h> 00027 #include <klocale.h> 00028 #include <kmessagebox.h> 00029 #include <kiconloader.h> 00030 00031 #include <qgroupbox.h> 00032 #include <qlayout.h> 00033 #include <qpushbutton.h> 00034 00035 #include "resource.h" 00036 00037 #include "selectdialog.h" 00038 00039 using namespace KRES; 00040 00041 SelectDialog::SelectDialog( QPtrList<Resource> list, QWidget *parent, 00042 const char *name ) 00043 : KDialog( parent, name, true ) 00044 { 00045 setCaption( i18n( "Resource Selection" ) ); 00046 resize( 300, 200 ); 00047 00048 QVBoxLayout *mainLayout = new QVBoxLayout( this ); 00049 mainLayout->setMargin( marginHint() ); 00050 00051 QGroupBox *groupBox = new QGroupBox( 2, Qt::Horizontal, this ); 00052 groupBox->setTitle( i18n( "Resources" ) ); 00053 00054 mResourceId = new KListBox( groupBox ); 00055 00056 mainLayout->addWidget( groupBox ); 00057 00058 mainLayout->addSpacing( 10 ); 00059 00060 KButtonBox *buttonBox = new KButtonBox( this ); 00061 00062 buttonBox->addStretch(); 00063 QPushButton* ok = buttonBox->addButton( i18n( "&OK" ), this, SLOT( accept() ) ); 00064 QPushButton* cancel = buttonBox->addButton( i18n( "&Cancel" ), this, SLOT( reject() ) ); 00065 if (KGlobalSettings::showIconsOnPushButtons()) { 00066 ok->setIconSet( SmallIconSet("button_ok") ); 00067 cancel->setIconSet( SmallIconSet("button_cancel") ); 00068 } 00069 buttonBox->layout(); 00070 00071 mainLayout->addWidget( buttonBox ); 00072 00073 // setup listbox 00074 uint counter = 0; 00075 for ( uint i = 0; i < list.count(); ++i ) { 00076 Resource *resource = list.at( i ); 00077 if ( resource && !resource->readOnly() ) { 00078 mResourceMap.insert( counter, resource ); 00079 mResourceId->insertItem( resource->resourceName() ); 00080 counter++; 00081 } 00082 } 00083 00084 mResourceId->setCurrentItem( 0 ); 00085 connect( mResourceId, SIGNAL(returnPressed(QListBoxItem*)), 00086 SLOT(accept()) ); 00087 connect( mResourceId, SIGNAL( executed( QListBoxItem* ) ), 00088 SLOT( accept() ) ); 00089 } 00090 00091 Resource *SelectDialog::resource() 00092 { 00093 if ( mResourceId->currentItem() != -1 ) 00094 return mResourceMap[ mResourceId->currentItem() ]; 00095 else 00096 return 0; 00097 } 00098 00099 Resource *SelectDialog::getResource( QPtrList<Resource> list, QWidget *parent ) 00100 { 00101 if ( list.count() == 0 ) { 00102 KMessageBox::error( parent, i18n( "There is no resource available!" ) ); 00103 return 0; 00104 } 00105 00106 if ( list.count() == 1 ) return list.first(); 00107 00108 // the following lines will return a writeable resource if only _one_ writeable 00109 // resource exists 00110 Resource *found = 0; 00111 Resource *it = list.first(); 00112 while ( it ) { 00113 if ( !it->readOnly() ) { 00114 if ( found ) { 00115 found = 0; 00116 break; 00117 } else 00118 found = it; 00119 } 00120 it = list.next(); 00121 } 00122 00123 if ( found ) 00124 return found; 00125 00126 SelectDialog dlg( list, parent ); 00127 if ( dlg.exec() == KDialog::Accepted ) return dlg.resource(); 00128 else return 0; 00129 }
KDE Logo
This file is part of the documentation for kresources Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:55:41 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003