kresources
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 "selectdialog.h" 00025 00026 #include <klocale.h> 00027 #include <kmessagebox.h> 00028 00029 #include <QtGui/QGroupBox> 00030 #include <QtGui/QLayout> 00031 #include <QtGui/QListWidget> 00032 00033 #include "resource.h" 00034 00035 using namespace KRES; 00036 00037 class SelectDialog::SelectDialogPrivate 00038 { 00039 public: 00040 QListWidget *mResourceId; 00041 QMap<int, Resource*> mResourceMap; 00042 }; 00043 00044 static bool resourceNameLessThan( Resource *a, Resource *b ) 00045 { 00046 return a->resourceName() < b->resourceName(); 00047 } 00048 00049 SelectDialog::SelectDialog( QList<Resource *> list, QWidget *parent ) 00050 : KDialog( parent ), d( new SelectDialogPrivate ) 00051 { 00052 setModal(true); 00053 setCaption( i18n( "Resource Selection" ) ); 00054 resize( 300, 200 ); 00055 setButtons( Ok|Cancel ); 00056 setDefaultButton( Ok ); 00057 00058 QWidget *widget = new QWidget( this ); 00059 setMainWidget( widget ); 00060 00061 QVBoxLayout *mainLayout = new QVBoxLayout( widget ); 00062 mainLayout->setMargin( 0 ); 00063 00064 QGroupBox *groupBox = new QGroupBox( widget ); 00065 QGridLayout *grid = new QGridLayout; 00066 groupBox->setLayout( grid ); 00067 groupBox->setTitle( i18n( "Resources" ) ); 00068 00069 d->mResourceId = new QListWidget( groupBox ); 00070 grid->addWidget( d->mResourceId, 0, 0 ); 00071 00072 mainLayout->addWidget( groupBox ); 00073 00074 // sort resources by name 00075 qSort( list.begin(), list.end(), resourceNameLessThan ); 00076 00077 // setup listbox 00078 uint counter = 0; 00079 for ( int i = 0; i < list.count(); ++i ) { 00080 Resource *resource = list.at( i ); 00081 if ( resource && !resource->readOnly() ) { 00082 d->mResourceMap.insert( counter, resource ); 00083 d->mResourceId->addItem( resource->resourceName() ); 00084 counter++; 00085 } 00086 } 00087 00088 d->mResourceId->setCurrentRow( 0 ); 00089 connect( d->mResourceId, SIGNAL( itemActivated(QListWidgetItem*)), 00090 SLOT(accept()) ); 00091 } 00092 00093 SelectDialog::~SelectDialog() 00094 { 00095 delete d; 00096 } 00097 00098 Resource *SelectDialog::resource() 00099 { 00100 if ( d->mResourceId->currentRow() != -1 ) { 00101 return d->mResourceMap[ d->mResourceId->currentRow() ]; 00102 } else { 00103 return 0; 00104 } 00105 } 00106 00107 Resource *SelectDialog::getResource( QList<Resource *> list, QWidget *parent ) 00108 { 00109 if ( list.count() == 0 ) { 00110 KMessageBox::error( parent, i18n( "There is no resource available." ) ); 00111 return 0; 00112 } 00113 00114 if ( list.count() == 1 ) { 00115 return list.first(); 00116 } 00117 00118 // the following lines will return a writeable resource if only _one_ 00119 // writeable resource exists 00120 Resource *found = 0; 00121 00122 for ( int i=0; i< list.size(); ++i ) { 00123 if ( !list.at(i)->readOnly() ) { 00124 if ( found ) { 00125 found = 0; 00126 break; 00127 } 00128 } else { 00129 found = list.at(i); 00130 } 00131 } 00132 00133 if ( found ) { 00134 return found; 00135 } 00136 00137 SelectDialog dlg( list, parent ); 00138 if ( dlg.exec() == KDialog::Accepted ) { 00139 return dlg.resource(); 00140 } else { 00141 return 0; 00142 } 00143 }