krita

kis_tool_paint.cc

00001 /*
00002  *  Copyright (c) 2003 Boudewijn Rempt
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <qwidget.h>
00020 #include <qrect.h>
00021 #include <qlayout.h>
00022 #include <qlabel.h>
00023 #include <qpushbutton.h>
00024 #include <qwhatsthis.h>
00025 #include <qcheckbox.h>
00026 
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 #include <knuminput.h>
00030 #include <kiconloader.h>
00031 
00032 #include "kis_button_release_event.h"
00033 #include "kis_canvas_subject.h"
00034 #include "kis_cmb_composite.h"
00035 #include "kis_colorspace.h"
00036 #include "kis_config.h"
00037 #include "kis_cursor.h"
00038 #include "kis_global.h"
00039 #include "kis_image.h"
00040 #include "kis_int_spinbox.h"
00041 #include "kis_paint_device.h"
00042 #include "kis_tool_controller.h"
00043 #include "kis_tool_paint.h"
00044 
00045 KisToolPaint::KisToolPaint(const QString& UIName)
00046     : super(UIName)
00047 {
00048     m_subject = 0;
00049 
00050     m_UIName = UIName;
00051 
00052     m_optionWidget = 0;
00053     m_optionWidgetLayout = 0;
00054 
00055     m_lbOpacity = 0;
00056     m_slOpacity = 0;
00057     m_lbComposite= 0;
00058     m_cmbComposite = 0;
00059 
00060     m_opacity = OPACITY_OPAQUE;
00061     m_compositeOp = COMPOSITE_OVER;
00062 }
00063 
00064 KisToolPaint::~KisToolPaint()
00065 {
00066 }
00067 
00068 void KisToolPaint::update(KisCanvasSubject *subject)
00069 {
00070     m_subject = subject;
00071     updateCompositeOpComboBox();
00072 }
00073 
00074 void KisToolPaint::paint(KisCanvasPainter&)
00075 {
00076 }
00077 
00078 void KisToolPaint::paint(KisCanvasPainter&, const QRect&)
00079 {
00080 }
00081 
00082 void KisToolPaint::deactivate()
00083 {
00084 }
00085 
00086 void KisToolPaint::buttonPress(KisButtonPressEvent *)
00087 {
00088 }
00089 
00090 void KisToolPaint::move(KisMoveEvent *)
00091 {
00092 }
00093 
00094 void KisToolPaint::buttonRelease(KisButtonReleaseEvent * e)
00095 {
00096     kdDebug() << "buttonRelease" << endl;
00097     if(e->button() == Qt::MidButton)
00098     {
00099         kdDebug() << "switch" << endl;
00100         KisColor bg = m_subject->bgColor();
00101         m_subject->setBGColor(m_subject->fgColor());
00102         m_subject->setFGColor(bg);
00103     }
00104 }
00105 
00106 void KisToolPaint::doubleClick(KisDoubleClickEvent *)
00107 {
00108 }
00109 
00110 void KisToolPaint::keyPress(QKeyEvent *)
00111 {
00112 }
00113 
00114 void KisToolPaint::keyRelease(QKeyEvent *)
00115 {
00116 }
00117 
00118 QWidget* KisToolPaint::createOptionWidget(QWidget* parent)
00119 {
00120     m_optionWidget = new QWidget(parent);
00121     m_optionWidget->setCaption(m_UIName);
00122 
00123     m_lbOpacity = new QLabel(i18n("Opacity:"), m_optionWidget);
00124     m_slOpacity = new KisIntSpinbox( m_optionWidget, "int_m_optionwidget");
00125     m_slOpacity->setRange( 0, 100);
00126     m_slOpacity->setValue(m_opacity / OPACITY_OPAQUE * 100);
00127     connect(m_slOpacity, SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
00128 
00129     m_lbComposite = new QLabel(i18n("Mode:"), m_optionWidget);
00130     m_cmbComposite = new KisCmbComposite(m_optionWidget);
00131     connect(m_cmbComposite, SIGNAL(activated(const KisCompositeOp&)), this, SLOT(slotSetCompositeMode(const KisCompositeOp&)));
00132 
00133     QVBoxLayout* verticalLayout = new QVBoxLayout(m_optionWidget);
00134     verticalLayout->setMargin(0);
00135     verticalLayout->setSpacing(3);
00136     
00137     m_optionWidgetLayout = new QGridLayout(verticalLayout, 2, 3, 6);
00138 
00139     m_optionWidgetLayout->addWidget(m_lbOpacity, 0, 0);
00140     m_optionWidgetLayout->addWidget(m_slOpacity, 0, 1);
00141 
00142     m_optionWidgetLayout->addWidget(m_lbComposite, 1, 0);
00143     m_optionWidgetLayout->addWidget(m_cmbComposite, 1, 1);
00144 
00145     verticalLayout->addItem(new QSpacerItem(0,0,QSizePolicy::Fixed,QSizePolicy::Expanding));
00146 
00147     if (!quickHelp().isEmpty()) {
00148         QPushButton* push = new QPushButton(SmallIconSet( "help" ), "", m_optionWidget);
00149         connect(push, SIGNAL(clicked()), this, SLOT(slotPopupQuickHelp()));
00150 
00151         QHBoxLayout* hLayout = new QHBoxLayout(m_optionWidget);
00152         hLayout->addWidget(push);
00153         hLayout->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Fixed));
00154         verticalLayout->addLayout(hLayout);
00155     }
00156     return m_optionWidget;
00157 }
00158 
00159 QWidget* KisToolPaint::optionWidget()
00160 {
00161     return m_optionWidget;
00162 }
00163 
00164 void KisToolPaint::addOptionWidgetLayout(QLayout *layout)
00165 {
00166     Q_ASSERT(m_optionWidget != 0);
00167     Q_ASSERT(m_optionWidgetLayout != 0);
00168     int rowCount = m_optionWidgetLayout->numRows();
00169     m_optionWidgetLayout->addMultiCellLayout(layout, rowCount, rowCount, 0, 1);
00170 }
00171 
00172 void KisToolPaint::addOptionWidgetOption(QWidget *control, QWidget *label)
00173 {
00174     Q_ASSERT(m_optionWidget != 0);
00175     Q_ASSERT(m_optionWidgetLayout != 0);
00176     if(label)
00177     {
00178         m_optionWidgetLayout->addWidget(label, m_optionWidgetLayout->numRows(), 0);
00179         m_optionWidgetLayout->addWidget(control, m_optionWidgetLayout->numRows()-1, 1);
00180     }
00181     else
00182         m_optionWidgetLayout->addMultiCellWidget(control, m_optionWidgetLayout->numRows(), m_optionWidgetLayout->numRows(), 0, 1);
00183 }
00184 
00185 void KisToolPaint::slotSetOpacity(int opacityPerCent)
00186 {
00187     m_opacity = opacityPerCent * OPACITY_OPAQUE / 100;
00188 }
00189 
00190 void KisToolPaint::slotSetCompositeMode(const KisCompositeOp& compositeOp)
00191 {
00192     m_compositeOp = compositeOp;
00193 }
00194 
00195 QCursor KisToolPaint::cursor()
00196 {
00197     return m_cursor;
00198 }
00199 
00200 void KisToolPaint::setCursor(const QCursor& cursor)
00201 {
00202     m_cursor = cursor;
00203 
00204     if (m_subject) {
00205         KisToolControllerInterface *controller = m_subject->toolController();
00206 
00207         if (controller && controller->currentTool() == this) {
00208             m_subject->canvasController()->setCanvasCursor(m_cursor);
00209         }
00210     }
00211 }
00212 
00213 void KisToolPaint::activate()
00214 {
00215     if (m_subject) {
00216         KisToolControllerInterface *controller = m_subject->toolController();
00217 
00218         if (controller)
00219             controller->setCurrentTool(this);
00220             
00221         updateCompositeOpComboBox();
00222 
00223         KisConfig cfg;
00224         m_paintOutline = (cfg.cursorStyle() == CURSOR_STYLE_OUTLINE);
00225     }
00226 }
00227 
00228 void KisToolPaint::notifyModified() const
00229 {
00230     if (m_subject && m_subject->currentImg()) {
00231         m_subject->currentImg()->setModified();
00232     }
00233 }
00234 
00235 void KisToolPaint::updateCompositeOpComboBox()
00236 {
00237     if (m_optionWidget && m_subject) {
00238         KisImageSP img = m_subject->currentImg();
00239 
00240         if (img) {
00241             KisPaintDeviceSP device = img->activeDevice();
00242 
00243             if (device) {
00244                 KisCompositeOpList compositeOps = device->colorSpace()->userVisiblecompositeOps();
00245                 m_cmbComposite->setCompositeOpList(compositeOps);
00246 
00247                 if (compositeOps.find(m_compositeOp) == compositeOps.end()) {
00248                     m_compositeOp = COMPOSITE_OVER;
00249                 }
00250                 m_cmbComposite->setCurrentItem(m_compositeOp);
00251             }
00252         }
00253     }
00254 }
00255 
00256 void KisToolPaint::slotPopupQuickHelp() {
00257     QWhatsThis::display(quickHelp());
00258 }
00259 
00260 #include "kis_tool_paint.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys