3 次程式碼提交

共有 15 個文件被更改,包括 119 次插入287 次删除

查看文件

@ -13,11 +13,6 @@ 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;
@ -25,7 +20,7 @@ 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 std::runtime_error("Failed to open file"); throw new 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.
@ -120,7 +115,7 @@ EntryConfig EntryProvider::readqsrunFile(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 std::runtime_error("Failed to open file"); throw new std::runtime_error("Failed to open file");
} }
QHash<QString, QString> map; QHash<QString, QString> map;
QTextStream stream(&file); QTextStream stream(&file);
@ -131,7 +126,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
int spacePos = line.indexOf(' '); int spacePos = line.indexOf(' ');
if(spacePos == -1) if(spacePos == -1)
{ {
throw ConfigFormatException("misformated line in .qsrun config file " + path.toStdString()); throw new ConfigFormatException("misformated line in .qsrun config file " + path.toStdString());
} }
QString key = line.mid(0, spacePos); QString key = line.mid(0, spacePos);
@ -139,7 +134,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
if(key == "" || value == "") if(key == "" || value == "")
{ {
throw ConfigFormatException("empty key or value in .qsrun config file " + path.toStdString()); throw new ConfigFormatException("empty key or value in .qsrun config file " + path.toStdString());
} }
map[key] = value; map[key] = value;
} }
@ -154,7 +149,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
} }
else else
{ {
throw ConfigFormatException("Error attempting to read inherited entry"); throw new ConfigFormatException("Error attempting to read inherited entry");
} }
} }
QString type = map["type"]; QString type = map["type"];
@ -162,7 +157,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
{ {
if(type == "system") if(type == "system")
{ {
throw ConfigFormatException(".qsrun files cannot be designated as system entries " + throw new ConfigFormatException(".qsrun files cannot be designated as system entries " +
path.toStdString()); path.toStdString());
} }
else if(type == "inherit") else if(type == "inherit")
@ -175,7 +170,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
} }
else else
{ {
throw ConfigFormatException("Invalid value for type provided in file: " + path.toStdString()); throw new ConfigFormatException("Invalid value for type provided in file: " + path.toStdString());
} }
} }
else else
@ -290,7 +285,7 @@ QVector<EntryConfig> EntryProvider::getSystemEntries()
void EntryProvider::saveUserEntry(const EntryConfig &config) void EntryProvider::saveUserEntry(const EntryConfig &config)
{ {
if(!isSavable(config)) if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty())
{ {
throw std::runtime_error("Only user/inherited entries can be saved"); throw std::runtime_error("Only user/inherited entries can be saved");
} }
@ -301,35 +296,32 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
throw std::runtime_error("Error: Can not open file for writing"); throw std::runtime_error("Error: Can not open file for writing");
} }
QTextStream outStream(&file); QTextStream outStream(&file);
outStream << "type" << " " << ((config.type == EntryType::USER) ? "user" : "inherit") << Qt::endl; outStream << "type" << " " << ((config.type == EntryType::USER) ? "user" : "inherit") << endl;
if(!config.inherit.isEmpty()) if(!config.inherit.isEmpty())
{ {
outStream << "inherit" << " " << config.inherit << Qt::endl; outStream << "inherit" << " " << config.inherit << 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;
} }
outStream << "row" << " " << config.row << endl;
outStream << "col" << " " << config.col << endl;
outStream << "hidden" << " " << config.hidden << endl;
outStream << "key" << " " << config.key << endl;
if(config.type == EntryType::USER) if(config.type == EntryType::USER)
{ {
if(!config.name.isEmpty()) if(!config.name.isEmpty())
{ {
outStream << "name" << " " << config.name << Qt::endl; outStream << "name" << " " << config.name << endl;
} }
if(!config.command.isEmpty()) if(!config.command.isEmpty())
{ {
outStream << "command" << " " << config.command << Qt::endl; outStream << "command" << " " << config.command << endl;
} }
if(!config.iconPath.isEmpty()) if(!config.iconPath.isEmpty())
{ {
outStream << "icon" << " " << config.iconPath << Qt::endl; outStream << "icon" << " " << config.iconPath << endl;
} }
if(!config.arguments.empty()) if(!config.arguments.empty())
{ {
outStream << "arguments" << " " << config.arguments.join(' ') << Qt::endl; outStream << "arguments" << " " << config.arguments.join(' ') << endl;
} }
} }
@ -347,7 +339,7 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
bool EntryProvider::deleteUserEntry(const EntryConfig &config) bool EntryProvider::deleteUserEntry(const EntryConfig &config)
{ {
if(!isSavable(config)) if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty())
{ {
throw std::runtime_error("Only user/inherited entries can be deleted"); throw std::runtime_error("Only user/inherited entries can be deleted");
} }

查看文件

@ -19,8 +19,7 @@ enum EntryType
{ {
USER, USER,
INHERIT, INHERIT,
SYSTEM, SYSTEM
DYNAMIC
}; };
class EntryConfig class EntryConfig
@ -56,7 +55,6 @@ class EntryProvider
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); void saveUserEntry(const EntryConfig &config);

查看文件

@ -33,7 +33,19 @@ EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
icon = resolveIcon(config.iconPath); icon = resolveIcon(config.iconPath);
} }
this->setIcon(icon); this->setIcon(icon);
this->setIconSize(QSize{256, 256}); if(!icon.availableSizes().isEmpty())
{
auto sizes = icon.availableSizes();
QSize maxSize = sizes.first();
for(QSize &current : sizes)
{
if(current.width() > maxSize.width())
{
maxSize = current;
}
}
this->setIconSize(maxSize);
}
this->config = config; this->config = config;
connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked())); connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked()));
@ -94,7 +106,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event)
{ {
this->userEntryMenu.exec(QCursor::pos()); this->userEntryMenu.exec(QCursor::pos());
} }
else if(this->config.type == EntryType::SYSTEM) else
{ {
this->systemEntryMenu.exec(QCursor::pos()); this->systemEntryMenu.exec(QCursor::pos());
} }
@ -104,7 +116,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event)
void EntryPushButton::mouseMoveEvent(QMouseEvent *event) void EntryPushButton::mouseMoveEvent(QMouseEvent *event)
{ {
if(this->config.type == EntryType::SYSTEM || this->config.type == EntryType::DYNAMIC) if(this->config.type == EntryType::SYSTEM)
{ {
return; return;
} }

查看文件

@ -14,7 +14,6 @@
* 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>
@ -31,76 +30,56 @@ 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)
{ {
QCommandLineParser parser; configDirectoryPath = QCoreApplication::arguments().at(1);
parser.addOptions({ if(!dir.exists(configDirectoryPath))
{"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;
} }
} }
if(configDirectoryPath.isEmpty()) else
{ {
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", QMessageBox::warning(nullptr, "Failed to create dir", configDirectoryPath + " was not found and could not be created!");
configDirectoryPath + " was not found and could not be created!");
return 1; return 1;
} }
} }
QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat); QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat);
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; QLocalSocket localSocket;
localSocket.connectToServer("/tmp/qsrun.socket");
bool singleInstanceMode = !newInstanceRequested && settingsProvider.singleInstanceMode(); SingleInstanceServer server;
if(singleInstanceMode) if(localSocket.isOpen() && localSocket.isWritable())
{ {
QLocalSocket localSocket; QDataStream stream(&localSocket);
localSocket.connectToServer(settingsProvider.socketPath()); stream << (int)0x01; //maximize
if(localSocket.isOpen() && localSocket.isWritable()) localSocket.flush();
{ localSocket.waitForBytesWritten();
QDataStream stream(&localSocket); localSocket.disconnectFromServer();
stream << (int)0x01; // maximize return 0;
localSocket.flush(); }
localSocket.waitForBytesWritten(); else
localSocket.disconnectFromServer(); {
return 0; if(!server.listen("/tmp/qsrun.socket"))
}
server = new SingleInstanceServer();
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 };
QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w]{
Window *w = new Window{entryProvider, settingsProvider};
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";
@ -110,10 +89,12 @@ int main(int argc, char *argv[])
w->focusInput(); w->focusInput();
} }
}); });
w->showMaximized();
w->focusInput();
} }
w->showMaximized();
w->focusInput();
return app.exec(); return app.exec();
} }

查看文件

@ -12,8 +12,6 @@ HEADERS += calculationengine.h \
entrypushbutton.h \ entrypushbutton.h \
settingsprovider.h \ settingsprovider.h \
singleinstanceserver.h \ singleinstanceserver.h \
specialcommandconfig.h \
textoutputlabel.h \
window.h window.h
SOURCES += calculationengine.cpp \ SOURCES += calculationengine.cpp \
entryprovider.cpp \ entryprovider.cpp \
@ -21,7 +19,6 @@ SOURCES += calculationengine.cpp \
main.cpp \ main.cpp \
settingsprovider.cpp \ settingsprovider.cpp \
singleinstanceserver.cpp \ singleinstanceserver.cpp \
textoutputlabel.cpp \
window.cpp window.cpp
QT += widgets sql network QT += widgets sql network
QT_CONFIG -= no-pkg-config QT_CONFIG -= no-pkg-config

Binary file not shown.

Before

Width:  |  Height:  |  大小: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  大小: 48 KiB

二進制
screenshots/startview.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  大小: 112 KiB

Binary file not shown.

Before

Width:  |  Height:  |  大小: 205 KiB

查看文件

@ -33,33 +33,3 @@ QString SettingsProvider::getTerminalCommand() const
{ {
return settings->value("terminal", "/usr/bin/x-terminal-emulator -e %c").toString(); return settings->value("terminal", "/usr/bin/x-terminal-emulator -e %c").toString();
} }
QString SettingsProvider::socketPath() const
{
return settings->value("singleInstanceSocket", "/tmp/qsrun").toString();
}
QVector<SpecialCommandConfig> SettingsProvider::specialCommands() const
{
QVector<SpecialCommandConfig> result;
SpecialCommandConfig uname;
uname.command = "uname";
uname.reqArgCount = 0;
uname.immediateProcessing = true;
result.append(uname);
SpecialCommandConfig date;
date.command = "date";
date.reqArgCount = 0;
date.immediateProcessing = true;
result.append(date);
SpecialCommandConfig echo;
echo.command = "echo";
echo.reqArgCount = 0;
echo.immediateProcessing = true;
result.append(echo);
return result;
}

查看文件

@ -3,7 +3,6 @@
#include <QSettings> #include <QSettings>
#include <stdexcept> #include <stdexcept>
#include "specialcommandconfig.h"
class SettingsProvider class SettingsProvider
{ {
@ -17,8 +16,6 @@ class SettingsProvider
virtual int getMaxCols() const; virtual int getMaxCols() const;
virtual bool singleInstanceMode() const; virtual bool singleInstanceMode() const;
QString getTerminalCommand() const; QString getTerminalCommand() const;
QString socketPath() const;
QVector<SpecialCommandConfig> specialCommands() const;
}; };
#endif // SETTINGSPROVIDER_H #endif // SETTINGSPROVIDER_H

查看文件

@ -1,37 +0,0 @@
#include "textoutputlabel.h"
TextoutputLabel::TextoutputLabel()
{
QFont font;
font.setPointSize(48);
font.setBold(true);
this->setFont(font);
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setAlignment(Qt::AlignCenter);
this->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
}
void TextoutputLabel::setText(const QString &text)
{
QLabel::setText(text);
QFont currentFont = this->font();
int calculatedPointSize = currentFont.pointSize();
QFontMetrics fm(currentFont);
int contentWidth = this->contentsRect().width() - this->margin();
while(calculatedPointSize < 48 && fm.boundingRect(this->text()).width() < contentWidth)
{
calculatedPointSize += 1;
currentFont.setPointSize(calculatedPointSize);
fm = QFontMetrics(currentFont);
}
while(fm.boundingRect(this->text()).width() >= contentWidth)
{
calculatedPointSize -= 1;
currentFont.setPointSize(calculatedPointSize);
fm = QFontMetrics(currentFont);
}
this->setFont(currentFont);
}

查看文件

@ -1,14 +0,0 @@
#ifndef TEXTOUTPUTLABEL_H
#define TEXTOUTPUTLABEL_H
#include <QLabel>
class TextoutputLabel : public QLabel
{
public:
TextoutputLabel();
virtual void setText(const QString &text);
};
#endif // TEXTOUTPUTLABEL_H

查看文件

