Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

painter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2002 by Yann Hodique                                    *
00003  *   Yann.Hodique@lifl.fr                                                  *
00004  *   Copyright (C) 1992-2000 Trolltech AS                                  *
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 
00012 #include "painter.h"
00013 #include "canvas.h"
00014 
00015 #include <iostream>
00016 #include <qvariant.h>
00017 #include <qcombobox.h>
00018 #include <qspinbox.h>
00019 #include <qtoolbutton.h>
00020 #include <qlayout.h>
00021 #include <qtooltip.h>
00022 #include <qwhatsthis.h>
00023 #include <qimage.h>
00024 #include <qcolordialog.h>
00025 #include <qregexp.h>
00026 
00027 PLUGIN_FACTORY(Painter,"drawing");
00028 /*
00029  *  Constructs a Painter as a child of 'parent', with the 
00030  *  name 'name' and widget flags set to 'f'.
00031  */
00032 Painter::Painter( QWidget* parent, const char* name, Master * session )
00033         : Page( parent, name, session ) {
00034     if ( !name )
00035         setName( "Painter" );
00036     PainterLayout = new QGridLayout( this, 1, 1, 6, 6, "PainterLayout");
00037 
00038     canvas = new Canvas( this, "myCustomWidget1" );
00039 
00040     PainterLayout->addMultiCellWidget( canvas, 1, 1, 0, 3 );
00041     QSpacerItem* spacer = new QSpacerItem( 330, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00042     PainterLayout->addItem( spacer, 0, 3 );
00043 
00044     color_button = new QToolButton( this, "color_button" );
00045     connect(color_button, SIGNAL(pressed()), this, SLOT(slotColor()));
00046 
00047     PainterLayout->addWidget( color_button, 0, 0 );
00048 
00049     size_box = new QSpinBox( this, "size_box" );
00050     size_box->setMaxValue( 20 );
00051     size_box->setMinValue( 1 );
00052     size_box->setValue( 3 );
00053 
00054     PainterLayout->addWidget( size_box, 0, 1 );
00055 
00056     shape_combo = new QComboBox( FALSE, this, "shape_combo" );
00057     connect(shape_combo, SIGNAL(activated(int)), this, SLOT(slotShape(int)));
00058 
00059     PainterLayout->addWidget( shape_combo, 0, 2 );
00060     languageChange();
00061     resize( QSize(548, 426).expandedTo(minimumSizeHint()) );
00062 
00063     connect( size_box, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidth( int ) ) );
00064     
00065     connect(canvas,SIGNAL(drawing(QString&)),SLOT(sendOutput(QString&)));
00066 }
00067 
00068 /*
00069  *  Destroys the object and frees any allocated resources
00070  */
00071 Painter::~Painter() {
00072     // no need to delete child widgets, Qt does it all for us
00073 }
00074 
00075 /*
00076  *  Sets the strings of the subwidgets using the current
00077  *  language.
00078  */
00079 void Painter::languageChange() {
00080     setCaption( tr( "Form1" ) );
00081     color_button->setText( tr( "Color" ) );
00082     shape_combo->clear();
00083     shape_combo->insertItem( tr( "Line" ) );
00084     shape_combo->insertItem( tr( "Circle" ) );
00085     shape_combo->insertItem( tr( "Text" ) );
00086 }
00087 
00088 void Painter::slotColor() {
00089     QColor c = QColorDialog::getColor( canvas->penColor(), this );
00090     if ( c.isValid() )
00091         canvas->setPenColor( c );
00092 }
00093 
00094 void Painter::slotWidth( int w ) {
00095     canvas->setPenWidth( w );
00096 }
00097 
00098 void Painter::slotShape(int s) {
00099     canvas->setDrawingShape((Canvas::Shape)s);
00100 }
00101 
00102 void Painter::append(const QString & s) {
00103     //std::cerr << s << std::endl;
00104     if (s.startsWith("}L")) { // Line
00105 //  QRegExp re("\\}L *0x([0-9A-Fa-f]*) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *");
00106   QRegExp re("\\}L *([0-9]*) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *"); 
00107   if (re.exactMatch(s)) {
00108       QColor col(re.cap(1).toUInt());
00109       QPoint s(re.cap(2).toInt(),re.cap(3).toInt());
00110       QPoint e(re.cap(4).toInt(),re.cap(5).toInt());
00111       int w = re.cap(6).toInt();
00112       canvas->draw(Canvas::LINE,col,s,e,w);
00113       // TODO : line width
00114   }
00115     }
00116     else if (s.startsWith("}C")) { // Circle
00117 //  QRegExp re("\\}C *0x([0-9A-Fa-f]*) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *");
00118   QRegExp re("\\}C *([0-9]*) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *"); 
00119   if (re.exactMatch(s)) {
00120       QColor col(re.cap(1).toUInt());
00121       QPoint s(re.cap(2).toInt(),re.cap(3).toInt());
00122       QPoint e(re.cap(2).toInt(),re.cap(3).toInt()+re.cap(4).toInt());
00123       int w = re.cap(5).toInt();
00124       canvas->draw(Canvas::CIRCLE,col,s,e,w);
00125   }
00126     }
00127     else if (s.startsWith("}T")) { // Text
00128 //  QRegExp re("\\}T *0x([0-9A-Fa-f]*) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *([\\w ]+) *");
00129   QRegExp re("\\}T *([0-9]*) *(\\d+) *(\\d+) *(\\d+) *(\\d+) *([\\w ]+) *");  
00130   if (re.exactMatch(s)) {
00131       QColor col(re.cap(1).toUInt());
00132       QPoint s(re.cap(2).toInt(),re.cap(3).toInt());
00133       QPoint e(re.cap(2).toInt()+re.cap(4).toInt(),re.cap(3).toInt()+re.cap(5).toInt());
00134       QString text = re.cap(6);
00135       canvas->draw(Canvas::TEXT,col,s,e,0,text);
00136   }
00137     }
00138 }
00139 
00140 void Painter::sendOutput(const QString& msg) {
00141   if(getMaster()->context()->getValue("channel") == "Dessin")
00142     getMaster()->send(msg);
00143 }

Generated on Sat May 10 15:09:27 2003 for qnet by doxygen1.3