add lambda support

This commit is contained in:
arcan1s 2015-08-21 00:05:15 +03:00
parent a5e9b32b28
commit 835eb00f0d
5 changed files with 81 additions and 13 deletions

View File

@ -26,6 +26,7 @@
#include <QNetworkInterface>
#include <QProcessEnvironment>
#include <QRegExp>
#include <QScriptEngine>
#include <QSettings>
#include <QStandardPaths>
#include <QThread>
@ -131,6 +132,8 @@ QString AWKeys::parsePattern() const
QString parsed = pattern;
parsed.replace(QString("$$"), QString("$\\$\\"));
foreach(QString key, foundLambdas)
parsed.replace(QString("${{%1}}").arg(key), calculateLambda(key));
foreach(QString key, foundKeys)
parsed.replace(QString("$%1").arg(key), htmlValue(key));
foreach(QString bar, foundBars)
@ -820,6 +823,7 @@ void AWKeys::reinitKeys()
keys = dictKeys();
foundBars = findGraphicalItems();
foundKeys = findKeys();
foundLambdas = findLambdas();
}
@ -868,6 +872,27 @@ void AWKeys::addKeyToCache(const QString type, const QString key)
}
QString AWKeys::calculateLambda(QString key) const
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Lambda key" << key;
QScriptEngine engine;
foreach(QString lambdaKey, foundKeys)
key.replace(QString("$%1").arg(lambdaKey), values[lambdaKey]);
if (debug) qDebug() << PDEBUG << ":" << "Expression" << key;
QScriptValue result = engine.evaluate(key);
if (engine.hasUncaughtException()) {
int line = engine.uncaughtExceptionLineNumber();
if (debug) qDebug() << PDEBUG << ":" << "Uncaught exception at line"
<< line << ":" << result.toString();
return QString();
} else
return result.toString();
}
QString AWKeys::htmlValue(QString key) const
{
if (debug) qDebug() << PDEBUG;
@ -934,6 +959,8 @@ QStringList AWKeys::findGraphicalItems() const
QStringList AWKeys::findKeys() const
{
if (debug) qDebug() << PDEBUG;
QStringList selectedKeys;
foreach(QString key, keys) {
if (key.startsWith(QString("bar"))) continue;
@ -947,6 +974,31 @@ QStringList AWKeys::findKeys() const
}
QStringList AWKeys::findLambdas() const
{
if (debug) qDebug() << PDEBUG;
QStringList selectedKeys;
// substring inside ${{ }} (with brackets) which should not contain ${{
QRegularExpression lambdaRegexp(QString("\\$\\{\\{((?!\\$\\{\\{).)*?\\}\\}"));
lambdaRegexp.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator it = lambdaRegexp.globalMatch(pattern);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString lambda = match.captured();
// drop brakets
lambda.remove(QRegExp(QString("^\\$\\{\\{")));
lambda.remove(QRegExp(QString("\\}\\}$")));
// append
if (debug) qDebug() << PDEBUG << ":" << "Found lambda" << lambda;
selectedKeys.append(lambda);
}
return selectedKeys;
}
GraphicalItem *AWKeys::getItemByTag(const QString tag) const
{
if (debug) qDebug() << PDEBUG;

View File

@ -75,12 +75,14 @@ private slots:
private:
// methods
void addKeyToCache(const QString type, const QString key = QString(""));
QString calculateLambda(QString key) const;
QString htmlValue(QString key) const;
int numberCpus() const;
float temperature(const float temp, const QString units) const;
// find methods
QStringList findGraphicalItems() const;
QStringList findKeys() const;
QStringList findLambdas() const;
// get methods
GraphicalItem *getItemByTag(const QString tag) const;
QStringList getTimeKeys() const;
@ -94,7 +96,7 @@ private:
ExtItemAggregator<ExtUpgrade> *extUpgrade;
ExtItemAggregator<ExtWeather> *extWeather;
QString pattern;
QStringList foundBars, foundKeys, keys;
QStringList foundBars, foundKeys, foundLambdas, keys;
QHash<QString, QString> values;
QStringList diskDevices, hddDevices, mountDevices, networkDevices, tempDevices;
};

View File

@ -67,21 +67,35 @@ public:
m_items = getItems();
};
T *itemByTagNumber(const int _number) const
{
if (debug) qDebug() << PDEBUG;
T *found = nullptr;
foreach(T *item, m_items) {
if (item->number() != _number) continue;
found = item;
break;
}
return found;
}
T *itemFromWidget() const
{
if (debug) qDebug() << PDEBUG;
QListWidgetItem *item = widgetDialog->currentItem();
if (item == nullptr) return nullptr;
QListWidgetItem *widgetItem = widgetDialog->currentItem();
if (widgetItem == nullptr) return nullptr;
int originalItem = -1;
for (int i=0; i<m_items.count(); i++) {
if (m_items.at(i)->fileName() != item->text()) continue;
originalItem = i;
T *found = nullptr;
foreach(T *item, m_items) {
if (item->fileName() != widgetItem->text()) continue;
found = item;
break;
}
return originalItem == -1 ? nullptr : m_items[originalItem];
return found;
};
QList<T *> items() const

View File

@ -1,5 +1,5 @@
find_package(Gettext REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Core DBus Network Qml Widgets)
find_package(Qt5 REQUIRED COMPONENTS Core DBus Network Script Qml Widgets)
find_package(ECM 0.0.11 REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
find_package(KF5 REQUIRED COMPONENTS I18n Notifications Plasma Service WindowSystem)
@ -10,16 +10,16 @@ include(KDECompilerSettings)
add_definitions(
${Qt5Core_DEFINITIONS} ${Qt5DBus_DEFINITIONS} ${Qt5Network_DEFINITIONS}
${Qt5Qml_DEFINITIONS} ${Qt5Widgets_DEFINITIONS}
${Qt5Script_DEFINITIONS} ${Qt5Qml_DEFINITIONS} ${Qt5Widgets_DEFINITIONS}
)
set(Qt_INCLUDE
${Qt5Core_INCLUDE_DIRS} ${Qt5DBus_INCLUDE_DIRS} ${Qt5Network_INCLUDE_DIRS}
${Qt5Qml_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS}
${Qt5Script_INCLUDE_DIRS} ${Qt5Qml_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS}
)
set(Kf5_INCLUDE ${I18n_INCLUDE_DIR} ${Notifications_INCLUDE_DIR} ${Plasma_INCLUDE_DIR})
set(Qt_LIBRARIES
${Qt5Core_LIBRARIES} ${Qt5DBus_LIBRARIES} ${Qt5Network_LIBRARIES}
${Qt5Qml_LIBRARIES} ${Qt5Widgets_LIBRARIES}
${Qt5Script_LIBRARIES} ${Qt5Qml_LIBRARIES} ${Qt5Widgets_LIBRARIES}
)
set(Kf5_LIBRARIES KF5::I18n KF5::Notifications KF5::Plasma KF5::WindowSystem)