kalarm/lib
lineedit.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00041
00042
00043
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
00079
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;
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
00124 if (mailList.count())
00125 {
00126 if (mType == Emails)
00127 newText = mailList.first().from();
00128 else
00129 setText(mailList.first().subject());
00130 }
00131 }
00132
00133 else if (mType == Emails
00134 && KPIM::KVCardDrag::canDecode(data) && KPIM::KVCardDrag::fromMimeData(data, addrList))
00135 {
00136
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
00147 switch (mType)
00148 {
00149 case Url:
00150
00151 setText(files.first().prettyUrl());
00152 break;
00153 case Emails:
00154 {
00155
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
00172 QString txt = data->text();
00173 if (mType == Emails)
00174 {
00175
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 }