kalarm/lib
lineedit.cpp00001
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.h>
00024 #include <qdragobject.h>
00025
00026 #include <kurldrag.h>
00027 #include <kurlcompletion.h>
00028
00029 #include <libkdepim/maillistdrag.h>
00030 #include <libkdepim/kvcarddrag.h>
00031 #include <libkcal/icaldrag.h>
00032
00033 #include "lineedit.moc"
00034
00035
00036
00037
00038
00039
00040
00041
00042 LineEdit::LineEdit(Type type, QWidget* parent, const char* name)
00043 : KLineEdit(parent, name),
00044 mType(type),
00045 mNoSelect(false),
00046 mSetCursorAtEnd(false)
00047 {
00048 init();
00049 }
00050
00051 LineEdit::LineEdit(QWidget* parent, const char* name)
00052 : KLineEdit(parent, name),
00053 mType(Text),
00054 mNoSelect(false),
00055 mSetCursorAtEnd(false)
00056 {
00057 init();
00058 }
00059
00060 void LineEdit::init()
00061 {
00062 if (mType == Url)
00063 {
00064 setCompletionMode(KGlobalSettings::CompletionShell);
00065 KURLCompletion* comp = new KURLCompletion(KURLCompletion::FileCompletion);
00066 comp->setReplaceHome(true);
00067 setCompletionObject(comp);
00068 setAutoDeleteCompletionObject(true);
00069 }
00070 else
00071 setCompletionMode(KGlobalSettings::CompletionNone);
00072 }
00073
00074
00075
00076
00077
00078 void LineEdit::focusInEvent(QFocusEvent* e)
00079 {
00080 if (mNoSelect)
00081 QFocusEvent::setReason(QFocusEvent::Other);
00082 KLineEdit::focusInEvent(e);
00083 if (mNoSelect)
00084 {
00085 QFocusEvent::resetReason();
00086 mNoSelect = false;
00087 }
00088 }
00089
00090 void LineEdit::setText(const QString& text)
00091 {
00092 KLineEdit::setText(text);
00093 setCursorPosition(mSetCursorAtEnd ? text.length() : 0);
00094 }
00095
00096 void LineEdit::dragEnterEvent(QDragEnterEvent* e)
00097 {
00098 if (KCal::ICalDrag::canDecode(e))
00099 e->accept(false);
00100 e->accept(QTextDrag::canDecode(e)
00101 || KURLDrag::canDecode(e)
00102 || mType != Url && KPIM::MailListDrag::canDecode(e)
00103 || mType == Emails && KVCardDrag::canDecode(e));
00104 }
00105
00106 void LineEdit::dropEvent(QDropEvent* e)
00107 {
00108 QString newText;
00109 QStringList newEmails;
00110 QString txt;
00111 KPIM::MailList mailList;
00112 KURL::List files;
00113 KABC::Addressee::List addrList;
00114
00115 if (mType != Url
00116 && e->provides(KPIM::MailListDrag::format())
00117 && KPIM::MailListDrag::decode(e, mailList))
00118 {
00119
00120 if (mailList.count())
00121 {
00122 if (mType == Emails)
00123 newText = mailList.first().from();
00124 else
00125 setText(mailList.first().subject());
00126 }
00127 }
00128
00129 else if (mType == Emails
00130 && KVCardDrag::canDecode(e) && KVCardDrag::decode(e, addrList))
00131 {
00132
00133 for (KABC::Addressee::List::Iterator it = addrList.begin(); it != addrList.end(); ++it)
00134 {
00135 QString em((*it).fullEmail());
00136 if (!em.isEmpty())
00137 newEmails.append(em);
00138 }
00139 }
00140 else if (KURLDrag::decode(e, files) && files.count())
00141 {
00142
00143 switch (mType)
00144 {
00145 case Url:
00146
00147 setText(files.first().prettyURL());
00148 break;
00149 case Emails:
00150 {
00151
00152 QString mailto = QString::fromLatin1("mailto");
00153 for (KURL::List::Iterator it = files.begin(); it != files.end(); ++it)
00154 {
00155 if ((*it).protocol() == mailto)
00156 newEmails.append((*it).path());
00157 }
00158 break;
00159 }
00160 case Text:
00161 newText = files.first().prettyURL();
00162 break;
00163 }
00164 }
00165 else if (QTextDrag::decode(e, txt))
00166 {
00167
00168 if (mType == Emails)
00169 {
00170
00171 QString mailto = QString::fromLatin1("mailto:");
00172 newEmails = QStringList::split(QRegExp("[\r\n]+"), txt);
00173 for (QStringList::Iterator it = newEmails.begin(); it != newEmails.end(); ++it)
00174 {
00175 if ((*it).startsWith(mailto))
00176 {
00177 KURL url(*it);
00178 *it = url.path();
00179 }
00180 }
00181 }
00182 else
00183 {
00184 int newline = txt.find('\n');
00185 newText = (newline >= 0) ? txt.left(newline) : txt;
00186 }
00187 }
00188
00189 if (newEmails.count())
00190 {
00191 newText = newEmails.join(",");
00192 int c = cursorPosition();
00193 if (c > 0)
00194 newText.prepend(",");
00195 if (c < static_cast<int>(text().length()))
00196 newText.append(",");
00197 }
00198 if (!newText.isEmpty())
00199 insert(newText);
00200 }
|