2018-01-05 15:52:11 +01:00
|
|
|
/*
|
2020-09-07 23:16:45 +02:00
|
|
|
* Copyright (c) 2018-2020 Albert S. <mail at quitesimple dot org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QDate>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDesktopServices>
|
2018-01-03 09:52:05 +01:00
|
|
|
#include <QDirIterator>
|
2020-09-07 23:16:45 +02:00
|
|
|
#include <QFileIconProvider>
|
|
|
|
#include <QHeaderView>
|
2018-01-03 09:52:05 +01:00
|
|
|
#include <QIcon>
|
|
|
|
#include <QKeySequence>
|
|
|
|
#include <QLabel>
|
2018-06-29 18:16:14 +02:00
|
|
|
#include <QMenu>
|
2020-09-07 23:16:45 +02:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QProcessEnvironment>
|
2019-08-23 23:42:56 +02:00
|
|
|
#include <QScrollArea>
|
2020-09-07 23:16:45 +02:00
|
|
|
|
2020-09-06 19:43:17 +02:00
|
|
|
#include "entryprovider.h"
|
2020-09-07 23:29:10 +02:00
|
|
|
#include "window.h"
|
2020-09-06 19:43:17 +02:00
|
|
|
Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-06 19:43:17 +02:00
|
|
|
this->entryProvider = &entryProvider;
|
|
|
|
this->settingsProvider = &configProvider;
|
2018-01-03 09:52:05 +01:00
|
|
|
createGui();
|
2019-06-03 07:51:27 +02:00
|
|
|
initFromConfig();
|
2018-01-03 09:52:05 +01:00
|
|
|
this->lineEdit->installEventFilter(this);
|
2020-09-13 14:33:40 +02:00
|
|
|
this->setAcceptDrops(true);
|
2018-07-04 17:09:21 +02:00
|
|
|
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);
|
2020-09-07 23:16:45 +02:00
|
|
|
connect(&calculationResultLabel, &QLabel::customContextMenuRequested, this,
|
|
|
|
&Window::showCalculationResultContextMenu);
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2018-06-29 18:16:14 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 07:51:27 +02:00
|
|
|
void Window::initFromConfig()
|
|
|
|
{
|
2019-06-09 13:06:44 +02:00
|
|
|
try
|
|
|
|
{
|
2020-09-06 19:43:17 +02:00
|
|
|
this->userEntryButtons = generateEntryButtons(entryProvider->getUserEntries());
|
|
|
|
this->systemEntryButtons = generateEntryButtons(entryProvider->getSystemEntries());
|
2019-06-09 13:06:44 +02:00
|
|
|
}
|
|
|
|
catch(const ConfigFormatException &e)
|
|
|
|
{
|
|
|
|
qDebug() << "Config is misformated: " << e.what();
|
|
|
|
QMessageBox::critical(this, "Misformated config file", e.what());
|
|
|
|
qApp->quit();
|
|
|
|
}
|
|
|
|
|
2019-06-03 07:51:27 +02:00
|
|
|
populateGrid(this->userEntryButtons);
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
|
2018-07-04 17:09:21 +02:00
|
|
|
void Window::showCalculationResultContextMenu(const QPoint &point)
|
|
|
|
{
|
|
|
|
QMenu menu("Calc", this);
|
|
|
|
menu.addAction("Copy result", [&] { QGuiApplication::clipboard()->setText(currentCalculationResult); });
|
|
|
|
menu.addAction("Copy full content", [&] { QGuiApplication::clipboard()->setText(calculationResultLabel.text()); });
|
|
|
|
menu.exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
2020-09-07 23:16:45 +02:00
|
|
|
QVector<EntryPushButton *> Window::generateEntryButtons(const QVector<EntryConfig> &configs)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-07 23:16:45 +02:00
|
|
|
QVector<EntryPushButton *> result;
|
2018-01-03 09:52:05 +01:00
|
|
|
for(const EntryConfig &config : configs)
|
|
|
|
{
|
|
|
|
EntryPushButton *button = createEntryButton(config);
|
|
|
|
result.append(button);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::createGui()
|
|
|
|
{
|
2019-08-24 09:19:49 +02:00
|
|
|
QVBoxLayout *vbox = new QVBoxLayout(this);
|
|
|
|
QScrollArea *sa = new QScrollArea;
|
|
|
|
grid = new QGridLayout();
|
|
|
|
lineEdit = new QLineEdit();
|
|
|
|
QWidget *w = new QWidget(this);
|
|
|
|
w->setLayout(grid);
|
|
|
|
sa->setWidget(w);
|
|
|
|
sa->setWidgetResizable(true);
|
|
|
|
vbox->setAlignment(Qt::AlignTop);
|
|
|
|
vbox->addWidget(lineEdit);
|
|
|
|
vbox->addWidget(sa);
|
|
|
|
|
|
|
|
connect(lineEdit, &QLineEdit::textChanged, this, [this](QString newtext) { this->lineEditTextChanged(newtext); });
|
|
|
|
connect(lineEdit, &QLineEdit::returnPressed, this, &Window::lineEditReturnPressed);
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::populateGrid(const QVector<EntryPushButton *> &list)
|
|
|
|
{
|
|
|
|
clearGrid();
|
|
|
|
for(EntryPushButton *button : list)
|
|
|
|
{
|
|
|
|
button->setVisible(true);
|
|
|
|
grid->addWidget(button, button->getRow(), button->getCol());
|
|
|
|
buttonsInGrid.append(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 19:00:47 +02:00
|
|
|
void Window::executeConfig(const EntryConfig &config)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-10-04 22:16:49 +02:00
|
|
|
if(config.isTerminalCommand)
|
|
|
|
{
|
|
|
|
QString cmd = settingsProvider->getTerminalCommand();
|
|
|
|
cmd.replace("%c", config.command);
|
|
|
|
QProcess::startDetached(cmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QProcess::startDetached(config.command, config.arguments);
|
|
|
|
}
|
2019-09-01 19:46:38 +02:00
|
|
|
this->closeWindow();
|
|
|
|
}
|
|
|
|
|
2020-09-28 18:59:48 +02:00
|
|
|
void Window::addToFavourites(const EntryConfig &config)
|
2020-09-27 15:42:53 +02:00
|
|
|
{
|
|
|
|
std::pair<int, int> cell = getNextFreeCell();
|
2020-09-28 19:18:35 +02:00
|
|
|
EntryConfig userConfig;
|
2020-10-04 19:30:41 +02:00
|
|
|
userConfig.type = EntryType::INHERIT;
|
2020-09-27 15:42:53 +02:00
|
|
|
userConfig.row = cell.first;
|
|
|
|
userConfig.col = cell.second;
|
2020-10-04 19:30:41 +02:00
|
|
|
userConfig.inherit = config.entryPath;
|
|
|
|
QFileInfo fi{config.entryPath};
|
2020-09-27 15:42:53 +02:00
|
|
|
QString entryName = fi.completeBaseName() + ".qsrun";
|
2020-10-04 19:46:35 +02:00
|
|
|
QString entryPath;
|
|
|
|
if(config.type == EntryType::SYSTEM)
|
|
|
|
{
|
|
|
|
entryPath = this->settingsProvider->userEntriesPaths()[0] + "/" + entryName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entryPath = fi.absoluteDir().absoluteFilePath(entryName);
|
|
|
|
}
|
|
|
|
userConfig.entryPath = entryPath;
|
2020-09-27 22:09:51 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
entryProvider->saveUserEntry(userConfig);
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(this, "Failed to save item to favourites", e.what());
|
|
|
|
return;
|
|
|
|
}
|
2020-09-28 19:18:35 +02:00
|
|
|
/*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);
|
2020-10-19 21:36:37 +02:00
|
|
|
userConfig.key = "";
|
2020-09-27 21:37:59 +02:00
|
|
|
userEntryButtons.append(createEntryButton(userConfig));
|
2020-09-27 15:42:53 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 22:28:16 +02:00
|
|
|
void Window::deleteEntry(EntryConfig &config)
|
|
|
|
{
|
|
|
|
this->entryProvider->deleteUserEntry(config);
|
|
|
|
initFromConfig();
|
|
|
|
}
|
|
|
|
|
2019-09-01 19:46:38 +02:00
|
|
|
void Window::closeWindow()
|
|
|
|
{
|
2020-09-06 19:43:17 +02:00
|
|
|
if(settingsProvider->singleInstanceMode())
|
2019-06-03 07:51:27 +02:00
|
|
|
{
|
|
|
|
this->lineEdit->setText("");
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qApp->quit();
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-27 15:42:53 +02:00
|
|
|
std::pair<int, int> Window::getNextFreeCell()
|
|
|
|
{
|
2020-09-27 21:37:59 +02:00
|
|
|
/* 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)
|
2020-09-27 15:42:53 +02:00
|
|
|
{
|
2020-09-27 21:37:59 +02:00
|
|
|
return true;
|
2020-09-27 15:42:53 +02:00
|
|
|
}
|
2020-09-27 21:37:59 +02:00
|
|
|
if(config_a.row > config_b.row)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return config_a.col < config_b.col;
|
|
|
|
});
|
|
|
|
|
|
|
|
int expectedRow = 1;
|
|
|
|
int expectedCol = 1;
|
2020-09-27 15:42:53 +02:00
|
|
|
int maxCols = this->settingsProvider->getMaxCols();
|
2020-09-27 21:37:59 +02:00
|
|
|
for(EntryPushButton *current : userEntryButtons)
|
2020-09-27 15:42:53 +02:00
|
|
|
{
|
2020-09-27 21:37:59 +02:00
|
|
|
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
|
2020-09-27 15:42:53 +02:00
|
|
|
{
|
2020-09-27 21:37:59 +02:00
|
|
|
++expectedCol;
|
2020-09-27 15:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 21:37:59 +02:00
|
|
|
return {expectedRow, expectedCol};
|
2020-09-27 15:42:53 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 09:52:05 +01:00
|
|
|
QStringList Window::generatePATHSuggestions(const QString &text)
|
|
|
|
{
|
|
|
|
QStringList results;
|
|
|
|
QString pathVar = QProcessEnvironment::systemEnvironment().value("PATH", "/usr/bin/:/bin/:");
|
|
|
|
QStringList paths = pathVar.split(":");
|
|
|
|
for(const QString &path : paths)
|
|
|
|
{
|
|
|
|
QDirIterator it(path);
|
|
|
|
while(it.hasNext())
|
|
|
|
{
|
|
|
|
QFileInfo info(it.next());
|
|
|
|
if(info.isFile() && info.isExecutable())
|
|
|
|
{
|
|
|
|
QString entry = info.baseName();
|
|
|
|
if(entry.startsWith(text))
|
|
|
|
{
|
|
|
|
results.append(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::addPATHSuggestion(const QString &text)
|
|
|
|
{
|
|
|
|
QStringList suggestions = generatePATHSuggestions(text);
|
|
|
|
if(suggestions.length() == 1)
|
|
|
|
{
|
|
|
|
EntryConfig e;
|
|
|
|
e.name = suggestions[0];
|
2020-09-07 23:16:45 +02:00
|
|
|
e.col = 0;
|
|
|
|
e.row = 0;
|
2018-01-03 09:52:05 +01:00
|
|
|
e.command = suggestions[0];
|
2020-09-27 17:00:28 +02:00
|
|
|
e.iconPath = suggestions[0];
|
2018-01-03 09:52:05 +01:00
|
|
|
EntryPushButton *button = createEntryButton(e);
|
|
|
|
clearGrid();
|
|
|
|
grid->addWidget(button, 0, 0);
|
|
|
|
buttonsInGrid.append(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::clearGrid()
|
|
|
|
{
|
|
|
|
int count = grid->count();
|
|
|
|
for(int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
auto item = grid->itemAt(0)->widget();
|
|
|
|
grid->removeWidget(item);
|
|
|
|
item->setVisible(false);
|
|
|
|
}
|
|
|
|
buttonsInGrid.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::addCalcResult(const QString &expression)
|
|
|
|
{
|
|
|
|
clearGrid();
|
2018-07-04 17:09:21 +02:00
|
|
|
currentCalculationResult = calcEngine.evaluate(expression);
|
2019-06-01 12:17:28 +02:00
|
|
|
QString labelText = expression + ": " + currentCalculationResult;
|
|
|
|
calculationResultLabel.setText(labelText);
|
2018-07-04 17:09:21 +02:00
|
|
|
calculationResultLabel.setVisible(true);
|
2019-06-01 12:17:28 +02:00
|
|
|
|
|
|
|
QFont currentFont = calculationResultLabel.font();
|
|
|
|
int calculatedPointSize = currentFont.pointSize();
|
|
|
|
QFontMetrics fm(currentFont);
|
|
|
|
int contentWidth = calculationResultLabel.contentsRect().width() - calculationResultLabel.margin();
|
2019-09-30 21:58:11 +02:00
|
|
|
while(calculatedPointSize < 48 && fm.boundingRect(labelText).width() < contentWidth)
|
2019-06-01 12:17:28 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2018-07-04 17:09:21 +02:00
|
|
|
grid->addWidget(&calculationResultLabel, 0, 0);
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 23:16:45 +02:00
|
|
|
// main problem here there is no easy event compression (clearing emit queue and only processing the last one)
|
2018-01-03 09:52:05 +01:00
|
|
|
void Window::lineEditTextChanged(QString text)
|
|
|
|
{
|
|
|
|
if(text.length() >= 2)
|
|
|
|
{
|
|
|
|
QString input = text.mid(1);
|
|
|
|
if(text[0] == '=')
|
|
|
|
{
|
|
|
|
addCalcResult(input);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filterGridFor(text);
|
|
|
|
if(this->grid->count() == 0)
|
|
|
|
{
|
|
|
|
addPATHSuggestion(text);
|
2019-09-30 21:58:11 +02:00
|
|
|
if(this->grid->count() == 0)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
QStringList arguments = text.split(" ");
|
|
|
|
EntryConfig e;
|
|
|
|
e.name = "Execute: " + text;
|
|
|
|
if(arguments.length() > 1)
|
|
|
|
{
|
|
|
|
e.arguments = arguments.mid(1);
|
|
|
|
}
|
|
|
|
e.command = arguments[0];
|
2020-09-27 17:00:28 +02:00
|
|
|
e.iconPath = "utilities-terminal";
|
2019-08-24 09:19:49 +02:00
|
|
|
|
2018-01-03 09:52:05 +01:00
|
|
|
EntryPushButton *button = createEntryButton(e);
|
|
|
|
clearGrid();
|
|
|
|
grid->addWidget(button, 0, 0);
|
|
|
|
buttonsInGrid.append(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
if(event->key() == Qt::Key_Control)
|
|
|
|
{
|
|
|
|
for(EntryPushButton *button : buttonsInGrid)
|
|
|
|
{
|
|
|
|
button->showName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QWidget::keyReleaseEvent(event);
|
|
|
|
}
|
|
|
|
void Window::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
2020-09-07 23:16:45 +02:00
|
|
|
bool closeWindow =
|
|
|
|
((event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_Q) || event->key() == Qt::Key_Escape);
|
2019-09-01 19:46:38 +02:00
|
|
|
if(closeWindow)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-09-01 19:46:38 +02:00
|
|
|
this->closeWindow();
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2019-08-24 09:19:49 +02:00
|
|
|
|
2018-01-03 09:52:05 +01:00
|
|
|
if(event->modifiers() & Qt::ControlModifier && buttonsInGrid.count() > 0)
|
|
|
|
{
|
|
|
|
if(event->key() == Qt::Key_L)
|
|
|
|
{
|
|
|
|
this->lineEdit->setFocus();
|
|
|
|
this->lineEdit->selectAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(EntryPushButton *button : buttonsInGrid)
|
|
|
|
{
|
2020-10-10 22:25:15 +02:00
|
|
|
if(!button->getEntryConfig().key.isEmpty())
|
|
|
|
{
|
|
|
|
button->showShortcut();
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QKeySequence seq(event->key());
|
|
|
|
QString key = seq.toString().toLower();
|
2019-08-24 09:19:49 +02:00
|
|
|
|
2020-09-07 23:16:45 +02:00
|
|
|
auto it = std::find_if(buttonsInGrid.begin(), buttonsInGrid.end(),
|
|
|
|
[&key](const EntryPushButton *y) { return y->getShortcutKey() == key; });
|
2019-06-01 13:38:58 +02:00
|
|
|
if(it != buttonsInGrid.end())
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-28 19:00:47 +02:00
|
|
|
executeConfig((*it)->getEntryConfig());
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::filterGridFor(QString filter)
|
|
|
|
{
|
|
|
|
if(filter.length() > 0)
|
|
|
|
{
|
|
|
|
clearGrid();
|
|
|
|
bool userEntryMatch = false;
|
|
|
|
for(EntryPushButton *button : this->userEntryButtons)
|
|
|
|
{
|
|
|
|
if(button->getName().contains(filter, Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
button->setVisible(true);
|
|
|
|
grid->addWidget(button, button->getRow(), button->getCol());
|
|
|
|
this->buttonsInGrid.append(button);
|
|
|
|
userEntryMatch = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!userEntryMatch)
|
|
|
|
{
|
|
|
|
int currow = 0;
|
|
|
|
int curcol = 0;
|
|
|
|
int i = 1;
|
2020-09-07 23:29:10 +02:00
|
|
|
const int MAX_COLS = this->settingsProvider->getMaxCols();
|
2018-01-03 09:52:05 +01:00
|
|
|
for(EntryPushButton *button : this->systemEntryButtons)
|
|
|
|
{
|
|
|
|
if(button->getName().contains(filter, Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
button->setVisible(true);
|
2019-06-01 13:38:58 +02:00
|
|
|
if(i < 10)
|
|
|
|
{
|
|
|
|
button->setShortcutKey(QString::number(i++));
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
grid->addWidget(button, currow, curcol++);
|
|
|
|
this->buttonsInGrid.append(button);
|
2020-09-07 23:29:10 +02:00
|
|
|
if(curcol == MAX_COLS)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
curcol = 0;
|
|
|
|
++currow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-24 09:19:49 +02:00
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
populateGrid(this->userEntryButtons);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 23:16:45 +02:00
|
|
|
EntryPushButton *Window::createEntryButton(const EntryConfig &entry)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
EntryPushButton *button = new EntryPushButton(entry);
|
2020-09-28 19:00:47 +02:00
|
|
|
connect(button, &EntryPushButton::clicked, this, &Window::executeConfig);
|
2020-09-27 15:42:53 +02:00
|
|
|
connect(button, &EntryPushButton::addToFavourites, this, &Window::addToFavourites);
|
2020-09-27 22:28:16 +02:00
|
|
|
connect(button, &EntryPushButton::deleteRequested, this, &Window::deleteEntry);
|
2018-01-03 09:52:05 +01:00
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::lineEditReturnPressed()
|
|
|
|
{
|
2019-06-03 07:51:27 +02:00
|
|
|
if(this->lineEdit->text() == "/reload")
|
|
|
|
{
|
|
|
|
initFromConfig();
|
|
|
|
this->lineEdit->setText("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-07 23:16:45 +02:00
|
|
|
if(buttonsInGrid.length() > 0 && this->lineEdit->text().length() > 0)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-28 19:00:47 +02:00
|
|
|
executeConfig(buttonsInGrid[0]->getEntryConfig());
|
2018-01-03 09:52:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setSystemConfig(const QVector<EntryConfig> &config)
|
|
|
|
{
|
|
|
|
this->systemEntryButtons = generateEntryButtons(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
|
|
|
if(obj == this->lineEdit)
|
|
|
|
{
|
2020-09-07 23:16:45 +02:00
|
|
|
if(event->type() == QEvent::KeyPress)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
|
|
|
|
if(keyEvent->key() == Qt::Key_Tab)
|
|
|
|
{
|
|
|
|
QStringList suggestions = generatePATHSuggestions(this->lineEdit->text());
|
|
|
|
if(suggestions.length() == 1)
|
|
|
|
{
|
|
|
|
this->lineEdit->setText(suggestions[0] + " ");
|
2020-09-07 23:16:45 +02:00
|
|
|
this->lineEdit->setCursorPosition(this->lineEdit->text().length() + 1);
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QObject::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:42:39 +02:00
|
|
|
void Window::focusInput()
|
|
|
|
{
|
|
|
|
this->lineEdit->setFocus();
|
2019-09-30 21:58:11 +02:00
|
|
|
}
|
2020-09-13 14:33:40 +02:00
|
|
|
|
|
|
|
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);
|
2020-09-13 21:08:17 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this->entryProvider->saveUserEntry(buttonAtDrop->getEntryConfig());
|
|
|
|
this->entryProvider->saveUserEntry(buttonAtSource->getEntryConfig());
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(this, "Failed to rearrange items", e.what());
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:33:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|