| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic> |
| 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 | |
| 21 | #include "MainToolBar.h" |
| 22 | |
| 23 | MainToolBar::MainToolBar( |
| 24 | const QList<CaptureModes> &captureModes, |
| 25 | QAction* undoAction, |
| 26 | QAction* redoAction, |
| 27 | const QSharedPointer<IIconLoader> &iconLoader, |
| 28 | const QSharedPointer<IScaledSizeProvider> &scaledSizeProvider) : |
| 29 | QToolBar(), |
| 30 | mSaveButton(new QToolButton(this)), |
| 31 | mCopyButton(new QToolButton(this)), |
| 32 | mCropButton(new QToolButton(this)), |
| 33 | mUndoButton(new QToolButton(this)), |
| 34 | mRedoButton(new QToolButton(this)), |
| 35 | mCaptureModePicker(new CaptureModePicker(captureModes, iconLoader)), |
| 36 | mDelayPicker(new CustomSpinBox(0,100)), |
| 37 | mDelayLabel(new QLabel(this)), |
| 38 | mNewCaptureAction(new QAction(this)), |
| 39 | mSaveAction(new QAction(this)), |
| 40 | mCopyAction(new QAction(this)), |
| 41 | mCropAction(new QAction(this)), |
| 42 | mUndoAction(undoAction), |
| 43 | mRedoAction(redoAction) |
| 44 | { |
| 45 | connect(mCaptureModePicker, &CaptureModePicker::captureModeSelected, this, &MainToolBar::captureModeSelected); |
| 46 | |
| 47 | setStyleSheet(QLatin1String("QToolBar { border: 0px }")); |
| 48 | |
| 49 | mNewCaptureAction->setText(tr("New")); |
| 50 | mNewCaptureAction->setShortcut(QKeySequence::New); |
| 51 | connect(mNewCaptureAction, &QAction::triggered, this, &MainToolBar::newCaptureTriggered); |
| 52 | |
| 53 | mSaveButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 54 | mSaveButton->addAction(mSaveAction); |
| 55 | mSaveButton->setDefaultAction(mSaveAction); |
| 56 | |
| 57 | mCopyButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 58 | mCopyButton->addAction(mCopyAction); |
| 59 | mCopyButton->setDefaultAction(mCopyAction); |
| 60 | |
| 61 | mUndoButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 62 | mUndoButton->addAction(mUndoAction); |
| 63 | mUndoButton->setDefaultAction(mUndoAction); |
| 64 | |
| 65 | mRedoButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 66 | mRedoButton->addAction(mRedoAction); |
| 67 | mRedoButton->setDefaultAction(mRedoAction); |
| 68 | |
| 69 | mCropButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 70 | mCropButton->addAction(mCropAction); |
| 71 | mCropButton->setDefaultAction(mCropAction); |
| 72 | |
| 73 | auto clockIcon = iconLoader->loadForTheme(QLatin1String("clock.svg")); |
| 74 | auto clockPixmap = clockIcon.pixmap(scaledSizeProvider->scaledSize(QSize(24, 24))); |
| 75 | mDelayLabel->setPixmap(clockPixmap); |
| 76 | mDelayLabel->setContentsMargins(0, 0, 2, 0); |
| 77 | mDelayLabel->setToolTip(tr("Delay in seconds between triggering\n" |
| 78 | "and capturing screenshot.")); |
| 79 | //: The small letter s stands for seconds. |
| 80 | mDelayPicker->setSuffix(tr("s")); |
| 81 | mDelayPicker->setFixedWidth(scaledSizeProvider->scaledWidth(55)); |
| 82 | mDelayPicker->setToolTip(mDelayLabel->toolTip()); |
| 83 | connect(mDelayPicker, &CustomSpinBox::valueChanged, this, &MainToolBar::captureDelayChanged); |
| 84 | |
| 85 | mSaveAction->setText(tr("Save")); |
| 86 | mSaveAction->setToolTip(tr("Save Screen Capture to file system")); |
| 87 | mSaveAction->setIcon(iconLoader->loadForTheme(QLatin1String("save.svg"))); |
| 88 | mSaveAction->setShortcut(QKeySequence::Save); |
| 89 | connect(mSaveAction, &QAction::triggered, this, &MainToolBar::saveActionTriggered); |
| 90 | |
| 91 | mCopyAction->setText(tr("Copy")); |
| 92 | mCopyAction->setToolTip(tr("Copy Screen Capture to clipboard")); |
| 93 | mCopyAction->setIcon(iconLoader->loadForTheme(QLatin1String("copy.svg"))); |
| 94 | mCopyAction->setShortcut(QKeySequence::Copy); |
| 95 | connect(mCopyAction, &QAction::triggered, this, &MainToolBar::copyActionTriggered); |
| 96 | |
| 97 | mUndoAction->setIcon(iconLoader->loadForTheme(QLatin1String("undo.svg")));; |
| 98 | mUndoAction->setText(tr("Undo")); |
| 99 | mUndoAction->setShortcut(QKeySequence::Undo); |
| 100 | |
| 101 | mRedoAction->setIcon(iconLoader->loadForTheme(QLatin1String("redo.svg"))); |
| 102 | mRedoAction->setText(tr("Redo")); |
| 103 | mRedoAction->setShortcut(QKeySequence::Redo); |
| 104 | |
| 105 | mCropAction->setText(tr("Crop")); |
| 106 | mCropAction->setToolTip(tr("Crop Screen Capture")); |
| 107 | mCropAction->setIcon(iconLoader->loadForTheme(QLatin1String("crop.svg"))); |
| 108 | mCropAction->setShortcut(Qt::SHIFT + Qt::Key_C); |
| 109 | connect(mCropAction, &QAction::triggered, this, &MainToolBar::cropActionTriggered); |
| 110 | |
| 111 | setWindowTitle(tr("Tools")); |
| 112 | setFloatable(false); |
| 113 | setMovable(false); |
| 114 | setAllowedAreas(Qt::BottomToolBarArea); |
| 115 | addWidget(mCaptureModePicker); |
| 116 | addSeparator(); |
| 117 | addWidget(mSaveButton); |
| 118 | addWidget(mCopyButton); |
| 119 | addWidget(mUndoButton); |
| 120 | addWidget(mRedoButton); |
| 121 | addSeparator(); |
| 122 | addWidget(mCropButton); |
| 123 | addSeparator(); |
| 124 | addWidget(mDelayLabel); |
| 125 | addWidget(mDelayPicker); |
| 126 | setFixedSize(sizeHint()); |
| 127 | } |
| 128 | |
| 129 | MainToolBar::~MainToolBar() |
| 130 | { |
| 131 | delete mCaptureModePicker; |
| 132 | delete mDelayPicker; |
| 133 | } |
| 134 | |
| 135 | void MainToolBar::selectCaptureMode(CaptureModes captureModes) |
| 136 | { |
| 137 | mCaptureModePicker->setCaptureMode(captureModes); |
| 138 | } |
| 139 | |
| 140 | void MainToolBar::setCaptureDelay(int delay) |
| 141 | { |
| 142 | mDelayPicker->setValue(delay); |
| 143 | } |
| 144 | |
| 145 | void MainToolBar::newCaptureTriggered() |
| 146 | { |
| 147 | mCaptureModePicker->trigger(); |
| 148 | } |
| 149 | |
| 150 | void MainToolBar::setSaveActionEnabled(bool enabled) |
| 151 | { |
| 152 | mSaveAction->setEnabled(enabled); |
| 153 | } |
| 154 | |
| 155 | void MainToolBar::setCopyActionEnabled(bool enabled) |
| 156 | { |
| 157 | mCopyAction->setEnabled(enabled); |
| 158 | } |
| 159 | |
| 160 | void MainToolBar::setCropEnabled(bool enabled) |
| 161 | { |
| 162 | mCropAction->setEnabled(enabled); |
| 163 | } |
| 164 | |
| 165 | QAction *MainToolBar::newCaptureAction() const |
| 166 | { |
| 167 | return mNewCaptureAction; |
| 168 | } |
| 169 | |
| 170 | QAction *MainToolBar::saveAction() const |
| 171 | { |
| 172 | return mSaveAction; |
| 173 | } |
| 174 | |
| 175 | QAction *MainToolBar::copyToClipboardAction() const |
| 176 | { |
| 177 | return mCopyAction; |
| 178 | } |
| 179 | |
| 180 | QAction *MainToolBar::cropAction() const |
| 181 | { |
| 182 | return mCropAction; |
| 183 | } |
| 184 | |
| 185 | QAction *MainToolBar::undoAction() const |
| 186 | { |
| 187 | return mUndoAction; |
| 188 | } |
| 189 | |
| 190 | QAction *MainToolBar::redoAction() const |
| 191 | { |
| 192 | return mRedoAction; |
| 193 | } |
| 194 | |
| 195 | QList<QAction *> MainToolBar::captureActions() const |
| 196 | { |
| 197 | return mCaptureModePicker->captureActions(); |
| 198 | } |
| 199 | |
| 200 | void MainToolBar::setCollapsed(bool isCollapsed) |
| 201 | { |
| 202 | isCollapsed ? setFixedSize(0, 0) : setFixedSize(sizeHint()); |
| 203 | } |
| 204 | |
| 205 | bool MainToolBar::isCollapsed() const |
| 206 | { |
| 207 | return size() != sizeHint(); |
| 208 | } |