Compare commits
51 Commits
wip/inheri
...
master
Author | SHA1 | Date | |
---|---|---|---|
8a47838d94 | |||
89c9cc573e | |||
7c15d7b145 | |||
d9b0be5063 | |||
f1b17c0842 | |||
4315de0e76 | |||
f0387db468 | |||
6e4f0ecabf | |||
2df37212dd | |||
b13815a924 | |||
bb74d156c9 | |||
f9038f3098 | |||
042b53a0be | |||
64d3223fb2 | |||
adfb065358 | |||
98cff50c41 | |||
5b5333585c | |||
8eb52fbfb3 | |||
241bec54ad | |||
ac5498990b | |||
38cc87fbcd | |||
2d14b01c5c | |||
59ff382856 | |||
bf8f09ec66 | |||
776cbe4d30 | |||
101885ffe4 | |||
79d15fb628 | |||
95e855f325 | |||
2acfe6126a | |||
2d79d74e06 | |||
7c2461626d | |||
ae7f310ab2 | |||
2ab0750f3a | |||
f9eff3b5b2 | |||
84cf8837bd | |||
47c0358a25 | |||
7bae1183c6 | |||
9daaba2543 | |||
6b924c0f53 | |||
653089c475 | |||
f06e9a4f7b | |||
61001ed6cc | |||
e2c80b665e | |||
2a9292958b | |||
27ac3155d3 | |||
b72931cc9e | |||
b588fd07be | |||
5eb09540c6 | |||
416bfa6314 | |||
edb781580e | |||
36b6390292 |
4
TODO
4
TODO
@ -1,4 +0,0 @@
|
|||||||
- Investigate memory leaks (relevant now that we have a single user mode,
|
|
||||||
so qsrun may stay open after launch)
|
|
||||||
- Provide --help
|
|
||||||
- ESC/CTRL+Q close the app. May not be expected behaviour
|
|
@ -13,6 +13,11 @@ EntryProvider::EntryProvider(QStringList userEntriesDirsPaths, QStringList syste
|
|||||||
<< "%u";
|
<< "%u";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EntryProvider::isSavable(const EntryConfig &config) const
|
||||||
|
{
|
||||||
|
return ! config.entryPath.isEmpty() && (config.type == EntryType::USER || config.type == EntryType::INHERIT);
|
||||||
|
}
|
||||||
|
|
||||||
EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
|
EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
|
||||||
{
|
{
|
||||||
EntryConfig result;
|
EntryConfig result;
|
||||||
@ -20,20 +25,22 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
|
|||||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||||
{
|
{
|
||||||
// TODO: better exception class
|
// TODO: better exception class
|
||||||
throw new std::runtime_error("Failed to open file");
|
throw std::runtime_error("Failed to open file");
|
||||||
}
|
}
|
||||||
QTextStream stream(&file);
|
QTextStream stream(&file);
|
||||||
// There should be nothing preceding this group in the desktop entry file but possibly one or more comments.
|
// There should be nothing preceding this group in the desktop entry file but possibly one or more comments.
|
||||||
// https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html#group-header
|
// https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html#group-header
|
||||||
QString firstLine;
|
// Ignore that as there some that violate that in the wild
|
||||||
|
const QString startSection = "[Desktop Entry]";
|
||||||
|
QString line;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
firstLine = stream.readLine().trimmed();
|
line = stream.readLine().trimmed();
|
||||||
} while(!stream.atEnd() && (firstLine.isEmpty() || firstLine[0] == '#'));
|
} while(!stream.atEnd() && line != startSection);
|
||||||
|
|
||||||
if(firstLine != "[Desktop Entry]")
|
if(line != startSection)
|
||||||
{
|
{
|
||||||
throw ConfigFormatException(".desktop file does not start with [Desktop Entry]: " + path.toStdString());
|
throw ConfigFormatException(".desktop file does not contain [Desktop Entry] section: " + path.toStdString());
|
||||||
}
|
}
|
||||||
|
|
||||||
while(!stream.atEnd())
|
while(!stream.atEnd())
|
||||||
@ -56,7 +63,7 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
|
|||||||
}
|
}
|
||||||
if(key == "icon")
|
if(key == "icon")
|
||||||
{
|
{
|
||||||
result.icon = QIcon::fromTheme(args);
|
result.iconPath = args;
|
||||||
}
|
}
|
||||||
if(key == "exec")
|
if(key == "exec")
|
||||||
{
|
{
|
||||||
@ -75,12 +82,39 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(key == "nodisplay")
|
||||||
|
{
|
||||||
|
result.hidden = args == "true";
|
||||||
}
|
}
|
||||||
|
if(key == "terminal")
|
||||||
|
{
|
||||||
|
result.isTerminalCommand = args == "true";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.type = EntryType::SYSTEM;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* qsrun own's config file */
|
std::optional<EntryConfig> EntryProvider::readEntryFromPath(const QString &path)
|
||||||
EntryConfig EntryProvider::readFromFile(const QString &path)
|
{
|
||||||
|
QFileInfo info(path);
|
||||||
|
if(info.isFile())
|
||||||
|
{
|
||||||
|
QString suffix = info.suffix();
|
||||||
|
if(suffix == "desktop")
|
||||||
|
{
|
||||||
|
return readFromDesktopFile(path);
|
||||||
|
}
|
||||||
|
if(suffix == "qsrun")
|
||||||
|
{
|
||||||
|
return readqsrunFile(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* qsrun's own format */
|
||||||
|
EntryConfig EntryProvider::readqsrunFile(const QString &path)
|
||||||
{
|
{
|
||||||
EntryConfig result;
|
EntryConfig result;
|
||||||
EntryConfig inheritedConfig;
|
EntryConfig inheritedConfig;
|
||||||
@ -88,21 +122,73 @@ EntryConfig EntryProvider::readFromFile(const QString &path)
|
|||||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||||
{
|
{
|
||||||
// TODO: better exception class
|
// TODO: better exception class
|
||||||
throw new std::runtime_error("Failed to open file");
|
throw std::runtime_error("Failed to open file");
|
||||||
}
|
}
|
||||||
|
QHash<QString, QString> map;
|
||||||
QTextStream stream(&file);
|
QTextStream stream(&file);
|
||||||
while(!stream.atEnd())
|
while(!stream.atEnd())
|
||||||
{
|
{
|
||||||
QString line = stream.readLine();
|
QString line = stream.readLine();
|
||||||
QStringList splitted = line.split(" ");
|
|
||||||
if(splitted.length() < 2)
|
int spacePos = line.indexOf(' ');
|
||||||
|
if(spacePos == -1)
|
||||||
{
|
{
|
||||||
throw new ConfigFormatException("misformated line in .qsrun config file " + path.toStdString());
|
throw ConfigFormatException("misformated line in .qsrun config file " + path.toStdString());
|
||||||
}
|
}
|
||||||
QString key = splitted[0];
|
|
||||||
if(key == "arguments")
|
QString key = line.mid(0, spacePos);
|
||||||
|
QString value = line.mid(spacePos + 1);
|
||||||
|
|
||||||
|
if(key == "" || value == "")
|
||||||
{
|
{
|
||||||
auto args = splitted.mid(1);
|
throw ConfigFormatException("empty key or value in .qsrun config file " + path.toStdString());
|
||||||
|
}
|
||||||
|
map[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(map.contains("inherit"))
|
||||||
|
{
|
||||||
|
auto entry = readEntryFromPath(map["inherit"]);
|
||||||
|
if(entry)
|
||||||
|
{
|
||||||
|
result = entry.value();
|
||||||
|
result.inherit = map["inherit"];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw ConfigFormatException("Error attempting to read inherited entry");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QString type = map["type"];
|
||||||
|
if(!type.isEmpty())
|
||||||
|
{
|
||||||
|
if(type == "system")
|
||||||
|
{
|
||||||
|
throw ConfigFormatException(".qsrun files cannot be designated as system entries " +
|
||||||
|
path.toStdString());
|
||||||
|
}
|
||||||
|
else if(type == "inherit")
|
||||||
|
{
|
||||||
|
result.type = EntryType::INHERIT;
|
||||||
|
}
|
||||||
|
else if(type == "user")
|
||||||
|
{
|
||||||
|
result.type = EntryType::USER;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw ConfigFormatException("Invalid value for type provided in file: " + path.toStdString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.type = EntryType::USER;
|
||||||
|
}
|
||||||
|
if(result.type != EntryType::INHERIT)
|
||||||
|
{
|
||||||
|
if(map.contains("arguments"))
|
||||||
|
{
|
||||||
|
auto args = map["arguments"].split(' ');
|
||||||
QString merged;
|
QString merged;
|
||||||
for(QString &str : args)
|
for(QString &str : args)
|
||||||
{
|
{
|
||||||
@ -131,38 +217,21 @@ EntryConfig EntryProvider::readFromFile(const QString &path)
|
|||||||
throw ConfigFormatException("non-closed \" in config file " + path.toStdString());
|
throw ConfigFormatException("non-closed \" in config file " + path.toStdString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(key == "name")
|
auto assignIfSourceNotEmpty = [](QString source, QString &val) {
|
||||||
|
if(!source.isEmpty())
|
||||||
{
|
{
|
||||||
result.name = splitted[1];
|
val = source;
|
||||||
}
|
}
|
||||||
if(key == "icon")
|
};
|
||||||
{
|
assignIfSourceNotEmpty(map["key"].toLower(), result.key);
|
||||||
result.icon = QIcon(splitted[1]);
|
assignIfSourceNotEmpty(map["command"], result.command);
|
||||||
|
assignIfSourceNotEmpty(map["icon"], result.iconPath);
|
||||||
|
assignIfSourceNotEmpty(map["name"], result.name);
|
||||||
}
|
}
|
||||||
if(key == "row")
|
result.col = map["col"].toInt();
|
||||||
{
|
result.row = map["row"].toInt();
|
||||||
result.row = splitted[1].toInt();
|
result.isTerminalCommand = map["terminal"] == "true";
|
||||||
}
|
return result;
|
||||||
if(key == "col")
|
|
||||||
{
|
|
||||||
result.col = splitted[1].toInt();
|
|
||||||
}
|
|
||||||
if(key == "command")
|
|
||||||
{
|
|
||||||
result.command = splitted[1];
|
|
||||||
}
|
|
||||||
if(key == "key")
|
|
||||||
{
|
|
||||||
// QKeySequence sequence(splitted[1]);
|
|
||||||
// result.keySequence = sequence;
|
|
||||||
result.key = splitted[1].toLower();
|
|
||||||
}
|
|
||||||
if(key == "inherit")
|
|
||||||
{
|
|
||||||
inheritedConfig = readFromDesktopFile(resolveEntryPath(splitted[1]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result.update(inheritedConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString EntryProvider::resolveEntryPath(QString path)
|
QString EntryProvider::resolveEntryPath(QString path)
|
||||||
@ -197,17 +266,13 @@ QVector<EntryConfig> EntryProvider::readConfig(QStringList paths)
|
|||||||
while(it.hasNext())
|
while(it.hasNext())
|
||||||
{
|
{
|
||||||
QString path = it.next();
|
QString path = it.next();
|
||||||
QFileInfo info(path);
|
std::optional<EntryConfig> entry = readEntryFromPath(path);
|
||||||
if(info.isFile())
|
if(entry)
|
||||||
{
|
{
|
||||||
QString suffix = info.suffix();
|
if(!entry->hidden)
|
||||||
if(suffix == "desktop")
|
|
||||||
{
|
{
|
||||||
result.append(readFromDesktopFile(path));
|
entry->entryPath = path;
|
||||||
}
|
result.append(*entry);
|
||||||
if(suffix == "qsrun")
|
|
||||||
{
|
|
||||||
result.append(readFromFile(path));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,6 +290,73 @@ QVector<EntryConfig> EntryProvider::getSystemEntries()
|
|||||||
return readConfig(this->systemEntriesDirsPaths);
|
return readConfig(this->systemEntriesDirsPaths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EntryProvider::saveUserEntry(const EntryConfig &config)
|
||||||
|
{
|
||||||
|
if(!isSavable(config))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Only user/inherited entries can be saved");
|
||||||
|
}
|
||||||
|
QString transitPath = config.entryPath + ".transit";
|
||||||
|
QFile file{transitPath};
|
||||||
|
if(!file.open(QIODevice::WriteOnly))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Error: Can not open file for writing");
|
||||||
|
}
|
||||||
|
QTextStream outStream(&file);
|
||||||
|
outStream << "type" << " " << ((config.type == EntryType::USER) ? "user" : "inherit") << Qt::endl;
|
||||||
|
if(!config.inherit.isEmpty())
|
||||||
|
{
|
||||||
|
outStream << "inherit" << " " << config.inherit << Qt::endl;
|
||||||
|
}
|
||||||
|
outStream << "row" << " " << config.row << Qt::endl;
|
||||||
|
outStream << "col" << " " << config.col << Qt::endl;
|
||||||
|
outStream << "hidden" << " " << config.hidden << Qt::endl;
|
||||||
|
if(!config.key.isEmpty())
|
||||||
|
{
|
||||||
|
outStream << "key" << " " << config.key << Qt::endl;
|
||||||
|
}
|
||||||
|
if(config.type == EntryType::USER)
|
||||||
|
{
|
||||||
|
if(!config.name.isEmpty())
|
||||||
|
{
|
||||||
|
outStream << "name" << " " << config.name << Qt::endl;
|
||||||
|
}
|
||||||
|
if(!config.command.isEmpty())
|
||||||
|
{
|
||||||
|
outStream << "command" << " " << config.command << Qt::endl;
|
||||||
|
}
|
||||||
|
if(!config.iconPath.isEmpty())
|
||||||
|
{
|
||||||
|
outStream << "icon" << " " << config.iconPath << Qt::endl;
|
||||||
|
}
|
||||||
|
if(!config.arguments.empty())
|
||||||
|
{
|
||||||
|
outStream << "arguments" << " " << config.arguments.join(' ') << Qt::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
outStream.flush();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
// Qts don't work if file already exists and c++17... don't want to pull in the fs lib yet
|
||||||
|
int ret = rename(transitPath.toStdString().c_str(), config.entryPath.toStdString().c_str());
|
||||||
|
if(ret != 0)
|
||||||
|
{
|
||||||
|
qDebug() << strerror(errno);
|
||||||
|
throw std::runtime_error("Failed to save entry file: Error during rename");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EntryProvider::deleteUserEntry(const EntryConfig &config)
|
||||||
|
{
|
||||||
|
if(!isSavable(config))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Only user/inherited entries can be deleted");
|
||||||
|
}
|
||||||
|
QFile file{config.entryPath};
|
||||||
|
return file.remove();
|
||||||
|
}
|
||||||
|
|
||||||
template <class T> void assignIfDestDefault(T &dest, const T &source)
|
template <class T> void assignIfDestDefault(T &dest, const T &source)
|
||||||
{
|
{
|
||||||
if(dest == T())
|
if(dest == T())
|
||||||
@ -238,12 +370,16 @@ EntryConfig &EntryConfig::update(const EntryConfig &o)
|
|||||||
assignIfDestDefault(this->arguments, o.arguments);
|
assignIfDestDefault(this->arguments, o.arguments);
|
||||||
assignIfDestDefault(this->col, o.col);
|
assignIfDestDefault(this->col, o.col);
|
||||||
assignIfDestDefault(this->command, o.command);
|
assignIfDestDefault(this->command, o.command);
|
||||||
if(this->icon.isNull())
|
if(this->iconPath.isEmpty())
|
||||||
{
|
{
|
||||||
this->icon = o.icon;
|
this->iconPath = o.iconPath;
|
||||||
}
|
}
|
||||||
assignIfDestDefault(this->key, o.key);
|
assignIfDestDefault(this->key, o.key);
|
||||||
assignIfDestDefault(this->name, o.name);
|
assignIfDestDefault(this->name, o.name);
|
||||||
assignIfDestDefault(this->row, o.row);
|
assignIfDestDefault(this->row, o.row);
|
||||||
|
assignIfDestDefault(this->hidden, o.hidden);
|
||||||
|
assignIfDestDefault(this->inherit, o.inherit);
|
||||||
|
assignIfDestDefault(this->entryPath, o.entryPath);
|
||||||
|
assignIfDestDefault(this->type, o.type);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define ENTRYPROVIDER_H
|
#define ENTRYPROVIDER_H
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
class ConfigFormatException : public std::runtime_error
|
class ConfigFormatException : public std::runtime_error
|
||||||
{
|
{
|
||||||
@ -14,14 +15,27 @@ class ConfigFormatException : public std::runtime_error
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum EntryType
|
||||||
|
{
|
||||||
|
USER,
|
||||||
|
INHERIT,
|
||||||
|
SYSTEM,
|
||||||
|
DYNAMIC
|
||||||
|
};
|
||||||
|
|
||||||
class EntryConfig
|
class EntryConfig
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
EntryType type = SYSTEM;
|
||||||
|
bool hidden = false;
|
||||||
|
bool isTerminalCommand = false;
|
||||||
|
QString entryPath;
|
||||||
QString key;
|
QString key;
|
||||||
QString name;
|
QString name;
|
||||||
QString command;
|
QString command;
|
||||||
|
QString iconPath;
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
QIcon icon;
|
QString inherit;
|
||||||
int row = 0;
|
int row = 0;
|
||||||
int col = 0;
|
int col = 0;
|
||||||
|
|
||||||
@ -34,15 +48,19 @@ class EntryProvider
|
|||||||
QStringList _desktopIgnoreArgs;
|
QStringList _desktopIgnoreArgs;
|
||||||
QStringList userEntriesDirsPaths;
|
QStringList userEntriesDirsPaths;
|
||||||
QStringList systemEntriesDirsPaths;
|
QStringList systemEntriesDirsPaths;
|
||||||
EntryConfig readFromFile(const QString &path);
|
EntryConfig readqsrunFile(const QString &path);
|
||||||
EntryConfig readFromDesktopFile(const QString &path);
|
EntryConfig readFromDesktopFile(const QString &path);
|
||||||
|
std::optional<EntryConfig> readEntryFromPath(const QString &path);
|
||||||
QVector<EntryConfig> readConfig(QStringList paths);
|
QVector<EntryConfig> readConfig(QStringList paths);
|
||||||
QString resolveEntryPath(QString path);
|
QString resolveEntryPath(QString path);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths);
|
EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths);
|
||||||
|
bool isSavable(const EntryConfig &config) const;
|
||||||
QVector<EntryConfig> getUserEntries();
|
QVector<EntryConfig> getUserEntries();
|
||||||
QVector<EntryConfig> getSystemEntries();
|
QVector<EntryConfig> getSystemEntries();
|
||||||
|
void saveUserEntry(const EntryConfig &config);
|
||||||
|
bool deleteUserEntry(const EntryConfig &config);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ENTRYPROVIDER_H
|
#endif // ENTRYPROVIDER_H
|
||||||
|
@ -13,39 +13,56 @@
|
|||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <QDrag>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QApplication>
|
||||||
#include "entrypushbutton.h"
|
#include "entrypushbutton.h"
|
||||||
|
|
||||||
EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
|
EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
|
||||||
{
|
{
|
||||||
this->setText(config.name);
|
this->setText(config.name);
|
||||||
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||||
this->setIcon(config.icon);
|
QIcon icon;
|
||||||
if(!config.icon.availableSizes().isEmpty())
|
if(config.isTerminalCommand && config.iconPath.isEmpty())
|
||||||
{
|
{
|
||||||
auto sizes = config.icon.availableSizes();
|
icon = resolveIcon("utilities-terminal");
|
||||||
QSize maxSize = sizes.first();
|
}
|
||||||
for(QSize ¤t : sizes)
|
else
|
||||||
{
|
{
|
||||||
if(current.width() > maxSize.width())
|
icon = resolveIcon(config.iconPath);
|
||||||
{
|
|
||||||
maxSize = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this->setIconSize(maxSize);
|
|
||||||
}
|
}
|
||||||
|
this->setIcon(icon);
|
||||||
|
this->setIconSize(QSize{256, 256});
|
||||||
this->config = config;
|
this->config = config;
|
||||||
connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked()));
|
connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked()));
|
||||||
|
|
||||||
|
systemEntryMenu.addAction("Add to favorites", [&] { emit addToFavourites(this->config); });
|
||||||
|
userEntryMenu.addAction("Delete", [&] { emit deleteRequested(this->config); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QIcon EntryPushButton::resolveIcon(QString path)
|
||||||
|
{
|
||||||
|
if(!path.isEmpty())
|
||||||
|
{
|
||||||
|
if(path[0] == '/')
|
||||||
|
{
|
||||||
|
return QIcon(path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return QIcon::fromTheme(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QIcon();
|
||||||
|
}
|
||||||
|
|
||||||
void EntryPushButton::emitOwnClicked()
|
void EntryPushButton::emitOwnClicked()
|
||||||
{
|
{
|
||||||
emit clicked(this->config);
|
emit clicked(this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
const EntryConfig &EntryPushButton::getEntryConfig()
|
const EntryConfig &EntryPushButton::getEntryConfig() const
|
||||||
{
|
{
|
||||||
return this->config;
|
return this->config;
|
||||||
}
|
}
|
||||||
@ -65,12 +82,82 @@ void EntryPushButton::showName()
|
|||||||
this->setText(this->config.name);
|
this->setText(this->config.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int EntryPushButton::getRow() const { return config.row; }
|
void EntryPushButton::mousePressEvent(QMouseEvent *event)
|
||||||
int EntryPushButton::getCol() const { return config.col; }
|
{
|
||||||
QString EntryPushButton::getName() const { return config.name; }
|
if(event->button() == Qt::LeftButton)
|
||||||
QString EntryPushButton::getShortcutKey() const { return config.key; }
|
{
|
||||||
void EntryPushButton::setShortcutKey(QString key) { this->config.key = key; }
|
dragStartPosition = event->pos();
|
||||||
void EntryPushButton::setRow(int row) { this->config.row = row; }
|
}
|
||||||
void EntryPushButton::setCol(int col) { this->config.col = col; }
|
if(event->button() == Qt::RightButton)
|
||||||
QStringList EntryPushButton::getArguments() const { return this->config.arguments; }
|
{
|
||||||
QString EntryPushButton::getCommand() const { return this->config.command; }
|
if(this->config.type == EntryType::USER || this->config.type == EntryType::INHERIT)
|
||||||
|
{
|
||||||
|
this->userEntryMenu.exec(QCursor::pos());
|
||||||
|
}
|
||||||
|
else if(this->config.type == EntryType::SYSTEM)
|
||||||
|
{
|
||||||
|
this->systemEntryMenu.exec(QCursor::pos());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QPushButton::mousePressEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntryPushButton::mouseMoveEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
if(this->config.type == EntryType::SYSTEM || this->config.type == EntryType::DYNAMIC)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!(event->buttons() & Qt::LeftButton))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if((event->pos() - dragStartPosition).manhattanLength() < QApplication::startDragDistance())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDrag *drag = new QDrag(this);
|
||||||
|
QMimeData *mimeData = new QMimeData();
|
||||||
|
QByteArray data;
|
||||||
|
mimeData->setData(ENTRYBUTTON_MIME_TYPE_STR, data);
|
||||||
|
drag->setMimeData(mimeData);
|
||||||
|
Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
int EntryPushButton::getRow() const
|
||||||
|
{
|
||||||
|
return config.row;
|
||||||
|
}
|
||||||
|
int EntryPushButton::getCol() const
|
||||||
|
{
|
||||||
|
return config.col;
|
||||||
|
}
|
||||||
|
QString EntryPushButton::getName() const
|
||||||
|
{
|
||||||
|
return config.name;
|
||||||
|
}
|
||||||
|
QString EntryPushButton::getShortcutKey() const
|
||||||
|
{
|
||||||
|
return config.key;
|
||||||
|
}
|
||||||
|
void EntryPushButton::setShortcutKey(QString key)
|
||||||
|
{
|
||||||
|
this->config.key = key;
|
||||||
|
}
|
||||||
|
void EntryPushButton::setRow(int row)
|
||||||
|
{
|
||||||
|
this->config.row = row;
|
||||||
|
}
|
||||||
|
void EntryPushButton::setCol(int col)
|
||||||
|
{
|
||||||
|
this->config.col = col;
|
||||||
|
}
|
||||||
|
QStringList EntryPushButton::getArguments() const
|
||||||
|
{
|
||||||
|
return this->config.arguments;
|
||||||
|
}
|
||||||
|
QString EntryPushButton::getCommand() const
|
||||||
|
{
|
||||||
|
return this->config.command;
|
||||||
|
}
|
||||||
|
@ -18,20 +18,38 @@
|
|||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
|
#include <QMouseEvent>
|
||||||
|
#include <QMenu>
|
||||||
|
|
||||||
#include "entryprovider.h"
|
#include "entryprovider.h"
|
||||||
|
|
||||||
|
#define ENTRYBUTTON_MIME_TYPE_STR "application/x-qsrun-entrypushbutton"
|
||||||
|
|
||||||
class EntryPushButton : public QPushButton
|
class EntryPushButton : public QPushButton
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
|
QMenu systemEntryMenu;
|
||||||
|
QMenu userEntryMenu;
|
||||||
EntryConfig config;
|
EntryConfig config;
|
||||||
|
QPoint dragStartPosition;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void emitOwnClicked();
|
void emitOwnClicked();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void clicked(const EntryConfig &config);
|
void clicked(const EntryConfig &config);
|
||||||
|
void deleteRequested(EntryConfig &config);
|
||||||
|
void addToFavourites(const EntryConfig &config);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void mousePressEvent(QMouseEvent *event);
|
||||||
|
void mouseMoveEvent(QMouseEvent *event);
|
||||||
|
QIcon resolveIcon(QString path);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntryPushButton(const EntryConfig &config);
|
EntryPushButton(const EntryConfig &config);
|
||||||
const EntryConfig &getEntryConfig();
|
const EntryConfig &getEntryConfig() const;
|
||||||
void setEntryConfig(const EntryConfig &config);
|
void setEntryConfig(const EntryConfig &config);
|
||||||
void showShortcut();
|
void showShortcut();
|
||||||
void showName();
|
void showName();
|
||||||
@ -46,6 +64,4 @@ public:
|
|||||||
void setShortcutKey(QString key);
|
void setShortcutKey(QString key);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // ENTRYPUSHBUTTON_H
|
#endif // ENTRYPUSHBUTTON_H
|
||||||
|
51
main.cpp
51
main.cpp
@ -14,6 +14,7 @@
|
|||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QCommandLineParser>
|
||||||
#include <QFuture>
|
#include <QFuture>
|
||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
#include <QtConcurrent/QtConcurrentRun>
|
#include <QtConcurrent/QtConcurrentRun>
|
||||||
@ -30,27 +31,38 @@ int main(int argc, char *argv[])
|
|||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
QString configDirectoryPath;
|
QString configDirectoryPath;
|
||||||
QDir dir;
|
QDir dir;
|
||||||
|
bool newInstanceRequested = false;
|
||||||
if(argc >= 2)
|
if(argc >= 2)
|
||||||
{
|
{
|
||||||
configDirectoryPath = QCoreApplication::arguments().at(1);
|
QCommandLineParser parser;
|
||||||
if(!dir.exists(configDirectoryPath))
|
parser.addOptions({
|
||||||
|
{"new-instance", "Launch a new instance, ignoring any running ones"},
|
||||||
|
{"config", "Use supplied config dir instead of default"},
|
||||||
|
});
|
||||||
|
parser.addHelpOption();
|
||||||
|
parser.process(app.arguments());
|
||||||
|
configDirectoryPath = parser.value("config");
|
||||||
|
newInstanceRequested = parser.isSet("new-instance");
|
||||||
|
|
||||||
|
if(!configDirectoryPath.isEmpty() && !dir.exists(configDirectoryPath))
|
||||||
{
|
{
|
||||||
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found");
|
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
if(configDirectoryPath.isEmpty())
|
||||||
{
|
{
|
||||||
configDirectoryPath = QDir::homePath() + "/.config/qsrun/";
|
configDirectoryPath = QDir::homePath() + "/.config/qsrun/";
|
||||||
}
|
}
|
||||||
qRegisterMetaType<QVector<QString> >("QVector<QString>");
|
|
||||||
|
|
||||||
|
qRegisterMetaType<QVector<QString>>("QVector<QString>");
|
||||||
|
|
||||||
if(!dir.exists(configDirectoryPath))
|
if(!dir.exists(configDirectoryPath))
|
||||||
{
|
{
|
||||||
if(!dir.mkdir(configDirectoryPath))
|
if(!dir.mkdir(configDirectoryPath))
|
||||||
{
|
{
|
||||||
QMessageBox::warning(nullptr, "Failed to create dir", configDirectoryPath + " was not found and could not be created!");
|
QMessageBox::warning(nullptr, "Failed to create dir",
|
||||||
|
configDirectoryPath + " was not found and could not be created!");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,10 +71,14 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
SettingsProvider settingsProvider{settings};
|
SettingsProvider settingsProvider{settings};
|
||||||
EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths());
|
EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths());
|
||||||
//TODO if setting single instance mode
|
|
||||||
|
SingleInstanceServer *server = nullptr;
|
||||||
|
|
||||||
|
bool singleInstanceMode = !newInstanceRequested && settingsProvider.singleInstanceMode();
|
||||||
|
if(singleInstanceMode)
|
||||||
|
{
|
||||||
QLocalSocket localSocket;
|
QLocalSocket localSocket;
|
||||||
localSocket.connectToServer("/tmp/qsrun.socket");
|
localSocket.connectToServer(settingsProvider.socketPath());
|
||||||
SingleInstanceServer server;
|
|
||||||
if(localSocket.isOpen() && localSocket.isWritable())
|
if(localSocket.isOpen() && localSocket.isWritable())
|
||||||
{
|
{
|
||||||
QDataStream stream(&localSocket);
|
QDataStream stream(&localSocket);
|
||||||
@ -72,14 +88,19 @@ int main(int argc, char *argv[])
|
|||||||
localSocket.disconnectFromServer();
|
localSocket.disconnectFromServer();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
server = new SingleInstanceServer();
|
||||||
if(!server.listen("/tmp/qsrun.socket"))
|
if(!server->listen(settingsProvider.socketPath()))
|
||||||
{
|
{
|
||||||
qDebug() << "Failed to listen on socket!";
|
qDebug() << "Failed to listen on socket!";
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Window *w = new Window{entryProvider, settingsProvider};
|
Window *w = new Window{entryProvider, settingsProvider};
|
||||||
QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w]{
|
if(singleInstanceMode && server != nullptr)
|
||||||
|
{
|
||||||
|
QObject::connect(server, &SingleInstanceServer::receivedMaximizationRequest, [&w] {
|
||||||
if(w != nullptr)
|
if(w != nullptr)
|
||||||
{
|
{
|
||||||
qInfo() << "maximizing as requested by other instance";
|
qInfo() << "maximizing as requested by other instance";
|
||||||
@ -89,12 +110,10 @@ int main(int argc, char *argv[])
|
|||||||
w->focusInput();
|
w->focusInput();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
w->showMaximized();
|
w->showMaximized();
|
||||||
w->focusInput();
|
w->focusInput();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -23,5 +23,5 @@ SOURCES += calculationengine.cpp \
|
|||||||
QT += widgets sql network
|
QT += widgets sql network
|
||||||
QT_CONFIG -= no-pkg-config
|
QT_CONFIG -= no-pkg-config
|
||||||
LIBS += -lcln
|
LIBS += -lcln
|
||||||
CONFIG += link_pkgconfig
|
CONFIG += link_pkgconfig c++17
|
||||||
PKGCONFIG += libqalculate
|
PKGCONFIG += libqalculate
|
||||||
|
BIN
screenshots/calc.png
Normal file
BIN
screenshots/calc.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 31 KiB |
BIN
screenshots/search-libreoffice.png
Normal file
BIN
screenshots/search-libreoffice.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
Binary file not shown.
Before Width: | Height: | Size: 112 KiB |
BIN
screenshots/startview.png
Normal file
BIN
screenshots/startview.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 205 KiB |
@ -1,6 +1,6 @@
|
|||||||
#include "settingsprovider.h"
|
#include "settingsprovider.h"
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QFileInfo>
|
||||||
|
|
||||||
SettingsProvider::SettingsProvider(QSettings &settings)
|
SettingsProvider::SettingsProvider(QSettings &settings)
|
||||||
{
|
{
|
||||||
@ -19,9 +19,22 @@ QStringList SettingsProvider::systemApplicationsEntriesPaths() const
|
|||||||
return settings->value("sysAppsPaths", "/usr/share/applications/").toStringList();
|
return settings->value("sysAppsPaths", "/usr/share/applications/").toStringList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SettingsProvider::getMaxCols() const
|
||||||
|
{
|
||||||
|
return settings->value("maxColumns", 3).toInt();
|
||||||
|
}
|
||||||
|
|
||||||
bool SettingsProvider::singleInstanceMode() const
|
bool SettingsProvider::singleInstanceMode() const
|
||||||
{
|
{
|
||||||
return settings->value("singleInstance", true).toBool();
|
return settings->value("singleInstance", true).toBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString SettingsProvider::getTerminalCommand() const
|
||||||
|
{
|
||||||
|
return settings->value("terminal", "/usr/bin/x-terminal-emulator -e %c").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString SettingsProvider::socketPath() const
|
||||||
|
{
|
||||||
|
return settings->value("singleInstanceSocket", "/tmp/qsrun").toString();
|
||||||
|
}
|
||||||
|
@ -1,18 +1,22 @@
|
|||||||
#ifndef SETTINGSPROVIDER_H
|
#ifndef SETTINGSPROVIDER_H
|
||||||
#define SETTINGSPROVIDER_H
|
#define SETTINGSPROVIDER_H
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
class SettingsProvider
|
class SettingsProvider
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
QSettings *settings;
|
QSettings *settings;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SettingsProvider(QSettings &settings);
|
SettingsProvider(QSettings &settings);
|
||||||
virtual QStringList userEntriesPaths() const;
|
virtual QStringList userEntriesPaths() const;
|
||||||
virtual QStringList systemApplicationsEntriesPaths() const;
|
virtual QStringList systemApplicationsEntriesPaths() const;
|
||||||
|
virtual int getMaxCols() const;
|
||||||
virtual bool singleInstanceMode() const;
|
virtual bool singleInstanceMode() const;
|
||||||
|
QString getTerminalCommand() const;
|
||||||
|
QString socketPath() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SETTINGSPROVIDER_H
|
#endif // SETTINGSPROVIDER_H
|
||||||
|
246
window.cpp
246
window.cpp
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2019 Albert S. <mail at quitesimple dot org>
|
* Copyright (c) 2018-2020 Albert S. <mail at quitesimple dot org>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
@ -13,22 +13,23 @@
|
|||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <QProcess>
|
#include <QClipboard>
|
||||||
#include <QProcessEnvironment>
|
#include <QDate>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QDesktopServices>
|
||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
|
#include <QFileIconProvider>
|
||||||
|
#include <QHeaderView>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QKeySequence>
|
#include <QKeySequence>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QDate>
|
|
||||||
#include <QHeaderView>
|
|
||||||
#include <QDesktopServices>
|
|
||||||
#include <QFileIconProvider>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QClipboard>
|
#include <QProcess>
|
||||||
|
#include <QProcessEnvironment>
|
||||||
#include <QScrollArea>
|
#include <QScrollArea>
|
||||||
#include "window.h"
|
|
||||||
#include "entryprovider.h"
|
#include "entryprovider.h"
|
||||||
|
#include "window.h"
|
||||||
Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
|
Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
|
||||||
{
|
{
|
||||||
this->entryProvider = &entryProvider;
|
this->entryProvider = &entryProvider;
|
||||||
@ -36,6 +37,7 @@ Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
|
|||||||
createGui();
|
createGui();
|
||||||
initFromConfig();
|
initFromConfig();
|
||||||
this->lineEdit->installEventFilter(this);
|
this->lineEdit->installEventFilter(this);
|
||||||
|
this->setAcceptDrops(true);
|
||||||
QFont font;
|
QFont font;
|
||||||
font.setPointSize(48);
|
font.setPointSize(48);
|
||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
@ -43,13 +45,12 @@ Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
|
|||||||
calculationResultLabel.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
calculationResultLabel.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||||
calculationResultLabel.setAlignment(Qt::AlignCenter);
|
calculationResultLabel.setAlignment(Qt::AlignCenter);
|
||||||
calculationResultLabel.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
|
calculationResultLabel.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
|
||||||
connect(&calculationResultLabel, &QLabel::customContextMenuRequested, this, &Window::showCalculationResultContextMenu);
|
connect(&calculationResultLabel, &QLabel::customContextMenuRequested, this,
|
||||||
|
&Window::showCalculationResultContextMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
Window::~Window()
|
Window::~Window()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::initFromConfig()
|
void Window::initFromConfig()
|
||||||
@ -117,12 +118,65 @@ void Window::populateGrid(const QVector<EntryPushButton *> &list)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::buttonClick(const EntryPushButton &config)
|
void Window::executeConfig(const EntryConfig &config)
|
||||||
{
|
{
|
||||||
QProcess::startDetached(config.getCommand(), config.getArguments());
|
if(config.isTerminalCommand || QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier))
|
||||||
|
{
|
||||||
|
QString cmd = settingsProvider->getTerminalCommand();
|
||||||
|
cmd.replace("%c", config.command);
|
||||||
|
QStringList args = QProcess::splitCommand(cmd);
|
||||||
|
QProcess::startDetached(args[0], args);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QProcess::startDetached(config.command, config.arguments);
|
||||||
|
}
|
||||||
this->closeWindow();
|
this->closeWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::addToFavourites(const EntryConfig &config)
|
||||||
|
{
|
||||||
|
std::pair<int, int> cell = getNextFreeCell();
|
||||||
|
EntryConfig userConfig;
|
||||||
|
userConfig.type = EntryType::INHERIT;
|
||||||
|
userConfig.row = cell.first;
|
||||||
|
userConfig.col = cell.second;
|
||||||
|
userConfig.inherit = config.entryPath;
|
||||||
|
QFileInfo fi{config.entryPath};
|
||||||
|
QString entryName = fi.completeBaseName() + ".qsrun";
|
||||||
|
QString entryPath;
|
||||||
|
if(config.type == EntryType::SYSTEM)
|
||||||
|
{
|
||||||
|
entryPath = this->settingsProvider->userEntriesPaths()[0] + "/" + entryName;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
entryPath = fi.absoluteDir().absoluteFilePath(entryName);
|
||||||
|
}
|
||||||
|
userConfig.entryPath = entryPath;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
entryProvider->saveUserEntry(userConfig);
|
||||||
|
}
|
||||||
|
catch(std::exception &e)
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, "Failed to save item to favourites", e.what());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/*we only want to save a minimal, inherited config. but it should be a "complete" button
|
||||||
|
when we add it to the favourites. the alternative would be to reload the whole config,
|
||||||
|
but that's probably overkill. */
|
||||||
|
userConfig.update(config);
|
||||||
|
userConfig.key = "";
|
||||||
|
userEntryButtons.append(createEntryButton(userConfig));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::deleteEntry(EntryConfig &config)
|
||||||
|
{
|
||||||
|
this->entryProvider->deleteUserEntry(config);
|
||||||
|
initFromConfig();
|
||||||
|
}
|
||||||
|
|
||||||
void Window::closeWindow()
|
void Window::closeWindow()
|
||||||
{
|
{
|
||||||
if(settingsProvider->singleInstanceMode())
|
if(settingsProvider->singleInstanceMode())
|
||||||
@ -136,6 +190,48 @@ void Window::closeWindow()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<int, int> Window::getNextFreeCell()
|
||||||
|
{
|
||||||
|
/* Not the most efficient way perhaps but for now it'll do */
|
||||||
|
std::sort(userEntryButtons.begin(), userEntryButtons.end(), [](EntryPushButton *a, EntryPushButton *b) {
|
||||||
|
const EntryConfig &config_a = a->getEntryConfig();
|
||||||
|
const EntryConfig &config_b = b->getEntryConfig();
|
||||||
|
if(config_a.row < config_b.row)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(config_a.row > config_b.row)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return config_a.col < config_b.col;
|
||||||
|
});
|
||||||
|
|
||||||
|
int expectedRow = 1;
|
||||||
|
int expectedCol = 1;
|
||||||
|
int maxCols = this->settingsProvider->getMaxCols();
|
||||||
|
for(EntryPushButton *current : userEntryButtons)
|
||||||
|
{
|
||||||
|
int currentRow = current->getEntryConfig().row;
|
||||||
|
int currentCol = current->getEntryConfig().col;
|
||||||
|
|
||||||
|
if(currentRow != expectedRow || currentCol != expectedCol)
|
||||||
|
{
|
||||||
|
return {expectedRow, expectedCol};
|
||||||
|
}
|
||||||
|
if(expectedCol == maxCols)
|
||||||
|
{
|
||||||
|
expectedCol = 1;
|
||||||
|
++expectedRow;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++expectedCol;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {expectedRow, expectedCol};
|
||||||
|
}
|
||||||
|
|
||||||
QStringList Window::generatePATHSuggestions(const QString &text)
|
QStringList Window::generatePATHSuggestions(const QString &text)
|
||||||
{
|
{
|
||||||
QStringList results;
|
QStringList results;
|
||||||
@ -170,7 +266,8 @@ void Window::addPATHSuggestion(const QString &text)
|
|||||||
e.col = 0;
|
e.col = 0;
|
||||||
e.row = 0;
|
e.row = 0;
|
||||||
e.command = suggestions[0];
|
e.command = suggestions[0];
|
||||||
e.icon = QIcon::fromTheme(suggestions[0]);
|
e.iconPath = suggestions[0];
|
||||||
|
e.type = EntryType::DYNAMIC;
|
||||||
EntryPushButton *button = createEntryButton(e);
|
EntryPushButton *button = createEntryButton(e);
|
||||||
clearGrid();
|
clearGrid();
|
||||||
grid->addWidget(button, 0, 0);
|
grid->addWidget(button, 0, 0);
|
||||||
@ -198,7 +295,6 @@ void Window::addCalcResult(const QString &expression)
|
|||||||
calculationResultLabel.setText(labelText);
|
calculationResultLabel.setText(labelText);
|
||||||
calculationResultLabel.setVisible(true);
|
calculationResultLabel.setVisible(true);
|
||||||
|
|
||||||
|
|
||||||
QFont currentFont = calculationResultLabel.font();
|
QFont currentFont = calculationResultLabel.font();
|
||||||
int calculatedPointSize = currentFont.pointSize();
|
int calculatedPointSize = currentFont.pointSize();
|
||||||
QFontMetrics fm(currentFont);
|
QFontMetrics fm(currentFont);
|
||||||
@ -248,7 +344,8 @@ void Window::lineEditTextChanged(QString text)
|
|||||||
e.arguments = arguments.mid(1);
|
e.arguments = arguments.mid(1);
|
||||||
}
|
}
|
||||||
e.command = arguments[0];
|
e.command = arguments[0];
|
||||||
e.icon = QIcon::fromTheme("utilities-terminal");
|
e.iconPath = "utilities-terminal";
|
||||||
|
e.type = EntryType::DYNAMIC;
|
||||||
|
|
||||||
EntryPushButton *button = createEntryButton(e);
|
EntryPushButton *button = createEntryButton(e);
|
||||||
clearGrid();
|
clearGrid();
|
||||||
@ -268,11 +365,11 @@ void Window::keyReleaseEvent(QKeyEvent *event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
QWidget::keyReleaseEvent(event);
|
QWidget::keyReleaseEvent(event);
|
||||||
|
|
||||||
}
|
}
|
||||||
void Window::keyPressEvent(QKeyEvent *event)
|
void Window::keyPressEvent(QKeyEvent *event)
|
||||||
{
|
{
|
||||||
bool closeWindow = ((event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_Q) || event->key() == Qt::Key_Escape);
|
bool closeWindow =
|
||||||
|
((event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_Q) || event->key() == Qt::Key_Escape);
|
||||||
if(closeWindow)
|
if(closeWindow)
|
||||||
{
|
{
|
||||||
this->closeWindow();
|
this->closeWindow();
|
||||||
@ -287,22 +384,47 @@ void Window::keyPressEvent(QKeyEvent *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(EntryPushButton *button : buttonsInGrid)
|
for(EntryPushButton *button : buttonsInGrid)
|
||||||
|
{
|
||||||
|
if(!button->getEntryConfig().key.isEmpty())
|
||||||
{
|
{
|
||||||
button->showShortcut();
|
button->showShortcut();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QKeySequence seq(event->key());
|
QKeySequence seq(event->key());
|
||||||
QString key = seq.toString().toLower();
|
QString key = seq.toString().toLower();
|
||||||
|
|
||||||
auto it = std::find_if(buttonsInGrid.begin(), buttonsInGrid.end(), [&key](const EntryPushButton *y) { return y->getShortcutKey() == key; });
|
auto it = std::find_if(buttonsInGrid.begin(), buttonsInGrid.end(),
|
||||||
|
[&key](const EntryPushButton *y) { return y->getShortcutKey() == key; });
|
||||||
if(it != buttonsInGrid.end())
|
if(it != buttonsInGrid.end())
|
||||||
{
|
{
|
||||||
buttonClick(**it);
|
executeConfig((*it)->getEntryConfig());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QWidget::keyPressEvent(event);
|
QWidget::keyPressEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Window::rankConfig(const EntryConfig &config, QString filter) const
|
||||||
|
{
|
||||||
|
if(config.name.startsWith(filter, Qt::CaseInsensitive))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if(config.command.startsWith(filter, Qt::CaseInsensitive))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if(config.name.contains(filter, Qt::CaseInsensitive))
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else if(config.command.contains(filter, Qt::CaseInsensitive))
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
void Window::filterGridFor(QString filter)
|
void Window::filterGridFor(QString filter)
|
||||||
{
|
{
|
||||||
if(filter.length() > 0)
|
if(filter.length() > 0)
|
||||||
@ -311,7 +433,8 @@ void Window::filterGridFor(QString filter)
|
|||||||
bool userEntryMatch = false;
|
bool userEntryMatch = false;
|
||||||
for(EntryPushButton *button : this->userEntryButtons)
|
for(EntryPushButton *button : this->userEntryButtons)
|
||||||
{
|
{
|
||||||
if(button->getName().contains(filter, Qt::CaseInsensitive))
|
if(button->getName().contains(filter, Qt::CaseInsensitive) ||
|
||||||
|
button->getCommand().contains(filter, Qt::CaseInsensitive))
|
||||||
{
|
{
|
||||||
button->setVisible(true);
|
button->setVisible(true);
|
||||||
grid->addWidget(button, button->getRow(), button->getCol());
|
grid->addWidget(button, button->getRow(), button->getCol());
|
||||||
@ -321,13 +444,27 @@ void Window::filterGridFor(QString filter)
|
|||||||
}
|
}
|
||||||
if(!userEntryMatch)
|
if(!userEntryMatch)
|
||||||
{
|
{
|
||||||
|
QVector<RankedButton> rankedEntries;
|
||||||
int currow = 0;
|
int currow = 0;
|
||||||
int curcol = 0;
|
int curcol = 0;
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
const int MAX_COLS = this->settingsProvider->getMaxCols();
|
||||||
for(EntryPushButton *button : this->systemEntryButtons)
|
for(EntryPushButton *button : this->systemEntryButtons)
|
||||||
{
|
{
|
||||||
if(button->getName().contains(filter, Qt::CaseInsensitive))
|
int ranking = rankConfig(button->getEntryConfig(), filter);
|
||||||
|
if(ranking > -1)
|
||||||
{
|
{
|
||||||
|
RankedButton rb;
|
||||||
|
rb.button = button;
|
||||||
|
rb.ranking = ranking;
|
||||||
|
rankedEntries.append(rb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::sort(rankedEntries.begin(), rankedEntries.end(),
|
||||||
|
[](const RankedButton &a, const RankedButton &b) -> bool { return a.ranking < b.ranking; });
|
||||||
|
for(RankedButton &rankedButton : rankedEntries)
|
||||||
|
{
|
||||||
|
EntryPushButton *button = rankedButton.button;
|
||||||
button->setVisible(true);
|
button->setVisible(true);
|
||||||
if(i < 10)
|
if(i < 10)
|
||||||
{
|
{
|
||||||
@ -335,7 +472,7 @@ void Window::filterGridFor(QString filter)
|
|||||||
}
|
}
|
||||||
grid->addWidget(button, currow, curcol++);
|
grid->addWidget(button, currow, curcol++);
|
||||||
this->buttonsInGrid.append(button);
|
this->buttonsInGrid.append(button);
|
||||||
if(curcol == 3)
|
if(curcol == MAX_COLS)
|
||||||
{
|
{
|
||||||
curcol = 0;
|
curcol = 0;
|
||||||
++currow;
|
++currow;
|
||||||
@ -343,21 +480,18 @@ void Window::filterGridFor(QString filter)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
populateGrid(this->userEntryButtons);
|
populateGrid(this->userEntryButtons);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EntryPushButton *Window::createEntryButton(const EntryConfig &entry)
|
EntryPushButton *Window::createEntryButton(const EntryConfig &entry)
|
||||||
{
|
{
|
||||||
EntryPushButton *button = new EntryPushButton(entry);
|
EntryPushButton *button = new EntryPushButton(entry);
|
||||||
connect(button, &EntryPushButton::clicked, this, &Window::buttonClick);
|
connect(button, &EntryPushButton::clicked, this, &Window::executeConfig);
|
||||||
|
connect(button, &EntryPushButton::addToFavourites, this, &Window::addToFavourites);
|
||||||
|
connect(button, &EntryPushButton::deleteRequested, this, &Window::deleteEntry);
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,10 +504,9 @@ void Window::lineEditReturnPressed()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(buttonsInGrid.length() > 0 && this->lineEdit->text().length() > 0)
|
if(buttonsInGrid.length() > 0 && this->lineEdit->text().length() > 0)
|
||||||
{
|
{
|
||||||
buttonClick(*buttonsInGrid[0]);
|
executeConfig(buttonsInGrid[0]->getEntryConfig());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -401,7 +534,6 @@ bool Window::eventFilter(QObject *obj, QEvent *event)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return QObject::eventFilter(obj, event);
|
return QObject::eventFilter(obj, event);
|
||||||
}
|
}
|
||||||
@ -410,3 +542,49 @@ void Window::focusInput()
|
|||||||
{
|
{
|
||||||
this->lineEdit->setFocus();
|
this->lineEdit->setFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::dragEnterEvent(QDragEnterEvent *event)
|
||||||
|
{
|
||||||
|
if(event->mimeData()->hasFormat(ENTRYBUTTON_MIME_TYPE_STR))
|
||||||
|
{
|
||||||
|
event->acceptProposedAction();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::dropEvent(QDropEvent *event)
|
||||||
|
{
|
||||||
|
int count = grid->count();
|
||||||
|
for(int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
QLayoutItem *current = grid->itemAt(i);
|
||||||
|
if(current->geometry().contains(event->pos()))
|
||||||
|
{
|
||||||
|
EntryPushButton *buttonAtDrop = (EntryPushButton *)current->widget();
|
||||||
|
EntryPushButton *buttonAtSource = (EntryPushButton *)event->source();
|
||||||
|
|
||||||
|
int tmp_row = buttonAtSource->getRow();
|
||||||
|
int tmp_col = buttonAtSource->getCol();
|
||||||
|
|
||||||
|
grid->addWidget(buttonAtSource, buttonAtDrop->getRow(), buttonAtDrop->getCol());
|
||||||
|
buttonAtSource->setRow(buttonAtDrop->getRow());
|
||||||
|
buttonAtSource->setCol(buttonAtDrop->getCol());
|
||||||
|
|
||||||
|
grid->addWidget(buttonAtDrop, tmp_row, tmp_col);
|
||||||
|
buttonAtDrop->setRow(tmp_row);
|
||||||
|
buttonAtDrop->setCol(tmp_col);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this->entryProvider->saveUserEntry(buttonAtDrop->getEntryConfig());
|
||||||
|
this->entryProvider->saveUserEntry(buttonAtSource->getEntryConfig());
|
||||||
|
}
|
||||||
|
catch(std::exception &e)
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, "Failed to rearrange items", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
event->acceptProposedAction();
|
||||||
|
}
|
||||||
|
22
window.h
22
window.h
@ -30,13 +30,27 @@
|
|||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QTreeWidget>
|
#include <QTreeWidget>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QRect>
|
||||||
#include "entrypushbutton.h"
|
#include "entrypushbutton.h"
|
||||||
#include "calculationengine.h"
|
#include "calculationengine.h"
|
||||||
#include "settingsprovider.h"
|
#include "settingsprovider.h"
|
||||||
|
|
||||||
|
class RankedButton
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EntryPushButton *button = nullptr;
|
||||||
|
int ranking;
|
||||||
|
};
|
||||||
|
|
||||||
class Window : public QWidget
|
class Window : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
protected:
|
||||||
|
void dragEnterEvent(QDragEnterEvent *event);
|
||||||
|
void dropEvent(QDropEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EntryProvider *entryProvider;
|
EntryProvider *entryProvider;
|
||||||
SettingsProvider *settingsProvider;
|
SettingsProvider *settingsProvider;
|
||||||
@ -56,7 +70,9 @@ private:
|
|||||||
void keyReleaseEvent(QKeyEvent *event);
|
void keyReleaseEvent(QKeyEvent *event);
|
||||||
QVector<EntryPushButton *> generateEntryButtons(const QVector<EntryConfig> &userEntryButtons);
|
QVector<EntryPushButton *> generateEntryButtons(const QVector<EntryConfig> &userEntryButtons);
|
||||||
void keyPressEvent(QKeyEvent *event);
|
void keyPressEvent(QKeyEvent *event);
|
||||||
void buttonClick(const EntryPushButton &config);
|
void executeConfig(const EntryConfig &button);
|
||||||
|
void addToFavourites(const EntryConfig &button);
|
||||||
|
void deleteEntry(EntryConfig &config);
|
||||||
QLineEdit *lineEdit;
|
QLineEdit *lineEdit;
|
||||||
QGridLayout *grid;
|
QGridLayout *grid;
|
||||||
EntryPushButton *createEntryButton(const EntryConfig &config);
|
EntryPushButton *createEntryButton(const EntryConfig &config);
|
||||||
@ -67,16 +83,18 @@ private:
|
|||||||
void initTreeWidgets();
|
void initTreeWidgets();
|
||||||
QStringList generatePATHSuggestions(const QString &text);
|
QStringList generatePATHSuggestions(const QString &text);
|
||||||
void closeWindow();
|
void closeWindow();
|
||||||
|
std::pair<int, int> getNextFreeCell();
|
||||||
|
int rankConfig(const EntryConfig &config, QString filter) const;
|
||||||
private slots:
|
private slots:
|
||||||
void lineEditReturnPressed();
|
void lineEditReturnPressed();
|
||||||
void showCalculationResultContextMenu(const QPoint &point);
|
void showCalculationResultContextMenu(const QPoint &point);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider);
|
Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider);
|
||||||
void setSystemConfig(const QVector<EntryConfig> &config);
|
void setSystemConfig(const QVector<EntryConfig> &config);
|
||||||
bool eventFilter(QObject *obj, QEvent *event);
|
bool eventFilter(QObject *obj, QEvent *event);
|
||||||
void focusInput();
|
void focusInput();
|
||||||
~Window();
|
~Window();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user