krita/ui

kis_custom_image_widget.cc

Go to the documentation of this file.
00001 /* This file is part of the KOffice project
00002  * Copyright (C) 2005 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2005 Casper Boemann <cbr@boemann.dk>
00004  * Copyright (C) 2007 Boudewijn Rempt <boud@valdyas.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "widgets/kis_custom_image_widget.h"
00022 
00023 
00024 #include <QPushButton>
00025 #include <QSlider>
00026 #include <QComboBox>
00027 #include <QRect>
00028 #include <QApplication>
00029 #include <QClipboard>
00030 #include <QDesktopWidget>
00031 
00032 #include <kcolorcombo.h>
00033 #include <kis_debug.h>
00034 
00035 #include <KoCompositeOp.h>
00036 #include <KoUnitDoubleSpinBox.h>
00037 #include <KoColorSpaceRegistry.h>
00038 #include <KoColorProfile.h>
00039 #include <KoColorSpace.h>
00040 #include <KoID.h>
00041 #include <KoColor.h>
00042 #include <KoUnit.h>
00043 #include <KoColorModelStandardIds.h>
00044 
00045 #include "kis_clipboard.h"
00046 #include "kis_doc2.h"
00047 
00048 #include "widgets/kis_cmb_idlist.h"
00049 #include "widgets/squeezedcombobox.h"
00050 #include "kis_image.h"
00051 #include "kis_layer.h"
00052 #include "kis_group_layer.h"
00053 #include "kis_paint_layer.h"
00054 #include "kis_paint_device.h"
00055 #include "kis_painter.h"
00056 
00057 KisCustomImageWidget::KisCustomImageWidget(QWidget *parent, KisDoc2 *doc, qint32 defWidth, qint32 defHeight, bool clipAvailable, double resolution, const QString & defColorSpaceName, const QString & imageName)
00058         : WdgNewImage(parent)
00059 {
00060     Q_UNUSED(defColorSpaceName);
00061     setObjectName("KisCustomImageWidget");
00062     m_doc = doc;
00063 
00064     txtName->setText(imageName);
00065 
00066     m_widthUnit = KoUnit(KoUnit::Pixel, resolution);
00067     doubleWidth->setValue(defWidth);
00068     doubleWidth->setDecimals(0);
00069     m_width = m_widthUnit.fromUserValue(defWidth);
00070     cmbWidthUnit->addItems(KoUnit::listOfUnitName(false));
00071     cmbWidthUnit->setCurrentIndex(KoUnit::Pixel);
00072 
00073     m_heightUnit = KoUnit(KoUnit::Pixel, resolution);
00074     doubleHeight->setValue(defHeight);
00075     doubleHeight->setDecimals(0);
00076     m_height = m_heightUnit.fromUserValue(defHeight);
00077     cmbHeightUnit->addItems(KoUnit::listOfUnitName(false));
00078     cmbHeightUnit->setCurrentIndex(KoUnit::Pixel);
00079 
00080     doubleResolution->setValue(72.0 * resolution);
00081     doubleResolution->setDecimals(0);
00082 
00083     connect(doubleResolution, SIGNAL(valueChanged(double)),
00084             this, SLOT(resolutionChanged(double)));
00085     connect(cmbWidthUnit, SIGNAL(activated(int)),
00086             this, SLOT(widthUnitChanged(int)));
00087     connect(doubleWidth, SIGNAL(valueChanged(double)),
00088             this, SLOT(widthChanged(double)));
00089     connect(cmbHeightUnit, SIGNAL(activated(int)),
00090             this, SLOT(heightUnitChanged(int)));
00091     connect(doubleHeight, SIGNAL(valueChanged(double)),
00092             this, SLOT(heightChanged(double)));
00093     connect(m_createButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
00094     m_createButton -> setDefault(true);
00095 
00096     chkFromClipboard->setChecked(clipAvailable);
00097     chkFromClipboard->setEnabled(clipAvailable);
00098 
00099     colorSpaceSelector->setCurrentColorModel(RGBAColorModelID);
00100     colorSpaceSelector->setCurrentColorDepth(Integer8BitsColorDepthID);
00101 
00102     connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
00103     connect(QApplication::clipboard(), SIGNAL(selectionChanged()), this, SLOT(clipboardDataChanged()));
00104     connect(QApplication::clipboard(), SIGNAL(changed(QClipboard::Mode)), this, SLOT(clipboardDataChanged()));
00105 
00106     connect(bnScreenSize, SIGNAL(clicked()), this, SLOT(screenSizeClicked()));
00107     connect(colorSpaceSelector, SIGNAL(selectionChanged(bool)), m_createButton, SLOT(setEnabled(bool)));
00108 }
00109 
00110 void KisCustomImageWidget::resolutionChanged(double res)
00111 {
00112     if (m_widthUnit.indexInList(false) == KoUnit::Pixel) {
00113         m_widthUnit = KoUnit(KoUnit::Pixel, res / 72.0);
00114         m_width = m_widthUnit.fromUserValue(doubleWidth->value());
00115     }
00116 
00117     if (m_heightUnit.indexInList(false) == KoUnit::Pixel) {
00118         m_heightUnit = KoUnit(KoUnit::Pixel, res / 72.0);
00119         m_height = m_heightUnit.fromUserValue(doubleHeight->value());
00120     }
00121 }
00122 
00123 
00124 void KisCustomImageWidget::widthUnitChanged(int index)
00125 {
00126     doubleWidth->blockSignals(true);
00127 
00128     if (index == KoUnit::Pixel) {
00129         doubleWidth->setDecimals(0);
00130         m_widthUnit = KoUnit(KoUnit::Pixel, doubleResolution->value() / 72.0);
00131     } else {
00132         doubleWidth->setDecimals(2);
00133         m_widthUnit = KoUnit((KoUnit::Unit)cmbWidthUnit->currentIndex());
00134     }
00135 
00136     doubleWidth->setValue(KoUnit::ptToUnit(m_width, m_widthUnit));
00137 
00138     doubleWidth->blockSignals(false);
00139 }
00140 
00141 void KisCustomImageWidget::widthChanged(double value)
00142 {
00143     m_width = m_widthUnit.fromUserValue(value);
00144 }
00145 
00146 void KisCustomImageWidget::heightUnitChanged(int index)
00147 {
00148     doubleHeight->blockSignals(true);
00149 
00150     if (index == KoUnit::Pixel) {
00151         doubleHeight->setDecimals(0);
00152         m_heightUnit = KoUnit(KoUnit::Pixel, doubleResolution->value() / 72.0);
00153     } else {
00154         doubleHeight->setDecimals(2);
00155         m_heightUnit = KoUnit((KoUnit::Unit)cmbHeightUnit->currentIndex());
00156     }
00157 
00158     doubleHeight->setValue(KoUnit::ptToUnit(m_height, m_heightUnit));
00159 
00160     doubleHeight->blockSignals(false);
00161 }
00162 
00163 void KisCustomImageWidget::heightChanged(double value)
00164 {
00165     m_height = m_heightUnit.fromUserValue(value);
00166 }
00167 
00168 void KisCustomImageWidget::buttonClicked()
00169 {
00170     const KoColorSpace * cs = colorSpaceSelector->currentColorSpace();
00171 
00172     QColor qc = cmbColor->color();
00173 
00174     qint32 width, height;
00175     double resolution;
00176     resolution =  doubleResolution->value() / 72.0;  // internal resolution is in pixels per pt
00177 
00178     // XXX: Added explicit casts to get rid of warning
00179     width = static_cast<qint32>(0.5  + KoUnit::ptToUnit(m_width, KoUnit(KoUnit::Pixel, resolution)));
00180     height = static_cast<qint32>(0.5 + KoUnit::ptToUnit(m_height, KoUnit(KoUnit::Pixel, resolution)));
00181     m_doc->newImage(txtName->text(), width, height, cs, KoColor(qc, cs), txtDescription->toPlainText(), resolution);
00182 
00183     KisImageWSP img = m_doc->image();
00184     if (img && img->root() && img->root()->firstChild()) {
00185         KisLayer * layer = dynamic_cast<KisLayer*>(img->root()->firstChild().data());
00186         if (layer) {
00187             layer->setOpacity(backgroundOpacity());
00188         }
00189         if (chkFromClipboard->isChecked()) {
00190             KisPaintDeviceSP clip = KisClipboard::instance()->clip();
00191             if (clip) {
00192                 QRect r = clip->exactBounds();
00193                 KisPainter gc;
00194                 gc.begin(layer->paintDevice());
00195                 gc.setCompositeOp(COMPOSITE_COPY);
00196                 gc.bitBlt(0, 0, clip, r.x(), r.y(), r.width(), r.height());
00197                 gc.end();
00198             }
00199         }
00200         layer->setDirty(QRect(0, 0, width, height));
00201     }
00202 
00203     emit documentSelected();
00204 }
00205 
00206 quint8 KisCustomImageWidget::backgroundOpacity() const
00207 {
00208     qint32 opacity = sliderOpacity->value();
00209 
00210     if (!opacity)
00211         return 0;
00212 
00213     return (opacity * 255) / 100;
00214 }
00215 
00216 void KisCustomImageWidget::clipboardDataChanged()
00217 {
00218     QClipboard *cb = QApplication::clipboard();
00219     QImage qimg = cb->image();
00220     const QMimeData *cbData = cb->mimeData();
00221     QByteArray mimeType("application/x-krita-selection");
00222 
00223     if ((cbData && cbData->hasFormat(mimeType)) || !qimg.isNull()) {
00224         KisClipboard * cb = KisClipboard::instance();
00225         QSize sz = cb->clipSize();
00226         if (sz.isValid() && sz.width() != 0 && sz.height() != 0) {
00227             chkFromClipboard->setChecked(true);
00228             chkFromClipboard->setEnabled(true);
00229             doubleWidth->setValue(sz.width());
00230             doubleWidth->setDecimals(0);
00231             doubleHeight->setValue(sz.height());
00232             doubleHeight->setDecimals(0);
00233         } else {
00234             chkFromClipboard->setChecked(false);
00235             chkFromClipboard->setEnabled(false);
00236 
00237         }
00238     }
00239 
00240 }
00241 
00242 void KisCustomImageWidget::screenSizeClicked()
00243 {
00244     QSize sz = QApplication::desktop()->availableGeometry(this).size();
00245     doubleWidth->setValue(sz.width());
00246     doubleWidth->setDecimals(0);
00247     doubleHeight->setValue(sz.height());
00248     doubleHeight->setDecimals(0);
00249 }
00250 
00251 #include "kis_custom_image_widget.moc"