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

kalarm/lib

  • sources
  • kde-4.12
  • kdepim
  • kalarm
  • lib
lineedit.cpp
Go to the documentation of this file.
1 /*
2  * lineedit.cpp - Line edit widget with extra drag and drop options
3  * Program: kalarm
4  * Copyright © 2003-2005,2009-2010 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 #include "lineedit.moc"
23 
24 #include <libkdepim/misc/maillistdrag.h>
25 #include <kabc/vcarddrag.h>
26 #ifdef USE_AKONADI
27 #include <kcalutils/icaldrag.h>
28 #else
29 #include <kcal/icaldrag.h>
30 #endif
31 
32 #include <kurl.h>
33 #include <kurlcompletion.h>
34 #include <kshell.h>
35 
36 #include <QRegExp>
37 #include <QMimeData>
38 #include <QDragEnterEvent>
39 #include <QDropEvent>
40 #include <QFocusEvent>
41 
42 
43 /*=============================================================================
44 = Class LineEdit
45 = Line edit which accepts drag and drop of text, URLs and/or email addresses.
46 * It has an option to prevent its contents being selected when it receives
47 = focus.
48 =============================================================================*/
49 LineEdit::LineEdit(Type type, QWidget* parent)
50  : KLineEdit(parent),
51  mType(type),
52  mNoSelect(false),
53  mSetCursorAtEnd(false)
54 {
55  init();
56 }
57 
58 LineEdit::LineEdit(QWidget* parent)
59  : KLineEdit(parent),
60  mType(Text),
61  mNoSelect(false),
62  mSetCursorAtEnd(false)
63 {
64  init();
65 }
66 
67 void LineEdit::init()
68 {
69  if (mType == Url)
70  {
71  setCompletionMode(KGlobalSettings::CompletionShell);
72  KUrlCompletion* comp = new KUrlCompletion(KUrlCompletion::FileCompletion);
73  comp->setReplaceHome(true);
74  setCompletionObject(comp);
75  setAutoDeleteCompletionObject(true);
76  }
77  else
78  setCompletionMode(KGlobalSettings::CompletionNone);
79 }
80 
81 /******************************************************************************
82 * Called when the line edit receives focus.
83 * If 'noSelect' is true, prevent the contents being selected.
84 */
85 void LineEdit::focusInEvent(QFocusEvent* e)
86 {
87  QFocusEvent newe(QEvent::FocusIn, (mNoSelect ? Qt::OtherFocusReason : e->reason()));
88  KLineEdit::focusInEvent(&newe);
89  mNoSelect = false;
90 }
91 
92 QString LineEdit::text() const
93 {
94  if (mType == Url)
95  return KShell::tildeExpand(KLineEdit::text());
96  return KLineEdit::text();
97 }
98 
99 void LineEdit::setText(const QString& text)
100 {
101  KLineEdit::setText(text);
102  setCursorPosition(mSetCursorAtEnd ? text.length() : 0);
103 }
104 
105 void LineEdit::dragEnterEvent(QDragEnterEvent* e)
106 {
107  const QMimeData* data = e->mimeData();
108  bool ok;
109 #ifdef USE_AKONADI
110  if (KCalUtils::ICalDrag::canDecode(data))
111 #else
112  if (KCal::ICalDrag::canDecode(data))
113 #endif
114  ok = false; // don't accept "text/calendar" objects
115  else
116  ok = (data->hasText()
117  || KUrl::List::canDecode(data)
118  || (mType != Url && KPIM::MailList::canDecode(data))
119  || (mType == Emails && KABC::VCardDrag::canDecode(data)));
120  if (ok)
121  e->accept(rect());
122  else
123  e->ignore(rect());
124 }
125 
126 void LineEdit::dropEvent(QDropEvent* e)
127 {
128  const QMimeData* data = e->mimeData();
129  QString newText;
130  QStringList newEmails;
131  KUrl::List files;
132  KABC::Addressee::List addrList;
133 
134  if (mType != Url
135  && KPIM::MailList::canDecode(data))
136  {
137  KPIM::MailList mailList = KPIM::MailList::fromMimeData(data);
138  // KMail message(s) - ignore all but the first
139  if (mailList.count())
140  {
141  if (mType == Emails)
142  newText = mailList.first().from();
143  else
144  setText(mailList.first().subject()); // replace any existing text
145  }
146  }
147  // This must come before KUrl
148  else if (mType == Emails
149  && KABC::VCardDrag::canDecode(data) && KABC::VCardDrag::fromMimeData(data, addrList))
150  {
151  // KAddressBook entries
152  for (KABC::Addressee::List::Iterator it = addrList.begin(); it != addrList.end(); ++it)
153  {
154  QString em((*it).fullEmail());
155  if (!em.isEmpty())
156  newEmails.append(em);
157  }
158  }
159  else if (!(files = KUrl::List::fromMimeData(data)).isEmpty())
160  {
161  // URL(s)
162  switch (mType)
163  {
164  case Url:
165  // URL entry field - ignore all but the first dropped URL
166  setText(files.first().prettyUrl()); // replace any existing text
167  break;
168  case Emails:
169  {
170  // Email entry field - ignore all but mailto: URLs
171  QString mailto = QLatin1String("mailto");
172  for (KUrl::List::Iterator it = files.begin(); it != files.end(); ++it)
173  {
174  if ((*it).protocol() == mailto)
175  newEmails.append((*it).path());
176  }
177  break;
178  }
179  case Text:
180  newText = files.first().prettyUrl();
181  break;
182  }
183  }
184  else if (data->hasText())
185  {
186  // Plain text
187  QString txt = data->text();
188  if (mType == Emails)
189  {
190  // Remove newlines from a list of email addresses, and allow an eventual mailto: protocol
191  QString mailto = QLatin1String("mailto:");
192  newEmails = txt.split(QRegExp(QLatin1String("[\r\n]+")), QString::SkipEmptyParts);
193  for (QStringList::Iterator it = newEmails.begin(); it != newEmails.end(); ++it)
194  {
195  if ((*it).startsWith(mailto))
196  {
197  KUrl url(*it);
198  *it = url.path();
199  }
200  }
201  }
202  else
203  {
204  int newline = txt.indexOf(QLatin1Char('\n'));
205  newText = (newline >= 0) ? txt.left(newline) : txt;
206  }
207  }
208 
209  if (newEmails.count())
210  {
211  newText = newEmails.join(QLatin1String(","));
212  int c = cursorPosition();
213  if (c > 0)
214  newText.prepend(QLatin1String(","));
215  if (c < static_cast<int>(text().length()))
216  newText.append(QLatin1String(","));
217  }
218  if (!newText.isEmpty())
219  insert(newText);
220 }
221 
222 // vim: et sw=4:
LineEdit::Type
Type
Types of drag and drop content which will be accepted.
Definition: lineedit.h:64
LineEdit::setText
virtual void setText(const QString &str)
Sets the contents of the line edit to be str.
Definition: lineedit.cpp:99
text
virtual QByteArray text(quint32 serialNumber) const =0
QWidget
LineEdit::text
QString text() const
Return the entered text.
Definition: lineedit.cpp:92
KPIM::MailList::fromMimeData
static MailList fromMimeData(const QMimeData *md)
maillistdrag.h
QMimeData
canDecode
static bool canDecode(const QMimeData *md)
LineEdit::dropEvent
virtual void dropEvent(QDropEvent *)
Definition: lineedit.cpp:126
LineEdit::Url
Definition: lineedit.h:64
LineEdit::Text
Definition: lineedit.h:64
KLineEdit
LineEdit::focusInEvent
virtual void focusInEvent(QFocusEvent *)
Definition: lineedit.cpp:85
KPIM::MailList
LineEdit::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *)
Definition: lineedit.cpp:105
LineEdit::LineEdit
LineEdit(Type type, QWidget *parent=0)
Constructor.
Definition: lineedit.cpp:49
LineEdit::Emails
Definition: lineedit.h:64
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm/lib

Skip menu "kalarm/lib"
  • 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