• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • newcertificatewizard
listwidget.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  newcertificatewizard/listwidget.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra 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 Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), 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  Qt. 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 #include <config-kleopatra.h>
34 
35 #include "listwidget.h"
36 
37 #include "ui_listwidget.h"
38 
39 #include <KIcon>
40 
41 #include <QItemSelectionModel>
42 #include <QStringListModel>
43 #include <QRegExp>
44 #include <QRegExpValidator>
45 #include <QItemDelegate>
46 #include <QLineEdit>
47 
48 using namespace Kleo::NewCertificateUi;
49 
50 namespace {
51 
52  class ItemDelegate : public QItemDelegate {
53  Q_OBJECT
54  public:
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 ) {}
59 
60  void setRegExpFilter( const QRegExp & rx ) {
61  m_rx = rx;
62  }
63  const QRegExp & regExpFilter() const { return m_rx; }
64 
65  /* reimp */ 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 ) );
70  return w;
71  }
72  private:
73  QRegExp m_rx;
74  };
75 }
76 
77 class ListWidget::Private {
78  friend class ::Kleo::NewCertificateUi::ListWidget;
79  ListWidget * const q;
80 public:
81  explicit Private( ListWidget * qq )
82  : q( qq ),
83  stringListModel(),
84  ui( q )
85  {
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)),
91  q, SIGNAL(itemsChanged()) );
92  connect( &stringListModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
93  q, SIGNAL(itemsChanged()) );
94  connect( &stringListModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
95  q, SIGNAL(itemsChanged()) );
96  }
97 
98 private:
99  void slotAdd() {
100  const int idx = stringListModel.rowCount();
101  if ( stringListModel.insertRows( idx, 1 ) ) {
102  stringListModel.setData( stringListModel.index( idx ), defaultValue );
103  editRow( idx );
104  }
105  }
106 
107  void slotRemove() {
108  const int idx = selectedRow();
109  stringListModel.removeRows( idx, 1 );
110  selectRow( idx );
111  }
112 
113  void slotUp() {
114  const int idx = selectedRow();
115  swapRows( idx - 1, idx );
116  selectRow( idx - 1 );
117  }
118 
119  void slotDown() {
120  const int idx = selectedRow();
121  swapRows( idx, idx + 1 );
122  selectRow( idx + 1 );
123  }
124 
125  void slotSelectionChanged() {
126  enableDisableActions();
127  }
128 
129 private:
130  void editRow( int idx ) {
131  const QModelIndex mi = stringListModel.index( idx );
132  if ( !mi.isValid() )
133  return;
134  ui.listView->setCurrentIndex( mi );
135  ui.listView->edit( mi );
136  }
137 
138  QModelIndexList selectedIndexes() const {
139  return ui.listView->selectionModel()->selectedRows();
140  }
141  int selectedRow() const {
142  const QModelIndexList mil = selectedIndexes();
143  return mil.empty() ? -1 : mil.front().row() ;
144  }
145  void selectRow( int idx ) {
146  const QModelIndex mi = stringListModel.index( idx );
147  if ( mi.isValid() )
148  ui.listView->selectionModel()->select( mi, QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows );
149  }
150  void swapRows( int r1, int r2 ) {
151  if ( r1 < 0 || r2 < 0 || r1 >= stringListModel.rowCount() || r2 >= stringListModel.rowCount() )
152  return;
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 );
159  }
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 );
165  }
166 
167 private:
168  QStringListModel stringListModel;
169  ItemDelegate delegate;
170  QString defaultValue;
171  struct UI : Ui_ListWidget {
172  explicit UI( ListWidget * q )
173  : Ui_ListWidget()
174  {
175  setupUi( q );
176 
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") ) );
181  }
182  } ui;
183 
184 };
185 
186 ListWidget::ListWidget( QWidget * p )
187  : QWidget( p ), d( new Private( this ) )
188 {
189 
190 }
191 
192 ListWidget::~ListWidget() {}
193 
194 QStringList ListWidget::items() const {
195  return d->stringListModel.stringList();
196 }
197 
198 void ListWidget::setItems( const QStringList & items ) {
199  d->stringListModel.setStringList( items );
200 }
201 
202 QRegExp ListWidget::regExpFilter() const {
203  return d->delegate.regExpFilter();
204 }
205 
206 void ListWidget::setRegExpFilter( const QRegExp & rx ) {
207  d->delegate.setRegExpFilter( rx );
208 }
209 
210 QString ListWidget::defaultValue() const {
211  return d->defaultValue;
212 }
213 
214 void ListWidget::setDefaultValue( const QString & df ) {
215  d->defaultValue = df;
216 }
217 
218 #include "moc_listwidget.cpp"
219 #include "listwidget.moc"
Kleo::NewCertificateUi::ListWidget::itemsChanged
void itemsChanged()
QWidget
Kleo::NewCertificateUi::ListWidget::~ListWidget
~ListWidget()
Definition: listwidget.cpp:192
Kleo::NewCertificateUi::ListWidget::setItems
void setItems(const QStringList &items)
Definition: listwidget.cpp:198
Kleo::NewCertificateUi::ListWidget::items
QStringList items() const
Kleo::NewCertificateUi::ListWidget::regExpFilter
QRegExp regExpFilter() const
Kleo::NewCertificateUi::ListWidget::setRegExpFilter
void setRegExpFilter(const QRegExp &rx)
Definition: listwidget.cpp:206
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::NewCertificateUi::ListWidget::ListWidget
ListWidget(QWidget *parent=0)
Definition: listwidget.cpp:186
listwidget.h
Kleo::NewCertificateUi::ListWidget::setDefaultValue
void setDefaultValue(const QString &defaultValue)
Definition: listwidget.cpp:214
Kleo::NewCertificateUi::ListWidget::defaultValue
QString defaultValue() const
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::NewCertificateUi::ListWidget
Definition: listwidget.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal