File indexing completed on 2024-05-12 16:36:45
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2009 Jean-Nicolas Artaud <jeannicolasartaud@gmail.com> 0003 * Copyright (C) 2009 Alexia Allanic <alexia_allanic@yahoo.fr> 0004 * Copyright (C) 2009 Jérémy Lugagne <jejewindsurf@hotmail.com> 0005 * Copyright (C) 2009 Johann Hingue <yoan1703@hotmail.fr> 0006 * Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org> 0007 * 0008 * This library is free software; you can redistribute it and/or 0009 * modify it under the terms of the GNU Library General Public 0010 * License as published by the Free Software Foundation; either 0011 * version 2 of the License, or (at your option) any later version. 0012 * 0013 * This library is distributed in the hope that it will be useful, 0014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 * Library General Public License for more details. 0017 * 0018 * You should have received a copy of the GNU Library General Public License 0019 * along with this library; see the file COPYING.LIB. If not, write to 0020 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0021 * Boston, MA 02110-1301, USA. 0022 */ 0023 0024 #include "KPrPresentationDrawWidget.h" 0025 0026 #include <QVBoxLayout> 0027 #include <QPainter> 0028 #include <QPaintEvent> 0029 #include <QMenu> 0030 #include <QIcon> 0031 0032 #include <klocalizedstring.h> 0033 0034 #include <KoPACanvasBase.h> 0035 #include <KoPointerEvent.h> 0036 0037 #include "StageDebug.h" 0038 0039 0040 KPrPresentationDrawWidget::KPrPresentationDrawWidget(KoPACanvasBase * canvas) 0041 : KPrPresentationToolEventForwarder(canvas) 0042 , m_draw( false ) 0043 , m_penSize( 10 ) 0044 , m_penColor( Qt::black ) 0045 { 0046 setFocusPolicy( Qt::StrongFocus ); 0047 setMouseTracking( true ); 0048 0049 resize( canvas->canvasWidget()->size() ); 0050 } 0051 0052 KPrPresentationDrawWidget::~KPrPresentationDrawWidget() 0053 { 0054 } 0055 0056 void KPrPresentationDrawWidget::paintEvent(QPaintEvent * event) 0057 { 0058 Q_UNUSED(event); 0059 QPainter painter( this ); 0060 QBrush brush( Qt::SolidPattern ); 0061 QPen pen( brush, m_penSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ); 0062 foreach ( const KPrPresentationDrawPath &path, m_pointVectors ) { 0063 pen.setColor( path.color ); 0064 pen.setWidth( path.size ); 0065 painter.setPen( pen ); 0066 painter.drawPolyline( QPolygonF( path.points ) ); 0067 } 0068 } 0069 0070 void KPrPresentationDrawWidget::mousePressEvent( QMouseEvent* e ) 0071 { 0072 KPrPresentationDrawPath path; 0073 path.color = m_penColor; 0074 path.size = m_penSize; 0075 path.points = QVector<QPointF>() << e->pos(); 0076 m_pointVectors.append( path ); 0077 m_draw = true; 0078 } 0079 0080 void KPrPresentationDrawWidget::mouseMoveEvent( QMouseEvent* e ) 0081 { 0082 if ( m_draw ) { 0083 m_pointVectors.last().points << e->pos(); 0084 update(); 0085 } 0086 } 0087 0088 void KPrPresentationDrawWidget::mouseReleaseEvent( QMouseEvent* e ) 0089 { 0090 Q_UNUSED(e); 0091 m_draw = false; 0092 } 0093 0094 void KPrPresentationDrawWidget::contextMenuEvent(QContextMenuEvent* event) 0095 { 0096 // TODO rework to not recreate the menu all the time 0097 // make strings translateable. 0098 // maybe the options which are shown here should be configurable in the 0099 // config file 0100 QMenu menu( this ); 0101 0102 QMenu *color = new QMenu( i18n("Pen Color"), &menu ); 0103 QMenu *size = new QMenu( i18n("Pen Size"), &menu ); 0104 0105 color->addAction( buildActionColor(Qt::black, i18n("Black"))); 0106 color->addAction( buildActionColor(Qt::white, i18n("White"))); 0107 color->addAction( buildActionColor(Qt::green, i18n("Green"))); 0108 color->addAction( buildActionColor(Qt::red, i18n("Red"))); 0109 color->addAction( buildActionColor(Qt::blue, i18n("Blue"))); 0110 color->addAction( buildActionColor(Qt::yellow, i18n("Yellow"))); 0111 connect( color, SIGNAL(triggered(QAction*)), this, SLOT(updateColor(QAction*)) ); 0112 0113 size->addAction( buildActionSize ( 9 ) ); 0114 size->addAction( buildActionSize ( 10 ) ); 0115 size->addAction( buildActionSize ( 12 ) ); 0116 size->addAction( buildActionSize ( 14 ) ); 0117 size->addAction( buildActionSize ( 16 ) ); 0118 size->addAction( buildActionSize ( 18 ) ); 0119 size->addAction( buildActionSize ( 20 ) ); 0120 size->addAction( buildActionSize ( 22 ) ); 0121 0122 connect( size, SIGNAL(triggered(QAction*)), this, SLOT(updateSize(QAction*)) ); 0123 0124 menu.addMenu( color ); 0125 menu.addMenu( size ); 0126 0127 menu.exec( event->globalPos() ); 0128 m_draw = false; 0129 } 0130 0131 QAction* KPrPresentationDrawWidget::buildActionSize( int size ) 0132 { 0133 QAction *action = new QAction( buildIconSize( size ), QString::number(size)+"px", this ); 0134 action->setProperty( "size", size ); 0135 return action; 0136 } 0137 0138 QAction* KPrPresentationDrawWidget::buildActionColor( const QColor &color, const QString &name ) 0139 { 0140 QAction *action; 0141 action = new QAction( buildIconColor ( color ) , name, this ); 0142 action->setProperty( "color", QVariant( color ) ); 0143 return action; 0144 } 0145 0146 QIcon KPrPresentationDrawWidget::buildIconSize( int size ) 0147 { 0148 QPen thumbPen( Qt::black, Qt::MiterJoin ); 0149 thumbPen.setCapStyle( Qt::RoundCap ); 0150 thumbPen.setWidth( size ); 0151 QPixmap thumbPixmap( QSize ( 26, 26 ) ); 0152 thumbPixmap.fill(); 0153 QPainter thumbPainter( &thumbPixmap ); 0154 thumbPainter.setPen( thumbPen ); 0155 thumbPainter.drawPoint( 13, 13 ); 0156 QIcon thumbIcon( thumbPixmap ); 0157 return thumbIcon; 0158 } 0159 0160 QIcon KPrPresentationDrawWidget::buildIconColor( const QColor &color ) 0161 { 0162 QPixmap thumbPixmap( QSize ( 24, 20 ) ); 0163 thumbPixmap.fill( color ); 0164 QIcon thumbIcon( thumbPixmap ); 0165 return thumbIcon; 0166 } 0167 0168 void KPrPresentationDrawWidget::updateSize( QAction *size ) 0169 { 0170 m_penSize = size->property( "size" ).toInt(); 0171 m_draw = false; 0172 } 0173 0174 void KPrPresentationDrawWidget::updateSize(int size) 0175 { 0176 m_penSize=size; 0177 m_draw=false; 0178 } 0179 0180 void KPrPresentationDrawWidget::updateColor( QAction *color ) 0181 { 0182 m_penColor = color->property( "color" ).value<QColor>(); 0183 m_draw = false; 0184 } 0185 0186 void KPrPresentationDrawWidget::updateColor(const QString &color) 0187 { 0188 m_penColor.setNamedColor(color); 0189 m_draw=false; 0190 }