File indexing completed on 2024-04-21 03:41:58

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool...
0003     SPDX-FileCopyrightText: 2001 Jason Katz-Brown <jason@katzbrown.com>
0004     SPDX-FileCopyrightText: 2005 Paul Temple <paul.temple@gmx.net>
0005     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0006     SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "resultsview.h"
0012 #include "kitenconfig.h"
0013 #include <KColorScheme>
0014 #include <QScrollBar>
0015 
0016 ResultsView::ResultsView(QWidget *parent, const char *name)
0017     : QTextBrowser(parent)
0018     , _scrollValue(0)
0019 {
0020     Q_UNUSED(name)
0021     setOpenLinks(false);
0022     connect(this, &QTextBrowser::anchorClicked, this, [=](const QUrl &url) {
0023         Q_EMIT urlClicked(url.toString());
0024     });
0025 }
0026 
0027 /**
0028  * As the name implies, it appends @param text to the printText
0029  */
0030 void ResultsView::append(const QString &text)
0031 {
0032     _printText.append(text);
0033 }
0034 
0035 /**
0036  * This clears the printText
0037  */
0038 void ResultsView::clear()
0039 {
0040     _printText.clear();
0041 }
0042 
0043 void ResultsView::doScroll()
0044 {
0045     verticalScrollBar()->setValue(_scrollValue);
0046 }
0047 
0048 /**
0049  * Flushes the printText to screen
0050  */
0051 void ResultsView::flush()
0052 {
0053     const QString content = QStringLiteral("<head><style>%1</style></head><body>%2</body>").arg(generateCSS()).arg(_printText);
0054     setHtml(content);
0055 }
0056 
0057 QString ResultsView::generateCSS()
0058 {
0059     KColorScheme scheme(QPalette::Active);
0060     QFont font = KitenConfigSkeleton::self()->font();
0061 
0062     return QStringLiteral(
0063                ".Word { font-size: %9px }"
0064                ".Entry { font-size: %8px; color: %1; font-family: \"%7\"; }"
0065                //"div.Entry {display: inline; }"
0066                ".DictionaryHeader { color: %2; border-bottom: solid %3 }"
0067                ".CommonHeader { color: %2; text-align:center }"
0068                ".UncommonHeader { color: %2; text-align:center }"
0069                "a{ text-decoration: none; }"
0070                "a:link { color: %4; }"
0071                "a:visited {color: %5} "
0072                "a:hover {color: %6 } "
0073                "a:active {color: %6}"
0074                ".odd { background-color: %10 }"
0075                "query { color: %6 }")
0076         .arg(scheme.foreground().color().name())
0077         .arg(scheme.foreground(KColorScheme::InactiveText).color().name())
0078         .arg(scheme.shade(KColorScheme::MidlightShade).name())
0079         .arg(scheme.foreground(KColorScheme::LinkText).color().name())
0080         .arg(scheme.foreground(KColorScheme::VisitedText).color().name())
0081         .arg(scheme.foreground(KColorScheme::ActiveText).color().name())
0082         .arg(font.family())
0083         .arg(font.pointSize()) // the text size
0084         .arg(font.pointSize() + 10) // a larger size for kanji
0085         .arg(scheme.background(KColorScheme::AlternateBackground).color().name());
0086 }
0087 
0088 /**
0089  * Prints the contents of the resultview to a
0090  * printer with @param title as the title.
0091  */
0092 void ResultsView::print(const QString &title)
0093 {
0094     Q_UNUSED(title)
0095     /* KDE4 CHANGE
0096     KPrinter printer;
0097     printer.setFullPage(true);
0098     if (printer.setup(this, i18n("Print Japanese Reference")))
0099     {
0100       QPainter p(&printer);
0101       //Q3PaintDeviceMetrics metrics(p.device());
0102       int dpix = metrics.logicalDpiX();
0103       int dpiy = metrics.logicalDpiY();
0104       const int margin = 72; // pt
0105 
0106       QRect body(dpix, dpiy, metrics.width() - margin * dpix / margin * 2,
0107                       metrics.height() - margin * dpiy / margin * 2);
0108 
0109       //Q3SimpleRichText richText(title.isNull()?
0110                       printText :
0111                       i18n("<h1>Search for \"%1\"</h1>",title) + printText,
0112                       font(), context(), styleSheet(), mimeSourceFactory(),
0113                       body.height(), Qt::black, false);
0114       richText.setWidth(&p, body.width());
0115       QRect view(body);
0116       int page = 1;
0117 
0118       QColorGroup goodColorGroup = QColorGroup(colorGroup());
0119       goodColorGroup.setColor(QColorGroup::Link, Qt::black);
0120 
0121       do
0122       {
0123         richText.draw(&p, body.left(), body.top(), view, goodColorGroup);
0124         view.moveBy(0, body.height());
0125         p.translate(0, -body.height());
0126 
0127         QFont myFont(font());
0128         myFont.setPointSize(9);
0129         p.setFont(myFont);
0130         QString footer(QString("%1 - Kiten").arg(QString::number(page)));
0131         p.drawText(view.right() - p.fontMetrics().width(footer),
0132                         view.bottom() + p.fontMetrics().ascent() + 5, footer);
0133 
0134         if (view.top() >= richText.height())
0135           break;
0136 
0137         printer.newPage();
0138         page++;
0139 
0140         qApp->processEvents();
0141       }
0142       while (true);
0143   }
0144   */
0145 }
0146 
0147 void ResultsView::setBasicMode(bool yes)
0148 {
0149     _basicMode = yes;
0150 }
0151 
0152 /**
0153  * Non-buffered write of contents to screen
0154  */
0155 void ResultsView::setContents(const QString &text)
0156 {
0157     const QString content = QStringLiteral("<head><style>%1</style></head><body>%2</body>").arg(generateCSS()).arg(text);
0158     setHtml(content);
0159     //  qDebug() << "Set HTML to " << content;
0160 }
0161 
0162 void ResultsView::setLaterScrollValue(int scrollValue)
0163 {
0164     this->_scrollValue = scrollValue;
0165 }
0166 
0167 #include "moc_resultsview.cpp"