File indexing completed on 2025-03-09 05:18:52

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 #include "mainpane.h"
0026 #include <QHBoxLayout>
0027 #include <memory>
0028 #include <QTextBrowser>
0029 #include <QPrintDialog>
0030 #include <QPrintPreviewDialog>
0031 #include <QPrinter>
0032 #include <QScrollBar>
0033 #include "recipedocument.h"
0034 
0035 MainPane::MainPane(QWidget* parent) : PaneBase(parent)
0036 {
0037     m_document = std::make_unique<QTextDocument>();
0038     auto layout = std::make_unique<QHBoxLayout>();
0039     auto textView = std::make_unique<QTextBrowser>();
0040     textView->setReadOnly(true);
0041     textView->setDocument(m_document.get());
0042     textView->setOpenExternalLinks(true);
0043     connect(textView.get(), QOverload<const QUrl&>::of(&QTextBrowser::highlighted), this, [this](const QUrl &url) {
0044         Q_EMIT notifySimple(url.toString());
0045     });
0046 
0047     m_textView = textView.get();
0048     layout->addWidget(textView.release());
0049     setLayout(layout.release());
0050     openPath(QString());
0051 }
0052 
0053 void MainPane::openPath(const QString& path)
0054 {
0055     m_document->setBaseUrl(QUrl::fromLocalFile(path));
0056     m_document->setMarkdown(RecipeDocument::openPath(path));
0057 
0058     QTextCursor cursor = m_textView->textCursor();
0059     cursor.setPosition(0);
0060     m_textView->setTextCursor(cursor);
0061 }
0062 
0063 void MainPane::print()
0064 {
0065     QPrintDialog d;
0066     if (d.exec() == QDialog::Accepted)
0067     {
0068         m_document->print(d.printer());
0069     }
0070 }
0071 
0072 void MainPane::printPreview()
0073 {
0074     QPrintPreviewDialog d;
0075     connect(&d, &QPrintPreviewDialog::paintRequested, this, [this](QPrinter* printer) {
0076         m_document->print(printer);
0077     });
0078     d.exec();
0079 }
0080 
0081 MainPane::~MainPane()
0082 {
0083     // for smart pointers
0084 }