File indexing completed on 2024-04-21 05:43:21

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "dptext.h"
0012 #include "itemdocument.h"
0013 #include "libraryitem.h"
0014 #include "resizeoverlay.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QPainter>
0019 #include <QTextEdit>
0020 
0021 // #include <q3simplerichtext.h> // 2018.08.13 - not needed anymore
0022 // #include <q3stylesheet.h>
0023 
0024 Item *DPText::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0025 {
0026     return new DPText(itemDocument, newItem, id);
0027 }
0028 
0029 LibraryItem *DPText::libraryItem()
0030 {
0031     QStringList idList;
0032     idList << "dp/text"
0033            << "dp/canvas_text"
0034            << "canvas_text";
0035 
0036     return new LibraryItem(idList, i18n("Canvas Text"), i18n("Other"), QIcon::fromTheme("text"), LibraryItem::lit_drawpart, DPText::construct);
0037 }
0038 
0039 DPText::DPText(ItemDocument *itemDocument, bool newItem, const char *id)
0040     : DrawPart(itemDocument, newItem, id ? id : "canvas_text")
0041 {
0042     m_rectangularOverlay = new RectangularOverlay(this);
0043     m_name = i18n("Text");
0044 
0045     createProperty("text", Variant::Type::RichText);
0046     property("text")->setValue(i18n("Text"));
0047 
0048     createProperty("background", Variant::Type::Bool);
0049     property("background")->setValue(false);
0050     property("background")->setCaption(i18n("Display Background"));
0051     property("background")->setAdvanced(true);
0052 
0053     createProperty("background-color", Variant::Type::Color);
0054     property("background-color")->setValue(QColor(Qt::white));
0055     property("background-color")->setCaption(i18n("Background Color"));
0056     property("background-color")->setAdvanced(true);
0057 
0058     createProperty("frame-color", Variant::Type::Color);
0059     property("frame-color")->setValue(QColor(Qt::black));
0060     property("frame-color")->setCaption(i18n("Frame Color"));
0061     property("frame-color")->setAdvanced(true);
0062 }
0063 
0064 DPText::~DPText()
0065 {
0066 }
0067 
0068 void DPText::setSelected(bool yes)
0069 {
0070     if (yes == isSelected())
0071         return;
0072 
0073     DrawPart::setSelected(yes);
0074     m_rectangularOverlay->showResizeHandles(yes);
0075 }
0076 
0077 void DPText::dataChanged()
0078 {
0079     b_displayBackground = dataBool("background");
0080     m_backgroundColor = dataColor("background-color");
0081     m_frameColor = dataColor("frame-color");
0082 
0083     m_text = dataString("text");
0084 
0085     if (!Qt::mightBeRichText(m_text)) {
0086         // Format the text to be HTML
0087         m_text.replace('\n', "<br>");
0088     }
0089 
0090     update();
0091 }
0092 
0093 void DPText::postResize()
0094 {
0095     setItemPoints(QPolygon(m_sizeRect), false);
0096 }
0097 
0098 QSize DPText::minimumSize() const
0099 {
0100     return QSize(48, 24);
0101 }
0102 
0103 void DPText::drawShape(QPainter &p)
0104 {
0105     QRect bound = m_sizeRect;
0106     bound.setWidth(bound.width() - 2);
0107     bound.setHeight(bound.height() - 2);
0108     bound.translate(int(x() + 1), int(y() + 1));
0109 
0110     if (b_displayBackground) {
0111         p.save();
0112         p.setPen(QPen(m_frameColor, 1, Qt::DotLine));
0113         p.setBrush(m_backgroundColor);
0114         p.drawRect(bound);
0115         p.restore();
0116     }
0117 
0118     const int pad = 6;
0119 
0120     bound.setLeft(bound.left() + pad);
0121     bound.setTop(bound.top());
0122     bound.setRight(bound.right() - pad);
0123     bound.setBottom(bound.bottom() - pad);
0124 
0125     QTextEdit *t = new QTextEdit(m_text);
0126     t->resize(bound.width(), bound.height()); // t->setWidth( bound.width() );
0127     t->viewport()->setAutoFillBackground(false);
0128     t->setFrameStyle(QFrame::NoFrame);
0129 
0130     t->render(&p, bound.topLeft(), QRegion(), QWidget::DrawChildren); // t->draw( &p, bound.left(), bound.top(), bound, QColorGroup() );
0131     delete t;
0132 }