mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-07-15 14:55:48 +00:00
add lambda support
This commit is contained in:
@ -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;
|
||||
|
@ -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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user