• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 SelectDialog::SelectDialog( QList<Resource *> list, QWidget *parent )
00045   : KDialog( parent ), d( new SelectDialogPrivate )
00046 {
00047   setModal(true);
00048   setCaption( i18n( "Resource Selection" ) );
00049   resize( 300, 200 );
00050   setButtons( Ok|Cancel );
00051   setDefaultButton( Ok );
00052 
00053   QWidget *widget = new QWidget( this );
00054   setMainWidget( widget );
00055 
00056   QVBoxLayout *mainLayout = new QVBoxLayout( widget );
00057   mainLayout->setMargin( 0 );
00058   mainLayout->setSpacing( spacingHint() );
00059 
00060   QGroupBox *groupBox = new QGroupBox( widget );
00061   QGridLayout *grid = new QGridLayout;
00062   grid->setMargin( marginHint() );
00063   grid->setSpacing( spacingHint() );
00064   groupBox->setLayout( grid );
00065   groupBox->setTitle( i18n( "Resources" ) );
00066 
00067   d->mResourceId = new QListWidget( groupBox );
00068   grid->addWidget( d->mResourceId, 0, 0 );
00069 
00070   mainLayout->addWidget( groupBox );
00071 
00072   // setup listbox
00073   uint counter = 0;
00074   for ( int i = 0; i < list.count(); ++i ) {
00075     Resource *resource = list.at( i );
00076     if ( resource && !resource->readOnly() ) {
00077       d->mResourceMap.insert( counter, resource );
00078       d->mResourceId->addItem( resource->resourceName() );
00079       counter++;
00080     }
00081   }
00082 
00083   d->mResourceId->setCurrentRow( 0 );
00084   connect( d->mResourceId, SIGNAL( itemActivated(QListWidgetItem*)),
00085            SLOT(accept()) );
00086 }
00087 
00088 SelectDialog::~SelectDialog()
00089 {
00090   delete d;
00091 }
00092 
00093 Resource *SelectDialog::resource()
00094 {
00095   if ( d->mResourceId->currentRow() != -1 ) {
00096     return d->mResourceMap[ d->mResourceId->currentRow() ];
00097   } else {
00098     return 0;
00099   }
00100 }
00101 
00102 Resource *SelectDialog::getResource( QList<Resource *> list, QWidget *parent )
00103 {
00104   if ( list.count() == 0 ) {
00105     KMessageBox::error( parent, i18n( "There is no resource available." ) );
00106     return 0;
00107   }
00108 
00109   if ( list.count() == 1 ) {
00110     return list.first();
00111   }
00112 
00113   // the following lines will return a writeable resource if only _one_
00114   // writeable resource exists
00115   Resource *found = 0;
00116 
00117   for ( int i=0; i< list.size(); ++i ) {
00118     if ( !list.at(i)->readOnly() ) {
00119       if ( found ) {
00120         found = 0;
00121         break;
00122       }
00123     } else {
00124       found = list.at(i);
00125     }
00126   }
00127 
00128   if ( found ) {
00129     return found;
00130   }
00131 
00132   SelectDialog dlg( list, parent );
00133   if ( dlg.exec() == KDialog::Accepted ) {
00134     return dlg.resource();
00135   } else {
00136     return 0;
00137   }
00138 }

kresources

Skip menu "kresources"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal