kis_spray_shape_option.cpp 5.37 KB
Newer Older
1
/*
2
 *  Copyright (c) 2008,2009,2010 Lukáš Tvrdý <lukast.dev@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "kis_spray_shape_option.h"
19
#include <klocalizedstring.h>
20

21
#include <QImage>
22
#include <QFile>
23
#include <KisImportExportManager.h>
24
#include <kis_file_name_requester.h>
25 26
#include "kis_aspect_ratio_locker.h"
#include "kis_signals_blocker.h"
27

28
#include "ui_wdgsprayshapeoptions.h"
29

30
class KisShapeOptionsWidget: public QWidget, public Ui::WdgSprayShapeOptions
31 32 33
{
public:
    KisShapeOptionsWidget(QWidget *parent = 0)
34
        : QWidget(parent) {
35
        setupUi(this);
36
        imageUrl->setMimeTypeFilters(KisImportExportManager::supportedMimeTypes(KisImportExportManager::Import));
37
    }
38

39 40 41
};

KisSprayShapeOption::KisSprayShapeOption()
42 43
    : KisPaintOpOption(KisPaintOpOption::GENERAL, true),
      m_sizeRatioLocker(new KisAspectRatioLocker(this))
44
{
45 46 47

    setObjectName("KisSprayShapeOption");

48
    m_checkable = true;
49 50
    // save this to be able to restore it back
    m_maxSize = 1000;
51

52
    m_options = new KisShapeOptionsWidget();
53 54 55 56

    //initializer slider values
    m_options->widthSpin->setRange(1, 1000, 0);
    m_options->widthSpin->setValue(6);
57
    m_options->widthSpin->setSuffix(i18n(" px"));
58 59 60

    m_options->heightSpin->setRange(1, 1000, 0);
    m_options->heightSpin->setValue(6);
61
    m_options->heightSpin->setSuffix(i18n(" px"));
62

63
    // UI signals
64
    connect(m_options->proportionalBox, SIGNAL(clicked(bool)), SLOT(changeSizeUI(bool)));
65
    connect(m_options->imageUrl, SIGNAL(textChanged(QString)), this, SLOT(prepareImage()));
66

67 68 69 70
    m_sizeRatioLocker->connectSpinBoxes(m_options->widthSpin, m_options->heightSpin, m_options->aspectButton);
    m_sizeRatioLocker->setBlockUpdateSignalOnDrag(true);
    connect(m_sizeRatioLocker, SIGNAL(sliderValueChanged()), SLOT(emitSettingChanged()));
    connect(m_sizeRatioLocker, SIGNAL(aspectButtonChanged()), SLOT(emitSettingChanged()));
71

72 73 74 75
    connect(m_options->proportionalBox, SIGNAL(toggled(bool)), SLOT(emitSettingChanged()));
    connect(m_options->proportionalBox, SIGNAL(clicked(bool)), SLOT(emitSettingChanged()));
    connect(m_options->shapeBox, SIGNAL(currentIndexChanged(int)), SLOT(emitSettingChanged()));
    connect(m_options->imageUrl, SIGNAL(textChanged(QString)), SLOT(emitSettingChanged()));
76

77 78
    setConfigurationPage(m_options);
}
79

80 81
KisSprayShapeOption::~KisSprayShapeOption()
{
82
    delete m_options;
83 84
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
85 86
int KisSprayShapeOption::shape() const
{
87 88 89
    return m_options->shapeBox->currentIndex();
}

90

91
void KisSprayShapeOption::writeOptionSetting(KisPropertiesConfigurationSP setting) const
92
{
93 94
    setting->setProperty(SPRAYSHAPE_ENABLED, isChecked());
    setting->setProperty(SPRAYSHAPE_SHAPE, shape());
95
    setting->setProperty(SPRAYSHAPE_USE_ASPECT, m_options->aspectButton->keepAspectRatio());
96
    setting->setProperty(SPRAYSHAPE_PROPORTIONAL, m_options->proportionalBox->isChecked());
97 98
    setting->setProperty(SPRAYSHAPE_WIDTH, m_options->widthSpin->value());
    setting->setProperty(SPRAYSHAPE_HEIGHT, m_options->heightSpin->value());
99
    setting->setProperty(SPRAYSHAPE_IMAGE_URL, m_options->imageUrl->fileName());
100 101
}

102

103
void KisSprayShapeOption::readOptionSetting(const KisPropertiesConfigurationSP setting)
104
{
105
    // in 2.2 there is not shape enabled so true by default
106
    setChecked(setting->getBool(SPRAYSHAPE_ENABLED, true));
107
    m_options->shapeBox->setCurrentIndex(setting->getInt(SPRAYSHAPE_SHAPE));
108
    m_options->proportionalBox->setChecked(setting->getBool(SPRAYSHAPE_PROPORTIONAL));
109
    m_options->aspectButton->setKeepAspectRatio(setting->getBool(SPRAYSHAPE_USE_ASPECT, false));
110 111
    m_options->widthSpin->setValue(setting->getInt(SPRAYSHAPE_WIDTH));
    m_options->heightSpin->setValue(setting->getInt(SPRAYSHAPE_HEIGHT));
112
    m_options->imageUrl->setFileName(setting->getString(SPRAYSHAPE_IMAGE_URL));
113 114
}

115 116 117

void KisSprayShapeOption::prepareImage()
{
118
    QString path = m_options->imageUrl->fileName();
119
    if (QFile::exists(path)) {
120
        QImage image(path);
121
        if (!image.isNull()) {
122
            KisSignalsBlocker b(m_options->widthSpin, m_options->heightSpin);
123 124
            m_options->widthSpin->setValue(image.width());
            m_options->heightSpin->setValue(image.height());
125 126
        }
    }
127 128
}

129 130 131
void KisSprayShapeOption::changeSizeUI(bool proportionalSize)
{
    // if proportionalSize is false, pixel size is used
132 133
    if (!proportionalSize) {
        m_options->widthSpin->setMaximum(m_maxSize);
134
        m_options->widthSpin->setSuffix(i18n(" px"));
135
        m_options->heightSpin->setMaximum(m_maxSize);
136
        m_options->heightSpin->setSuffix(i18n(" px"));
137 138 139
    }
    else {
        m_options->widthSpin->setMaximum(100);
140
        m_options->widthSpin->setSuffix(i18n("%"));
141
        m_options->heightSpin->setMaximum(100);
142
        m_options->heightSpin->setSuffix(i18n("%"));
143 144
    }
}