File indexing completed on 2024-04-28 04:32:00

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  */
0010 
0011 #include "FilePropertiesDlg.h"
0012 
0013 #include <QLocale>
0014 #include <QRect>
0015 
0016 #include <KHelpClient>
0017 #include <KLocalizedString>
0018 
0019 #include "Commands.h"
0020 #include "Document.h"
0021 #include "Floss.h"
0022 #include "FlossScheme.h"
0023 #include "SchemeManager.h"
0024 
0025 FilePropertiesDlg::FilePropertiesDlg(QWidget *parent, Document *document)
0026     : QDialog(parent)
0027     , m_document(document)
0028 {
0029     setWindowTitle(i18n("File Properties"));
0030     ui.setupUi(this);
0031 
0032     ui.FlossScheme->addItems(SchemeManager::schemes());
0033     ui.FlossScheme->setCurrentItem(m_document->pattern()->palette().schemeName());
0034 
0035     m_unitsFormat = static_cast<Configuration::EnumDocument_UnitsFormat::type>(m_document->property(QStringLiteral("unitsFormat")).toInt());
0036 
0037     // get the extents of the pattern so that absolute minimum values for
0038     // the height and width of the pattern can be set.
0039     QRect extents = m_document->pattern()->stitches().extents();
0040 
0041     // extents will be (-1, -1) if no stitches have been added, set extents to minimum size
0042     if (extents.width() == -1) {
0043         extents.setWidth(1);
0044     }
0045 
0046     if (extents.height() == -1) {
0047         extents.setHeight(1);
0048     }
0049 
0050     m_minWidthStitches = extents.width();
0051     m_minHeightStitches = extents.height();
0052 
0053     // get the current width and height of the pattern in stitches.
0054     m_widthStitches = m_document->pattern()->stitches().width();
0055     m_heightStitches = m_document->pattern()->stitches().height();
0056 
0057     m_horizontalClothCount = m_document->property(QStringLiteral("horizontalClothCount")).toDouble();
0058     m_verticalClothCount = m_document->property(QStringLiteral("verticalClothCount")).toDouble();
0059 
0060     m_clothCountLink = m_document->property(QStringLiteral("clothCountLink")).toBool();
0061     m_clothCountUnits = static_cast<Configuration::EnumEditor_ClothCountUnits::type>(m_document->property(QStringLiteral("clothCountUnits")).toInt());
0062 
0063     updatePatternSizes();
0064 
0065     ui.ClothCountLink->setChecked(m_clothCountLink);
0066     on_ClothCountLink_clicked(m_clothCountLink);
0067 
0068     ui.PatternTitle->setText(m_document->property(QStringLiteral("title")).toString());
0069     ui.PatternAuthor->setText(m_document->property(QStringLiteral("author")).toString());
0070     ui.PatternCopyright->setText(m_document->property(QStringLiteral("copyright")).toString());
0071     ui.PatternFabric->setText(m_document->property(QStringLiteral("fabric")).toString());
0072     ui.FabricColor->setColor(m_document->property(QStringLiteral("fabricColor")).value<QColor>());
0073     ui.Instructions->setPlainText(m_document->property(QStringLiteral("instructions")).toString());
0074 }
0075 
0076 int FilePropertiesDlg::documentWidth() const
0077 {
0078     return m_widthStitches;
0079 }
0080 
0081 int FilePropertiesDlg::documentHeight() const
0082 {
0083     return m_heightStitches;
0084 }
0085 
0086 Configuration::EnumDocument_UnitsFormat::type FilePropertiesDlg::unitsFormat() const
0087 {
0088     return m_unitsFormat;
0089 }
0090 
0091 double FilePropertiesDlg::horizontalClothCount() const
0092 {
0093     return m_horizontalClothCount;
0094 }
0095 
0096 bool FilePropertiesDlg::clothCountLink() const
0097 {
0098     return ui.ClothCountLink->isChecked();
0099 }
0100 
0101 double FilePropertiesDlg::verticalClothCount() const
0102 {
0103     return m_verticalClothCount;
0104 }
0105 
0106 Configuration::EnumEditor_ClothCountUnits::type FilePropertiesDlg::clothCountUnits() const
0107 {
0108     return m_clothCountUnits;
0109 }
0110 
0111 QString FilePropertiesDlg::title() const
0112 {
0113     return ui.PatternTitle->text();
0114 }
0115 
0116 QString FilePropertiesDlg::author() const
0117 {
0118     return ui.PatternAuthor->text();
0119 }
0120 
0121 QString FilePropertiesDlg::copyright() const
0122 {
0123     return ui.PatternCopyright->text();
0124 }
0125 
0126 QString FilePropertiesDlg::fabric() const
0127 {
0128     return ui.PatternFabric->text();
0129 }
0130 
0131 QColor FilePropertiesDlg::fabricColor() const
0132 {
0133     return ui.FabricColor->color();
0134 }
0135 
0136 QString FilePropertiesDlg::instructions() const
0137 {
0138     return ui.Instructions->toPlainText();
0139 }
0140 
0141 QString FilePropertiesDlg::flossScheme() const
0142 {
0143     return ui.FlossScheme->currentText();
0144 }
0145 
0146 void FilePropertiesDlg::hideEvent(QHideEvent *event)
0147 {
0148     KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).writeEntry(QStringLiteral("FilePropertiesDlg"), size());
0149 
0150     QDialog::hideEvent(event);
0151 }
0152 
0153 void FilePropertiesDlg::showEvent(QShowEvent *event)
0154 {
0155     QDialog::showEvent(event);
0156 
0157     if (KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).hasKey(QStringLiteral("FilePropertiesDlg"))) {
0158         resize(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).readEntry(QStringLiteral("FilePropertiesDlg"), QSize()));
0159     }
0160 }
0161 
0162 void FilePropertiesDlg::on_UnitsFormat_activated(int index)
0163 {
0164     m_unitsFormat = static_cast<Configuration::EnumDocument_UnitsFormat::type>(index);
0165 
0166     updatePatternSizes();
0167 }
0168 
0169 void FilePropertiesDlg::on_PatternWidth_valueChanged(double d)
0170 {
0171     if (m_unitsFormat != Configuration::EnumDocument_UnitsFormat::Stitches) {
0172         d *= m_horizontalClothCount;
0173     }
0174 
0175     m_widthStitches = std::max((int)d, m_minWidthStitches);
0176 }
0177 
0178 void FilePropertiesDlg::on_PatternHeight_valueChanged(double d)
0179 {
0180     if (m_unitsFormat != Configuration::EnumDocument_UnitsFormat::Stitches) {
0181         d *= m_horizontalClothCount;
0182     }
0183 
0184     m_heightStitches = std::max((int)d, m_minHeightStitches);
0185 }
0186 
0187 void FilePropertiesDlg::on_HorizontalClothCount_valueChanged(double d)
0188 {
0189     m_horizontalClothCount = d;
0190 
0191     if (ui.ClothCountLink->isChecked()) {
0192         ui.VerticalClothCount->setValue(d);
0193     }
0194 
0195     updatePatternSizes();
0196 }
0197 
0198 void FilePropertiesDlg::on_VerticalClothCount_valueChanged(double d)
0199 {
0200     m_verticalClothCount = d;
0201 
0202     updatePatternSizes();
0203 }
0204 
0205 void FilePropertiesDlg::on_ClothCountLink_clicked(bool checked)
0206 {
0207     ui.ClothCountLink->setIcon((checked) ? QIcon::fromTheme(QStringLiteral("object-locked")) : QIcon::fromTheme(QStringLiteral("object-unlocked")));
0208 
0209     if (checked) {
0210         ui.VerticalClothCount->setValue(ui.HorizontalClothCount->value());
0211         ui.VerticalClothCount->setEnabled(false);
0212     } else {
0213         ui.VerticalClothCount->setEnabled(true);
0214     }
0215 }
0216 
0217 void FilePropertiesDlg::on_DialogButtonBox_accepted()
0218 {
0219     accept();
0220 }
0221 
0222 void FilePropertiesDlg::on_DialogButtonBox_rejected()
0223 {
0224     reject();
0225 }
0226 
0227 void FilePropertiesDlg::on_DialogButtonBox_helpRequested()
0228 {
0229     KHelpClient::invokeHelp(QStringLiteral("PatternPropertiesDialog"), QStringLiteral("kxstitch"));
0230 }
0231 
0232 void FilePropertiesDlg::updatePatternSizes()
0233 {
0234     // block signals from elements to avoid recursive calls
0235     ui.UnitsFormat->blockSignals(true);
0236     ui.PatternWidth->blockSignals(true);
0237     ui.PatternHeight->blockSignals(true);
0238     ui.HorizontalClothCount->blockSignals(true);
0239     ui.VerticalClothCount->blockSignals(true);
0240 
0241     ui.UnitsFormat->setCurrentIndex(m_unitsFormat);
0242 
0243     double horizontalScale = 1.0;
0244     double verticalScale = 1.0;
0245 
0246     switch (m_unitsFormat) {
0247     case Configuration::EnumDocument_UnitsFormat::Inches:
0248         if (m_clothCountUnits == Configuration::EnumEditor_ClothCountUnits::Inches) {
0249             horizontalScale = m_horizontalClothCount;
0250             verticalScale = m_verticalClothCount;
0251         } else {
0252             horizontalScale = m_horizontalClothCount * 2.54;
0253             verticalScale = m_verticalClothCount * 2.54;
0254         }
0255 
0256         break;
0257 
0258     case Configuration::EnumDocument_UnitsFormat::Centimeters:
0259         if (m_clothCountUnits == Configuration::EnumEditor_ClothCountUnits::Centimeters) {
0260             horizontalScale = m_horizontalClothCount;
0261             verticalScale = m_verticalClothCount;
0262         } else {
0263             horizontalScale = m_horizontalClothCount / 2.54;
0264             verticalScale = m_verticalClothCount / 2.54;
0265         }
0266 
0267         break;
0268 
0269     default:
0270         break;
0271     }
0272 
0273     double scaledMinWidth = m_minWidthStitches / horizontalScale;
0274     double scaledMinHeight = m_minHeightStitches / verticalScale;
0275 
0276     double scaledWidth = m_widthStitches / horizontalScale;
0277     double scaledHeight = m_heightStitches / verticalScale;
0278 
0279     if (m_unitsFormat == Configuration::EnumDocument_UnitsFormat::Stitches) {
0280         ui.PatternWidth->setDecimals(0);
0281         ui.PatternHeight->setDecimals(0);
0282         ui.PatternWidth->setSingleStep(1);
0283         ui.PatternHeight->setSingleStep(1);
0284     } else {
0285         ui.PatternWidth->setDecimals(2);
0286         ui.PatternHeight->setDecimals(2);
0287         ui.PatternWidth->setSingleStep(0.01);
0288         ui.PatternHeight->setSingleStep(0.01);
0289     }
0290 
0291     ui.PatternWidth->setMinimum(scaledMinWidth);
0292     ui.PatternHeight->setMinimum(scaledMinHeight);
0293 
0294     ui.PatternWidth->setValue(scaledWidth);
0295     ui.PatternHeight->setValue(scaledHeight);
0296 
0297     if (m_clothCountUnits == Configuration::EnumEditor_ClothCountUnits::Centimeters) {
0298         ui.HorizontalClothCount->setSuffix(i18nc("Per centimeter measurements", "/cm"));
0299         ui.VerticalClothCount->setSuffix(i18nc("Per centimeter measurements", "/cm"));
0300         ui.HorizontalClothCount->setDecimals(1);
0301         ui.VerticalClothCount->setDecimals(1);
0302         ui.HorizontalClothCount->setSingleStep(0.1);
0303         ui.VerticalClothCount->setSingleStep(0.1);
0304     } else {
0305         ui.HorizontalClothCount->setSuffix(i18nc("Per inch measurements", "/in"));
0306         ui.VerticalClothCount->setSuffix(i18nc("Per inch measurements", "/in"));
0307         ui.HorizontalClothCount->setDecimals(0);
0308         ui.VerticalClothCount->setDecimals(0);
0309         ui.HorizontalClothCount->setSingleStep(1);
0310         ui.VerticalClothCount->setSingleStep(1);
0311     }
0312 
0313     ui.HorizontalClothCount->setValue(m_horizontalClothCount);
0314     ui.VerticalClothCount->setValue(m_verticalClothCount);
0315 
0316     // re-enable signals from elements to avoid recursive calls
0317     ui.UnitsFormat->blockSignals(false);
0318     ui.PatternWidth->blockSignals(false);
0319     ui.PatternHeight->blockSignals(false);
0320     ui.HorizontalClothCount->blockSignals(false);
0321     ui.VerticalClothCount->blockSignals(false);
0322 }
0323 
0324 #include "moc_FilePropertiesDlg.cpp"