| 1 | /* |
| 2 | * Copyright (C) 2020 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 Lesser 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 Lesser 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 "SaverSettings.h" |
| 21 | |
| 22 | SaverSettings::SaverSettings(const QSharedPointer<IConfig> &config, const QSharedPointer<IFileDialogService> &fileDialogService) : |
| 23 | mConfig(config), |
| 24 | mAutoSaveNewCapturesCheckbox(new QCheckBox(this)), |
| 25 | mPromptToSaveBeforeExitCheckbox(new QCheckBox(this)), |
| 26 | mRememberSaveDirectoryCheckbox(new QCheckBox(this)), |
| 27 | mSaveQualityDefaultRadioButton(new QRadioButton(this)), |
| 28 | mSaveQualityFactorRadioButton(new QRadioButton(this)), |
| 29 | mSaveLocationLabel(new QLabel(this)), |
| 30 | mSaveLocationLineEdit(new QLineEdit(this)), |
| 31 | mBrowseButton(new QPushButton(this)), |
| 32 | mOverwriteFileCheckbox(new QCheckBox(this)), |
| 33 | mSaveQualityFactorSpinBox(new CustomSpinBox(0, 100, this)), |
| 34 | mLayout(new QGridLayout), |
| 35 | mSaveQualityLayout(new QGridLayout), |
| 36 | mSaveQualityGroupBox(new QGroupBox(this)), |
| 37 | mFileDialogService(fileDialogService) |
| 38 | { |
| 39 | initGui(); |
| 40 | loadConfig(); |
| 41 | } |
| 42 | |
| 43 | void SaverSettings::initGui() |
| 44 | { |
| 45 | mAutoSaveNewCapturesCheckbox->setText(tr("Automatically save new captures to default location")); |
| 46 | mPromptToSaveBeforeExitCheckbox->setText(tr("Prompt to save before discarding unsaved changes")); |
| 47 | |
| 48 | mSaveQualityDefaultRadioButton->setText(tr("Default")); |
| 49 | mSaveQualityFactorRadioButton->setText(tr("Factor")); |
| 50 | mSaveQualityFactorRadioButton->setToolTip(tr("Specify 0 to obtain small compressed files, 100 for large uncompressed files.\n" |
| 51 | "Not all image formats support the full range, JPEG does.")); |
| 52 | |
| 53 | mSaveQualityFactorSpinBox->setToolTip(mSaveQualityFactorRadioButton->toolTip()); |
| 54 | |
| 55 | mSaveQualityGroupBox->setTitle(tr("Save Quality")); |
| 56 | |
| 57 | mRememberSaveDirectoryCheckbox->setText(tr("Remember last Save Directory")); |
| 58 | mRememberSaveDirectoryCheckbox->setToolTip(tr("When enabled will overwrite the save directory stored in settings\n" |
| 59 | "with the latest save directory, for every save.")); |
| 60 | |
| 61 | mSaveLocationLabel->setText(tr("Capture save location and filename") + QLatin1String(":")); |
| 62 | |
| 63 | mSaveLocationLineEdit->setToolTip(tr("Supported Formats are JPG, PNG and BMP. If no format provided, PNG will be used as default.\n" |
| 64 | "Filename can contain following wildcards:\n" |
| 65 | "- $Y, $M, $D for date, $h, $m, $s for time, or $T for time in hhmmss format.\n" |
| 66 | "- Multiple consecutive # for counter. #### will result in 0001, next capture would be 0002.")); |
| 67 | |
| 68 | mBrowseButton->setText(tr("Browse")); |
| 69 | connect(mBrowseButton, &QPushButton::clicked, this, &SaverSettings::chooseSaveDirectory); |
| 70 | |
| 71 | mOverwriteFileCheckbox->setText(tr("Overwrite file with same name")); |
| 72 | |
| 73 | mSaveQualityLayout->addWidget(mSaveQualityDefaultRadioButton, 0, 0, 1, 1); |
| 74 | mSaveQualityLayout->addWidget(mSaveQualityFactorRadioButton, 1, 0, 1, 1); |
| 75 | mSaveQualityLayout->addWidget(mSaveQualityFactorSpinBox, 1, 1, 1, 1); |
| 76 | mSaveQualityLayout->setColumnStretch(2, 1); |
| 77 | |
| 78 | mSaveQualityGroupBox->setLayout(mSaveQualityLayout); |
| 79 | |
| 80 | mLayout->setAlignment(Qt::AlignTop); |
| 81 | mLayout->addWidget(mAutoSaveNewCapturesCheckbox, 0, 0, 1, 4); |
| 82 | mLayout->addWidget(mPromptToSaveBeforeExitCheckbox, 1, 0, 1, 4); |
| 83 | mLayout->addWidget(mRememberSaveDirectoryCheckbox, 2, 0, 1, 4); |
| 84 | mLayout->setRowMinimumHeight(3, 15); |
| 85 | mLayout->addWidget(mSaveQualityGroupBox, 4, 0, 1, 4); |
| 86 | mLayout->setRowMinimumHeight(5, 15); |
| 87 | mLayout->addWidget(mSaveLocationLabel, 6, 0, 1, 4); |
| 88 | mLayout->addWidget(mSaveLocationLineEdit, 7, 0, 1, 3); |
| 89 | mLayout->addWidget(mBrowseButton, 7, 3); |
| 90 | mLayout->addWidget(mOverwriteFileCheckbox, 8, 0, 1, 4); |
| 91 | |
| 92 | setTitle(tr("Saver Settings")); |
| 93 | setLayout(mLayout); |
| 94 | } |
| 95 | |
| 96 | void SaverSettings::loadConfig() |
| 97 | { |
| 98 | mAutoSaveNewCapturesCheckbox->setChecked(mConfig->autoSaveNewCaptures()); |
| 99 | mPromptToSaveBeforeExitCheckbox->setChecked(mConfig->promptSaveBeforeExit()); |
| 100 | mRememberSaveDirectoryCheckbox->setChecked(mConfig->rememberLastSaveDirectory()); |
| 101 | mSaveQualityFactorSpinBox->setValue(mConfig->saveQualityFactor()); |
| 102 | mSaveQualityDefaultRadioButton->setChecked(mConfig->saveQualityMode() == SaveQualityMode::Default); |
| 103 | mSaveQualityFactorRadioButton->setChecked(mConfig->saveQualityMode() == SaveQualityMode::Factor); |
| 104 | mSaveLocationLineEdit->setText(mConfig->saveDirectory() + mConfig->saveFilename() + QLatin1String(".") + mConfig->saveFormat()); |
| 105 | mOverwriteFileCheckbox->setChecked(mConfig->overwriteFile()); |
| 106 | } |
| 107 | |
| 108 | void SaverSettings::saveSettings() |
| 109 | { |
| 110 | mConfig->setAutoSaveNewCaptures(mAutoSaveNewCapturesCheckbox->isChecked()); |
| 111 | mConfig->setPromptSaveBeforeExit(mPromptToSaveBeforeExitCheckbox->isChecked()); |
| 112 | mConfig->setRememberLastSaveDirectory(mRememberSaveDirectoryCheckbox->isChecked()); |
| 113 | mConfig->setSaveQualityMode(getSaveQualityMode()); |
| 114 | mConfig->setSaveQualityFactor(mSaveQualityFactorSpinBox->value()); |
| 115 | mConfig->setSaveDirectory(PathHelper::extractParentDirectory(mSaveLocationLineEdit->displayText())); |
| 116 | mConfig->setSaveFilename(PathHelper::extractFilename(mSaveLocationLineEdit->displayText())); |
| 117 | mConfig->setSaveFormat(PathHelper::extractFormat(mSaveLocationLineEdit->displayText())); |
| 118 | mConfig->setOverwriteFile(mOverwriteFileCheckbox->isChecked()); |
| 119 | } |
| 120 | |
| 121 | void SaverSettings::chooseSaveDirectory() |
| 122 | { |
| 123 | auto path = mFileDialogService->getExistingDirectory(this, tr("Capture save location"), mConfig->saveDirectory()); |
| 124 | if(!path.isEmpty()) { |
| 125 | auto filename = PathHelper::extractFilename(mSaveLocationLineEdit->text()); |
| 126 | auto format = PathHelper::extractFormat(mSaveLocationLineEdit->text()); |
| 127 | |
| 128 | if(!filename.isEmpty()) { |
| 129 | path.append(QLatin1Char('/')).append(filename); |
| 130 | } |
| 131 | |
| 132 | if(!format.isEmpty()) { |
| 133 | path.append(QLatin1Char('.')).append(format); |
| 134 | } |
| 135 | |
| 136 | mSaveLocationLineEdit->setText(path); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | SaveQualityMode SaverSettings::getSaveQualityMode() |
| 141 | { |
| 142 | return mSaveQualityDefaultRadioButton->isChecked() ? SaveQualityMode::Default : SaveQualityMode::Factor; |
| 143 | } |