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

libkdepim

  • sources
  • kde-4.14
  • kdepim
  • libkdepim
  • addressline
  • recentaddress
recentaddresswidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2015 Montel Laurent <montel@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 
19 */
20 
21 #include "recentaddresswidget.h"
22 #include "recentaddresses.h"
23 #include <kpimutils/email.h>
24 
25 #include <KConfig>
26 #include <KConfigGroup>
27 #include <KDebug>
28 #include <KGlobal>
29 #include <KLocale>
30 #include <KLineEdit>
31 #include <KPushButton>
32 #include <KMessageBox>
33 
34 #include <QCoreApplication>
35 #include <QLayout>
36 #include <QVBoxLayout>
37 #include <QListWidget>
38 #include <QKeyEvent>
39 
40 
41 using namespace KPIM;
42 RecentAddressWidget::RecentAddressWidget(QWidget *parent)
43  : QWidget(parent)
44 {
45  QVBoxLayout *layout = new QVBoxLayout( this );
46 
47  mLineEdit = new KLineEdit(this);
48  mLineEdit->setObjectName(QLatin1String("line_edit"));
49  layout->addWidget(mLineEdit);
50 
51  mLineEdit->setTrapReturnKey(true);
52  mLineEdit->installEventFilter(this);
53 
54  connect(mLineEdit,SIGNAL(textChanged(QString)),SLOT(slotTypedSomething(QString)));
55  connect(mLineEdit,SIGNAL(returnPressed()),SLOT(slotAddItem()));
56 
57 
58  QHBoxLayout* hboxLayout = new QHBoxLayout;
59 
60  QVBoxLayout* btnsLayout = new QVBoxLayout;
61  btnsLayout->addStretch();
62  mNewButton = new KPushButton(KIcon(QLatin1String("list-add")), i18n("&Add"), this);
63  mNewButton->setObjectName(QLatin1String("new_button"));
64  connect(mNewButton, SIGNAL(clicked()), SLOT(slotAddItem()));
65  btnsLayout->insertWidget(0 ,mNewButton);
66 
67  mRemoveButton = new KPushButton(KIcon(QLatin1String("list-remove")), i18n("&Remove"), this);
68  mRemoveButton->setObjectName(QLatin1String("remove_button"));
69  mRemoveButton->setEnabled(false);
70  connect(mRemoveButton, SIGNAL(clicked()), SLOT(slotRemoveItem()));
71  btnsLayout->insertWidget(1, mRemoveButton);
72 
73 
74  mListView = new QListWidget(this);
75  mListView->setObjectName(QLatin1String("list_view"));
76  mListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
77  mListView->setSortingEnabled(true);
78  hboxLayout->addWidget(mListView);
79  hboxLayout->addLayout(btnsLayout);
80  layout->addLayout(hboxLayout);
81  connect(mListView, SIGNAL(itemSelectionChanged()),
82  SLOT(slotSelectionChanged()));
83  // maybe supplied lineedit has some text already
84  slotTypedSomething( mLineEdit->text() );
85 }
86 
87 RecentAddressWidget::~RecentAddressWidget()
88 {
89 
90 }
91 
92 void RecentAddressWidget::slotTypedSomething(const QString& text)
93 {
94  if (mListView->currentItem()) {
95  if (mListView->currentItem()->text() != mLineEdit->text() && !mLineEdit->text().isEmpty()) {
96  // IMHO changeItem() shouldn't do anything with the value
97  // of currentItem() ... like changing it or emitting signals ...
98  // but TT disagree with me on this one (it's been that way since ages ... grrr)
99  bool block = mListView->signalsBlocked();
100  mListView->blockSignals( true );
101  QListWidgetItem *currentIndex = mListView->currentItem();
102  if ( currentIndex ) {
103  currentIndex->setText(text);
104  mDirty = true;
105  }
106  mListView->blockSignals( block );
107  }
108  }
109 }
110 
111 void RecentAddressWidget::slotAddItem()
112 {
113  if (mListView->count() > 0) {
114  const QString text = mListView->item(0)->text();
115  if (text.isEmpty()) {
116  return;
117  }
118  }
119  mListView->blockSignals(true);
120  mListView->insertItem(0, QString());
121  mListView->blockSignals(false);
122  mListView->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
123  mLineEdit->setFocus();
124  mDirty = true;
125  updateButtonState();
126 }
127 
128 void RecentAddressWidget::slotRemoveItem()
129 {
130  QList<QListWidgetItem *> selectedItems = mListView->selectedItems();
131  if (selectedItems.isEmpty())
132  return;
133  if (KMessageBox::Yes == KMessageBox::questionYesNo(this, i18np("Do you want to remove this email?", "Do you want to remove %1 emails?", selectedItems.count()), i18n("Remove"))) {
134  Q_FOREACH(QListWidgetItem *item, selectedItems) {
135  delete mListView->takeItem(mListView->row(item));
136  }
137  mDirty = true;
138  updateButtonState();
139  }
140 }
141 
142 void RecentAddressWidget::updateButtonState()
143 {
144  QList<QListWidgetItem *> selectedItems = mListView->selectedItems();
145  const int numberOfElementSelected(selectedItems.count());
146  mRemoveButton->setEnabled(numberOfElementSelected);
147  mNewButton->setEnabled(numberOfElementSelected <= 1);
148  mLineEdit->setEnabled(numberOfElementSelected <= 1);
149 
150  if (numberOfElementSelected == 1) {
151  const QString text = mListView->currentItem()->text();
152  if (text != mLineEdit->text())
153  mLineEdit->setText(text);
154  } else {
155  mLineEdit->clear();
156  }
157 }
158 
159 void RecentAddressWidget::slotSelectionChanged()
160 {
161  updateButtonState();
162 }
163 
164 void RecentAddressWidget::setAddresses( const QStringList &addrs )
165 {
166  mListView->clear();
167  mListView->addItems( addrs );
168 }
169 
170 bool RecentAddressWidget::eventFilter( QObject* o, QEvent* e )
171 {
172  if (o == mLineEdit && e->type() == QEvent::KeyPress ) {
173  QKeyEvent* keyEvent = (QKeyEvent*)e;
174  if (keyEvent->key() == Qt::Key_Down ||
175  keyEvent->key() == Qt::Key_Up) {
176  return ((QObject*)mListView)->event(e);
177  }
178  }
179 
180  return false;
181 }
182 
183 void RecentAddressWidget::storeAddresses(KConfig *config)
184 {
185  const int numberOfItem(mListView->count());
186  for (int i = 0; i < numberOfItem; ++i) {
187  KPIM::RecentAddresses::self( config )->add( mListView->item(i)->text() );
188  }
189 }
190 
191 bool RecentAddressWidget::wasChanged() const
192 {
193  return mDirty;
194 }
QWidget::layout
QLayout * layout() const
KPIM::RecentAddresses::add
void add(const QString &entry)
Adds an entry to the list.
Definition: recentaddresses.cpp:110
QEvent
QWidget
QEvent::type
Type type() const
QListWidget::currentItem
QListWidgetItem * currentItem() const
QListWidget::setSortingEnabled
void setSortingEnabled(bool enable)
KPIM::RecentAddressWidget::updateButtonState
void updateButtonState()
Definition: recentaddresswidget.cpp:142
QAbstractItemView::setSelectionMode
void setSelectionMode(QAbstractItemView::SelectionMode mode)
KPIM::RecentAddressWidget::RecentAddressWidget
RecentAddressWidget(QWidget *parent=0)
Definition: recentaddresswidget.cpp:42
KPIM::RecentAddresses::self
static RecentAddresses * self(KConfig *config=0)
Definition: recentaddresses.cpp:51
KPIM::RecentAddressWidget::setAddresses
void setAddresses(const QStringList &addrs)
Definition: recentaddresswidget.cpp:164
recentaddresswidget.h
QListWidgetItem
QHBoxLayout
KPIM::RecentAddressWidget::storeAddresses
void storeAddresses(KConfig *config)
Definition: recentaddresswidget.cpp:183
QListWidget::insertItem
void insertItem(int row, QListWidgetItem *item)
QListWidget
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
QListWidget::clear
void clear()
QObject
QListWidget::takeItem
QListWidgetItem * takeItem(int row)
QList::isEmpty
bool isEmpty() const
QObject::setObjectName
void setObjectName(const QString &name)
QString::isEmpty
bool isEmpty() const
QVBoxLayout
QString
QList
QListWidget::item
QListWidgetItem * item(int row) const
QListWidget::count
count
QListWidget::setCurrentRow
void setCurrentRow(int row)
QStringList
QObject::signalsBlocked
bool signalsBlocked() const
QKeyEvent::key
int key() const
QObject::blockSignals
bool blockSignals(bool block)
QListWidget::selectedItems
QList< QListWidgetItem * > selectedItems() const
QListWidget::row
int row(const QListWidgetItem *item) const
QKeyEvent
KLineEdit
QListWidget::addItems
void addItems(const QStringList &labels)
QLatin1String
QBoxLayout::addStretch
void addStretch(int stretch)
KPIM::RecentAddressWidget::~RecentAddressWidget
~RecentAddressWidget()
Definition: recentaddresswidget.cpp:87
KPIM::RecentAddressWidget::eventFilter
bool eventFilter(QObject *o, QEvent *e)
Definition: recentaddresswidget.cpp:170
QBoxLayout::insertWidget
void insertWidget(int index, QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
KPIM::RecentAddressWidget::wasChanged
bool wasChanged() const
Definition: recentaddresswidget.cpp:191
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidget::event
virtual bool event(QEvent *event)
QListWidgetItem::text
QString text() const
QListWidgetItem::setText
void setText(const QString &text)
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
recentaddresses.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

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

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
  • pimprint

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