Implement addToFavourites() and getNextFreeCell()

This commit is contained in:
Albert S. 2020-09-27 15:42:53 +02:00
parent 653089c475
commit 6b924c0f53
4 changed files with 43 additions and 2 deletions

View File

@ -49,7 +49,7 @@ 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;
} }

View File

@ -48,7 +48,7 @@ class EntryPushButton : public QPushButton
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();

View File

@ -124,6 +124,22 @@ void Window::buttonClick(const EntryPushButton &config)
this->closeWindow(); this->closeWindow();
} }
void Window::addToFavourites(const EntryPushButton &button)
{
std::pair<int, int> cell = getNextFreeCell();
EntryConfig userConfig = button.getEntryConfig();
userConfig.userEntry = true;
userConfig.icon = QIcon();
userConfig.row = cell.first;
userConfig.col = cell.second;
userConfig.inherit = userConfig.entryPath;
QFileInfo fi{userConfig.entryPath};
QString entryName = fi.completeBaseName() + ".qsrun";
userConfig.entryPath = this->settingsProvider->userEntriesPaths()[0] + "/" + entryName;
entryProvider->saveUserEntry(userConfig);
initFromConfig();
}
void Window::closeWindow() void Window::closeWindow()
{ {
if(settingsProvider->singleInstanceMode()) if(settingsProvider->singleInstanceMode())
@ -137,6 +153,27 @@ void Window::closeWindow()
} }
} }
std::pair<int, int> Window::getNextFreeCell()
{
int maxRow = 0;
for(EntryPushButton *button : userEntryButtons)
{
if(button->getRow() > maxRow)
{
maxRow = button->getRow();
}
}
int maxCols = this->settingsProvider->getMaxCols();
for(int i = 1; i < maxCols; i++)
{
if(grid->itemAtPosition(maxRow, i) == 0)
{
return {maxRow, i + 1};
}
}
return {maxRow + 1, 0};
}
QStringList Window::generatePATHSuggestions(const QString &text) QStringList Window::generatePATHSuggestions(const QString &text)
{ {
QStringList results; QStringList results;
@ -356,6 +393,7 @@ 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::buttonClick);
connect(button, &EntryPushButton::addToFavourites, this, &Window::addToFavourites);
return button; return button;
} }

View File

@ -64,6 +64,7 @@ class Window : public QWidget
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 buttonClick(const EntryPushButton &config);
void addToFavourites(const EntryPushButton &button);
QLineEdit *lineEdit; QLineEdit *lineEdit;
QGridLayout *grid; QGridLayout *grid;
EntryPushButton *createEntryButton(const EntryConfig &config); EntryPushButton *createEntryButton(const EntryConfig &config);
@ -74,6 +75,8 @@ class Window : public QWidget
void initTreeWidgets(); void initTreeWidgets();
QStringList generatePATHSuggestions(const QString &text); QStringList generatePATHSuggestions(const QString &text);
void closeWindow(); void closeWindow();
std::pair<int, int> getNextFreeCell();
private slots: private slots:
void lineEditReturnPressed(); void lineEditReturnPressed();
void showCalculationResultContextMenu(const QPoint &point); void showCalculationResultContextMenu(const QPoint &point);