@ -30,7 +30,6 @@
#include "entryprovider.h" #include "entryprovider.h"
#include "window.h" #include "window.h"
Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider) Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
{ {
this->entryProvider = &entryProvider; this->entryProvider = &entryProvider;
@ -39,7 +38,13 @@ Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
initFromConfig(); initFromConfig();
this->lineEdit->installEventFilter(this); this->lineEdit->installEventFilter(this);
this->setAcceptDrops(true); this->setAcceptDrops(true);
QFont font;
font.setPointSize(48);
font.setBold(true);
calculationResultLabel.setFont(font);
calculationResultLabel.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
calculationResultLabel.setAlignment(Qt::AlignCenter);
calculationResultLabel.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(&calculationResultLabel, &QLabel::customContextMenuRequested, this, connect(&calculationResultLabel, &QLabel::customContextMenuRequested, this,
&Window::showCalculationResultContextMenu); &Window::showCalculationResultContextMenu);
} }
@ -54,7 +59,6 @@ void Window::initFromConfig()
{ {
this->userEntryButtons = generateEntryButtons(entryProvider->getUserEntries()); this->userEntryButtons = generateEntryButtons(entryProvider->getUserEntries());
this->systemEntryButtons = generateEntryButtons(entryProvider->getSystemEntries()); this->systemEntryButtons = generateEntryButtons(entryProvider->getSystemEntries());
this->specialCommands = settingsProvider->specialCommands();
} }
catch(const ConfigFormatException &e) catch(const ConfigFormatException &e)
{ {
@ -116,12 +120,11 @@ void Window::populateGrid(const QVector<EntryPushButton *> &list)
void Window::executeConfig(const EntryConfig &config) void Window::executeConfig(const EntryConfig &config)
{ {
if(config.isTerminalCommand || QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) if(config.isTerminalCommand)
{ {
QString cmd = settingsProvider->getTerminalCommand(); QString cmd = settingsProvider->getTerminalCommand();
cmd.replace("%c", config.command); cmd.replace("%c", config.command + " " + config.arguments.join(' '));
QStringList args = QProcess::splitCommand(cmd); QProcess::startDetached(cmd);
QProcess::startDetached(args[0], args);
} }
else else
{ {
@ -163,7 +166,6 @@ void Window::addToFavourites(const EntryConfig &config)
when we add it to the favourites. the alternative would be to reload the whole config, when we add it to the favourites. the alternative would be to reload the whole config,
but that's probably overkill. */ but that's probably overkill. */
userConfig.update(config); userConfig.update(config);
userConfig.key = "";
userEntryButtons.append(createEntryButton(userConfig)); userEntryButtons.append(createEntryButton(userConfig));
} }
@ -263,7 +265,6 @@ void Window::addPATHSuggestion(const QString &text)
e.row = 0; e.row = 0;
e.command = suggestions[0]; e.command = suggestions[0];
e.iconPath = 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);
@ -283,22 +284,34 @@ void Window::clearGrid()
buttonsInGrid.clear(); buttonsInGrid.clear();
} }
void Window::showGrowingOutputText(QString text)
{
clearGrid();
calculationResultLabel.setText(text);
calculationResultLabel.setVisible(true);
grid->addWidget(&calculationResultLabel, 0, 0);
}
void Window::addCalcResult(const QString &expression) void Window::addCalcResult(const QString &expression)
{ {
clearGrid();
currentCalculationResult = calcEngine.evaluate(expression); currentCalculationResult = calcEngine.evaluate(expression);
QString labelText = expression + ": " + currentCalculationResult; QString labelText = expression + ": " + currentCalculationResult;
showGrowingOutputText(labelText); calculationResultLabel.setText(labelText);
calculationResultLabel.setVisible(true);
QFont currentFont = calculationResultLabel.font();
int calculatedPointSize = currentFont.pointSize();
QFontMetrics fm(currentFont);
int contentWidth = calculationResultLabel.contentsRect().width() - calculationResultLabel.margin();
while(calculatedPointSize < 48 && fm.boundingRect(labelText).width() < contentWidth)
{
calculatedPointSize += 1;
currentFont.setPointSize(calculatedPointSize);
fm = QFontMetrics(currentFont);
}
while(fm.boundingRect(labelText).width() >= contentWidth)
{
calculatedPointSize -= 1;
currentFont.setPointSize(calculatedPointSize);
fm = QFontMetrics(currentFont);
}
calculationResultLabel.setFont(currentFont);
grid->addWidget(&calculationResultLabel, 0, 0);
} }
// main problem here there is no easy event compression (clearing emit queue and only processing the last one) // main problem here there is no easy event compression (clearing emit queue and only processing the last one)
@ -320,20 +333,15 @@ void Window::lineEditTextChanged(QString text)
addPATHSuggestion(text); addPATHSuggestion(text);
if(this->grid->count() == 0) if(this->grid->count() == 0)
{ {
QStringList arguments = QProcess::splitCommand(text); QStringList arguments = text.split(" ");
QString command = arguments[0];
auto specialCommandConfig = getSpecialCommandConfig(command);
if(specialCommandConfig)
{
executeSpecialCommand(specialCommandConfig.value(), arguments);
return;
}
EntryConfig e; EntryConfig e;
e.name = "Execute: " + text; e.name = "Execute: " + text;
e.command = command; if(arguments.length() > 1)
e.arguments = arguments; {
e.arguments = arguments.mid(1);
}
e.command = arguments[0];
e.iconPath = "utilities-terminal"; e.iconPath = "utilities-terminal";
e.type = EntryType::DYNAMIC;
EntryPushButton *button = createEntryButton(e); EntryPushButton *button = createEntryButton(e);
clearGrid(); clearGrid();
@ -373,10 +381,7 @@ 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());
@ -392,49 +397,6 @@ void Window::keyPressEvent(QKeyEvent *event)
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;
}
std::optional<SpecialCommandConfig> Window::getSpecialCommandConfig(QString cmd) const
{
SpecialCommandConfig result;
for(const SpecialCommandConfig &config : this->specialCommands)
{
if(config.command == cmd)
{
return config;
}
}
return { };
}
void Window::executeSpecialCommand(const SpecialCommandConfig &config, QStringList arguments)
{
QProcess process;
process.start(config.command, arguments.mid(1));
process.waitForFinished();
QString result = process.readAllStandardOutput();
showGrowingOutputText(result);
}
void Window::filterGridFor(QString filter) void Window::filterGridFor(QString filter)
{ {
if(filter.length() > 0) if(filter.length() > 0)
@ -443,8 +405,7 @@ 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());
@ -454,38 +415,26 @@ 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(); const int MAX_COLS = this->settingsProvider->getMaxCols();
for(EntryPushButton *button : this->systemEntryButtons) for(EntryPushButton *button : this->systemEntryButtons)
{ {
int ranking = rankConfig(button->getEntryConfig(), filter); if(button->getName().contains(filter, Qt::CaseInsensitive))
if(ranking > -1)
{ {
RankedButton rb; button->setVisible(true);
rb.button = button; if(i < 10)
rb.ranking = ranking; {
rankedEntries.append(rb); button->setShortcutKey(QString::number(i++));
} }
} grid->addWidget(button, currow, curcol++);
std::sort(rankedEntries.begin(), rankedEntries.end(), this->buttonsInGrid.append(button);
[](const RankedButton &a, const RankedButton &b) -> bool { return a.ranking < b.ranking; }); if(curcol == MAX_COLS)
for(RankedButton &rankedButton : rankedEntries) {
{ curcol = 0;
EntryPushButton *button = rankedButton.button; ++currow;
button->setVisible(true); }
if(i < 10)
{
button->setShortcutKey(QString::number(i++));
}
grid->addWidget(button, currow, curcol++);
this->buttonsInGrid.append(button);
if(curcol == MAX_COLS)
{
curcol = 0;
++currow;
} }
} }
} }

