From 6b924c0f539fb20f1991ea1957871e49a1ad4a7c Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 27 Sep 2020 15:42:53 +0200 Subject: [PATCH] Implement addToFavourites() and getNextFreeCell() --- entrypushbutton.cpp | 2 +- entrypushbutton.h | 2 +- window.cpp | 38 ++++++++++++++++++++++++++++++++++++++ window.h | 3 +++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/entrypushbutton.cpp b/entrypushbutton.cpp index 7eb682e..36cac1c 100644 --- a/entrypushbutton.cpp +++ b/entrypushbutton.cpp @@ -49,7 +49,7 @@ void EntryPushButton::emitOwnClicked() emit clicked(this->config); } -const EntryConfig &EntryPushButton::getEntryConfig() +const EntryConfig &EntryPushButton::getEntryConfig() const { return this->config; } diff --git a/entrypushbutton.h b/entrypushbutton.h index edd40eb..d8655e2 100644 --- a/entrypushbutton.h +++ b/entrypushbutton.h @@ -48,7 +48,7 @@ class EntryPushButton : public QPushButton public: EntryPushButton(const EntryConfig &config); - const EntryConfig &getEntryConfig(); + const EntryConfig &getEntryConfig() const; void setEntryConfig(const EntryConfig &config); void showShortcut(); void showName(); diff --git a/window.cpp b/window.cpp index 04e96ce..7f32440 100644 --- a/window.cpp +++ b/window.cpp @@ -124,6 +124,22 @@ void Window::buttonClick(const EntryPushButton &config) this->closeWindow(); } +void Window::addToFavourites(const EntryPushButton &button) +{ + std::pair 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() { if(settingsProvider->singleInstanceMode()) @@ -137,6 +153,27 @@ void Window::closeWindow() } } +std::pair 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 results; @@ -356,6 +393,7 @@ EntryPushButton *Window::createEntryButton(const EntryConfig &entry) { EntryPushButton *button = new EntryPushButton(entry); connect(button, &EntryPushButton::clicked, this, &Window::buttonClick); + connect(button, &EntryPushButton::addToFavourites, this, &Window::addToFavourites); return button; } diff --git a/window.h b/window.h index 8ef965c..95964a1 100644 --- a/window.h +++ b/window.h @@ -64,6 +64,7 @@ class Window : public QWidget QVector generateEntryButtons(const QVector &userEntryButtons); void keyPressEvent(QKeyEvent *event); void buttonClick(const EntryPushButton &config); + void addToFavourites(const EntryPushButton &button); QLineEdit *lineEdit; QGridLayout *grid; EntryPushButton *createEntryButton(const EntryConfig &config); @@ -74,6 +75,8 @@ class Window : public QWidget void initTreeWidgets(); QStringList generatePATHSuggestions(const QString &text); void closeWindow(); + std::pair getNextFreeCell(); + private slots: void lineEditReturnPressed(); void showCalculationResultContextMenu(const QPoint &point);