1
0

Comparar comentimentos

..

Não há cometimentos em comum. "776cbe4d3096ff0b2278682d1149e01b581c07b8" e "b588fd07be537f2db7d1c97966b50d2f71bba872" têm históricos completamente diferentes.

6 ficheiros modificados com 79 adições e 442 eliminações

Ver ficheiro

@ -56,7 +56,7 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
}
if(key == "icon")
{
result.iconPath = args;
result.icon = QIcon::fromTheme(args);
}
if(key == "exec")
{
@ -80,7 +80,6 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
result.hidden = args == "true";
}
}
result.type = EntryType::SYSTEM;
return result;
}
@ -113,71 +112,19 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
// TODO: better exception class
throw new std::runtime_error("Failed to open file");
}
QHash<QString, QString> map;
QTextStream stream(&file);
while(!stream.atEnd())
{
QString line = stream.readLine();
int spacePos = line.indexOf(' ');
if(spacePos == -1)
QStringList splitted = line.split(" ");
if(splitted.length() < 2)
{
throw new ConfigFormatException("misformated line in .qsrun config file " + path.toStdString());
}
QString key = line.mid(0, spacePos);
QString value = line.mid(spacePos + 1);
if(key == "" || value == "")
QString key = splitted[0];
if(key == "arguments")
{
throw new 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 new ConfigFormatException("Error attempting to read inherited entry");
}
}
QString type = map["type"];
if(!type.isEmpty())
{
if(type == "system")
{
throw new 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 new 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(' ');
auto args = splitted.mid(1);
QString merged;
for(QString &str : args)
{
@ -206,20 +153,42 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
throw ConfigFormatException("non-closed \" in config file " + path.toStdString());
}
}
auto assignIfSourceNotEmpty = [](QString source, QString &val) {
if(!source.isEmpty())
if(key == "name")
{
result.name = splitted.mid(1).join(' ');
}
if(key == "icon")
{
result.icon = QIcon(splitted[1]);
}
if(key == "row")
{
result.row = splitted[1].toInt();
}
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")
{
auto entry = readEntryFromPath(resolveEntryPath(splitted[1]));
if(entry)
{
val = source;
inheritedConfig = *entry;
}
};
assignIfSourceNotEmpty(map["key"].toLower(), result.key);
assignIfSourceNotEmpty(map["command"], result.command);
assignIfSourceNotEmpty(map["icon"], result.iconPath);
assignIfSourceNotEmpty(map["name"], result.name);
}
}
result.col = map["col"].toInt();
result.row = map["row"].toInt();
return result;
return result.update(inheritedConfig);
}
QString EntryProvider::resolveEntryPath(QString path)
@ -259,7 +228,6 @@ QVector<EntryConfig> EntryProvider::readConfig(QStringList paths)
{
if(!entry->hidden)
{
entry->entryPath = path;
result.append(*entry);
}
}
@ -278,70 +246,6 @@ QVector<EntryConfig> EntryProvider::getSystemEntries()
return readConfig(this->systemEntriesDirsPaths);
}
void EntryProvider::saveUserEntry(const EntryConfig &config)
{
if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty())
{
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") << endl;
if(!config.inherit.isEmpty())
{
outStream << "inherit" << " " << config.inherit << 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.name.isEmpty())
{
outStream << "name" << " " << config.name << endl;
}
if(!config.command.isEmpty())
{
outStream << "command" << " " << config.command << endl;
}
if(!config.iconPath.isEmpty())
{
outStream << "icon" << " " << config.iconPath << endl;
}
if(!config.arguments.empty())
{
outStream << "arguments" << " " << config.arguments.join(' ') << 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(config.type == EntryType::SYSTEM || config.entryPath.isEmpty())
{
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)
{
if(dest == T())
@ -355,16 +259,12 @@ EntryConfig &EntryConfig::update(const EntryConfig &o)
assignIfDestDefault(this->arguments, o.arguments);
assignIfDestDefault(this->col, o.col);
assignIfDestDefault(this->command, o.command);
if(this->iconPath.isEmpty())
if(this->icon.isNull())
{
this->iconPath = o.iconPath;
this->icon = o.icon;
}
assignIfDestDefault(this->key, o.key);
assignIfDestDefault(this->name, o.name);
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;
}

Ver ficheiro

@ -15,25 +15,15 @@ class ConfigFormatException : public std::runtime_error
}
};
enum EntryType
{
USER,
INHERIT,
SYSTEM
};
class EntryConfig
{
public:
EntryType type = SYSTEM;
bool hidden = false;
QString entryPath;
QString key;
QString name;
QString command;
QString iconPath;
QStringList arguments;
QString inherit;
QIcon icon;
int row = 0;
int col = 0;
@ -56,8 +46,6 @@ class EntryProvider
EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths);
QVector<EntryConfig> getUserEntries();
QVector<EntryConfig> getSystemEntries();
void saveUserEntry(const EntryConfig &config);
bool deleteUserEntry(const EntryConfig &config);
};
#endif // ENTRYPROVIDER_H

Ver ficheiro

@ -13,21 +13,16 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <QDrag>
#include <QMimeData>
#include <QApplication>
#include "entrypushbutton.h"
EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
{
this->setText(config.name);
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QIcon icon = resolveIcon(config.iconPath);
this->setIcon(icon);
if(!icon.availableSizes().isEmpty())
this->setIcon(config.icon);
if(!config.icon.availableSizes().isEmpty())
{
auto sizes = icon.availableSizes();
auto sizes = config.icon.availableSizes();
QSize maxSize = sizes.first();
for(QSize &current : sizes)
{
@ -40,33 +35,17 @@ EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
}
this->config = config;
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()
{
emit clicked(this->config);
}
const EntryConfig &EntryPushButton::getEntryConfig() const
const EntryConfig &EntryPushButton::getEntryConfig()
{
return this->config;
}
@ -86,82 +65,12 @@ void EntryPushButton::showName()
this->setText(this->config.name);
}
void EntryPushButton::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
dragStartPosition = event->pos();
}
if(event->button() == Qt::RightButton)
{
if(this->config.type == EntryType::USER || this->config.type == EntryType::INHERIT)
{
this->userEntryMenu.exec(QCursor::pos());
}
else
{
this->systemEntryMenu.exec(QCursor::pos());
}
}
return QPushButton::mousePressEvent(event);
}
void EntryPushButton::mouseMoveEvent(QMouseEvent *event)
{
if(this->config.type == EntryType::SYSTEM)
{
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;
}
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; }

Ver ficheiro

@ -18,38 +18,20 @@
#include <QWidget>
#include <QPushButton>
#include <QMouseEvent>
#include <QMenu>
#include "entryprovider.h"
#define ENTRYBUTTON_MIME_TYPE_STR "application/x-qsrun-entrypushbutton"
class EntryPushButton : public QPushButton
{
Q_OBJECT
private:
QMenu systemEntryMenu;
QMenu userEntryMenu;
private:
EntryConfig config;
QPoint dragStartPosition;
private slots:
private slots:
void emitOwnClicked();
signals:
signals:
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);
const EntryConfig &getEntryConfig() const;
const EntryConfig &getEntryConfig();
void setEntryConfig(const EntryConfig &config);
void showShortcut();
void showName();
@ -64,4 +46,6 @@ class EntryPushButton : public QPushButton
void setShortcutKey(QString key);
};
#endif // ENTRYPUSHBUTTON_H

Ver ficheiro

