F:/KPlato/koffice/libs/kofficecore/tests/rtreetestapp.cpp

Aller à la documentation de ce fichier.
00001 /*
00002  *  Copyright (c) 2006 Thorsten Zachmann <zachmann@kde.org>
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 <QApplication>
00020 #include <QtGui>
00021 #include <QPainterPath>
00022 #include <QLayout>
00023 #include <QPoint>
00024 #include <QPointF>
00025 #include <QDebug>
00026 #include "rtreetestapp.h"
00027 #include "Tool.h"
00028 
00029 int main(int argc, char *argv[])
00030 {
00031     QApplication app(argc, argv);
00032     MainWindow mainWin;
00033 
00034     mainWin.show();
00035     return app.exec();
00036 }
00037 
00038 Canvas::Canvas( )
00039 : QWidget()
00040 , m_zoom( 1 )    
00041 , m_rtree( 4, 2 )
00042 //, m_rtree( 2, 1 )
00043 , m_tool( 0 )
00044 , m_createTool( this )    
00045 , m_selectTool( this )    
00046 , m_removeTool( this )    
00047 , m_file( "data.trc" )    
00048 , m_listId( 0 )
00049 , m_paintTree( false )
00050 {
00051     m_tool = &m_createTool;
00052     setBackgroundRole(QPalette::Base);
00053     m_file.open( QIODevice::WriteOnly );
00054     m_out.setDevice( &m_file );
00055 }
00056 
00057 void Canvas::updateCanvas()
00058 {
00059     update();
00060 }
00061 
00062 void Canvas::insert( QRectF & rect )
00063 {
00064     m_out << "i " << rect.left() << " " << rect.top() << " " << rect.width() << " " << rect.height() << "\n";
00065     Data * data = new Data( rect );
00066     m_rects.insert( data );
00067     m_rtree.insert( rect, data );
00068     update();
00069 }
00070 
00071 void Canvas::select( QRectF & rect )
00072 {
00073     if ( rect.isEmpty() )
00074     {
00075         m_found = m_rtree.contains( rect.topLeft() );
00076     }
00077     else
00078     {
00079         m_found = m_rtree.intersects( rect );
00080     }
00081     update();
00082 }
00083 
00084 void Canvas::remove( QRectF & rect )
00085 {
00086     m_out << "r " << rect.left() << " " << rect.top() << " " << rect.width() << " " << rect.height() << "\n";
00087     m_found = QList<Data *>();
00088     QList<Data *> remove = m_rtree.intersects( rect );
00089     foreach ( Data * data, remove )
00090     {
00091         m_rtree.remove( data );
00092         m_rects.remove( data );
00093         delete data;
00094     }
00095     update();
00096 }
00097 
00098 void Canvas::replay()
00099 {
00100     if ( QCoreApplication::arguments().size() > 1 )
00101     {
00102         QString filename( QCoreApplication::arguments().at( 1 ) );
00103         qDebug() << "parameter:" << filename;
00104         QFile file( filename ); 
00105         file.open( QIODevice::ReadOnly | QIODevice::Text );
00106         QTextStream in( &file );
00107         while ( !in.atEnd() ) 
00108         {
00109             m_list.push_back( in.readLine() );
00110         }
00111         m_listId = 0;
00112         QTimer::singleShot( 1000, this, SLOT( replayStep() ) );
00113     }
00114 }
00115 
00116 void Canvas::replayStep()
00117 {
00118 
00119     QString line = m_list.at( m_listId++ );
00120     qDebug() << "Line:" << line;
00121     QStringList values = line.split( " " );
00122     int left = values[1].toInt();
00123     int top = values[2].toInt();
00124     int right = values[3].toInt();
00125     int bottom = values[4].toInt();
00126     QRectF rect( left, top, right, bottom );
00127     if ( values[0] == "i" )
00128     {
00129         insert( rect );
00130     }
00131     else if ( values[0] == "r" )
00132     {
00133         remove( rect );
00134     }
00135 
00136     update();
00137     if ( m_listId < m_list.size() )
00138     {
00139         int sleep = 1000;
00140         if ( QCoreApplication::arguments().size() >= 3 )
00141         {
00142             sleep = QCoreApplication::arguments().at( 2 ).toInt();
00143         }
00144         QTimer::singleShot( sleep, this, SLOT( replayStep() ) );
00145     }
00146 }
00147 
00148 void Canvas::debug()
00149 {
00150     m_rtree.debug();
00151 }
00152 
00153 void Canvas::paintTree( bool paintTree )
00154 {
00155     m_paintTree = paintTree;
00156     update();
00157 }
00158    
00159 void Canvas::paintEvent(QPaintEvent * e)
00160 {
00161     Q_UNUSED( e );
00162     QPainter p( this );
00163     p.setRenderHint(QPainter::Antialiasing);
00164     p.scale(m_zoom, m_zoom);
00165     
00166     if ( m_tool )
00167         m_tool->paint( p );
00168 
00169     QPen pen( Qt::black );
00170     p.setPen( pen );
00171     foreach ( Data * data, m_rects )
00172     {
00173         data->paint( p );
00174     }
00175 
00176     if ( m_paintTree )
00177     {
00178         m_rtree.paint( p );
00179     }
00180 
00181     foreach ( Data * data, m_found )
00182     {
00183         QColor c( Qt::yellow );
00184         c.setAlphaF( 0.1 );
00185         QBrush brush( c );
00186         p.setBrush( brush );
00187         p.drawRect( data->boundingBox() );
00188     }
00189 }
00190 
00191 void Canvas::mouseMoveEvent(QMouseEvent *e)
00192 {
00193     if ( m_tool )
00194     {
00195         m_tool->mouseMoveEvent( e );
00196     }
00197 }
00198 
00199 
00200 void Canvas::mousePressEvent(QMouseEvent *e)
00201 {
00202     if ( m_tool )
00203     {
00204         m_tool->mousePressEvent( e );
00205     }
00206 }
00207 
00208 void Canvas::mouseReleaseEvent(QMouseEvent *e)
00209 {
00210     if ( m_tool )
00211     {
00212         m_tool->mouseReleaseEvent( e );
00213     }
00214 }
00215 
00216 void Canvas::selectInsertTool()
00217 {
00218     m_tool = &m_createTool;
00219 }
00220 
00221 void Canvas::selectSelectTool()
00222 {
00223     m_tool = &m_selectTool;
00224 }
00225 
00226 void Canvas::selectRemoveTool()
00227 {
00228     m_tool = &m_removeTool;
00229 }
00230 
00231 MainWindow::MainWindow()
00232 {
00233     m_canvas = new Canvas();
00234     setCentralWidget(m_canvas);
00235     m_canvas->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
00236     resize( 640, 480 );
00237     createActions();
00238     createMenus();
00239     createToolBars();
00240     createStatusBar();
00241 
00242     setWindowTitle(tr("R-Tree Library Test Application"));
00243 
00244     m_canvas->repaint();
00245 
00246     repaint();
00247 }
00248 
00249 void MainWindow::about()
00250 {
00251    QMessageBox::about(this, tr("About test"),
00252             tr("R-Tree Library Test Application"));
00253 }
00254 
00255 void MainWindow::createActions()
00256 {
00257     m_quitAct = new QAction(tr("&Quit"), this);
00258     m_quitAct->setShortcut(tr("Ctrl+Q"));
00259     m_quitAct->setStatusTip(tr("Quit the application"));
00260     connect(m_quitAct, SIGNAL(triggered()), this, SLOT(close()));
00261 
00262     m_aboutAct = new QAction(tr("&About"), this);
00263     m_aboutAct->setStatusTip(tr("Show the application's About box"));
00264     connect(m_aboutAct, SIGNAL(triggered()), this, SLOT(about()));
00265 
00266     m_aboutQtAct = new QAction(tr("About &Qt"), this);
00267     m_aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
00268     connect(m_aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
00269 
00270     m_insertAct = new QAction(tr("&Insert"), this);
00271     m_insertAct->setStatusTip(tr("Insert Object"));
00272     connect(m_insertAct, SIGNAL(triggered()), m_canvas, SLOT( selectInsertTool() ) );
00273 
00274     m_selectAct = new QAction(tr("&Select"), this);
00275     m_selectAct->setStatusTip(tr("Select Objects"));
00276     connect(m_selectAct, SIGNAL(triggered()), m_canvas, SLOT( selectSelectTool() ) );
00277 
00278     m_removeAct = new QAction(tr("&Remove"), this);
00279     m_removeAct->setStatusTip(tr("Remove Object"));
00280     connect(m_removeAct, SIGNAL(triggered()), m_canvas, SLOT( selectRemoveTool() ) );
00281 
00282     m_replayAct = new QAction(tr("&Replay"), this);
00283     m_replayAct->setShortcut(tr("Ctrl+R"));
00284     m_replayAct->setStatusTip(tr("Replay"));
00285     connect(m_replayAct, SIGNAL(triggered()), m_canvas, SLOT( replay() ) );
00286 
00287     m_debugAct = new QAction(tr("&Debug"), this);
00288     m_debugAct->setShortcut(tr("Ctrl+D"));
00289     m_debugAct->setStatusTip(tr("Debug"));
00290     connect(m_debugAct, SIGNAL(triggered()), m_canvas, SLOT( debug() ) );
00291 
00292     m_paintTreeAct = new QAction(tr("&Paint Tree"), this);
00293     m_paintTreeAct->setShortcut(tr("Ctrl+P"));
00294     m_paintTreeAct->setStatusTip(tr("Paint Tree"));
00295     m_paintTreeAct->setCheckable( true );
00296     connect(m_paintTreeAct, SIGNAL(toggled( bool )), m_canvas, SLOT( paintTree( bool ) ) );
00297 }
00298 
00299 void MainWindow::createMenus()
00300 {
00301     m_fileMenu = menuBar()->addMenu(tr("&File"));
00302     m_fileMenu->addSeparator();
00303     m_fileMenu->addAction(m_replayAct);
00304     m_fileMenu->addAction(m_debugAct);
00305     m_fileMenu->addAction(m_paintTreeAct);
00306     m_fileMenu->addAction(m_quitAct);
00307 
00308     m_editMenu = menuBar()->addMenu(tr("&Edit"));
00309     m_editMenu->addAction(m_insertAct);
00310     m_editMenu->addAction(m_selectAct);
00311     m_editMenu->addAction(m_removeAct);
00312     menuBar()->addSeparator();
00313 
00314 
00315     m_helpMenu = menuBar()->addMenu(tr("&Help"));
00316     m_helpMenu->addAction(m_aboutAct);
00317     m_helpMenu->addAction(m_aboutQtAct);
00318 }
00319 
00320 void MainWindow::createToolBars()
00321 {
00322 }
00323 
00324 void MainWindow::createStatusBar()
00325 {
00326     statusBar()->showMessage(tr("Ready"));
00327 }
00328 
00329 #include "rtreetestapp.moc"

Généré le Wed Nov 22 23:41:04 2006 pour KPlato par  doxygen 1.5.1-p1