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

kalarm/lib

lineedit.cpp

Go to the documentation of this file.
00001 /*
00002  *  lineedit.cpp  -  Line edit widget with extra drag and drop options
00003  *  Program:  kalarm
00004  *  Copyright © 2003-2005 by David Jarvie <djarvie@kde.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 
00023 #include <QRegExp>
00024 #include <QMimeData>
00025 #include <QDragEnterEvent>
00026 #include <QDropEvent>
00027 #include <QFocusEvent>
00028 
00029 #include <kurl.h>
00030 #include <kurlcompletion.h>
00031 
00032 #include <libkdepim/maillistdrag.h>
00033 #include <libkdepim/kvcarddrag.h>
00034 #include <kcal/icaldrag.h>
00035 
00036 #include "lineedit.moc"
00037 
00038 
00039 /*=============================================================================
00040 = Class LineEdit
00041 = Line edit which accepts drag and drop of text, URLs and/or email addresses.
00042 * It has an option to prevent its contents being selected when it receives
00043 = focus.
00044 =============================================================================*/
00045 LineEdit::LineEdit(Type type, QWidget* parent)
00046     : KLineEdit(parent),
00047       mType(type),
00048       mNoSelect(false),
00049       mSetCursorAtEnd(false)
00050 {
00051     init();
00052 }
00053 
00054 LineEdit::LineEdit(QWidget* parent)
00055     : KLineEdit(parent),
00056       mType(Text),
00057       mNoSelect(false),
00058       mSetCursorAtEnd(false)
00059 {
00060     init();
00061 }
00062 
00063 void LineEdit::init()
00064 {
00065     if (mType == Url)
00066     {
00067         setCompletionMode(KGlobalSettings::CompletionShell);
00068         KUrlCompletion* comp = new KUrlCompletion(KUrlCompletion::FileCompletion);
00069         comp->setReplaceHome(true);
00070         setCompletionObject(comp);
00071         setAutoDeleteCompletionObject(true);
00072     }
00073     else
00074         setCompletionMode(KGlobalSettings::CompletionNone);
00075 }
00076 
00077 /******************************************************************************
00078 *  Called when the line edit receives focus.
00079 *  If 'noSelect' is true, prevent the contents being selected.
00080 */
00081 void LineEdit::focusInEvent(QFocusEvent* e)
00082 {
00083     QFocusEvent newe(QEvent::FocusIn, (mNoSelect ? Qt::OtherFocusReason : e->reason()));
00084     KLineEdit::focusInEvent(&newe);
00085     mNoSelect = false;
00086 }
00087 
00088 void LineEdit::setText(const QString& text)
00089 {
00090     KLineEdit::setText(text);
00091     setCursorPosition(mSetCursorAtEnd ? text.length() : 0);
00092 }
00093 
00094 void LineEdit::dragEnterEvent(QDragEnterEvent* e)
00095 {
00096     const QMimeData* data = e->mimeData();
00097     bool ok;
00098     if (KCal::ICalDrag::canDecode(data))
00099         ok = false;   // don't accept "text/calendar" objects
00100     else
00101         ok = (data->hasText()
00102            || KUrl::List::canDecode(data)
00103            || (mType != Url && KPIM::MailList::canDecode(data))
00104            || (mType == Emails && KPIM::KVCardDrag::canDecode(data)));
00105     if (ok)
00106         e->accept(rect());
00107     else
00108         e->ignore(rect());
00109 }
00110 
00111 void LineEdit::dropEvent(QDropEvent* e)
00112 {
00113     const QMimeData* data = e->mimeData();
00114     QString               newText;
00115     QStringList           newEmails;
00116     KUrl::List            files;
00117     KABC::Addressee::List addrList;
00118 
00119     if (mType != Url
00120     &&  KPIM::MailList::canDecode(data))
00121     {
00122         KPIM::MailList mailList = KPIM::MailList::fromMimeData(data);
00123         // KMail message(s) - ignore all but the first
00124         if (mailList.count())
00125         {
00126             if (mType == Emails)
00127                 newText = mailList.first().from();
00128             else
00129                 setText(mailList.first().subject());    // replace any existing text
00130         }
00131     }
00132     // This must come before KUrl
00133     else if (mType == Emails
00134     &&  KPIM::KVCardDrag::canDecode(data)  &&  KPIM::KVCardDrag::fromMimeData(data, addrList))
00135     {
00136         // KAddressBook entries
00137         for (KABC::Addressee::List::Iterator it = addrList.begin();  it != addrList.end();  ++it)
00138         {
00139             QString em((*it).fullEmail());
00140             if (!em.isEmpty())
00141                 newEmails.append(em);
00142         }
00143     }
00144     else if (!(files = KUrl::List::fromMimeData(data)).isEmpty())
00145     {
00146         // URL(s)
00147         switch (mType)
00148         {
00149             case Url:
00150                 // URL entry field - ignore all but the first dropped URL
00151                 setText(files.first().prettyUrl());    // replace any existing text
00152                 break;
00153             case Emails:
00154             {
00155                 // Email entry field - ignore all but mailto: URLs
00156                 QString mailto = QLatin1String("mailto");
00157                 for (KUrl::List::Iterator it = files.begin();  it != files.end();  ++it)
00158                 {
00159                     if ((*it).protocol() == mailto)
00160                         newEmails.append((*it).path());
00161                 }
00162                 break;
00163             }
00164             case Text:
00165                 newText = files.first().prettyUrl();
00166                 break;
00167         }
00168     }
00169     else if (data->hasText())
00170     {
00171         // Plain text
00172         QString txt = data->text();
00173         if (mType == Emails)
00174         {
00175             // Remove newlines from a list of email addresses, and allow an eventual mailto: protocol
00176             QString mailto = QLatin1String("mailto:");
00177             newEmails = txt.split(QRegExp("[\r\n]+"), QString::SkipEmptyParts);
00178             for (QStringList::Iterator it = newEmails.begin();  it != newEmails.end();  ++it)
00179             {
00180                 if ((*it).startsWith(mailto))
00181                 {
00182                     KUrl url(*it);
00183                     *it = url.path();
00184                 }
00185             }
00186         }
00187         else
00188         {
00189             int newline = txt.indexOf(QLatin1Char('\n'));
00190             newText = (newline >= 0) ? txt.left(newline) : txt;
00191         }
00192     }
00193 
00194     if (newEmails.count())
00195     {
00196         newText = newEmails.join(",");
00197         int c = cursorPosition();
00198         if (c > 0)
00199             newText.prepend(",");
00200         if (c < static_cast<int>(text().length()))
00201             newText.append(",");
00202     }
00203     if (!newText.isEmpty())
00204         insert(newText);
00205 }

kalarm/lib

Skip menu "kalarm/lib"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal