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     btnZoomOneToOne->setIconSet(KGlobal::instance()->iconLoader()->loadIconSet( "viewmag1", KIcon::MainToolbar, 16 ));
00074     connect(btnZoomOneToOne, SIGNAL(clicked()), this, SLOT(zoomOneToOne()));
00075 
00076 
00077 
00078 /*    kToolBar1->insertLineSeparator();
00079     kToolBar1->insertButton("reload",2, true, i18n("Update"));
00080     connect(kToolBar1->getButton(2),SIGNAL(clicked()),this,SLOT(forceUpdate()));
00081 
00082     kToolBar1->insertButton("",3, true, i18n("Auto Update"));
00083     connect(kToolBar1->getButton(3),SIGNAL(clicked()),this,SLOT(toggleAutoUpdate()));
00084 
00085     kToolBar1->insertButton("",4, true, i18n("Switch"));
00086     connect(kToolBar1->getButton(4),SIGNAL(clicked()),this,SLOT(toggleImageDisplayed()));*/
00087 // these currently don't yet work, reenable when they do work :)  (TZ-12-2005)
00088 // TODO reenable these
00089 //   kToolBar1->insertButton("",5, true, i18n("Popup Original and Preview"));
00090 }
00091 
00092 void KisPreviewWidget::forceUpdate()
00093 {
00094     if (!m_origDevice) return;
00095     if(m_previewIsDisplayed)
00096     {
00097         m_groupBox->setTitle(m_origDevice->name());
00098         emit updated();
00099     }
00100 }
00101 
00102 void KisPreviewWidget::slotSetDevice(KisPaintDeviceSP dev)
00103 {
00104     Q_ASSERT( dev );
00105 
00106     if (!dev) return;
00107 
00108     m_origDevice = dev;
00109 
00110     KisConfig cfg;
00111     QString monitorProfileName = cfg.monitorProfile();
00112     m_profile = KisMetaRegistry::instance()->csRegistry()->getProfileByName(monitorProfileName);
00113 
00114     QRect r = dev->exactBounds();
00115 
00116     m_groupBox->setTitle(i18n("Preview: ") + dev->name());
00117     m_previewIsDisplayed = true;
00118 
00119     m_zoom = (double)m_preview->width() / (double)r.width();
00120     zoomChanged();
00121 }
00122 
00123 
00124 KisPaintDeviceSP KisPreviewWidget::getDevice()
00125 {
00126     return m_previewDevice;
00127 }
00128 
00129 void KisPreviewWidget::slotUpdate()
00130 {
00131     QRect r = m_previewDevice->exactBounds();
00132     m_scaledPreview = m_previewDevice->convertToQImage(m_profile, 0, 0, r.width(), r.height());
00133     if(m_zoom > 1.0)
00134     {
00135         int w, h;
00136         w = (int) ceil(r.width() * m_zoom );
00137         h = (int) ceil(r.height() * m_zoom );
00138         m_scaledPreview = m_scaledPreview.smoothScale (w,h, QImage::ScaleMax);
00139     }
00140     if(m_previewIsDisplayed)
00141     {
00142         m_preview->setImage(m_scaledPreview);
00143     }
00144 }
00145 
00146 void KisPreviewWidget::slotSetAutoUpdate(bool set) {
00147     m_autoupdate = set;
00148 }
00149 
00150 void KisPreviewWidget::wheelEvent(QWheelEvent * e)
00151 {
00152     if (e->delta() > 0)
00153         zoomIn();
00154     else
00155         zoomOut();
00156     e->accept();
00157 }
00158 
00159 void KisPreviewWidget::setPreviewDisplayed(bool v)
00160 {
00161     if (!m_origDevice) return;
00162     if (!m_preview) return;
00163     if (m_scaledPreview == 0) return;
00164 
00165     m_previewIsDisplayed = v;
00166     if(m_previewIsDisplayed)
00167     {
00168         m_groupBox->setTitle(i18n("Preview: ") + m_origDevice->name());
00169         m_preview->setImage(m_scaledPreview);
00170     } else {
00171         m_groupBox->setTitle(i18n("Original: ") + m_origDevice->name());
00172         m_preview->setImage(m_scaledOriginal);
00173     }
00174 }
00175 
00176 void KisPreviewWidget::needUpdate()
00177 {
00178     if(m_previewIsDisplayed)
00179         m_groupBox->setTitle(i18n("Preview (needs update)"));
00180 }
00181 
00182 bool KisPreviewWidget::getAutoUpdate()  const {
00183     return m_autoupdate;
00184 }
00185 
00186 bool KisPreviewWidget::zoomChanged()
00187 {
00188     QApplication::setOverrideCursor(KisCursor::waitCursor());
00189     if (!m_origDevice) return false;
00190 
00191     QRect r = m_origDevice->exactBounds();
00192     int w = (int) ceil(r.width() * m_zoom );
00193     int h = (int) ceil(r.height() * m_zoom );
00194 
00195     if( w == 0 || h == 0 )
00196         return false;
00197 
00198     if(m_zoom < 1.0) // if m_zoom > 1.0, we will scale after applying the filter
00199     {
00200         m_previewDevice = m_origDevice->createThumbnailDevice(w, h); 
00201     }
00202     else {
00203         m_previewDevice = new KisPaintDevice( *m_origDevice );
00204     }
00205     
00206     m_scaledOriginal = m_previewDevice->convertToQImage(m_profile, 0, 0, w, h);
00207 
00208 
00209     if(!m_previewIsDisplayed)
00210     {
00211         m_preview->setImage(m_scaledOriginal);
00212     }
00213     
00214     emit updated();
00215 
00216     QApplication::restoreOverrideCursor();
00217 
00218     return true;
00219 }
00220 
00221 void KisPreviewWidget::zoomIn() {
00222     double oldZoom = m_zoom;
00223     if (m_zoom > 0 && m_zoom * 1.5 < 8) {
00224         m_zoom = m_zoom * 1.5;
00225         if( !zoomChanged() )
00226         m_zoom = oldZoom;
00227     }
00228 }
00229 
00230 void KisPreviewWidget::zoomOut() {
00231     double oldZoom = m_zoom;
00232     if (m_zoom > 0 && m_zoom / 1.5 > 1/8) {
00233         m_zoom = m_zoom / 1.5;
00234     if( !zoomChanged() )
00235        m_zoom = oldZoom;
00236    }
00237 }
00238 
00239 void KisPreviewWidget::zoomOneToOne() {
00240     double oldZoom = m_zoom;
00241     m_zoom = 1;
00242     if( !zoomChanged() ) m_zoom = oldZoom;
00243 }
00244 
00245 
00246 #include "kis_previewwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys