#include "util.h"#include "QDebug"#include <QString>#include <QGestureEvent>#include <QTouchEvent>#include <QContextMenuEvent>#include <QApplication>#include <QPlainTextEdit>#include <QLineEdit>#include <QFileInfo>#ifdef Q_OS_ANDROID#include <QtAndroid>#include <QAndroidJniEnvironment>#endifUtil::Util(){}const char Util::unescapeTable[] = "a007円b010円e033円f014円n012円r015円t011円v013円\\134円\'047円\"042円\?077円";int Util::unescapeHelper(QStringRef text, int& result, int baseBits){int i, n = 0;result = 0;for(i = 0; i < text.size(); i++){n = text[i].toLatin1();if(n >= 'a' && n <= 'z' && n - 'a' + 10 < (1 << baseBits)){result <<= baseBits;result += n - 'a' + 10;}else if(n >= 'A' && n <= 'Z' && n - 'A' + 10 < (1 << baseBits)){result <<= baseBits;result += n - 'A' + 10;}else if(n >= '0' && n <= '9' && n - '0' < (1 << baseBits)){result <<= baseBits;result += n - '0';}elsebreak;}return qMin(i, text.size());}QByteArray Util::unescape(const QString &text, QTextCodec* codec){QByteArray result;for(int i = 0; i < text.size(); i++){// keep the normal string unchangedif(text[i] != '\\' || i + 1 >= text.size()){int end = i;while(end < text.size() && text[end] != '\\')end++;result += codec->fromUnicode(text.constData() + i, end - i);i = end - 1;continue;}// '\' is not at the end, process// see https://en.wikipedia.org/wiki/Escape_sequences_in_C#Table_of_escape_sequences// 1. \a, \b, \e, \f, \n, \r, \t, \v, \,円 \', \", \?size_t j;for(j = 0; j < sizeof(unescapeTable) / 2; j++){if(text[i + 1] == unescapeTable[j * 2]){result += unescapeTable[j * 2 + 1];i++;break;}}if(j < sizeof(unescapeTable) / 2) // processedcontinue;int handled, ch;// 2. \nnn, 1~3 octal digits for a byteif(i + 1 < text.size() && text[i + 1] >= '0' && text[i + 1] <= '7'){handled = unescapeHelper(text.midRef(i + 1, qMin(3, text.size() - (i + 1))), ch, 3);i += handled;if(handled)result += ch & 0xFF;}// 3. \xHH, 1~2 hexadecimal digits for a byteelse if(i + 2 < text.size() && text[i + 1] == 'x'){handled = unescapeHelper(text.midRef(i + 2, qMin(2, text.size() - (i + 2))), ch, 4);if(handled){result += ch & 0xFF;i += handled + 1; // including 'x'}}// 4. \uHHHH, exact 4 hexadecimal digits for a unicode char// might be slowelse if(i + 5 < text.size() && text[i + 1] == 'u'){bool isOk;ch = text.midRef(i + 2, 4).toInt(&isOk, 16);if(isOk){QChar qch(ch); // treat it like QString with length=1result += codec->fromUnicode(&qch, 1);i += 5;}}// 5. invalid ,円 keep it unchangedelse{result += codec->fromUnicode(text.constData() + i, 1);}}// qDebug() << result.toHex(' ');// qDebug() << result;return result;}void Util::disableItem(QStandardItemModel* model, int id, bool enabled){if(model == nullptr)return;QStandardItem *item = model->item(id);Qt::ItemFlags flags = item->flags();flags.setFlag(Qt::ItemIsEnabled, enabled);item->setFlags(flags);}#ifdef Q_OS_ANDROIDvoid Util::showToast(const QString& message, bool isLong){// all the magic must happen on Android UI thread// don't capture by reference thereQtAndroid::runOnAndroidThread([ = ]{QAndroidJniObject javaString = QAndroidJniObject::fromString(message);QAndroidJniObject toast = QAndroidJniObject::callStaticObjectMethod("android/widget/Toast", "makeText","(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;",QtAndroid::androidActivity().object(),javaString.object(),jint(isLong ? 1 : 0));toast.callMethod<void>("show");});}#endifQString Util::getValidLocalFilename(const QList<QUrl>& urlList){for(const auto& url : urlList){if(url.isLocalFile() && QFileInfo(url.toLocalFile()).isFile())return url.toLocalFile();}return QString();}// use TapAndHold gesture to show the context menu// call widget->grabGesture(Qt::TapAndHoldGesture) then use the event filter// for QLineEdit, the edit menu will be shown// for QPlainTextEdit, the parent's context menu will be shown// useless now...bool GestureConverter::eventFilter(QObject *obj, QEvent *event){qDebug() << obj->objectName() << event->type();if(event->type() == QEvent::Gesture || event->type() == QEvent::GestureOverride){QGestureEvent *ge = static_cast<QGestureEvent*>(event);qDebug() << obj->objectName() << ge->gestures();QGesture *ges = ge->gesture(Qt::TapAndHoldGesture);if(ges->state() == Qt::GestureFinished){QContextMenuEvent newEvent(QContextMenuEvent::Mouse, ge->mapToGraphicsScene(ges->hotSpot()).toPoint());QApplication::sendEvent(obj, &newEvent);}}return false;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。