File indexing completed on 2024-04-28 04:32:09

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 
0011 #include "TextToolDlg.h"
0012 
0013 #include <QBitmap>
0014 #include <QPainter>
0015 #include <QPen>
0016 #include <QPushButton>
0017 
0018 #include <KHelpClient>
0019 #include <KLocalizedString>
0020 
0021 #include "configuration.h"
0022 
0023 TextToolDlg::TextToolDlg(QWidget *parent)
0024     : QDialog(parent)
0025 {
0026     setWindowTitle(i18n("Text Tool"));
0027     ui.setupUi(this);
0028 
0029     ui.TextToolFont->setCurrentFont(QFont(Configuration::textTool_FontFamily()));
0030     ui.TextToolSize->setValue(Configuration::textTool_FontSize());
0031     ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0032 }
0033 
0034 QImage TextToolDlg::image()
0035 {
0036     QBitmap bitmap(1000, 100);
0037     QPainter painter;
0038 
0039     painter.begin(&bitmap);
0040     QFont font = painter.font();
0041     font.setFamily(m_font.family());
0042     font.setPixelSize(m_size);
0043     painter.setFont(font);
0044     painter.setPen(QPen(Qt::color0));
0045     QRect boundingRect = painter.boundingRect(0, 0, 1000, 100, Qt::AlignLeft | Qt::AlignTop, m_text);
0046     painter.end();
0047 
0048     int boundingWidth = boundingRect.width();
0049     int boundingHeight = boundingRect.height();
0050     bitmap = bitmap.copy(0, 0, boundingWidth, boundingHeight); // a resize
0051     painter.begin(&bitmap);
0052     painter.setFont(font);
0053     bitmap.fill(Qt::color0);
0054     painter.setPen(QPen(Qt::color1));
0055     painter.drawText(0, 0, boundingWidth, boundingHeight, Qt::AlignLeft | Qt::AlignTop, m_text);
0056     painter.end();
0057 
0058     QImage image = bitmap.toImage();
0059 
0060     return image;
0061 }
0062 
0063 void TextToolDlg::hideEvent(QHideEvent *event)
0064 {
0065     KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).writeEntry(QStringLiteral("TextToolDlg"), size());
0066 
0067     QDialog::hideEvent(event);
0068 }
0069 
0070 void TextToolDlg::showEvent(QShowEvent *event)
0071 {
0072     QDialog::showEvent(event);
0073 
0074     if (KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).hasKey(QStringLiteral("PrintSetupDlg"))) {
0075         resize(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).readEntry(QStringLiteral("TextToolDlg"), QSize()));
0076     }
0077 }
0078 
0079 void TextToolDlg::on_TextToolFont_currentFontChanged(const QFont &font)
0080 {
0081     QFont f = ui.TextToolText->font();
0082     f.setFamily(font.family());
0083     ui.TextToolText->setFont(f);
0084 }
0085 
0086 void TextToolDlg::on_TextToolSize_valueChanged(int s)
0087 {
0088     QFont f = ui.TextToolText->font();
0089     f.setPointSize(s);
0090     ui.TextToolText->setFont(f);
0091 }
0092 
0093 void TextToolDlg::on_TextToolText_textChanged(const QString &s)
0094 {
0095     ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(!s.isEmpty());
0096 }
0097 
0098 void TextToolDlg::on_DialogButtonBox_accepted()
0099 {
0100     m_font = ui.TextToolFont->currentFont();
0101     m_text = ui.TextToolText->text();
0102     m_size = ui.TextToolSize->value();
0103 
0104     Configuration::setTextTool_FontFamily(ui.TextToolFont->currentFont().family());
0105     Configuration::setTextTool_FontSize(ui.TextToolSize->value());
0106 
0107     accept();
0108 }
0109 
0110 void TextToolDlg::on_DialogButtonBox_rejected()
0111 {
0112     reject();
0113 }
0114 
0115 void TextToolDlg::on_DialogButtonBox_helpRequested()
0116 {
0117     KHelpClient::invokeHelp(QStringLiteral("TextDialog"), QStringLiteral("kxstitch"));
0118 }
0119 
0120 #include "moc_TextToolDlg.cpp"