@ -37,7 +37,6 @@ Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
createGui();
initFromConfig();
this->lineEdit->installEventFilter(this);
this->setAcceptDrops(true);
QFont font;
font.setPointSize(48);
font.setBold(true);
@ -118,54 +117,12 @@ void Window::populateGrid(const QVector<EntryPushButton *> &list)
}
}
void Window::executeConfig(const EntryConfig &config)
void Window::buttonClick(const EntryPushButton &config)
{
QProcess::startDetached(config.command, config.arguments);
QProcess::startDetached(config.getCommand(), config.getArguments());
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);
userEntryButtons.append(createEntryButton(userConfig));
}
void Window::deleteEntry(EntryConfig &config)
{
this->entryProvider->deleteUserEntry(config);
initFromConfig();
}
void Window::closeWindow()
{
if(settingsProvider->singleInstanceMode())
@ -179,48 +136,6 @@ 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 results;
@ -255,7 +170,7 @@ void Window::addPATHSuggestion(const QString &text)
e.col = 0;
e.row = 0;
e.command = suggestions[0];
e.iconPath = suggestions[0];
e.icon = QIcon::fromTheme(suggestions[0]);
EntryPushButton *button = createEntryButton(e);
clearGrid();
grid->addWidget(button, 0, 0);
@ -332,7 +247,7 @@ void Window::lineEditTextChanged(QString text)
e.arguments = arguments.mid(1);
}
e.command = arguments[0];
e.iconPath = "utilities-terminal";
e.icon = QIcon::fromTheme("utilities-terminal");
EntryPushButton *button = createEntryButton(e);
clearGrid();
@ -382,7 +297,7 @@ void Window::keyPressEvent(QKeyEvent *event)
[&key](const EntryPushButton *y) { return y->getShortcutKey() == key; });
if(it != buttonsInGrid.end())
{
executeConfig((*it)->getEntryConfig());
buttonClick(**it);
}
}
QWidget::keyPressEvent(event);
@ -439,9 +354,7 @@ void Window::filterGridFor(QString filter)
EntryPushButton *Window::createEntryButton(const EntryConfig &entry)
{
EntryPushButton *button = new EntryPushButton(entry);
connect(button, &EntryPushButton::clicked, this, &Window::executeConfig);
connect(button, &EntryPushButton::addToFavourites, this, &Window::addToFavourites);
connect(button, &EntryPushButton::deleteRequested, this, &Window::deleteEntry);
connect(button, &EntryPushButton::clicked, this, &Window::buttonClick);
return button;
}
@ -456,7 +369,7 @@ void Window::lineEditReturnPressed()
if(buttonsInGrid.length() > 0 && this->lineEdit->text().length() > 0)
{
executeConfig(buttonsInGrid[0]->getEntryConfig());
buttonClick(*buttonsInGrid[0]);
return;
}
}
@ -492,49 +405,3 @@ void Window::focusInput()
{
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();
}

Ver ficheiro

@ -30,9 +30,6 @@
#include <QThread>
#include <QTreeWidget>
#include <QLabel>
#include <QMimeData>
#include <QDebug>
#include <QRect>
#include "entrypushbutton.h"
#include "calculationengine.h"
#include "settingsprovider.h"
@ -40,17 +37,13 @@
class Window : public QWidget
{
Q_OBJECT
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
private:
private:
EntryProvider *entryProvider;
SettingsProvider *settingsProvider;
CalculationEngine calcEngine;
QString calculationresult;
QVector<EntryPushButton *> userEntryButtons;
QVector<EntryPushButton *> systemEntryButtons;
QVector<EntryPushButton*> userEntryButtons;
QVector<EntryPushButton*> systemEntryButtons;
QVector<EntryPushButton *> buttonsInGrid;
QLabel calculationResultLabel;
QString currentCalculationResult;
@ -63,31 +56,27 @@ class Window : public QWidget
void keyReleaseEvent(QKeyEvent *event);
QVector<EntryPushButton *> generateEntryButtons(const QVector<EntryConfig> &userEntryButtons);
void keyPressEvent(QKeyEvent *event);
void executeConfig(const EntryConfig &button);
void addToFavourites(const EntryConfig &button);
void deleteEntry(EntryConfig &config);
void buttonClick(const EntryPushButton &config);
QLineEdit *lineEdit;
QGridLayout *grid;
EntryPushButton *createEntryButton(const EntryConfig &config);
void lineEditTextChanged(QString text);
void addPATHSuggestion(const QString &text);
void clearGrid();
void addCalcResult(const QString &expression);
void addCalcResult(const QString & expression);
void initTreeWidgets();
QStringList generatePATHSuggestions(const QString &text);
void closeWindow();
std::pair<int, int> getNextFreeCell();
private slots:
private slots:
void lineEditReturnPressed();
void showCalculationResultContextMenu(const QPoint &point);
public:
public:
Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider);
void setSystemConfig(const QVector<EntryConfig> &config);
bool eventFilter(QObject *obj, QEvent *event);
void focusInput();
~Window();
};
#endif