File indexing completed on 2024-04-21 04:32:15

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
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  * Some of the concepts and code behind the rich text editor have been
0011  * copied / modified from the TextEdit example program from here:
0012  * http://qt-project.org/doc/qt-4.8/demos-textedit.html
0013  */
0014 
0015 #include "TextElementDlg.h"
0016 
0017 #include <QButtonGroup>
0018 #include <QColorDialog>
0019 
0020 #include <KHelpClient>
0021 #include <KLocalizedString>
0022 
0023 #include "Element.h"
0024 
0025 TextElementDlg::TextElementDlg(QWidget *parent, TextElement *textElement)
0026     : QDialog(parent)
0027     , m_textElement(textElement)
0028 {
0029     setWindowTitle(i18n("Text Element Properties"));
0030 
0031     ui.setupUi(this);
0032 
0033     ui.BoldButton->setIcon(QIcon::fromTheme(QStringLiteral("format-text-bold")));
0034     ui.UnderlineButton->setIcon(QIcon::fromTheme(QStringLiteral("format-text-underline")));
0035     ui.ItalicButton->setIcon(QIcon::fromTheme(QStringLiteral("format-text-italic")));
0036     ui.AlignLeft->setIcon(QIcon::fromTheme(QStringLiteral("format-justify-left")));
0037     ui.AlignHCenter->setIcon(QIcon::fromTheme(QStringLiteral("format-justify-center")));
0038     ui.AlignRight->setIcon(QIcon::fromTheme(QStringLiteral("format-justify-right")));
0039     ui.AlignJustify->setIcon(QIcon::fromTheme(QStringLiteral("format-justify-fill")));
0040 
0041     QButtonGroup *group = new QButtonGroup(this);
0042     group->addButton(ui.AlignLeft);
0043     group->addButton(ui.AlignHCenter);
0044     group->addButton(ui.AlignRight);
0045     group->addButton(ui.AlignJustify);
0046     group->setExclusive(true);
0047     connect(group, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this, &TextElementDlg::textAlign);
0048     alignmentChanged(m_textElement->m_alignment);
0049 
0050     ui.MarginLeft->setValue(m_textElement->m_margins.left());
0051     ui.MarginTop->setValue(m_textElement->m_margins.top());
0052     ui.MarginRight->setValue(m_textElement->m_margins.right());
0053     ui.MarginBottom->setValue(m_textElement->m_margins.bottom());
0054 
0055     ui.ShowBorder->setChecked(m_textElement->m_showBorder);
0056     ui.BorderColor->setColor(m_textElement->m_borderColor);
0057     ui.BorderThickness->setValue(double(m_textElement->m_borderThickness) / 10);
0058 
0059     ui.BackgroundColor->setColor(m_textElement->m_backgroundColor);
0060     ui.BackgroundTransparency->setValue(m_textElement->m_backgroundTransparency);
0061     ui.FillBackground->setChecked(m_textElement->m_fillBackground);
0062 
0063     fontChanged(m_textElement->m_textFont);
0064     colorChanged(m_textElement->m_textColor);
0065 
0066     if (!m_textElement->m_text.isEmpty()) {
0067         ui.Text->setHtml(m_textElement->m_text);
0068     }
0069 }
0070 
0071 void TextElementDlg::hideEvent(QHideEvent *event)
0072 {
0073     KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).writeEntry(QStringLiteral("TextElementDlg"), size());
0074 
0075     QDialog::hideEvent(event);
0076 }
0077 
0078 void TextElementDlg::showEvent(QShowEvent *event)
0079 {
0080     QDialog::showEvent(event);
0081 
0082     if (KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).hasKey(QStringLiteral("TextElementDlg"))) {
0083         resize(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).readEntry(QStringLiteral("TextElementDlg"), QSize()));
0084     }
0085 }
0086 
0087 void TextElementDlg::on_FillBackground_toggled(bool checked)
0088 {
0089     ui.Text->setAutoFillBackground(checked);
0090     on_BackgroundColor_activated((checked) ? ui.BackgroundColor->color() : QWidget().palette().color(QPalette::Window));
0091 }
0092 
0093 void TextElementDlg::on_BackgroundColor_activated(const QColor &color)
0094 {
0095     QPalette pal = ui.Text->palette();
0096     pal.setColor(QPalette::Base, color);
0097     ui.Text->setPalette(pal);
0098 }
0099 
0100 void TextElementDlg::on_BackgroundTransparency_valueChanged(int value)
0101 {
0102     ui.Text->setWindowOpacity(value);
0103 }
0104 
0105 void TextElementDlg::on_BoldButton_clicked()
0106 {
0107     QTextCharFormat fmt;
0108     fmt.setFontWeight(ui.BoldButton->isChecked() ? QFont::Bold : QFont::Normal);
0109     mergeFormatOnWordOrSelection(fmt);
0110 }
0111 
0112 void TextElementDlg::on_UnderlineButton_clicked()
0113 {
0114     QTextCharFormat fmt;
0115     fmt.setFontUnderline(ui.UnderlineButton->isChecked());
0116     mergeFormatOnWordOrSelection(fmt);
0117 }
0118 
0119 void TextElementDlg::on_ItalicButton_clicked()
0120 {
0121     QTextCharFormat fmt;
0122     fmt.setFontItalic(ui.ItalicButton->isChecked());
0123     mergeFormatOnWordOrSelection(fmt);
0124 }
0125 
0126 void TextElementDlg::on_FontFamily_currentFontChanged(const QFont &font)
0127 {
0128     QTextCharFormat fmt;
0129     fmt.setFontFamily(font.family());
0130     mergeFormatOnWordOrSelection(fmt);
0131 }
0132 
0133 void TextElementDlg::on_PointSize_valueChanged(int size)
0134 {
0135     QTextCharFormat fmt;
0136     fmt.setFontPointSize(double(size));
0137     mergeFormatOnWordOrSelection(fmt);
0138 }
0139 
0140 void TextElementDlg::on_TextColor_clicked()
0141 {
0142     QColor color = QColorDialog::getColor(ui.Text->textColor(), this);
0143 
0144     if (color.isValid()) {
0145         QTextCharFormat fmt;
0146         fmt.setForeground(color);
0147         mergeFormatOnWordOrSelection(fmt);
0148         colorChanged(color);
0149     }
0150 }
0151 
0152 void TextElementDlg::textAlign(QAbstractButton *button)
0153 {
0154     if (button == ui.AlignLeft) {
0155         ui.Text->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
0156     } else if (button == ui.AlignHCenter) {
0157         ui.Text->setAlignment(Qt::AlignHCenter);
0158     } else if (button == ui.AlignRight) {
0159         ui.Text->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
0160     } else if (button == ui.AlignJustify) {
0161         ui.Text->setAlignment(Qt::AlignJustify);
0162     }
0163 }
0164 
0165 void TextElementDlg::on_Text_currentCharFormatChanged(const QTextCharFormat &format)
0166 {
0167     fontChanged(format.font());
0168     colorChanged(format.foreground().color());
0169 }
0170 
0171 void TextElementDlg::on_Text_cursorPositionChanged()
0172 {
0173     alignmentChanged(ui.Text->alignment());
0174 }
0175 
0176 void TextElementDlg::on_DialogButtonBox_accepted()
0177 {
0178     m_textElement->m_margins = QMargins(ui.MarginLeft->value(), ui.MarginTop->value(), ui.MarginRight->value(), ui.MarginBottom->value());
0179     m_textElement->m_showBorder = ui.ShowBorder->isChecked();
0180     m_textElement->m_borderColor = ui.BorderColor->color();
0181     m_textElement->m_borderThickness = int(ui.BorderThickness->value() * 10);
0182     m_textElement->m_fillBackground = ui.FillBackground->isChecked();
0183     m_textElement->m_backgroundColor = ui.BackgroundColor->color();
0184     m_textElement->m_backgroundTransparency = ui.BackgroundTransparency->value();
0185     m_textElement->m_text = ui.Text->toHtml();
0186 
0187     if (ui.AlignLeft->isChecked()) {
0188         m_textElement->m_alignment = Qt::AlignLeft;
0189     } else if (ui.AlignHCenter->isChecked()) {
0190         m_textElement->m_alignment = Qt::AlignHCenter;
0191     } else if (ui.AlignRight->isChecked()) {
0192         m_textElement->m_alignment = Qt::AlignRight;
0193     } else if (ui.AlignJustify->isChecked()) {
0194         m_textElement->m_alignment = Qt::AlignJustify;
0195     }
0196 
0197     m_textElement->m_textFont = ui.Text->font();
0198 
0199     accept();
0200 }
0201 
0202 void TextElementDlg::on_DialogButtonBox_rejected()
0203 {
0204     reject();
0205 }
0206 
0207 void TextElementDlg::on_DialogButtonBox_helpRequested()
0208 {
0209     KHelpClient::invokeHelp(QStringLiteral("TextElement"), QStringLiteral("kxstitch"));
0210 }
0211 
0212 void TextElementDlg::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
0213 {
0214     QTextCursor cursor = ui.Text->textCursor();
0215 
0216     if (!cursor.hasSelection()) {
0217         cursor.select(QTextCursor::WordUnderCursor);
0218     }
0219 
0220     cursor.mergeCharFormat(format);
0221     ui.Text->mergeCurrentCharFormat(format);
0222 }
0223 
0224 void TextElementDlg::fontChanged(const QFont &f)
0225 {
0226     ui.FontFamily->setCurrentFont(QFontInfo(f).family());
0227     ui.PointSize->setValue(f.pointSize());
0228     ui.BoldButton->setChecked(f.bold());
0229     ui.ItalicButton->setChecked(f.italic());
0230     ui.UnderlineButton->setChecked(f.underline());
0231 }
0232 
0233 void TextElementDlg::colorChanged(const QColor &color)
0234 {
0235     QPixmap pix(22, 22);
0236     pix.fill(color);
0237     ui.TextColor->setIcon(pix);
0238 }
0239 
0240 void TextElementDlg::alignmentChanged(Qt::Alignment align)
0241 {
0242     if (align & Qt::AlignLeft) {
0243         ui.AlignLeft->setChecked(true);
0244     } else if (align & Qt::AlignHCenter) {
0245         ui.AlignHCenter->setChecked(true);
0246     } else if (align & Qt::AlignRight) {
0247         ui.AlignRight->setChecked(true);
0248     } else if (align & Qt::AlignJustify) {
0249         ui.AlignJustify->setChecked(true);
0250     }
0251 }
0252 
0253 #include "moc_TextElementDlg.cpp"