| 1 | /* |
| 2 | * Copyright (C) 2018 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 "CaptureModePicker.h" |
| 21 | |
| 22 | CaptureModePicker::CaptureModePicker(const QList<CaptureModes> &captureModes, const QSharedPointer<IIconLoader> &iconLoader) |
| 23 | { |
| 24 | init(captureModes, iconLoader); |
| 25 | |
| 26 | setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 27 | setButtonText(tr("New")); |
| 28 | } |
| 29 | |
| 30 | void CaptureModePicker::setCaptureMode(CaptureModes mode) |
| 31 | { |
| 32 | for(auto action : mCaptureActions) { |
| 33 | if (action->data().value<CaptureModes>() == mode) { |
| 34 | setDefaultAction(action); |
| 35 | mSelectedCaptureMode = mode; |
| 36 | return; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | CaptureModes CaptureModePicker::captureMode() const |
| 42 | { |
| 43 | return mSelectedCaptureMode; |
| 44 | } |
| 45 | |
| 46 | void CaptureModePicker::init(const QList<CaptureModes> &captureModes, const QSharedPointer<IIconLoader> &iconLoader) |
| 47 | { |
| 48 | auto menu = new CustomMenu(); |
| 49 | |
| 50 | if (isCaptureModeSupported(captureModes, CaptureModes::RectArea)) { |
| 51 | auto action = createAction( |
| 52 | EnumTranslator::instance()->toTranslatedString(CaptureModes::RectArea), |
| 53 | tr("Draw a rectangular area with your mouse"), |
| 54 | iconLoader->loadForTheme(QLatin1String("drawRect.svg")), |
| 55 | CaptureModes::RectArea, |
| 56 | QKeySequence(Qt::SHIFT | Qt::Key_R)); |
| 57 | menu->addAction(action); |
| 58 | } |
| 59 | |
| 60 | if (isCaptureModeSupported(captureModes, CaptureModes::LastRectArea)) { |
| 61 | auto action = createAction( |
| 62 | EnumTranslator::instance()->toTranslatedString(CaptureModes::LastRectArea), |
| 63 | tr("Capture a screenshot of the last selected rectangular area"), |
| 64 | iconLoader->loadForTheme(QLatin1String("lastRect.svg")), |
| 65 | CaptureModes::LastRectArea, |
| 66 | QKeySequence(Qt::SHIFT | Qt::Key_L)); |
| 67 | menu->addAction(action); |
| 68 | } |
| 69 | |
| 70 | if (isCaptureModeSupported(captureModes, CaptureModes::FullScreen)) { |
| 71 | auto action = createAction( |
| 72 | EnumTranslator::instance()->toTranslatedString(CaptureModes::FullScreen), |
| 73 | tr("Capture full screen including all monitors"), |
| 74 | iconLoader->loadForTheme(QLatin1String("fullScreen.svg")), |
| 75 | CaptureModes::FullScreen, |
| 76 | QKeySequence(Qt::SHIFT | Qt::Key_F)); |
| 77 | menu->addAction(action); |
| 78 | } |
| 79 | |
| 80 | if (isCaptureModeSupported(captureModes, CaptureModes::CurrentScreen)) { |
| 81 | auto action = createAction( |
| 82 | EnumTranslator::instance()->toTranslatedString(CaptureModes::CurrentScreen), |
| 83 | tr("Capture screen where the mouse is located"), |
| 84 | iconLoader->loadForTheme(QLatin1String("currentScreen.svg")), |
| 85 | CaptureModes::CurrentScreen, |
| 86 | QKeySequence(Qt::SHIFT | Qt::Key_M)); |
| 87 | menu->addAction(action); |
| 88 | } |
| 89 | |
| 90 | if (isCaptureModeSupported(captureModes, CaptureModes::ActiveWindow)) { |
| 91 | auto action = createAction( |
| 92 | EnumTranslator::instance()->toTranslatedString(CaptureModes::ActiveWindow), |
| 93 | tr("Capture window that currently has focus"), |
| 94 | iconLoader->loadForTheme(QLatin1String("activeWindow.svg")), |
| 95 | CaptureModes::ActiveWindow, |
| 96 | QKeySequence(Qt::SHIFT | Qt::Key_A)); |
| 97 | menu->addAction(action); |
| 98 | } |
| 99 | |
| 100 | if (isCaptureModeSupported(captureModes, CaptureModes::WindowUnderCursor)) { |
| 101 | auto action = createAction( |
| 102 | EnumTranslator::instance()->toTranslatedString(CaptureModes::WindowUnderCursor), |
| 103 | tr("Capture that is currently under the mouse cursor"), |
| 104 | iconLoader->loadForTheme(QLatin1String("windowUnderCursor.svg")), |
| 105 | CaptureModes::WindowUnderCursor, |
| 106 | QKeySequence(Qt::SHIFT | Qt::Key_U)); |
| 107 | menu->addAction(action); |
| 108 | } |
| 109 | |
| 110 | if (isCaptureModeSupported(captureModes, CaptureModes::Portal)) { |
| 111 | auto action = createAction( |
| 112 | EnumTranslator::instance()->toTranslatedString(CaptureModes::Portal), |
| 113 | tr("Uses the screenshot Portal for taking screenshot"), |
| 114 | iconLoader->loadForTheme(QLatin1String("wayland.svg")), |
| 115 | CaptureModes::Portal, |
| 116 | QKeySequence(Qt::SHIFT | Qt::Key_T)); |
| 117 | menu->addAction(action); |
| 118 | } |
| 119 | |
| 120 | if (!mCaptureActions.isEmpty()) { |
| 121 | setDefaultAction(mCaptureActions.first()); |
| 122 | } |
| 123 | setMenu(menu); |
| 124 | } |
| 125 | |
| 126 | bool CaptureModePicker::isCaptureModeSupported(const QList<CaptureModes> &captureModes, CaptureModes captureMode) |
| 127 | { |
| 128 | return captureModes.contains(captureMode); |
| 129 | } |
| 130 | |
| 131 | QAction *CaptureModePicker::createAction(const QString &text, const QString &tooltip, const QIcon &icon, CaptureModes captureMode, const QKeySequence &shortcut) |
| 132 | { |
| 133 | auto action = new QAction(this); |
| 134 | action->setIconText(text); |
| 135 | action->setToolTip(tooltip); |
| 136 | action->setIcon(icon); |
| 137 | action->setShortcut(shortcut); |
| 138 | action->setData(static_cast<int>(captureMode)); |
| 139 | connect(action, &QAction::triggered, [this, captureMode]() { selectCaptureMode(captureMode); } ); |
| 140 | mCaptureActions.append(action); |
| 141 | return action; |
| 142 | } |
| 143 | |
| 144 | void CaptureModePicker::selectCaptureMode(CaptureModes mode) |
| 145 | { |
| 146 | mSelectedCaptureMode = mode; |
| 147 | emit captureModeSelected(mode); |
| 148 | } |
| 149 | |
| 150 | QList<QAction*> CaptureModePicker::captureActions() const |
| 151 | { |
| 152 | return mCaptureActions; |
| 153 | } |