krita

kis_previewwidget.cc

00001 /*
00002  *  kis_previewwidget.cc - part of Krita
00003  *
00004  *  Copyright (c) 2001 John Califf  <jwcaliff@compuzone.net>
00005  *  Copyright (c) 2004 Bart Coppens <kde@bartcoppens.be>
00006  *  Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #include <qcheckbox.h>
00024 #include <qradiobutton.h>
00025 #include <qpainter.h>
00026 #include <qpoint.h>
00027 #include <qpushbutton.h>
00028 #include <qlayout.h>
00029 #include <qlabel.h>
00030 #include <qapplication.h>
00031 #include <qcolor.h>
00032 #include <qgroupbox.h>
00033 #include <qcursor.h>
00034 
00035 #include <kdebug.h>
00036 #include <kiconloader.h>
00037 #include <kpushbutton.h>
00038 
00039 #include <kis_cursor.h>
00040 #include <kis_colorspace.h>
00041 #include <kis_colorspace_factory_registry.h>
00042 #include <kis_config.h>
00043 #include <kis_filter_strategy.h>
00044 #include <kis_global.h>
00045 #include <kis_image.h>
00046 #include <kis_layer.h>
00047 #include <kis_meta_registry.h>
00048 #include <kis_painter.h>
00049 #include <kis_profile.h>
00050 #include <kis_types.h>
00051 #include <kis_undo_adapter.h>
00052 
00053 #include "kis_previewwidgetbase.h"
00054 #include "kis_previewwidget.h"
00055 #include "imageviewer.h"
00056 
00057 KisPreviewWidget::KisPreviewWidget( QWidget* parent, const char* name )
00058     : PreviewWidgetBase( parent, name )/*, m_image(0)*/
00059 {
00060     m_autoupdate = true;
00061     m_previewIsDisplayed = true;
00062 
00063     btnZoomIn->setIconSet(KGlobal::instance()->iconLoader()->loadIconSet( "viewmag+", KIcon::MainToolbar, 16 ));
00064     connect(btnZoomIn, SIGNAL(clicked()), this, SLOT(zoomIn()));
00065     btnZoomOut->setIconSet(KGlobal::instance()->iconLoader()->loadIconSet( "viewmag-", KIcon::MainToolbar, 16 ));
00066     connect(btnZoomOut, SIGNAL(clicked()), this, SLOT(zoomOut()));
00067     btnUpdate->setIconSet(KGlobal::instance()->iconLoader()->loadIconSet( "reload", KIcon::MainToolbar, 16 ));
00068     connect(btnUpdate, SIGNAL(clicked()), this, SLOT(forceUpdate()));
00069 
00070     connect(radioBtnPreview, SIGNAL(toggled(bool)), this, SLOT(setPreviewDisplayed(bool)));
00071 
00072     connect(checkBoxAutoUpdate, SIGNAL(toggled(bool)), this, SLOT(slotSetAutoUpdate(bool)));
00073 
00074 
00075 
00076 /*    kToolBar1->insertLineSeparator();
00077     kToolBar1->insertButton("reload",2, true, i18n("Update"));
00078     connect(kToolBar1->getButton(2),SIGNAL(clicked()),this,SLOT(forceUpdate()));
00079 
00080     kToolBar1->insertButton("",3, true, i18n("Auto Update"));
00081     connect(kToolBar1->getButton(3),SIGNAL(clicked()),this,SLOT(toggleAutoUpdate()));
00082 
00083     kToolBar1->insertButton("",4, true, i18n("Switch"));
00084     connect(kToolBar1->getButton(4),SIGNAL(clicked()),this,SLOT(toggleImageDisplayed()));*/
00085 // these currently don't yet work, reenable when they do work :)  (TZ-12-2005)
00086 // TODO reenable these
00087 //   kToolBar1->insertButton("",5, true, i18n("Popup Original and Preview"));
00088 }
00089 
00090 void KisPreviewWidget::forceUpdate()
00091 {
00092     if (!m_origDevice) return;
00093     if(m_previewIsDisplayed)
00094     {
00095         m_groupBox->setTitle(m_origDevice->name());
00096         emit updated();
00097     }
00098 }
00099 
00100 void KisPreviewWidget::slotSetDevice(KisPaintDeviceSP dev)
00101 {
00102     Q_ASSERT( dev );
00103 
00104     if (!dev) return;
00105 
00106     m_origDevice = dev;
00107 
00108     KisConfig cfg;
00109     QString monitorProfileName = cfg.monitorProfile();
00110     m_profile = KisMetaRegistry::instance()->csRegistry()->getProfileByName(monitorProfileName);
00111 
00112     QRect r = dev->exactBounds();
00113 
00114     m_groupBox->setTitle(i18n("Preview: ") + dev->name());
00115     m_previewIsDisplayed = true;
00116 
00117     m_zoom = (double)m_preview->width() / (double)r.width();
00118     zoomChanged();
00119 }
00120 
00121 
00122 KisPaintDeviceSP KisPreviewWidget::getDevice()
00123 {
00124     return m_previewDevice;
00125 }
00126 
00127 void KisPreviewWidget::slotUpdate()
00128 {
00129     QRect r = m_previewDevice->exactBounds();
00130     m_scaledPreview = m_previewDevice->convertToQImage(m_profile, 0, 0, r.width(), r.height());
00131     if(m_zoom > 1.0)
00132     {
00133         int w, h;
00134         w = (int) ceil(r.width() * m_zoom );
00135         h = (int) ceil(r.height() * m_zoom );
00136         m_scaledPreview = m_scaledPreview.smoothScale (w,h, QImage::ScaleMax);
00137     }
00138     if(m_previewIsDisplayed)
00139     {
00140         m_preview->setImage(m_scaledPreview);
00141     }
00142 }
00143 
00144 void KisPreviewWidget::slotSetAutoUpdate(bool set) {
00145     m_autoupdate = set;
00146 }
00147 
00148 void KisPreviewWidget::wheelEvent(QWheelEvent * e)
00149 {
00150     if (e->delta() > 0)
00151         zoomIn();
00152     else
00153         zoomOut();
00154     e->accept();
00155 }
00156 
00157 void KisPreviewWidget::setPreviewDisplayed(bool v)
00158 {
00159     if (!m_origDevice) return;
00160     if (!m_preview) return;
00161     if (m_scaledPreview == 0) return;
00162 
00163     m_previewIsDisplayed = v;
00164     if(m_previewIsDisplayed)
00165     {
00166         m_groupBox->setTitle(i18n("Preview: ") + m_origDevice->name());
00167         m_preview->setImage(m_scaledPreview);
00168     } else {
00169         m_groupBox->setTitle(i18n("Original: ") + m_origDevice->name());
00170         m_preview->setImage(m_scaledOriginal);
00171     }
00172 }
00173 
00174 void KisPreviewWidget::needUpdate()
00175 {
00176     if(m_previewIsDisplayed)
00177         m_groupBox->setTitle(i18n("Preview (needs update)"));
00178 }
00179 
00180 bool KisPreviewWidget::getAutoUpdate()  const {
00181     return m_autoupdate;
00182 }
00183 
00184 bool KisPreviewWidget::zoomChanged()
00185 {
00186     QApplication::setOverrideCursor(KisCursor::waitCursor());
00187     if (!m_origDevice) return false;
00188 
00189     QRect r = m_origDevice->exactBounds();
00190     int w = (int) ceil(r.width() * m_zoom );
00191     int h = (int) ceil(r.height() * m_zoom );
00192 
00193     if( w == 0 || h == 0 )
00194         return false;
00195 
00196     if(m_zoom < 1.0) // if m_zoom > 1.0, we will scale after applying the filter
00197     {
00198         m_previewDevice = m_origDevice->createThumbnailDevice(w, h); 
00199     }
00200     else {
00201         m_previewDevice = new KisPaintDevice( *m_origDevice );
00202     }
00203     
00204     m_scaledOriginal = m_previewDevice->convertToQImage(m_profile, 0, 0, w, h);
00205 
00206 
00207     if(!m_previewIsDisplayed)
00208     {
00209         m_preview->setImage(m_scaledOriginal);
00210     }
00211     
00212     emit updated();
00213 
00214     QApplication::restoreOverrideCursor();
00215 
00216     return true;
00217 }
00218 
00219 void KisPreviewWidget::zoomIn() {
00220     double oldZoom = m_zoom;
00221     if (m_zoom > 0 && m_zoom * 1.5 < 8) {
00222         m_zoom = m_zoom * 1.5;
00223         if( !zoomChanged() )
00224         m_zoom = oldZoom;
00225     }
00226 }
00227 
00228 void KisPreviewWidget::zoomOut() {
00229     double oldZoom = m_zoom;
00230     if (m_zoom > 0 && m_zoom / 1.5 > 1/8) {
00231         m_zoom = m_zoom / 1.5;
00232     if( !zoomChanged() )
00233        m_zoom = oldZoom;
00234    }
00235 }
00236 
00237 
00238 
00239 #include "kis_previewwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys