File indexing completed on 2023-05-30 10:45:25

0001 /*
0002     SPDX-FileCopyrightText: 2009-2011 Peter Hedlund <peter.hedlund@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kwqcardscene.h"
0007 
0008 #include <QFontMetrics>
0009 #include <QGraphicsSceneMouseEvent>
0010 #include <QGraphicsDropShadowEffect>
0011 #include <QTextOption>
0012 #include <QTextDocument>
0013 
0014 static const int cardWidth = 500;
0015 static const int cardHeight = 300;
0016 static const int cardMargin = 20;
0017 static const int textMargin = 30;
0018 static const int shadowOffset = 2;
0019 
0020 KWQCardScene::KWQCardScene(QObject *parent) : QGraphicsScene(parent)
0021 {
0022     QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
0023     shadowEffect->setBlurRadius(5);
0024     shadowEffect->setOffset(shadowOffset);
0025 
0026     m_card = addRect(0, 0, cardWidth, cardHeight);
0027     m_card->setGraphicsEffect(shadowEffect);
0028 
0029     m_line = addLine(cardMargin, cardMargin * 3, cardWidth - cardMargin, cardMargin * 3);
0030     m_identifier = addSimpleText(QLatin1String(""));
0031 
0032     QFont f = m_identifier->font();
0033     f.setPixelSize(cardMargin);
0034     m_identifier->setFont(f);
0035     m_identifier->setPos(cardMargin, cardMargin);
0036 
0037     m_textArea = addRect(textMargin, cardMargin * 4, cardWidth - (textMargin * 2), cardHeight - (cardMargin * 5));
0038     m_textArea->setPen(Qt::NoPen);
0039     m_textArea->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
0040     m_textArea->setZValue(4);
0041 
0042     m_text = addText(QLatin1String(""));
0043     m_text->setTextWidth(cardWidth - (textMargin * 2));
0044     f.setPointSize(12);
0045     m_text->setParentItem(m_textArea);
0046 
0047     m_pixmap = new KWQPixmapItem(QPixmap(), m_card);
0048     m_pixmap->setImageRect(QRect(textMargin, cardMargin * 4, cardWidth - (textMargin * 2), cardHeight - (cardMargin * 5)));
0049 
0050     setIdentifier(QLatin1String(""));
0051     setText(QLatin1String(""));
0052     setTextColor(Qt::blue);
0053     setTextFont(f);
0054     setCardColor(Qt::white);
0055     setFrameColor(Qt::red);
0056 }
0057 
0058 const QSize KWQCardScene::minimumSizeHint() const
0059 {
0060     int width = cardWidth / 10;
0061     int height = cardHeight / 10;
0062     return QSize((width) + 10, (height) + 10);  // the +10 is to provide padding and to avoid scrollbars
0063 }
0064 
0065 void KWQCardScene::setIdentifier(const QString &identifier)
0066 {
0067     QFontMetrics fm(m_identifier->font());
0068     QString s = fm.elidedText(identifier, Qt::ElideRight, cardWidth - (textMargin * 2));
0069     m_identifier->setText(identifier);
0070 }
0071 
0072 void KWQCardScene::setText(const QString &text)
0073 {
0074     QTextOption option = m_text->document()->defaultTextOption();
0075     option.setAlignment(Qt::AlignCenter);
0076     m_text->document()->setDefaultTextOption(option);
0077     m_text->setPlainText(text);
0078     repositionText();
0079 }
0080 
0081 void KWQCardScene::setTextColor(const QColor &textColor)
0082 {
0083     m_text->setDefaultTextColor(textColor);
0084 }
0085 
0086 void KWQCardScene::setTextFont(const QFont &font)
0087 {
0088     m_text->setFont(font);
0089     repositionText();
0090 }
0091 
0092 void KWQCardScene::setCardColor(const QColor &cardColor)
0093 {
0094     m_card->setBrush(QBrush(cardColor));
0095     m_textArea->setBrush(QBrush(cardColor));
0096 }
0097 
0098 void KWQCardScene::setFrameColor(const QColor &frameColor)
0099 {
0100     m_card->setPen(QPen(frameColor));
0101     m_line->setPen(QPen(frameColor));
0102 }
0103 
0104 void KWQCardScene::setImage(const QPixmap &image)
0105 {
0106     realign(image.isNull());
0107     m_pixmap->setPixmap(image);
0108     repositionText();
0109 }
0110 
0111 void KWQCardScene::realign(bool textOnly)
0112 {
0113     if (textOnly) {
0114         m_textArea->setRect(textMargin, cardMargin * 4, cardWidth - (textMargin * 2), cardHeight - (cardMargin * 5));
0115     } else {
0116         m_pixmap->setImageRect(QRect(textMargin, cardMargin * 4, (cardWidth / 2) - textMargin,  cardHeight - (cardMargin * 5)));
0117         m_textArea->setRect((cardWidth / 2), cardMargin * 4, (cardWidth / 2) - textMargin, cardHeight - (cardMargin * 5));
0118     }
0119 }
0120 
0121 void KWQCardScene::repositionText()
0122 {
0123     int h = 0;
0124 
0125     if (m_pixmap->pixmap().isNull()) {
0126         h = ((cardMargin * 4) + ((cardHeight - (cardMargin * 5)) - m_text->boundingRect().height()) / 2);
0127         m_text->setTextWidth(cardWidth - (textMargin * 2));
0128         m_text->setPos(textMargin, h);
0129     } else {
0130         h = ((cardMargin * 4) + ((cardHeight - (cardMargin * 5)) - m_textArea->boundingRect().height()) / 2);
0131         m_pixmap->setPos(textMargin, h);
0132         h = ((cardMargin * 4) + ((cardHeight - (cardMargin * 5)) - m_text->boundingRect().height()) / 2);
0133         m_text->setTextWidth((cardWidth / 2) - textMargin);
0134         m_text->setPos((cardWidth + (cardWidth / 2) - m_text->boundingRect().width() - textMargin) / 2, h);
0135     }
0136 }
0137 
0138 void KWQCardScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
0139 {
0140     QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
0141     if (item != 0)
0142         Q_EMIT cardClicked();
0143     QGraphicsScene::mouseReleaseEvent(mouseEvent);
0144 }