Implement addToFavourites() and getNextFreeCell()

This commit is contained in:
Albert S. 2020-09-27 15:42:53 +02:00
rodzic 653089c475
commit 6b924c0f53
4 zmienionych plików z 43 dodań i 2 usunięć

Wyświetl plik

@ -49,7 +49,7 @@ void EntryPushButton::emitOwnClicked()
emit clicked(this->config);
}
const EntryConfig &EntryPushButton::getEntryConfig()
const EntryConfig &EntryPushButton::getEntryConfig() const
{
return this->config;
}

Wyświetl plik

@ -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();

Wyświetl plik

@ -124,6 +124,22 @@ void Window::buttonClick(const EntryPushButton &config)
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()
{
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 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;
}

Wyświetl plik

@ -64,6 +64,7 @@ class Window : public QWidget
QVector<EntryPushButton *> generateEntryButtons(const QVector<EntryConfig> &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<int, int> getNextFreeCell();
private slots:
void lineEditReturnPressed();
void showCalculationResultContextMenu(const QPoint &point);