| 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 "ActionSettingTab.h" |
| 21 | |
| 22 | ActionSettingTab::ActionSettingTab(const QString &name, const QList<CaptureModes> &captureModes, const QSharedPointer<IPlatformChecker> &platformChecker) : |
| 23 | ActionSettingTab(captureModes, platformChecker) |
| 24 | { |
| 25 | mNameLineEdit->setTextAndPlaceholderText(name); |
| 26 | } |
| 27 | |
| 28 | ActionSettingTab::ActionSettingTab(const Action &action, const QList<CaptureModes> &captureModes, const QSharedPointer<IPlatformChecker> &platformChecker) : |
| 29 | ActionSettingTab(captureModes, platformChecker) |
| 30 | { |
| 31 | setAction(action); |
| 32 | } |
| 33 | |
| 34 | ActionSettingTab::~ActionSettingTab() |
| 35 | { |
| 36 | delete mCaptureModeComboBox; |
| 37 | delete mDelaySpinBox; |
| 38 | delete mShortcutLineEdit; |
| 39 | } |
| 40 | |
| 41 | void ActionSettingTab::initGui(const QList<CaptureModes> &captureModes) |
| 42 | { |
| 43 | mNameLabel->setText(tr("Name") + QLatin1String(":")); |
| 44 | mNameLineEdit->setMaxLength(50); |
| 45 | connect(mNameLineEdit, &QLineEdit::editingFinished, this, &ActionSettingTab::nameEditingFinished); |
| 46 | |
| 47 | mShortcutLabel->setText(tr("Shortcut") + QLatin1String(":")); |
| 48 | mShortcutLabel->setToolTip("When global hotkeys are enabled and supported then\n" |
| 49 | "this shortcut will also work as a global hotkey\n" |
| 50 | "when the 'Global' option is enabled."); |
| 51 | |
| 52 | mIsGlobalShortcutCheckBox->setText(tr("Global")); |
| 53 | mIsGlobalShortcutCheckBox->setToolTip(tr("When enabled will make the shortcut\n" |
| 54 | "available even when ksnip has no focus.")); |
| 55 | |
| 56 | mShortcutLineEdit->setToolTip(mShortcutLabel->toolTip()); |
| 57 | |
| 58 | mShortcutClearButton->setText(tr("Clear")); |
| 59 | connect(mShortcutClearButton, &QPushButton::clicked, mShortcutLineEdit, &KeySequenceLineEdit::clear); |
| 60 | |
| 61 | mCaptureEnabledCheckBox->setText(tr("Take Capture")); |
| 62 | connect(mCaptureEnabledCheckBox, &QCheckBox::toggled, this, &ActionSettingTab::captureEnabledChanged); |
| 63 | |
| 64 | mIncludeCursorCheckBox->setText(tr("Include Cursor")); |
| 65 | |
| 66 | mDelayLabel->setText(tr("Delay") + QLatin1String(":")); |
| 67 | //: The small letter s stands for seconds. |
| 68 | mDelaySpinBox->setSuffix(tr("s")); |
| 69 | |
| 70 | mCaptureModeLabel->setText(tr("Capture Mode") + QLatin1String(":")); |
| 71 | populateCaptureModeCombobox(captureModes); |
| 72 | |
| 73 | mShowPinWindowCheckBox->setText(tr("Show image in Pin Window")); |
| 74 | |
| 75 | mCopyToClipboardCheckBox->setText(tr("Copy image to Clipboard")); |
| 76 | |
| 77 | mUploadCheckBox->setText(tr("Upload image")); |
| 78 | |
| 79 | mOpenDirectoryCheckBox->setText(tr("Open image parent directory")); |
| 80 | |
| 81 | mSaveCheckBox->setText(tr("Save image")); |
| 82 | |
| 83 | mHideMainWindowCheckBox->setText(tr("Hide Main Window")); |
| 84 | |
| 85 | mLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); |
| 86 | mLayout->setColumnMinimumWidth(0, 10); |
| 87 | mLayout->addWidget(mNameLabel, 0, 0, 1, 2); |
| 88 | mLayout->addWidget(mNameLineEdit, 0, 2, 1, 3); |
| 89 | mLayout->addWidget(mShortcutLabel, 1, 0, 1, 2); |
| 90 | mLayout->addWidget(mShortcutLineEdit, 1, 2, 1, 3); |
| 91 | mLayout->addWidget(mIsGlobalShortcutCheckBox, 1, 7, 1, 1); |
| 92 | mLayout->addWidget(mShortcutClearButton, 1, 6, 1, 1); |
| 93 | mLayout->setRowMinimumHeight(2, 10); |
| 94 | mLayout->addWidget(mCaptureEnabledCheckBox, 3, 0, 1, 5); |
| 95 | mLayout->addWidget(mIncludeCursorCheckBox, 4, 1, 1, 4); |
| 96 | mLayout->addWidget(mDelayLabel, 5, 1, 1, 2); |
| 97 | mLayout->addWidget(mDelaySpinBox, 5, 3, 1, 2); |
| 98 | mLayout->addWidget(mCaptureModeLabel, 6, 1, 1, 2); |
| 99 | mLayout->addWidget(mCaptureModeComboBox, 6, 3, 1, 2); |
| 100 | mLayout->setRowMinimumHeight(7, 10); |
| 101 | mLayout->addWidget(mShowPinWindowCheckBox, 8, 0, 1, 5); |
| 102 | mLayout->addWidget(mCopyToClipboardCheckBox, 9, 0, 1, 5); |
| 103 | mLayout->addWidget(mUploadCheckBox, 10, 0, 1, 5); |
| 104 | mLayout->addWidget(mSaveCheckBox, 11, 0, 1, 5); |
| 105 | mLayout->addWidget(mOpenDirectoryCheckBox, 12, 0, 1, 5); |
| 106 | mLayout->addWidget(mHideMainWindowCheckBox, 13, 0, 1, 5); |
| 107 | |
| 108 | setLayout(mLayout); |
| 109 | } |
| 110 | |
| 111 | void ActionSettingTab::populateCaptureModeCombobox(const QList<CaptureModes> &captureModes) |
| 112 | { |
| 113 | for (auto captureMode: captureModes) { |
| 114 | const auto label = EnumTranslator::instance()->toTranslatedString(captureMode); |
| 115 | mCaptureModeComboBox->addItem(label, static_cast<int>(captureMode)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void ActionSettingTab::captureEnabledChanged() |
| 120 | { |
| 121 | auto isEnabled = mCaptureEnabledCheckBox->isChecked(); |
| 122 | mIncludeCursorCheckBox->setEnabled(isEnabled); |
| 123 | mDelayLabel->setEnabled(isEnabled); |
| 124 | mDelaySpinBox->setEnabled(isEnabled); |
| 125 | mCaptureModeLabel->setEnabled(isEnabled); |
| 126 | mCaptureModeComboBox->setEnabled(isEnabled); |
| 127 | } |
| 128 | |
| 129 | Action ActionSettingTab::action() const |
| 130 | { |
| 131 | Action action; |
| 132 | action.setName(getTextWithEscapedAmpersand(mNameLineEdit->textOrPlaceholderText())); |
| 133 | action.setShortcut(mShortcutLineEdit->value()); |
| 134 | action.setIsGlobalShortcut(mIsGlobalShortcutCheckBox->isChecked()); |
| 135 | action.setIsCaptureEnabled(mCaptureEnabledCheckBox->isChecked()); |
| 136 | action.setCaptureDelay(mDelaySpinBox->value() * 1000); |
| 137 | action.setIncludeCursor(mIncludeCursorCheckBox->isChecked()); |
| 138 | action.setCaptureMode(mCaptureModeComboBox->currentData().value<CaptureModes>()); |
| 139 | action.setIsPinImageEnabled(mShowPinWindowCheckBox->isChecked()); |
| 140 | action.setIsSaveEnabled(mSaveCheckBox->isChecked()); |
| 141 | action.setIsCopyToClipboardEnabled(mCopyToClipboardCheckBox->isChecked()); |
| 142 | action.setIsOpenDirectoryEnabled(mOpenDirectoryCheckBox->isChecked()); |
| 143 | action.setIsUploadEnabled(mUploadCheckBox->isChecked()); |
| 144 | action.setIsHideMainWindowEnabled(mHideMainWindowCheckBox->isChecked()); |
| 145 | return action; |
| 146 | } |
| 147 | |
| 148 | void ActionSettingTab::setAction(const Action &action) const |
| 149 | { |
| 150 | mNameLineEdit->setTextAndPlaceholderText(getTextWithoutEscapedAmpersand(action.name())); |
| 151 | mShortcutLineEdit->setValue(action.shortcut()); |
| 152 | mIsGlobalShortcutCheckBox->setChecked(action.isGlobalShortcut()); |
| 153 | mCaptureEnabledCheckBox->setChecked(action.isCaptureEnabled()); |
| 154 | mDelaySpinBox->setValue(action.captureDelay() / 1000); |
| 155 | mIncludeCursorCheckBox->setChecked(action.includeCursor()); |
| 156 | mCaptureModeComboBox->setCurrentIndex(indexOfSelectedCaptureMode(action.captureMode())); |
| 157 | mShowPinWindowCheckBox->setChecked(action.isPinImageEnabled()); |
| 158 | mSaveCheckBox->setChecked(action.isSaveEnabled()); |
| 159 | mCopyToClipboardCheckBox->setChecked(action.isCopyToClipboardEnabled()); |
| 160 | mOpenDirectoryCheckBox->setChecked(action.isOpenDirectoryEnabled()); |
| 161 | mUploadCheckBox->setChecked(action.isUploadEnabled()); |
| 162 | mHideMainWindowCheckBox->setChecked(action.isHideMainWindowEnabled()); |
| 163 | } |
| 164 | |
| 165 | int ActionSettingTab::indexOfSelectedCaptureMode(CaptureModes modes) const |
| 166 | { |
| 167 | return mCaptureModeComboBox->findData(QVariant::fromValue(modes)); |
| 168 | } |
| 169 | |
| 170 | QString ActionSettingTab::getTextWithEscapedAmpersand(const QString &text) |
| 171 | { |
| 172 | auto copiedText = text; |
| 173 | return copiedText.replace(QLatin1String("&"), QLatin1String("&&")); |
| 174 | } |
| 175 | |
| 176 | QString ActionSettingTab::getTextWithoutEscapedAmpersand(const QString &text) |
| 177 | { |
| 178 | auto copiedText = text; |
| 179 | return copiedText.replace(QLatin1String("&&"), QLatin1String("&")); |
| 180 | } |
| 181 | |
| 182 | void ActionSettingTab::nameEditingFinished() |
| 183 | { |
| 184 | emit nameChanged(getTextWithEscapedAmpersand(mNameLineEdit->textOrPlaceholderText())); |
| 185 | } |
| 186 | |
| 187 | ActionSettingTab::ActionSettingTab(const QList<CaptureModes> &captureModes, const QSharedPointer<IPlatformChecker> &platformChecker) : |
| 188 | mCaptureModeComboBox(new QComboBox(nullptr)), |
| 189 | mCaptureEnabledCheckBox(new QCheckBox(this)), |
| 190 | mIncludeCursorCheckBox(new QCheckBox(this)), |
| 191 | mShowPinWindowCheckBox(new QCheckBox(this)), |
| 192 | mCopyToClipboardCheckBox(new QCheckBox(this)), |
| 193 | mUploadCheckBox(new QCheckBox(this)), |
| 194 | mOpenDirectoryCheckBox(new QCheckBox(this)), |
| 195 | mHideMainWindowCheckBox(new QCheckBox(this)), |
| 196 | mSaveCheckBox(new QCheckBox(this)), |
| 197 | mIsGlobalShortcutCheckBox(new QCheckBox(this)), |
| 198 | mCaptureModeLabel(new QLabel(this)), |
| 199 | mDelayLabel(new QLabel(this)), |
| 200 | mNameLabel(new QLabel(this)), |
| 201 | mShortcutLabel(new QLabel(this)), |
| 202 | mDelaySpinBox(new CustomSpinBox(0, 100)), |
| 203 | mNameLineEdit(new CustomLineEdit(this)), |
| 204 | mShortcutLineEdit(new KeySequenceLineEdit(this, HotKeyMap::instance()->getAllKeys(), platformChecker)), |
| 205 | mShortcutClearButton(new QPushButton(this)), |
| 206 | mLayout(new QGridLayout(this)) |
| 207 | { |
| 208 | initGui(captureModes); |
| 209 | captureEnabledChanged(); |
| 210 | } |