File indexing completed on 2024-05-05 17:59:06

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