certmanager/lib

dnattributeorderconfigwidget.cpp
1/*
2 dnattributeorderconfigwidget.cpp
3
4 This file is part of libkleopatra, the KDE keymanagement library
5 Copyright (c) 2004 Klarälvdalens Datakonsult AB
6
7 Libkleopatra is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 Libkleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the TQt library by Trolltech AS, Norway (or with modified versions
24 of TQt that use the same license as TQt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 TQt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
31 */
32
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "dnattributeorderconfigwidget.h"
38
39#include "kleo/dn.h"
40
41#include <tdelocale.h>
42#include <kdebug.h>
43#include <kdialog.h>
44#include <kiconloader.h>
45#include <tdeconfig.h>
46#include <tdeapplication.h>
47
48#include <tqtoolbutton.h>
49#include <tqlayout.h>
50#include <tqheader.h>
51#include <tqlabel.h>
52#include <tqlistview.h>
53#include <tqtooltip.h>
54
55#include <assert.h>
56
57struct Kleo::DNAttributeOrderConfigWidget::Private {
58 enum { UUp=0, Up=1, Left=2, Right=3, Down=4, DDown=5 };
59
60 TQListView * availableLV;
61 TQListView * currentLV;
62 TQToolButton * navTB[6];
63
64 TQListViewItem * placeHolderItem;
65
67};
68
69static void prepare( TQListView * lv ) {
70 lv->setAllColumnsShowFocus( true );
71 lv->setResizeMode( TQListView::LastColumn );
72 lv->header()->setClickEnabled( false );
73 lv->addColumn( TQString() );
74 lv->addColumn( i18n("Description") );
75}
76
77Kleo::DNAttributeOrderConfigWidget::DNAttributeOrderConfigWidget( DNAttributeMapper * mapper, TQWidget * parent, const char * name, WFlags f )
78 : TQWidget( parent, name, f ), d( 0 )
79{
80 assert( mapper );
81 d = new Private();
82 d->mapper = mapper;
83
84 TQGridLayout * glay = new TQGridLayout( this, 2, 3, 0, KDialog::spacingHint() );
85 glay->setColStretch( 0, 1 );
86 glay->setColStretch( 2, 1 );
87
88 int row = -1;
89
90 ++row;
91 glay->addWidget( new TQLabel( i18n("Available attributes:"), this ), row, 0 );
92 glay->addWidget( new TQLabel( i18n("Current attribute order:"), this ), row, 2 );
93
94
95 ++row;
96 glay->setRowStretch( row, 1 );
97
98 d->availableLV = new TQListView( this );
99 prepare( d->availableLV );
100 d->availableLV->setSorting( 0 );
101 glay->addWidget( d->availableLV, row, 0 );
102
103 d->currentLV = new TQListView( this );
104 prepare( d->currentLV );
105 d->currentLV->setSorting( -1 );
106 glay->addWidget( d->currentLV, row, 2 );
107
108 connect( d->availableLV, TQ_SIGNAL(clicked( TQListViewItem * )),
109 TQ_SLOT(slotAvailableSelectionChanged(TQListViewItem*)) );
110 connect( d->currentLV, TQ_SIGNAL(clicked(TQListViewItem*)),
111 TQ_SLOT(slotCurrentOrderSelectionChanged(TQListViewItem*)) );
112
113 d->placeHolderItem = new TQListViewItem( d->availableLV, "_X_", i18n("All others") );
114
115 // the up/down/left/right arrow cross:
116
117 TQGridLayout * xlay = new TQGridLayout( 5, 3, 0, "xlay" );
118 xlay->setAlignment( AlignCenter );
119
120 static const struct {
121 const char * icon;
122 int row, col;
123 const char * tooltip;
124 const char * slot;
125 } navButtons[] = {
126 { "2uparrow", 0, 1, I18N_NOOP( "Move to top" ), TQ_SLOT(slotDoubleUpButtonClicked()) },
127 { "1uparrow", 1, 1, I18N_NOOP( "Move one up" ), TQ_SLOT(slotUpButtonClicked()) },
128 { "1leftarrow", 2, 0, I18N_NOOP( "Remove from current attribute order" ), TQ_SLOT(slotLeftButtonClicked()) },
129 { "1rightarrow", 2, 2, I18N_NOOP( "Add to current attribute order" ), TQ_SLOT(slotRightButtonClicked()) },
130 { "1downarrow", 3, 1, I18N_NOOP( "Move one down" ), TQ_SLOT(slotDownButtonClicked()) },
131 { "2downarrow", 4, 1, I18N_NOOP( "Move to bottom" ), TQ_SLOT(slotDoubleDownButtonClicked()) }
132 };
133
134 for ( unsigned int i = 0 ; i < sizeof navButtons / sizeof *navButtons ; ++i ) {
135 TQToolButton * tb = d->navTB[i] = new TQToolButton( this );
136 tb->setIconSet( SmallIconSet( navButtons[i].icon ) );
137 tb->setEnabled( false );
138 TQToolTip::add( tb, i18n( navButtons[i].tooltip ) );
139 xlay->addWidget( tb, navButtons[i].row, navButtons[i].col );
140 connect( tb, TQ_SIGNAL(clicked()), navButtons[i].slot );
141 }
142
143 glay->addLayout( xlay, row, 1 );
144}
145
146Kleo::DNAttributeOrderConfigWidget::~DNAttributeOrderConfigWidget() {
147 delete d; d = 0;
148}
149
150void Kleo::DNAttributeOrderConfigWidget::load() {
151 // save the _X_ item:
152 takePlaceHolderItem();
153 // clear the rest:
154 d->availableLV->clear();
155 d->currentLV->clear();
156
157 const TQStringList order = d->mapper->attributeOrder();
158
159 // fill the RHS listview:
160 TQListViewItem * last = 0;
161 for ( TQStringList::const_iterator it = order.begin() ; it != order.end() ; ++it ) {
162 const TQString attr = (*it).upper();
163 if ( attr == "_X_" ) {
164 takePlaceHolderItem();
165 d->currentLV->insertItem( d->placeHolderItem );
166 d->placeHolderItem->moveItem( last );
167 last = d->placeHolderItem;
168 } else
169 last = new TQListViewItem( d->currentLV, last, attr, d->mapper->name2label( attr ) );
170 }
171
172 // fill the LHS listview with what's left:
173
174 const TQStringList all = Kleo::DNAttributeMapper::instance()->names();
175 for ( TQStringList::const_iterator it = all.begin() ; it != all.end() ; ++it )
176 if ( order.find( *it ) == order.end() )
177 (void)new TQListViewItem( d->availableLV, *it, d->mapper->name2label( *it ) );
178
179 if ( !d->placeHolderItem->listView() )
180 d->availableLV->insertItem( d->placeHolderItem );
181}
182
183void Kleo::DNAttributeOrderConfigWidget::takePlaceHolderItem() {
184 if ( TQListView * lv = d->placeHolderItem->listView() )
185 lv->takeItem( d->placeHolderItem );
186}
187
188void Kleo::DNAttributeOrderConfigWidget::save() const {
189 TQStringList order;
190 for ( TQListViewItemIterator it( d->currentLV ) ; it.current() ; ++it )
191 order.push_back( it.current()->text( 0 ) );
192
193 d->mapper->setAttributeOrder( order );
194}
195
196void Kleo::DNAttributeOrderConfigWidget::defaults() {
197 kdDebug() << "Sorry, not implemented: Kleo::DNAttributeOrderConfigWidget::defaults()" << endl;
198}
199
200
201
202void Kleo::DNAttributeOrderConfigWidget::slotAvailableSelectionChanged( TQListViewItem * item ) {
203 d->navTB[Private::Right]->setEnabled( item );
204}
205
206void Kleo::DNAttributeOrderConfigWidget::slotCurrentOrderSelectionChanged( TQListViewItem * item ) {
207 enableDisableButtons( item );
208}
209
210void Kleo::DNAttributeOrderConfigWidget::enableDisableButtons( TQListViewItem * item ) {
211 d->navTB[Private::UUp ]->setEnabled( item && item->itemAbove() );
212 d->navTB[Private::Up ]->setEnabled( item && item->itemAbove() );
213 d->navTB[Private::Left ]->setEnabled( item );
214 d->navTB[Private::Down ]->setEnabled( item && item->itemBelow() );
215 d->navTB[Private::DDown]->setEnabled( item && item->itemBelow() );
216}
217
218void Kleo::DNAttributeOrderConfigWidget::slotUpButtonClicked() {
219 TQListViewItem * item = d->currentLV->selectedItem();
220 if ( !item )
221 return;
222 TQListViewItem * above = item->itemAbove();
223 if ( !above )
224 return;
225 above->moveItem( item ); // moves "above" to after "item", ie. "item" one up
226 enableDisableButtons( item );
227 emit changed();
228}
229
230void Kleo::DNAttributeOrderConfigWidget::slotDoubleUpButtonClicked() {
231 TQListViewItem * item = d->currentLV->selectedItem();
232 if ( !item )
233 return;
234 if ( item == d->currentLV->firstChild() )
235 return;
236 d->currentLV->takeItem( item );
237 d->currentLV->insertItem( item );
238 d->currentLV->setSelected( item, true );
239 enableDisableButtons( item );
240 emit changed();
241}
242
243void Kleo::DNAttributeOrderConfigWidget::slotDownButtonClicked() {
244 TQListViewItem * item = d->currentLV->selectedItem();
245 if ( !item )
246 return;
247 TQListViewItem * below = item->itemBelow();
248 if ( !below )
249 return;
250 item->moveItem( below ); // moves "item" to after "below", ie. "item" one down
251 enableDisableButtons( item );
252 emit changed();
253}
254
255void Kleo::DNAttributeOrderConfigWidget::slotDoubleDownButtonClicked() {
256 TQListViewItem * item = d->currentLV->selectedItem();
257 if ( !item )
258 return;
259 TQListViewItem * last = d->currentLV->lastItem();
260 assert( last );
261 if ( item == last )
262 return;
263 item->moveItem( last ); // moves "item" to after "last", ie. to the bottom
264 enableDisableButtons( item );
265 emit changed();
266}
267
268void Kleo::DNAttributeOrderConfigWidget::slotLeftButtonClicked() {
269 TQListViewItem * right = d->currentLV->selectedItem();
270 if ( !right )
271 return;
272 TQListViewItem * next = right->itemBelow();
273 if ( !next )
274 next = right->itemAbove();
275 d->currentLV->takeItem( right );
276 d->availableLV->insertItem( right );
277 if ( next )
278 d->currentLV->setSelected( next, true );
279 enableDisableButtons( next );
280 emit changed();
281}
282
283void Kleo::DNAttributeOrderConfigWidget::slotRightButtonClicked() {
284 TQListViewItem * left = d->availableLV->selectedItem();
285 if ( !left )
286 return;
287 TQListViewItem * next = left->itemBelow();
288 if ( !next )
289 next = left->itemAbove();
290 d->availableLV->takeItem( left );
291 d->currentLV->insertItem( left );
292 if ( TQListViewItem * right = d->currentLV->selectedItem() ) {
293 if ( TQListViewItem * above = right->itemAbove() )
294 left->moveItem( above ); // move new item immediately before old selected
295 d->currentLV->setSelected( right, false );
296 }
297 d->currentLV->setSelected( left, true );
298 enableDisableButtons( left );
299 d->navTB[Private::Right]->setEnabled( next );
300 if ( next )
301 d->availableLV->setSelected( next, true );
302 emit changed();
303}
304
305
306
307void Kleo::DNAttributeOrderConfigWidget::virtual_hook( int, void* ) {}
308
309#include "dnattributeorderconfigwidget.moc"
DN Attribute mapper.
Definition: dn.h:52