From kde-commits Tue May 31 23:27:29 2011 From: Peter Hedlund Date: Tue, 31 May 2011 23:27:29 +0000 To: kde-commits Subject: =?utf-8?q?=5Bkwordquiz=5D_src=3A_Make_sure_images_on_flashcards_?= Message-Id: <20110531232729.78023A60A4 () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=130688448222153 Git commit 1ebe5fda2d064e42b34cb8630163c8bd342e815d by Peter Hedlund. Committed on 01/06/2011 at 01:18. Pushed by hedlund into branch 'master'. Make sure images on flashcards are painted with best possible resolution. BUG:273276 M +1 -0 src/CMakeLists.txt M +7 -22 src/kwqcardscene.cpp M +5 -3 src/kwqcardscene.h M +1 -1 src/kwqcardview.cpp M +1 -1 src/kwqcardview.h A +50 -0 src/kwqpixmapitem.cpp [License: GPL (v2+)] A +37 -0 src/kwqpixmapitem.h [License: GPL (v2+)] http://commits.kde.org/kwordquiz/1ebe5fda2d064e42b34cb8630163c8bd342e815d diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4c9ee3c..fef14a2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,7 @@ set(kwordquiz_SRCS kwqcardview.cpp kwqquizview.cpp kwqcleardialog.cpp + kwqpixmapitem.cpp ) diff --git a/src/kwqcardscene.cpp b/src/kwqcardscene.cpp index 41a9d5c..272e552 100644 --- a/src/kwqcardscene.cpp +++ b/src/kwqcardscene.cpp @@ -1,7 +1,7 @@ /*************************************************************************** kwqcardscene.cpp ------------------- - copyright : (C) 2009-2010 by Peter Hedlund + copyright : (C) 2009-2011 by Peter Hedlund email : peter.hedlund@kdemail.net ***************************************************************************/ @@ -55,13 +55,8 @@ KWQCardScene::KWQCardScene(QObject *parent) : QGraphicsScene(parent) f.setPointSize(12); m_text->setParentItem(m_textArea); - m_imageArea = addRect(textMargin, cardMargin * 4, cardWidth - (textMargin * 2), cardHeight - (cardMargin * 5)); - m_imageArea->setPen(Qt::NoPen); - m_imageArea->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); - m_imageArea->setZValue(3); - - m_pixmap = addPixmap(QPixmap()); - m_pixmap->setParentItem(m_imageArea); + m_pixmap = new KWQPixmapItem(QPixmap(), m_card); + m_pixmap->setImageRect(QRect(textMargin, cardMargin * 4, cardWidth - (textMargin * 2), cardHeight - (cardMargin * 5))); setIdentifier(""); setText(""); @@ -119,18 +114,8 @@ void KWQCardScene::setFrameColor(const QColor &frameColor) void KWQCardScene::setImage(const QPixmap &image) { - if (!image.isNull()) { - realign(false); - QSize s; - s.setWidth(m_imageArea->rect().width()); - s.setHeight(m_imageArea->rect().height()); - m_pixmap->setPixmap(image.scaled(s, Qt::KeepAspectRatio)); - } - else - { - realign(true); - m_pixmap->setPixmap(image); //clear image - } + realign(image.isNull()); + m_pixmap->setPixmap(image); repositionText(); } @@ -139,7 +124,7 @@ void KWQCardScene::realign(bool textOnly) if (textOnly) { m_textArea->setRect(textMargin, cardMargin * 4, cardWidth - (textMargin * 2), cardHeight - (cardMargin * 5)); } else { - m_imageArea->setRect(textMargin, cardMargin * 4, (cardWidth / 2) - textMargin, cardHeight - (cardMargin * 5)); + m_pixmap->setImageRect(QRect(textMargin, cardMargin * 4, (cardWidth / 2) - textMargin, cardHeight - (cardMargin * 5))); m_textArea->setRect((cardWidth / 2), cardMargin * 4, (cardWidth / 2) - textMargin, cardHeight - (cardMargin * 5)); } } @@ -153,7 +138,7 @@ void KWQCardScene::repositionText() m_text->setTextWidth(cardWidth - (textMargin * 2)); m_text->setPos(textMargin, h); } else { - h = ((cardMargin * 4) + ((cardHeight - (cardMargin * 5)) - m_pixmap->boundingRect().height()) / 2); + h = ((cardMargin * 4) + ((cardHeight - (cardMargin * 5)) - m_textArea->boundingRect().height()) / 2); m_pixmap->setPos(textMargin, h); h = ((cardMargin * 4) + ((cardHeight - (cardMargin * 5)) - m_text->boundingRect().height()) / 2); m_text->setTextWidth((cardWidth / 2) - textMargin); diff --git a/src/kwqcardscene.h b/src/kwqcardscene.h index a8e4191..59aba31 100644 --- a/src/kwqcardscene.h +++ b/src/kwqcardscene.h @@ -1,7 +1,7 @@ /*************************************************************************** kwqcardscene.h ------------------- - copyright : (C) 2009-2010 by Peter Hedlund + copyright : (C) 2009-2011 by Peter Hedlund email : peter.hedlund@kdemail.net ***************************************************************************/ @@ -22,6 +22,9 @@ #include #include #include +#include + +#include "kwqpixmapitem.h" class KWQCardScene : public QGraphicsScene { @@ -56,8 +59,7 @@ private: QGraphicsSimpleTextItem *m_identifier; QGraphicsRectItem *m_textArea; QGraphicsTextItem *m_text; - QGraphicsRectItem *m_imageArea; - QGraphicsPixmapItem *m_pixmap; + KWQPixmapItem *m_pixmap; void realign(bool ); void repositionText(); diff --git a/src/kwqcardview.cpp b/src/kwqcardview.cpp index fccffc9..e60b80f 100644 --- a/src/kwqcardview.cpp +++ b/src/kwqcardview.cpp @@ -1,7 +1,7 @@ /*************************************************************************** kwqcardview.cpp ------------------- - copyright : (C) 2009-2010 by Peter Hedlund + copyright : (C) 2009-2011 by Peter Hedlund email : peter.hedlund@kdemail.net ***************************************************************************/ diff --git a/src/kwqcardview.h b/src/kwqcardview.h index 7853bda..7b08fd4 100644 --- a/src/kwqcardview.h +++ b/src/kwqcardview.h @@ -1,7 +1,7 @@ /*************************************************************************** kwqcardview.h ------------------- - copyright : (C) 2009-2010 by Peter Hedlund + copyright : (C) 2009-2011 by Peter Hedlund email : peter.hedlund@kdemail.net ***************************************************************************/ diff --git a/src/kwqpixmapitem.cpp b/src/kwqpixmapitem.cpp new file mode 100644 index 0000000..0e66399 --- /dev/null +++ b/src/kwqpixmapitem.cpp @@ -0,0 +1,50 @@ +/*************************************************************************** + kwqpixmapitem.cpp + ------------------- + copyright : (C) 2011 by Peter Hedlund + email : peter.hedlund@kdemail.net + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + + +#include "kwqpixmapitem.h" + +#include + +KWQPixmapItem::KWQPixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem) : QGraphicsPixmapItem(pixmap, parentItem) +{ + setCacheMode(NoCache); + setTransformationMode(Qt::SmoothTransformation); +} + + +QRectF KWQPixmapItem::boundingRect() const +{ + return m_imageRect; +} + + +void KWQPixmapItem::setImageRect(const QRect& rect) +{ + m_imageRect = rect; +} + + +void KWQPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem* options, QWidget* widget) +{ + Q_UNUSED(options); + Q_UNUSED(widget); + QRect viewRect = painter->combinedTransform().mapRect(boundingRect().toRect()); + QPixmap pm = pixmap().scaled(viewRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); + QSize pixmapSize = pm.size(); + pixmapSize.scale(QSizeF(boundingRect().width(), boundingRect().height()).toSize(), Qt::KeepAspectRatio); + painter->drawPixmap((boundingRect().width() - pixmapSize.width()) / 2, (boundingRect().height() - pixmapSize.height()) / 2, pixmapSize.width(), pixmapSize.height(), pm); +} diff --git a/src/kwqpixmapitem.h b/src/kwqpixmapitem.h new file mode 100644 index 0000000..9776f24 --- /dev/null +++ b/src/kwqpixmapitem.h @@ -0,0 +1,37 @@ +/*************************************************************************** + kwqpixmapitem.h + ------------------- + copyright : (C) 2011 by Peter Hedlund + email : peter.hedlund@kdemail.net + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + + +#ifndef KWQPIXMAPITEM_H +#define KWQPIXMAPITEM_H + +#include + +class KWQPixmapItem: public QGraphicsPixmapItem +{ +public: + KWQPixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem = 0); + +public: + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + void setImageRect(const QRect &rect); + +private: + QRect m_imageRect; +}; + +#endif // KWQPIXMAPITEM_H