1 | /* |
2 | This file is part of libkresources. |
3 | |
4 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> |
5 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> |
6 | Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> |
7 | |
8 | This library is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Library General Public |
10 | License as published by the Free Software Foundation; either |
11 | version 2 of the License, or (at your option) any later version. |
12 | |
13 | This library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Library General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Library General Public License |
19 | along with this library; see the file COPYING.LIB. If not, write to |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. |
22 | */ |
23 | |
24 | #include "selectdialog.h" |
25 | |
26 | #include <klocalizedstring.h> |
27 | #include <kmessagebox.h> |
28 | |
29 | #include <QGroupBox> |
30 | #include <QLayout> |
31 | #include <QListWidget> |
32 | |
33 | #include "resource.h" |
34 | |
35 | using namespace KRES; |
36 | |
37 | class SelectDialog::SelectDialogPrivate |
38 | { |
39 | public: |
40 | QListWidget *mResourceId; |
41 | QMap<int, Resource*> mResourceMap; |
42 | }; |
43 | |
44 | static bool resourceNameLessThan( Resource *a, Resource *b ) |
45 | { |
46 | return a->resourceName() < b->resourceName(); |
47 | } |
48 | |
49 | SelectDialog::SelectDialog( QList<Resource *> list, QWidget *parent ) |
50 | : KDialog( parent ), d( new SelectDialogPrivate ) |
51 | { |
52 | setModal( true ); |
53 | setCaption( i18n( "Resource Selection" ) ); |
54 | resize( 300, 200 ); |
55 | setButtons( Ok|Cancel ); |
56 | setDefaultButton( Ok ); |
57 | |
58 | QWidget *widget = new QWidget( this ); |
59 | setMainWidget( widget ); |
60 | |
61 | QVBoxLayout *mainLayout = new QVBoxLayout( widget ); |
62 | mainLayout->setMargin( 0 ); |
63 | |
64 | QGroupBox *groupBox = new QGroupBox( widget ); |
65 | QGridLayout *grid = new QGridLayout; |
66 | groupBox->setLayout( grid ); |
67 | groupBox->setTitle( i18n( "Resources" ) ); |
68 | |
69 | d->mResourceId = new QListWidget( groupBox ); |
70 | grid->addWidget( d->mResourceId, 0, 0 ); |
71 | |
72 | mainLayout->addWidget( groupBox ); |
73 | |
74 | // sort resources by name |
75 | qSort( list.begin(), list.end(), resourceNameLessThan ); |
76 | |
77 | // setup listbox |
78 | uint counter = 0; |
79 | for ( int i = 0; i < list.count(); ++i ) { |
80 | Resource *resource = list.at( i ); |
81 | if ( resource && !resource->readOnly() ) { |
82 | d->mResourceMap.insert( counter, resource ); |
83 | d->mResourceId->addItem( resource->resourceName() ); |
84 | counter++; |
85 | } |
86 | } |
87 | |
88 | d->mResourceId->setCurrentRow( 0 ); |
89 | connect( d->mResourceId, SIGNAL(itemActivated(QListWidgetItem*)), |
90 | SLOT(accept()) ); |
91 | } |
92 | |
93 | SelectDialog::~SelectDialog() |
94 | { |
95 | delete d; |
96 | } |
97 | |
98 | Resource *SelectDialog::resource() |
99 | { |
100 | if ( d->mResourceId->currentRow() != -1 ) { |
101 | return d->mResourceMap[ d->mResourceId->currentRow() ]; |
102 | } else { |
103 | return 0; |
104 | } |
105 | } |
106 | |
107 | Resource *SelectDialog::getResource( QList<Resource *> list, QWidget *parent ) |
108 | { |
109 | if ( list.count() == 0 ) { |
110 | KMessageBox::error( parent, i18n( "There is no resource available." ) ); |
111 | return 0; |
112 | } |
113 | |
114 | if ( list.count() == 1 ) { |
115 | return list.first(); |
116 | } |
117 | |
118 | // the following lines will return a writeable resource if only _one_ |
119 | // writeable resource exists |
120 | Resource *found = 0; |
121 | |
122 | for ( int i=0; i< list.size(); ++i ) { |
123 | if ( !list.at( i )->readOnly() ) { |
124 | if ( found ) { |
125 | found = 0; |
126 | break; |
127 | } |
128 | } else { |
129 | found = list.at( i ); |
130 | } |
131 | } |
132 | |
133 | if ( found ) { |
134 | return found; |
135 | } |
136 | |
137 | SelectDialog dlg( list, parent ); |
138 | if ( dlg.exec() == KDialog::Accepted ) { |
139 | return dlg.resource(); |
140 | } else { |
141 | return 0; |
142 | } |
143 | } |
144 | |