File indexing completed on 2024-05-12 04:04:51

0001 /*
0002  * Copyright (C) 1995 Paul Olav Tvete <paul@troll.no>
0003  * Copyright (C) 2000-2008 Stephan Kulow <coolo@kde.org>
0004  * Copyright (C) 2009-2010 Parker Coates <coates@kde.org>
0005  *
0006  * License of original code:
0007  * -------------------------------------------------------------------------
0008  *   Permission to use, copy, modify, and distribute this software and its
0009  *   documentation for any purpose and without fee is hereby granted,
0010  *   provided that the above copyright notice appear in all copies and that
0011  *   both that copyright notice and this permission notice appear in
0012  *   supporting documentation.
0013  *
0014  *   This file is provided AS IS with no warranties of any kind.  The author
0015  *   shall have no liability with respect to the infringement of copyrights,
0016  *   trade secrets or any patents by this file or any part thereof.  In no
0017  *   event will the author be liable for any lost revenue or profits or
0018  *   other special, indirect and consequential damages.
0019  * -------------------------------------------------------------------------
0020  *
0021  * License of modifications/additions made after 2009-01-01:
0022  * -------------------------------------------------------------------------
0023  *   This program is free software; you can redistribute it and/or
0024  *   modify it under the terms of the GNU General Public License as
0025  *   published by the Free Software Foundation; either version 2 of
0026  *   the License, or (at your option) any later version.
0027  *
0028  *   This program is distributed in the hope that it will be useful,
0029  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0030  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0031  *   GNU General Public License for more details.
0032  *
0033  *   You should have received a copy of the GNU General Public License
0034  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0035  * -------------------------------------------------------------------------
0036  */
0037 
0038 #include "view.h"
0039 
0040 // own
0041 #include "gameselectionscene.h"
0042 #include "renderer.h"
0043 // KCardGame
0044 #include <KCardScene>
0045 // Qt
0046 #include <QResizeEvent>
0047 
0048 // ================================================================
0049 //                        class PatienceView
0050 
0051 PatienceView::PatienceView(QWidget *parent)
0052     : QGraphicsView(parent)
0053     , KGameRendererClient(Renderer::self(), QStringLiteral("background"))
0054 {
0055     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0056     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0057     setFrameStyle(QFrame::NoFrame);
0058 
0059     // This makes the background of the widget transparent so that Oxygen's
0060     // (or any other style's) window gradient is visible in unpainted areas of
0061     // the scene.
0062     QPalette p = palette();
0063     QColor c = p.color(QPalette::Base);
0064     c.setAlpha(0);
0065     p.setColor(QPalette::Base, c);
0066     setPalette(p);
0067     setBackgroundRole(QPalette::Base);
0068 
0069     setAlignment(Qt::AlignLeft | Qt::AlignTop);
0070     setCacheMode(QGraphicsView::CacheBackground);
0071 }
0072 
0073 PatienceView::~PatienceView()
0074 {
0075 }
0076 
0077 void PatienceView::setScene(QGraphicsScene *scene)
0078 {
0079     QGraphicsView::setScene(scene);
0080     updateSceneSize();
0081 }
0082 
0083 void PatienceView::resizeEvent(QResizeEvent *e)
0084 {
0085     QGraphicsView::resizeEvent(e);
0086     setRenderSize(e->size());
0087     resetCachedContent();
0088     updateSceneSize();
0089 }
0090 
0091 void PatienceView::drawBackground(QPainter *painter, const QRectF &rect)
0092 {
0093     QRectF source = rect.translated(-sceneRect().topLeft());
0094     if (m_background.size() != size()) {
0095         qreal xScale = m_background.width() / width();
0096         qreal yScale = m_background.height() / height();
0097         source = QRectF(source.x() * xScale, source.y() * yScale, source.width() * xScale, source.height() * yScale);
0098     }
0099 
0100     painter->drawPixmap(rect, m_background, source);
0101 }
0102 
0103 void PatienceView::receivePixmap(const QPixmap &pixmap)
0104 {
0105     m_background = pixmap;
0106     resetCachedContent();
0107 }
0108 
0109 void PatienceView::updateSceneSize()
0110 {
0111     KCardScene *cs = dynamic_cast<KCardScene *>(scene());
0112     if (cs) {
0113         cs->resizeScene(size());
0114     } else {
0115         GameSelectionScene *gss = dynamic_cast<GameSelectionScene *>(scene());
0116         if (gss)
0117             gss->resizeScene(size());
0118     }
0119 }