File indexing completed on 2024-05-12 04:55:01

0001 /**
0002  * \file browserdialog.cpp
0003  * Help browser.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 10 Jun 2009
0008  *
0009  * Copyright (C) 2003-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "browserdialog.h"
0028 #include <QTextBrowser>
0029 #include <QLocale>
0030 #include <QDir>
0031 #include <QPushButton>
0032 #include <QVBoxLayout>
0033 #include <QLabel>
0034 #include <QLineEdit>
0035 #include <QToolButton>
0036 #include <QStyle>
0037 #include <QAction>
0038 #include "config.h"
0039 #include "loadtranslation.h"
0040 
0041 /**
0042  * Constructor.
0043  *
0044  * @param parent parent widget
0045  * @param caption dialog title
0046  */
0047 BrowserDialog::BrowserDialog(QWidget* parent, const QString& caption)
0048   : QDialog(parent)
0049 {
0050   setObjectName(QLatin1String("BrowserDialog"));
0051   setWindowTitle(caption);
0052   auto vlayout = new QVBoxLayout(this);
0053 
0054   QString docDir;
0055 #ifdef CFG_DOCDIR
0056   docDir = QLatin1String(CFG_DOCDIR);
0057   Utils::prependApplicationDirPathIfRelative(docDir);
0058 #endif
0059 
0060   QLocale locale;
0061   QStringList docPaths;
0062 #ifndef Q_OS_WIN32
0063   const auto uiLangs = locale.uiLanguages();
0064   for (const QString& uiLang : uiLangs) {
0065     QString lang(uiLang.left(2));
0066     docPaths += QDir::currentPath() + QLatin1String("/kid3_") + lang +
0067         QLatin1String(".html");
0068     if (!docDir.isNull()) {
0069       docPaths += docDir + QLatin1String("/kid3_") + lang + QLatin1String(".html"); // clazy:exclude=reserve-candidates
0070     }
0071   }
0072 #endif
0073   QString lang(locale.name().left(2));
0074   if (!docDir.isNull()) {
0075     docPaths += docDir + QLatin1String("/kid3_") + lang + QLatin1String(".html");
0076     docPaths += docDir + QLatin1String("/kid3_en.html");
0077   }
0078   docPaths += QDir::currentPath() + QLatin1String("/kid3_") + lang +
0079       QLatin1String(".html");
0080   docPaths += QDir::currentPath() + QLatin1String("/kid3_en.html");
0081   for (auto it = docPaths.constBegin(); it != docPaths.constEnd(); ++it) {
0082     m_filename = *it;
0083     if (QFile::exists(m_filename)) break;
0084   }
0085   m_textBrowser = new QTextBrowser(this);
0086   m_textBrowser->setOpenExternalLinks(true);
0087   m_textBrowser->setSource(QUrl::fromLocalFile(m_filename));
0088   vlayout->addWidget(m_textBrowser);
0089 
0090   auto hlayout = new QHBoxLayout;
0091   auto backButton = new QPushButton(tr("&Back"), this);
0092   backButton->setEnabled(false);
0093   connect(backButton, &QAbstractButton::clicked,
0094           m_textBrowser, &QTextBrowser::backward);
0095   connect(m_textBrowser, &QTextBrowser::backwardAvailable,
0096           backButton, &QWidget::setEnabled);
0097   hlayout->addWidget(backButton);
0098   auto forwardButton = new QPushButton(tr("&Forward"), this);
0099   forwardButton->setEnabled(false);
0100   connect(forwardButton, &QAbstractButton::clicked,
0101           m_textBrowser, &QTextBrowser::forward);
0102   connect(m_textBrowser, &QTextBrowser::forwardAvailable,
0103           forwardButton, &QWidget::setEnabled);
0104   hlayout->addWidget(forwardButton);
0105   auto findLabel = new QLabel(tr("&Find:"), this);
0106   hlayout->addWidget(findLabel);
0107   m_findLineEdit = new QLineEdit(this);
0108   m_findLineEdit->setFocus();
0109   findLabel->setBuddy(m_findLineEdit);
0110   connect(m_findLineEdit, &QLineEdit::returnPressed,
0111           this, &BrowserDialog::findNext);
0112   hlayout->addWidget(m_findLineEdit);
0113   auto findAction = new QAction(this);
0114   findAction->setShortcut(QKeySequence::Find);
0115   connect(findAction, &QAction::triggered,
0116           m_findLineEdit, static_cast<void (QWidget::*)()>(&QWidget::setFocus));
0117   m_findLineEdit->addAction(findAction);
0118   auto findPreviousAction = new QAction(this);
0119   findPreviousAction->setIcon(
0120         QIcon(style()->standardIcon(QStyle::SP_ArrowBack)));
0121   findPreviousAction->setText(tr("Find Previous"));
0122   findPreviousAction->setShortcut(QKeySequence::FindPrevious);
0123   connect(findPreviousAction, &QAction::triggered,
0124           this, &BrowserDialog::findPrevious);
0125   auto findPreviousButton = new QToolButton(this);
0126   findPreviousButton->setDefaultAction(findPreviousAction);
0127   hlayout->addWidget(findPreviousButton);
0128   auto findNextAction = new QAction(this);
0129   findNextAction->setIcon(
0130         QIcon(style()->standardIcon(QStyle::SP_ArrowForward)));
0131   findNextAction->setText(tr("Find Next"));
0132   findNextAction->setShortcut(QKeySequence::FindNext);
0133   connect(findNextAction, &QAction::triggered,
0134           this, &BrowserDialog::findNext);
0135   auto findNextButton = new QToolButton(this);
0136   findNextButton->setDefaultAction(findNextAction);
0137   hlayout->addWidget(findNextButton);
0138   hlayout->addStretch();
0139   auto closeButton = new QPushButton(tr("&Close"), this);
0140   closeButton->setAutoDefault(false);
0141   connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept);
0142   hlayout->addWidget(closeButton);
0143   vlayout->addLayout(hlayout);
0144   resize(500, 500);
0145 }
0146 
0147 /**
0148  * Display help document at anchor.
0149  *
0150  * @param anchor anchor
0151  */
0152 void BrowserDialog::goToAnchor(const QString& anchor)
0153 {
0154   QUrl url = QUrl::fromLocalFile(m_filename);
0155   url.setFragment(anchor);
0156   m_textBrowser->setSource(url);
0157 }
0158 
0159 /**
0160  * Find previous occurrence of search text.
0161  */
0162 void BrowserDialog::findPrevious()
0163 {
0164   m_textBrowser->find(m_findLineEdit->text(), QTextDocument::FindBackward);
0165 }
0166 
0167 /**
0168  * Find next occurrence of search text.
0169  */
0170 void BrowserDialog::findNext()
0171 {
0172   m_textBrowser->find(m_findLineEdit->text());
0173 }