File indexing completed on 2024-05-12 04:19:47
0001 // vim: set tabstop=4 shiftwidth=4 expandtab: 0002 /* 0003 Gwenview: an image viewer 0004 Copyright 2007 Aurélien Gâteau <agateau@kde.org> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License 0008 as published by the Free Software Foundation; either version 2 0009 of the License, or (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; if not, write to the Free Software 0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0019 0020 */ 0021 // Self 0022 #include "printoptionspage.h" 0023 0024 // Qt 0025 #include <QButtonGroup> 0026 #include <QGridLayout> 0027 #include <QToolButton> 0028 0029 // KF 0030 #include <KConfigDialogManager> 0031 0032 // Local 0033 #include "gwenview_lib_debug.h" 0034 #include <gwenviewconfig.h> 0035 #include <signalblocker.h> 0036 #include <ui_printoptionspage.h> 0037 0038 namespace Gwenview 0039 { 0040 static inline double unitToInches(PrintOptionsPage::Unit unit) 0041 { 0042 if (unit == PrintOptionsPage::Inches) { 0043 return 1.; 0044 } else if (unit == PrintOptionsPage::Centimeters) { 0045 return 1 / 2.54; 0046 } else { // Millimeters 0047 return 1 / 25.4; 0048 } 0049 } 0050 0051 struct PrintOptionsPagePrivate : public Ui_PrintOptionsPage { 0052 QSize mImageSize; 0053 QButtonGroup mScaleGroup; 0054 QButtonGroup mPositionGroup; 0055 KConfigDialogManager *mConfigDialogManager = nullptr; 0056 0057 void initPositionFrame() 0058 { 0059 mPositionFrame->setStyleSheet( 0060 QStringLiteral("QFrame {" 0061 " background-color: palette(mid);" 0062 " border: 1px solid palette(dark);" 0063 "}" 0064 "QToolButton {" 0065 " border: none;" 0066 " background: palette(base);" 0067 "}" 0068 "QToolButton:hover {" 0069 " background: palette(alternate-base);" 0070 " border: 1px solid palette(highlight);" 0071 "}" 0072 "QToolButton:checked {" 0073 " background-color: palette(highlight);" 0074 "}")); 0075 0076 auto layout = new QGridLayout(mPositionFrame); 0077 layout->setContentsMargins(0, 0, 0, 0); 0078 layout->setSpacing(1); 0079 for (int row = 0; row < 3; ++row) { 0080 for (int col = 0; col < 3; ++col) { 0081 auto button = new QToolButton(mPositionFrame); 0082 button->setFixedSize(40, 40); 0083 button->setCheckable(true); 0084 layout->addWidget(button, row, col); 0085 0086 Qt::Alignment alignment; 0087 if (row == 0) { 0088 alignment = Qt::AlignTop; 0089 } else if (row == 1) { 0090 alignment = Qt::AlignVCenter; 0091 } else { 0092 alignment = Qt::AlignBottom; 0093 } 0094 if (col == 0) { 0095 alignment |= Qt::AlignLeft; 0096 } else if (col == 1) { 0097 alignment |= Qt::AlignHCenter; 0098 } else { 0099 alignment |= Qt::AlignRight; 0100 } 0101 0102 mPositionGroup.addButton(button, int(alignment)); 0103 } 0104 } 0105 } 0106 }; 0107 0108 PrintOptionsPage::PrintOptionsPage(const QSize &imageSize) 0109 : QWidget() 0110 , d(new PrintOptionsPagePrivate) 0111 { 0112 d->setupUi(this); 0113 d->mImageSize = imageSize; 0114 d->mConfigDialogManager = new KConfigDialogManager(this, GwenviewConfig::self()); 0115 0116 d->initPositionFrame(); 0117 0118 d->mScaleGroup.addButton(d->mNoScale, NoScale); 0119 d->mScaleGroup.addButton(d->mScaleToPage, ScaleToPage); 0120 d->mScaleGroup.addButton(d->mScaleTo, ScaleToCustomSize); 0121 0122 connect(d->kcfg_PrintWidth, SIGNAL(valueChanged(double)), SLOT(adjustHeightToRatio())); 0123 0124 connect(d->kcfg_PrintHeight, SIGNAL(valueChanged(double)), SLOT(adjustWidthToRatio())); 0125 0126 connect(d->kcfg_PrintKeepRatio, &QAbstractButton::toggled, this, &PrintOptionsPage::adjustHeightToRatio); 0127 } 0128 0129 PrintOptionsPage::~PrintOptionsPage() 0130 { 0131 delete d; 0132 } 0133 0134 Qt::Alignment PrintOptionsPage::alignment() const 0135 { 0136 const int id = d->mPositionGroup.checkedId(); 0137 qCWarning(GWENVIEW_LIB_LOG) << "alignment=" << id; 0138 return Qt::Alignment(id); 0139 } 0140 0141 PrintOptionsPage::ScaleMode PrintOptionsPage::scaleMode() const 0142 { 0143 return PrintOptionsPage::ScaleMode(d->mScaleGroup.checkedId()); 0144 } 0145 0146 bool PrintOptionsPage::enlargeSmallerImages() const 0147 { 0148 return d->kcfg_PrintEnlargeSmallerImages->isChecked(); 0149 } 0150 0151 PrintOptionsPage::Unit PrintOptionsPage::scaleUnit() const 0152 { 0153 return PrintOptionsPage::Unit(d->kcfg_PrintUnit->currentIndex()); 0154 } 0155 0156 double PrintOptionsPage::scaleWidth() const 0157 { 0158 return d->kcfg_PrintWidth->value() * unitToInches(scaleUnit()); 0159 } 0160 0161 double PrintOptionsPage::scaleHeight() const 0162 { 0163 return d->kcfg_PrintHeight->value() * unitToInches(scaleUnit()); 0164 } 0165 0166 void PrintOptionsPage::adjustWidthToRatio() 0167 { 0168 if (!d->kcfg_PrintKeepRatio->isChecked()) { 0169 return; 0170 } 0171 double width = d->mImageSize.width() * d->kcfg_PrintHeight->value() / d->mImageSize.height(); 0172 0173 SignalBlocker blocker(d->kcfg_PrintWidth); 0174 d->kcfg_PrintWidth->setValue(width ? width : 1.); 0175 } 0176 0177 void PrintOptionsPage::adjustHeightToRatio() 0178 { 0179 if (!d->kcfg_PrintKeepRatio->isChecked()) { 0180 return; 0181 } 0182 double height = d->mImageSize.height() * d->kcfg_PrintWidth->value() / d->mImageSize.width(); 0183 0184 SignalBlocker blocker(d->kcfg_PrintHeight); 0185 d->kcfg_PrintHeight->setValue(height ? height : 1.); 0186 } 0187 0188 void PrintOptionsPage::loadConfig() 0189 { 0190 QAbstractButton *button; 0191 0192 button = d->mPositionGroup.button(GwenviewConfig::printPosition()); 0193 if (button) { 0194 button->setChecked(true); 0195 } else { 0196 qCWarning(GWENVIEW_LIB_LOG) << "Unknown button for position group"; 0197 } 0198 0199 button = d->mScaleGroup.button(GwenviewConfig::printScaleMode()); 0200 if (button) { 0201 button->setChecked(true); 0202 } else { 0203 qCWarning(GWENVIEW_LIB_LOG) << "Unknown button for scale group"; 0204 } 0205 0206 d->mConfigDialogManager->updateWidgets(); 0207 0208 if (d->kcfg_PrintKeepRatio->isChecked()) { 0209 adjustHeightToRatio(); 0210 } 0211 } 0212 0213 void PrintOptionsPage::saveConfig() 0214 { 0215 int position = d->mPositionGroup.checkedId(); 0216 GwenviewConfig::setPrintPosition(position); 0217 0218 auto scaleMode = ScaleMode(d->mScaleGroup.checkedId()); 0219 GwenviewConfig::setPrintScaleMode(scaleMode); 0220 0221 d->mConfigDialogManager->updateSettings(); 0222 0223 GwenviewConfig::self()->save(); 0224 } 0225 0226 } // namespace 0227 0228 #include "moc_printoptionspage.cpp"