查看文件

@ -36,15 +36,6 @@
#include "entrypushbutton.h" #include "entrypushbutton.h"
#include "calculationengine.h" #include "calculationengine.h"
#include "settingsprovider.h" #include "settingsprovider.h"
#include "specialcommandconfig.h"
#include "textoutputlabel.h"
class RankedButton
{
public:
EntryPushButton *button = nullptr;
int ranking;
};
class Window : public QWidget class Window : public QWidget
{ {
@ -61,8 +52,7 @@ class Window : public QWidget
QVector<EntryPushButton *> userEntryButtons; QVector<EntryPushButton *> userEntryButtons;
QVector<EntryPushButton *> systemEntryButtons; QVector<EntryPushButton *> systemEntryButtons;
QVector<EntryPushButton *> buttonsInGrid; QVector<EntryPushButton *> buttonsInGrid;
QVector<SpecialCommandConfig> specialCommands; QLabel calculationResultLabel;
TextoutputLabel calculationResultLabel;
QString currentCalculationResult; QString currentCalculationResult;
QString queuedFileSearch; QString queuedFileSearch;
QString queuedContentSearch; QString queuedContentSearch;
@ -87,11 +77,8 @@ class Window : public QWidget
QStringList generatePATHSuggestions(const QString &text); QStringList generatePATHSuggestions(const QString &text);
void closeWindow(); void closeWindow();
std::pair<int, int> getNextFreeCell(); std::pair<int, int> getNextFreeCell();
int rankConfig(const EntryConfig &config, QString filter) const;
std::optional<SpecialCommandConfig> getSpecialCommandConfig(QString cmd) const; private slots:
void executeSpecialCommand(const SpecialCommandConfig &config, QStringList arguments);
void showGrowingOutputText(QString text);
private slots:
void lineEditReturnPressed(); void lineEditReturnPressed();
void showCalculationResultContextMenu(const QPoint &point); void showCalculationResultContextMenu(const QPoint &point);