| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "RecentImagesMenu.h" |
| 21 | |
| 22 | |
| 23 | RecentImagesMenu::RecentImagesMenu(const QSharedPointer<IRecentImageService> &recentImageService, QWidget *parent) : |
| 24 | QMenu(parent), |
| 25 | mRecentImageService(recentImageService) |
| 26 | { |
| 27 | connect(this, &QMenu::aboutToShow, this, &RecentImagesMenu::populateMenu); |
| 28 | } |
| 29 | |
| 30 | void RecentImagesMenu::populateMenu() |
| 31 | { |
| 32 | clear(); |
| 33 | |
| 34 | const auto recentImages = mRecentImageService->getRecentImagesPath(); |
| 35 | |
| 36 | auto imageIndex = 0; |
| 37 | for(const auto& path : recentImages) { |
| 38 | auto action = createAction(path, imageIndex++); |
| 39 | addAction(action); |
| 40 | } |
| 41 | |
| 42 | setEnabled(!recentImages.isEmpty()); |
| 43 | } |
| 44 | |
| 45 | QAction *RecentImagesMenu::createAction(const QString &path, int index) |
| 46 | { |
| 47 | auto action = new QAction(this); |
| 48 | action->setText(path); |
| 49 | action->setShortcut(Qt::CTRL + Qt::Key_0 + index); |
| 50 | |
| 51 | connect(action, &QAction::triggered, this, [this, path](){ |
| 52 | emit openRecentSelected(path); |
| 53 | }); |
| 54 | |
| 55 | return action; |
| 56 | } |