File indexing completed on 2024-04-21 05:53:49

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2007 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "headerfooterframerenderer.hpp"
0010 
0011 // KF
0012 #include <KLocalizedString>
0013 #include <KUser>
0014 // Qt
0015 #include <QLocale>
0016 #include <QHash>
0017 #include <QDateTime>
0018 #include <QPainter>
0019 #include <QFontMetrics>
0020 #include <QRegularExpression>
0021 
0022 HeaderFooterFrameRenderer::HeaderFooterFrameRenderer(const PrintInfo* info)
0023     : mInfo(info)
0024 {
0025     calculateHeight();
0026 }
0027 
0028 HeaderFooterFrameRenderer::~HeaderFooterFrameRenderer() = default;
0029 
0030 int HeaderFooterFrameRenderer::height() const { return mHeight; }
0031 int HeaderFooterFrameRenderer::width() const { return mWidth; }
0032 
0033 void HeaderFooterFrameRenderer::setWidth(int width) { mWidth = width; }
0034 
0035 void HeaderFooterFrameRenderer::setTexts(const QString& leftText, const QString& centerText, const QString& rightText)
0036 {
0037     mOriginalTextList.clear();
0038     mOriginalTextList << leftText << centerText << rightText;
0039 }
0040 
0041 void HeaderFooterFrameRenderer::setBoxStyle(int boxStyle)
0042 {
0043     if (mBoxStyle == boxStyle) {
0044         return;
0045     }
0046     mBoxStyle = boxStyle;
0047     calculateHeight();
0048 }
0049 
0050 void HeaderFooterFrameRenderer::calculateHeight()
0051 {
0052     const QFontMetrics fontMetrics(mFont);
0053 
0054     mHeight = fontMetrics.height();
0055 
0056 //     const bool hasSpaceAbove = ( mBoxStyle & (BackgroundDrawn|LineAbove) );
0057 //     const bool hasSpacedBelow = ( mBoxStyle & (BackgroundDrawn|LineBelow) );
0058 //     if( !(hasSpaceAbove||hasSpacedBelow) )
0059     mHeight += fontMetrics.leading();
0060 //     else
0061 //     {
0062 //         if( hasSpaceAbove )
0063 //         {
0064 //         mHeight += mBoxMargin * 2;
0065 //
0066 //         }
0067 //     }
0068 }
0069 
0070 void HeaderFooterFrameRenderer::prepare()
0071 {
0072     const QDateTime dateTime = QDateTime::currentDateTime();
0073     const KUser user(KUser::UseRealUserID);
0074     const QUrl url = mInfo->url();
0075 
0076     // create list of replacements
0077     QHash<char, QString> tagReplacements;
0078 
0079     QLocale locale;
0080     tagReplacements['d'] = locale.toString(dateTime, QLocale::ShortFormat);
0081     tagReplacements['D'] = locale.toString(dateTime, QLocale::LongFormat);
0082     tagReplacements['h'] = locale.toString(dateTime.time(), QLocale::ShortFormat);
0083     tagReplacements['y'] = locale.toString(dateTime.date(), QLocale::ShortFormat);
0084     tagReplacements['Y'] = locale.toString(dateTime.date(), QLocale::LongFormat);
0085     tagReplacements['u'] = user.loginName();
0086     tagReplacements['U'] = user.property(KUser::FullName).toString();
0087 //     tagReplacements['f'] = isSelection ? i18n("(Selection of) %1", url.fileName()) : url.fileName();
0088     tagReplacements['f'] = url.fileName();
0089     tagReplacements['F'] = url.toDisplayString(QUrl::PrettyDecoded | QUrl::PreferLocalFile);
0090     tagReplacements['P'] = QString::number(mInfo->noOfPages());
0091 
0092     // create text with globally replaced tags
0093     const int sizeOfTag = 2;
0094     QRegularExpression tagsPattern(QStringLiteral("%([dDhyYuUfFP])"));
0095 
0096     mGloballyReplacedTextList.clear();
0097 
0098     for (int i = 0; i < 3; ++i) {
0099         QString text = mOriginalTextList.at(i);
0100         int pos = 0;
0101         while (true) {
0102             pos = text.indexOf(tagsPattern, pos);
0103             if (pos < 0) {
0104                 break;
0105             }
0106             QString replacement = tagReplacements[text[pos + 1].toLatin1()];
0107             text.replace((uint)pos, sizeOfTag, replacement);
0108             pos += replacement.length();
0109         }
0110         mGloballyReplacedTextList << text;
0111     }
0112 }
0113 
0114 void HeaderFooterFrameRenderer::renderFrame(QPainter* painter, int frameIndex)
0115 {
0116     painter->setPen(mFgColor);
0117     painter->setFont(mFont);
0118 //     if( mBoxStyle & BackgroundDrawn )
0119 //         painter->fillRect( 0, 0, mWidth, mHeight, mBgColor );
0120 
0121     const bool isTextFramed = false;// ( mBoxStyle & (Box|BackgroundDrawn) );
0122 
0123     const int horizontalAlign[3] = { Qt::AlignLeft, Qt::AlignHCenter, Qt::AlignRight };
0124     int verticalAlign = isTextFramed ? Qt::AlignVCenter : Qt::AlignTop;
0125     int margin = 0;// ( mBoxStyle & (mBoxDrawn || mBackgroundDrawn ) ? mBoxMargin : 0;
0126 //     if ( mBoxStyle & LinesAtSide )
0127 //         margin += mLineWidth;
0128     const int rightEnd = mWidth - (margin * 2);
0129 
0130     for (int i = 0; i < 3; i++) {
0131         QString text = mGloballyReplacedTextList[i];
0132         // substitute locally
0133         const QString pageNumberTag = QStringLiteral("%p");
0134         if (text.indexOf(pageNumberTag) != -1) {
0135             text.replace(pageNumberTag, QString::number(frameIndex + 1)); // TODO: frameIndex != pageNumber in general
0136         }
0137         int align = verticalAlign | horizontalAlign[i];
0138 
0139         painter->drawText(margin, 0, rightEnd, mHeight, align, text);
0140     }
0141 
0142 //     if ( !isTextFramed )
0143     {
0144 //         painter->drawLine( 0, mHeight-mLineWidth, mWidth, mHeight-mLineWidth );
0145 //         painter->drawLine( 0, maxHeight + mBoxMargin - 1, headerWidth, maxHeight + mBoxMargin - 1 );
0146 //         painter->drawRect( 0, 0, pdmWidth, pdmHeight);
0147 //             paint.drawLine(0, headerHeight, headerWidth, headerHeight);
0148     }
0149 }