33 #include <config-kleopatra.h>
37 #include "ui_listwidget.h"
41 #include <QItemSelectionModel>
42 #include <QStringListModel>
44 #include <QRegExpValidator>
45 #include <QItemDelegate>
48 using namespace Kleo::NewCertificateUi;
52 class ItemDelegate :
public QItemDelegate {
55 explicit ItemDelegate( QObject * p=0 )
56 : QItemDelegate( p ), m_rx() {}
57 explicit ItemDelegate(
const QRegExp & rx, QObject * p=0 )
58 : QItemDelegate( p ), m_rx( rx ) {}
60 void setRegExpFilter(
const QRegExp & rx ) {
63 const QRegExp & regExpFilter()
const {
return m_rx; }
65 QWidget * createEditor(
QWidget * p,
const QStyleOptionViewItem & o,
const QModelIndex & i )
const {
66 QWidget * w = QItemDelegate::createEditor( p, o, i );
67 if ( !m_rx.isEmpty() )
68 if ( QLineEdit *
const le = qobject_cast<QLineEdit*>( w ) )
69 le->setValidator(
new QRegExpValidator( m_rx, le ) );
77 class ListWidget::Private {
78 friend class ::Kleo::NewCertificateUi::ListWidget;
86 ui.listView->setModel( &stringListModel );
87 ui.listView->setItemDelegate( &delegate );
88 connect( ui.listView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
89 q, SLOT(slotSelectionChanged()) );
90 connect( &stringListModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
92 connect( &stringListModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
94 connect( &stringListModel, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
100 const int idx = stringListModel.rowCount();
101 if ( stringListModel.insertRows( idx, 1 ) ) {
102 stringListModel.setData( stringListModel.index( idx ),
defaultValue );
108 const int idx = selectedRow();
109 stringListModel.removeRows( idx, 1 );
114 const int idx = selectedRow();
115 swapRows( idx - 1, idx );
116 selectRow( idx - 1 );
120 const int idx = selectedRow();
121 swapRows( idx, idx + 1 );
122 selectRow( idx + 1 );
125 void slotSelectionChanged() {
126 enableDisableActions();
130 void editRow(
int idx ) {
131 const QModelIndex mi = stringListModel.index( idx );
134 ui.listView->setCurrentIndex( mi );
135 ui.listView->edit( mi );
138 QModelIndexList selectedIndexes()
const {
139 return ui.listView->selectionModel()->selectedRows();
141 int selectedRow()
const {
142 const QModelIndexList mil = selectedIndexes();
143 return mil.empty() ? -1 : mil.front().row() ;
145 void selectRow(
int idx ) {
146 const QModelIndex mi = stringListModel.index( idx );
148 ui.listView->selectionModel()->select( mi, QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows );
150 void swapRows(
int r1,
int r2 ) {
151 if ( r1 < 0 || r2 < 0 || r1 >= stringListModel.rowCount() || r2 >= stringListModel.rowCount() )
153 const QModelIndex m1 = stringListModel.index( r1 );
154 const QModelIndex m2 = stringListModel.index( r2 );
155 const QVariant data1 = m1.data();
156 const QVariant data2 = m2.data();
157 stringListModel.setData( m1, data2 );
158 stringListModel.setData( m2, data1 );
160 void enableDisableActions() {
161 const QModelIndexList mil = selectedIndexes();
162 ui.removeTB->setEnabled( !mil.empty() );
163 ui.upTB->setEnabled( mil.size() == 1 && mil.front().row() > 0 );
164 ui.downTB->setEnabled( mil.size() == 1 && mil.back().row() < stringListModel.rowCount() - 1 );
168 QStringListModel stringListModel;
169 ItemDelegate delegate;
171 struct UI : Ui_ListWidget {
177 addTB->setIcon( KIcon( QLatin1String(
"list-add") ) );
178 removeTB->setIcon( KIcon( QLatin1String(
"list-remove") ) );
179 upTB->setIcon( KIcon( QLatin1String(
"go-up") ) );
180 downTB->setIcon( KIcon( QLatin1String(
"go-down") ) );
187 :
QWidget( p ),
d( new Private( this ) )
195 return d->stringListModel.stringList();
199 d->stringListModel.setStringList( items );
203 return d->delegate.regExpFilter();
207 d->delegate.setRegExpFilter( rx );
211 return d->defaultValue;
215 d->defaultValue = df;
218 #include "moc_listwidget.cpp"
219 #include "listwidget.moc"