gameview.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE games lskat program
00003    Copyright (c) 2006 Martin Heni <kde@heni-online.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // Qt includes
00022 #include <QPoint>
00023 #include <QFont>
00024 #include <QTimer>
00025 
00026 // KDE includes
00027 #include <klocale.h>
00028 #include <kmessagebox.h>
00029 #include <kdebug.h>
00030 #include <kstandarddirs.h>
00031 
00032 // Local includes
00033 #include "gameview.h"
00034 #include "thememanager.h"
00035 #include "lskatglobal.h"
00036 
00037 
00038 // Constructor for the view
00039 GameView::GameView(QSize size, int advancePeriod, QGraphicsScene* scene, ThemeManager* theme, QWidget* parent)
00040         : QGraphicsView(scene, parent)
00041 {
00042   // Store attributes    
00043   mTheme  = theme;
00044 
00045   // We do not need scrolling so switch it off
00046   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00047   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00048 
00049   // Cache on
00050   setCacheMode(QGraphicsView::CacheBackground);
00051 
00052 
00053   // Update/advance in [ms]
00054   QTimer *timer = new QTimer(this);
00055   connect(timer, SIGNAL(timeout()), this, SLOT(updateAndAdvance()));
00056   timer->start(advancePeriod);
00057   
00058   // Set size and position of the view and the canvas:
00059   // they are reseized once a level is loaded
00060   resize(size);
00061   scene->setSceneRect(0, 0, this->width(), this->height()); 
00062   adjustSize();
00063 
00064   // Enable mouse
00065   setInteractive(true);
00066 
00067   // Scale theme
00068   mTheme->rescale(this->width());
00069 }
00070 
00071 
00072 // Advance and update canvas
00073 void GameView::updateAndAdvance()
00074 {
00075   scene()->advance();
00076   //NOTE regarding QGV porting
00077   //QGV will handle dirty rects for us
00078   //Calling update will just dirty the view and cause a full redraw, killing performance
00079   //scene()->update();
00080 }
00081 
00082 
00083 // Slot called by the framework when the window is
00084 // resized.
00085 void GameView::resizeEvent(QResizeEvent* e)
00086 {
00087   if (global_debug > 2) kDebug() <<"RESIZE EVENT " << e->size() << endl;
00088 
00089   // Adapt the canvas size to the window size
00090   if (scene())
00091   {
00092     scene()->setSceneRect(0,0, e->size().width(), e->size().height());
00093   }
00094 
00095 
00096   QSizeF size = QSizeF(e->size());
00097   // Rescale on minimum fitting aspect ratio either width or height limiting
00098   double aspect = size.width() / size.height();
00099   if (global_debug > 2) kDebug() << "Aspect=" << aspect << " theme aspect="<< mTheme->aspectRatio() << endl;
00100   if (aspect > mTheme->aspectRatio()) mTheme->rescale(int(e->size().height()*mTheme->aspectRatio()));
00101   else mTheme->rescale(int(e->size().width()));
00102 }
00103 
00104 
00105 // Our subclassed (temporary) QGraphicsView paintEvent, see header file
00106 void GameView::paintEvent(QPaintEvent* event)
00107 {
00108     QPaintEvent* newEvent = new QPaintEvent(event->region().boundingRect());
00109     QGraphicsView::paintEvent(newEvent);
00110     delete newEvent;
00111 }
00112 
00113 
00114 // Mouse click event
00115 void GameView::mousePressEvent(QMouseEvent *ev)
00116 {
00117   if (ev->button() != Qt::LeftButton) return ;
00118 
00119   QPointF point = ev->pos();
00120   emit signalLeftMousePress(point.toPoint());
00121 }
00122 
00123 
00124 #include "gameview.moc"

Generated on Tue May 1 09:34:40 2007 for LSkat by  doxygen 1.4.6